Go to the previous, next section.

Attributes

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:

Create an Attribute

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:

ncattput: C Interface

int ncattput(int ncid, int varid, const char* name, nc_type datatype,
             int len, const void* values);

ncid
NetCDF ID, returned from a previous call to ncopen or nccreate.

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 Attribute Conventions, for examples of attribute conventions.

datatype
One of the set of predefined netCDF data types. The type of this parameter, 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
Number of values provided for the attribute. If the attribute is of type NC_CHAR, this is one more than the string length (since the terminating null character is stored).

values
Pointer to one or more data values. The pointer is declared to be of the type 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 */

NCAPT, NCAPTC: FORTRAN Interface

      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
NetCDF ID, returned from a previous call to NCOPN or NCCRE.

VARID
Variable ID, returned from a previous call to NCVDEF or NCVID.

ATTNAM
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.

ATTYPE
One of the set of predefined netCDF data types. The valid netCDF data types are NCBYTE, NCCHAR, NCSHORT, NCLONG, NCFLOAT, and NCDOUBLE.

ATTLEN
In NCAPT, the number of numeric values provided for the attribute.

VALUE
In 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
In NCAPTC, the character-string value of the attribute.

LENSTR
In 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
Returned error code. If no errors occurred, 0 is returned.

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

Get Information about an Attribute

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:

ncattinq: C Interface

int ncattinq(int ncid, int varid, const char* name,
             nc_type* datatype, int* len);

ncid
NetCDF ID, returned from a previous call to ncopen or nccreate.

varid
Variable ID of the attribute's variable, or NC_GLOBAL for a global attribute.

name
Attribute name.

datatype
Returned attribute type, one of the set of predefined netCDF data types. The type of this parameter, 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
Returned number of values currently stored in the attribute. If the attribute is of type 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);
   ...

NCAINQ: FORTRAN Interface

      SUBROUTINE NCAINQ (INTEGER NCID, INTEGER VARID,
     +                   CHARACTER*(*) ATTNAM, INTEGER ATTYPE,
     +                   INTEGER ATTLEN,INTEGER RCODE)

NCID
NetCDF ID, returned from a previous call to NCOPN or NCCRE.

VARID
Variable ID of the attribute's variable, or NCGLOBAL for a global attribute.

ATTNAM
Attribute name.

ATTYPE
Returned attribute type, one of the set of predefined netCDF data types. The valid netCDF data types are NCBYTE, NCCHAR, NCSHORT, NCLONG, NCFLOAT, and NCDOUBLE.

ATTLEN
Returned number of values currently stored in the attribute. For a string-valued attribute, this is the number of characters in the string.

RCODE
Returned error code. If no errors occurred, 0 is returned.

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)

Get Attribute's Values

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:

ncattget: C Interface

int ncattget(int ncid, int varid, const char* name, void* value);

ncid
NetCDF ID, returned from a previous call to ncopen or nccreate.

varid
Variable ID of the attribute's variable, or NC_GLOBAL for a global attribute.

name
Attribute name.

value
Returned attribute values. All elements of the vector of attribute values are returned, so you must allocate enough space to hold them. If you don't know how much space to reserve, call 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);
   ...

NCAGT, NCAGTC: FORTRAN Interface

      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
NetCDF ID, returned from a previous call to NCOPN or NCCRE.

VARID
Variable ID of the attribute's variable, or NCGLOBAL for a global attribute.

ATTNAM
Attribute name.

VALUES
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 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
In NCAGTC, the character-string value of the attribute.

LENSTR
In 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
Returned error code. If no errors occurred, 0 is returned.

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

Copy Attribute from One NetCDF to Another

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:

ncattcopy: C Interface

int ncattcopy(int incdf, int invar, const char* name, int outcdf, int outvar);

incdf
The netCDF ID of an input netCDF file from which the attribute will be copied, returned from a previous call to ncopen or nccreate.

invar
ID of the variable in the input netCDF file from which the attribute will be copied, or NC_GLOBAL for a global attribute.

name
Name of the attribute in the input netCDF file to be copied.

outcdf
The netCDF ID of the output netCDF file to which the attribute will be copied, returned from a previous call to 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
ID of the variable in the output netCDF file to which the attribute will be copied, or 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 */

