NetCDF  4.9.2
dattget.c File Reference

Attribute functions. More...

#include "ncdispatch.h"
Include dependency graph for dattget.c:

Go to the source code of this file.

Functions

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_double (int ncid, int varid, const char *name, double *value)
 Get an attribute array of type double. 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_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_longlong (int ncid, int varid, const char *name, long long *value)
 Get an attribute array of type long long. 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_short (int ncid, int varid, const char *name, short *value)
 Get an attribute array of type short. More...
 
int nc_get_att_string (int ncid, int varid, const char *name, char **value)
 Get an attribute array of type string. More...
 
int nc_get_att_text (int ncid, int varid, const char *name, char *value)
 Get a text attribute. 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_uchar (int ncid, int varid, const char *name, unsigned char *value)
 Get an attribute of an signed char type. 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_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_ushort (int ncid, int varid, const char *name, unsigned short *value)
 Get an attribute array of type unsigned short. More...
 

Detailed Description

Attribute functions.

These functions in this file read attributes.

Definition in file dattget.c.