Go to the previous, next section.
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
Go to the previous, next section.