Next: , Previous: Attribute Conventions, Up: Attributes


5.3 NF_PUT_ATT_ type

The function NF_PUT_ATT_ type 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.

     INTEGER FUNCTION  NF_PUT_ATT_TEXT  (INTEGER NCID, INTEGER VARID,
                                         CHARACTER*(*) NAME, INTEGER LEN,
                                         CHARACTER*(*) TEXT)
     INTEGER FUNCTION  NF_PUT_ATT_INT1  (INTEGER NCID, INTEGER VARID,
                                         CHARACTER*(*) NAME, INTEGER XTYPE,
                                         LEN, INTEGER*1 I1VALS(*))
     INTEGER FUNCTION  NF_PUT_ATT_INT2  (INTEGER NCID, INTEGER VARID,
                                          CHARACTER*(*) NAME, INTEGER XTYPE,
                                         LEN, INTEGER*2 I2VALS(*))
     INTEGER FUNCTION  NF_PUT_ATT_INT   (INTEGER NCID, INTEGER VARID,
                                         CHARACTER*(*) NAME, INTEGER XTYPE,
                                         LEN, INTEGER IVALS(*))
     INTEGER FUNCTION  NF_PUT_ATT_REAL  (INTEGER NCID, INTEGER VARID,
                                         CHARACTER*(*) NAME, INTEGER XTYPE,
                                         LEN, REAL RVALS(*))
     INTEGER FUNCTION  NF_PUT_ATT_DOUBLE(INTEGER NCID, INTEGER VARID,
                                         CHARACTER*(*) NAME, INTEGER XTYPE,
                                         LEN, DOUBLE DVALS(*))
NCID
NetCDF ID, from a previous call to NF_OPEN or NF_CREATE.
VARID
Variable ID of the variable to which the attribute will be assigned or NF_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.
XTYPE
One of the set of predefined netCDF external data types. The type of this parameter, NF_TYPE, is defined in the netCDF header file. The valid netCDF external data types are NF_BYTE, NF_CHAR, NF_SHORT, NF_INT, NF_FLOAT, and NF_DOUBLE. Although it's possible to create attributes of all types, NF_CHAR and NF_DOUBLE attributes are adequate for most purposes.
LEN
Number of values provided for the attribute.
TEXT
I1VALS
I2VALS
IVALS
RVALS
DVALS
An array of LEN attribute values. The data should be of a type appropriate for the function called. You cannot write CHARACTER data into a numeric attribute or numeric data into a text attribute. For numeric data, if the type of data differs from the attribute type, type conversion will occur See Type Conversion.

Errors

NF_PUT_ATT_ type returns the value NF_NOERR if no errors occurred. Otherwise, the returned status indicates an error. Possible causes of errors include:

Example

Here is an example using NF_PUT_ATT_DOUBLE 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:

     INCLUDE 'netcdf.inc'
        ...
     INTEGER STATUS, NCID
     INTEGER RHID                 ! variable ID
     DOUBLE RHRNGE(2)
     DATA RHRNGE /0.0D0, 100.0D0/
        ...
     STATUS = NF_OPEN ('foo.nc', NF_WRITE, NCID)
     IF (STATUS .NE. NF_NOERR) CALL HANDLE_ERR(STATUS)
        ...
     STATUS = NF_REDEF (NCID)     ! enter define mode
     IF (STATUS .NE. NF_NOERR) CALL HANDLE_ERR(STATUS)
     STATUS = NF_INQ_VARID (NCID, 'rh', RHID)
     IF (STATUS .NE. NF_NOERR) CALL HANDLE_ERR(STATUS)
        ...
     STATUS = NF_PUT_ATT_DOUBLE (NCID, RHID, 'valid_range', NF_DOUBLE, &
                                 2, RHRNGE)
     IF (STATUS .NE. NF_NOERR) CALL HANDLE_ERR(STATUS)
     STATUS = NF_PUT_ATT_TEXT (NCID, NF_GLOBAL, 'title', 19,
                               'example netCDF dataset')
     IF (STATUS .NE. NF_NOERR) CALL HANDLE_ERR(STATUS)
        ...
     STATUS = NF_ENDDEF (NCID)    ! leave define mode
     IF (STATUS .NE. NF_NOERR) CALL HANDLE_ERR(STATUS)