|
|
|||
|
||||
[Next] [Previous] [Top] [Contents] [Index] [netCDF Home Page] [Unidata Home Page]
NetCDF User's Guide for FORTRAN
Attributes
may be associated with each netCDF variable to specify such properties as
units, special values, maximum and minimum valid values, scaling factors,
and offsets. Attributes for a netCDF dataset are defined when the dataset
is first created, while the netCDF dataset is in define mode. Additional attributes
may be added later by reentering define mode. A netCDF attribute has a netCDF
variable to which it is assigned, a name, a type, a length, and a sequence
of one or more values. An attribute is designated by its variable ID and name.
When an attribute name is not known, it may be designated by its variable
ID and number in order to determine its name, using the function NF_INQ_ATTNAME.
The attributes associated with a variable are typically defined immediately after the variable is created, while still in define mode. The data type, length, and value of an attribute may be changed even when in data mode, as long as the changed attribute requires no more space than the attribute as originally defined.
It is also possible to have attributes that are not associated with any variable.
These are called global
attributes and are identified by using NF_GLOBAL
as a variable pseudo-ID. Global attributes are usually related to the netCDF
dataset as a whole and may be used for purposes such as providing a title
or processing history for a netCDF dataset.
Operations supported on attributes are:
Names commencing with underscore ('_') are reserved for use
by the netCDF library. Most generic applications that process netCDF datasets
assume standard attribute
conventions and it is strongly recommended that these be followed unless there
are good reasons for not doing so. Below we list the names and meanings of
recommended standard attributes that have proven useful. Note that some of
these (e.g. units, valid_range, scale_factor)
assume numeric data and should not be used with character data.
A character string that specifies the units used for the variable's data. Unidata has developed a freely-available library of routines to convert between character string and binary forms of unit specifications and to perform various useful operations on the binary forms. This library is used in some netCDF applications. Using the recommended units syntax permits data represented in conformable units to be automatically converted to common units for arithmetic operations. See Appendix A "Units," page 113, for more information. |
|
A long descriptive name. This could be used for labeling plots,
for example. If a variable has no |
|
A scalar specifying the minimum valid value for this variable. |
|
A scalar specifying the maximum valid value for this variable. |
|
A vector of two numbers specifying the minimum and maximum valid
values for this variable, equivalent to specifying values for both Generic applications should treat values outside the valid range
as missing. The type
of each If neither |
|
If present for a variable, the data are to be multiplied by this factor after the data are read by the application that accesses the data. |
|
If present for a variable, this number is to be added to the data
after it is read by the application that accesses the data. If both
When |
|
The Generic applications often need to write a value to represent undefined or missing values. The fill value provides an appropriate value for this purpose because it is normally outside the valid range and therefore treated as missing when read by generic applications. It is legal (but not recommended) for the fill value to be within the valid range. See Section 7.16 "Fill Values," page 78, for more information. |
|
This attribute is not treated in any special way by the library
or conforming generic applications, but is often useful documentation
and may be used by specific applications. The |
|
Deprecated attribute, originally designed to indicate whether byte
values should be treated as signed
or unsigned. The attributes |
|
A character array providing the format that should be used by C
applications to print
values for this variable. For example, if you know a variable is only
accurate to three significant digits, it would be appropriate to define
the |
|
A character array providing the format that should be used by FORTRAN
applications to print values for this variable. For example, if you
know a variable is only accurate to three significant digits, it would
be appropriate to define the |
|
A global attribute that is a character array providing a succinct description of what is in the dataset. |
|
A global attribute for an audit trail. This is a character array with a line for each invocation of a program that has modified the dataset. Well-behaved generic netCDF applications should append a line containing: date, time of day, user name, program name and command arguments. |
|
If present, ' |
|
|
For example, if a group named NUWG agrees upon a set of conventions
for dimension names, variable names, required attributes, and netCDF
representations for certain discipline-specific
data structures, they may store a document describing the agreed-upon
conventions in a dataset in the |
|
Later, if the group agrees upon some additional conventions for a
specific subset of NUWG data, for example time series data, the description
of the additional conventions might be stored in the |
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.
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(*))
|
NetCDF ID, from a previous call to |
|
Variable ID. |
|
Attribute name. Must begin with an alphabetic character, followed
by zero or more alphanumeric characters including the underscore (' |
|
One of the set of predefined netCDF external data types. The valid
netCDF external data types are |
|
The number of values provided for the attribute, or the length of
the text attribute for |
|
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 Section 3.3 "Type Conversion," page 24, for details). |
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:
NF_MAX_ATTRS.
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)
NF_INQ_ATT
FamilyThis family of functions returns information
about a netCDF attribute. All but one of these functions require the variable
ID and attribute name; the exception is NF_INQ_ATTNAME.
Information about an attribute includes its type, length, name, and number.
See the NF_GET_ATT family for getting
attribute values.
The function NF_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.
The function NF_INQ_ATT returns the
attribute's type and length. The other functions each return just one item
of information about an attribute.
INTEGER FUNCTION NF_INQ_ATT (INTEGER NCID, INTEGER VARID, CHARACTER*(*) NAME, INTEGER xtype, INTEGER len) INTEGER FUNCTION NF_INQ_ATTTYPE(INTEGER NCID, INTEGER VARID, CHARACTER*(*) NAME, INTEGER xtype) INTEGER FUNCTION NF_INQ_ATTLEN (INTEGER NCID, INTEGER VARID, CHARACTER*(*) NAME, INTEGER len) INTEGER FUNCTION NF_INQ_ATTNAME(INTEGER NCID, INTEGER VARID, INTEGER ATTNUM, CHARACTER*(*) name) INTEGER FUNCTION NF_INQ_ATTID (INTEGER NCID, INTEGER VARID, CHARACTER*(*) NAME, INTEGER attnum)
Each function returns the value NF_NOERR
if no errors occurred. Otherwise, the returned status indicates an error.
Possible causes of errors include:
NF_INQ_ATTNAME, the specified
attribute number is negative or more than the number of attributes defined
for the specified variable.
Here is an example using NF_INQ_ATTLEN
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:
INCLUDE 'netcdf.inc'
...
INTEGER STATUS, NCID
INTEGER RHID ! variable ID
INTEGER VRLEN, TLEN ! attribute lengths
...
STATUS = NF_OPEN ('foo.nc', NF_NOWRITE, NCID)
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_INQ_ATTLEN (NCID, RHID, 'valid_range', VRLEN)
IF (STATUS .NE. NF_NOERR) CALL HANDLE_ERR(STATUS)
STATUS = NF_INQ_ATTLEN (NCID, NF_GLOBAL, 'title', TLEN)
IF (STATUS .NE. NF_NOERR) CALL HANDLE_ERR(STATUS)
NF_GET_ATT_
type Members of the NF_GET_ATT_ type
family of functions get the value(s) of a netCDF attribute, given its variable
ID and name.
INTEGER FUNCTION NF_GET_ATT_TEXT (INTEGER NCID, INTEGER VARID, CHARACTER*(*) NAME, CHARACTER*(*) text) INTEGER FUNCTION NF_GET_ATT_INT1 (INTEGER NCID, INTEGER VARID, CHARACTER*(*) NAME, INTEGER*1 i1vals(*)) INTEGER FUNCTION NF_GET_ATT_INT2 (INTEGER NCID, INTEGER VARID, CHARACTER*(*) NAME, INTEGER*2 i2vals(*)) INTEGER FUNCTION NF_GET_ATT_INT (INTEGER NCID, INTEGER VARID, CHARACTER*(*) NAME, INTEGER ivals(*)) INTEGER FUNCTION NF_GET_ATT_REAL (INTEGER NCID, INTEGER VARID, CHARACTER*(*) NAME, REAL rvals(*)) INTEGER FUNCTION NF_GET_ATT_DOUBLE (INTEGER NCID, INTEGER VARID, CHARACTER*(*) NAME, DOUBLE dvals(*))
NF_GET_ATT_ type returns the
value NF_NOERR
if no errors occurred. Otherwise, the returned status indicates an error.
Possible causes of errors include:
Here is an example using NF_GET_ATT_DOUBLE
to determine the values 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. In this example,
it is assumed that we don't know how many values will be returned, so we first
inquire about the length of the attributes to make sure we have enough space
to store them:
INCLUDE 'netcdf.inc'
...
PARAMETER (MVRLEN=3) ! max number of "valid_range" values
PARAMETER (MTLEN=80) ! max length of "title" attribute
INTEGER STATUS, NCID
INTEGER RHID ! variable ID
INTEGER VRLEN, TLEN ! attribute lengths
DOUBLE PRECISION VRVAL(MVRLEN) ! vr attribute values
CHARACTER*80 TITLE ! title attribute values
...
STATUS = NF_OPEN ('foo.nc', NF_WRITE, NCID)
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)
...
! find out attribute lengths, to make sure we have enough space
STATUS = NF_INQ_ATTLEN (NCID, RHID, 'valid_range', VRLEN)
IF (STATUS .NE. NF_NOERR) CALL HANDLE_ERR(STATUS)
STATUS = NF_INQ_ATTLEN (NCID, NF_GLOBAL, 'title', TLEN)
IF (STATUS .NE. NF_NOERR) CALL HANDLE_ERR(STATUS)
! get attribute values, if not too big
IF (VRLEN .GT. MVRLEN) THEN
WRITE (*,*) 'valid_range attribute too big!'
CALL EXIT
ELSE
STATUS = NF_GET_ATT_DOUBLE (NCID, RHID, 'valid_range', VRVAL)
IF (STATUS .NE. NF_NOERR) CALL HANDLE_ERR(STATUS)
ENDIF
IF (TLEN .GT. MTLEN) THEN
WRITE (*,*) 'title attribute too big!'
CALL EXIT
ELSE
STATUS = NF_GET_ATT_TEXT (NCID, NF_GLOBAL, 'title', TITLE)
IF (STATUS .NE. NF_NOERR) CALL HANDLE_ERR(STATUS)
ENDIF
NF_COPY_ATT
The function NF_COPY_ATT copies an
attribute from one open netCDF dataset to another. It can also be used to
copy an attribute from one variable to another within the same netCDF.
INTEGER FUNCTION NF_COPY_ATT (INTEGER NCID_IN, INTEGER VARID_IN, CHARACTER*(*) NAME, INTEGER NCID_OUT, INTEGER VARID_OUT)
|
The netCDF ID of an input netCDF dataset from which the attribute
will be copied, from a previous call to |
|
ID of the variable in the input netCDF dataset from which the attribute
will be copied, or |
|
Name of the attribute in the input netCDF dataset to be copied. |
|
The netCDF ID of the output netCDF dataset to which the attribute
will be copied, from a previous call to |
|
ID of the variable in the output netCDF dataset to which the attribute
will be copied, or |
NF_COPY_ATT returns the value NF_NOERR
if no errors occurred. Otherwise, the returned status indicates an error.
Possible causes of errors include:
Here is an example using NF_COPY_ATT
to copy the variable attribute units from the variable rh
in an existing netCDF dataset named foo.nc to the variable avgrh
in another existing netCDF dataset named bar.nc, assuming that
the variable avgrh already exists, but does not yet have a units
attribute:
INCLUDE 'netcdf.inc' ... INTEGER STATUS ! error status INTEGER NCID1, NCID2 ! netCDF IDs INTEGER RHID, AVRHID ! variable IDs ... STATUS = NF_OPEN ('foo.nc', NF_NOWRITE, NCID1) IF (STATUS .NE. NF_NOERR) CALL HANDLE_ERR(STATUS) STATUS = NF_OPEN ('bar.nc', NF_WRITE, NCID2) IF (STATUS .NE. NF_NOERR) CALL HANDLE_ERR(STATUS) ... STATUS = NF_INQ_VARID (NCID1, 'rh', RHID) IF (STATUS .NE. NF_NOERR) CALL HANDLE_ERR(STATUS) STATUS = NF_INQ_VARID (NCID2, 'avgrh', AVRHID) IF (STATUS .NE. NF_NOERR) CALL HANDLE_ERR(STATUS) ... STATUS = NF_REDEF (NCID2) ! enter define mode IF (STATUS .NE. NF_NOERR) CALL HANDLE_ERR(STATUS) ! copy variable attribute from "rh" to "avgrh" STATUS = NF_COPY_ATT (NCID1, RHID, 'units', NCID2, AVRHID) IF (STATUS .NE. NF_NOERR) CALL HANDLE_ERR(STATUS) ... STATUS = NF_ENDDEF (NCID2) ! leave define mode IF (STATUS .NE. NF_NOERR) CALL HANDLE_ERR(STATUS)
NF_RENAME_ATT The function NF_RENAME_ATT changes
the name of an attribute. If the new name is longer than the original name,
the netCDF dataset must be in define mode. You cannot rename an attribute
to have the same name as another attribute of the same variable.
INTEGER FUNCTION NF_RENAME_ATT (INTEGER NCID, INTEGER VARID, CHARACTER*(*) NAME, CHARACTER*(*) NEWNAME)
|
NetCDF ID, from a previous call to |
|
ID of the attribute's variable, or |
|
The current attribute name. |
|
The new name to be assigned to the specified attribute. If the new name is longer than the current name, the netCDF dataset must be in define mode. |
NF_RENAME_ATT returns the value NF_NOERR
if no errors occurred. Otherwise, the returned status indicates an error.
Possible causes of errors include:
Here is an example using NF_RENAME_ATT
to rename the variable attribute units to Units
for a variable rh in an existing netCDF dataset named foo.nc:
INCLUDE "netcdf.inc" ... INTEGER STATUS ! error status INTEGER NCID ! netCDF ID INTEGER RHID ! variable ID ... STATUS = NF_OPEN ("foo.nc", NF_NOWRITE, NCID) 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) ... ! rename attribute STATUS = NF_RENAME_ATT (NCID, RHID, "units", "Units") IF (STATUS .NE. NF_NOERR) CALL HANDLE_ERR(STATUS)
NF_DEL_ATT
The function NF_DEL_ATT deletes a
netCDF attribute from an open netCDF dataset. The netCDF dataset must be in
define mode.
INTEGER FUNCTION NF_DEL_ATT (INTEGER NCID, INTEGER VARID, CHARACTER*(*) NAME)
|
NetCDF ID, from a previous call to |
|
ID of the attribute's variable, or |
|
The original attribute name. |
NF_DEL_ATT returns the value NF_NOERR
if no errors occurred. Otherwise, the returned status indicates an error.
Possible causes of errors include:
Here is an example using NF_DEL_ATT
to delete the variable attribute Units for a variable rh
in an existing netCDF dataset named foo.nc:
Here is an example using NF_DEL_ATT
to delete the variable attribute Units for a variable rh
in an existing netCDF dataset named foo.nc:
INCLUDE 'netcdf.inc'
...
INTEGER STATUS ! error status
INTEGER NCID ! netCDF ID
INTEGER RHID ! variable ID
...
STATUS = NF_OPEN ('foo.nc', NF_WRITE, NCID)
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)
...
! delete attribute
STATUS = NF_REDEF (NCID) ! enter define mode
IF (STATUS .NE. NF_NOERR) CALL HANDLE_ERR(STATUS)
STATUS = NF_DEL_ATT (NCID, RHID, 'Units')
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)
[Next] [Previous] [Top] [Contents] [Index] [netCDF Home Page] [Unidata Home Page]
| Contact Us Site Map Search Terms and Conditions Privacy Policy Participation Policy | ||||||
|
||||||