NCACPY: FORTRAN Interface

      SUBROUTINE NCACPY (INTEGER INCDF, INTEGER INVAR,
     +                   CHARACTER*(*) ATTNAM, INTEGER OUTCDF,
     +                   INTEGER OUTVAR, INTEGER RCODE)

INCDF
The netCDF ID of an input netCDF file from which the attribute will be copied, returned from a previous call to NCOPN or NCCRE.

INVAR
ID of the variable in the input netCDF file from which the attribute will be copied, or NCGLOBAL for a global attribute.

ATTNAM
Name of the attribute in the input netCDF file to be copied.

OUTCDF
The netCDF ID of the output netCDF file to which the attribute will be copied, returned from a previous call to 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
ID of the variable in the output netCDF file to which the attribute will be copied, or 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

Get Name of Attribute from Its Number

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:

ncattname: C Interface

int ncattname (int ncid, int varid, int attnum, char* name);

ncid
NetCDF ID, returned from a previous call to ncopen or nccreate.

varid
ID of the attribute's variable, or NC_GLOBAL for a global attribute.

attnum
Number of the attribute. The attributes for each variable are numbered from 0 (the first attribute) to nvatts-1, where nvatts is the number of attributes for the variable, as returned from a call to ncvarinq.

name
Returned attribute name. The caller must allocate space for the returned name. The maximum possible length, in characters, of an attribute name is given by the predefined constant 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);

NCANAM: FORTRAN Interface

      SUBROUTINE NCANAM (INTEGER NCID, INTEGER VARID,
     +                   INTEGER ATTNUM, CHARACTER*(*) ATTNAM,
     +                   INTEGER RCODE)

NCID
NetCDF ID, returned from a previous call to NCOPN or NCCRE.

VARID
ID of the attribute's variable, or NCGLOBAL for a global attribute.

ATTNUM
Number of the attribute. The attributes for each variable are numbered from 1 (the first attribute) to NVATTS, where NVATTS is the number of attributes for the variable, as returned from a call to NCVINQ.

ATTNAM
Returned attribute name. The caller must allocate space for the returned name. The maximum possible length, in characters, of an attribute name is given by the predefined constant MAXNCNAM.

RCODE
Returned error code. If no errors occurred, 0 is returned.

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)

Rename an Attribute

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:

ncattrename: C Interface

int ncattrename (int ncid, int varid, const char* name, const char* newname);

ncid
NetCDF ID, returned from a previous call to ncopen or nccreate
varid
ID of the attribute's variable, or NC_GLOBAL for a global attribute
name
The original attribute name.
newname
The new name to be assigned to the specified attribute. If the new name is longer than the old name, the netCDF file must be in define mode.

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");

NCAREN: FORTRAN Interface

      SUBROUTINE NCAREN (INTEGER NCID, INTEGER VARID,
     +                   CHARACTER*(*) ATTNAM,
     +                   CHARACTER*(*) NEWNAM, INTEGER RCODE)

NCID
NetCDF ID, returned from a previous call to NCOPN or NCCRE
VARID
ID of the attribute's variable, or NCGLOBAL for a global attribute
ATTNAM
The original attribute name.
NEWNAM
The new name to be assigned to the specified attribute. If the new name is longer than the old name, the netCDF file must be in define mode.
RCODE
Returned error code. If no errors occurred, 0 is returned.

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)

Delete an Attribute

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:

ncattdel: C Interface

int ncattdel (int ncid, int varid, const char* name);

ncid
NetCDF ID, returned from a previous call to ncopen or nccreate.

varid
ID of the attribute's variable, or NC_GLOBAL for a global attribute.

name
The name of the attribute to be deleted.

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 */

NCADEL: FORTRAN Interface

      SUBROUTINE NCADEL (INTEGER NCID, INTEGER VARID,
     +                   CHARACTER*(*) ATTNAM, INTEGER RCODE)

NCID
NetCDF ID, returned from a previous call to NCOPN or NCCRE.

VARID
ID of the attribute's variable, or NCGLOBAL for a global attribute.

ATTNAM
The original attribute name.

RCODE
Returned error code. If no errors occurred, 0 is returned.

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.