NetCDF  4.9.2
Attributes

Attributes hold metadata about data and files. More...

Learning about Attributes

Functions to learn about the attributes in a file.

int nc_inq_att (int ncid, int varid, const char *name, nc_type *xtypep, size_t *lenp)
 Return information about a netCDF attribute. More...
 
int nc_inq_attid (int ncid, int varid, const char *name, int *idp)
 Find an attribute ID. More...
 
int nc_inq_attname (int ncid, int varid, int attnum, char *name)
 Find the name of an attribute. More...
 
int nc_inq_natts (int ncid, int *nattsp)
 Find number of global or group attributes. More...
 
int nc_inq_atttype (int ncid, int varid, const char *name, nc_type *xtypep)
 Find the type of an attribute. More...
 
int nc_inq_attlen (int ncid, int varid, const char *name, size_t *lenp)
 Find the length of an attribute. More...
 

Getting Attributes

Functions to get the values of attributes.

For classic format files, the netCDF library reads all attributes into memory when the file is opened with nc_open().

For netCDF-4/HDF5 files, since version 4.7.2, attributes are not read on file open. Instead, when the first read of a variable attribute is done, all attributes for that variable are read. Subsequent access to other attributes of that variable will not incur a disk read. Similarly, when the first NC_GLOBAL attribute is read in a group, all NC_GLOBAL attributes for that group will be read.

Note
All elements attribute data array are returned, so you must allocate enough space to hold them. If you don't know how much space to reserve, call nc_inq_attlen() first to find out the length of the attribute.

Example

Here is an example using nc_get_att_double() to determine the values of a variable attribute named valid_range for a netCDF variable named rh from a 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;
int ncid;
int rh_id;
int vr_len;
double *vr_val;
...
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_attlen (ncid, rh_id, "valid_range", &vr_len);
if (status != NC_NOERR) handle_error(status);
vr_val = (double *) malloc(vr_len * sizeof(double));
status = nc_get_att_double(ncid, rh_id, "valid_range", vr_val);
if (status != NC_NOERR) handle_error(status);
...
EXTERNL int nc_get_att_double(int ncid, int varid, const char *name, double *ip)
Get an attribute array of type double.
Definition: dattget.c:453
EXTERNL int nc_inq_attlen(int ncid, int varid, const char *name, size_t *lenp)
Find the length of an attribute.
Definition: dattinq.c:424
EXTERNL int nc_open(const char *path, int mode, int *ncidp)
Open an existing netCDF file.
Definition: dfile.c:666
EXTERNL int nc_inq_varid(int ncid, const char *name, int *varidp)
Find the ID of a variable, from the name.
Definition: dvarinq.c:60
Main header file for the C API.
#define NC_NOWRITE
Set read-only access for nc_open().
Definition: netcdf.h:126
#define NC_NOERR
No Error.
Definition: netcdf.h:368
int nc_get_att (int ncid, int varid, const char *name, void *value)
 Get an attribute of any type. More...
 
int nc_get_att_text (int ncid, int varid, const char *name, char *value)
 Get a text attribute. More...
 
int nc_get_att_schar (int ncid, int varid, const char *name, signed char *value)
 Get an attribute of an signed char type. More...
 
int nc_get_att_uchar (int ncid, int varid, const char *name, unsigned char *value)
 Get an attribute of an signed char type. More...
 
int nc_get_att_short (int ncid, int varid, const char *name, short *value)
 Get an attribute array of type short. More...
 
int nc_get_att_int (int ncid, int varid, const char *name, int *value)
 Get an attribute array of type int. More...
 
int nc_get_att_long (int ncid, int varid, const char *name, long *value)
 Get an attribute array of type long. More...
 
int nc_get_att_float (int ncid, int varid, const char *name, float *value)
 Get an attribute array of type float. More...
 
int nc_get_att_double (int ncid, int varid, const char *name, double *value)
 Get an attribute array of type double. More...
 
int nc_get_att_ubyte (int ncid, int varid, const char *name, unsigned char *value)
 Get an attribute array of type unsigned char. More...
 
int nc_get_att_ushort (int ncid, int varid, const char *name, unsigned short *value)
 Get an attribute array of type unsigned short. More...
 
int nc_get_att_uint (int ncid, int varid, const char *name, unsigned int *value)
 Get an attribute array of type unsigned int. More...
 
int nc_get_att_longlong (int ncid, int varid, const char *name, long long *value)
 Get an attribute array of type long long. More...
 
int nc_get_att_ulonglong (int ncid, int varid, const char *name, unsigned long long *value)
 Get an attribute array of type unsigned long long. More...
 
int nc_get_att_string (int ncid, int varid, const char *name, char **value)
 Get an attribute array of type string. More...
 

Writing Attributes

Functions to write attributes.

For netCDF classic formats, attributes are defined when the dataset is first created, while the netCDF dataset is in define mode. Additional attributes may be added later by reentering define mode. For netCDF-4/HDF5 netCDF files, attributes may be defined at any time.

In classic format files, 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. In netCDF-4/HDF5 files, attribute name, length, and value may be changed at any time.

Attribute data conversion automatically takes place when the type of the data does not match the xtype argument. All attribute data values are converted to xtype before being written to the file.

If writing a new attribute, or if the space required to store the attribute is greater than before, the netCDF dataset must be in define mode for classic formats (or netCDF-4/HDF5 with NC_CLASSIC_MODEL).

Note
With netCDF-4 files, nc_put_att will notice if you are writing a _FillValue attribute, and will tell the HDF5 layer to use the specified fill value for that variable. With either classic or netCDF-4 files, a _FillValue attribute will be checked for validity, to make sure it has only one value and that its type matches the type of the associated variable.
int nc_put_att_string (int ncid, int varid, const char *name, size_t len, const char **value)
 Write a string attribute. More...
 
int nc_put_att_text (int ncid, int varid, const char *name, size_t len, const char *value)
 Write a text attribute. More...
 
int nc_put_att (int ncid, int varid, const char *name, nc_type xtype, size_t len, const void *value)
 Write an attribute of any type. More...
 
int nc_put_att_schar (int ncid, int varid, const char *name, nc_type xtype, size_t len, const signed char *value)
 Write an attribute of type signed char. More...
 
int nc_put_att_uchar (int ncid, int varid, const char *name, nc_type xtype, size_t len, const unsigned char *value)
 Write an attribute of type unsigned char. More...
 
int nc_put_att_short (int ncid, int varid, const char *name, nc_type xtype, size_t len, const short *value)
 Write an attribute of type short. More...
 
int nc_put_att_int (int ncid, int varid, const char *name, nc_type xtype, size_t len, const int *value)
 Write an attribute of type int. More...
 
int nc_put_att_long (int ncid, int varid, const char *name, nc_type xtype, size_t len, const long *value)
 Write an attribute of type long. More...
 
int nc_put_att_float (int ncid, int varid, const char *name, nc_type xtype, size_t len, const float *value)
 Write an attribute of type float. More...
 
int nc_put_att_double (int ncid, int varid, const char *name, nc_type xtype, size_t len, const double *value)
 Write an attribute of type double. More...
 
int nc_put_att_ubyte (int ncid, int varid, const char *name, nc_type xtype, size_t len, const unsigned char *value)
 Write an attribute of type unsigned char. More...
 
int nc_put_att_ushort (int ncid, int varid, const char *name, nc_type xtype, size_t len, const unsigned short *value)
 Write an attribute of type unsigned short. More...
 
int nc_put_att_uint (int ncid, int varid, const char *name, nc_type xtype, size_t len, const unsigned int *value)
 Write an attribute of type unsigned int. More...
 
int nc_put_att_longlong (int ncid, int varid, const char *name, nc_type xtype, size_t len, const long long *value)
 Write an attribute of type long long. More...
 
