2019-11-07 22:09:54 +08:00
|
|
|
/* Copyright 2018 University Corporation for Atmospheric
|
|
|
|
Research/Unidata. See copyright file for more info. */
|
|
|
|
/**
|
|
|
|
* @file
|
|
|
|
* Attribute functions
|
|
|
|
*
|
|
|
|
* These functions in this file read attributes.
|
|
|
|
*/
|
2011-07-12 00:04:49 +08:00
|
|
|
|
|
|
|
#include "ncdispatch.h"
|
|
|
|
|
2019-11-07 22:09:54 +08:00
|
|
|
/**
|
2019-11-08 20:19:51 +08:00
|
|
|
* @anchor getting_attributes
|
2019-11-07 22:09:54 +08:00
|
|
|
* @name Getting Attributes
|
|
|
|
*
|
|
|
|
* Functions to get the values of attributes.
|
2019-11-08 19:45:45 +08:00
|
|
|
*
|
2019-11-08 20:19:51 +08:00
|
|
|
* 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.
|
2019-11-08 19:45:45 +08:00
|
|
|
*
|
|
|
|
* @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.
|
2022-05-29 13:43:56 +08:00
|
|
|
*
|
|
|
|
* <h1>Example</h1>
|
|
|
|
*
|
|
|
|
* 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.
|
|
|
|
|
|
|
|
@code
|
|
|
|
#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);
|
|
|
|
...
|
|
|
|
@endcode
|
2011-07-12 00:04:49 +08:00
|
|
|
*/
|
2019-11-08 20:19:51 +08:00
|
|
|
/**@{*/ /* Start doxygen member group. */
|
2017-11-13 21:27:05 +08:00
|
|
|
|
2019-11-07 22:09:54 +08:00
|
|
|
/**
|
|
|
|
* @ingroup attributes
|
|
|
|
* Get an attribute of any type.
|
|
|
|
*
|
2019-11-08 02:47:27 +08:00
|
|
|
* 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.
|
2019-11-07 22:09:54 +08:00
|
|
|
*
|
2019-11-08 20:19:51 +08:00
|
|
|
* Also see @ref getting_attributes "Getting Attributes"
|
|
|
|
*
|
2019-11-08 19:45:45 +08:00
|
|
|
* @param ncid NetCDF file or group ID.
|
|
|
|
* @param varid Variable ID, or ::NC_GLOBAL for a global attribute.
|
2019-11-07 22:09:54 +08:00
|
|
|
* @param name Attribute name.
|
2019-11-08 19:45:45 +08:00
|
|
|
* @param value Pointer that will get array of attribute value(s). Use
|
|
|
|
* nc_inq_attlen() to learn length.
|
2019-11-07 22:09:54 +08:00
|
|
|
*
|
|
|
|
* @note See documentation for nc_get_att_string() regarding a special
|
|
|
|
* case where memory must be explicitly released.
|
|
|
|
*
|
2019-11-08 02:47:27 +08:00
|
|
|
* <h1>Example</h1>
|
|
|
|
*
|
|
|
|
* 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.
|
2019-11-07 22:09:54 +08:00
|
|
|
*
|
2017-11-13 21:27:05 +08:00
|
|
|
@code
|
|
|
|
#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;
|
|
|
|
@endcode
|
|
|
|
|
2019-11-08 02:47:27 +08:00
|
|
|
* @return ::NC_NOERR for success.
|
|
|
|
* @return ::NC_EBADID Bad ncid.
|
|
|
|
* @return ::NC_ENOTVAR Bad varid.
|
|
|
|
* @return ::NC_EBADNAME Bad name. See \ref object_name.
|
|
|
|
* @return ::NC_EINVAL Invalid parameters.
|
|
|
|
* @return ::NC_ENOTATT Can't find attribute.
|
|
|
|
* @return ::NC_ECHAR Can't convert to or from NC_CHAR.
|
|
|
|
* @return ::NC_ENOMEM Out of memory.
|
|
|
|
* @return ::NC_ERANGE Data conversion went out of range.
|
|
|
|
*
|
|
|
|
* @author Glenn Davis, Ed Hartnett, Dennis Heimbigner
|
2011-07-12 00:04:49 +08:00
|
|
|
*/
|
|
|
|
int
|
|
|
|
nc_get_att(int ncid, int varid, const char *name, void *value)
|
|
|
|
{
|
|
|
|
NC* ncp;
|
2012-03-17 06:29:09 +08:00
|
|
|
int stat = NC_NOERR;
|
2011-07-12 00:04:49 +08:00
|
|
|
nc_type xtype;
|
|
|
|
|
|
|
|
if ((stat = NC_check_id(ncid, &ncp)))
|
|
|
|
return stat;
|
|
|
|
|
|
|
|
/* Need to get the type */
|
|
|
|
if ((stat = nc_inq_atttype(ncid, varid, name, &xtype)))
|
|
|
|
return stat;
|
|
|
|
|
2016-04-07 09:51:40 +08:00
|
|
|
TRACE(nc_get_att);
|
2011-07-12 00:04:49 +08:00
|
|
|
return ncp->dispatch->get_att(ncid, varid, name, value, xtype);
|
|
|
|
}
|
2019-11-07 22:09:54 +08:00
|
|
|
|
|
|
|
/**
|
|
|
|
* @ingroup attributes
|
2019-11-08 03:19:18 +08:00
|
|
|
* Get a text attribute.
|
2019-11-07 22:09:54 +08:00
|
|
|
*
|
2019-11-08 03:19:18 +08:00
|
|
|
* This function gets a text attribute from the netCDF
|
|
|
|
* file. Type conversions are not permitted.
|
2019-11-07 22:09:54 +08:00
|
|
|
*
|
2019-11-08 20:19:51 +08:00
|
|
|
* Also see @ref getting_attributes "Getting Attributes"
|
|
|
|
*
|
2019-11-08 19:45:45 +08:00
|
|
|
* @param ncid NetCDF file or group ID.
|
|
|
|
* @param varid Variable ID, or ::NC_GLOBAL for a global attribute.
|
2019-11-07 22:09:54 +08:00
|
|
|
* @param name Attribute name.
|
2019-11-08 19:45:45 +08:00
|
|
|
* @param value Pointer that will get array of attribute value(s). Use
|
|
|
|
* nc_inq_attlen() to learn length.
|
2019-11-08 20:19:51 +08:00
|
|
|
*
|
2019-11-08 19:45:45 +08:00
|
|
|
* @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.
|
2019-11-07 22:09:54 +08:00
|
|
|
*
|
2019-11-08 02:47:27 +08:00
|
|
|
* <h1>Example</h1>
|
|
|
|
*
|
2019-11-08 03:19:18 +08:00
|
|
|
* Here is an example using nc_get_att_text() to read a global
|
2019-11-08 02:47:27 +08:00
|
|
|
* attribute named title in an existing netCDF dataset named foo.nc.
|
|
|
|
*
|
2019-11-08 03:19:18 +08:00
|
|
|
* 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).
|
2011-07-12 00:04:49 +08:00
|
|
|
|
2019-11-07 22:09:54 +08:00
|
|
|
@code
|
2011-07-12 00:04:49 +08:00
|
|
|
#include <netcdf.h>
|
|
|
|
...
|
2015-12-24 09:37:42 +08:00
|
|
|
int status;
|
|
|
|
int ncid;
|
|
|
|
int rh_id;
|
2019-11-08 03:19:18 +08:00
|
|
|
int t_len;
|
2015-12-24 09:37:42 +08:00
|
|
|
char *title;
|
|
|
|
|
2011-07-12 00:04:49 +08:00
|
|
|
...
|
|
|
|
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);
|
2015-12-24 09:37:42 +08:00
|
|
|
|
|
|
|
title = (char *) malloc(t_len + 1);
|
2011-07-12 00:04:49 +08:00
|
|
|
status = nc_get_att_text(ncid, NC_GLOBAL, "title", title);
|
|
|
|
if (status != NC_NOERR) handle_error(status);
|
2015-12-24 09:37:42 +08:00
|
|
|
title[t_len] = '\0';
|
2011-07-12 00:04:49 +08:00
|
|
|
...
|
2019-11-07 22:09:54 +08:00
|
|
|
@endcode
|
2019-11-08 03:19:18 +08:00
|
|
|
*
|
2019-11-08 02:47:27 +08:00
|
|
|
* @return ::NC_NOERR for success.
|
|
|
|
* @return ::NC_EBADID Bad ncid.
|
|
|
|
* @return ::NC_ENOTVAR Bad varid.
|
|
|
|
* @return ::NC_EBADNAME Bad name. See \ref object_name.
|
|
|
|
* @return ::NC_EINVAL Invalid parameters.
|
|
|
|
* @return ::NC_ENOTATT Can't find attribute.
|
|
|
|
* @return ::NC_ECHAR Can't convert to or from NC_CHAR.
|
|
|
|
* @return ::NC_ENOMEM Out of memory.
|
|
|
|
* @return ::NC_ERANGE Data conversion went out of range.
|
|
|
|
* @author Glenn Davis, Ed Hartnett, Dennis Heimbigner
|
2011-07-12 00:04:49 +08:00
|
|
|
*/
|
2016-01-05 05:49:47 +08:00
|
|
|
int
|
|
|
|
nc_get_att_text(int ncid, int varid, const char *name, char *value)
|
2011-07-12 00:04:49 +08:00
|
|
|
{
|
|
|
|
NC* ncp;
|
|
|
|
int stat = NC_check_id(ncid, &ncp);
|
|
|
|
if(stat != NC_NOERR) return stat;
|
2016-04-07 09:51:40 +08:00
|
|
|
TRACE(nc_get_att_text);
|
2011-07-12 00:04:49 +08:00
|
|
|
return ncp->dispatch->get_att(ncid, varid, name, (void *)value, NC_CHAR);
|
|
|
|
}
|
|
|
|
|
2019-11-08 19:45:45 +08:00
|
|
|
/**
|
|
|
|
* @ingroup attributes
|
|
|
|
* Get an attribute of an signed char type.
|
|
|
|
*
|
2019-11-08 20:19:51 +08:00
|
|
|
* Also see @ref getting_attributes "Getting Attributes"
|
|
|
|
*
|
2019-11-08 19:45:45 +08:00
|
|
|
* @param ncid NetCDF file or group ID.
|
|
|
|
* @param varid Variable ID, or ::NC_GLOBAL for a global attribute.
|
|
|
|
* @param name Attribute name.
|
|
|
|
* @param value Pointer that will get array of attribute value(s). Use
|
|
|
|
* nc_inq_attlen() to learn length.
|
|
|
|
*
|
|
|
|
* @return ::NC_NOERR for success.
|
|
|
|
* @return ::NC_EBADID Bad ncid.
|
|
|
|
* @return ::NC_ENOTVAR Bad varid.
|
|
|
|
* @return ::NC_EBADNAME Bad name. See \ref object_name.
|
|
|
|
* @return ::NC_EINVAL Invalid parameters.
|
|
|
|
* @return ::NC_ENOTATT Can't find attribute.
|
|
|
|
* @return ::NC_ECHAR Can't convert to or from NC_CHAR.
|
|
|
|
* @return ::NC_ENOMEM Out of memory.
|
|
|
|
* @return ::NC_ERANGE Data conversion went out of range.
|
|
|
|
* @author Glenn Davis, Ed Hartnett, Dennis Heimbigner
|
|
|
|
*/
|
|
|
|
int
|
|
|
|
nc_get_att_schar(int ncid, int varid, const char *name, signed char *value)
|
|
|
|
{
|
|
|
|
NC* ncp;
|
|
|
|
int stat = NC_check_id(ncid, &ncp);
|
|
|
|
if(stat != NC_NOERR) return stat;
|
|
|
|
TRACE(nc_get_att_schar);
|
|
|
|
return ncp->dispatch->get_att(ncid, varid, name, (void *)value, NC_BYTE);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @ingroup attributes
|
|
|
|
* Get an attribute of an signed char type.
|
|
|
|
*
|
2019-11-08 20:19:51 +08:00
|
|
|
* Also see @ref getting_attributes "Getting Attributes"
|
|
|
|
*
|
2019-11-08 19:45:45 +08:00
|
|
|
* @param ncid NetCDF file or group ID.
|
|
|
|
* @param varid Variable ID, or ::NC_GLOBAL for a global attribute.
|
|
|
|
* @param name Attribute name.
|
|
|
|
* @param value Pointer that will get array of attribute value(s). Use
|
|
|
|
* nc_inq_attlen() to learn length.
|
|
|
|
*
|
|
|
|
* @return ::NC_NOERR for success.
|
|
|
|
* @return ::NC_EBADID Bad ncid.
|
|
|
|
* @return ::NC_ENOTVAR Bad varid.
|
|
|
|
* @return ::NC_EBADNAME Bad name. See \ref object_name.
|
|
|
|
* @return ::NC_EINVAL Invalid parameters.
|
|
|
|
* @return ::NC_ENOTATT Can't find attribute.
|
|
|
|
* @return ::NC_ECHAR Can't convert to or from NC_CHAR.
|
|
|
|
* @return ::NC_ENOMEM Out of memory.
|
|
|
|
* @return ::NC_ERANGE Data conversion went out of range.
|
|
|
|
* @author Glenn Davis, Ed Hartnett, Dennis Heimbigner
|
|
|
|
*/
|
2011-07-12 00:04:49 +08:00
|
|
|
int
|
|
|
|
nc_get_att_uchar(int ncid, int varid, const char *name, unsigned char *value)
|
|
|
|
{
|
|
|
|
NC* ncp;
|
|
|
|
int stat = NC_check_id(ncid, &ncp);
|
|
|
|
if(stat != NC_NOERR) return stat;
|
2016-04-07 09:51:40 +08:00
|
|
|
TRACE(nc_get_att_uchar);
|
2011-07-12 00:04:49 +08:00
|
|
|
return ncp->dispatch->get_att(ncid, varid, name, (void *)value, NC_UBYTE);
|
|
|
|
}
|
|
|
|
|
2019-11-08 19:45:45 +08:00
|
|
|
/**
|
|
|
|
* @ingroup attributes
|
|
|
|
* Get an attribute array of type short.
|
|
|
|
*
|
2019-11-08 20:19:51 +08:00
|
|
|
* Also see @ref getting_attributes "Getting Attributes"
|
|
|
|
*
|
2019-11-08 19:45:45 +08:00
|
|
|
* @param ncid NetCDF file or group ID.
|
|
|
|
* @param varid Variable ID, or ::NC_GLOBAL for a global attribute.
|
|
|
|
* @param name Attribute name.
|
|
|
|
* @param value Pointer that will get array of attribute value(s). Use
|
|
|
|
* nc_inq_attlen() to learn length.
|
|
|
|
*
|
|
|
|
* @return ::NC_NOERR for success.
|
|
|
|
* @return ::NC_EBADID Bad ncid.
|
|
|
|
* @return ::NC_ENOTVAR Bad varid.
|
|
|
|
* @return ::NC_EBADNAME Bad name. See \ref object_name.
|
|
|
|
* @return ::NC_EINVAL Invalid parameters.
|
|
|
|
* @return ::NC_ENOTATT Can't find attribute.
|
|
|
|
* @return ::NC_ECHAR Can't convert to or from NC_CHAR.
|
|
|
|
* @return ::NC_ENOMEM Out of memory.
|
|
|
|
* @return ::NC_ERANGE Data conversion went out of range.
|
|
|
|
* @author Glenn Davis, Ed Hartnett, Dennis Heimbigner
|
|
|
|
*/
|
2011-07-12 00:04:49 +08:00
|
|
|
int
|
|
|
|
nc_get_att_short(int ncid, int varid, const char *name, short *value)
|
|
|
|
{
|
|
|
|
NC* ncp;
|
|
|
|
int stat = NC_check_id(ncid, &ncp);
|
|
|
|
if(stat != NC_NOERR) return stat;
|
2016-04-07 09:51:40 +08:00
|
|
|
TRACE(nc_get_att_short);
|
2011-07-12 00:04:49 +08:00
|
|
|
return ncp->dispatch->get_att(ncid, varid, name, (void *)value, NC_SHORT);
|
|
|
|
}
|
|
|
|
|
2019-11-08 19:45:45 +08:00
|
|
|
/**
|
|
|
|
* @ingroup attributes
|
|
|
|
* Get an attribute array of type int.
|
|
|
|
*
|
2019-11-08 20:19:51 +08:00
|
|
|
* Also see @ref getting_attributes "Getting Attributes"
|
|
|
|
*
|
2019-11-08 19:45:45 +08:00
|
|
|
* @param ncid NetCDF file or group ID.
|
|
|
|
* @param varid Variable ID, or ::NC_GLOBAL for a global attribute.
|
|
|
|
* @param name Attribute name.
|
|
|
|
* @param value Pointer that will get array of attribute value(s). Use
|
|
|
|
* nc_inq_attlen() to learn length.
|
|
|
|
*
|
|
|
|
* @return ::NC_NOERR for success.
|
|
|
|
* @return ::NC_EBADID Bad ncid.
|
|
|
|
* @return ::NC_ENOTVAR Bad varid.
|
|
|
|
* @return ::NC_EBADNAME Bad name. See \ref object_name.
|
|
|
|
* @return ::NC_EINVAL Invalid parameters.
|
|
|
|
* @return ::NC_ENOTATT Can't find attribute.
|
|
|
|
* @return ::NC_ECHAR Can't convert to or from NC_CHAR.
|
|
|
|
* @return ::NC_ENOMEM Out of memory.
|
|
|
|
* @return ::NC_ERANGE Data conversion went out of range.
|
|
|
|
* @author Glenn Davis, Ed Hartnett, Dennis Heimbigner
|
|
|
|
*/
|
2011-07-12 00:04:49 +08:00
|
|
|
int
|
|
|
|
nc_get_att_int(int ncid, int varid, const char *name, int *value)
|
|
|
|
{
|
|
|
|
NC* ncp;
|
|
|
|
int stat = NC_check_id(ncid, &ncp);
|
|
|
|
if(stat != NC_NOERR) return stat;
|
2016-04-07 09:51:40 +08:00
|
|
|
TRACE(nc_get_att_int);
|
2011-07-12 00:04:49 +08:00
|
|
|
return ncp->dispatch->get_att(ncid, varid, name, (void *)value, NC_INT);
|
|
|
|
}
|
|
|
|
|
2019-11-08 19:45:45 +08:00
|
|
|
/**
|
|
|
|
* @ingroup attributes
|
|
|
|
* Get an attribute array of type long.
|
|
|
|
*
|
2019-11-08 20:19:51 +08:00
|
|
|
* Also see @ref getting_attributes "Getting Attributes"
|
|
|
|
*
|
2019-11-08 19:45:45 +08:00
|
|
|
* @param ncid NetCDF file or group ID.
|
|
|
|
* @param varid Variable ID, or ::NC_GLOBAL for a global attribute.
|
|
|
|
* @param name Attribute name.
|
|
|
|
* @param value Pointer that will get array of attribute value(s). Use
|
|
|
|
* nc_inq_attlen() to learn length.
|
|
|
|
*
|
|
|
|
* @return ::NC_NOERR for success.
|
|
|
|
* @return ::NC_EBADID Bad ncid.
|
|
|
|
* @return ::NC_ENOTVAR Bad varid.
|
|
|
|
* @return ::NC_EBADNAME Bad name. See \ref object_name.
|
|
|
|
* @return ::NC_EINVAL Invalid parameters.
|
|
|
|
* @return ::NC_ENOTATT Can't find attribute.
|
|
|
|
* @return ::NC_ECHAR Can't convert to or from NC_CHAR.
|
|
|
|
* @return ::NC_ENOMEM Out of memory.
|
|
|
|
* @return ::NC_ERANGE Data conversion went out of range.
|
|
|
|
* @author Glenn Davis, Ed Hartnett, Dennis Heimbigner
|
|
|
|
*/
|
2011-07-12 00:04:49 +08:00
|
|
|
int
|
|
|
|
nc_get_att_long(int ncid, int varid, const char *name, long *value)
|
|
|
|
{
|
|
|
|
NC* ncp;
|
|
|
|
int stat = NC_check_id(ncid, &ncp);
|
|
|
|
if(stat != NC_NOERR) return stat;
|
2016-04-07 09:51:40 +08:00
|
|
|
TRACE(nc_get_att_long);
|
2011-07-12 00:04:49 +08:00
|
|
|
return ncp->dispatch->get_att(ncid, varid, name, (void *)value, longtype);
|
|
|
|
}
|
|
|
|
|
2019-11-08 19:45:45 +08:00
|
|
|
/**
|
|
|
|
* @ingroup attributes
|
|
|
|
* Get an attribute array of type float.
|
|
|
|
*
|
2019-11-08 20:19:51 +08:00
|
|
|
* Also see @ref getting_attributes "Getting Attributes"
|
|
|
|
*
|
2019-11-08 19:45:45 +08:00
|
|
|
* @param ncid NetCDF file or group ID.
|
|
|
|
* @param varid Variable ID, or ::NC_GLOBAL for a global attribute.
|
|
|
|
* @param name Attribute name.
|
|
|
|
* @param value Pointer that will get array of attribute value(s). Use
|
|
|
|
* nc_inq_attlen() to learn length.
|
|
|
|
*
|
|
|
|
* @return ::NC_NOERR for success.
|
|
|
|
* @return ::NC_EBADID Bad ncid.
|
|
|
|
* @return ::NC_ENOTVAR Bad varid.
|
|
|
|
* @return ::NC_EBADNAME Bad name. See \ref object_name.
|
|
|
|
* @return ::NC_EINVAL Invalid parameters.
|
|
|
|
* @return ::NC_ENOTATT Can't find attribute.
|
|
|
|
* @return ::NC_ECHAR Can't convert to or from NC_CHAR.
|
|
|
|
* @return ::NC_ENOMEM Out of memory.
|
|
|
|
* @return ::NC_ERANGE Data conversion went out of range.
|
|
|
|
* @author Glenn Davis, Ed Hartnett, Dennis Heimbigner
|
|
|
|
*/
|
2011-07-12 00:04:49 +08:00
|
|
|
int
|
|
|
|
nc_get_att_float(int ncid, int varid, const char *name, float *value)
|
|
|
|
{
|
|
|
|
NC* ncp;
|
|
|
|
int stat = NC_check_id(ncid, &ncp);
|
|
|
|
if(stat != NC_NOERR) return stat;
|
2016-04-07 09:51:40 +08:00
|
|
|
TRACE(nc_get_att_float);
|
2011-07-12 00:04:49 +08:00
|
|
|
return ncp->dispatch->get_att(ncid, varid, name, (void *)value, NC_FLOAT);
|
|
|
|
}
|
|
|
|
|
2019-11-08 19:45:45 +08:00
|
|
|
/**
|
|
|
|
* @ingroup attributes
|
|
|
|
* Get an attribute array of type double.
|
|
|
|
*
|
2019-11-08 20:19:51 +08:00
|
|
|
* Also see @ref getting_attributes "Getting Attributes"
|
|
|
|
*
|
2019-11-08 19:45:45 +08:00
|
|
|
* @param ncid NetCDF file or group ID.
|
|
|
|
* @param varid Variable ID, or ::NC_GLOBAL for a global attribute.
|
|
|
|
* @param name Attribute name.
|
|
|
|
* @param value Pointer that will get array of attribute value(s). Use
|
|
|
|
* nc_inq_attlen() to learn length.
|
|
|
|
*
|
|
|
|
* @return ::NC_NOERR for success.
|
|
|
|
* @return ::NC_EBADID Bad ncid.
|
|
|
|
* @return ::NC_ENOTVAR Bad varid.
|
|
|
|
* @return ::NC_EBADNAME Bad name. See \ref object_name.
|
|
|
|
* @return ::NC_EINVAL Invalid parameters.
|
|
|
|
* @return ::NC_ENOTATT Can't find attribute.
|
|
|
|
* @return ::NC_ECHAR Can't convert to or from NC_CHAR.
|
|
|
|
* @return ::NC_ENOMEM Out of memory.
|
|
|
|
* @return ::NC_ERANGE Data conversion went out of range.
|
|
|
|
* @author Glenn Davis, Ed Hartnett, Dennis Heimbigner
|
|
|
|
*/
|
2011-07-12 00:04:49 +08:00
|
|
|
int
|
|
|
|
nc_get_att_double(int ncid, int varid, const char *name, double *value)
|
|
|
|
{
|
|
|
|
NC* ncp;
|
|
|
|
int stat = NC_check_id(ncid, &ncp);
|
|
|
|
if(stat != NC_NOERR) return stat;
|
2016-04-07 09:51:40 +08:00
|
|
|
TRACE(nc_get_att_double);
|
2011-07-12 00:04:49 +08:00
|
|
|
return ncp->dispatch->get_att(ncid, varid, name, (void *)value, NC_DOUBLE);
|
|
|
|
}
|
|
|
|
|
2019-11-08 19:45:45 +08:00
|
|
|
/**
|
|
|
|
* @ingroup attributes
|
|
|
|
* Get an attribute array of type unsigned char.
|
|
|
|
*
|
2019-11-08 20:19:51 +08:00
|
|
|
* Also see @ref getting_attributes "Getting Attributes"
|
|
|
|
*
|
2019-11-08 19:45:45 +08:00
|
|
|
* @param ncid NetCDF file or group ID.
|
|
|
|
* @param varid Variable ID, or ::NC_GLOBAL for a global attribute.
|
|
|
|
* @param name Attribute name.
|
|
|
|
* @param value Pointer that will get array of attribute value(s). Use
|
|
|
|
* nc_inq_attlen() to learn length.
|
|
|
|
*
|
|
|
|
* @return ::NC_NOERR for success.
|
|
|
|
* @return ::NC_EBADID Bad ncid.
|
|
|
|
* @return ::NC_ENOTVAR Bad varid.
|
|
|
|
* @return ::NC_EBADNAME Bad name. See \ref object_name.
|
|
|
|
* @return ::NC_EINVAL Invalid parameters.
|
|
|
|
* @return ::NC_ENOTATT Can't find attribute.
|
|
|
|
* @return ::NC_ECHAR Can't convert to or from NC_CHAR.
|
|
|
|
* @return ::NC_ENOMEM Out of memory.
|
|
|
|
* @return ::NC_ERANGE Data conversion went out of range.
|
2019-11-08 19:47:57 +08:00
|
|
|
* @author Ed Hartnett, Dennis Heimbigner
|
2019-11-08 19:45:45 +08:00
|
|
|
*/
|
2011-07-12 00:04:49 +08:00
|
|
|
int
|
|
|
|
nc_get_att_ubyte(int ncid, int varid, const char *name, unsigned char *value)
|
|
|
|
{
|
|
|
|
NC* ncp;
|
|
|
|
int stat = NC_check_id(ncid, &ncp);
|
|
|
|
if(stat != NC_NOERR) return stat;
|
2016-04-07 09:51:40 +08:00
|
|
|
TRACE(nc_get_att_ubyte);
|
2011-07-12 00:04:49 +08:00
|
|
|
return ncp->dispatch->get_att(ncid, varid, name, (void *)value, NC_UBYTE);
|
|
|
|
}
|
|
|
|
|
2019-11-08 19:45:45 +08:00
|
|
|
/**
|
|
|
|
* @ingroup attributes
|
|
|
|
* Get an attribute array of type unsigned short.
|
|
|
|
*
|
2019-11-08 20:19:51 +08:00
|
|
|
* Also see @ref getting_attributes "Getting Attributes"
|
|
|
|
*
|
2019-11-08 19:45:45 +08:00
|
|
|
* @param ncid NetCDF file or group ID.
|
|
|
|
* @param varid Variable ID, or ::NC_GLOBAL for a global attribute.
|
|
|
|
* @param name Attribute name.
|
|
|
|
* @param value Pointer that will get array of attribute value(s). Use
|
|
|
|
* nc_inq_attlen() to learn length.
|
|
|
|
*
|
|
|
|
* @return ::NC_NOERR for success.
|
|
|
|
* @return ::NC_EBADID Bad ncid.
|
|
|
|
* @return ::NC_ENOTVAR Bad varid.
|
|
|
|
* @return ::NC_EBADNAME Bad name. See \ref object_name.
|
|
|
|
* @return ::NC_EINVAL Invalid parameters.
|
|
|
|
* @return ::NC_ENOTATT Can't find attribute.
|
|
|
|
* @return ::NC_ECHAR Can't convert to or from NC_CHAR.
|
|
|
|
* @return ::NC_ENOMEM Out of memory.
|
|
|
|
* @return ::NC_ERANGE Data conversion went out of range.
|
2019-11-08 19:47:57 +08:00
|
|
|
* @author Ed Hartnett, Dennis Heimbigner
|
2019-11-08 19:45:45 +08:00
|
|
|
*/
|
2011-07-12 00:04:49 +08:00
|
|
|
int
|
|
|
|
nc_get_att_ushort(int ncid, int varid, const char *name, unsigned short *value)
|
|
|
|
{
|
|
|
|
NC* ncp;
|
|
|
|
int stat = NC_check_id(ncid, &ncp);
|
|
|
|
if(stat != NC_NOERR) return stat;
|
2016-04-07 09:51:40 +08:00
|
|
|
TRACE(nc_get_att_ushort);
|
2011-07-12 00:04:49 +08:00
|
|
|
return ncp->dispatch->get_att(ncid, varid, name, (void *)value, NC_USHORT);
|
|
|
|
}
|
|
|
|
|
2019-11-08 19:45:45 +08:00
|
|
|
/**
|
|
|
|
* @ingroup attributes
|
|
|
|
* Get an attribute array of type unsigned int.
|
|
|
|
*
|
2019-11-08 20:19:51 +08:00
|
|
|
* Also see @ref getting_attributes "Getting Attributes"
|
|
|
|
*
|
2019-11-08 19:45:45 +08:00
|
|
|
* @param ncid NetCDF file or group ID.
|
|
|
|
* @param varid Variable ID, or ::NC_GLOBAL for a global attribute.
|
|
|
|
* @param name Attribute name.
|
|
|
|
* @param value Pointer that will get array of attribute value(s). Use
|
|
|
|
* nc_inq_attlen() to learn length.
|
|
|
|
*
|
|
|
|
* @return ::NC_NOERR for success.
|
|
|
|
* @return ::NC_EBADID Bad ncid.
|
|
|
|
* @return ::NC_ENOTVAR Bad varid.
|
|
|
|
* @return ::NC_EBADNAME Bad name. See \ref object_name.
|
|
|
|
* @return ::NC_EINVAL Invalid parameters.
|
|
|
|
* @return ::NC_ENOTATT Can't find attribute.
|
|
|
|
* @return ::NC_ECHAR Can't convert to or from NC_CHAR.
|
|
|
|
* @return ::NC_ENOMEM Out of memory.
|
|
|
|
* @return ::NC_ERANGE Data conversion went out of range.
|
2019-11-08 19:47:57 +08:00
|
|
|
* @author Ed Hartnett, Dennis Heimbigner
|
2019-11-08 19:45:45 +08:00
|
|
|
*/
|
2011-07-12 00:04:49 +08:00
|
|
|
int
|
|
|
|
nc_get_att_uint(int ncid, int varid, const char *name, unsigned int *value)
|
|
|
|
{
|
|
|
|
NC* ncp;
|
|
|
|
int stat = NC_check_id(ncid, &ncp);
|
|
|
|
if(stat != NC_NOERR) return stat;
|
2016-04-07 09:51:40 +08:00
|
|
|
TRACE(nc_get_att_uint);
|
2011-07-12 00:04:49 +08:00
|
|
|
return ncp->dispatch->get_att(ncid, varid, name, (void *)value, NC_UINT);
|
|
|
|
}
|
|
|
|
|
2019-11-08 19:45:45 +08:00
|
|
|
/**
|
|
|
|
* @ingroup attributes
|
|
|
|
* Get an attribute array of type long long.
|
|
|
|
*
|
2019-11-08 20:19:51 +08:00
|
|
|
* Also see @ref getting_attributes "Getting Attributes"
|
|
|
|
*
|
2019-11-08 19:45:45 +08:00
|
|
|
* @param ncid NetCDF file or group ID.
|
|
|
|
* @param varid Variable ID, or ::NC_GLOBAL for a global attribute.
|
|
|
|
* @param name Attribute name.
|
|
|
|
* @param value Pointer that will get array of attribute value(s). Use
|
|
|
|
* nc_inq_attlen() to learn length.
|
|
|
|
*
|
|
|
|
* @return ::NC_NOERR for success.
|
|
|
|
* @return ::NC_EBADID Bad ncid.
|
|
|
|
* @return ::NC_ENOTVAR Bad varid.
|
|
|
|
* @return ::NC_EBADNAME Bad name. See \ref object_name.
|
|
|
|
* @return ::NC_EINVAL Invalid parameters.
|
|
|
|
* @return ::NC_ENOTATT Can't find attribute.
|
|
|
|
* @return ::NC_ECHAR Can't convert to or from NC_CHAR.
|
|
|
|
* @return ::NC_ENOMEM Out of memory.
|
|
|
|
* @return ::NC_ERANGE Data conversion went out of range.
|
2019-11-08 19:47:57 +08:00
|
|
|
* @author Ed Hartnett, Dennis Heimbigner
|
2019-11-08 19:45:45 +08:00
|
|
|
*/
|
2011-07-12 00:04:49 +08:00
|
|
|
int
|
|
|
|
nc_get_att_longlong(int ncid, int varid, const char *name, long long *value)
|
|
|
|
{
|
|
|
|
NC* ncp;
|
|
|
|
int stat = NC_check_id(ncid, &ncp);
|
|
|
|
if(stat != NC_NOERR) return stat;
|
2016-04-07 09:51:40 +08:00
|
|
|
TRACE(nc_get_att_longlong);
|
2011-07-12 00:04:49 +08:00
|
|
|
return ncp->dispatch->get_att(ncid, varid, name, (void *)value, NC_INT64);
|
|
|
|
}
|
|
|
|
|
2019-11-08 19:45:45 +08:00
|
|
|
/**
|
|
|
|
* @ingroup attributes
|
|
|
|
* Get an attribute array of type unsigned long long.
|
|
|
|
*
|
2019-11-08 20:19:51 +08:00
|
|
|
* Also see @ref getting_attributes "Getting Attributes"
|
|
|
|
*
|
2019-11-08 19:45:45 +08:00
|
|
|
* @param ncid NetCDF file or group ID.
|
|
|
|
* @param varid Variable ID, or ::NC_GLOBAL for a global attribute.
|
|
|
|
* @param name Attribute name.
|
|
|
|
* @param value Pointer that will get array of attribute value(s). Use
|
|
|
|
* nc_inq_attlen() to learn length.
|
|
|
|
*
|
|
|
|
* @return ::NC_NOERR for success.
|
|
|
|
* @return ::NC_EBADID Bad ncid.
|
|
|
|
* @return ::NC_ENOTVAR Bad varid.
|
|
|
|
* @return ::NC_EBADNAME Bad name. See \ref object_name.
|
|
|
|
* @return ::NC_EINVAL Invalid parameters.
|
|
|
|
* @return ::NC_ENOTATT Can't find attribute.
|
|
|
|
* @return ::NC_ECHAR Can't convert to or from NC_CHAR.
|
|
|
|
* @return ::NC_ENOMEM Out of memory.
|
|
|
|
* @return ::NC_ERANGE Data conversion went out of range.
|
2019-11-08 19:47:57 +08:00
|
|
|
* @author Ed Hartnett, Dennis Heimbigner
|
2019-11-08 19:45:45 +08:00
|
|
|
*/
|
2011-07-12 00:04:49 +08:00
|
|
|
int
|
|
|
|
nc_get_att_ulonglong(int ncid, int varid, const char *name, unsigned long long *value)
|
|
|
|
{
|
|
|
|
NC *ncp;
|
|
|
|
int stat = NC_check_id(ncid, &ncp);
|
|
|
|
if(stat != NC_NOERR) return stat;
|
2016-04-07 09:51:40 +08:00
|
|
|
TRACE(nc_get_att_ulonglong);
|
2011-07-12 00:04:49 +08:00
|
|
|
return ncp->dispatch->get_att(ncid, varid, name, (void *)value, NC_UINT64);
|
|
|
|
}
|
2019-11-07 22:09:54 +08:00
|
|
|
|
|
|
|
/**
|
|
|
|
* @ingroup attributes
|
2019-11-08 19:45:45 +08:00
|
|
|
* Get an attribute array of type string.
|
2019-11-07 22:09:54 +08:00
|
|
|
*
|
|
|
|
* 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.
|
|
|
|
*
|
2019-11-08 20:19:51 +08:00
|
|
|
* Also see @ref getting_attributes "Getting Attributes"
|
|
|
|
*
|
2019-11-07 22:09:54 +08:00
|
|
|
* @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().
|
|
|
|
*
|
2019-11-08 19:45:45 +08:00
|
|
|
* @param ncid NetCDF file or group ID.
|
|
|
|
* @param varid Variable ID, or ::NC_GLOBAL for a global attribute.
|
2019-11-07 22:09:54 +08:00
|
|
|
* @param name Attribute name.
|
2019-11-08 19:45:45 +08:00
|
|
|
* @param value Pointer that will get array of attribute value(s). Use
|
|
|
|
* nc_inq_attlen() to learn length.
|
2019-11-07 22:09:54 +08:00
|
|
|
*
|
|
|
|
* @section nc_get_att_string_example Example
|
|
|
|
*
|
|
|
|
@code{.c}
|
2015-12-09 06:06:30 +08:00
|
|
|
#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;
|
|
|
|
}
|
2019-11-07 22:09:54 +08:00
|
|
|
@endcode
|
2016-01-05 05:59:51 +08:00
|
|
|
|
2019-11-08 02:47:27 +08:00
|
|
|
* @return ::NC_NOERR for success.
|
|
|
|
* @return ::NC_EBADID Bad ncid.
|
|
|
|
* @return ::NC_ENOTVAR Bad varid.
|
|
|
|
* @return ::NC_EBADNAME Bad name. See \ref object_name.
|
|
|
|
* @return ::NC_EINVAL Invalid parameters.
|
|
|
|
* @return ::NC_ENOTATT Can't find attribute.
|
|
|
|
* @return ::NC_ECHAR Can't convert to or from NC_CHAR.
|
|
|
|
* @return ::NC_ENOMEM Out of memory.
|
|
|
|
* @return ::NC_ERANGE Data conversion went out of range.
|
|
|
|
*
|
|
|
|
* @author Ed Hartnett, Dennis Heimbigner
|
|
|
|
*/
|
2016-01-05 05:49:47 +08:00
|
|
|
int
|
|
|
|
nc_get_att_string(int ncid, int varid, const char *name, char **value)
|
2011-07-12 00:04:49 +08:00
|
|
|
{
|
|
|
|
NC *ncp;
|
|
|
|
int stat = NC_check_id(ncid, &ncp);
|
|
|
|
if(stat != NC_NOERR) return stat;
|
2016-04-07 09:51:40 +08:00
|
|
|
TRACE(nc_get_att_string);
|
2011-07-12 00:04:49 +08:00
|
|
|
return ncp->dispatch->get_att(ncid,varid,name,(void*)value, NC_STRING);
|
|
|
|
}
|
2019-11-08 20:19:51 +08:00
|
|
|
/**@}*/ /* End doxygen member group. */
|