NetCDF User's Guide for C
nc_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 NC_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 nc_set_fill . See Section 5.12 "Set Fill Mode for Writes: nc_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. |
nc_put_att_type
nc_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.
int nc_put_att_text (int ncid, int varid, const char *name, size_t len, const char *tp); int nc_put_att_uchar (int ncid, int varid, const char *name, nc_type xtype, size_t len, const unsigned char *up); int nc_put_att_schar (int ncid, int varid, const char *name, nc_type xtype, size_t len, const signed char *cp); int nc_put_att_short (int ncid, int varid, const char *name, nc_type xtype, size_t len, const short *sp); int nc_put_att_int (int ncid, int varid, const char *name, nc_type xtype, size_t len, const int *ip); int nc_put_att_long (int ncid, int varid, const char *name, nc_type xtype, size_t len, const long *lp); int nc_put_att_float (int ncid, int varid, const char *name, nc_type xtype, size_t len, const float *fp); int nc_put_att_double (int ncid, int varid, const char *name, nc_type xtype, size_t len, const double *dp);
ncid | NetCDF ID, from a previous call to nc_open or nc_create . |
varid | Variable ID of the variable to which the attribute will be assigned or NC_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. See Section 8.1 "Attribute Conventions," page 81, for examples of attribute conventions. |
xtype | One of the set of predefined netCDF external data types. The type of this parameter, nc_type , is defined in the netCDF header file. The valid netCDF external data types are NC_BYTE , NC_CHAR , NC_SHORT , NC_INT , NC_FLOAT , and NC_DOUBLE . Although it's possible to create attributes of all types, NC_CHAR and NC_DOUBLE attributes are adequate for most purposes. |
len | Number of values provided for the attribute. |
tp, up, cp, sp, ip, lp, fp, or dp | Pointer to one or more values. If the type of values differs from the netCDF attribute type specified as xtype , type conversion will occur. See Section 3.3 "Type Conversion," page 20, for details. |
nc_put_att_type
returns the value NC_NOERR
if no errors occurred. Otherwise, the returned status indicates an error. Possible causes of errors include:
NC_MAX_ATTRS
.
nc_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.h> ... int status; /* error status */ int ncid; /* netCDF ID */ int rh_id; /* variable ID */ static double rh_range[] = {0.0, 100.0};/* attribute vals */ static char title[] = "example netCDF dataset"; ... status = nc_open("foo.nc", NC_WRITE, &ncid); if (status != NC_NOERR) handle_error(status); ... status = nc_redef(ncid); /* enter define mode */ if (status != NC_NOERR) handle_error(status); status = nc_inq_varid (ncid, "rh", &rh_id); if (status != NC_NOERR) handle_error(status); ... status = nc_put_att_double (ncid, rh_id, "valid_range", NC_DOUBLE, 2, rh_range); if (status != NC_NOERR) handle_error(status); status = nc_put_att_text (ncid, NC_GLOBAL, "title", NC_CHAR, strlen(title), title) if (status != NC_NOERR) handle_error(status); ... status = nc_enddef(ncid); /* leave define mode */ if (status != NC_NOERR) handle_error(status);
nc_inq_att
Familync_inq_attname
.
Information about an attribute includes its type, length, name, and number. See the nc_get_att
family for getting attribute values.
The function nc_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 nc_inq_att
returns the attribute's type and length. The other functions each return just one item of information about an attribute.
int nc_inq_att (int ncid, int varid, const char *name, nc_type *xtypep, size_t *lenp); int nc_inq_atttype(int ncid, int varid, const char *name, nc_type *xtypep); int nc_inq_attlen (int ncid, int varid, const char *name, size_t *lenp); int nc_inq_attname(int ncid, int varid, int attnum, char *name); int nc_inq_attid (int ncid, int varid, const char *name, int *attnump);
Each function returns the value NC_NOERR
if no errors occurred. Otherwise, the returned status indicates an error. Possible causes of errors include:
nc_inq_attname
, the specified attribute number is negative or more than the number of attributes defined for the specified variable.
nc_inq_att
to find out the type and length of a variable 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.h> ... int status; /* error status */ int ncid; /* netCDF ID */ int rh_id; /* variable ID */ nc_type vr_type, t_type; /* attribute types */ int vr_len, t_len; /* attribute lengths */ ... status = nc_open("foo.nc", NC_NOWRITE, &ncid); if (status != NC_NOERR) handle_error(status); ... status = nc_inq_varid (ncid, "rh", &rh_id); if (status != NC_NOERR) handle_error(status); ... status = nc_inq_att (ncid, rh_id, "valid_range", &vr_type, &vr_len); if (status != NC_NOERR) handle_error(status); status = nc_inq_att (ncid, NC_GLOBAL, "title", &t_type, &t_len); if (status != NC_NOERR) handle_error(status);
nc_get_att_type
nc_get_att_type
family of functions get the value(s) of a netCDF attribute, given its variable ID and name.int nc_get_att_text (int ncid, int varid, const char *name, char *tp); int nc_get_att_uchar (int ncid, int varid, const char *name, unsigned char *up); int nc_get_att_schar (int ncid, int varid, const char *name, signed char *cp); int nc_get_att_short (int ncid, int varid, const char *name, short *sp); int nc_get_att_int (int ncid, int varid, const char *name, int *ip); int nc_get_att_long (int ncid, int varid, const char *name, long *lp); int nc_get_att_float (int ncid, int varid, const char *name, float *fp); int nc_get_att_double (int ncid, int varid, const char *name, double *dp);
nc_get_att_type
returns the value NC_NOERR
if no errors occurred. Otherwise, the returned status indicates an error. Possible causes of errors include:
nc_get_att_double
to determine the values of a variable 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, but that we do know the types of the attributes. Hence, to allocate enough space to store them, we must first inquire about the length of the attributes.
#include <netcdf.h> ... int status; /* error status */ int ncid; /* netCDF ID */ int rh_id; /* variable ID */ int vr_len, t_len; /* attribute lengths */ double *vr_val; /* ptr to attribute values */ char *title; /* ptr to attribute values */ extern char *malloc(); /* memory allocator */ ... status = nc_open("foo.nc", NC_NOWRITE, &ncid); if (status != NC_NOERR) handle_error(status); ... status = nc_inq_varid (ncid, "rh", &rh_id); if (status != NC_NOERR) handle_error(status); ... /* find out how much space is needed for attribute values */ status = nc_inq_attlen (ncid, rh_id, "valid_range", &vr_len); if (status != NC_NOERR) handle_error(status); status = nc_inq_attlen (ncid, NC_GLOBAL, "title", &t_len); if (status != NC_NOERR) handle_error(status); /* allocate required space before retrieving values */ vr_val = (double *) malloc(vr_len * sizeof(double)); title = (char *) malloc(t_len + 1); /* + 1 for trailing null */ /* get attribute values */ status = nc_get_att_double(ncid, rh_id, "valid_range", vr_val); if (status != NC_NOERR) handle_error(status); status = nc_get_att_text(ncid, NC_GLOBAL, "title", title); if (status != NC_NOERR) handle_error(status); title[t_len] = '\0'; /* null terminate */ ...
nc_copy_att
nc_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. int nc_copy_att (int ncid_in, int varid_in, const char *name, int ncid_out, int varid_out);
ncid_in | The netCDF ID of an input netCDF dataset from which the attribute will be copied, from a previous call to nc_open or nc_create . |
varid_in | ID of the variable in the input netCDF dataset from which the attribute will be copied, or NC_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 nc_open or nc_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 NC_GLOBAL to copy to a global attribute. |
nc_copy_att
returns the value NC_NOERR
if no errors occurred. Otherwise, the returned status indicates an error. Possible causes of errors include:
nc_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.h> ... int status; /* error status */ int ncid1, ncid2; /* netCDF IDs */ int rh_id, avgrh_id; /* variable IDs */ ... status = nc_open("foo.nc", NC_NOWRITE, ncid1); if (status != NC_NOERR) handle_error(status); status = nc_open("bar.nc", NC_WRITE, ncid2); if (status != NC_NOERR) handle_error(status); ... status = nc_inq_varid (ncid1, "rh", &rh_id); if (status != NC_NOERR) handle_error(status); status = nc_inq_varid (ncid2, "avgrh", &avgrh_id); if (status != NC_NOERR) handle_error(status); ... status = nc_redef(ncid2); /* enter define mode */ if (status != NC_NOERR) handle_error(status); /* copy variable attribute from "rh" to "avgrh" */ status = nc_copy_att(ncid1, rh_id, "units", ncid2, avgrh_id); if (status != NC_NOERR) handle_error(status); ... status = nc_enddef(ncid2); /* leave define mode */ if (status != NC_NOERR) handle_error(status);
nc_rename_att
nc_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. int nc_rename_att (int ncid, int varid, const char* name, const char* newname);
ncid | NetCDF ID, from a previous call to nc_open or nc_create |
varid | ID of the attribute's variable, or NC_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. |
nc_rename_att
returns the value NC_NOERR
if no errors occurred. Otherwise, the returned status indicates an error. Possible causes of errors include:
nc_rename_att
to rename the variable attribute units
to Units
for a variable rh
in an existing netCDF dataset named foo.nc
:
#include <netcdf.h> ... int status; /* error status */ int ncid; /* netCDF ID */ int rh_id; /* variable id */ ... status = nc_open("foo.nc", NC_NOWRITE, &ncid); if (status != NC_NOERR) handle_error(status); ... status = nc_inq_varid (ncid, "rh", &rh_id); if (status != NC_NOERR) handle_error(status); ... /* rename attribute */ status = nc_rename_att(ncid, rh_id, "units", "Units"); if (status != NC_NOERR) handle_error(status);
nc_del_att
nc_del_att
deletes a netCDF attribute from an open netCDF dataset. The netCDF dataset must be in define mode. int nc_del_att (int ncid, int varid, const char* name);
ncid | NetCDF ID, from a previous call to nc_open or nc_create . |
varid | ID of the attribute's variable, or NC_GLOBAL for a global attribute. |
name | The name of the attribute to be deleted. |
nc_del_att
returns the value NC_NOERR
if no errors occurred. Otherwise, the returned status indicates an error. Possible causes of errors include:
nc_del_att
to delete the variable attribute Units
for a variable rh
in an existing netCDF dataset named foo.nc
:
#include <netcdf.h> ... int status; /* error status */ int ncid; /* netCDF ID */ int rh_id; /* variable ID */ ... status = nc_open("foo.nc", NC_WRITE, &ncid); if (status != NC_NOERR) handle_error(status); ... status = nc_inq_varid (ncid, "rh", &rh_id); if (status != NC_NOERR) handle_error(status); ... /* delete attribute */ status = nc_redef(ncid); /* enter define mode */ if (status != NC_NOERR) handle_error(status); status = nc_del_att(ncid, rh_id, "Units"); if (status != NC_NOERR) handle_error(status); status = nc_enddef(ncid); /* leave define mode */ if (status != NC_NOERR) handle_error(status);