6.16 Get a Variable ID from Its Name: nc_inq_varid
The function nc_inq_varid returns the ID of a netCDF variable, given
its name.
Usage
int nc_inq_varid (int ncid, const char *name, int *varidp);
ncid- NetCDF ID, from a previous call to nc_open or nc_create.
name- Variable name for which ID is desired.
varidp- Pointer to location for returned variable ID.
Errors
nc_inq_varid returns the value NC_NOERR if no errors
occurred. Otherwise, the returned status indicates an error. Possible
causes of errors include:
- The specified variable name is not a valid name for a variable in the
specified netCDF dataset.
- The specified netCDF ID does not refer to an open netCDF dataset.
Example
Here is an example using nc_inq_varid to find out the ID of a variable
named rh in an existing netCDF dataset named foo.nc:
#include <netcdf.h>
...
int status, ncid, rh_id;
...
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);