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

Last change on this file since 1022 was 1022, checked in by mhnguyen, 7 years ago
File size: 11.0 KB
Line 
1/** \file
2Functions to write attributes.
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 Writing Attributes
12
13Functions to write attributes. */
14/*! \{ */
15
16/*!
17\ingroup attributes
18Write a string attribute.
19
20The function nc_put_att_string adds or changes a variable attribute or
21global attribute of an open netCDF dataset. The string type is only
22available in netCDF-4/HDF5 files, when ::NC_CLASSIC_MODEL has not been
23used in nc_create().
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 variable to which the attribute will
30be assigned or ::NC_GLOBAL for a global or group attribute.
31
32\param name Attribute \ref object_name. \ref attribute_conventions may
33apply.
34
35\param len Number of values provided for the attribute.
36
37\param value Pointer to one or more values.
38
39\returns ::NC_NOERR No error.
40\returns ::NC_EINVAL Trying to set global _FillValue.
41\returns ::NC_ENOTVAR Couldn't find varid.
42\returns ::NC_EBADTYPE Fill value and var must be same type.
43\returns ::NC_ENOMEM Out of memory
44\returns ::NC_ELATEFILL Fill values must be written while the file
45is still in initial define mode.
46*/
47int
48nc_put_att_string(int ncid, int varid, const char *name,
49                  size_t len, const char** value)
50{
51    NC* ncp;
52    int stat = NC_check_id(ncid, &ncp);
53    if(stat != NC_NOERR) return stat;
54    return ncp->dispatch->put_att(ncid, varid, name, NC_STRING, 
55                                  len, (void*)value, NC_STRING);
56}
57
58/*!
59\ingroup attributes
60Write a text attribute.
61
62Add or change a text attribute. If this attribute is new,
63or if the space required to store the attribute is greater than
64before, the netCDF dataset must be in define mode. 
65
66Although it's possible to create attributes of all types, text and
67double attributes are adequate for most purposes.
68
69Use the nc_put_att function to create attributes of any type,
70including user-defined types. We recommend using the type safe
71versions of this function whenever possible.
72
73\param ncid NetCDF or group ID, from a previous call to nc_open(),
74nc_create(), nc_def_grp(), or associated inquiry functions such as
75nc_inq_ncid().
76
77\param varid Variable ID of the variable to which the attribute will
78be assigned or ::NC_GLOBAL for a global attribute.
79
80\param name Attribute \ref object_name. \ref attribute_conventions may
81apply.
82
83\param len Number of values provided for the attribute.
84
85\param value Pointer to one or more values.
86
87\returns ::NC_NOERR No error.
88\returns ::NC_EINVAL Trying to set global _FillValue.
89\returns ::NC_ENOTVAR Couldn't find varid.
90\returns ::NC_EBADTYPE Fill value and var must be same type.
91\returns ::NC_ENOMEM Out of memory
92\returns ::NC_ELATEFILL Fill values must be written while the file
93is still in initial define mode.
94
95\note With netCDF-4 files, nc_put_att will notice if you are writing a
96_Fill_Value_ attribute, and will tell the HDF5 layer to use the
97specified fill value for that variable.
98
99\section Example
100
101Here is an example using nc_put_att_double() to add a variable
102attribute named valid_range for a netCDF variable named rh and
103nc_put_att_text() to add a global attribute named title to an existing
104netCDF dataset named foo.nc:
105
106\code
107     #include <netcdf.h>
108        ...
109     int  status;       
110     int  ncid;         
111     int  rh_id;         
112     static double rh_range[] = {0.0, 100.0};
113     static char title[] = "example netCDF dataset";
114        ...
115     status = nc_open("foo.nc", NC_WRITE, &ncid);
116     if (status != NC_NOERR) handle_error(status);
117        ...
118     status = nc_redef(ncid);               
119     if (status != NC_NOERR) handle_error(status);
120     status = nc_inq_varid (ncid, "rh", &rh_id);
121     if (status != NC_NOERR) handle_error(status);
122        ...
123     status = nc_put_att_double (ncid, rh_id, "valid_range",
124                                 NC_DOUBLE, 2, rh_range);
125     if (status != NC_NOERR) handle_error(status);
126     status = nc_put_att_text (ncid, NC_GLOBAL, "title",
127                               strlen(title), title)
128     if (status != NC_NOERR) handle_error(status);
129        ...
130     status = nc_enddef(ncid);               
131     if (status != NC_NOERR) handle_error(status);
132\endcode
133*/
134int
135nc_put_att_text(int ncid, int varid, const char *name,
136                size_t len, const char *value)
137{
138   NC* ncp;
139   int stat = NC_check_id(ncid, &ncp);
140   if(stat != NC_NOERR) return stat;
141   return ncp->dispatch->put_att(ncid, varid, name, NC_CHAR, len, 
142                                 (void *)value, NC_CHAR);
143}
144
145/*! \} */ 
146/*!
147\ingroup attributes
148Write an attribute.
149
150The function nc_put_att_ type adds or changes a variable attribute or
151global attribute of an open netCDF dataset. If this attribute is new,
152or if the space required to store the attribute is greater than
153before, the netCDF dataset must be in define mode. 
154
155With netCDF-4 files, nc_put_att will notice if you are writing a
156_Fill_Value_ attribute, and will tell the HDF5 layer to use the
157specified fill value for that variable.
158
159Although it's possible to create attributes of all types, text and
160double attributes are adequate for most purposes.
161
162\param ncid NetCDF or group ID, from a previous call to nc_open(),
163nc_create(), nc_def_grp(), or associated inquiry functions such as
164nc_inq_ncid().
165
166\param varid Variable ID of the variable to which the attribute will
167be assigned or ::NC_GLOBAL for a global or group attribute.
168
169\param name Attribute \ref object_name. \ref attribute_conventions may
170apply.
171
172\param xtype \ref data_type of the attribute.
173
174\param len Number of values provided for the attribute.
175
176\param value Pointer to one or more values.
177
178\returns ::NC_NOERR No error.
179\returns ::NC_EINVAL Trying to set global _FillValue.
180\returns ::NC_ENOTVAR Couldn't find varid.
181\returns ::NC_EBADTYPE Fill value and var must be same type.
182\returns ::NC_ENOMEM Out of memory
183\returns ::NC_ELATEFILL Fill values must be written while the file
184is still in initial define mode.
185
186\section Example
187
188Here is an example using nc_put_att_double() to add a variable
189attribute named valid_range for a netCDF variable named rh and
190nc_put_att_text() to add a global attribute named title to an existing
191netCDF dataset named foo.nc:
192
193\code
194     #include <netcdf.h>
195        ...
196     int  status;       
197     int  ncid;         
198     int  rh_id;         
199     static double rh_range[] = {0.0, 100.0};
200     static char title[] = "example netCDF dataset";
201        ...
202     status = nc_open("foo.nc", NC_WRITE, &ncid);
203     if (status != NC_NOERR) handle_error(status);
204        ...
205     status = nc_redef(ncid);               
206     if (status != NC_NOERR) handle_error(status);
207     status = nc_inq_varid (ncid, "rh", &rh_id);
208     if (status != NC_NOERR) handle_error(status);
209        ...
210     status = nc_put_att_double (ncid, rh_id, "valid_range",
211                                 NC_DOUBLE, 2, rh_range);
212     if (status != NC_NOERR) handle_error(status);
213     status = nc_put_att_text (ncid, NC_GLOBAL, "title",
214                               strlen(title), title)
215     if (status != NC_NOERR) handle_error(status);
216        ...
217     status = nc_enddef(ncid);               
218     if (status != NC_NOERR) handle_error(status);
219\endcode
220*/
221/*! \{*/
222int
223nc_put_att(int ncid, int varid, const char *name, nc_type xtype,
224           size_t len, const void *value)
225{
226   NC* ncp;
227   int stat = NC_check_id(ncid, &ncp);
228   if(stat != NC_NOERR) return stat;
229   return ncp->dispatch->put_att(ncid, varid, name, xtype, len, 
230                                 value, xtype);
231}
232
233int
234nc_put_att_schar(int ncid, int varid, const char *name,
235                 nc_type xtype, size_t len, const signed char *value)
236{
237   NC *ncp;
238   int stat = NC_check_id(ncid, &ncp);
239   if(stat != NC_NOERR) return stat;
240   return ncp->dispatch->put_att(ncid, varid, name, xtype, len, 
241                                 (void *)value, NC_BYTE);
242}
243
244int
245nc_put_att_uchar(int ncid, int varid, const char *name,
246                 nc_type xtype, size_t len, const unsigned char *value)
247{
248   NC* ncp;
249   int stat = NC_check_id(ncid, &ncp);
250   if(stat != NC_NOERR) return stat;
251   return ncp->dispatch->put_att(ncid, varid, name, xtype, len,
252                                 (void *)value, NC_UBYTE);
253}
254
255int
256nc_put_att_short(int ncid, int varid, const char *name,
257                 nc_type xtype, size_t len, const short *value)
258{
259   NC* ncp;
260   int stat = NC_check_id(ncid, &ncp);
261   if(stat != NC_NOERR) return stat;
262   return ncp->dispatch->put_att(ncid, varid, name, xtype, len, 
263                                 (void *)value, NC_SHORT);
264}
265
266int
267nc_put_att_int(int ncid, int varid, const char *name,
268               nc_type xtype, size_t len, const int *value)
269{
270   NC* ncp;
271   int stat = NC_check_id(ncid, &ncp);
272   if(stat != NC_NOERR) return stat;
273   return ncp->dispatch->put_att(ncid, varid, name, xtype, len,
274                                 (void *)value, NC_INT);
275}
276
277int
278nc_put_att_long(int ncid, int varid, const char *name,
279                nc_type xtype, size_t len, const long *value)
280{
281   NC* ncp;
282   int stat = NC_check_id(ncid, &ncp);
283   if(stat != NC_NOERR) return stat;
284   return ncp->dispatch->put_att(ncid, varid, name, xtype, len,
285                                 (void *)value, longtype);
286}
287
288int
289nc_put_att_float(int ncid, int varid, const char *name,
290                 nc_type xtype, size_t len, const float *value)
291{
292   NC* ncp;
293   int stat = NC_check_id(ncid, &ncp);
294   if(stat != NC_NOERR) return stat;
295   return ncp->dispatch->put_att(ncid, varid, name, xtype, len,
296                                 (void *)value, NC_FLOAT);
297}
298
299int
300nc_put_att_double(int ncid, int varid, const char *name,
301                  nc_type xtype, size_t len, const double *value)
302{
303   NC* ncp;
304   int stat = NC_check_id(ncid, &ncp);
305   if(stat != NC_NOERR) return stat;
306   return ncp->dispatch->put_att(ncid, varid, name, xtype, len,
307                                 (void *)value, NC_DOUBLE);
308}
309
310int
311nc_put_att_ubyte(int ncid, int varid, const char *name,
312                 nc_type xtype, size_t len, const unsigned char *value)
313{
314   NC* ncp;
315   int stat = NC_check_id(ncid, &ncp);
316   if(stat != NC_NOERR) return stat;
317   return ncp->dispatch->put_att(ncid, varid, name, xtype, len,
318                                 (void *)value, NC_UBYTE);
319}
320
321int
322nc_put_att_ushort(int ncid, int varid, const char *name,
323                  nc_type xtype, size_t len, const unsigned short *value)
324{
325   NC* ncp;
326   int stat = NC_check_id(ncid, &ncp);
327   if(stat != NC_NOERR) return stat;
328   return ncp->dispatch->put_att(ncid, varid, name, xtype, len,
329                                 (void *)value, NC_USHORT);
330}
331
332int
333nc_put_att_uint(int ncid, int varid, const char *name,
334                nc_type xtype, size_t len, const unsigned int *value)
335{
336   NC* ncp;
337   int stat = NC_check_id(ncid, &ncp);
338   if(stat != NC_NOERR) return stat;
339   return ncp->dispatch->put_att(ncid, varid, name, xtype, len,
340                                 (void *)value, NC_UINT);
341}
342
343int
344nc_put_att_longlong(int ncid, int varid, const char *name,
345                    nc_type xtype, size_t len, 
346                    const long long *value)
347{
348   NC* ncp;
349   int stat = NC_check_id(ncid, &ncp);
350   if(stat != NC_NOERR) return stat;
351   return ncp->dispatch->put_att(ncid, varid, name, xtype, len,
352                                 (void *)value, NC_INT64);
353}
354
355int
356nc_put_att_ulonglong(int ncid, int varid, const char *name,
357                     nc_type xtype, size_t len, 
358                     const unsigned long long *value)
359{
360   NC* ncp;
361   int stat = NC_check_id(ncid, &ncp);
362   if(stat != NC_NOERR) return stat;
363   return ncp->dispatch->put_att(ncid, varid, name, xtype, len,
364                                 (void *)value, NC_UINT64);
365}
366
Note: See TracBrowser for help on using the repository browser.