int nc_put_att_ulonglong (int ncid, int varid, const char *name, nc_type xtype, size_t len, const unsigned long long *value)
 Write an attribute of type unsigned long long. More...
 

Deleting and Renaming Attributes

Functions to delete or rename an attribute.

int nc_rename_att (int ncid, int varid, const char *name, const char *newname)
 Rename an attribute. More...
 
int nc_del_att (int ncid, int varid, const char *name)
 Delete an attribute. More...
 

Detailed Description

Attributes hold metadata about data and files.

Attributes store metadata.

Attributes may be associated with a netCDF variable to specify such properties as units, special values, maximum and minimum valid values, scaling factors, and offsets.

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 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. In netCDF-4/HDF5 files, global attributes are associated with a hierarchical group.

An attribute is designated by its variable ID and name. When an attribute name is not known, it may be designated by its variable ID and number in order to determine its name, using the function nc_inq_attname().

Operations supported on attributes are:

Function Documentation

◆ nc_del_att()

int nc_del_att ( int  ncid,
int  varid,
const char *  name 
)

Delete an attribute.

The function nc_del_att() deletes a netCDF attribute from an open netCDF dataset. For classic netCDF formats, the dataset must be in define mode to delete an attribute. In netCDF-4/HDF5 files, attributes may be deleted at any time.

Parameters
ncidNetCDF or group ID, from a previous call to nc_open(), nc_create(), nc_def_grp(), or associated inquiry functions such as nc_inq_ncid().
varidVariable ID of the attribute's variable, or NC_GLOBAL for a global attribute.
nameAttribute name.

Example

Here is an example using 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;
int ncid;
int rh_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);
...
status = nc_redef(ncid);
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);
if (status != NC_NOERR) handle_error(status);
EXTERNL int nc_del_att(int ncid, int varid, const char *name)
Delete an attribute.
Definition: datt.c:177
EXTERNL int nc_enddef(int ncid)
Leave define mode.
Definition: dfile.c:1029
EXTERNL int nc_redef(int ncid)
Put open netcdf dataset into define mode.
Definition: dfile.c:965
#define NC_WRITE
Set read-write access for nc_open().
Definition: netcdf.h:127
Returns
NC_NOERR No error.
NC_EBADID Bad ncid.
NC_ENOTVAR Bad varid.
NC_EBADNAME Bad name.
NC_EINVAL Name not provided.
NC_EPERM File was opened read only.
NC_ENOTINDEFINE File is not in define mode.
NC_ENOTATT Attribute not found.
NC_EATTMETA Failure at HDF5 layer.
Author
Glenn Davis, Ed Hartnett, Dennis Heimbigner

Definition at line 177 of file datt.c.

◆ nc_get_att()

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

Get an attribute of any type.

The nc_get_att() function works for any type of attribute, and must be used to get attributes of user-defined type. We recommend that the type safe versions of this function be used for atomic data types.

Also see Getting Attributes

Parameters
ncidNetCDF file or group ID.
varidVariable ID, or NC_GLOBAL for a global attribute.
nameAttribute name.
valuePointer that will get array of attribute value(s). Use nc_inq_attlen() to learn length.
Note
See documentation for nc_get_att_string() regarding a special case where memory must be explicitly released.

Example

Here is an example using nc_get_att() from nc_test4/tst_vl.c creates a VLEN attribute, then uses nc_get_att() to read it.

#define FILE_NAME "tst_vl.nc"
#define VLEN_NAME "vlen_name"
#define ATT_NAME "att_name"
int ncid, typeid;
nc_vlen_t data[DIM_LEN], data_in[DIM_LEN];
...
if (nc_create(FILE_NAME, NC_NETCDF4, &ncid)) ERR;
if (nc_def_vlen(ncid, VLEN_NAME, NC_INT, &typeid)) ERR;
...
if (nc_put_att(ncid, NC_GLOBAL, ATT_NAME, typeid, DIM_LEN, data)) ERR;
if (nc_close(ncid)) ERR;
...
if (nc_open(FILE_NAME, NC_NOWRITE, &ncid)) ERR;
if (nc_get_att(ncid, NC_GLOBAL, ATT_NAME, data_in)) ERR;
...
if (nc_close(ncid)) ERR;
EXTERNL int nc_put_att(int ncid, int varid, const char *name, nc_type xtype, size_t len, const void *op)
Write an attribute of any type.
Definition: dattput.c:222
EXTERNL int nc_get_att(int ncid, int varid, const char *name, void *ip)
Get an attribute of any type.
Definition: dattget.c:133
EXTERNL int nc_close(int ncid)
Close an open netCDF dataset.
Definition: dfile.c:1302
EXTERNL int nc_create(const char *path, int cmode, int *ncidp)
Create a new netCDF file.
Definition: dfile.c:400
EXTERNL int nc_def_vlen(int ncid, const char *name, nc_type base_typeid, nc_type *xtypep)
Use this function to define a variable length array type.
Definition: dvlen.c:115
#define NC_NETCDF4
Use netCDF-4/HDF5 format.
Definition: netcdf.h:153
#define NC_INT
signed 4 byte integer
Definition: netcdf.h:38
#define NC_GLOBAL
Attribute id to put/get a global attribute.
Definition: netcdf.h:254
This is the type of arrays of vlens.
Definition: netcdf.h:746
Returns
NC_NOERR for success.
NC_EBADID Bad ncid.
NC_ENOTVAR Bad varid.
NC_EBADNAME Bad name. See NetCDF Names.
NC_EINVAL Invalid parameters.
NC_ENOTATT Can't find attribute.
NC_ECHAR Can't convert to or from NC_CHAR.
NC_ENOMEM Out of memory.
NC_ERANGE Data conversion went out of range.
Author
Glenn Davis, Ed Hartnett, Dennis Heimbigner

Definition at line 133 of file dattget.c.

◆ nc_get_att_double()

int nc_get_att_double ( int  ncid,
int  varid,
const char *  name,
double *  value 
)

Get an attribute array of type double.

Also see Getting Attributes

Parameters
ncidNetCDF file or group ID.
varidVariable ID, or NC_GLOBAL for a global attribute.
nameAttribute name.
valuePointer that will get array of attribute value(s). Use nc_inq_attlen() to learn length.
Returns
NC_NOERR for success.
NC_EBADID Bad ncid.
NC_ENOTVAR Bad varid.
NC_EBADNAME Bad name. See NetCDF Names.
NC_EINVAL Invalid parameters.
NC_ENOTATT Can't find attribute.
NC_ECHAR Can't convert to or from NC_CHAR.
NC_ENOMEM Out of memory.
NC_ERANGE Data conversion went out of range.
Author
Glenn Davis, Ed Hartnett, Dennis Heimbigner

Definition at line 453 of file dattget.c.

◆ nc_get_att_float()

int nc_get_att_float ( int  ncid,
int  varid,
const char *  name,
float *  value 
)

Get an attribute array of type float.

Also see Getting Attributes

Parameters
ncidNetCDF file or group ID.
varidVariable ID, or NC_GLOBAL for a global attribute.
nameAttribute name.
valuePointer that will get array of attribute value(s). Use nc_inq_attlen() to learn length.
Returns
NC_NOERR for success.
NC_EBADID Bad ncid.
NC_ENOTVAR Bad varid.
NC_EBADNAME Bad name. See NetCDF Names.
NC_EINVAL Invalid parameters.
NC_ENOTATT Can't find attribute.
NC_ECHAR Can't convert to or from NC_CHAR.
NC_ENOMEM Out of memory.
NC_ERANGE Data conversion went out of range.
Author
Glenn Davis, Ed Hartnett, Dennis Heimbigner

