-
Notifications
You must be signed in to change notification settings - Fork 266
/
Copy pathdattget.c
719 lines (674 loc) · 23 KB
/
dattget.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
/* Copyright 2018 University Corporation for Atmospheric
Research/Unidata. See copyright file for more info. */
/**
* @file
* Attribute functions
*
* These functions in this file read attributes.
*/
#include "ncdispatch.h"
/**
* @anchor getting_attributes
* @name 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.
*
* <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
*/
/**@{*/ /* Start doxygen member group. */
/**
* @ingroup attributes
* Get an attribute of any type.
*
* 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.
*
* Also see @ref getting_attributes "Getting Attributes"
*
* @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.
*
* @note See documentation for nc_get_att_string() regarding a special
* case where memory must be explicitly released.
*
* <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.
*
@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
* @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(int ncid, int varid, const char *name, void *value)
{
NC* ncp;
int stat = NC_NOERR;
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;
TRACE(nc_get_att);
return ncp->dispatch->get_att(ncid, varid, name, value, xtype);
}
/**
* @ingroup attributes
* Get a text attribute.
*
* This function gets a text attribute from the netCDF
* file. Type conversions are not permitted.
*
* Also see @ref getting_attributes "Getting Attributes"
*
* @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.
*
* @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.
*
* <h1>Example</h1>
*
* Here is an example using nc_get_att_text() to read a global
* attribute named title in an existing netCDF dataset named foo.nc.
*
* 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).
@code
#include <netcdf.h>
...
int status;
int ncid;
int rh_id;
int t_len;
char *title;
...
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);
title = (char *) malloc(t_len + 1);
status = nc_get_att_text(ncid, NC_GLOBAL, "title", title);
if (status != NC_NOERR) handle_error(status);
title[t_len] = '\0';
...
@endcode
*
* @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_text(int ncid, int varid, const char *name, char *value)
{
NC* ncp;
int stat = NC_check_id(ncid, &ncp);
if(stat != NC_NOERR) return stat;
TRACE(nc_get_att_text);
return ncp->dispatch->get_att(ncid, varid, name, (void *)value, NC_CHAR);
}
/**
* @ingroup attributes
* Get an attribute of an signed char type.
*
* Also see @ref getting_attributes "Getting Attributes"
*
* @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.
*
* Also see @ref getting_attributes "Getting Attributes"
*
* @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_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;
TRACE(nc_get_att_uchar);
return ncp->dispatch->get_att(ncid, varid, name, (void *)value, NC_UBYTE);
}
/**
* @ingroup attributes
* Get an attribute array of type short.
*
* Also see @ref getting_attributes "Getting Attributes"
*
* @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_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;
TRACE(nc_get_att_short);
return ncp->dispatch->get_att(ncid, varid, name, (void *)value, NC_SHORT);
}
/**
* @ingroup attributes
* Get an attribute array of type int.
*
* Also see @ref getting_attributes "Getting Attributes"
*
* @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_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;
TRACE(nc_get_att_int);
return ncp->dispatch->get_att(ncid, varid, name, (void *)value, NC_INT);
}
/**
* @ingroup attributes
* Get an attribute array of type long.
*
* Also see @ref getting_attributes "Getting Attributes"
*
* @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_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;
TRACE(nc_get_att_long);
return ncp->dispatch->get_att(ncid, varid, name, (void *)value, longtype);
}
/**
* @ingroup attributes
* Get an attribute array of type float.
*
* Also see @ref getting_attributes "Getting Attributes"
*
* @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_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;
TRACE(nc_get_att_float);
return ncp->dispatch->get_att(ncid, varid, name, (void *)value, NC_FLOAT);
}
/**
* @ingroup attributes
* Get an attribute array of type double.
*
* Also see @ref getting_attributes "Getting Attributes"
*
* @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_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;
TRACE(nc_get_att_double);
return ncp->dispatch->get_att(ncid, varid, name, (void *)value, NC_DOUBLE);
}
/**
* @ingroup attributes
* Get an attribute array of type unsigned char.
*
* Also see @ref getting_attributes "Getting Attributes"
*
* @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 Ed Hartnett, Dennis Heimbigner
*/
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;
TRACE(nc_get_att_ubyte);
return ncp->dispatch->get_att(ncid, varid, name, (void *)value, NC_UBYTE);
}
/**
* @ingroup attributes
* Get an attribute array of type unsigned short.
*
* Also see @ref getting_attributes "Getting Attributes"
*
* @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 Ed Hartnett, Dennis Heimbigner
*/
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;
TRACE(nc_get_att_ushort);
return ncp->dispatch->get_att(ncid, varid, name, (void *)value, NC_USHORT);
}
/**
* @ingroup attributes
* Get an attribute array of type unsigned int.
*
* Also see @ref getting_attributes "Getting Attributes"
*
* @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 Ed Hartnett, Dennis Heimbigner
*/
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;
TRACE(nc_get_att_uint);
return ncp->dispatch->get_att(ncid, varid, name, (void *)value, NC_UINT);
}
/**
* @ingroup attributes
* Get an attribute array of type long long.
*
* Also see @ref getting_attributes "Getting Attributes"
*
* @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 Ed Hartnett, Dennis Heimbigner
*/
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;
TRACE(nc_get_att_longlong);
return ncp->dispatch->get_att(ncid, varid, name, (void *)value, NC_INT64);
}
/**
* @ingroup attributes
* Get an attribute array of type unsigned long long.
*
* Also see @ref getting_attributes "Getting Attributes"
*
* @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 Ed Hartnett, Dennis Heimbigner
*/
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;
TRACE(nc_get_att_ulonglong);
return ncp->dispatch->get_att(ncid, varid, name, (void *)value, NC_UINT64);
}
/**
* @ingroup attributes
* Get an attribute array of type string.
*
* 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.
*
* Also see @ref getting_attributes "Getting Attributes"
*
* @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().
*
* @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.
*
* @section nc_get_att_string_example Example
*
@code{.c}
#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;
}
@endcode
* @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
*/
int
nc_get_att_string(int ncid, int varid, const char *name, char **value)
{
NC *ncp;
int stat = NC_check_id(ncid, &ncp);
if(stat != NC_NOERR) return stat;
TRACE(nc_get_att_string);
return ncp->dispatch->get_att(ncid,varid,name,(void*)value, NC_STRING);
}
/**@}*/ /* End doxygen member group. */