The members of the nc_get_vara_ type family of functions read an array of values from a netCDF variable of an open netCDF dataset. The array is specified by giving a corner and a vector of edge lengths. The values are read into consecutive locations with the last dimension varying fastest. The netCDF dataset must be in data mode.
int nc_get_vara_text (int ncid, int varid, const size_t start[], const size_t count[] char *tp); int nc_get_vara_uchar (int ncid, int varid, const size_t start[], const size_t count[] unsigned char *up); int nc_get_vara_schar (int ncid, int varid, const size_t start[], const size_t count[] signed char *cp); int nc_get_vara_short (int ncid, int varid, const size_t start[], const size_t count[] short *sp); int nc_get_vara_int (int ncid, int varid, const size_t start[], const size_t count[] int *ip); int nc_get_vara_long (int ncid, int varid, const size_t start[], const size_t count[] long *lp); int nc_get_vara_float (int ncid, int varid, const size_t start[], const size_t count[] float *fp); int nc_get_vara_double(int ncid, int varid, const size_t start[], const size_t count[] double *dp);
ncid
varid
start
count
tp
up
cp
sp
ip
lp
fp
dp
nc_get_vara_ type returns the value NC_NOERR if no errors occurred. Otherwise, the returned status indicates an error. Possible causes of errors include:
Here is an example using nc_get_vara_double to read all the values of the variable named rh from an existing netCDF dataset named foo.nc. For simplicity in this example, we assume that we know that rh is dimensioned with time, lat, and lon, and that there are three time values, five lat values, and ten lon values.
#include <netcdf.h> ... #define TIMES 3 #define LATS 5 #define LONS 10 int status; /* error status */ int ncid; /* netCDF ID */ int rh_id; /* variable ID */ static size_t start[] = {0, 0, 0}; /* start at first value */ static size_t count[] = {TIMES, LATS, LONS}; double rh_vals[TIMES*LATS*LONS]; /* array to hold values */ ... status = nc_open("foo.nc", NC_NOWRITE, &ncid); if (status != NC_NOERR) handle_error(status); ... status = nc_inq_varid (ncid, "rh", &rh_id); if (status != NC_NOERR) handle_error(status); ... /* read values from netCDF variable */ status = nc_get_vara_double(ncid, rh_id, start, count, rh_vals); if (status != NC_NOERR) handle_error(status);