Go to the previous, next section.

Get Name of Attribute from Its Number

The function ncattname (or NCANAM for FORTRAN) gets the name of an attribute, given its variable ID and number. This function is useful in generic applications that need to get the names of all the attributes associated with a variable, since attributes are accessed by name rather than number in all other attribute functions. The number of an attribute is more volatile than the name, since it can change when other attributes of the same variable are deleted. This is why an attribute number is not called an attribute ID.

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

ncattname: C Interface

int ncattname (int ncid, int varid, int attnum, char* name);

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

varid
ID of the attribute's variable, or NC_GLOBAL for a global attribute.

attnum
Number of the attribute. The attributes for each variable are numbered from 0 (the first attribute) to nvatts-1, where nvatts is the number of attributes for the variable, as returned from a call to ncvarinq.

name
Returned attribute name. The caller must allocate space for the returned name. The maximum possible length, in characters, of an attribute name is given by the predefined constant MAX_NC_NAME. If the name parameter is given as (char *) 0, no name will be returned and no space needs to be allocated.

Here is an example using ncattname to determine the name of the first attribute of the variable rh in an existing netCDF file named `foo.nc':

#include "netcdf.h"
   ...
int  ncid;        /* netCDF ID */
int  rh_id;       /* variable ID */
char attname[MAX_NC_NAME];  /* maximum-size attribute name */
   ...
ncid = ncopen("foo.nc", NC_NOWRITE);
   ...
rh_id = ncvarid (ncid, "rh");
   ...
/* get name of first attribute (number 0) */
ncattname(ncid, rh_id, 0, attname);

NCANAM: FORTRAN Interface

      SUBROUTINE NCANAM (INTEGER NCID, INTEGER VARID,
     +                   INTEGER ATTNUM, CHARACTER*(*) ATTNAM,
     +                   INTEGER RCODE)

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

VARID
ID of the attribute's variable, or NCGLOBAL for a global attribute.

ATTNUM
Number of the attribute. The attributes for each variable are numbered from 1 (the first attribute) to NVATTS, where NVATTS is the number of attributes for the variable, as returned from a call to NCVINQ.

ATTNAM
Returned attribute name. The caller must allocate space for the returned name. The maximum possible length, in characters, of an attribute name is given by the predefined constant MAXNCNAM.

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

Here is an example using NCANAM determine the name of the first attribute of the variable rh in an existing netCDF file named `foo.nc':

      INCLUDE 'netcdf.inc'
         ...
      INTEGER  NCID     ! netCDF ID
      INTEGER  RHID      ! variable ID
* 31 in the following should be MAXNCNAM
      CHARACTER*31 ATTNAM
         ...
      NCID = NCOPN ('foo.nc', NCNOWRIT, RCODE)
         ...
      RHID = NCVID (NCID, 'rh', RCODE)
         ...
* get name of first attribute (number 1)
      CALL NCANAM (NCID, RHID, 1, ATTNAM, RCODE)

Go to the previous, next section.