forked from Unidata/netcdf-c
/
atttests.c
1393 lines (1341 loc) · 49.6 KB
/
atttests.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
/*********************************************************************
* Copyright 1993, UCAR/Unidata
* See netcdf/COPYRIGHT file for copying and redistribution conditions.
* $Header: /upc/share/CVS/netcdf-3/nctest/atttests.c,v 1.18 2006/10/31 16:21:45 ed Exp $
*********************************************************************/
#ifdef _MPW
#define __SEG__ toobig /* under MPW on MacOS, makes it fit */
#endif
#include <config.h>
#include <stdio.h>
#include <string.h>
#include <stdlib.h> /* for free() */
#include "netcdf.h"
#include "testcdf.h" /* defines in-memory test cdf structure */
#include "add.h" /* functions to update in-memory netcdf */
#include "error.h"
#include "emalloc.h"
#include "tests.h"
#include "val.h"
#define LEN_OF(array) ((sizeof array) / (sizeof array[0]))
/*
* Test ncattput
* check that new attribute put works in define mode
* check that NC_GLOBAL variable id works
* check that changing type of existing attribute works in define mode
* check that increasing length of attribute works in define mode
* check that changing value of existing attribute works in define mode
* try with bad datatype, should fail
* try with negative length, should fail
* try increasing length of attribute in data mode, should fail
* try putting new attribute in data mode, should fail
* check that changing type of existing attribute works in data mode
* check that decreasing length of attribute works in data mode
* check that changing value of existing attribute works in data mode
* try with bad variable handle, should fail
* try with bad netCDF handle, check error
*/
int
test_ncattput(path)
const char *path; /* name of writable netcdf file to open */
{
int nerrs = 0;
static char pname[] = "test_ncattput";
int cdfid; /* netcdf id */
int ndims; /* number of dimensions */
int nvars; /* number of variables */
int ngatts_prev, ngatts; /* number of global attributes */
int xdimid; /* id of unlimited dimension */
int ia, id;
static char byte_vals[] = {'a', 'b'};
static char char_vals[] = "chars";
static short short_vals[] = {-999, 0, 999};
static nclong long_vals[] = {10, 20};
static float float_vals[] = {1.5, 2.5, 3.5 };
static double double_vals[] = {4.5, 5.5, 6.5, 7.5};
/*
* test attributes; it is important for this test that the size
* required for the attribute values increases monotonically.
*/
static struct cdfatt atts[] = {
{___, "att0", NC_BYTE, LEN_OF(byte_vals), (void *) byte_vals},
{___, "att1", NC_CHAR, LEN_OF(char_vals), (void *) char_vals},
{___, "att2", NC_SHORT, LEN_OF(short_vals), (void *) short_vals},
{___, "att3", NC_LONG, LEN_OF(long_vals), (void *) long_vals},
{___, "att4", NC_FLOAT, LEN_OF(float_vals), (void *) float_vals},
{___, "att5", NC_DOUBLE, LEN_OF(double_vals), (void *) double_vals}
};
int na = LEN_OF(atts); /* number of test attributes */
int ww_id; /* variable id */
static struct cdfvar ww = /* new variable */
{"ww", NC_LONG, 1, ___, 0};
static struct cdfatt tmp; /* attribute */
(void) fprintf(stderr, "*** Testing %s ...\t", &pname[5]);
if ((cdfid = ncopen(path, NC_WRITE)) == -1) {
error("%s: ncopen failed", pname);
return ++nerrs;
}
/* enter define mode */
if (ncredef(cdfid) == -1) {
error("%s: cdredef failed", pname);
ncclose(cdfid); return ++nerrs;
}
/* get count of global attributes */
if (ncinquire(cdfid, &ndims, &nvars, &ngatts, &xdimid) == -1) {
error("%s: ncinquire failed", pname);
ncclose(cdfid); return ++nerrs;
}
ngatts_prev = ngatts;
/* in define mode, add global attributes of every type */
for (ia = 0; ia < na; ia++) {
if (ncattput(cdfid, NC_GLOBAL, atts[ia].name, atts[ia].type,
atts[ia].len, atts[ia].val) == -1) {
error("%s: ncattput of NC_GLOBAL attribute failed", pname);
ncclose(cdfid); return ++nerrs;
}
add_att(&test, NC_GLOBAL, &atts[ia]); /* keep in-memory netcdf updated */
}
/* make sure count of global attributes has been updated */
if (ncinquire(cdfid, &ndims, &nvars, &ngatts, &xdimid) == -1) {
error("%s: ncinquire failed", pname);
ncclose(cdfid); return ++nerrs;
}
if (ngatts != ngatts_prev + na) {
error("%s: number of global = %d, expected %d",
pname, ngatts, ngatts_prev + na);
nerrs++;
}
/* check with ncattinq and ncattget that NC_GLOBAL attributes put OK */
for (ia = 0; ia < na; ia++) {
if (ncattinq(cdfid, NC_GLOBAL, atts[ia].name,
&tmp.type, &tmp.len) == -1) {
error("%s: ncattinq of global attribute failed", pname);
ncclose(cdfid); return ++nerrs;
}
if (atts[ia].type != tmp.type || atts[ia].len != tmp.len) {
error("%s: NC_GLOBAL ncattinq got unexpected type or len",
pname);
ncclose(cdfid); return ++nerrs;
}
/* allocate space to hold the attribute value to be retrieved */
tmp.val = emalloc(atts[ia].len * nctypelen(atts[ia].type));
if (ncattget(cdfid, NC_GLOBAL, atts[ia].name, tmp.val) == -1) {
error("%s: ncattget of variable attribute failed", pname);
ncclose(cdfid); return ++nerrs;
}
if (val_cmp(tmp.type, tmp.len, tmp.val, atts[ia].val) != 0) {
error("%s: ncattget got bad values after put of NC_GLOBAL attrs",
pname);
nerrs++;
}
free (tmp.val);
}
/* add a variable, then variable attributes of every type */
ww.dims = (int *) emalloc(sizeof(int) * ww.ndims);
for (id = 0; id < ww.ndims; id++)
ww.dims[id] = id;
if ((ww_id = ncvardef(cdfid,
ww.name, ww.type, ww.ndims, ww.dims)) == -1) {
error("%s: ncvardef failed", pname);
ncclose(cdfid); return ++nerrs;
}
add_var(&test, &ww); /* keep in-memory netcdf in sync */
for (ia = 0; ia < na; ia++) {
if (ncattput(cdfid, ww_id,
atts[ia].name, atts[ia].type, atts[ia].len, atts[ia].val)
== -1) {
error("%s: ncattput of variable attribute failed", pname);
ncclose(cdfid); return ++nerrs;
}
add_att(&test, ww_id, &atts[ia]); /* keep in-memory netcdf updated */
}
/* check with ncattinq and ncattget that variable attributes put OK */
for (ia = 0; ia < na; ia++) {
if (ncattinq(cdfid, ww_id, atts[ia].name,
&tmp.type, &tmp.len) == -1) {
error("%s: ncattinq of variable attribute failed", pname);
ncclose(cdfid); return ++nerrs;
}
if (atts[ia].type != tmp.type || atts[ia].len != tmp.len) {
error("%s: ncattinq for new attribute got bad type or len",
pname);
ncclose(cdfid); return ++nerrs;
}
/* allocate space to hold the attribute value to be retrieved */
tmp.val = emalloc(atts[ia].len * nctypelen(atts[ia].type));
if (ncattget(cdfid, ww_id, atts[ia].name, tmp.val) == -1) {
error("%s: ncattget of variable attribute failed", pname);
ncclose(cdfid); return ++nerrs;
}
if (val_cmp(tmp.type, tmp.len, tmp.val, atts[ia].val) != 0) {
error("%s: ncattget got bad values after put of variable attrs",
pname);
nerrs++;
}
free (tmp.val);
}
/*
* check that changing type of existing attribute, increasing
* length of attribute, and changing value of existing attribute
* work OK in define mode.
*/
tmp.name = (char *) emalloc(MAX_NC_NAME);
for (ia = 1; ia < na; ia++) {
if (ncattput(cdfid, ww_id, atts[ia-1].name, atts[ia].type,
atts[ia].len, atts[ia].val) == -1) {
error("%s: ncattput of larger attribute failed", pname);
ncclose(cdfid); return ++nerrs;
}
tmp.var = atts[ia].var;
(void) strcpy (tmp.name, atts[ia-1].name);
tmp.type = atts[ia].type;
tmp.len = atts[ia].len;
tmp.val = atts[ia].val;
add_att(&test, ww_id, &tmp); /* keep in-memory netcdf updated */
}
/* check with ncattinq and ncattget that variable attributes put OK */
for (ia = 1; ia < na; ia++) {
if (ncattinq(cdfid, ww_id, atts[ia-1].name,
&tmp.type, &tmp.len) == -1) {
error("%s: ncattinq of larger attribute failed", pname);
ncclose(cdfid); return ++nerrs;
}
if (atts[ia].type != tmp.type || atts[ia].len != tmp.len) {
error("%s: ncattinq for larger attribute got bad type or len",
pname);
ncclose(cdfid); return ++nerrs;
}
/* allocate space to hold the attribute value to be retrieved */
tmp.val = emalloc(atts[ia].len * nctypelen(atts[ia].type));
if (ncattget(cdfid, ww_id, atts[ia-1].name, tmp.val) == -1) {
error("%s: ncattget of variable attribute failed", pname);
ncclose(cdfid); return ++nerrs;
}
if (val_cmp(tmp.type, tmp.len, tmp.val, atts[ia].val) != 0) {
error("%s: ncattget got bad values after put of larger attrs",
pname);
nerrs++;
}
free (tmp.val);
}
/* try with bad datatype, should fail */
if (ncattput(cdfid, ww_id, "bogus_att1", BAD_TYPE,
atts[0].len, atts[0].val) != -1) {
error("%s: ncattput should fail with bad type", pname);
nerrs++;
}
/* try with negative length, should fail */
if (ncattput(cdfid, ww_id, "bogus_att2", atts[0].type,
-1, atts[0].val) != -1) {
error("%s: ncattput should fail with bad length", pname);
nerrs++;
}
if (ncendef (cdfid) == -1) {
error("%s: ncendef failed", pname);
ncclose(cdfid); return ++nerrs;
}
/* in data mode try increasing length of attribute, should fail */
if (ncattput(cdfid, ww_id, atts[0].name, atts[0].type,
atts[0].len + 10, atts[0].val) != -1) {
error("%s: ncattput should fail with increased length in data mode",
pname);
nerrs++;
/* reset to correct length for later tests */
if (ncattput(cdfid, ww_id, atts[0].name, atts[0].type,
atts[0].len, atts[0].val) != -1) {
error("%s: ncattput failed to reset length in data mode", pname);
nerrs++;
}
}
/* try creating new attribute in data mode, should fail */
if (ncattput(cdfid, ww_id, "new_name", atts[0].type,
atts[0].len, atts[0].val) != -1) {
error("%s: ncattput of new attribute in data mode should fail",
pname);
ncclose(cdfid); return ++nerrs;
}
/*
* check that changing type of existing attribute, decreasing
* length of attribute, and changing value of existing attribute
* work OK in data mode
*/
for (ia = 0; ia < na - 1; ia++) {
if (ncattput(cdfid, ww_id, atts[ia+1].name, atts[ia].type,
atts[ia].len, atts[ia].val) == -1) {
error("%s: ncattput of smaller attribute failed in data mode",
pname);
ncclose(cdfid); return ++nerrs;
}
tmp.var = atts[ia].var;
(void) strcpy (tmp.name, atts[ia+1].name);
tmp.type = atts[ia].type;
tmp.len = atts[ia].len;
tmp.val = atts[ia].val;
add_att(&test, ww_id, &tmp); /* keep in-memory netcdf updated */
}
/* check with ncattinq and ncattget that variable attributes put OK */
for (ia = 0; ia < na - 1; ia++) {
if (ncattinq(cdfid, ww_id, atts[ia+1].name, &tmp.type, &tmp.len)
== -1) {
error("%s: ncattinq of variable attribute failed in data mode",
pname);
ncclose(cdfid); return ++nerrs;
}
if (atts[ia].type != tmp.type || atts[ia].len != tmp.len) {
error("%s: VARIABLE ncattinq got bad type or len in data mode",
pname);
ncclose(cdfid); return ++nerrs;
}
/* allocate space to hold the attribute value to be retrieved */
tmp.val = emalloc(atts[ia].len * nctypelen(atts[ia].type));
if (ncattget(cdfid, ww_id, atts[ia+1].name, tmp.val) == -1) {
error("%s: ncattget of variable attribute failed in data mode",
pname);
ncclose(cdfid); return ++nerrs;
}
if (val_cmp(tmp.type, tmp.len, tmp.val, atts[ia].val) != 0) {
error("%s: ncattget got bad values in data mode", pname);
nerrs++;
}
free (tmp.val);
}
/* try with bad variable handle, should fail */
if (ncattput(cdfid, test.nvars, atts[0].name, atts[0].type, atts[0].len,
atts[0].val) != -1) {
error("%s: ncattput should fail with bad variable handle", pname);
ncclose(cdfid); return ++nerrs;
}
if (ncclose (cdfid) == -1) {
error("%s: ncclose failed", pname);
return ++nerrs;
}
/* try with bad netcdf handle, should fail */
if (ncattput(cdfid, ww_id, atts[0].name, atts[0].type, atts[0].len,
atts[0].val) != -1) {
error("%s: ncattput should fail with bad netcdf handle", pname);
ncclose(cdfid); return ++nerrs;
}
free(tmp.name);
free(ww.dims);
if (nerrs > 0)
(void) fprintf(stderr,"FAILED! ***\n");
else
(void) fprintf(stderr,"ok ***\n");
return nerrs;
}
/*
* Test ncattinq
* check returned values of properly created attributes
* try with nonexisting attribute, check error
* try with bad variable handle, check error
* try with bad netCDF handle, check error
*/
int
test_ncattinq(path)
const char *path; /* name of writable netcdf file to open */
{
int nerrs = 0;
static char pname[] = "test_ncattinq";
int cdfid; /* netcdf id */
int ia, id; /* attribute number */
nc_type type;
int len;
int vv_id; /* variable id */
static struct cdfvar vv = /* new variable */
{"vv", NC_SHORT, 2, ___, 0};
(void) fprintf(stderr, "*** Testing %s ...\t", &pname[5]);
if ((cdfid = ncopen(path, NC_WRITE)) == -1) {
error("%s: ncopen failed", pname);
return ++nerrs;
}
/* in data mode, check all attributes against test netcdf */
for (ia = 0; ia < test.natts; ia++) {
if (ncattinq(cdfid, test.atts[ia].var, test.atts[ia].name,
&type, &len) == -1) {
error("%s: ncattinq failed", pname);
ncclose(cdfid);
return ++nerrs;
}
if (type != test.atts[ia].type) {
error("%s: ncattinq returned wrong type", pname);
ncclose(cdfid);
return ++nerrs;
}
if (len != test.atts[ia].len) {
error("%s: ncattinq returned wrong len", pname);
ncclose(cdfid);
return ++nerrs;
}
}
/* enter define mode */
if (ncredef(cdfid) == -1) {
error("%s: cdredef failed", pname);
ncclose(cdfid); return ++nerrs;
}
/* in define mode, add a variable */
vv.dims = (int *) emalloc(sizeof(int) * vv.ndims);
for (id = 0; id < vv.ndims; id++)
vv.dims[id] = id; /* assumes vv.ndims <= test.ndims */
if ((vv_id = ncvardef(cdfid, vv.name, vv.type, vv.ndims, vv.dims))
== -1) {
error("%s: ncvardef failed", pname);
ncclose(cdfid); return ++nerrs;
}
add_var(&test, &vv); /* keep in-memory netcdf in sync */
/* try with nonexisting attribute, should fail */
if (ncattinq(cdfid, vv_id, "nonesuch", &type, &len) != -1) {
error("%s: ncattinq should fail with nonexisting attribute", pname);
ncclose(cdfid); return ++nerrs;
}
/* try with bad variable handle, should fail */
if (ncattinq(cdfid, test.nvars, test.atts[0].name, &type, &len) != -1) {
error("%s: ncattinq should fail with bad variable id", pname);
ncclose(cdfid); return ++nerrs;
}
/* in define mode check all attributes against test netcdf */
for (ia = 0; ia < test.natts; ia++) {
if (ncattinq(cdfid, test.atts[ia].var, test.atts[ia].name,
&type, &len) == -1) {
error("%s: ncattinq in define mode failed", pname);
ncclose(cdfid);
return ++nerrs;
}
if (type != test.atts[ia].type) {
error("%s: ncattinq in define mode returned wrong type", pname);
ncclose(cdfid);
return ++nerrs;
}
if (len != test.atts[ia].len) {
error("%s: ncattinq in define mode returned wrong len", pname);
ncclose(cdfid);
return ++nerrs;
}
}
if (ncendef (cdfid) == -1) {
error("%s: ncendef failed", pname);
ncclose(cdfid); return ++nerrs;
}
if (ncclose (cdfid) == -1) {
error("%s: ncclose failed", pname);
return ++nerrs;
}
if (ncattinq(cdfid, NC_GLOBAL, test.atts[0].name, &type, &len) != -1) {
error("%s: ncattinq should fail with bad cdfid", pname);
nerrs++;
}
if (nerrs > 0)
(void) fprintf(stderr,"FAILED! ***\n");
else
(void) fprintf(stderr,"ok ***\n");
free(vv.dims);
return nerrs;
}
/*
* Test ncattget
* check that NC_GLOBAL variable id works
* check in both modes
* check that proper call worked after ncattput
* try with bad variable handle, check error
* try with nonexisting attribute, check error
* try with bad netCDF handle, check error
*/
int
test_ncattget(path)
const char *path; /* name of writable netcdf file to open */
{
int nerrs = 0;
int cdfid; /* netcdf id */
int ia, id;
static struct cdfatt tmp; /* attribute */
int uu_id; /* variable id */
static struct cdfvar uu = /* variable */
{"uu", NC_LONG, 2, ___, 0};
static nclong uumax = 1000; /* attribute value */
static struct cdfatt vmax = /* attribute */
{___, "valid_max", NC_LONG, 1, (void *) &uumax};
static char pname[] = "test_ncattget";
(void) fprintf(stderr, "*** Testing %s ...\t", &pname[5]);
if ((cdfid = ncopen(path, NC_WRITE)) == -1) {
error("%s: ncopen failed", pname);
return ++nerrs;
}
/* enter define mode */
if (ncredef(cdfid) == -1) {
error("%s: cdredef failed", pname);
ncclose(cdfid); return ++nerrs;
}
/* add a variable */
uu.dims = (int *) emalloc(sizeof(int) * uu.ndims);
for (id = 0; id < uu.ndims; id++)
uu.dims[id] = id;
if ((uu_id = ncvardef(cdfid,
uu.name, uu.type, uu.ndims, uu.dims)) == -1) {
error("%s: ncvardef failed", pname);
ncclose(cdfid); return ++nerrs;
}
add_var(&test, &uu); /* keep in-memory netcdf in sync */
/* add an attribute */
if (ncattput(cdfid, uu_id,
vmax.name, vmax.type, vmax.len, vmax.val)
== -1) {
error("%s: ncattput of variable attribute failed", pname);
ncclose(cdfid); return ++nerrs;
}
add_att(&test, uu_id, &vmax); /* keep in-memory netcdf updated */
/* in define mode, check all attributes values against test netcdf */
for (ia = 0; ia < test.natts; ia++) {
if (ncattinq(cdfid, test.atts[ia].var, test.atts[ia].name,
&tmp.type, &tmp.len) == -1) {
error("%s: ncattinq in define mode failed", pname);
ncclose(cdfid); return ++nerrs;
}
if (tmp.type != test.atts[ia].type) {
error("%s: ncattinq in define mode returned wrong type", pname);
ncclose(cdfid);
return ++nerrs;
}
if (tmp.len != test.atts[ia].len) {
error("%s: ncattinq in define mode returned wrong len", pname);
ncclose(cdfid); return ++nerrs;
}
/* allocate space to hold the attribute value to be retrieved */
tmp.val = emalloc(tmp.len * nctypelen(tmp.type));
if (ncattget(cdfid, test.atts[ia].var, test.atts[ia].name, tmp.val)
== -1) {
error("%s: ncattget of variable attribute failed in define mode",
pname);
ncclose(cdfid); return ++nerrs;
}
if (val_cmp(tmp.type, tmp.len, tmp.val, test.atts[ia].val) != 0) {
error("%s: ncattget got bad values in define mode", pname);
error(" cdfid=%d, varname=%s, attname=%s, type=%d, len=%d",
cdfid, test.vars[test.atts[ia].var].name,
test.atts[ia].name, test.atts[ia].type, test.atts[ia].len);
(void)fprintf(stderr,"should have got:");
val_out(test.atts[ia].type, test.atts[ia].len,
test.atts[ia].val);
(void)fprintf(stderr," instead got:");
val_out(tmp.type, tmp.len, tmp.val);
nerrs++;
}
free (tmp.val);
}
if (ncendef (cdfid) == -1) {
error("%s: ncendef failed", pname);
ncclose(cdfid); return ++nerrs;
}
/* in data mode, check all attributes values against test netcdf */
for (ia = 0; ia < test.natts; ia++) {
if (ncattinq(cdfid, test.atts[ia].var, test.atts[ia].name,
&tmp.type, &tmp.len) == -1) {
error("%s: ncattinq failed", pname);
ncclose(cdfid);
return ++nerrs;
}
if (tmp.type != test.atts[ia].type) {
error("%s: ncattinq returned wrong type", pname);
ncclose(cdfid);
return ++nerrs;
}
if (tmp.len != test.atts[ia].len) {
error("%s: ncattinq returned wrong len", pname);
ncclose(cdfid);
return ++nerrs;
}
/* allocate space to hold the attribute value to be retrieved */
tmp.val = emalloc(tmp.len * nctypelen(tmp.type));
if (ncattget(cdfid, test.atts[ia].var, test.atts[ia].name, tmp.val)
== -1) {
error("%s: ncattget of variable attribute failed in data mode",
pname);
ncclose(cdfid); return ++nerrs;
}
if (val_cmp(tmp.type, tmp.len, tmp.val, test.atts[ia].val) != 0) {
error("%s: ncattget got bad values in data mode", pname);
error(" cdfid=%d, varname=%s, attname=%s, type=%d, len=%d",
cdfid, test.vars[test.atts[ia].var].name,
test.atts[ia].name, test.atts[ia].type, test.atts[ia].len);
(void)fprintf(stderr,"should have got:");
val_out(test.atts[ia].type, test.atts[ia].len,
test.atts[ia].val);
(void)fprintf(stderr," instead got:");
val_out(tmp.type, tmp.len, tmp.val);
nerrs++;
}
free (tmp.val);
}
/* try with bad variable handle, should fail */
if (ncattget(cdfid, test.nvars, vmax.name, vmax.val) != -1) {
error("%s: ncattget should fail with bad variable handle", pname);
ncclose(cdfid); return ++nerrs;
}
/* try getting non-existent attribute, should fail */
if (ncattget(cdfid, uu_id, "nonesuch", vmax.val) != -1) {
error("%s: ncattget should fail with nonexistent attribute", pname);
ncclose(cdfid); return ++nerrs;
}
if (ncclose (cdfid) == -1) {
error("%s: ncclose failed", pname);
return ++nerrs;
}
/* try with bad netcdf handle, should fail */
if (ncattget(cdfid, uu_id, vmax.name, vmax.val) != -1) {
error("%s: ncattput should fail with bad netcdf handle", pname);
ncclose(cdfid); return ++nerrs;
}
if (nerrs > 0)
(void) fprintf(stderr,"FAILED! ***\n");
else
(void) fprintf(stderr,"ok ***\n");
free(uu.dims);
return nerrs;
}
/*
* Test ncattcopy
* check that NC_GLOBAL variable for source or target works
* check that new attribute put works with target in define mode
* check that old attribute put works with target in data mode
* check that changing type and length of an attribute work OK
* try with same cdfid for source and target, different variables
* try with same cdfid for source and target, same variable
* try with nonexisting attribute, check error
* try with bad source or target netCDF handles, check error
* try with bad source or target variable handle, check error
*/
int
test_ncattcopy(path1, path2)
const char *path1; /* name of input netcdf file to open */
const char *path2; /* name of output netcdf file to create */
{
int nerrs = 0;
static char pname[] = "test_ncattcopy";
int cdfid, cdfid2; /* netcdf id */
int id; /* dimension id */
int tt_id; /* variable id */
static struct cdfvar tt = /* new variable for source netcdf */
{"tt", NC_LONG, 1, ___, 0};
int tu_id, tu2_id; /* variable ids */
static struct cdfvar tu = /* new variable for target netcdf */
{"tu", NC_DOUBLE, 2, ___, 0};
static double double_vals[] = {-1., -2.};
static float float_vals[] = {-1., -2.};
static struct cdfatt att = /* attribute */
{___, "att", NC_DOUBLE, LEN_OF(double_vals), (void *) double_vals};
static struct cdfatt att2 = /* attribute */
{___, "att", NC_FLOAT, LEN_OF(float_vals), (void *) float_vals};
static struct cdfatt tmp; /* attribute */
(void) fprintf(stderr, "*** Testing %s ...\t", &pname[5]);
if ((cdfid = ncopen(path1, NC_WRITE)) == -1) {
error("%s: ncopen failed", pname);
return ++nerrs;
}
/* opened OK, enter define mode */
if (ncredef(cdfid) == -1) {
error("%s: ncredef failed on source", pname);
ncclose(cdfid); return ++nerrs;
}
/* in define mode, add a global attribute, a variable and an attribute */
if (ncattput(cdfid, NC_GLOBAL, att.name, att.type, att.len, att.val) == -1) {
error("%s: ncattput failed", pname);
ncclose(cdfid); return ++nerrs;
}
add_att(&test, NC_GLOBAL, &att); /* keep in-memory netcdf consistent */
tt.dims = (int *) emalloc(sizeof(int) * tt.ndims);
for (id=0; id < tt.ndims; id++)
tt.dims[0] = id;
if ((tt_id=ncvardef(cdfid, tt.name, tt.type, tt.ndims, tt.dims)) == -1) {
error("%s: ncvardef failed", pname);
ncclose(cdfid); return ++nerrs;
}
add_var(&test, &tt); /* keep in-memory netcdf consistent */
if (ncattput(cdfid, tt_id, att.name, att.type, att.len, att.val) == -1) {
error("%s: ncattput failed", pname);
ncclose(cdfid); return ++nerrs;
}
add_att(&test, tt_id, &att); /* keep in-memory netcdf consistent */
tu.dims = (int *) emalloc(sizeof(int) * tu.ndims);
for (id = 0; id < tu.ndims; id++)
tu.dims[id] = id;
if ((tu_id=ncvardef(cdfid, tu.name, tu.type, tu.ndims, tu.dims)) == -1) {
error("%s: ncvardef failed", pname);
ncclose(cdfid); return ++nerrs;
}
add_var(&test, &tu); /* keep in-memory netcdf consistent */
if (ncattput(cdfid, tu_id, att.name, att.type, att.len, att.val) == -1) {
error("%s: ncattput failed", pname);
ncclose(cdfid); return ++nerrs;
}
add_att(&test, tu_id, &att); /* keep in-memory netcdf consistent */
if (ncendef (cdfid) == -1) {
error("%s: ncendef failed", pname);
ncclose(cdfid); return ++nerrs;
}
/* first (source) netcdf is in data mode */
/* create second netCDF to copy attributes to */
if ((cdfid2 = nccreate(path2, NC_CLOBBER)) == -1) {
error("%s: nccreate failed", pname);
return ++nerrs;
}
/* create dimensions and variable in second netcdf */
for (id = 0; id < tu.ndims; id++) { /* copy dimensions from source */
if ((tu.dims[id] =ncdimdef(cdfid2, test.dims[id].name,
test.dims[id].size)) == -1) {
error("%s: ncdimdef failed", pname);
ncclose(cdfid); ncclose(cdfid2); return ++nerrs;
}
}
if ((tu2_id=ncvardef(cdfid2, tu.name, tu.type, tu.ndims, tu.dims)) == -1) {
error("%s: ncvardef failed", pname);
ncclose(cdfid); ncclose(cdfid2); return ++nerrs;
}
/* try copying NC_GLOBAL attribute from source to target */
if (ncattcopy(cdfid, NC_GLOBAL, att.name, cdfid2, NC_GLOBAL) == -1) {
error("%s: ncattcopy on NC_GLOBAL attribute '%s' failed",
pname, att.name);
ncclose(cdfid); ncclose(cdfid2); return ++nerrs;
}
/* check that copy worked with ncattinq and ncattget */
if (ncattinq(cdfid2, NC_GLOBAL, att.name, &tmp.type, &tmp.len) == -1) {
error("%s: ncattinq of NC_GLOBAL attribute failed", pname);
ncclose(cdfid); ncclose(cdfid2); return ++nerrs;
}
if (att.type != tmp.type || att.len != tmp.len) {
error("%s: NC_GLOBAL ncattinq got unexpected type or len", pname);
ncclose(cdfid); ncclose(cdfid2); return ++nerrs;
}
/* allocate space to hold the attribute value to be retrieved */
tmp.val = emalloc(att.len * nctypelen(att.type));
if (ncattget(cdfid2, NC_GLOBAL, att.name, tmp.val) == -1) {
error("%s: ncattget of variable attribute failed", pname);
ncclose(cdfid); ncclose(cdfid2); return ++nerrs;
}
if (val_cmp(tmp.type, tmp.len, tmp.val, att.val) != 0) {
error("%s: ncattget got bad values after put of NC_GLOBAL attrs",
pname);
nerrs++;
}
free (tmp.val);
/* try copying variable attribute from source to target */
if (ncattcopy(cdfid, tt_id, att.name, cdfid2, tu2_id) == -1) {
error("%s: ncattcopy failed", pname);
ncclose(cdfid); ncclose(cdfid2); return ++nerrs;
}
/* check that copy worked with ncattinq and ncattget */
if (ncattinq(cdfid2, tu2_id, att.name, &tmp.type, &tmp.len) == -1) {
error("%s: ncattinq of variable attribute failed", pname);
ncclose(cdfid); ncclose(cdfid2); return ++nerrs;
}
if (att.type != tmp.type || att.len != tmp.len) {
error("%s: variable ncattinq got unexpected type or len", pname);
ncclose(cdfid); ncclose(cdfid2); return ++nerrs;
}
/* allocate space to hold the attribute value to be retrieved */
tmp.val = emalloc(att.len * nctypelen(att.type));
if (ncattget(cdfid2, tu2_id, att.name, tmp.val) == -1) {
error("%s: ncattget of variable attribute failed", pname);
ncclose(cdfid); ncclose(cdfid2); return ++nerrs;
}
if (val_cmp(tmp.type, tmp.len, tmp.val, att.val) != 0) {
error("%s: ncattget got bad values after copy of variable attrs",
pname);
nerrs++;
}
free (tmp.val);
/*
* check that old attribute put works with target in data mode,
* also checks that changing type and length of an attribute works OK
*/
if (ncendef (cdfid2) == -1) {
error("%s: ncendef failed", pname);
ncclose(cdfid); ncclose(cdfid2); return ++nerrs;
}
/* change attribute to shorter attribute */
if (ncattput(cdfid, NC_GLOBAL, att2.name, att2.type, att2.len, att2.val)
== -1) {
error("%s: ncattput of shorter NC_GLOBAL attribute failed", pname);
ncclose(cdfid); ncclose(cdfid2); return ++nerrs;
}
add_att(&test, NC_GLOBAL, &att2); /* keep in-memory netcdf consistent */
/* copy shorter attribute on existing attribute */
if (ncattcopy(cdfid, NC_GLOBAL, att2.name, cdfid2, tu2_id) == -1) {
error("%s: ncattcopy of shorter attribute on old attribute failed",
pname);
ncclose(cdfid); ncclose(cdfid2); return ++nerrs;
}
/* check that copy worked with ncattinq and ncattget */
if (ncattinq(cdfid2, tu2_id, att2.name, &tmp.type, &tmp.len) == -1) {
error("%s: ncattinq of variable attribute failed", pname);
ncclose(cdfid); ncclose(cdfid2); return ++nerrs;
}
if (att2.type != tmp.type || att2.len != tmp.len) {
error("%s: variable ncattinq got unexpected type or len", pname);
ncclose(cdfid); ncclose(cdfid2); return ++nerrs;
}
/* allocate space to hold the attribute value to be retrieved */
tmp.val = emalloc(att2.len * nctypelen(att2.type));
if (ncattget(cdfid2, tu2_id, att2.name, tmp.val) == -1) {
error("%s: ncattget of variable attribute failed", pname);
ncclose(cdfid); ncclose(cdfid2); return ++nerrs;
}
if (val_cmp(tmp.type, tmp.len, tmp.val, att2.val) != 0) {
error("%s: ncattget got bad values after copy of variable attrs",
pname);
nerrs++;
}
free (tmp.val);
/* try copying with same source and target netcdf, different variables */
/* copy shorter attribute on existing attribute */
if (ncattcopy(cdfid, NC_GLOBAL, att2.name, cdfid, tu_id) == -1) {
error("%s: ncattcopy of shorter NC_GLOBAL attribute failed", pname);
ncclose(cdfid); ncclose(cdfid2); return ++nerrs;
}
add_att(&test, tu_id, &att2); /* keep in-memory netcdf consistent */
/* check that copy worked with ncattinq and ncattget */
if (ncattinq(cdfid, tu_id, att2.name, &tmp.type, &tmp.len) == -1) {
error("%s: ncattinq of variable attribute failed", pname);
ncclose(cdfid); ncclose(cdfid2); return ++nerrs;
}
if (att2.type != tmp.type || att2.len != tmp.len) {
error("%s: variable ncattinq got unexpected type or len", pname);
ncclose(cdfid); ncclose(cdfid2); return ++nerrs;
}
/* allocate space to hold the attribute value to be retrieved */
tmp.val = emalloc(att2.len * nctypelen(att2.type));
if (ncattget(cdfid, tu_id, att2.name, tmp.val) == -1) {
error("%s: ncattget of variable attribute failed", pname);
ncclose(cdfid); ncclose(cdfid2); return ++nerrs;
}
if (val_cmp(tmp.type, tmp.len, tmp.val, att2.val) != 0) {
error("%s: ncattget got bad values after copy of variable attrs",
pname);
nerrs++;
}
free (tmp.val);
/* try with same cdfid for source and target, same variable */
if (ncattcopy(cdfid, tu_id, att.name, cdfid, tu_id) == -1) {
error("%s: ncattcopy failed with identical source and target",
pname);
ncclose(cdfid); ncclose(cdfid2); return ++nerrs;
}
/* try with nonexisting attribute, check error */
if (ncattcopy(cdfid, tt_id, "nonesuch", cdfid, tu_id) != -1) {
error("%s: ncattcopy should fail with bad attribute name",
pname);
ncclose(cdfid); ncclose(cdfid2); return ++nerrs;
}
/* try with bad source or target variable handle, check error */
if (ncattcopy(cdfid, test.nvars, att.name, cdfid, tu_id) != -1) {
error("%s: ncattcopy should fail with bad source variable id",
pname);
ncclose(cdfid); ncclose(cdfid2); return ++nerrs;
}
if (ncattcopy(cdfid, tt_id, att.name, cdfid, 2) != -1) {
error("%s: ncattcopy should fail with bad target variable id",
pname);
ncclose(cdfid); ncclose(cdfid2); return ++nerrs;
}
if (ncclose (cdfid2) == -1) {
error("%s: ncclose failed", pname);
ncclose(cdfid); return ++nerrs;
}
/* try with bad source or target netCDF handles, check error */
if (ncattcopy(cdfid, tt_id, att.name, cdfid2, tu_id) != -1) {
error("%s: ncattcopy should fail with bad target netcdf id",
pname);
ncclose(cdfid); return ++nerrs;
}
if (ncclose (cdfid) == -1) {
error("%s: ncclose failed", pname);
return ++nerrs;
}
if (ncattcopy(cdfid, tt_id, att.name, cdfid2, tu_id) != -1) {
error("%s: ncattcopy should fail with bad netcdf id", pname);
nerrs++;
}
if (nerrs > 0)
(void) fprintf(stderr,"FAILED! ***\n");
else
(void) fprintf(stderr,"ok ***\n");
free(tt.dims);
free(tu.dims);
return nerrs;
}
/*
* Test ncattname
* check that NC_GLOBAL variable id works
* check in both modes
* check that proper call worked after ncattput
* try with bad netCDF handle, check error
* try with bad variable handle, check error
* try with bad attribute number, check error
*/
int
test_ncattname(path)
const char *path; /* name of writable netcdf file to open */
{
int nerrs = 0;
static char pname[] = "test_ncattname";
int cdfid; /* netcdf id */
struct cdfatt tmp; /* attributes */
int ia, ib; /* attribute numbers */
int iv; /* variable id */
static short short_vals[] = {3, 4, 5};
static struct cdfatt att = /* attribute */
{___, ___, NC_SHORT, LEN_OF(short_vals), (void *) short_vals};
(void) fprintf(stderr, "*** Testing %s ...\t", &pname[5]);
if ((cdfid = ncopen(path, NC_WRITE)) == -1) {
error("%s: ncopen failed", pname);
return ++nerrs;
}
/* opened OK, enter define mode */
if (ncredef(cdfid) == -1) {
error("%s: ncredef failed", pname);
ncclose(cdfid); return ++nerrs;
}
/* for each NC_GLOBAL attribute, get name and compare with expected name */
att.name = (char *) emalloc(MAX_NC_NAME);
ib = 0;
for (ia = 0; ia < test.ngatts; ia++) {
if (ncattname(cdfid, NC_GLOBAL, ia, att.name) == -1) {
error("%s: ncattname failed on global attribute", pname);
ncclose(cdfid); return ++nerrs;
}
/* find number of next global attribute */
while (ib < test.natts && test.atts[ib].var != NC_GLOBAL)
ib++;
if (ib >= test.natts) {
error("%s: test problem, expected global attribute not found",
pname);
ncclose(cdfid); return ++nerrs;
}
if (strcmp(att.name, test.atts[ib].name) != 0) {
error("%s: NC_GLOBAL attribute name `%s' instead of expected `%s'",
pname, att.name, test.atts[ib].name);
nerrs++;
}
ib++;
}
/* for each variable attribute, get name and compare with expected name */
for (iv = 0; iv < test.nvars; iv++) {
ib = 0;
for (ia = 0; ia < test.vars[iv].natts; ia++) {
if (ncattname(cdfid, iv, ia, att.name) == -1) {
error("%s: ncattname failed on variable attribute", pname);
ncclose(cdfid); return ++nerrs;
}
/* find number of next attribute */
while (ib < test.natts && test.atts[ib].var != iv)
ib++;
if (ib >= test.natts) {
error("%s: problem in test, expected attribute not found",
pname);
ncclose(cdfid); return ++nerrs;
}
if (strcmp(att.name, test.atts[ib].name) != 0) {
error("%s: variable '%s' name `%s' instead of expected `%s'",
pname, test.vars[iv].name, att.name, test.atts[ib].name);
nerrs++;
}
ib++;
}
}
/* in define mode, add a global attribute */
(void) strcpy(att.name,"attx");
if (ncattput(cdfid, NC_GLOBAL, att.name, att.type, att.len, att.val)
== -1) {
error("%s: ncattput failed", pname);
ncclose(cdfid); return ++nerrs;
}
add_att(&test, NC_GLOBAL, &att); /* keep in-memory netcdf consistent */
/* test that ncattname works immediately after ncattput */
tmp.name = (char *) emalloc(MAX_NC_NAME);
if (ncattname(cdfid, NC_GLOBAL, test.ngatts-1, tmp.name) == -1) {
error("%s: ncattname failed on variable attribute", pname);
ncclose(cdfid); return ++nerrs;
}
if (strcmp(att.name, tmp.name) != 0) {
error("%s: immediate NC_GLOBAL name `%s' instead of expected `%s'",
pname, tmp.name, att.name);
nerrs++;
}
if (ncendef (cdfid) == -1) {
error("%s: ncendef failed", pname);
ncclose(cdfid); return ++nerrs;