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.
The functions for types ubyte, ushort, uint, longlong, ulonglong, and string are only available for netCDF-4/HDF5 files.
The nc_get_vara() function will write a variable of any type, including user defined type. For this function, the type of the data in memory must match the type of the variable - no data conversion is done.
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); int nc_get_vara_ubyte (int ncid, int varid, const size_t *startp, const size_t *countp, unsigned char *ip); int nc_get_vara_ushort(int ncid, int varid, const size_t *startp, const size_t *countp, unsigned short *ip); int nc_get_vara_uint (int ncid, int varid, const size_t *startp, const size_t *countp, unsigned int *ip); int nc_get_vara_longlong(int ncid, int varid, const size_t *startp, const size_t *countp, long long *ip); int nc_get_vara_ulonglong(int ncid, int varid, const size_t *startp, const size_t *countp, unsigned long long *ip); int nc_get_vara_string(int ncid, int varid, const size_t *startp, const size_t *countp, char **ip); int nc_get_vara (int ncid, int varid, const size_t start[], const size_t count[], void *ip);
ncid
varid
start
count
Note: setting any element of the count array to zero causes the
function to exit without error, and without doing anything.
tp
up
cp
sp
ip
lp
fp
dp
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);