source: XIOS/dev/dev_olga/src/extern/src_netcdf4/dattget.c @ 1022

Last change on this file since 1022 was 1022, checked in by mhnguyen, 7 years ago
File size: 7.5 KB
Line 
1/** \file
2Attribute functions
3
4These functions read and write attributes.
5
6Copyright 2010 University Corporation for Atmospheric
7Research/Unidata. See \ref copyright file for more info.  */
8
9#include "ncdispatch.h"
10
11/** \name Getting Attributes
12
13Functions to get the values of attributes.
14 */
15/*! \{ */
16
17/*!
18\ingroup attributes
19Get an attribute of any type.
20
21The nc_get_att() functions works for any type of attribute, and must
22be used to get attributes of user-defined type. We recommend that they
23type safe versions of this function be used where possible.
24
25\param ncid NetCDF or group ID, from a previous call to nc_open(),
26nc_create(), nc_def_grp(), or associated inquiry functions such as
27nc_inq_ncid().
28
29\param varid Variable ID of the attribute's variable, or ::NC_GLOBAL
30for a global attribute.
31
32\param name Attribute \ref object_name.
33
34\param value Pointer to location for returned attribute value(s). All
35elements of the vector of attribute values are returned, so you must
36allocate enough space to hold them. Before using the value as a C
37string, make sure it is null-terminated. Call nc_inq_attlen() first to
38find out the length of the attribute.
39*/
40int
41nc_get_att(int ncid, int varid, const char *name, void *value)
42{
43   NC* ncp;
44   int stat = NC_NOERR;
45   nc_type xtype;
46
47   if ((stat = NC_check_id(ncid, &ncp)))
48      return stat;
49
50   /* Need to get the type */
51   if ((stat = nc_inq_atttype(ncid, varid, name, &xtype)))
52      return stat;
53
54   return ncp->dispatch->get_att(ncid, varid, name, value, xtype);
55}
56/*! \} */ 
57
58/*!
59\ingroup attributes
60Get an attribute.
61
62This function gets an attribute from the netCDF file. The nc_get_att()
63function works with any type of data, including user defined types.
64
65\note The netCDF library reads all attributes into memory when the
66file is opened with nc_open(). Getting an attribute copies the value
67from the in-memory store, and does not incure any file I/O penalties.
68
69\param ncid NetCDF or group ID, from a previous call to nc_open(),
70nc_create(), nc_def_grp(), or associated inquiry functions such as
71nc_inq_ncid().
72
73\param varid Variable ID of the attribute's variable, or ::NC_GLOBAL
74for a global attribute.
75
76\param name Attribute \ref object_name.
77
78\param value Pointer to location for returned attribute value(s). All
79elements of the vector of attribute values are returned, so you must
80allocate enough space to hold them. If you don't know how much
81space to reserve, call nc_inq_attlen() first to find out the length of
82the attribute.
83
84<h1>Example</h1>
85
86Here is an example using nc_get_att_double() to determine the values
87of a variable attribute named valid_range for a netCDF variable named
88rh and using nc_get_att_text() to read a global attribute named title
89in an existing netCDF dataset named foo.nc.
90
91In this example, it is assumed that we don't know how many values will
92be returned, but that we do know the types of the attributes. Hence,
93to allocate enough space to store them, we must first inquire about
94the length of the attributes.
95
96\code
97     #include <netcdf.h>
98        ...
99     int  status;         
100     int  ncid;           
101     int  rh_id;         
102     int  vr_len, t_len; 
103     double *vr_val;     
104     char *title;         
105     extern char *malloc()
106     
107        ...
108     status = nc_open("foo.nc", NC_NOWRITE, &ncid);
109     if (status != NC_NOERR) handle_error(status);
110        ...
111     status = nc_inq_varid (ncid, "rh", &rh_id);
112     if (status != NC_NOERR) handle_error(status);
113        ...
114     status = nc_inq_attlen (ncid, rh_id, "valid_range", &vr_len);
115     if (status != NC_NOERR) handle_error(status);
116     status = nc_inq_attlen (ncid, NC_GLOBAL, "title", &t_len);
117     if (status != NC_NOERR) handle_error(status);
118     
119     vr_val = (double *) malloc(vr_len * sizeof(double));
120     title = (char *) malloc(t_len + 1);
121     
122     status = nc_get_att_double(ncid, rh_id, "valid_range", vr_val);
123     if (status != NC_NOERR) handle_error(status);
124     status = nc_get_att_text(ncid, NC_GLOBAL, "title", title);
125     if (status != NC_NOERR) handle_error(status);
126     title[t_len] = '\0'; 
127        ...
128\endcode
129*/
130/*! \{ */
131int
132nc_get_att_text(int ncid, int varid, const char *name, char *value)
133{
134   NC* ncp;
135   int stat = NC_check_id(ncid, &ncp);
136   if(stat != NC_NOERR) return stat;
137   return ncp->dispatch->get_att(ncid, varid, name, (void *)value, NC_CHAR);
138}
139
140int
141nc_get_att_schar(int ncid, int varid, const char *name, signed char *value)
142{
143   NC* ncp;
144   int stat = NC_check_id(ncid, &ncp);
145   if(stat != NC_NOERR) return stat;
146   return ncp->dispatch->get_att(ncid, varid, name, (void *)value, NC_BYTE);
147}
148
149int
150nc_get_att_uchar(int ncid, int varid, const char *name, unsigned char *value)
151{
152   NC* ncp;
153   int stat = NC_check_id(ncid, &ncp);
154   if(stat != NC_NOERR) return stat;
155   return ncp->dispatch->get_att(ncid, varid, name, (void *)value, NC_UBYTE);
156}
157
158int
159nc_get_att_short(int ncid, int varid, const char *name, short *value)
160{
161   NC* ncp;
162   int stat = NC_check_id(ncid, &ncp);
163   if(stat != NC_NOERR) return stat;
164   return ncp->dispatch->get_att(ncid, varid, name, (void *)value, NC_SHORT);
165}
166
167int
168nc_get_att_int(int ncid, int varid, const char *name, int *value)
169{
170   NC* ncp;
171   int stat = NC_check_id(ncid, &ncp);
172   if(stat != NC_NOERR) return stat;
173   return ncp->dispatch->get_att(ncid, varid, name, (void *)value, NC_INT);
174}
175
176int
177nc_get_att_long(int ncid, int varid, const char *name, long *value)
178{
179   NC* ncp;
180   int stat = NC_check_id(ncid, &ncp);
181   if(stat != NC_NOERR) return stat;
182   return ncp->dispatch->get_att(ncid, varid, name, (void *)value, longtype);
183}
184
185int
186nc_get_att_float(int ncid, int varid, const char *name, float *value)
187{
188   NC* ncp;
189   int stat = NC_check_id(ncid, &ncp);
190   if(stat != NC_NOERR) return stat;
191   return ncp->dispatch->get_att(ncid, varid, name, (void *)value, NC_FLOAT);
192}
193
194int
195nc_get_att_double(int ncid, int varid, const char *name, double *value)
196{
197   NC* ncp;
198   int stat = NC_check_id(ncid, &ncp);
199   if(stat != NC_NOERR) return stat;
200   return ncp->dispatch->get_att(ncid, varid, name, (void *)value, NC_DOUBLE);
201}
202
203int
204nc_get_att_ubyte(int ncid, int varid, const char *name, unsigned char *value)
205{
206   NC* ncp;
207   int stat = NC_check_id(ncid, &ncp);
208   if(stat != NC_NOERR) return stat;
209   return ncp->dispatch->get_att(ncid, varid, name, (void *)value, NC_UBYTE);
210}
211
212int
213nc_get_att_ushort(int ncid, int varid, const char *name, unsigned short *value)
214{
215   NC* ncp;
216   int stat = NC_check_id(ncid, &ncp);
217   if(stat != NC_NOERR) return stat;
218   return ncp->dispatch->get_att(ncid, varid, name, (void *)value, NC_USHORT);
219}
220
221int
222nc_get_att_uint(int ncid, int varid, const char *name, unsigned int *value)
223{
224   NC* ncp;
225   int stat = NC_check_id(ncid, &ncp);
226   if(stat != NC_NOERR) return stat;
227   return ncp->dispatch->get_att(ncid, varid, name, (void *)value, NC_UINT);
228}
229
230int
231nc_get_att_longlong(int ncid, int varid, const char *name, long long *value)
232{
233   NC* ncp;
234   int stat = NC_check_id(ncid, &ncp);
235   if(stat != NC_NOERR) return stat;
236   return ncp->dispatch->get_att(ncid, varid, name, (void *)value, NC_INT64);
237}
238
239int
240nc_get_att_ulonglong(int ncid, int varid, const char *name, unsigned long long *value)
241{
242   NC *ncp;
243   int stat = NC_check_id(ncid, &ncp);
244   if(stat != NC_NOERR) return stat;
245   return ncp->dispatch->get_att(ncid, varid, name, (void *)value, NC_UINT64);
246}
247
248int
249nc_get_att_string(int ncid, int varid, const char *name, char **value)
250{
251    NC *ncp;
252    int stat = NC_check_id(ncid, &ncp);
253    if(stat != NC_NOERR) return stat;
254    return ncp->dispatch->get_att(ncid,varid,name,(void*)value, NC_STRING);
255}
256/*! \} */ 
Note: See TracBrowser for help on using the repository browser.