Go to the previous, next section.

Get a Variable ID from Its Name

The function ncvarid (or NCVID for FORTRAN) returns the ID of a netCDF variable, given its name.

In case of an error, ncvarid returns -1; NCVID returns a nonzero value in rcode. Possible causes of errors include:

ncvarid: C Interface

int ncvarid(int ncid, const char* name);

ncid
NetCDF ID, returned from a previous call to ncopen or nccreate.

name
Variable name for which ID is desired.

Here is an example using ncvarid to find out the ID of a variable named rh in an existing netCDF file named `foo.nc':

#include "netcdf.h"
   ...
int  ncid;                         /* netCDF ID */
int  rh_id;                        /* variable ID */
   ...
ncid = ncopen("foo.nc", NC_NOWRITE);
   ...
rh_id = ncvarid (ncid, "rh");

NCVID: FORTRAN Interface

      INTEGER FUNCTION NCVID(INTEGER NCID,
     +                       CHARACTER*(*) VARNAM,
     +                       INTEGER RCODE)

NCID
NetCDF ID, returned from a previous call to NCOPN or NCCRE.

VARNAM
Variable name for which ID is desired.

RCODE
Returned error code. If no errors occurred, 0 is returned.

Here is an example using NCVID to find out the ID of a variable named rh in an existing netCDF file named `foo.nc':

      INCLUDE 'netcdf.inc'
         ...
      INTEGER  NCID, RCODE
      INTEGER  RHID                    ! variable ID
         ...
      NCID = NCOPN ('foo.nc', NCNOWRIT, RCODE)
         ...
      RHID = NCVID (NCID, 'rh', RCODE)

Go to the previous, next section.