Go to the previous, next section.

Get Information about an Attribute

The function ncattinq (or NCAINQ for FORTRAN) returns information about a netCDF attribute, given its variable ID and name. The information returned is the type and length of the attribute.

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

ncattinq: C Interface

int ncattinq(int ncid, int varid, const char* name,
             nc_type* datatype, int* len);

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

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

name
Attribute name.

datatype
Returned attribute type, one of the set of predefined netCDF data types. The type of this parameter, nc_type, is defined in the netCDF header file. The valid netCDF data types are NC_BYTE, NC_CHAR, NC_SHORT, NC_LONG, NC_FLOAT, and NC_DOUBLE. If this parameter is given as `(nc_type *) 0', no type will be returned so no variable to hold the type needs to be declared.

len
Returned number of values currently stored in the attribute. If the attribute is of type NC_CHAR, this is one more than the string length (since the terminating null character is stored). If this parameter is given as `(int *) 0', no length will be returned so no variable to hold this information needs to be declared.

Here is an example using ncattinq to find out the type and length of a variable attribute named valid_range for a netCDF variable named rh and a global attribute named title in an existing netCDF file named `foo.nc':

#include "netcdf.h"
   ...
int  ncid;                 /* netCDF ID */
int  rh_id;                /* variable ID */
nc_type vr_type, t_type;   /* attribute types */
int  vr_len, t_len;        /* attribute lengths *'

   ...
ncid = ncopen("foo.nc", NC_NOWRITE);
   ...
rh_id = ncvarid (ncid, "rh");
   ...
ncattinq (ncid, rh_id, "valid_range", &vr_type, &vr_len);
ncattinq (ncid, NC_GLOBAL, "title", &t_type, &t_len);
   ...

NCAINQ: FORTRAN Interface

      SUBROUTINE NCAINQ (INTEGER NCID, INTEGER VARID,
     +                   CHARACTER*(*) ATTNAM, INTEGER ATTYPE,
     +                   INTEGER ATTLEN,INTEGER RCODE)

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

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

ATTNAM
Attribute name.

ATTYPE
Returned attribute type, one of the set of predefined netCDF data types. The valid netCDF data types are NCBYTE, NCCHAR, NCSHORT, NCLONG, NCFLOAT, and NCDOUBLE.

ATTLEN
Returned number of values currently stored in the attribute. For a string-valued attribute, this is the number of characters in the string.

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

Here is an example using NCAINQ to add a variable attribute named valid_range for a netCDF variable named rh and a global attribute named title to an existing netCDF file named `foo.nc':

      INCLUDE 'netcdf.inc'
         ...
      INTEGER  NCID, RCODE
      INTEGER  RHID               ! variable ID
      INTEGER  VRTYPE, TTYPE      ! attribute types
      INTEGER  VRLEN, TLEN        ! attribute lengths
         ...
      NCID = NCOPN ('foo.nc', NCNOWRIT, RCODE)
         ...
      RHID = NCVID (NCID, 'rh', RCODE)! get ID
         ...
      CALL NCAINQ (NCID, RHID, 'valid_range', VRTYPE, VRLEN,
     +             RCODE)
      CALL NCAINQ (NCID, NCGLOBAL, 'title', TTYPE, TLEN,
     +             RCODE)

Go to the previous, next section.