Next: , Previous: Attribute Conventions, Up: Attributes


5.3 Create an Attribute: NF90_PUT_ATT

The function NF90_PUT_ATT adds or changes a variable attribute or global attribute of an open netCDF dataset. If this attribute is new, or if the space required to store the attribute is greater than before, the netCDF dataset must be in define mode.

Usage

Although it's possible to create attributes of all types, text and double attributes are adequate for most purposes.

      function nf90_put_att(ncid, varid, name, values)
        integer,            intent( in) :: ncid, varid
        character(len = *), intent( in) :: name
        any valid type, scalar or array of rank 1, &
                            intent( in) :: values
        integer                         :: nf90_put_att
ncid
NetCDF ID, from a previous call to NF90_OPEN or NF90_CREATE.
varid
Variable ID of the variable to which the attribute will be assigned or NF90_GLOBAL for a global attribute.
name
Attribute name. Must begin with an alphabetic character, followed by zero or more alphanumeric characters including the underscore ('_'). Case is significant. Attribute name conventions are assumed by some netCDF generic applications, e.g., units as the name for a string attribute that gives the units for a netCDF variable. For examples of attribute conventions see Attribute Conventions.
values
An array of attribute values. Values may be supplied as scalars or as arrays of rank one (one dimensional vectors). The external data type of the attribute is set to match the internal representation of the argument, that is if values is a two byte integer array, the attribute will be of type NF90_INT2. Fortran 90 intrinsic functions can be used to convert attributes to the desired type.

Errors

NF90_PUT_ATT 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_PUT_ATT to add a variable attribute named valid_range for a netCDF variable named rh and a global attribute named title to an existing netCDF dataset named foo.nc:

      use netcdf
      implicit none
      integer :: ncid, status, RHVarID
      ...
      status = nf90_open("foo.nc", nf90_write, ncid)
      if (status /= nf90_noerr) call handle_err(status)
      ...
      ! Enter define mode so we can add the attribute
      status = nf90_redef(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)
      ! ...  put the range attribute, setting it to eight byte reals...
      status = nf90_put_att(ncid, RHVarID, "valid_range", real((/ 0, 100 /))
      ! ... and the title attribute.
      if (status /= nf90_noerr) call handle_err(status)
      status = nf90_put_att(ncid, RHVarID, "title", "example netCDF dataset") )
      if (status /= nf90_noerr) call handle_err(status)
      ! Leave define mode
      status = nf90_enddef(ncid)
      if (status /= nf90_noerr) call handle_err(status)