read_netcdf_xy.c File Reference

Read NetCDF X and Y dimensions. More...

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

Go to the source code of this file.

Functions

int read_netcdf_xy (double **x, double **y, int *nx, int *ny, char *xname, char *yname, char *dimxname, char *dimyname, char *filename)
 Read X and Y dimensions in a NetCDF file.

Detailed Description

Read NetCDF X and Y dimensions.

Definition in file read_netcdf_xy.c.


Function Documentation

int read_netcdf_xy ( double **  x,
double **  y,
int *  nx,
int *  ny,
char *  xname,
char *  yname,
char *  dimxname,
char *  dimyname,
char *  filename 
)

Read X and Y dimensions in a NetCDF file.

Parameters:
[out] x X field
[out] y Y field
[out] nx X dimension
[out] ny Y dimension
[in] xname X variable name
[in] yname Y variable name
[in] dimxname X dimension name
[in] dimyname Y dimension name
[in] filename Input NetCDF filename
Returns:
Status.

1D dimensions y and x dimensions

Read dimensions variables

Definition at line 66 of file read_netcdf_xy.c.

References alloc_error(), and handle_netcdf_error().

Referenced by output_downscaled_analog().

00066                                                                                                                                    {
00081   int istat; /* Diagnostic status */
00082 
00083   size_t dimval; /* Variable used to retrieve dimension length */
00084 
00085   int ncinid; /* NetCDF input file handle ID */
00086   int yinid; /* Y variable ID */
00087   int xinid; /* X variable ID */
00088   nc_type vartype; /* Type of the variable (NC_FLOAT, NC_DOUBLE, etc.) */
00089   int varndims; /* Number of dimensions of variable */
00090   int vardimids[NC_MAX_VAR_DIMS]; /* Variable dimension ids */
00091   int xdiminid; /* X dimension ID */
00092   int ydiminid; /* Y dimension ID */
00093 
00094   size_t start[3]; /* Start position to read */
00095   size_t count[3]; /* Number of elements to read */
00096 
00097   int ndims; /* Number of dimensions of y and x variables */
00098 
00099   /* Read data in NetCDF file */
00100 
00101   /* Open NetCDF file for reading */
00102   printf("%s: Reading info from 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 
00107   ndims = 1;
00108   
00109   /* Get dimensions length */
00110   istat = nc_inq_dimid(ncinid, dimyname, &ydiminid);  /* get ID for Y dimension */
00111   if (istat != NC_NOERR) handle_netcdf_error(istat, __FILE__, __LINE__);
00112   istat = nc_inq_dimlen(ncinid, ydiminid, &dimval); /* get y length */
00113   if (istat != NC_NOERR) handle_netcdf_error(istat, __FILE__, __LINE__);
00114   *ny = (int) dimval;
00115   
00116   istat = nc_inq_dimid(ncinid, dimxname, &xdiminid);  /* get ID for X dimension */
00117   if (istat != NC_NOERR) handle_netcdf_error(istat, __FILE__, __LINE__);
00118   istat = nc_inq_dimlen(ncinid, xdiminid, &dimval); /* get x length */
00119   if (istat != NC_NOERR) handle_netcdf_error(istat, __FILE__, __LINE__);
00120   *nx = (int) dimval;
00121   
00122   /* Get dimension ID */
00123   istat = nc_inq_varid(ncinid, yname, &yinid);  /* get ID for y variable */
00124   if (istat != NC_NOERR) handle_netcdf_error(istat, __FILE__, __LINE__);
00125   /* Get y dimensions and type */
00126   istat = nc_inq_var(ncinid, yinid, (char *) NULL, &vartype, &varndims, vardimids, (int *) NULL); /* get variable information */
00127   if (istat != NC_NOERR) handle_netcdf_error(istat, __FILE__, __LINE__);  
00128   if (varndims != ndims) {
00129     (void) fprintf(stderr, "Error NetCDF type and/or dimensions %d != %d.\n", varndims, ndims);
00130     istat = ncclose(ncinid);
00131     if (istat != NC_NOERR) handle_netcdf_error(istat, __FILE__, __LINE__);
00132     return -1;
00133   }
00134 
00135   /* Get dimension ID */
00136   istat = nc_inq_varid(ncinid, xname, &xinid);  /* get ID for x variable */
00137   if (istat != NC_NOERR) handle_netcdf_error(istat, __FILE__, __LINE__);
00138   /* Get x dimensions and type */
00139   istat = nc_inq_var(ncinid, xinid, (char *) NULL, &vartype, &varndims, vardimids, (int *) NULL); /* get variable information */
00140   if (istat != NC_NOERR) handle_netcdf_error(istat, __FILE__, __LINE__);  
00141   if (varndims != ndims) {
00142     (void) fprintf(stderr, "Error NetCDF type and/or dimensions %d != %d.\n", varndims, ndims);
00143     istat = ncclose(ncinid);
00144     if (istat != NC_NOERR) handle_netcdf_error(istat, __FILE__, __LINE__);
00145     return -1;
00146   }
00147 
00149   /* Allocate memory and set start and count */
00150   start[0] = 0;
00151   start[1] = 0;
00152   start[2] = 0;
00153   count[0] = (size_t) (*ny);
00154   count[1] = 0;
00155   count[2] = 0;
00156   (*y) = (double *) malloc((*ny) * sizeof(double));
00157   if ((*y) == NULL) alloc_error(__FILE__, __LINE__);
00158   
00159   /* Read values from netCDF variable */
00160   istat = nc_get_vara_double(ncinid, yinid, start, count, *y);
00161   if (istat != NC_NOERR) handle_netcdf_error(istat, __FILE__, __LINE__);
00162   
00163   /* Allocate memory and set start and count */
00164   start[0] = 0;
00165   start[1] = 0;
00166   start[2] = 0;
00167   count[0] = (size_t) (*nx);
00168   count[1] = 0;
00169   count[2] = 0;
00170   (*x) = (double *) malloc((*nx) * sizeof(double));
00171   if ((*x) == NULL) alloc_error(__FILE__, __LINE__);
00172 
00173   /* Read values from netCDF variable */
00174   istat = nc_get_vara_double(ncinid, xinid, start, count, *x);
00175   if (istat != NC_NOERR) handle_netcdf_error(istat, __FILE__, __LINE__);
00176 
00177   istat = ncclose(ncinid);
00178   if (istat != NC_NOERR) handle_netcdf_error(istat, __FILE__, __LINE__);
00179 
00180   /* Success status */
00181   return 0;
00182 }


Generated on 12 May 2016 for DSCLIM by  doxygen 1.6.1