read_netcdf_var_generic_val.c File Reference

Read a NetCDF variable. More...

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

Go to the source code of this file.

Functions

int read_netcdf_var_generic_val (double *buf, info_field_struct *info_field, char *filename, char *varname, int index)
 Read a NetCDF variable scalar double at the index position, and return information in info_field_struct structure.

Detailed Description

Read a NetCDF variable.

Definition in file read_netcdf_var_generic_val.c.


Function Documentation

int read_netcdf_var_generic_val ( double *  buf,
info_field_struct info_field,
char *  filename,
char *  varname,
int  index 
)

Read a NetCDF variable scalar double at the index position, and return information in info_field_struct structure.

Parameters:
[out] buf Scalar double value
[out] info_field Information about the output variable
[in] filename NetCDF input filename
[in] varname NetCDF variable name
[in] index Index position in the variable to retrieve
Returns:
Status.

Definition at line 66 of file read_netcdf_var_generic_val.c.

References alloc_error(), info_field_struct::fillvalue, handle_netcdf_error(), info_field_struct::height, info_field_struct::long_name, MAXPATH, and info_field_struct::units.

Referenced by read_learning_fields().

00066                                                                                                                   {
00077   int istat; /* Diagnostic status */
00078 
00079   int ncinid; /* NetCDF input file handle ID */
00080   int varinid; /* NetCDF variable ID */
00081   nc_type vartype_main; /* Type of the variable (NC_FLOAT, NC_DOUBLE, etc.) */
00082   int varndims; /* Number of dimensions of variable */
00083   int vardimids[NC_MAX_VAR_DIMS]; /* Variable dimension ids */
00084 
00085   float valf; /* Variable used to retrieve fillvalue */
00086   char *tmpstr = NULL; /* Temporary string */
00087   size_t t_len; /* Length of string attribute */
00088   size_t *idx; /* Index position */
00089 
00090   /* Allocate memory and set index to input parameter value */
00091   idx = (size_t *) malloc(sizeof(size_t));
00092   if (idx == NULL) alloc_error(__FILE__, __LINE__);
00093   idx[0] = (size_t) index;
00094 
00095   /* Allocate memory */
00096   tmpstr = (char *) malloc(MAXPATH * sizeof(char));
00097   if (tmpstr == NULL) alloc_error(__FILE__, __LINE__);
00098 
00099   /* Read data in NetCDF file */
00100 
00101   /* Open NetCDF file for reading */
00102   printf("%s: Opening for reading NetCDF input file %s\n", __FILE__, filename);
00103   istat = nc_open(filename, NC_NOWRITE, &ncinid);  /* open for reading */
00104   if (istat != NC_NOERR) handle_netcdf_error(istat, __FILE__, __LINE__);
00105 
00106   printf("%s: READ %s %s\n", __FILE__, varname, filename);
00107 
00108   /* Get main variable ID */
00109   istat = nc_inq_varid(ncinid, varname, &varinid);
00110   if (istat != NC_NOERR) handle_netcdf_error(istat, __FILE__, __LINE__);
00111 
00112   /* Get variable information */
00113   istat = nc_inq_var(ncinid, varinid, (char *) NULL, &vartype_main, &varndims, vardimids, (int *) NULL);
00114   if (istat != NC_NOERR) handle_netcdf_error(istat, __FILE__, __LINE__);
00115 
00116   /* If info_field si not NULL, get some information about the read variable */
00117   if (info_field != NULL) {
00118     /* Get missing value */
00119     if (vartype_main == NC_FLOAT) {
00120       istat = nc_get_att_float(ncinid, varinid, "missing_value", &valf);
00121       if (istat != NC_NOERR)
00122         info_field->fillvalue = -9999.0;
00123       else
00124         info_field->fillvalue = (double) valf;
00125     }
00126     else if (vartype_main == NC_DOUBLE) {
00127       istat = nc_get_att_double(ncinid, varinid, "missing_value", &(info_field->fillvalue));
00128       if (istat != NC_NOERR)
00129         info_field->fillvalue = -9999.0;
00130     }
00131 
00132     /* Get units */
00133     istat = nc_inq_attlen(ncinid, varinid, "units", &t_len);
00134     if (istat == NC_NOERR) {
00135       handle_netcdf_error(istat, __FILE__, __LINE__);
00136       istat = nc_get_att_text(ncinid, varinid, "units", tmpstr);
00137       if (istat == NC_NOERR) {
00138         if (tmpstr[t_len-1] != '\0')
00139           tmpstr[t_len] = '\0';
00140         info_field->units = strdup(tmpstr);
00141       }
00142       else
00143         info_field->units = strdup("unknown");
00144     }
00145     else
00146       info_field->units = strdup("unknown");
00147 
00148     /* Get height */
00149     istat = nc_inq_attlen(ncinid, varinid, "height", &t_len);
00150     if (istat == NC_NOERR) {
00151       handle_netcdf_error(istat, __FILE__, __LINE__);
00152       istat = nc_get_att_text(ncinid, varinid, "height", tmpstr);
00153       if (istat == NC_NOERR) {
00154         if (tmpstr[t_len-1] != '\0')
00155           tmpstr[t_len] = '\0';
00156         info_field->height = strdup(tmpstr);
00157       }
00158       else
00159         info_field->height = strdup("unknown");
00160     }
00161     else
00162       info_field->height = strdup("unknown");
00163 
00164     /* Get long name */
00165     istat = nc_inq_attlen(ncinid, varinid, "long_name", &t_len);
00166     if (istat == NC_NOERR) {
00167       handle_netcdf_error(istat, __FILE__, __LINE__);
00168       istat = nc_get_att_text(ncinid, varinid, "long_name", tmpstr);
00169       if (istat == NC_NOERR) {
00170         if (tmpstr[t_len-1] != '\0')
00171           tmpstr[t_len] = '\0';
00172         info_field->long_name = strdup(tmpstr);
00173       }
00174       else
00175         info_field->long_name = strdup(varname);
00176     }
00177     else
00178       info_field->long_name = strdup(varname);
00179   }
00180 
00181   /* Read values from netCDF variable */
00182   istat = nc_get_var1_double(ncinid, varinid, idx, buf);
00183   if (istat != NC_NOERR) handle_netcdf_error(istat, __FILE__, __LINE__);
00184 
00185   /* Close the input netCDF file. */
00186   istat = ncclose(ncinid);
00187   if (istat != NC_NOERR) handle_netcdf_error(istat, __FILE__, __LINE__);
00188 
00189   /* Free memory */
00190   (void) free(tmpstr);
00191   (void) free(idx);
00192 
00193   /* Success status */
00194   return 0;
00195 }


Generated on 12 May 2016 for DSCLIM by  doxygen 1.6.1