read_netcdf_var_generic_val.c

Go to the documentation of this file.
00001 /* ***************************************************** */
00002 /* read_netcdf_var_generic_val Read a 1D NetCDF variable.*/
00003 /* read_netcdf_var_generic_val.c                         */
00004 /* ***************************************************** */
00005 /* Author: Christian Page, CERFACS, Toulouse, France.    */
00006 /* ***************************************************** */
00007 /* Date of creation: oct 2008                            */
00008 /* Last date of modification: oct 2008                   */
00009 /* ***************************************************** */
00010 /* Original version: 1.0                                 */
00011 /* Current revision:                                     */
00012 /* ***************************************************** */
00013 /* Revisions                                             */
00014 /* ***************************************************** */
00019 /* LICENSE BEGIN
00020 
00021 Copyright Cerfacs (Christian Page) (2015)
00022 
00023 christian.page@cerfacs.fr
00024 
00025 This software is a computer program whose purpose is to downscale climate
00026 scenarios using a statistical methodology based on weather regimes.
00027 
00028 This software is governed by the CeCILL license under French law and
00029 abiding by the rules of distribution of free software. You can use, 
00030 modify and/ or redistribute the software under the terms of the CeCILL
00031 license as circulated by CEA, CNRS and INRIA at the following URL
00032 "http://www.cecill.info". 
00033 
00034 As a counterpart to the access to the source code and rights to copy,
00035 modify and redistribute granted by the license, users are provided only
00036 with a limited warranty and the software's author, the holder of the
00037 economic rights, and the successive licensors have only limited
00038 liability. 
00039 
00040 In this respect, the user's attention is drawn to the risks associated
00041 with loading, using, modifying and/or developing or reproducing the
00042 software by the user in light of its specific status of free software,
00043 that may mean that it is complicated to manipulate, and that also
00044 therefore means that it is reserved for developers and experienced
00045 professionals having in-depth computer knowledge. Users are therefore
00046 encouraged to load and test the software's suitability as regards their
00047 requirements in conditions enabling the security of their systems and/or 
00048 data to be ensured and, more generally, to use and operate it in the 
00049 same conditions as regards security. 
00050 
00051 The fact that you are presently reading this means that you have had
00052 knowledge of the CeCILL license and that you accept its terms.
00053 
00054 LICENSE END */
00055 
00056 
00057 
00058 
00059 
00060 
00061 
00062 #include <io.h>
00063 
00065 int
00066 read_netcdf_var_generic_val(double *buf, info_field_struct *info_field, char *filename, char *varname, int index) {
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