Definition at line 420 of file dattget.c.

◆ nc_get_att_int()

int nc_get_att_int ( int  ncid,
int  varid,
const char *  name,
int *  value 
)

Get an attribute array of type int.

Also see Getting Attributes

Parameters
ncidNetCDF file or group ID.
varidVariable ID, or NC_GLOBAL for a global attribute.
nameAttribute name.
valuePointer that will get array of attribute value(s). Use nc_inq_attlen() to learn length.
Returns
NC_NOERR for success.
NC_EBADID Bad ncid.
NC_ENOTVAR Bad varid.
NC_EBADNAME Bad name. See NetCDF Names.
NC_EINVAL Invalid parameters.
NC_ENOTATT Can't find attribute.
NC_ECHAR Can't convert to or from NC_CHAR.
NC_ENOMEM Out of memory.
NC_ERANGE Data conversion went out of range.
Author
Glenn Davis, Ed Hartnett, Dennis Heimbigner

Definition at line 354 of file dattget.c.

◆ nc_get_att_long()

int nc_get_att_long ( int  ncid,
int  varid,
const char *  name,
long *  value 
)

Get an attribute array of type long.

Also see Getting Attributes

Parameters
ncidNetCDF file or group ID.
varidVariable ID, or NC_GLOBAL for a global attribute.
nameAttribute name.
valuePointer that will get array of attribute value(s). Use nc_inq_attlen() to learn length.
Returns
NC_NOERR for success.
NC_EBADID Bad ncid.
NC_ENOTVAR Bad varid.
NC_EBADNAME Bad name. See NetCDF Names.
NC_EINVAL Invalid parameters.
NC_ENOTATT Can't find attribute.
NC_ECHAR Can't convert to or from NC_CHAR.
NC_ENOMEM Out of memory.
NC_ERANGE Data conversion went out of range.
Author
Glenn Davis, Ed Hartnett, Dennis Heimbigner

Definition at line 387 of file dattget.c.

◆ nc_get_att_longlong()

int nc_get_att_longlong ( int  ncid,
int  varid,
const char *  name,
long long *  value 
)

Get an attribute array of type long long.

Also see Getting Attributes

Parameters
ncidNetCDF file or group ID.
varidVariable ID, or NC_GLOBAL for a global attribute.
nameAttribute name.
valuePointer that will get array of attribute value(s). Use nc_inq_attlen() to learn length.
Returns
NC_NOERR for success.
NC_EBADID Bad ncid.
NC_ENOTVAR Bad varid.
NC_EBADNAME Bad name. See NetCDF Names.
NC_EINVAL Invalid parameters.
NC_ENOTATT Can't find attribute.
NC_ECHAR Can't convert to or from NC_CHAR.
NC_ENOMEM Out of memory.
NC_ERANGE Data conversion went out of range.
Author
Ed Hartnett, Dennis Heimbigner

Definition at line 585 of file dattget.c.

◆ nc_get_att_schar()

int nc_get_att_schar ( int  ncid,
int  varid,
const char *  name,
signed char *  value 
)

Get an attribute of an signed char type.

Also see Getting Attributes

Parameters
ncidNetCDF file or group ID.
varidVariable ID, or NC_GLOBAL for a global attribute.
nameAttribute name.
valuePointer that will get array of attribute value(s). Use nc_inq_attlen() to learn length.
Returns
NC_NOERR for success.
NC_EBADID Bad ncid.
NC_ENOTVAR Bad varid.
NC_EBADNAME Bad name. See NetCDF Names.
NC_EINVAL Invalid parameters.
NC_ENOTATT Can't find attribute.
NC_ECHAR Can't convert to or from NC_CHAR.
NC_ENOMEM Out of memory.
NC_ERANGE Data conversion went out of range.
Author
Glenn Davis, Ed Hartnett, Dennis Heimbigner

Definition at line 255 of file dattget.c.

◆ nc_get_att_short()

int nc_get_att_short ( int  ncid,
int  varid,
const char *  name,
short *  value 
)

Get an attribute array of type short.

Also see Getting Attributes

Parameters
ncidNetCDF file or group ID.
varidVariable ID, or NC_GLOBAL for a global attribute.
nameAttribute name.
valuePointer that will get array of attribute value(s). Use nc_inq_attlen() to learn length.
Returns
NC_NOERR for success.
NC_EBADID Bad ncid.
NC_ENOTVAR Bad varid.
NC_EBADNAME Bad name. See NetCDF Names.
NC_EINVAL Invalid parameters.
NC_ENOTATT Can't find attribute.
NC_ECHAR Can't convert to or from NC_CHAR.
NC_ENOMEM Out of memory.
NC_ERANGE Data conversion went out of range.
Author
Glenn Davis, Ed Hartnett, Dennis Heimbigner

Definition at line 321 of file dattget.c.

◆ nc_get_att_string()

int nc_get_att_string ( int  ncid,
int  varid,
const char *  name,
char **  value 
)

Get an attribute array of type string.

This function gets an attribute from netCDF file. The nc_get_att() function works with any type of data including user defined types, but this function will retrieve attributes which are of type variable-length string.

Also see Getting Attributes

Note
Note that unlike most other nc_get_att functions, nc_get_att_string() allocates a chunk of memory which is returned to the calling function. This chunk of memory must be specifically deallocated with nc_free_string() to avoid any memory leaks. Also note that you must still preallocate the memory needed for the array of pointers passed to nc_get_att_string().
Parameters
ncidNetCDF file or group ID.
varidVariable ID, or NC_GLOBAL for a global attribute.
nameAttribute name.
valuePointer that will get array of attribute value(s). Use nc_inq_attlen() to learn length.

Example

#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <netcdf.h>
void check(int stat) {
if (stat != NC_NOERR) {
printf("NetCDF error: %s\n", nc_strerror(stat));
exit(1);
}
}
int main(int argc, char ** argv) {
int stat = 0;
int ncid = 0;
stat = nc_open("test.nc", NC_NOWRITE, &ncid); check(stat);
int varid = 0;
stat = nc_inq_varid(ncid, "variable", &varid); check(stat);
size_t attlen = 0;
stat = nc_inq_attlen(ncid, varid, "attribute", &attlen); check(stat);
char **string_attr = (char**)malloc(attlen * sizeof(char*));
memset(string_attr, 0, attlen * sizeof(char*));
stat = nc_get_att_string(ncid, varid, "attribute", string_attr); check(stat);
for (size_t k = 0; k < attlen; ++k) {
printf("variable:attribute[%d] = %s\n", k, string_attr[k]);
}
stat = nc_free_string(attlen, string_attr); check(stat);
free(string_attr);
stat = nc_close(ncid); check(stat);
return 0;
}
EXTERNL int nc_get_att_string(int ncid, int varid, const char *name, char **ip)
Get an attribute array of type string.
Definition: dattget.c:711
EXTERNL const char * nc_strerror(int ncerr)
Given an error number, return an error message.
Definition: derror.c:87
EXTERNL int nc_free_string(size_t len, char **data)
Free string space allocated by the library.
Definition: dvar.c:1316
Returns
NC_NOERR for success.
NC_EBADID Bad ncid.
NC_ENOTVAR Bad varid.
NC_EBADNAME Bad name. See NetCDF Names.
NC_EINVAL Invalid parameters.
NC_ENOTATT Can't find attribute.
NC_ECHAR Can't convert to or from NC_CHAR.
NC_ENOMEM Out of memory.
NC_ERANGE Data conversion went out of range.
Author
Ed Hartnett, Dennis Heimbigner

Definition at line 711 of file dattget.c.

◆ nc_get_att_text()

int nc_get_att_text ( int  ncid,
int  varid,
const char *  name,
char *  value 
)

