Go to the previous, next section.

Attributes

A netCDF attribute is meant to contain information about a netCDF variable or about an entire netCDF file. This information is ancillary data, or data about data, analogous to the information stored in data dictionaries and schema in conventional database systems. An attribute has an associated variable, a name, a data type, a length, and a value. Individual attributes are identified by specifying a variable and an attribute name.

Each attribute is associated with a single variable when it is created. Attributes for different variables may differ in data type, length, and values even though they share the same name.

A global attribute is one that applies to the whole netCDF rather than any particular variable. Global attributes are defined and accessed similarly to variable attributes; the details for defining global attributes in the CDL notation and in the netCDF procedural interface are presented later.

Attribute names follow the same rules as dimension and variable names. Providing meaningful names for attributes is important, but using agreed on conventional names is also required if generic applications and utility programs will be used on a netCDF file. For example, every variable for which units make sense should have a units attribute defined, so the units can be printed in labels. Furthermore, if the netCDF file is ever to be used as input to generic units-aware software, the values of the units attributes should be expressed in a conventional form as a character string that can be interpreted by that software.

The type of an attribute is specified when it is created. The types permitted for attributes are exactly the same as the netCDF data types used in creating variables. Attributes with the same name for different variables should sometimes be of different types. For example, the attribute valid_max specifying the maximum valid data value for a variable of type long should be of type long, whereas the attribute valid_max for a variable of type double should instead be of type double.

In addition to specifying the associated variable, attribute name, and type, the length and value of an attribute must also be specified when it is created. The information in an attribute is represented by either a single value (length 1) or a vector of values of the same type. Since "character string" is not a basic netCDF data type, string-valued attributes have a vector of characters as their value, with a length equal to the length of the character string.

Attributes are more dynamic than variables or dimensions; they can have their type, length, and values changed after they are created. For example, an attribute max_value might store the maximum value seen so far for a record variable, and might be updated every time a new record is written.

The CDL notation for defining an attribute is

    variable_name:attribute_name = list_of_values ;
for a variable attribute, or
    :attribute_name = list_of_values ;
for a global attribute. The type and length of each attribute are not explicitly declared in CDL; they are derived from the values assigned to the attribute. All values of an attribute must be of the same type. The notation used for constant values of the various netCDF types is discussed later (see section CDL Notation for Data Constants).

In the example netCDF (see section Components of a NetCDF File), units is an attribute for the variable lat that has a 13 character array value `degrees_north'. And valid_range is an attribute for the variable rh that has length 2 and values `0.0' and `1.0'.

One global attribute---source---is defined for the example netCDF file. This is a character array intended for documenting the data. Real netCDF files typically have more global attributes to document the origin, history, accuracy, and other characteristics of the data.

Go to the previous, next section.