write_netcdf_var_3d_2d.c File Reference

Write a 2D field in a 3D NetCDF variable. More...

#include <io.h>
Include dependency graph for write_netcdf_var_3d_2d.c:

Go to the source code of this file.

Functions

int write_netcdf_var_3d_2d (double *buf, double *timein, double fillvalue, char *filename, char *varname, char *longname, char *units, char *height, char *gridname, char *lonname, char *latname, char *timename, int t, int newfile, int format, int compression_level, int nlon, int nlat, int ntime, int outinfo)
 Write a 2D field in a 3D NetCDF variable.

Detailed Description

Write a 2D field in a 3D NetCDF variable.

Definition in file write_netcdf_var_3d_2d.c.


Function Documentation

int write_netcdf_var_3d_2d ( double *  buf,
double *  timein,
double  fillvalue,
char *  filename,
char *  varname,
char *  longname,
char *  units,
char *  height,
char *  gridname,
char *  lonname,
char *  latname,
char *  timename,
int  t,
int  newfile,
int  format,
int  compression_level,
int  nlon,
int  nlat,
int  ntime,
int  outinfo 
)

Write a 2D field in a 3D NetCDF variable.

Parameters:
[in] buf 3D Field to write
[in] timein Time dimension value
[in] fillvalue Missing value
[in] filename Output NetCDF filename
[in] varname Variable name in the NetCDF file
[in] longname Variable long name in the NetCDF file
[in] units Variable units in the NetCDF file
[in] height Variable height in the NetCDF file
[in] gridname Grid type name in the NetCDF file
[in] lonname Longitude name dimension in the NetCDF file
[in] latname Latitude name dimension in the NetCDF file
[in] timename Time name dimension in the NetCDF file
[in] t Time index of value to write
[in] newfile TRUE is new NetCDF file, FALSE if not
[in] format Format of NetCDF file
[in] compression_level Compression level of NetCDF file (only for NetCDF-4: format==4)
[in] outinfo TRUE if we want information output, FALSE if not
[in] nlon Longitude dimension
[in] nlat Latitude dimension
[in] ntime Time dimension
Returns:
Status.

Open already existing output file

Definition at line 68 of file write_netcdf_var_3d_2d.c.

References alloc_error(), handle_netcdf_error(), MAXPATH, and TRUE.

Referenced by output_downscaled_analog().

