read_netcdf_dims_eof.c

Go to the documentation of this file.
00001 /* ***************************************************** */
00002 /* read_netcdf_dims_eof Read NetCDF dimensions for EOF.  */
00003 /* read_netcdf_dims_eof.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_dims_eof(double **lon, double **lat, int *nlon, int *nlat, int *neof, char *coords,
00067                      char *lonname, char *latname, char *dimxname, char *dimyname, char *eofname, char *filename) {
00085   int istat; /* Diagnostic status */
00086 
00087   size_t dimval; /* Variable used to retrieve dimension length */
00088 
00089   int ncinid; /* NetCDF input file handle ID */
00090   int latinid; /* Latitude variable ID */
00091   int loninid; /* Longitude variable ID */
00092   nc_type vartype; /* Type of the variable (NC_FLOAT, NC_DOUBLE, etc.) */
00093   int varndims; /* Number of dimensions of variable */
00094   int vardimids[NC_MAX_VAR_DIMS]; /* Variable dimension ids */
00095   int eofdiminid; /* EOF dimension ID */
00096   int londiminid; /* Longitude dimension ID */
00097   int latdiminid; /* Latitude dimension ID */
00098 
00099   size_t start[3]; /* Start position to read */
00100   size_t count[3]; /* Number of elements to read */
00101 
00102   double *tmpd = NULL; /* Temporary buffer to read variable from NetCDF file */
00103 
00104   int i; /* Loop counter */
00105   int j; /* Loop counter */
00106   int ndims; /* Number of dimensions of latitude and longitude variables, 1 or 2 for 1D and 2D respectively */
00107 
00108   /* Read data in NetCDF file */
00109 
00110   /* Open NetCDF file for reading */
00111   printf("%s: Reading info from NetCDF input file %s\n", __FILE__, filename);
00112   istat = nc_open(filename, NC_NOWRITE, &ncinid);  /* open for reading */
00113   if (istat != NC_NOERR) handle_netcdf_error(istat, __FILE__, __LINE__);
00114 
00115   if ( !strcmp(coords, "1D") ) {
00117     ndims = 1;
00118 
00119     /* Get dimensions length */
00120     istat = nc_inq_dimid(ncinid, dimyname, &latdiminid);  /* get ID for lat dimension */
00121     if (istat != NC_NOERR) handle_netcdf_error(istat, __FILE__, __LINE__);
00122     istat = nc_inq_dimlen(ncinid, latdiminid, &dimval); /* get lat length */
00123     if (istat != NC_NOERR) handle_netcdf_error(istat, __FILE__, __LINE__);
00124     *nlat = (int) dimval;
00125     
00126     istat = nc_inq_dimid(ncinid, dimxname, &londiminid);  /* get ID for lon dimension */
00127     if (istat != NC_NOERR) handle_netcdf_error(istat, __FILE__, __LINE__);
00128     istat = nc_inq_dimlen(ncinid, londiminid, &dimval); /* get lon length */
00129     if (istat != NC_NOERR) handle_netcdf_error(istat, __FILE__, __LINE__);
00130     *nlon = (int) dimval;
00131   }
00132   else {
00134     ndims = 2;
00135 
00136     /* Get dimensions length */
00137     istat = nc_inq_dimid(ncinid, dimyname, &latdiminid);  /* get ID for lat dimension */
00138     if (istat != NC_NOERR) handle_netcdf_error(istat, __FILE__, __LINE__);
00139     istat = nc_inq_dimlen(ncinid, latdiminid, &dimval); /* get lat length */
00140     if (istat != NC_NOERR) handle_netcdf_error(istat, __FILE__, __LINE__);
00141     *nlat = (int) dimval;
00142     
00143     istat = nc_inq_dimid(ncinid, dimxname, &londiminid);  /* get ID for lon dimension */
00144     if (istat != NC_NOERR) handle_netcdf_error(istat, __FILE__, __LINE__);
00145     istat = nc_inq_dimlen(ncinid, londiminid, &dimval); /* get lon length */
00146     if (istat != NC_NOERR) handle_netcdf_error(istat, __FILE__, __LINE__);
00147     *nlon = (int) dimval;
00148   }
00149   
00150   /* Get dimensions length */
00151   istat = nc_inq_dimid(ncinid, eofname, &eofdiminid);  /* get ID for eof dimension */
00152   if (istat != NC_NOERR) handle_netcdf_error(istat, __FILE__, __LINE__);
00153   istat = nc_inq_dimlen(ncinid, eofdiminid, &dimval); /* get eof length */
00154   if (istat != NC_NOERR) handle_netcdf_error(istat, __FILE__, __LINE__);
00155   *neof = (int) dimval;
00156 
00157   /* Get variables IDs */
00158   istat = nc_inq_varid(ncinid, latname, &latinid);  /* get ID for lat variable */
00159   if (istat != NC_NOERR) handle_netcdf_error(istat, __FILE__, __LINE__);
00160   /* Get lat dimensions and type */
00161   istat = nc_inq_var(ncinid, latinid, (char *) NULL, &vartype, &varndims, vardimids, (int *) NULL); /* get variable information */
00162   if (istat != NC_NOERR) handle_netcdf_error(istat, __FILE__, __LINE__);  
00163   if (varndims != ndims) {
00164     (void) fprintf(stderr, "Error NetCDF type and/or dimensions %d != %d.\n", varndims, ndims);
00165     istat = ncclose(ncinid);
00166     if (istat != NC_NOERR) handle_netcdf_error(istat, __FILE__, __LINE__);
00167     return -1;
00168   }
00169   istat = nc_inq_varid(ncinid, lonname, &loninid);  /* get ID for lon variable */
00170   if (istat != NC_NOERR) handle_netcdf_error(istat, __FILE__, __LINE__);
00171   /* Get lat dimensions and type */
00172   istat = nc_inq_var(ncinid, loninid, (char *) NULL, &vartype, &varndims, vardimids, (int *) NULL); /* get variable information */
00173   if (istat != NC_NOERR) handle_netcdf_error(istat, __FILE__, __LINE__);  
00174   if (varndims != ndims) {
00175     (void) fprintf(stderr, "Error NetCDF type and/or dimensions %d != %d.\n", varndims, ndims);
00176     istat = ncclose(ncinid);
00177     if (istat != NC_NOERR) handle_netcdf_error(istat, __FILE__, __LINE__);
00178     return -1;
00179   }
00180 
00182   if ( !strcmp(coords, "1D") ) {
00183     /* Allocate memory and set start and count */
00184     start[0] = 0;
00185     start[1] = 0;
00186     start[2] = 0;
00187     count[0] = (size_t) (*nlat);
00188     count[1] = 0;
00189     count[2] = 0;
00190     (*lat) = (double *) malloc((*nlat) * (*nlon) * sizeof(double));
00191     if ((*lat) == NULL) alloc_error(__FILE__, __LINE__);
00192     tmpd = (double *) malloc((*nlat) * sizeof(double));
00193     if (tmpd == NULL) alloc_error(__FILE__, __LINE__);
00194     
00195     /* Read values from netCDF variable */
00196     istat = nc_get_vara_double(ncinid, latinid, start, count, tmpd);
00197     if (istat != NC_NOERR) handle_netcdf_error(istat, __FILE__, __LINE__);
00198     for (j=0; j<(*nlat); j++)
00199       for (i=0; i<(*nlon); i++)
00200         (*lat)[i+j*(*nlon)] = tmpd[j];
00201     
00202     /* Allocate memory and set start and count */
00203     start[0] = 0;
00204     start[1] = 0;
00205     start[2] = 0;
00206     count[0] = (size_t) (*nlon);
00207     count[1] = 0;
00208     count[2] = 0;
00209     (*lon) = (double *) malloc((*nlat) * (*nlon) * sizeof(double));
00210     if ((*lon) == NULL) alloc_error(__FILE__, __LINE__);
00211     tmpd = (double *) realloc(tmpd, (*nlon) * sizeof(double));
00212     if (tmpd == NULL) alloc_error(__FILE__, __LINE__);
00213     
00214     /* Read values from netCDF variable */
00215     istat = nc_get_vara_double(ncinid, loninid, start, count, tmpd);
00216     if (istat != NC_NOERR) handle_netcdf_error(istat, __FILE__, __LINE__);
00217     for (j=0; j<(*nlat); j++)
00218       for (i=0; i<(*nlon); i++)
00219         (*lon)[i+j*(*nlon)] = tmpd[i];
00220 
00221     (void) free(tmpd);
00222     
00223   }
00224   else {  
00225     /* Allocate memory and set start and count */
00226     start[0] = 0;
00227     start[1] = 0;
00228     start[2] = 0;
00229     count[0] = (size_t) (*nlat);
00230     count[1] = (size_t) (*nlon);
00231     count[2] = 0;
00232     (*lat) = (double *) malloc((*nlat) * (*nlon) * sizeof(double));
00233     if ((*lat) == NULL) alloc_error(__FILE__, __LINE__);
00234     
00235     /* Read values from netCDF variable */
00236     istat = nc_get_vara_double(ncinid, latinid, start, count, (*lat));
00237     if (istat != NC_NOERR) handle_netcdf_error(istat, __FILE__, __LINE__);
00238     
00239     /* Allocate memory and set start and count */
00240     start[0] = 0;
00241     start[1] = 0;
00242     start[2] = 0;
00243     count[0] = (size_t) (*nlat);
00244     count[1] = (size_t) (*nlon);
00245     count[2] = 0;
00246     (*lon) = (double *) malloc((*nlat) * (*nlon) * sizeof(double));
00247     if ((*lon) == NULL) alloc_error(__FILE__, __LINE__);
00248     
00249     /* Read values from netCDF variable */
00250     istat = nc_get_vara_double(ncinid, loninid, start, count, (*lon));
00251     if (istat != NC_NOERR) handle_netcdf_error(istat, __FILE__, __LINE__);
00252   }
00253 
00254   istat = ncclose(ncinid);
00255   if (istat != NC_NOERR) handle_netcdf_error(istat, __FILE__, __LINE__);
00256 
00257   /* Success status */
00258   return 0;
00259 }

Generated on 12 May 2016 for DSCLIM by  doxygen 1.6.1