-
Notifications
You must be signed in to change notification settings - Fork 266
/
Copy pathncdump.c
2603 lines (2432 loc) · 72.5 KB
/
ncdump.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
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*! \file
Copyright 2018 University Corporation for Atmospheric
Research/Unidata. See \ref copyright file for more info. */
#include "config.h"
#include <stddef.h>
#include <stdint.h>
#include <stdio.h>
#ifdef HAVE_GETOPT_H
#include <getopt.h>
#endif
#if defined(_WIN32) && !defined(__MINGW32__)
#include "XGetopt.h"
#endif
#ifdef HAVE_UNISTD_H
#include <unistd.h>
#endif
#ifdef HAVE_FCNTL_H
#include <fcntl.h>
#endif
#include <stdlib.h>
#include <string.h>
#include <ctype.h>
#include <assert.h>
#include <math.h>
#include "netcdf.h"
#include "netcdf_mem.h"
#include "netcdf_filter.h"
#include "netcdf_aux.h"
#include "utils.h"
#include "nccomps.h"
#include "nctime0.h" /* new iso time and calendar stuff */
#include "dumplib.h"
#include "ncdump.h"
#include "vardata.h"
#include "indent.h"
#include "isnan.h"
#include "cdl.h"
#include "nclog.h"
#include "ncpathmgr.h"
#include "nclist.h"
#include "ncuri.h"
#include "nc_provenance.h"
#include "ncpathmgr.h"
#ifdef USE_NETCDF4
#include "nc4internal.h" /* to get name of the special properties file */
#endif
#define XML_VERSION "1.0"
#define LPAREN "("
#define RPAREN ")"
#define int64_t long long
#define uint64_t unsigned long long
/* If we have a variable named one of these:
we need to be careful about printing their attributes.
*/
static const char* keywords[] = {
"variables",
"dimensions",
"data",
"group",
"types",
NULL
};
/*Forward*/
static int searchgrouptreedim(int ncid, int dimid, int* parentidp);
extern int nc__testurl(const char*,char**);
static int iskeyword(const char* kw)
{
const char** p;
for(p=keywords;*p;p++) {
if(strcmp(kw,*p)==0) return 1;
}
return 0;
}
/* globals */
char *progname;
fspec_t formatting_specs = /* defaults, overridden by command-line options */
{
0, /* construct netcdf name from file name */
false, /* print header info only, no data? */
false, /* just print coord vars? */
false, /* brief comments in data section? */
false, /* full annotations in data section? */
false, /* human-readable output for date-time values? */
false, /* use 'T' separator between date and time values as strings? */
false, /* output special attributes, eg chunking? */
false, /* if -F specified */
LANG_C, /* language conventions for indices */
false, /* for DAP URLs, client-side cache used */
0, /* if -v specified, number of variables in list */
0, /* if -v specified, list of variable names */
0, /* if -g specified, number of groups names in list */
0, /* if -g specified, list of group names */
0, /* if -g specified, list of matching grpids */
0, /* kind of netCDF file */
0, /* extended format */
0, /* mode */
0, /* inmemory */
0, /* print _NCproperties */
0 /* print _FillValue type*/
};
static void
usage(void)
{
#define USAGE "\
[-c] Coordinate variable data and header information\n\
[-h] Header information only, no data\n\
[-v var1[,...]] Data for variable(s) <var1>,... only\n\
[-b [c|f]] Brief annotations for C or Fortran indices in data\n\
[-f [c|f]] Full annotations for C or Fortran indices in data\n\
[-l len] Line length maximum in data section (default 80)\n\
[-n name] Name for netCDF (default derived from file name)\n\
[-p n[,n]] Display floating-point values with less precision\n\
[-k] Output kind of netCDF file\n\
[-s] Output special (virtual) attributes\n\
[-t] Output time data as date-time strings\n\
[-i] Output time data as date-time strings with ISO-8601 'T' separator\n\
[-g grp1[,...]] Data and metadata for group(s) <grp1>,... only\n\
[-w] With client-side caching of variables for DAP URLs\n\
[-x] Output XML (NcML) instead of CDL\n\
[-F] Output _Filter and _Codecs instead of _Fletcher32, _Shuffle, and _Deflate\n\
[-Xp] Unconditionally suppress output of the properties attribute\n\
[-XF] Unconditionally output the type of the _FillValue attribute\n\
[-Ln] Set log level to n (>= 0); ignore if logging not enabled.\n\
file Name of netCDF file (or URL if DAP access enabled)\n"
(void) fprintf(stderr,
"%s [-c|-h] [-v ...] [[-b|-f] [c|f]] [-l len] [-n name] [-p n[,n]] [-k] [-x] [-s] [-t|-i] [-g ...] [-w] [-F] [-Ln] file\n%s",
progname,
USAGE);
(void) fprintf(stderr,
"netcdf library version %s\n",
nc_inq_libvers());
}
/*
* convert pathname of netcdf file into name for cdl unit, by taking
* last component of path and stripping off any extension.
* DMH: add code to handle OPeNDAP url.
* DMH: I think this also works for UTF8.
*/
static char *
name_path(const char *path)
{
char* cvtpath = NULL;
const char *cp = NULL;
char *sp = NULL;
size_t cplen = 0;
char* base = NULL;
if (NCpathcanonical(path, &cvtpath) || cvtpath==NULL)
return NULL;
/* See if this is a url */
if(nc__testurl(cvtpath,&base))
goto done; /* Looks like a url */
/* else fall thru and treat like a file path */
cp = strrchr(cvtpath, '/');
if (cp == NULL) /* no delimiter */
cp = cvtpath;
else /* skip delimiter */
cp++;
cplen = strlen(cp);
base = (char *) emalloc((unsigned) (cplen+1));
base[0] = '\0';
strlcat(base,cp,cplen+1);
if ((sp = strrchr(base, '.')) != NULL)
*sp = '\0'; /* strip off any extension */
done:
nullfree(cvtpath);
return base;
}
/* Return primitive type name */
static const char *
prim_type_name(nc_type type)
{
switch (type) {
case NC_BYTE:
return "byte";
case NC_CHAR:
return "char";
case NC_SHORT:
return "short";
case NC_INT:
return "int";
case NC_FLOAT:
return "float";
case NC_DOUBLE:
return "double";
case NC_UBYTE:
return "ubyte";
case NC_USHORT:
return "ushort";
case NC_UINT:
return "uint";
case NC_INT64:
return "int64";
case NC_UINT64:
return "uint64";
case NC_STRING:
return "string";
case NC_VLEN:
return "vlen";
case NC_OPAQUE:
return "opaque";
case NC_COMPOUND:
return "compound";
default:
error("prim_type_name: bad type %d", type);
return "bogus";
}
}
/*
* Remove trailing zeros (after decimal point) but not trailing decimal
* point from ss, a string representation of a floating-point number that
* might include an exponent part.
*/
static void
tztrim(char *ss)
{
char *cp, *ep;
cp = ss;
if (*cp == '-')
cp++;
while(isdigit((int)*cp) || *cp == '.')
cp++;
if (*--cp == '.')
return;
ep = cp+1;
while (*cp == '0')
cp--;
cp++;
if (cp == ep)
return;
while (*ep)
*cp++ = *ep++;
*cp = '\0';
}
/* Return file type string */
static const char *
kind_string(int kind)
{
switch (kind) {
case NC_FORMAT_CLASSIC:
return "classic";
case NC_FORMAT_64BIT_OFFSET:
return "64-bit offset";
case NC_FORMAT_CDF5:
return "cdf5";
case NC_FORMAT_NETCDF4:
return "netCDF-4";
case NC_FORMAT_NETCDF4_CLASSIC:
return "netCDF-4 classic model";
default:
error("unrecognized file format: %d", kind);
return "unrecognized";
}
}
/* Return extended format string */
static const char *
kind_string_extended(int kind, int mode)
{
static char text[1024];
switch (kind) {
case NC_FORMATX_NC3:
if(mode & NC_CDF5)
snprintf(text,sizeof(text),"%s mode=%08x", "64-bit data",mode);
else if(mode & NC_64BIT_OFFSET)
snprintf(text,sizeof(text),"%s mode=%08x", "64-bit offset",mode);
else
snprintf(text,sizeof(text),"%s mode=%08x", "classic",mode);
break;
case NC_FORMATX_NC_HDF5:
snprintf(text,sizeof(text),"%s mode=%08x", "HDF5",mode);
break;
case NC_FORMATX_NC_HDF4:
snprintf(text,sizeof(text),"%s mode=%08x", "HDF4",mode);
break;
case NC_FORMATX_PNETCDF:
snprintf(text,sizeof(text),"%s mode=%08x", "PNETCDF",mode);
break;
case NC_FORMATX_DAP2:
snprintf(text,sizeof(text),"%s mode=%08x", "DAP2",mode);
break;
case NC_FORMATX_DAP4:
snprintf(text,sizeof(text),"%s mode=%08x", "DAP4",mode);
break;
case NC_FORMATX_UNDEFINED:
snprintf(text,sizeof(text),"%s mode=%08x", "unknown",mode);
break;
default:
error("unrecognized extended format: %d",kind);
snprintf(text,sizeof(text),"%s mode=%08x", "unrecognized",mode);
break;
}
return text;
}
#if 0
static int
fileopen(const char* path, void** memp, size_t* sizep)
{
int status = NC_NOERR;
int fd = -1;
int oflags = 0;
off_t size = 0;
void* mem = NULL;
off_t red = 0;
char* pos = NULL;
/* Open the file, but make sure we can write it if needed */
oflags = O_RDONLY;
#ifdef O_BINARY
oflags |= O_BINARY;
#endif
oflags |= O_EXCL;
#ifdef vms
fd = NCopen3(path, oflags, 0, "ctx=stm");
#else
fd = NCopen2(path, oflags);
#endif
if(fd < 0) {
status = errno;
goto done;
}
/* get current filesize = max(|file|,initialize)*/
size = lseek(fd,0,SEEK_END);
if(size < 0) {status = errno; goto done;}
/* move pointer back to beginning of file */
(void)lseek(fd,0,SEEK_SET);
mem = malloc(size);
if(mem == NULL) {status = NC_ENOMEM; goto done;}
/* Read the file into memory */
/* We need to do multiple reads because there is no
guarantee that the amount read will be the full amount */
red = size;
pos = (char*)mem;
while(red > 0) {
ssize_t count = read(fd, pos, red);
if(count < 0) {status = errno; goto done;}
if(count == 0) {status = NC_ENOTNC; goto done;}
/* assert(count > 0) */
red -= count;
pos += count;
}
done:
if(fd >= 0)
(void)close(fd);
if(status != NC_NOERR) {
#ifndef DEBUG
fprintf(stderr,"open failed: file=%s err=%d\n",path,status);
fflush(stderr);
#endif
}
if(status != NC_NOERR && mem != NULL) {
free(mem);
mem = NULL;
} else {
if(sizep) *sizep = size;
if(memp) {
*memp = mem;
} else if(mem) {
free(mem);
}
}
return status;
}
#endif
/*
* Emit initial line of output for NcML
*/
static void
pr_initx(int ncid, const char *path)
{
printf("<?xml version=\"%s\" encoding=\"UTF-8\"?>\n<netcdf xmlns=\"https://www.unidata.ucar.edu/namespaces/netcdf/ncml-2.2\" location=\"%s\">\n",
XML_VERSION, path);
}
/*
* Print attribute string, for text attributes.
*/
static void
pr_att_string(
int kind,
size_t len,
const char *string
)
{
int iel;
const char *cp;
const char *sp;
unsigned char uc;
cp = string;
printf ("\"");
/* adjust len so trailing nulls don't get printed */
sp = cp + len - 1;
while (len != 0 && *sp-- == '\0')
len--;
for (iel = 0; iel < len; iel++)
switch (uc = (unsigned char)(*cp++ & 0377)) {
case '\b':
printf ("\\b");
break;
case '\f':
printf ("\\f");
break;
case '\n':
/* Only generate linebreaks after embedded newlines for
* classic, 64-bit offset, cdf5, or classic model files. For
* netCDF-4 files, don't generate linebreaks, because that
* would create an extra string in a list of strings. */
if (kind != NC_FORMAT_NETCDF4) {
printf ("\\n\",\n\t\t\t\"");
} else {
printf("\\n");
}
break;
case '\r':
printf ("\\r");
break;
case '\t':
printf ("\\t");
break;
case '\v':
printf ("\\v");
break;
case '\\':
printf ("\\\\");
break;
case '\'':
printf ("\\\'");
break;
case '\"':
printf ("\\\"");
break;
default:
if (iscntrl(uc))
printf ("\\%03o",uc);
else
printf ("%c",uc);
break;
}
printf ("\"");
}
/*
* Print NcML attribute string, for text attributes.
*/
static void
pr_attx_string(
const char* attname,
size_t len,
const char *string
)
{
int iel;
const char *cp;
const char *sp;
unsigned char uc;
int nulcount = 0;
cp = string;
printf ("\"");
/* adjust len so trailing nulls don't get printed */
sp = cp + len - 1;
while (len != 0 && *sp-- == '\0')
len--;
for (iel = 0; iel < len; iel++)
switch (uc = (unsigned char)*cp++ & 0377) {
case '\"':
printf (""");
break;
case '<':
printf ("<");
break;
case '>':
printf (">");
break;
case '&':
printf ("&");
break;
case '\n':
printf ("
");
break;
case '\r':
printf ("
");
break;
case '\t':
printf ("	");
break;
case '\0':
printf ("�");
if(nulcount++ == 0)
fprintf(stderr,"Attribute: '%s'; value contains nul characters; producing illegal xml\n",attname);
break;
default:
if (iscntrl(uc))
printf ("&#%d;",uc);
else
printf ("%c",uc);
break;
}
printf ("\"");
}
/*
* Print list of attribute values, for attributes of primitive types.
* Attribute values must be printed with explicit type tags for
* netCDF-3 primitive types, because CDL doesn't require explicit
* syntax to declare such attribute types.
*/
static void
pr_att_valgs(
int kind,
nc_type type,
size_t len,
const void *vals
)
{
int iel;
signed char sc;
short ss;
int ii;
char gps[PRIM_LEN];
float ff;
double dd;
unsigned char uc;
unsigned short us;
unsigned int ui;
int64_t i64;
uint64_t ui64;
#ifdef USE_NETCDF4
char *stringp;
#endif /* USE_NETCDF4 */
char *delim = ", "; /* delimiter between output values */
if (type == NC_CHAR) {
char *cp = (char *) vals;
pr_att_string(kind, len, cp);
return;
}
/* else */
for (iel = 0; iel < len; iel++) {
if (iel == len - 1)
delim = "";
switch (type) {
case NC_BYTE:
sc = ((signed char *) vals)[iel];
printf ("%db%s", sc, delim);
break;
case NC_SHORT:
ss = ((short *) vals)[iel];
printf ("%ds%s", ss, delim);
break;
case NC_INT:
ii = ((int *) vals)[iel];
printf ("%d%s", ii, delim);
break;
case NC_FLOAT:
ff = ((float *) vals)[iel];
if(isfinite(ff)) {
int res;
res = snprintf(gps, PRIM_LEN, float_att_fmt, ff);
assert(res < PRIM_LEN);
tztrim(gps); /* trim trailing 0's after '.' */
printf ("%s%s", gps, delim);
} else {
if(isnan(ff)) {
printf("NaNf%s", delim);
} else if(isinf(ff)) {
if(ff < 0.0F) {
printf("-");
}
printf("Infinityf%s", delim);
}
}
break;
case NC_DOUBLE:
dd = ((double *) vals)[iel];
if(isfinite(dd)) {
int res;
res = snprintf(gps, PRIM_LEN, double_att_fmt, dd);
assert(res < PRIM_LEN);
tztrim(gps);
printf ("%s%s", gps, delim);
} else {
if(isnan(dd)) {
printf("NaN%s", delim);
} else if(isinf(dd)) {
if(dd < 0.0) {
printf("-");
}
printf("Infinity%s", delim);
}
}
break;
case NC_UBYTE:
uc = ((unsigned char *) vals)[iel];
printf ("%uUB%s", uc, delim);
break;
case NC_USHORT:
us = ((unsigned short *) vals)[iel];
printf ("%huUS%s", us, delim);
break;
case NC_UINT:
ui = ((unsigned int *) vals)[iel];
printf ("%uU%s", ui, delim);
break;
case NC_INT64:
i64 = ((int64_t *) vals)[iel];
printf ("%lldLL%s", i64, delim);
break;
case NC_UINT64:
ui64 = ((uint64_t *) vals)[iel];
printf ("%lluULL%s", ui64, delim);
break;
#ifdef USE_NETCDF4
case NC_STRING:
stringp = ((char **) vals)[iel];
if(stringp)
pr_att_string(kind, strlen(stringp), stringp);
else
printf("NIL");
printf("%s", delim);
break;
#endif /* USE_NETCDF4 */
default:
error("pr_att_vals: bad type");
}
}
}
/*
* Print list of numeric attribute values to string for use in NcML output.
* Unlike CDL, NcML makes type explicit, so don't need type suffixes.
*/
static void
pr_att_valsx(
nc_type type,
size_t len,
const double *vals,
char *attvals, /* returned string */
size_t attvalslen /* size of attvals buffer, assumed
large enough to hold all len
blank-separated values */
)
{
int iel;
float ff;
double dd;
int ii;
unsigned int ui;
int64_t i64;
uint64_t ui64;
attvals[0]='\0';
if (len == 0)
return;
for (iel = 0; iel < len; iel++) {
char gps[PRIM_LEN];
int res;
switch (type) {
case NC_BYTE:
case NC_SHORT:
case NC_INT:
ii = (int)vals[iel];
res = snprintf(gps, PRIM_LEN, "%d", ii);
assert(res < PRIM_LEN);
(void) strlcat(attvals, gps, attvalslen);
(void) strlcat(attvals, iel < len-1 ? " " : "", attvalslen);
break;
case NC_UBYTE:
case NC_USHORT:
case NC_UINT:
ui = (unsigned int)vals[iel];
res = snprintf(gps, PRIM_LEN, "%u", ui);
assert(res < PRIM_LEN);
(void) strlcat(attvals, gps, attvalslen);
(void) strlcat(attvals, iel < len-1 ? " " : "", attvalslen);
break;
case NC_INT64:
i64 = (int64_t)vals[iel];
res = snprintf(gps, PRIM_LEN, "%lld", i64);
assert(res < PRIM_LEN);
(void) strlcat(attvals, gps, attvalslen);
(void) strlcat(attvals, iel < len-1 ? " " : "", attvalslen);
break;
case NC_UINT64:
ui64 = (uint64_t)vals[iel];
res = snprintf(gps, PRIM_LEN, "%llu", ui64);
assert(res < PRIM_LEN);
(void) strlcat(attvals, gps, attvalslen);
(void) strlcat(attvals, iel < len-1 ? " " : "", attvalslen);
break;
case NC_FLOAT:
ff = (float)vals[iel];
res = snprintf(gps, PRIM_LEN, float_attx_fmt, ff);
assert(res < PRIM_LEN);
tztrim(gps); /* trim trailing 0's after '.' */
(void) strlcat(attvals, gps, attvalslen);
(void) strlcat(attvals, iel < len-1 ? " " : "", attvalslen);
break;
case NC_DOUBLE:
dd = vals[iel];
res = snprintf(gps, PRIM_LEN, double_att_fmt, dd);
assert(res < PRIM_LEN);
tztrim(gps); /* trim trailing 0's after '.' */
(void) strlcat(attvals, gps, attvalslen);
(void) strlcat(attvals, iel < len-1 ? " " : "", attvalslen);
break;
default:
error("pr_att_valsx: bad type");
}
}
}
/*
* Print a variable attribute
*/
static void
pr_att(
int ncid,
int kind,
int varid,
const char *varname,
int ia
)
{
ncatt_t att; /* attribute */
NC_CHECK( nc_inq_attname(ncid, varid, ia, att.name) );
#ifdef USE_NETCDF4
if (ncid == getrootid(ncid)
&& varid == NC_GLOBAL
&& strcmp(att.name,NCPROPS)==0)
return; /* will be printed elsewhere */
#endif
NC_CHECK( nc_inq_att(ncid, varid, att.name, &att.type, &att.len) );
att.tinfo = get_typeinfo(att.type);
indent_out();
printf ("\t\t");
#ifdef USE_NETCDF4
if (is_user_defined_type(att.type) || att.type == NC_STRING
|| (formatting_specs.xopt_filltype && varid != NC_GLOBAL && strcmp(NC_FillValue,att.name)==0))
#else
if (is_user_defined_type(att.type))
#endif
{
/* TODO: omit next two lines if att_type_name not needed
* because print_type_name() looks it up */
char att_type_name[NC_MAX_NAME + 1];
get_type_name(ncid, att.type, att_type_name);
/* printf ("\t\t%s ", att_type_name); */
/* ... but handle special characters in CDL names with escapes */
print_type_name(ncid, att.type);
printf(" ");
}
/* printf ("\t\t%s:%s = ", varname, att.name); */
print_name(varname);
if(iskeyword(varname)) /* see discussion about escapes in ncgen man page*/
printf(" ");
printf(":");
print_name(att.name);
printf(" = ");
if (att.len == 0) { /* show 0-length attributes as empty strings */
att.type = NC_CHAR;
}
if (! is_user_defined_type(att.type) ) {
att.valgp = (void *) emalloc((att.len + 1) * att.tinfo->size );
NC_CHECK( nc_get_att(ncid, varid, att.name, att.valgp ) );
if(att.type == NC_CHAR) /* null-terminate retrieved text att value */
((char *)att.valgp)[att.len] = '\0';
/* (1) Print normal list of attribute values. */
pr_att_valgs(kind, att.type, att.len, att.valgp);
printf (" ;"); /* terminator for normal list */
/* (2) If -t option, add list of date/time strings as CDL comments. */
if(formatting_specs.string_times) {
/* Prints text after semicolon and before final newline.
* Prints nothing if not qualified for time interpretation.
* Will include line breaks for longer lists. */
print_att_times(ncid, varid, &att);
if(is_bounds_att(&att)) {
insert_bounds_info(ncid, varid, &att);
}
}
#ifdef USE_NETCDF4
/* If NC_STRING, need to free all the strings also */
if(att.type == NC_STRING) {
nc_free_string(att.len, att.valgp);
}
#endif /* USE_NETCDF4 */
free(att.valgp);
}
#ifdef USE_NETCDF4
else /* User-defined type. */
{
char type_name[NC_MAX_NAME + 1];
size_t type_size, nfields;
nc_type base_nc_type;
int class, i;
void *data = NULL;
NC_CHECK( nc_inq_user_type(ncid, att.type, type_name, &type_size,
&base_nc_type, &nfields, &class));
switch(class)
{
case NC_VLEN:
/* because size returned for vlen is base type size, but we
* need space to read array of vlen structs into ... */
data = emalloc((att.len + 1) * sizeof(nc_vlen_t));
break;
case NC_OPAQUE:
data = emalloc((att.len + 1) * type_size);
break;
case NC_ENUM:
/* a long long is ample for all base types */
data = emalloc((att.len + 1) * sizeof(int64_t));
break;
case NC_COMPOUND:
data = emalloc((att.len + 1) * type_size);
break;
default:
error("unrecognized class of user defined type: %d", class);
}
NC_CHECK( nc_get_att(ncid, varid, att.name, data));
switch(class) {
case NC_VLEN:
pr_any_att_vals(&att, data);
break;
case NC_OPAQUE: {
char *sout = emalloc(2 * type_size + strlen("0X") + 1);
unsigned char *cp = data;
for (i = 0; i < att.len; i++) {
(void) ncopaque_val_as_hex(type_size, sout, cp);
printf("%s%s", sout, i < att.len-1 ? ", " : "");
cp += type_size;
}
free(sout);
} break;
case NC_ENUM: {
int64_t value;
for (i = 0; i < att.len; i++) {
char enum_name[NC_MAX_NAME + 1];
switch(base_nc_type)
{
case NC_BYTE:
value = *((char *)data + i);
break;
case NC_UBYTE:
value = *((unsigned char *)data + i);
break;
case NC_SHORT:
value = *((short *)data + i);
break;
case NC_USHORT:
value = *((unsigned short *)data + i);
break;
case NC_INT:
value = *((int *)data + i);
break;
case NC_UINT:
value = *((unsigned int *)data + i);
break;
case NC_INT64:
value = *((int64_t *)data + i);
break;
case NC_UINT64:
value = (int64_t)*((uint64_t *)data + i);
break;
default:
error("enum must have an integer base type: %d", base_nc_type);
}
NC_CHECK( nc_inq_enum_ident(ncid, att.type, value,
enum_name));
/* printf("%s%s", enum_name, i < att.len-1 ? ", " : ""); */
print_name(enum_name);
printf("%s", i < att.len-1 ? ", " : "");
}
} break;
case NC_COMPOUND:
pr_any_att_vals(&att, data);
break;
default:
error("unrecognized class of user defined type: %d", class);
}
NC_CHECK(nc_reclaim_data_all(ncid,att.type,data,att.len));
printf (" ;"); /* terminator for user defined types */
}
#endif /* USE_NETCDF4 */
printf ("\n"); /* final newline for all attribute types */
}
/* Common code for printing attribute name */
static void
pr_att_name(
int ncid,
const char *varname,
const char *attname
)
{
indent_out();
printf ("\t\t");
print_name(varname);
printf(":");
print_name(attname);
}
/*
* Print special _Format global attribute, a virtual attribute not
* actually stored in the file.
*/
static void
pr_att_global_format(
int ncid,
int kind
)
{
pr_att_name(ncid, "", NC_ATT_FORMAT);
printf(" = ");
printf("\"%s\"", kind_string(kind));
printf (" ;\n");
}
#ifdef USE_NETCDF4
/*
* Print special reserved variable attributes, such as _Chunking,
* _DeflateLevel, ... These are virtual, not real, attributes
* generated from the result of inquire calls. They are of primitive
* type to fit into the classic model. Currently, these only exist
* for netCDF-4 data.
*/
static void
pr_att_specials(
int ncid,
int kind,
int varid,
const ncvar_t *varp
)
{
int contig = NC_CHUNKED;
/* No special variable attributes for classic or 64-bit offset data */
if(kind == 1 || kind == 2)
return;
/* _Chunking tests */
NC_CHECK( nc_inq_var_chunking(ncid, varid, &contig, NULL ) );
if(contig == NC_CONTIGUOUS) {
pr_att_name(ncid, varp->name, NC_ATT_STORAGE);
printf(" = \"contiguous\" ;\n");
} else if(contig == NC_COMPACT) {
pr_att_name(ncid, varp->name, NC_ATT_STORAGE);
printf(" = \"compact\" ;\n");
} else if(contig == NC_CHUNKED) {
size_t *chunkp;
int i;
pr_att_name(ncid, varp->name, NC_ATT_STORAGE);
printf(" = \"chunked\" ;\n");