00072                                                                    {
00098   int istat; /* Diagnostic status */
00099 
00100   size_t dimval; /* Temporary variable used to get values from dimension lengths */
00101 
00102   int ncoutid; /* NetCDF output file handle ID */
00103   int varoutid; /* NetCDF variable output ID */
00104   int timedimoutid; /* NetCDF time dimension output ID */
00105   int timeid; /* NetCDF time variable ID */
00106   int londimoutid; /* NetCDF longitude dimension output ID */
00107   int latdimoutid; /* NetCDF latitude dimension output ID */
00108   int vardimids[NC_MAX_VAR_DIMS]; /* NetCDF dimension IDs */
00109   //  size_t chunksize[NC_MAX_VAR_DIMS]; /* Chunksize */
00110   //  size_t cachesize; /* HDF5 cache size */
00111   //  size_t cache_nelems = 2000; /* HDF5 cache number of elements */
00112   //  float cache_preemp;
00113 
00114   int ntime_file; /* Time dimension in NetCDF output file */
00115   int nlat_file; /* Latitude dimension in NetCDF output file */
00116   int nlon_file; /* Longitude dimension in NetCDF output file */
00117 
00118   size_t start[3]; /* Start element when writing */
00119   size_t count[3]; /* Count of elements to write */
00120 
00121   char *attname = NULL; /* Attribute name */
00122   char *tmpstr = NULL; /* Temporary string */
00123 
00124   /* Allocate memory */
00125   attname = (char *) malloc(MAXPATH * sizeof(char));
00126   if (attname == NULL) alloc_error(__FILE__, __LINE__);
00127 
00128   /* Change directory to output directory for autofs notification */
00129   tmpstr = strdup(filename);
00130   istat = chdir(dirname(tmpstr));
00131   (void) free(tmpstr);
00132 
00133   /*  if (format == 4 && compression_level > 0) {
00134     if ( !strcmp(gridname, "list") )
00135       cachesize = (size_t) nlon*sizeof(float)*cache_nelems;
00136     else
00137       cachesize = (size_t) nlat*nlon*sizeof(float)*cache_nelems;
00138     istat = nc_get_chunk_cache(&cachesize, &cache_nelems, &cache_preemp);
00139     cache_preemp = 0.75;
00140     cachesize=128000000;
00141     printf("%d %d %f\n",(int)cachesize,(int)cache_nelems,cache_preemp);
00142     istat = nc_set_chunk_cache(cachesize, cache_nelems, cache_preemp);
00143     if (istat != NC_NOERR) handle_netcdf_error(istat, __FILE__, __LINE__);
00144     } */
00145 
00147   istat = nc_open(filename, NC_WRITE, &ncoutid);
00148   if (istat != NC_NOERR) handle_netcdf_error(istat, __FILE__, __LINE__);  
00149 
00150   /* Get dimension lengths */
00151   istat = nc_inq_dimid(ncoutid, timename, &timedimoutid);  /* get ID for time dimension */
00152   if (istat != NC_NOERR) handle_netcdf_error(istat, __FILE__, __LINE__);
00153   istat = nc_inq_dimlen(ncoutid, timedimoutid, &dimval); /* get time length */
00154   if (istat != NC_NOERR) handle_netcdf_error(istat, __FILE__, __LINE__);
00155   ntime_file = (int) dimval;
00156 
00157   istat = nc_inq_varid(ncoutid, timename, &timeid);  /* get ID for time variable */
00158   if (istat != NC_NOERR) handle_netcdf_error(istat, __FILE__, __LINE__);
00159 
00160   istat = nc_inq_dimid(ncoutid, latname, &latdimoutid);  /* get ID for lat dimension */
00161   if (istat != NC_NOERR) handle_netcdf_error(istat, __FILE__, __LINE__);
00162   istat = nc_inq_dimlen(ncoutid, latdimoutid, &dimval); /* get lat length */
00163   if (istat != NC_NOERR) handle_netcdf_error(istat, __FILE__, __LINE__);
00164   nlat_file = (int) dimval;
00165 
00166   istat = nc_inq_dimid(ncoutid, lonname, &londimoutid);  /* get ID for lon dimension */
00167   if (istat != NC_NOERR) handle_netcdf_error(istat, __FILE__, __LINE__);
00168   istat = nc_inq_dimlen(ncoutid, londimoutid, &dimval); /* get lon length */
00169   if (istat != NC_NOERR) handle_netcdf_error(istat, __FILE__, __LINE__);
00170   nlon_file = (int) dimval;
00171 
00172   /* Verify that they match the provided ones in parameters */
00173   if ( !strcmp(gridname, "list") ) {
00174     if ( ((nlat_file != nlon) || (nlon_file != nlon) )) {
00175       (void) fprintf(stderr, "%s: Error NetCDF type and/or dimensions.\n", __FILE__);
00176       return -1;
00177     }
00178   }
00179   else {
00180     if ( ((nlat_file != nlat) || (nlon_file != nlon) )) {
00181       (void) fprintf(stderr, "%s: Error NetCDF type and/or dimensions.\n", __FILE__);
00182       return -1;
00183     }
00184   }
00185 
00186   /* Go into NetCDF define mode only if first element */
00187   if (newfile == TRUE) {
00188     istat = nc_redef(ncoutid);
00189     if (istat != NC_NOERR) handle_netcdf_error(istat, __FILE__, __LINE__);
00190     
00191     /* Define main output variable */
00192     vardimids[0] = timedimoutid;
00193     if ( !strcmp(gridname, "list") ) {
00194       vardimids[1] = londimoutid;
00195       istat = nc_def_var(ncoutid, varname, NC_FLOAT, 2, vardimids, &varoutid);  
00196     }
00197     else {
00198       vardimids[1] = latdimoutid;
00199       vardimids[2] = londimoutid;
00200       istat = nc_def_var(ncoutid, varname, NC_FLOAT, 3, vardimids, &varoutid);  
00201     }
00202     if (istat != NC_NOERR) handle_netcdf_error(istat, __FILE__, __LINE__);
00203 
00204 #ifdef NC_NETCDF4
00205     if (format == 4 && compression_level > 0) {
00206       /* Set up compression level */
00207       istat = nc_def_var_deflate(ncoutid, varoutid, 0, 1, compression_level);
00208       if (istat != NC_NOERR) handle_netcdf_error(istat, __FILE__, __LINE__);
00209       /* Set up chunking */
00210       /*      if ( !strcmp(gridname, "list") ) {
00211         chunksize[0] = (size_t) 1;
00212         chunksize[1] = (size_t) nlon;
00213         chunksize[2] = (size_t) 0;
00214       }
00215       else {
00216         chunksize[0] = (size_t) 1;
00217         chunksize[1] = (size_t) nlat;
00218         chunksize[2] = (size_t) nlon;
00219       }
00220       istat = nc_def_var_chunking(ncoutid, varoutid, NC_CHUNKED, chunksize);
00221       if (istat != NC_NOERR) handle_netcdf_error(istat, __FILE__, __LINE__);*/
00222     }
00223 #endif
00224 
00225     /* Set main variable attributes */
00226     (void) strcpy(attname, "_FillValue");
00227     istat = nc_put_att_double(ncoutid, varoutid, attname, NC_FLOAT, 1, &fillvalue);
00228     if (istat != NC_NOERR) handle_netcdf_error(istat, __FILE__, __LINE__);
00229     
00230     (void) strcpy(attname, "missing_value");
00231     istat = nc_put_att_double(ncoutid, varoutid, attname, NC_FLOAT, 1, &fillvalue);
00232     if (istat != NC_NOERR) handle_netcdf_error(istat, __FILE__, __LINE__);
00233     
00234     tmpstr = (char *) malloc(100 * sizeof(char));
00235     if (tmpstr == NULL) alloc_error(__FILE__, __LINE__);
00236     istat = nc_put_att_text(ncoutid, varoutid, "long_name", strlen(longname), longname);
00237     istat = nc_put_att_text(ncoutid, varoutid, "grid_mapping", strlen(gridname), gridname);
00238     istat = nc_put_att_text(ncoutid, varoutid, "units", strlen(units), units);
00239     istat = nc_put_att_text(ncoutid, varoutid, "height", strlen(height), height);
00240     istat = sprintf(tmpstr, "lon lat");
00241     istat = nc_put_att_text(ncoutid, varoutid, "coordinates", strlen(tmpstr), tmpstr);
00242     (void) free(tmpstr);
00243     
00244     /* End definition mode */
00245     istat = nc_enddef(ncoutid);
00246     if (istat != NC_NOERR) handle_netcdf_error(istat, __FILE__, __LINE__);
00247   }
00248   else {
00249     istat = nc_inq_varid(ncoutid, varname, &varoutid);
00250     if (istat != NC_NOERR) handle_netcdf_error(istat, __FILE__, __LINE__);
00251   }
00252 
00253   /* Write time dimension variable to NetCDF output file */
00254   start[0] = ntime_file;
00255   start[1] = 0;
00256   start[2] = 0;
00257   count[0] = (size_t) 1;
00258   count[1] = 0;
00259   count[2] = 0;
00260   istat = nc_put_vara_double(ncoutid, timeid, start, count, &(timein[t]));
00261   if (istat != NC_NOERR) handle_netcdf_error(istat, __FILE__, __LINE__);
00262 
00263   /* Write variable to NetCDF output file */
00264   start[0] = ntime_file;
00265   start[1] = 0;
00266   start[2] = 0;
00267   count[0] = (size_t) 1;
00268   if ( !strcmp(gridname, "list") ) {
00269     count[1] = (size_t) nlon;
00270     count[2] = 0;
00271   }
00272   else {
00273     count[1] = (size_t) nlat;
00274     count[2] = (size_t) nlon;
00275   }
00276   if (outinfo == TRUE)
00277     printf("%s: WRITE %s %s\n", __FILE__, varname, filename);
00278   istat = nc_put_vara_double(ncoutid, varoutid, start, count, buf);
00279   if (istat != NC_NOERR) handle_netcdf_error(istat, __FILE__, __LINE__);
00280 
00281   /* Close the output netCDF file. */
00282   istat = ncclose(ncoutid);
00283   if (istat != NC_NOERR) handle_netcdf_error(istat, __FILE__, __LINE__);
00284 
00285   /* Free memory */
00286   (void) free(attname);
00287 
00288   /* Diagnostic status */
00289   return 0;
00290 }


Generated on 12 May 2016 for DSCLIM by  doxygen 1.6.1