Get a text attribute.

This function gets a text attribute from the netCDF file. Type conversions are not permitted.

Also see Getting Attributes

Parameters
ncidNetCDF file or group ID.
varidVariable ID, or NC_GLOBAL for a global attribute.
nameAttribute name.
valuePointer that will get array of attribute value(s). Use nc_inq_attlen() to learn length.
Note
The handling of NULL terminators is not specified by netCDF. C programs can write attributes with or without NULL terminators. It is up to the reader to know whether NULL terminators have been used, and, if not, to add a NULL terminator when reading text attributes.

Example

Here is an example using nc_get_att_text() to read a global attribute named title in an existing netCDF dataset named foo.nc.

In this example we learn the length of the attribute, so that an array may be allocated, adding 1 in case a NULL terminator is needed. We then take the precaution of setting the last element of the array to 0, to NULL terminate the string. If a NULL terminator was written with this attribute, strlen(title) will show the correct length (the number of chars before the first NULL terminator).

#include <netcdf.h>
...
int status;
int ncid;
int rh_id;
int t_len;
char *title;
...
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_attlen (ncid, NC_GLOBAL, "title", &t_len);
if (status != NC_NOERR) handle_error(status);
title = (char *) malloc(t_len + 1);
status = nc_get_att_text(ncid, NC_GLOBAL, "title", title);
if (status != NC_NOERR) handle_error(status);
title[t_len] = '\0';
...
EXTERNL int nc_get_att_text(int ncid, int varid, const char *name, char *ip)
Get a text attribute.
Definition: dattget.c:222
Returns
NC_NOERR for success.
NC_EBADID Bad ncid.
NC_ENOTVAR Bad varid.
NC_EBADNAME Bad name. See NetCDF Names.
NC_EINVAL Invalid parameters.
NC_ENOTATT Can't find attribute.
NC_ECHAR Can't convert to or from NC_CHAR.
NC_ENOMEM Out of memory.
NC_ERANGE Data conversion went out of range.
Author
Glenn Davis, Ed Hartnett, Dennis Heimbigner

Definition at line 222 of file dattget.c.

◆ nc_get_att_ubyte()

int nc_get_att_ubyte ( int  ncid,
int  varid,
const char *  name,
unsigned char *  value 
)

Get an attribute array of type unsigned char.

Also see Getting Attributes

Parameters
ncidNetCDF file or group ID.
varidVariable ID, or NC_GLOBAL for a global attribute.
nameAttribute name.
valuePointer that will get array of attribute value(s). Use nc_inq_attlen() to learn length.
Returns
NC_NOERR for success.
NC_EBADID Bad ncid.
NC_ENOTVAR Bad varid.
NC_EBADNAME Bad name. See NetCDF Names.
NC_EINVAL Invalid parameters.
NC_ENOTATT Can't find attribute.
NC_ECHAR Can't convert to or from NC_CHAR.
NC_ENOMEM Out of memory.
NC_ERANGE Data conversion went out of range.
Author
Ed Hartnett, Dennis Heimbigner

Definition at line 486 of file dattget.c.

◆ nc_get_att_uchar()

int nc_get_att_uchar ( int  ncid,
int  varid,
const char *  name,
unsigned char *  value 
)

Get an attribute of an signed char type.

Also see Getting Attributes

Parameters
ncidNetCDF file or group ID.
varidVariable ID, or NC_GLOBAL for a global attribute.
nameAttribute name.
valuePointer that will get array of attribute value(s). Use nc_inq_attlen() to learn length.
Returns
NC_NOERR for success.
NC_EBADID Bad ncid.
NC_ENOTVAR Bad varid.
NC_EBADNAME Bad name. See NetCDF Names.
NC_EINVAL Invalid parameters.
NC_ENOTATT Can't find attribute.
NC_ECHAR Can't convert to or from NC_CHAR.
NC_ENOMEM Out of memory.
NC_ERANGE Data conversion went out of range.
Author
Glenn Davis, Ed Hartnett, Dennis Heimbigner

Definition at line 288 of file dattget.c.

◆ nc_get_att_uint()

int nc_get_att_uint ( int  ncid,
int  varid,
const char *  name,
unsigned int *  value 
)

Get an attribute array of type unsigned int.

Also see Getting Attributes

Parameters
ncidNetCDF file or group ID.
varidVariable ID, or NC_GLOBAL for a global attribute.
nameAttribute name.
valuePointer that will get array of attribute value(s). Use nc_inq_attlen() to learn length.
Returns
NC_NOERR for success.
NC_EBADID Bad ncid.
NC_ENOTVAR Bad varid.
NC_EBADNAME Bad name. See NetCDF Names.
NC_EINVAL Invalid parameters.
NC_ENOTATT Can't find attribute.
NC_ECHAR Can't convert to or from NC_CHAR.
NC_ENOMEM Out of memory.
NC_ERANGE Data conversion went out of range.
Author
Ed Hartnett, Dennis Heimbigner

Definition at line 552 of file dattget.c.

◆ nc_get_att_ulonglong()

int nc_get_att_ulonglong ( int  ncid,
int  varid,
const char *  name,
unsigned long long *  value 
)

Get an attribute array of type unsigned long long.

Also see Getting Attributes

Parameters
ncidNetCDF file or group ID.
varidVariable ID, or NC_GLOBAL for a global attribute.
nameAttribute name.
valuePointer that will get array of attribute value(s). Use nc_inq_attlen() to learn length.
Returns
NC_NOERR for success.
NC_EBADID Bad ncid.
NC_ENOTVAR Bad varid.
NC_EBADNAME Bad name. See NetCDF Names.
NC_EINVAL Invalid parameters.
NC_ENOTATT Can't find attribute.
NC_ECHAR Can't convert to or from NC_CHAR.
NC_ENOMEM Out of memory.
NC_ERANGE Data conversion went out of range.
Author
Ed Hartnett, Dennis Heimbigner

Definition at line 618 of file dattget.c.

◆ nc_get_att_ushort()

int nc_get_att_ushort ( int  ncid,
int  varid,
const char *  name,
unsigned short *  value 
)

Get an attribute array of type unsigned short.

Also see Getting Attributes

Parameters
ncidNetCDF file or group ID.
varidVariable ID, or NC_GLOBAL for a global attribute.
nameAttribute name.
valuePointer that will get array of attribute value(s). Use nc_inq_attlen() to learn length.
Returns
NC_NOERR for success.
NC_EBADID Bad ncid.
NC_ENOTVAR Bad varid.
NC_EBADNAME Bad name. See NetCDF Names.
NC_EINVAL Invalid parameters.
NC_ENOTATT Can't find attribute.
NC_ECHAR Can't convert to or from NC_CHAR.
NC_ENOMEM Out of memory.
NC_ERANGE Data conversion went out of range.
Author
Ed Hartnett, Dennis Heimbigner

Definition at line 519 of file dattget.c.

◆ nc_inq_att()

int nc_inq_att ( int  ncid,
int  varid,
const char *  name,
nc_type xtypep,
size_t *  lenp 
)

Return information about a netCDF attribute.

The function nc_inq_att returns the attribute's type and length.

Parameters
ncidNetCDF or group ID, from a previous call to nc_open(), nc_create(), nc_def_grp(), or associated inquiry functions such as nc_inq_ncid().
varidVariable ID of the attribute's variable, or NC_GLOBAL for a global attribute.
namePointer to the location for the returned attribute NetCDF Names. Ignored if NULL.
xtypepPointer to location for returned attribute data type. Ignored if NULL.
lenpPointer to location for returned number of values currently stored in the attribute. For attributes of type NC_CHAR, you should not assume that this includes a trailing zero byte; it doesn't if the attribute was stored without a trailing zero byte, for example from a FORTRAN program. Before using the value as a C string, make sure it is null-terminated. Ignored if NULL.

