NetCDF User's Guide for Fortran
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:
_
') 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.
units | 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. |
long_name | A long descriptive name. This could be used for labeling plots, for example. If a variable has no long_name attribute assigned, the variable name should be used as a default. |
valid_min | A scalar specifying the minimum valid value for this variable. |
valid_max | A scalar specifying the maximum valid value for this variable. |
valid_range | A vector of two numbers specifying the minimum and maximum valid values for this variable, equivalent to specifying values for both valid_min and valid_max attributes. Any of these attributes define the valid range. The attribute valid_range must not be defined if either valid_min or valid_max is defined. Generic applications should treat values outside the valid range as missing. The type of each If neither |
scale_factor | 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. |
add_offset | 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 scale_factor and add_offset attributes are present, the data are first scaled before the offset is added. The attributes scale_factor and add_offset can be used together to provide simple data compression to store low-resolution floating-point data as small integers in a netCDF dataset. When scaled data are written, the application should first subtract the offset and then divide by the scale factor. When |
_FillValue | The _FillValue attribute specifies the fill value used to pre-fill disk space allocated to the variable. Such pre-fill occurs unless nofill mode is set using NF_SET_FILL . See Section 5.12 "Set Fill Mode for Writes: NF_SET_FILL," page 39, for details. The fill value is returned when reading values that were never written. If _FillValue is defined then it should be scalar and of the same type as the variable. It is not necessary to define your own _FillValue attribute for a variable if the default fill value for the type of the variable is adequate. However, use of the default fill value for data type byte is not recommended. Note that if you change the value of this attribute, the changed value applies only to subsequent writes; previously written data are not changed. 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. |
missing_value | 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 missing_value attribute can be a scalar or vector containing values indicating missing data. These values should all be outside the valid range so that generic applications will treat them as missing. |
signedness | Deprecated attribute, originally designed to indicate whether byte values should be treated as signed or unsigned. The attributes valid_min and valid_max may be used for this purpose. For example, if you intend that a byte variable store only nonnegative values, you can use valid_min = 0 and valid_max = 255 . This attribute is ignored by the netCDF library. |
C_format | 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 C_format attribute as "%.3g" . The ncdump utility program uses this attribute for variables for which it is defined. The format applies to the scaled (internal) type and value, regardless of the presence of the scaling attributes scale_factor and add_offset . |
FORTRAN_format | 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 FORTRAN_format attribute as "(G10.3)" . |
title | A global attribute that is a character array providing a succinct description of what is in the dataset. |
history | 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. |
Conventions | If present, 'Conventions ' is a global attribute that is a character array for the name of the conventions followed by the dataset, in the form of a string that is interpreted as a directory name relative to a directory that is a repository of documents describing sets of discipline-specific conventions. This permits a hierarchical structure for conventions and provides a place where descriptions and examples of the conventions may be maintained by the defining institutions and groups. The conventions directory name is currently interpreted relative to the directory pub/netcdf/Conventions/ on the host machine ftp.unidata.ucar.edu . Alternatively, a full URL specification may be used to name a WWW site where documents that describe the conventions are maintained. |
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 NUWG/ subdirectory of the Conventions directory. Datasets that followed these conventions would contain a global Conventions attribute with value "NUWG" . | |
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 NUWG/Time_series/ subdirectory, and datasets that adhered to these additional conventions would use the global Conventions attribute with value "NUWG/Time_series" , implying that this dataset adheres to the NUWG conventions and also to the additional NUWG time-series conventions. |
NF_PUT_ATT_type
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.
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. |
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. A table of conventional attribute names is presented in the earlier chapter on the netCDF interface. |
XTYPE | One of the set of predefined netCDF external data types. 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 | The number of values provided for the attribute, or the length of the text attribute for NF_PUT_ATT_TEXT . |
TEXT, I1VALS,
I2VALS, IVALS,RVALS, or 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 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
.
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
FamilyNF_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)
Errors
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.
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
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(*))
NCID | NetCDF ID, from a previous call to NF_OPEN or NF_CREATE . |
VARID | Variable ID of the attribute's variable, or NF_GLOBAL for a global attribute. |
NAME | Attribute name. |
text, i1vals,
i2vals, ivals,rvals, or dvals | Returned attribute values. All elements of the vector of attribute values are returned, so you must provide enough space to hold them. If you don't know how much space to reserve, call NF_INQ_ATTLEN first to find out the length of the attribute. You cannot read character data from a numeric variable or numeric data from a text variable. For numeric data, if the type of data differs from the netCDF variable type, type conversion will occur (see Section 3.3 "Type Conversion," page 24, for details). |
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:
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
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)
NCID_IN | The netCDF ID of an input netCDF dataset from which the attribute will be copied, from a previous call to NF_OPEN or NF_CREATE . |
VARID_IN | ID of the variable in the input netCDF dataset from which the attribute will be copied, or NF_GLOBAL for a global attribute. |
NAME | Name of the attribute in the input netCDF dataset to be copied. |
NCID_OUT | The netCDF ID of the output netCDF dataset to which the attribute will be copied, from a previous call to NF_OPEN or NF_CREATE . It is permissible for the input and output netCDF IDs to be the same. The output netCDF dataset should be in define mode if the attribute to be copied does not already exist for the target variable, or if it would cause an existing target attribute to grow. |
VARID_OUT | ID of the variable in the output netCDF dataset to which the attribute will be copied, or NF_GLOBAL to copy to a global attribute. |
NF_COPY_ATT
returns the value NF_NOERR
if no errors occurred. Otherwise, the returned status indicates an error. Possible causes of errors include:
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
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)
NCID | NetCDF ID, from a previous call to NF_OPEN or NF_CREATE |
VARID | ID of the attribute's variable, or NF_GLOBAL for a global attribute |
NAME | The current attribute name. |
NEWNAME | 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:
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
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)
NCID | NetCDF ID, from a previous call to NF_OPEN or NF_CREATE . |
VARID | ID of the attribute's variable, or NF_GLOBAL for a global attribute. |
NAME | 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:
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)