Next: , Previous: NF90_PUT_ATT, Up: Attributes


5.4 Get Information about an Attribute: NF90_INQUIRE_ATTRIBUTE and NF90_INQ_ATTNAME

The function NF90_INQUIRE_ATTRIBUTE returns information about a netCDF attribute given the variable ID and attribute name. Information about an attribute includes its type, length, name, and number. See NF90_GET_ATT for getting attribute values.

The function NF90_INQ_ATTNAME 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.

Usage

      function nf90_inquire_attribute(ncid, varid, name, xtype, len, attnum)
        integer,             intent( in)           :: ncid, varid
        character (len = *), intent( in)           :: name
        integer,             intent(out), optional :: xtype, len, attnum
        integer                                    :: nf90_inquire_attribute
      function nf90_inq_attname(ncid, varid, attnum, name)
        integer,             intent( in) :: ncid, varid, attnum
        character (len = *), intent(out) :: name
        integer                          :: nf90_inq_attname
ncid
NetCDF ID, from a previous call to NF90_OPEN or NF90_CREATE.
varid
Variable ID of the attribute's variable, or NF90_GLOBAL for a global attribute.
name
Attribute name. For NF90_INQ_ATTNAME, this is a pointer to the location for the returned attribute name.
xtype
Returned attribute type, one of the set of predefined netCDF external data types. The valid netCDF external data types are NF90_BYTE, NF90_CHAR, NF90_SHORT, NF90_INT, NF90_FLOAT, and NF90_DOUBLE.
len
Returned number of values currently stored in the attribute. For a string-valued attribute, this is the number of characters in the string.
attnum
For NF90_INQ_ATTNAME, the input attribute number; for NF90_INQ_ATTID, the returned attribute number. The attributes for each variable are numbered from 1 (the first attribute) to NATTS, where NATTS is the number of attributes for the variable, as returned from a call to NF90_INQ_VARNATTS.

(If you already know an attribute name, knowing its number is not very useful, because accessing information about an attribute requires its name.)

Errors

Each function returns the value NF90_NOERR if no errors occurred. Otherwise, the returned status indicates an error. Possible causes of errors include:

Example

Here is an example using NF90_INQUIRE_ATTRIBUTE to inquire about the lengths of an attribute named valid_range for a netCDF variable named rh and a global attribute named title in an existing netCDF dataset named foo.nc:

      use netcdf
      implicit none
      integer :: ncid, status
      integer :: RHVarID                       ! Variable ID
      integer :: validRangeLength, titleLength ! Attribute lengths
      ...
      status = nf90_open("foo.nc", nf90_nowrite, ncid)
      if (status /= nf90_noerr) call handle_err(status)
      ...
      ! Get the variable ID for "rh"...
      status = nf90_inq_varid(ncid, "rh", RHVarID)
      if (status /= nf90_noerr) call handle_err(status)
      ! ...  get the length of the "valid_range" attribute...
      status = nf90_inquire_attribute(ncid, RHVarID, "valid_range", &
                                len = validRangeLength)
      if (status /= nf90_noerr) call handle_err(status)
      ! ... and the global title attribute.
      status = nf90_inquire_attribute(ncid, nf90_global, "title", len = titleLength)
      if (status /= nf90_noerr) call handle_err(status)