read_netcdf_dims_eof.c File Reference

Read NetCDF dimensions for EOF. More...

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

Go to the source code of this file.

Functions

int read_netcdf_dims_eof (double **lon, double **lat, int *nlon, int *nlat, int *neof, char *coords, char *lonname, char *latname, char *dimxname, char *dimyname, char *eofname, char *filename)
 Read dimensions in a NetCDF file for EOF data.

Detailed Description

Read NetCDF dimensions for EOF.

Definition in file read_netcdf_dims_eof.c.


Function Documentation

int read_netcdf_dims_eof ( double **  lon,
double **  lat,
int *  nlon,
int *  nlat,
int *  neof,
char *  coords,
char *  lonname,
char *  latname,
char *  dimxname,
char *  dimyname,
char *  eofname,
char *  filename 
)

Read dimensions in a NetCDF file for EOF data.

Parameters:
[out] lon Longitude field
[out] lat Latitude field
[out] nlon Longitude dimension
[out] nlat Latitude dimension
[out] neof EOF dimension
[in] coords Coordinates arrangement of latitude and longitude data: either 1D or 2D
[in] lonname Longitude variable name
[in] latname Latitude variable name
[in] dimxname Longitude dimension name
[in] dimyname Latitude dimension name
[in] eofname EOF dimension name
[in] filename Input NetCDF filename
Returns:
Status.

1D dimensions lat & lon

2D lat & lon variables

Read dimensions variables

Definition at line 66 of file read_netcdf_dims_eof.c.

References alloc_error(), and handle_netcdf_error().

Referenced by read_large_scale_eof().

00067                                                                                                                   {
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