Example

Here is an example using 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;
int ncid;
int rh_id;
nc_type vr_type, t_type;
size_t vr_len, t_len;
...
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);
EXTERNL int nc_inq_att(int ncid, int varid, const char *name, nc_type *xtypep, size_t *lenp)
Return information about a netCDF attribute.
Definition: dattinq.c:86
int nc_type
The nc_type type is just an int.
Definition: netcdf.h:25
Returns
NC_NOERR no error.
NC_EBADID bad ncid.
NC_ENOTVAR bad varid.
NC_EBADGRPID bad group ID.
NC_EBADNAME bad name.
NC_ENOTATT attribute not found.
NC_ECHAR illegal conversion to or from NC_CHAR.
NC_ENOMEM out of memory.
NC_ERANGE range error when converting data.
Author
Glenn Davis, Ed Hartnett, Dennis Heimbigner

Definition at line 86 of file dattinq.c.

◆ nc_inq_attid()

int nc_inq_attid ( int  ncid,
int  varid,
const char *  name,
int *  idp 
)

Find an attribute ID.

Parameters
ncidNetCDF or group ID, from a previous call to nc_open(), nc_create(), nc_def_grp(), or associated inquiry functions such as nc_inq_ncid().
varidVariable ID of the attribute's variable, or NC_GLOBAL for a global attribute.
nameAttribute NetCDF Names.
idpPointer to location for returned attribute number that specifies which attribute this is for this variable (or which global attribute). If you already know the attribute name, knowing its number is not very useful, because accessing information about an attribute requires its name.

Example

Here is an example using nc_inq_attid() from nc_test4/tst_vars2.c. In this example three attributes are created in a file. Then it is re-opened, and their IDs are checked. They will be 0, 1, and 2, in the order that the attributes were written to the file.

