Go to the previous, next section.
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 file are
defined when the file is first created, while the netCDF file 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, except in one case
(ncattname
or NCANAM
in FORTRAN), where attributes are
designated by variable ID and number because their names are unknown.
The attributes associated with a variable are typically defined right 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 assign attributes not associated with any
variable to the netCDF as a whole, by using the NC_GLOBAL
variable
pseudo-ID. Global attributes may be used for purposes such as providing
a title or processing history for a netCDF data set.
Operations supported on attributes are:
The function ncattput
(or NCAPT
or NCAPTC
for
FORTRAN) adds or changes a variable attribute or global attribute of an
open netCDF file. If this attribute is new, or if the space required to
store the attribute is greater than before, the netCDF file must be in
define mode.
In case of an error, ncattput
returns -1; NCAPT
returns a
nonzero value in rcode
. Possible causes of errors include:
int ncattput(int ncid, int varid, const char* name, nc_type datatype, int len, const void* values);
ncid
ncopen
or nccreate
.
varid
NC_GLOBAL
for a global attribute.
name
units
as the
name for a string attribute that gives the units for a netCDF variable.
See section Attribute Conventions, for examples of attribute conventions.
datatype
nc_type
, is defined in the netCDF header file. The
valid netCDF data types are NC_BYTE
, NC_CHAR
,
NC_SHORT
, NC_LONG
, NC_FLOAT
, and NC_DOUBLE
.
len
NC_CHAR
, this is one more than the string length (since the
terminating null character is stored).
values
void *
because it can point to data of any of the basic
netCDF types. The data should be of the appropriate type for the netCDF
attribute. Warning: neither the compiler nor the netCDF
software can detect whether the wrong type of data is used.
Here is an example using ncattput
to add a variable attribute
named valid_range
for a netCDF variable named rh
and a
global attribute named title
to an existing netCDF file named
`foo.nc':
#include "netcdf.h" ... int ncid; /* netCDF ID */ int rh_id; /* variable ID */ static double rh_range[] = {0.0, 100.0}; /* attribute vals */ static char title[] = "example netCDF file"; ... ncid = ncopen("foo.nc", NC_WRITE); ... ncredef(ncid); /* enter define mode */ rh_id = ncvarid (ncid, "rh"); ... ncattput (ncid, rh_id, "valid_range", NC_DOUBLE, 2, rh_range); ncattput (ncid, NC_GLOBAL, "title", NC_CHAR, strlen(title)+1, title); ... ncendef(ncid); /* leave define mode */
SUBROUTINE NCAPT (INTEGER NCID, INTEGER VARID, + CHARACTER*(*) ATTNAM, INTEGER ATTYPE, + INTEGER ATTLEN, type VALUE, + INTEGER RCODE) SUBROUTINE NCAPTC (INTEGER NCID, INTEGER VARID, + CHARACTER*(*) ATTNAM, INTEGER ATTYPE, + INTEGER LENSTR, CHARACTER*(*) STRING, + INTEGER RCODE)
There are two FORTRAN subroutines, NCAPT
and NCAPTC
, for
creating attributes. The first is for attributes of numeric type, and
the second is for attributes of character-string type.
NCID
NCOPN
or NCCRE
.
VARID
NCVDEF
or
NCVID
.
ATTNAM
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.
ATTYPE
NCBYTE
, NCCHAR
, NCSHORT
, NCLONG
,
NCFLOAT
, and NCDOUBLE
.
ATTLEN
NCAPT
, the number of numeric values provided for the
attribute.
VALUE
NCAPT
, an array of ATTLEN
data values. The data should
be of the appropriate type for the netCDF attribute. Warning:
neither the compiler nor the netCDF software can detect if the wrong
type of data is used.
STRING
NCAPTC
, the character-string value of the attribute.
LENSTR
NCAPTC
, the total declared length (in characters) of the
STRING
parameter. Note that this is not necessarily the same as
the value returned by the FORTRAN LEN
function, because an array
argument may be provided.
RCODE
Here is an example using NCAPT
to add a variable attribute
named valid_range
for a netCDF variable named rh
and a
global attribute named title
to an existing netCDF file named
`foo.nc':
INCLUDE 'netcdf.inc' ... INTEGER NCID, RCODE INTEGER RHID ! variable ID DOUBLE RHRNGE(2) DATA RHRNGE /0.0D0, 100.0D0/ ... NCID = NCOPN ('foo.nc', NCWRITE, RCODE) ... CALL NCREDF (NCID, RCODE) ! enter define mode RHID = NCVID (NCID, 'rh', RCODE)! get ID ... CALL NCAPT (NCID, RHID, 'valid_range', NCDOUBLE, 2, + RHRNGE, RCODE) CALL NCAPTC (NCID, NCGLOBAL, 'title', NCCHAR, 19, + 'example netCDF file', RCODE) ... CALL NCENDF (NCID, RCODE) ! leave define mode
The function ncattinq
(or NCAINQ
for FORTRAN) returns
information about a netCDF attribute, given its variable ID and name.
The information returned is the type and length of the attribute.
In case of an error, ncattinq
returns -1; NCAINQ
returns a
nonzero value in rcode
. Possible causes of errors include:
int ncattinq(int ncid, int varid, const char* name, nc_type* datatype, int* len);
ncid
ncopen
or nccreate
.
varid
NC_GLOBAL
for a
global attribute.
name
datatype
nc_type
, is defined in the netCDF
header file. The valid netCDF data types are NC_BYTE
,
NC_CHAR
, NC_SHORT
, NC_LONG
, NC_FLOAT
, and
NC_DOUBLE
. If this parameter is given as `(nc_type *) 0',
no type will be returned so no variable to hold the type needs to be
declared.
len
NC_CHAR
, this is one more than the string
length (since the terminating null character is stored). If this
parameter is given as `(int *) 0', no length will be returned so no
variable to hold this information needs to be declared.
Here is an example using ncattinq
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 file named `foo.nc':
#include "netcdf.h" ... int ncid; /* netCDF ID */ int rh_id; /* variable ID */ nc_type vr_type, t_type; /* attribute types */ int vr_len, t_len; /* attribute lengths *' ... ncid = ncopen("foo.nc", NC_NOWRITE); ... rh_id = ncvarid (ncid, "rh"); ... ncattinq (ncid, rh_id, "valid_range", &vr_type, &vr_len); ncattinq (ncid, NC_GLOBAL, "title", &t_type, &t_len); ...
SUBROUTINE NCAINQ (INTEGER NCID, INTEGER VARID, + CHARACTER*(*) ATTNAM, INTEGER ATTYPE, + INTEGER ATTLEN,INTEGER RCODE)
NCID
NCOPN
or NCCRE
.
VARID
NCGLOBAL
for a
global attribute.
ATTNAM
ATTYPE
NCBYTE
, NCCHAR
,
NCSHORT
, NCLONG
, NCFLOAT
, and NCDOUBLE
.
ATTLEN
RCODE
Here is an example using NCAINQ
to add a variable attribute
named valid_range
for a netCDF variable named rh
and a
global attribute named title
to an existing netCDF file named
`foo.nc':
INCLUDE 'netcdf.inc' ... INTEGER NCID, RCODE INTEGER RHID ! variable ID INTEGER VRTYPE, TTYPE ! attribute types INTEGER VRLEN, TLEN ! attribute lengths ... NCID = NCOPN ('foo.nc', NCNOWRIT, RCODE) ... RHID = NCVID (NCID, 'rh', RCODE)! get ID ... CALL NCAINQ (NCID, RHID, 'valid_range', VRTYPE, VRLEN, + RCODE) CALL NCAINQ (NCID, NCGLOBAL, 'title', TTYPE, TLEN, + RCODE)
The function ncattget
(or NCAGT
or NCAGTC
for
FORTRAN) gets the value(s) of a netCDF attribute, given its
variable ID and name.
In case of an error, ncattget
returns -1; NCAGT
returns a
nonzero value in rcode
. Possible causes of errors include:
int ncattget(int ncid, int varid, const char* name, void* value);
ncid
ncopen
or nccreate
.
varid
NC_GLOBAL
for a
global attribute.
name
value
ncattinq
first to find out the length of the attribute.
Here is an example using ncattget
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 file 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 ncid; /* netCDF ID */ int rh_id; /* variable ID */ nc_type vr_type, t_type; /* attribute types */ 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 */ ... ncid = ncopen("foo.nc", NC_NOWRITE); ... rh_id = ncvarid (ncid, "rh"); ... /* find out how much space is needed for attribute values */ ncattinq (ncid, rh_id, "valid_range", &vr_type, &vr_len); ncattinq (ncid, NC_GLOBAL, "title", &t_type, &t_len); /* allocate required space before retrieving values */ vr_val = (double *) malloc(vr_len * nctypelen(vr_type)); title = (char *) malloc(t_len * nctypelen(t_type)); /* get attribute values */ ncattget(ncid, rh_id, "valid_range", (void *)vr_val); ncattget(ncid, NC_GLOBAL, "title", (void *)title); ...
SUBROUTINE NCAGT (INTEGER NCID, INTEGER VARID, + CHARACTER*(*) ATTNAM, type VALUES, + INTEGER RCODE) SUBROUTINE NCAGTC (INTEGER NCID, INTEGER VARID, + CHARACTER*(*) ATTNAM, CHARACTER*(*) STRING, + INTEGER LENSTR, INTEGER RCODE)
There are two FORTRAN subroutines, NCAGT
and NCAGTC
, for
retrieving attribute values. The first is for attributes of numeric
type, and the second is for attributes of character-string type.
NCID
NCOPN
or NCCRE
.
VARID
NCGLOBAL
for a
global attribute.
ATTNAM
VALUES
NCAINQ
first to find out the length of the attribute. Warning:
neither the compiler nor the netCDF software can detect if the wrong
type of data is used.
STRING
NCAGTC
, the character-string value of the attribute.
LENSTR
NCAGTC
, the total declared length (in characters) of the
STRING
parameter in the caller. Note that this is not
necessarily the same as the value returned by the FORTRAN LEN
function, because an array argument may be provided. NCAGTC
will
check to make sure the requested data will fit in LENSTR
characters.
RCODE
Here is an example using NCAGT
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 file
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 NCID, RCODE INTEGER RHID ! variable ID INTEGER VRTYPE, TTYPE ! attribute types INTEGER VRLEN, TLEN ! attribute lengths DOUBLE PRECISION VRVAL(MVRLEN) ! vr attribute values CHARACTER*80 TITLE ! title attribute values ... NCID = NCOPN ('foo.nc', NCWRITE, RCODE) ... RHID = NCVID (NCID, 'rh', RCODE) ! get ID ... * find out attribute lengths, to make sure we have enough space CALL NCAINQ (NCID, RHID, 'valid_range', VRTYPE, VRLEN, + RCODE) CALL NCAINQ (NCID, NCGLOBAL, 'title', TTYPE, TLEN, + RCODE) * get attribute values, if not too big IF (VRLEN > MVRLEN) THEN WRITE (*,*) 'valid_range attribute too big!' CALL EXIT ELSE CALL NCAGT (NCID, RHID, 'valid_range', VRVAL, RCODE) ENDIF IF (TLEN > MTLEN) THEN WRITE (*,*) 'title attribute too big!' CALL EXIT ELSE CALL NCAGTC (NCID, NCGLOBAL, 'title', TITLE, MTLEN, RCODE) ENDIF
The function ncattcopy
(or NCACPY
for FORTRAN) copies an
attribute from one open netCDF file to another. It can also be used to
copy an attribute from one variable to another within the same netCDF.
In case of an error, ncattcopy
returns -1; NCACPY
returns a
nonzero value in rcode
. Possible causes of errors include:
int ncattcopy(int incdf, int invar, const char* name, int outcdf, int outvar);
incdf
ncopen
or nccreate
.
invar
NC_GLOBAL
for a global attribute.
name
outcdf
ncopen
or nccreate
. It
is permissible for the input and output netCDF IDs to be the same. The
output netCDF file 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.
outvar
NC_GLOBAL
to copy to a global attribute.
Here is an example using ncattcopy
to copy the variable attribute
units
from the variable rh
in an existing netCDF file
named `foo.nc' to the variable avgrh
in another existing
netCDF file named `bar.nc', assuming that the variable
avgrh
already exists, but does not yet have a units
attribute:
#include "netcdf.h" ... int ncid1, ncid2; /* netCDF IDs */ int rh_id, avgrh_id; /* variable IDs */ ... ncid1 = ncopen("foo.nc", NC_NOWRITE); ncid2 = ncopen("bar.nc", NC_WRITE); ... rh_id = ncvarid (ncid1, "rh"); avgrh_id = ncvarid (ncid2, "avgrh"); ... ncredef(ncid2); /* enter define mode */ /* copy variable attribute from "rh" to "avgrh" */ ncattcopy(ncid1, rh_id, "units", ncid2, avgrh_id); ... ncendef(ncid2); /* leave define mode */
SUBROUTINE NCACPY (INTEGER INCDF, INTEGER INVAR, + CHARACTER*(*) ATTNAM, INTEGER OUTCDF, + INTEGER OUTVAR, INTEGER RCODE)
INCDF
NCOPN
or NCCRE
.
INVAR
NCGLOBAL
for a global attribute.
ATTNAM
OUTCDF
NCOPN
or NCCRE
. It
is permissible for the input and output netCDF IDs to be the same. The
output netCDF file 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.
OUTVAR
NCGLOBAL
to copy to a global
attribute.
Here is an example using NCACPY
to copy the variable attribute
units
from the variable rh
in an existing netCDF file
named `foo.nc' to the variable avgrh
in another existing
netCDF file named `bar.nc', assuming that the variable
avgrh
already exists, but does not yet have a units
attribute:
INCLUDE 'netcdf.inc' ... INTEGER NCID1, NCID2 ! netCDF IDs INTEGER RHID, AVRHID ! variable IDs ... NCID1 = NCOPN ('foo.nc', NCNOWRIT, RCODE) NCID2 = NCOPN ('bar.nc', NCWRITE, RCODE) ... RHID = NCVID (NCID1, 'rh', RCODE) AVRHID = NCVID (NCID2, 'avgrh', RCODE) ... CALL NCREDF (NCID2, RCODE) ! enter define mode * copy variable attribute from "rh" to "avgrh" CALL NCACPY (NCID1, RHID, 'units', NCID2, AVRHID, RCODE) ... CALL NCENDF (NCID2, RCODE) ! leave define mode
The function ncattname
(or NCANAM
for FORTRAN) 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.
In case of an error, ncattname
returns -1; NCANAM
returns a
nonzero value in rcode
. Possible causes of errors include:
int ncattname (int ncid, int varid, int attnum, char* name);
ncid
ncopen
or nccreate
.
varid
NC_GLOBAL
for a global
attribute.
attnum
nvatts-1
, where nvatts
is
the number of attributes for the variable, as returned from a call to
ncvarinq
.
name
MAX_NC_NAME
.
If the name parameter is given as (char *) 0
, no name will be
returned and no space needs to be allocated.
Here is an example using ncattname
to determine the name of the
first attribute of the variable rh
in an existing netCDF file
named `foo.nc':
#include "netcdf.h" ... int ncid; /* netCDF ID */ int rh_id; /* variable ID */ char attname[MAX_NC_NAME]; /* maximum-size attribute name */ ... ncid = ncopen("foo.nc", NC_NOWRITE); ... rh_id = ncvarid (ncid, "rh"); ... /* get name of first attribute (number 0) */ ncattname(ncid, rh_id, 0, attname);
SUBROUTINE NCANAM (INTEGER NCID, INTEGER VARID, + INTEGER ATTNUM, CHARACTER*(*) ATTNAM, + INTEGER RCODE)
NCID
NCOPN
or NCCRE
.
VARID
NCGLOBAL
for a global
attribute.
ATTNUM
NVATTS
, where NVATTS
is
the number of attributes for the variable, as returned from a call to
NCVINQ
.
ATTNAM
MAXNCNAM
.
RCODE
Here is an example using NCANAM
determine the name of the
first attribute of the variable rh
in an existing netCDF file
named `foo.nc':
INCLUDE 'netcdf.inc' ... INTEGER NCID ! netCDF ID INTEGER RHID ! variable ID * 31 in the following should be MAXNCNAM CHARACTER*31 ATTNAM ... NCID = NCOPN ('foo.nc', NCNOWRIT, RCODE) ... RHID = NCVID (NCID, 'rh', RCODE) ... * get name of first attribute (number 1) CALL NCANAM (NCID, RHID, 1, ATTNAM, RCODE)
The function ncattrename
(or NCAREN
for FORTRAN) changes the
name of an attribute. If the new name is longer than the original name,
the netCDF must be in define mode. You cannot rename an attribute to
have the same name as another attribute of the same variable.
In case of an error, ncattrename
returns -1; NCAREN
returns a
nonzero value in rcode
. Possible causes of errors include:
int ncattrename (int ncid, int varid, const char* name, const char* newname);
ncid
ncopen
or nccreate
varid
NC_GLOBAL
for a
global attribute
name
newname
Here is an example using ncattrename
to rename the variable
attribute units
to Units
for a variable rh
in an existing netCDF file named `foo.nc':
#include "netcdf.h" ... int ncid; /* netCDF ID */ int rh_id; /* variable id */ ... ncid = ncopen("foo.nc", NC_NOWRITE); ... rh_id = ncvarid (ncid, "rh"); ... /* rename attribute */ ncattrename(ncid, rh_id, "units", "Units");
SUBROUTINE NCAREN (INTEGER NCID, INTEGER VARID, + CHARACTER*(*) ATTNAM, + CHARACTER*(*) NEWNAM, INTEGER RCODE)
NCID
NCOPN
or NCCRE
VARID
NCGLOBAL
for a
global attribute
ATTNAM
NEWNAM
RCODE
Here is an example using NCAREN
to rename the variable
attribute units
to Units
for a variable rh
in an existing netCDF file named `foo.nc':
INCLUDE "netcdf.inc" ... INTEGER NCID ! netCDF ID INTEGER RHID ! variable ID ... NCID = NCOPN ("foo.nc", NCNOWRIT, RCODE) ... RHID = NCVID (NCID, "rh", RCODE) ... * rename attribute CALL NCAREN (NCID, RHID, "units", "Units", RCODE)
The function ncattdel
(or NCADEL
for FORTRAN) deletes a
netCDF attribute from an open netCDF file. The netCDF file must be in
define mode.
In case of an error, ncattdel
returns -1; NCADEL
returns a
nonzero value in rcode
. Possible causes of errors include:
int ncattdel (int ncid, int varid, const char* name);
ncid
ncopen
or nccreate
.
varid
NC_GLOBAL
for a
global attribute.
name
Here is an example using ncattdel
to delete the variable
attribute Units
for a variable rh
in an existing netCDF
file named `foo.nc':
#include "netcdf.h" ... int ncid; /* netCDF ID */ int rh_id; /* variable ID */ ... ncid = ncopen("foo.nc", NC_WRITE); ... rh_id = ncvarid (ncid, "rh"); ... /* delete attribute */ ncredef(ncid); /* enter define mode */ ncattdel(ncid, rh_id, "Units"); ncendef(ncid); /* leave define mode */
SUBROUTINE NCADEL (INTEGER NCID, INTEGER VARID, + CHARACTER*(*) ATTNAM, INTEGER RCODE)
NCID
NCOPN
or NCCRE
.
VARID
NCGLOBAL
for a
global attribute.
ATTNAM
RCODE
Here is an example using NCADEL
to delete the variable attribute
Units
for a variable rh
in an existing netCDF file named
`foo.nc':
INCLUDE 'netcdf.inc' ... INTEGER NCID ! netCDF ID INTEGER RHID ! variable ID ... NCID = NCOPN ('foo.nc', NCWRITE, RCODE) ... RHID = NCVID (NCID, 'rh', RCODE) ... * delete attribute CALL NCREDF (NCID, RCODE) ! enter define mode CALL NCADEL (NCID, RHID, 'Units', RCODE) CALL NCENDF (NCID, RCODE) ! leave define mode
Go to the previous, next section.