Next: , Previous: nc_inq_att Family, Up: Attributes


5.4 Get Attribute's Values:nc_get_att_ type

Members of the nc_get_att_ type family of functions get the value(s) of a netCDF attribute, given its variable ID and name.

Usage

     int nc_get_att_text   (int ncid, int varid, const char *name,
                            char *tp);
     int nc_get_att_uchar  (int ncid, int varid, const char *name,
                            unsigned char *up);
     int nc_get_att_schar  (int ncid, int varid, const char *name,
                            signed char *cp);
     int nc_get_att_short  (int ncid, int varid, const char *name,
                            short *sp);
     int nc_get_att_int    (int ncid, int varid, const char *name,
                            int *ip);
     int nc_get_att_long   (int ncid, int varid, const char *name,
                            long *lp);
     int nc_get_att_float  (int ncid, int varid, const char *name,
                            float *fp);
     int nc_get_att_double (int ncid, int varid, const char *name,
                            double *dp);
ncid
NetCDF ID, from a previous call to nc_open or nc_create.
varid
Variable ID of the attribute's variable, or NC_GLOBAL for a global attribute.
name
Attribute name.
tp
up
cp
sp
ip
lp
fp
dp
Pointer to location for returned attribute value(s). All elements of the vector of attribute values are returned, so you must allocate enough space to hold them. For attributes of type NC_CHAR, you should not assume that the returned values include a trailing zero byte; they won'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. If you don't know how much space to reserve, call nc_inq_attlen first to find out the length of the attribute.

Errors

nc_get_att_ type returns the value NC_NOERR if no errors occurred. Otherwise, the returned status indicates an error. Possible causes of errors include:

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 and a global attribute named title in an existing 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;               /* error status */
     int  ncid;                 /* netCDF ID */
     int  rh_id;                /* variable ID */
     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 */
     
        ...
     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);
        ...
     /* find out how much space is needed for attribute values */
     status = nc_inq_attlen (ncid, rh_id, "valid_range", &vr_len);
     if (status != NC_NOERR) handle_error(status);
     status = nc_inq_attlen (ncid, NC_GLOBAL, "title", &t_len);
     if (status != NC_NOERR) handle_error(status);
     
     /* allocate required space before retrieving values */
     vr_val = (double *) malloc(vr_len * sizeof(double));
     title = (char *) malloc(t_len + 1);  /* + 1 for trailing null */
     
     /* get attribute values */
     status = nc_get_att_double(ncid, rh_id, "valid_range", vr_val);
     if (status != NC_NOERR) handle_error(status);
     status = nc_get_att_text(ncid, NC_GLOBAL, "title", title);
     if (status != NC_NOERR) handle_error(status);
     title[t_len] = '\0';       /* null terminate */
        ...