The function nc_put_att_ type adds or changes a variable attribute or global attribute of an open netCDF dataset. If this attribute is new, or if the space required to store the attribute is greater than before, the netCDF dataset must be in define mode.
With netCDF-4 files, nc_put_att will notice if you are writing a _Fill_Value_ attribute, and will tell the HDF5 layer to use the specified fill value for that variable.
Although it's possible to create attributes of all types, text and double attributes are adequate for most purposes.
Use the nc_put_att function to create attributes of any type, including user-defined types. We recommend using the type safe versions of this function whenever possible.
int nc_put_att_text (int ncid, int varid, const char *name, size_t len, const char *tp); int nc_put_att_uchar (int ncid, int varid, const char *name, nc_type xtype, size_t len, const unsigned char *up); int nc_put_att_schar (int ncid, int varid, const char *name, nc_type xtype, size_t len, const signed char *cp); int nc_put_att_short (int ncid, int varid, const char *name, nc_type xtype, size_t len, const short *sp); int nc_put_att_int (int ncid, int varid, const char *name, nc_type xtype, size_t len, const int *ip); int nc_put_att_long (int ncid, int varid, const char *name, nc_type xtype, size_t len, const long *lp); int nc_put_att_float (int ncid, int varid, const char *name, nc_type xtype, size_t len, const float *fp); int nc_put_att_double (int ncid, int varid, const char *name, nc_type xtype, size_t len, const double *dp); int nc_put_att_ubyte (int ncid, int varid, const char *name, nc_type xtype, size_t len, const unsigned char *op); int nc_put_att_ushort (int ncid, int varid, const char *name, nc_type xtype, size_t len, const unsigned short *op); int nc_put_att_uint (int ncid, int varid, const char *name, nc_type xtype, size_t len, const unsigned int *op); int nc_put_att_longlong (int ncid, int varid, const char *name, nc_type xtype, size_t len, const long long *op); int nc_put_att_ulonglong (int ncid, int varid, const char *name, nc_type xtype, size_t len, const unsigned long long *op); int nc_put_att_string (int ncid, int varid, const char *name, size_t len, const char **op); int nc_put_att (int ncid, int varid, const char *name, nc_type xtype, size_t len, const void *op);
ncid
varid
name
xtype
len
tp, up, cp, sp, ip, lp, fp, or dp
nc_put_att_ type returns the value NC_NOERR if no errors occurred. Otherwise, the returned status indicates an error. Possible causes of errors include:
NC_NOERR
NC_EINVAL
NC_ENOTVAR
NC_EBADTYPE
NC_ENOMEM
NC_EFILLVALUE
Here is an example using nc_put_att_double to add a variable attribute named valid_range for a netCDF variable named rh and a global attribute named title to an existing netCDF dataset named foo.nc:
#include <netcdf.h> ... int status; /* error status */ int ncid; /* netCDF ID */ int rh_id; /* variable ID */ static double rh_range[] = {0.0, 100.0};/* attribute vals */ static char title[] = "example netCDF dataset"; ... status = nc_open("foo.nc", NC_WRITE, &ncid); if (status != NC_NOERR) handle_error(status); ... status = nc_redef(ncid); /* enter define mode */ if (status != NC_NOERR) handle_error(status); status = nc_inq_varid (ncid, "rh", &rh_id); if (status != NC_NOERR) handle_error(status); ... status = nc_put_att_double (ncid, rh_id, "valid_range", NC_DOUBLE, 2, rh_range); if (status != NC_NOERR) handle_error(status); status = nc_put_att_text (ncid, NC_GLOBAL, "title", strlen(title), title) if (status != NC_NOERR) handle_error(status); ... status = nc_enddef(ncid); /* leave define mode */ if (status != NC_NOERR) handle_error(status);