#include <netcdf.h>
...
printf("**** testing fill value with three other attributes...");
{
#define NUM_LEADERS 3
char leader[NUM_LEADERS][NC_MAX_NAME + 1] = {"hair_length_of_strategoi",
"hair_length_of_Miltiades",
"hair_length_of_Darius_I"};
short hair_length[NUM_LEADERS] = {3, 11, 4};
short short_in;
int a;
if (nc_create(FILE_NAME, cmode, &ncid)) ERR;
if (nc_def_dim(ncid, DIM1_NAME, DIM1_LEN, &dimids[0])) ERR;
if (nc_def_var(ncid, VAR_NAME, NC_BYTE, NUM_DIMS, dimids, &varid)) ERR;
for (a = 0; a < NUM_LEADERS; a++)
if (nc_put_att_short(ncid, varid, leader[a], NC_SHORT, 1, &hair_length[a])) ERR;
if (nc_put_att_schar(ncid, varid, _FillValue, NC_BYTE, 1, &fill_value)) ERR;
if (nc_close(ncid)) ERR;
if (nc_open(FILE_NAME, NC_NOWRITE, &ncid)) ERR;
for (a = 0; a < NUM_LEADERS; a++)
{
...
if (nc_inq_attid(ncid, 0, leader[a], &attnum_in)) ERR;
if (attnum_in != a) ERR;
}
if (nc_close(ncid)) ERR;
EXTERNL int nc_inq_attid(int ncid, int varid, const char *name, int *idp)
Find an attribute ID.
Definition: dattinq.c:164
EXTERNL int nc_put_att_schar(int ncid, int varid, const char *name, nc_type xtype, size_t len, const signed char *op)
Write an attribute of type signed char.
Definition: dattput.c:256
EXTERNL int nc_put_att_short(int ncid, int varid, const char *name, nc_type xtype, size_t len, const short *op)
Write an attribute of type short.
Definition: dattput.c:324
EXTERNL int nc_def_dim(int ncid, const char *name, size_t len, int *idp)
Define a new dimension.
Definition: ddim.c:121
EXTERNL int nc_def_var(int ncid, const char *name, nc_type xtype, int ndims, const int *dimidsp, int *varidp)
Define a new variable.
Definition: dvar.c:214
#define NC_BYTE
signed 1 byte integer
Definition: netcdf.h:35
#define NC_SHORT
signed 2 byte integer
Definition: netcdf.h:37
#define NC_MAX_NAME
Maximum for classic library.
Definition: netcdf.h:281
#define _FillValue
Name of fill value attribute.
Definition: netcdf.h:113
Returns
NC_NOERR no error.
NC_EBADID bad ncid.
NC_ENOTVAR bad varid.
NC_EBADGRPID bad group ID.
NC_EBADNAME bad name.
NC_ENOTATT attribute not found.
NC_ENOMEM out of memory.
Author
Glenn Davis, Ed Hartnett, Dennis Heimbigner

Definition at line 164 of file dattinq.c.

◆ nc_inq_attlen()

int nc_inq_attlen ( int  ncid,
int  varid,
const char *  name,
size_t *  lenp 
)

Find the length of an attribute.

Parameters
ncidNetCDF or group ID, from a previous call to nc_open(), nc_create(), nc_def_grp(), or associated inquiry functions such as nc_inq_ncid().
varidVariable ID of the attribute's variable, or NC_GLOBAL for a global or group attribute.
nameAttribute NetCDF Names.
lenpPointer to location for returned number of values currently stored in the attribute. Before using the value as a C string, make sure it is null-terminated. Ignored if NULL.

Example

Here is an example from nc_test4/tst_h_scalar.c which checks the attributes of an already-open netCDF file. In this code, the length of two attributes are checked, and found to be 1.

#include <netcdf.h>
...
int
check_attrs(int ncid, int obj)
{
int attid;
int natts = 0;
size_t len;
nc_type type;
char *vlstr;
char fixstr[10];
int x;
...
if (nc_inq_attlen(ncid, obj, VSTR_ATT1_NAME, &len)) ERR_GOTO;
if (len != 1) ERR_GOTO;
...
if (nc_inq_attlen(ncid, obj, VSTR_ATT2_NAME, &len)) ERR_GOTO;
if (len != 1) ERR_GOTO;
Returns
NC_NOERR no error.
NC_EBADID bad ncid.
NC_ENOTVAR bad varid.
NC_EBADGRPID bad group ID.
NC_EBADNAME bad name.
NC_ENOTATT attribute not found.
NC_ECHAR illegal conversion to or from NC_CHAR.
NC_ENOMEM out of memory.
NC_ERANGE range error when converting data.
Author
Glenn Davis, Ed Hartnett, Dennis Heimbigner

Definition at line 424 of file dattinq.c.

◆ nc_inq_attname()

int nc_inq_attname ( int  ncid,
int  varid,
int  attnum,
char *  name 
)

Find the name of an attribute.

Parameters
ncidNetCDF or group ID, from a previous call to nc_open(), nc_create(), nc_def_grp(), or associated inquiry functions such as nc_inq_ncid().
varidVariable ID of the attribute's variable, or NC_GLOBAL for a global attribute.
attnumAttribute number. The attributes for each variable are numbered from 0 (the first attribute) to natts-1, where natts is the number of attributes for the variable, as returned from a call to nc_inq_varnatts().
namePointer to the location for the returned attribute NetCDF Names.

Example

Here is an example from nc_test4/tst_atts3.c a variable of every type is added to a file, with names from the 'names' array. Then the file is re-opened, and the names of the attributes are checked in a for loop.

#include <netcdf.h>
...
#define NUM_ATTS 8
#define ATT_MAX_NAME 25
int
tst_att_ordering(int cmode)
{
int ncid;
char name[NUM_ATTS][ATT_MAX_NAME + 1] = {"Gc", "Gb", "Gs", "Gi", "Gf",
"Gd", "Gatt-name-dashes", "Gatt.name.dots"};
int len[NUM_ATTS] = {0, 2, 3, 3, 3, 3, 1, 1};
signed char b[2] = {-128, 127};
short s[3] = {-32768, 0, 32767};
int i[3] = {42, 0, -42};
float f[3] = {42.0, -42.0, 42.0};
double d[3] = {420.0, -420.0, 420.0};
int att_name_dashes = -1, att_name_dots = -2;
char name_in[NC_MAX_NAME];
int j;
if (nc_create(FILE_NAME, cmode, &ncid)) ERR;
...
if (nc_put_att_text(ncid, NC_GLOBAL, name[0], len[0], NULL)) ERR;
if (nc_put_att_schar(ncid, NC_GLOBAL, name[1], NC_BYTE, len[1], b)) ERR;
if (nc_put_att_short(ncid, NC_GLOBAL, name[2], NC_SHORT, len[2], s)) ERR;
if (nc_put_att_int(ncid, NC_GLOBAL, name[3], NC_INT, len[3], i)) ERR;
if (nc_put_att_float(ncid, NC_GLOBAL, name[4], NC_FLOAT, len[4], f)) ERR;
if (nc_put_att_double(ncid, NC_GLOBAL, name[5], NC_DOUBLE, len[5], d)) ERR;
if (nc_put_att_int(ncid, NC_GLOBAL, name[6], NC_INT, len[6], &att_name_dashes)) ERR;
if (nc_put_att_int(ncid, NC_GLOBAL, name[7], NC_INT, len[7], &att_name_dots)) ERR;
if (nc_close(ncid)) ERR;
...
if (nc_open(FILE_NAME, 0, &ncid)) ERR;
for (j = 0; j < NUM_ATTS; j++)
{
if (nc_inq_attname(ncid, NC_GLOBAL, j, name_in)) ERR;
if (strcmp(name_in, name[j])) ERR;
}
if (nc_close(ncid)) ERR;
EXTERNL int nc_put_att_text(int ncid, int varid, const char *name, size_t len, const char *op)
Write a text attribute.
Definition: dattput.c:153
EXTERNL int nc_put_att_float(int ncid, int varid, const char *name, nc_type xtype, size_t len, const float *op)
Write an attribute of type float.
Definition: dattput.c:426
EXTERNL int nc_put_att_int(int ncid, int varid, const char *name, nc_type xtype, size_t len, const int *op)
Write an attribute of type int.
Definition: dattput.c:358
EXTERNL int nc_inq_attname(int ncid, int varid, int attnum, char *name)
Find the name of an attribute.
Definition: dattinq.c:255
EXTERNL int nc_put_att_double(int ncid, int varid, const char *name, nc_type xtype, size_t len, const double *op)
Write an attribute of type double.
Definition: dattput.c:460
#define NC_DOUBLE
double precision floating point number
Definition: netcdf.h:41
#define NC_FLOAT
single precision floating point number
Definition: netcdf.h:40
Returns
NC_NOERR no error.
NC_EBADID bad ncid.
NC_ENOTVAR bad varid.
NC_EBADGRPID bad group ID.
NC_EBADNAME bad name.
NC_ENOTATT attribute not found.
NC_ECHAR illegal conversion to or from NC_CHAR.
NC_ENOMEM out of memory.
NC_ERANGE range error when converting data.
Author
Glenn Davis, Ed Hartnett, Dennis Heimbigner

Definition at line 255 of file dattinq.c.

◆ nc_inq_atttype()

int nc_inq_atttype ( int  ncid,
int  varid,
const char *  name,
nc_type xtypep 
)

Find the type of an attribute.

Parameters
ncidNetCDF or group ID, from a previous call to nc_open(), nc_create(), nc_def_grp(), or associated inquiry functions such as nc_inq_ncid().
varidVariable ID of the attribute's variable, or NC_GLOBAL for a global or group attribute.
nameAttribute NetCDF Names.
xtypepPointer to location for returned attribute data type.

Example

Here is an example from nc_test4/tst_h_refs.c. In this example, a file with an integer attribute is open. It's type is confirmed to be NC_INT.

#include <netcdf.h>
...
printf("*** Checking accessing file through netCDF-4 API...");
{
int ncid, varid, attid;
nc_type type;
if (nc_open(FILE_NAME, NC_NOWRITE, &ncid)) ERR;
...
if (nc_inq_atttype(ncid, NC_GLOBAL, INT_ATT_NAME, &type)) ERR;
if (type != NC_INT) ERR;
EXTERNL int nc_inq_atttype(int ncid, int varid, const char *name, nc_type *xtypep)
Find the type of an attribute.
Definition: dattinq.c:358
Returns
NC_NOERR no error.
NC_EBADID bad ncid.
NC_ENOTVAR bad varid.
NC_EBADGRPID bad group ID.
NC_EBADNAME bad name.
NC_ENOTATT attribute not found.
NC_ECHAR illegal conversion to or from NC_CHAR.
NC_ENOMEM out of memory.
NC_ERANGE range error when converting data.
Author
Glenn Davis, Ed Hartnett, Dennis Heimbigner

Definition at line 358 of file dattinq.c.

◆ nc_inq_natts()

int nc_inq_natts ( int  ncid,
int *  nattsp 
)

Find number of global or group attributes.

Parameters
ncidNetCDF or group ID, from a previous call to nc_open(), nc_create(), nc_def_grp(), or associated inquiry functions such as nc_inq_ncid().
nattspPointer where number of global or group attributes will be written. Ignored if NULL.

Example

Here is an example from nc_test4/tst_vars.c:

#include <netcdf.h>
...
int
check_4D_example(char *file_name, int expected_format)
{
int ncid;
int format, ndims_in, nvars_in, natts_in;
...
if (nc_open(file_name, 0, &ncid)) ERR;
...
if (nc_inq_natts(ncid, &natts_in)) ERR;
if (natts_in != 0) ERR;
EXTERNL int nc_inq_natts(int ncid, int *nattsp)
Find number of global or group attributes.
Definition: dattinq.c:300
Returns
NC_NOERR no error.
NC_EBADID bad ncid.
NC_EBADGRPID bad group ID.
Author
Glenn Davis, Ed Hartnett, Dennis Heimbigner

Definition at line 300 of file dattinq.c.

◆ nc_put_att()

int nc_put_att ( int  ncid,
int  varid,
const char *  name,
nc_type  xtype,
size_t  len,
const void *  value 
)

Write an attribute of any type.

Also see Writing Attributes

Parameters
ncidNetCDF file or group ID.
varidVariable ID, or NC_GLOBAL for a global attribute.
nameAttribute NetCDF Names.
xtypeThe type of attribute to write. Data will be converted to this type.
lenNumber of values provided for the attribute.
valuePointer to one or more values.

Example

Here is an example using nc_put_att_double() to add a variable attribute named valid_range for a netCDF variable named rh and nc_put_att_text() to add a global attribute named title to an existing netCDF dataset named foo.nc:

#include <netcdf.h>
...
int status;
int ncid;
int rh_id;
static double rh_range[] = {0.0, 100.0};
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);
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",
strlen(title), title)
if (status != NC_NOERR) handle_error(status);
...
status = nc_enddef(ncid);
if (status != NC_NOERR) handle_error(status);
Returns
NC_NOERR No error.
NC_EINVAL Invalid or global _FillValue.
NC_ENOTVAR Couldn't find varid.
NC_EBADTYPE Fill value and var must be same type.
NC_ENOMEM Out of memory
NC_ELATEFILL Too late to set fill value.
Author
Glenn Davis, Ed Hartnett, Dennis Heimbigner

Definition at line 222 of file dattput.c.

◆ nc_put_att_double()

int nc_put_att_double ( int  ncid,
int  varid,
const char *  name,
nc_type  xtype,
size_t  len,
const double *  value 
)

Write an attribute of type double.

Also see Writing Attributes

Parameters
ncidNetCDF file or group ID.
varidVariable ID, or NC_GLOBAL for a global attribute.
nameAttribute NetCDF Names.
xtypeThe type of attribute to write. Data will be converted to this type.
lenNumber of values provided for the attribute.
valuePointer to one or more values.
Returns
NC_NOERR No error.
NC_EINVAL Invalid or global _FillValue.
NC_ENOTVAR Couldn't find varid.
NC_EBADTYPE Fill value and var must be same type.
NC_ENOMEM Out of memory
NC_ELATEFILL Too late to set fill value.
Author
Glenn Davis, Ed Hartnett, Dennis Heimbigner

Definition at line 460 of file dattput.c.

◆ nc_put_att_float()

int nc_put_att_float ( int  ncid,
int  varid,
const char *  name,
nc_type  xtype,
size_t  len,
const float *  value 
)

Write an attribute of type float.

Also see Writing Attributes

Parameters
ncidNetCDF file or group ID.
varidVariable ID, or NC_GLOBAL for a global attribute.
nameAttribute NetCDF Names.
xtypeThe type of attribute to write. Data will be converted to this type.
lenNumber of values provided for the attribute.
valuePointer to one or more values.
Returns
NC_NOERR No error.
NC_EINVAL Invalid or global _FillValue.
NC_ENOTVAR Couldn't find varid.
NC_EBADTYPE Fill value and var must be same type.
NC_ENOMEM Out of memory
NC_ELATEFILL Too late to set fill value.
Author
Glenn Davis, Ed Hartnett, Dennis Heimbigner

Definition at line 426 of file dattput.c.

◆ nc_put_att_int()

int nc_put_att_int ( int  ncid,
int  varid,
const char *  name,
nc_type  xtype,
size_t  len,
const int *  value 
)

Write an attribute of type int.

Also see Writing Attributes

Parameters
ncidNetCDF file or group ID.
varidVariable ID, or NC_GLOBAL for a global attribute.
nameAttribute NetCDF Names.
xtypeThe type of attribute to write. Data will be converted to this type.
lenNumber of values provided for the attribute.
valuePointer to one or more values.
Returns
NC_NOERR No error.
NC_EINVAL Invalid or global _FillValue.
NC_ENOTVAR Couldn't find varid.
NC_EBADTYPE Fill value and var must be same type.
NC_ENOMEM Out of memory
NC_ELATEFILL Too late to set fill value.
Author
Glenn Davis, Ed Hartnett, Dennis Heimbigner

Definition at line 358 of file dattput.c.

◆ nc_put_att_long()

int nc_put_att_long ( int  ncid,
int  varid,
const char *  name,
nc_type  xtype,
size_t  len,
const long *  value 
)

Write an attribute of type long.

Also see Writing Attributes

Parameters
ncidNetCDF file or group ID.
varidVariable ID, or NC_GLOBAL for a global attribute.
nameAttribute NetCDF Names.
xtypeThe type of attribute to write. Data will be converted to this type.
lenNumber of values provided for the attribute.
valuePointer to one or more values.
Returns
NC_NOERR No error.
NC_EINVAL Invalid or global _FillValue.
NC_ENOTVAR Couldn't find varid.
NC_EBADTYPE Fill value and var must be same type.
NC_ENOMEM Out of memory
NC_ELATEFILL Too late to set fill value.
Author
Glenn Davis, Ed Hartnett, Dennis Heimbigner

Definition at line 392 of file dattput.c.

◆ nc_put_att_longlong()

int nc_put_att_longlong ( int  ncid,
int  varid,
const char *  name,
nc_type  xtype,
size_t  len,
const long long *  value 
)

Write an attribute of type long long.

Also see Writing Attributes

Parameters
ncidNetCDF file or group ID.
varidVariable ID, or NC_GLOBAL for a global attribute.
nameAttribute NetCDF Names.
xtypeThe type of attribute to write. Data will be converted to this type.
lenNumber of values provided for the attribute.
valuePointer to one or more values.
Returns
NC_NOERR No error.
NC_EINVAL Invalid or global _FillValue.
NC_ENOTVAR Couldn't find varid.
NC_EBADTYPE Fill value and var must be same type.
NC_ENOMEM Out of memory
NC_ELATEFILL Too late to set fill value.
Author
Ed Hartnett, Dennis Heimbigner

Definition at line 596 of file dattput.c.

◆ nc_put_att_schar()

int nc_put_att_schar ( int  ncid,
int  varid,
const char *  name,
nc_type  xtype,
size_t  len,
const signed char *  value 
)

Write an attribute of type signed char.

Also see Writing Attributes

Parameters
ncidNetCDF file or group ID.
varidVariable ID, or NC_GLOBAL for a global attribute.
nameAttribute NetCDF Names.
xtypeThe type of attribute to write. Data will be converted to this type.
lenNumber of values provided for the attribute.
valuePointer to one or more values.
Returns
NC_NOERR No error.
NC_EINVAL Invalid or global _FillValue.
NC_ENOTVAR Couldn't find varid.
NC_EBADTYPE Fill value and var must be same type.
NC_ENOMEM Out of memory
NC_ELATEFILL Too late to set fill value.
Author
Glenn Davis, Ed Hartnett, Dennis Heimbigner

Definition at line 256 of file dattput.c.

◆ nc_put_att_short()

int nc_put_att_short ( int  ncid,
int  varid,
const char *  name,
nc_type  xtype,
size_t  len,
const short *  value 
)

Write an attribute of type short.

Also see Writing Attributes

Parameters
ncidNetCDF file or group ID.
varidVariable ID, or NC_GLOBAL for a global attribute.
nameAttribute NetCDF Names.
xtypeThe type of attribute to write. Data will be converted to this type.
lenNumber of values provided for the attribute.
valuePointer to one or more values.
Returns
NC_NOERR No error.
NC_EINVAL Invalid or global _FillValue.
NC_ENOTVAR Couldn't find varid.
NC_EBADTYPE Fill value and var must be same type.
NC_ENOMEM Out of memory
NC_ELATEFILL Too late to set fill value.
Author
Glenn Davis, Ed Hartnett, Dennis Heimbigner

Definition at line 324 of file dattput.c.

◆ nc_put_att_string()

int nc_put_att_string ( int  ncid,
int  varid,
const char *  name,
size_t  len,
const char **  value 
)

Write a string attribute.

The function nc_put_att_string adds or changes a variable attribute or global attribute of an open netCDF dataset. The string type is only available in netCDF-4/HDF5 files, when NC_CLASSIC_MODEL has not been used in nc_create().

Also see Writing Attributes

Parameters
ncidNetCDF file or group ID.
varidVariable ID, or NC_GLOBAL for a global attribute.
nameAttribute NetCDF Names.
lenNumber of values provided for the attribute.
valuePointer to one or more values.
Returns
NC_NOERR No error.
NC_EINVAL Invalid or global _FillValue.
NC_ENOTVAR Couldn't find varid.
NC_EBADTYPE Fill value and var must be same type.
NC_ENOMEM Out of memory
NC_ELATEFILL Too late to set fill value.
Author
Ed Hartnett, Dennis Heimbigner

Definition at line 75 of file dattput.c.

◆ nc_put_att_text()

int nc_put_att_text ( int  ncid,
int  varid,
const char *  name,
size_t  len,
const char *  value 
)

Write a text attribute.

Add or change a text attribute. 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 for classic formats (or netCDF-4/HDF5 with NC_CLASSIC_MODEL).

Type conversion is not available with text attributes.

Also see Writing Attributes

Note
Whether or not this length includes the NULL character in C char arrays is a user decision. If the NULL character is not written, then all C programs must add the NULL character after reading a text attribute.
Parameters
ncidNetCDF file or group ID.
varidVariable ID, or NC_GLOBAL for a global attribute.
nameAttribute NetCDF Names.
lenThe length of the text array.
valuePointer to the start of the character array.

Example

Here is an example using nc_put_att_double() to add a variable attribute named valid_range for a netCDF variable named rh and nc_put_att_text() to add a global attribute named title to an existing netCDF dataset named foo.nc:

#include <netcdf.h>
...
int status;
int ncid;
int rh_id;
static double rh_range[] = {0.0, 100.0};
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);
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",
strlen(title), title)
if (status != NC_NOERR) handle_error(status);
...
status = nc_enddef(ncid);
if (status != NC_NOERR) handle_error(status);
Returns
NC_NOERR No error.
NC_EINVAL Invalid or global _FillValue.
NC_ENOTVAR Couldn't find varid.
NC_EBADTYPE Fill value and var must be same type.
NC_ENOMEM Out of memory
NC_ELATEFILL Too late to set fill value.
Author
Glenn Davis, Ed Hartnett, Dennis Heimbigner

Definition at line 153 of file dattput.c.

◆ nc_put_att_ubyte()

int nc_put_att_ubyte ( int  ncid,
int  varid,
const char *  name,
nc_type  xtype,
size_t  len,
const unsigned char *  value 
)

Write an attribute of type unsigned char.

Also see Writing Attributes

Parameters
ncidNetCDF file or group ID.
varidVariable ID, or NC_GLOBAL for a global attribute.
nameAttribute NetCDF Names.
xtypeThe type of attribute to write. Data will be converted to this type.
lenNumber of values provided for the attribute.
valuePointer to one or more values.
Returns
NC_NOERR No error.
NC_EINVAL Invalid or global _FillValue.
NC_ENOTVAR Couldn't find varid.
NC_EBADTYPE Fill value and var must be same type.
NC_ENOMEM Out of memory
NC_ELATEFILL Too late to set fill value.
Author
Ed Hartnett, Dennis Heimbigner

Definition at line 494 of file dattput.c.

◆ nc_put_att_uchar()

int nc_put_att_uchar ( int  ncid,
int  varid,
const char *  name,
nc_type  xtype,
size_t  len,
const unsigned char *  value 
)

Write an attribute of type unsigned char.

Also see Writing Attributes

Parameters
ncidNetCDF file or group ID.
varidVariable ID, or NC_GLOBAL for a global attribute.
nameAttribute NetCDF Names.
xtypeThe type of attribute to write. Data will be converted to this type.
lenNumber of values provided for the attribute.
valuePointer to one or more values.
Returns
NC_NOERR No error.
NC_EINVAL Invalid or global _FillValue.
NC_ENOTVAR Couldn't find varid.
NC_EBADTYPE Fill value and var must be same type.
NC_ENOMEM Out of memory
NC_ELATEFILL Too late to set fill value.
Author
Glenn Davis, Ed Hartnett, Dennis Heimbigner

Definition at line 290 of file dattput.c.

◆ nc_put_att_uint()

int nc_put_att_uint ( int  ncid,
int  varid,
const char *  name,
nc_type  xtype,
size_t  len,
const unsigned int *  value 
)

Write an attribute of type unsigned int.

Also see Writing Attributes

Parameters
ncidNetCDF file or group ID.
varidVariable ID, or NC_GLOBAL for a global attribute.
nameAttribute NetCDF Names.
xtypeThe type of attribute to write. Data will be converted to this type.
lenNumber of values provided for the attribute.
valuePointer to one or more values.
Returns
NC_NOERR No error.
NC_EINVAL Invalid or global _FillValue.
NC_ENOTVAR Couldn't find varid.
NC_EBADTYPE Fill value and var must be same type.
NC_ENOMEM Out of memory
NC_ELATEFILL Too late to set fill value.
Author
Ed Hartnett, Dennis Heimbigner

Definition at line 562 of file dattput.c.

◆ nc_put_att_ulonglong()

int nc_put_att_ulonglong ( int  ncid,
int  varid,
const char *  name,
nc_type  xtype,
size_t  len,
const unsigned long long *  value 
)

Write an attribute of type unsigned long long.

Also see Writing Attributes

Parameters
ncidNetCDF file or group ID.
varidVariable ID, or NC_GLOBAL for a global attribute.
nameAttribute NetCDF Names.
xtypeThe type of attribute to write. Data will be converted to this type.
lenNumber of values provided for the attribute.
valuePointer to one or more values.
Returns
NC_NOERR No error.
NC_EINVAL Invalid or global _FillValue.
NC_ENOTVAR Couldn't find varid.
NC_EBADTYPE Fill value and var must be same type.
NC_ENOMEM Out of memory
NC_ELATEFILL Too late to set fill value.
Author
Ed Hartnett, Dennis Heimbigner

Definition at line 631 of file dattput.c.

◆ nc_put_att_ushort()

int nc_put_att_ushort ( int  ncid,
int  varid,
const char *  name,
nc_type  xtype,
size_t  len,
const unsigned short *  value 
)

Write an attribute of type unsigned short.

Also see Writing Attributes

Parameters
ncidNetCDF file or group ID.
varidVariable ID, or NC_GLOBAL for a global attribute.
nameAttribute NetCDF Names.
xtypeThe type of attribute to write. Data will be converted to this type.
lenNumber of values provided for the attribute.
valuePointer to one or more values.
Returns
NC_NOERR No error.
NC_EINVAL Invalid or global _FillValue.
NC_ENOTVAR Couldn't find varid.
NC_EBADTYPE Fill value and var must be same type.
NC_ENOMEM Out of memory
NC_ELATEFILL Too late to set fill value.
Author
Ed Hartnett, Dennis Heimbigner

Definition at line 528 of file dattput.c.

◆ nc_rename_att()

int nc_rename_att ( int  ncid,
int  varid,
const char *  name,
const char *  newname 
)

Rename an attribute.

The function nc_rename_att() changes the name of an attribute. In classic formats, if the new name is longer than the original name, the netCDF dataset must be in define mode. In netCDF-4/HDF5 files, attributes may be renamed at any time. You cannot rename an attribute to have the same name as another attribute of the same variable.

Parameters
ncidNetCDF or group ID, from a previous call to nc_open(), nc_create(), nc_def_grp(), or associated inquiry functions such as nc_inq_ncid().
varidVariable ID of the attribute's variable, or NC_GLOBAL for a global attribute.
nameAttribute NetCDF Names.
newnameThe new attribute NetCDF Names.

Example

Here is an example using 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;
int ncid;
int rh_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);
...
status = nc_rename_att(ncid, rh_id, "units", "Units");
if (status != NC_NOERR) handle_error(status);
EXTERNL int nc_rename_att(int ncid, int varid, const char *name, const char *newname)
Rename an attribute.
Definition: datt.c:113
Returns
NC_NOERR No error.
NC_EBADID Bad ncid.
NC_ENOTVAR Bad varid.
NC_EBADNAME Bad name.
NC_EMAXNAME New name too long.
NC_EINVAL Name or new name not provided.
NC_ENAMEINUSE Name already in use.
NC_EPERM File was opened read only.
NC_ENOTINDEFINE File is not in define mode.
NC_ENOTATT Attribute not found.
NC_EHDFERR Failure at HDF5 layer.
NC_ENOMEM Out of memory.
Author
Glenn Davis, Ed Hartnett, Dennis Heimbigner

Definition at line 113 of file datt.c.