forked from Unidata/netcdf-c
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathnc4internal.c
More file actions
2200 lines (1915 loc) · 59.5 KB
/
nc4internal.c
File metadata and controls
2200 lines (1915 loc) · 59.5 KB
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 2003-2018, University Corporation for Atmospheric
* Research. See the COPYRIGHT file for copying and redistribution
* conditions.
*/
/**
* @file
* @internal
* Internal netcdf-4 functions.
*
* This file contains functions internal to the netcdf4 library. None of
* the functions in this file are exposed in the exetnal API. These
* functions all relate to the manipulation of netcdf-4's in-memory
* buffer of metadata information, i.e. the linked list of NC
* structs.
*
* @author Ed Hartnett, Dennis Heimbigner, Ward Fisher
*/
#include "config.h"
#include "netcdf.h"
#include "netcdf_filter.h"
#include "netcdf_meta.h"
#include "nc4internal.h"
#include "nc.h" /* from libsrc */
#include "ncdispatch.h" /* from libdispatch */
#include "ncutf8.h"
#include <stdarg.h>
#include "ncrc.h"
/** @internal Number of reserved attributes. These attributes are
* hidden from the netcdf user, but exist in the implementation
* datasets to help netcdf read the dataset.
* Moved here from hdf5file.c.
* These tables need to capture all reserved attributes
* across all possible dispatchers
*/
/** @internal List of reserved attributes.
WARNING: This list must be in (strcmp) sorted order for binary search. */
static const NC_reservedatt NC_reserved[] = {
{NC_ATT_CLASS, READONLYFLAG|HIDDENATTRFLAG}, /*CLASS*/
{NC_ATT_DIMENSION_LIST, READONLYFLAG|HIDDENATTRFLAG}, /*DIMENSION_LIST*/
{NC_ATT_NAME, READONLYFLAG|HIDDENATTRFLAG}, /*NAME*/
{NC_ATT_REFERENCE_LIST, READONLYFLAG|HIDDENATTRFLAG}, /*REFERENCE_LIST*/
{NC_XARRAY_DIMS, READONLYFLAG|NAMEONLYFLAG|HIDDENATTRFLAG}, /*_ARRAY_DIMENSIONS*/
{NC_ATT_CODECS, VARFLAG|READONLYFLAG|NAMEONLYFLAG}, /*_Codecs*/
{NC_ATT_FORMAT, READONLYFLAG}, /*_Format*/
{ISNETCDF4ATT, READONLYFLAG|NAMEONLYFLAG}, /*_IsNetcdf4*/
{NCPROPS,READONLYFLAG|NAMEONLYFLAG|HIDDENATTRFLAG}, /*_NCProperties*/
{NC_NCZARR_ATTR_UC, READONLYFLAG|HIDDENATTRFLAG}, /*_NCZARR_ATTR */
{NC_ATT_COORDINATES, READONLYFLAG|HIDDENATTRFLAG}, /*_Netcdf4Coordinates*/
{NC_ATT_DIMID_NAME, READONLYFLAG|HIDDENATTRFLAG}, /*_Netcdf4Dimid*/
{SUPERBLOCKATT, READONLYFLAG|NAMEONLYFLAG}, /*_SuperblockVersion*/
{NC_ATT_NC3_STRICT_NAME, READONLYFLAG}, /*_nc3_strict*/
{NC_ATT_NC3_STRICT_NAME, READONLYFLAG}, /*_nc3_strict*/
{NC_NCZARR_ATTR, READONLYFLAG|HIDDENATTRFLAG}, /*_nczarr_attr */
};
#define NRESERVED (sizeof(NC_reserved) / sizeof(NC_reservedatt)) /*|NC_reservedatt|*/
static int NC4_move_in_NCList(NC* nc, int new_id);
#if NC_HAS_LOGGING
/* This is the severity level of messages which will be logged. Use
severity 0 for errors, 1 for important log messages, 2 for less
important, etc. */
int nc_log_level = NC_TURN_OFF_LOGGING;
#if NC_HAS_PARALLEL4
/* File pointer for the parallel I/O log file. */
FILE *LOG_FILE = NULL;
#endif /* NC_HAS_PARALLEL4 */
/* This function prints out a message, if the severity of
* the message is lower than the global nc_log_level. To use it, do
* something like this:
*
* nc_log(0, "this computer will explode in %d seconds", i);
*
* After the first arg (the severity), use the rest like a normal
* printf statement. Output will appear on stderr for sequential
* builds, and in a file nc4_log_R.log for each process for a parallel
* build, where R is the rank of the process.
*
* Ed Hartnett
*/
void
nc_log(int severity, const char *fmt, ...)
{
va_list argp;
int t;
FILE *f = stderr;
/* If the severity is greater than the log level, we don't print
* this message. */
if (severity > nc_log_level)
return;
#if NC_HAS_PARALLEL4
/* For parallel I/O build, if MPI has been initialized, instead of
* printing logging output to stderr, it goes to a file for each
* process. */
{
int mpi_initialized;
int mpierr;
/* Check to see if MPI has been initialized. */
if ((mpierr = MPI_Initialized(&mpi_initialized)))
return;
/* If MPI has been initialized use a log file. */
assert(LOG_FILE);
if (mpi_initialized)
f = LOG_FILE;
}
#endif /* NC_HAS_PARALLEL4 */
/* If the severity is zero, this is an error. Otherwise insert that
many tabs before the message. */
if (!severity)
fprintf(f, "ERROR: ");
for (t = 0; t < severity; t++)
fprintf(f, "\t");
/* Print out the variable list of args with vprintf. */
va_start(argp, fmt);
vfprintf(f, fmt, argp);
va_end(argp);
/* Put on a final linefeed. */
fprintf(f, "\n");
fflush(f);
}
#endif /* NC_HAS_LOGGING */
/**
* @internal Check and normalize and name.
*
* @param name Name to normalize.
* @param norm_name The normalized name.
*
* @return ::NC_NOERR No error.
* @return ::NC_EMAXNAME Name too long.
* @return ::NC_EINVAL NULL given for name.
* @return ::NC_ENOMEM Out of memory.
* @author Dennis Heimbigner
*/
int
nc4_check_name(const char *name, char *norm_name)
{
char *temp;
int retval;
assert(norm_name);
/* Check for NULL. */
if (!name)
return NC_EINVAL;
/* Make sure this is a valid netcdf name. This should be done
* before the name is normalized, because it gives better error
* codes for bad utf8 strings. */
if ((retval = NC_check_name(name)))
return retval;
/* Normalize the name. */
if ((retval = nc_utf8_normalize((const unsigned char *)name,
(unsigned char **)&temp)))
return retval;
/* Check length of normalized name. */
if (strlen(temp) > NC_MAX_NAME)
{
free(temp);
return NC_EMAXNAME;
}
/* Copy the normalized name. */
strcpy(norm_name, temp);
free(temp);
return NC_NOERR;
}
/**
* @internal Add a file to the list of libsrc4 open files. This is
* used by dispatch layers that wish to use the libsrc4 metadata
* model, but don't know about struct NC. This is the same as
* nc4_nc4f_list_add(), except it takes an ncid instead of an NC *,
* and also passes back the dispatchdata pointer.
*
* @param ncid The (already-assigned) ncid of the file (aka ext_ncid).
* @param path The file name of the new file.
* @param mode The mode flag.
* @param dispatchdata Void * that gets pointer to dispatch data,
* which is the NC_FILE_INFO_T struct allocated for this file and its
* metadata. Ignored if NULL. (This is passed as a void to allow
* external user-defined formats to use this function.)
*
* @return ::NC_NOERR No error.
* @return ::NC_EBADID No NC struct with this ext_ncid.
* @return ::NC_ENOMEM Out of memory.
* @author Ed Hartnett
*/
int
nc4_file_list_add(int ncid, const char *path, int mode, void **dispatchdata)
{
NC *nc;
int ret;
/* Find NC pointer for this file. */
if ((ret = NC_check_id(ncid, &nc)))
return ret;
/* Add necessary structs to hold netcdf-4 file data. This is where
* the NC_FILE_INFO_T struct is allocated for the file. */
if ((ret = nc4_nc4f_list_add(nc, path, mode)))
return ret;
/* If the user wants a pointer to the NC_FILE_INFO_T, then provide
* it. */
if (dispatchdata)
*dispatchdata = nc->dispatchdata;
return NC_NOERR;
}
/**
* @internal Change the ncid of an open file. This is needed for PIO
* integration.
*
* @param ncid The ncid of the file (aka ext_ncid).
* @param new_ncid_index The new ncid index to use (i.e. the first two bytes
* of the ncid).
*
* @return ::NC_NOERR No error.
* @return ::NC_EBADID No NC struct with this ext_ncid.
* @return ::NC_ENOMEM Out of memory.
* @author Ed Hartnett
*/
int
nc4_file_change_ncid(int ncid, unsigned short new_ncid_index)
{
NC *nc;
int ret;
LOG((2, "%s: ncid %d new_ncid_index %d", __func__, ncid, new_ncid_index));
/* Find NC pointer for this file. */
if ((ret = NC_check_id(ncid, &nc)))
return ret;
/* Move it in the list. It will faile if list spot is already
* occupied. */
LOG((3, "moving nc->ext_ncid %d nc->ext_ncid >> ID_SHIFT %d",
nc->ext_ncid, nc->ext_ncid >> ID_SHIFT));
if (NC4_move_in_NCList(nc, new_ncid_index))
return NC_EIO;
LOG((3, "moved to new_ncid_index %d new nc->ext_ncid %d", new_ncid_index,
nc->ext_ncid));
return NC_NOERR;
}
/**
* @internal Get info about a file on the list of libsrc4 open
* files. This is used by dispatch layers that wish to use the libsrc4
* metadata model, but don't know about struct NC.
*
* @param ncid The ncid of the file (aka ext_ncid).
* @param path A pointer that gets file name (< NC_MAX_NAME). Ignored
* if NULL.
* @param mode A pointer that gets the mode flag. Ignored if NULL.
* @param dispatchdata Void * that gets pointer to dispatch data,
* which is the NC_FILE_INFO_T struct allocated for this file and its
* metadata. Ignored if NULL. (This is passed as a void to allow
* external user-defined formats to use this function.)
*
* @return ::NC_NOERR No error.
* @return ::NC_EBADID No NC struct with this ext_ncid.
* @return ::NC_ENOMEM Out of memory.
* @author Ed Hartnett
*/
int
nc4_file_list_get(int ncid, char **path, int *mode, void **dispatchdata)
{
NC *nc;
int ret;
/* Find NC pointer for this file. */
if ((ret = NC_check_id(ncid, &nc)))
return ret;
/* If the user wants path, give it. */
if (path)
strncpy(*path, nc->path, NC_MAX_NAME);
/* If the user wants mode, give it. */
if (mode)
*mode = nc->mode;
/* If the user wants dispatchdata, give it. */
if (dispatchdata)
*dispatchdata = nc->dispatchdata;
return NC_NOERR;
}
/**
* @internal Given an NC pointer, add the necessary stuff for a
* netcdf-4 file. This allocates the NC_FILE_INFO_T struct for the
* file, which is used by libhdf5 and libhdf4 (and perhaps other
* future dispatch layers) to hold the metadata for the file.
*
* @param nc Pointer to file's NC struct.
* @param path The file name of the new file.
* @param mode The mode flag.
*
* @return ::NC_NOERR No error.
* @return ::NC_ENOMEM Out of memory.
* @author Ed Hartnett, Dennis Heimbigner
*/
int
nc4_nc4f_list_add(NC *nc, const char *path, int mode)
{
NC_FILE_INFO_T *h5;
int retval;
assert(nc && !NC4_DATA(nc) && path);
/* We need to malloc and initialize the substructure
NC_FILE_INFO_T. */
if (!(h5 = calloc(1, sizeof(NC_FILE_INFO_T))))
return NC_ENOMEM;
nc->dispatchdata = h5;
h5->controller = nc;
h5->hdr.sort = NCFIL;
h5->hdr.name = strdup(path);
h5->hdr.id = nc->ext_ncid;
/* Hang on to cmode, and note that we're in define mode. */
h5->cmode = mode | NC_INDEF;
/* The next_typeid needs to be set beyond the end of our atomic
* types. */
h5->next_typeid = NC_FIRSTUSERTYPEID;
/* Initialize lists for dimensions, types, and groups. */
h5->alldims = nclistnew();
h5->alltypes = nclistnew();
h5->allgroups = nclistnew();
/* There's always at least one open group - the root
* group. Allocate space for one group's worth of information. Set
* its grp id, name, and allocate associated empty lists. */
if ((retval = nc4_grp_list_add(h5, NULL, NC_GROUP_NAME, &h5->root_grp)))
return retval;
return NC_NOERR;
}
/**
* @internal Given an ncid, find the relevant group and return a
* pointer to it.
*
* @param ncid File and group ID.
* @param grp Pointer that gets pointer to group info struct. Ignored
* if NULL.
*
* @return ::NC_NOERR No error.
* @return ::NC_ENOTNC4 Not a netCDF-4 file.
* @author Ed Hartnett
*/
int
nc4_find_nc4_grp(int ncid, NC_GRP_INFO_T **grp)
{
return nc4_find_nc_grp_h5(ncid, NULL, grp, NULL);
}
/**
* @internal Given an ncid, find the relevant group and return a
* pointer to it, also set a pointer to the nc4_info struct of the
* related file.
*
* @param ncid File and group ID.
* @param grp Pointer that gets pointer to group info struct. Ignored
* if NULL.
* @param h5 Pointer that gets pointer to file info struct. Ignored if
* NULL.
*
* @return ::NC_NOERR No error.
* @return ::NC_EBADID Bad ncid.
* @author Ed Hartnett
*/
int
nc4_find_grp_h5(int ncid, NC_GRP_INFO_T **grp, NC_FILE_INFO_T **h5)
{
return nc4_find_nc_grp_h5(ncid, NULL, grp, h5);
}
/**
* @internal Find info for this file and group, and set pointers.
*
* @param ncid File and group ID.
* @param nc Pointer that gets a pointer to the file's NC
* struct. Ignored if NULL.
* @param grp Pointer that gets a pointer to the group
* struct. Ignored if NULL.
* @param h5 Pointer that gets HDF5 file struct. Ignored if NULL.
*
* @return ::NC_NOERR No error.
* @return ::NC_EBADID Bad ncid.
* @author Ed Hartnett, Dennis Heimbigner
*/
int
nc4_find_nc_grp_h5(int ncid, NC **nc, NC_GRP_INFO_T **grp, NC_FILE_INFO_T **h5)
{
NC_GRP_INFO_T *my_grp = NULL;
NC_FILE_INFO_T *my_h5 = NULL;
NC *my_nc;
int retval;
size_t index;
/* Look up file metadata. */
if ((retval = NC_check_id(ncid, &my_nc)))
return retval;
my_h5 = my_nc->dispatchdata;
assert(my_h5 && my_h5->root_grp);
/* If we can't find it, the grp id part of ncid is bad. */
index = (ncid & GRP_ID_MASK);
if (!(my_grp = nclistget(my_h5->allgroups,index)))
return NC_EBADID;
/* Return pointers to caller, if desired. */
if (nc)
*nc = my_nc;
if (h5)
*h5 = my_h5;
if (grp)
*grp = my_grp;
return NC_NOERR;
}
/**
* @internal Given an ncid and varid, get pointers to the group and var
* metadata.
*
* @param ncid File ID.
* @param varid Variable ID.
* @param h5 Pointer that gets pointer to the NC_FILE_INFO_T struct
* for this file. Ignored if NULL.
* @param grp Pointer that gets pointer to group info. Ignored if
* NULL.
* @param var Pointer that gets pointer to var info. Ignored if NULL.
*
* @return ::NC_NOERR No error.
* @author Ed Hartnett
*/
int
nc4_find_grp_h5_var(int ncid, int varid, NC_FILE_INFO_T **h5, NC_GRP_INFO_T **grp,
NC_VAR_INFO_T **var)
{
NC_FILE_INFO_T *my_h5;
NC_GRP_INFO_T *my_grp;
NC_VAR_INFO_T *my_var;
int retval;
/* Look up file and group metadata. */
if ((retval = nc4_find_grp_h5(ncid, &my_grp, &my_h5)))
return retval;
assert(my_grp && my_h5);
/* Find the var. */
if (!(my_var = (NC_VAR_INFO_T *)ncindexith(my_grp->vars, varid)))
return NC_ENOTVAR;
assert(my_var && my_var->hdr.id == varid);
/* Return pointers that caller wants. */
if (h5)
*h5 = my_h5;
if (grp)
*grp = my_grp;
if (var)
*var = my_var;
return NC_NOERR;
}
/**
* @internal Find a dim in the file.
*
* @param grp Pointer to group info struct.
* @param dimid Dimension ID to find.
* @param dim Pointer that gets pointer to dim info if found.
* @param dim_grp Pointer that gets pointer to group info of group
* that contains dimension. Ignored if NULL.
*
* @return ::NC_NOERR No error.
* @return ::NC_EBADDIM Dimension not found.
* @author Ed Hartnett, Dennis Heimbigner
*/
int
nc4_find_dim(NC_GRP_INFO_T *grp, int dimid, NC_DIM_INFO_T **dim,
NC_GRP_INFO_T **dim_grp)
{
assert(grp && grp->nc4_info && dim);
LOG((4, "%s: dimid %d", __func__, dimid));
/* Find the dim info. */
if (!((*dim) = nclistget(grp->nc4_info->alldims, dimid)))
return NC_EBADDIM;
/* Give the caller the group the dimension is in. */
if (dim_grp)
*dim_grp = (*dim)->container;
return NC_NOERR;
}
/**
* @internal Find a var (by name) in a grp.
*
* @param grp Pointer to group info.
* @param name Name of var to find.
* @param var Pointer that gets pointer to var info struct, if found.
*
* @return ::NC_NOERR No error.
* @author Ed Hartnett
*/
int
nc4_find_var(NC_GRP_INFO_T *grp, const char *name, NC_VAR_INFO_T **var)
{
assert(grp && var && name);
/* Find the var info. */
*var = (NC_VAR_INFO_T*)ncindexlookup(grp->vars,name);
return NC_NOERR;
}
/**
* @internal Locate netCDF type by name.
*
* @param start_grp Pointer to starting group info.
* @param name Name of type to find.
*
* @return Pointer to type info, or NULL if not found.
* @author Ed Hartnett, Dennis Heimbigner
*/
NC_TYPE_INFO_T *
nc4_rec_find_named_type(NC_GRP_INFO_T *start_grp, char *name)
{
NC_GRP_INFO_T *g;
NC_TYPE_INFO_T *type, *res;
int i;
assert(start_grp);
/* Does this group have the type we are searching for? */
type = (NC_TYPE_INFO_T*)ncindexlookup(start_grp->type,name);
if(type != NULL)
return type;
/* Search subgroups. */
for(i=0;i<ncindexsize(start_grp->children);i++) {
g = (NC_GRP_INFO_T*)ncindexith(start_grp->children,i);
if(g == NULL) continue;
if ((res = nc4_rec_find_named_type(g, name)))
return res;
}
/* Can't find it. Oh, woe is me! */
return NULL;
}
/**
* @internal Use a netCDF typeid to find a type in a type_list.
*
* @param h5 Pointer to HDF5 file info struct.
* @param typeid The netCDF type ID.
* @param type Pointer to pointer to the list of type info structs.
*
* @return ::NC_NOERR No error.
* @return ::NC_EINVAL Invalid input.
* @author Ed Hartnett
*/
int
nc4_find_type(const NC_FILE_INFO_T *h5, nc_type typeid, NC_TYPE_INFO_T **type)
{
/* Check inputs. */
assert(h5);
if (typeid < 0 || !type)
return NC_EINVAL;
*type = NULL;
/* Atomic types don't have associated NC_TYPE_INFO_T struct, just
* return NOERR. */
if (typeid <= NC_STRING)
return NC_NOERR;
/* Find the type. */
if (!(*type = nclistget(h5->alltypes,typeid)))
return NC_EBADTYPID;
return NC_NOERR;
}
/**
* @internal Given a group, find an att. If name is provided, use that,
* otherwise use the attnum.
*
* @param grp Pointer to group info struct.
* @param varid Variable ID.
* @param name Name to of attribute.
* @param attnum Number of attribute.
* @param att Pointer to pointer that gets attribute info struct.
*
* @return ::NC_NOERR No error.
* @return ::NC_ENOTVAR Variable not found.
* @return ::NC_ENOTATT Attribute not found.
* @author Ed Hartnett
*/
int
nc4_find_grp_att(NC_GRP_INFO_T *grp, int varid, const char *name, int attnum,
NC_ATT_INFO_T **att)
{
NC_VAR_INFO_T *var;
NC_ATT_INFO_T *my_att;
NCindex *attlist = NULL;
assert(grp && grp->hdr.name && att);
LOG((4, "%s: grp->name %s varid %d attnum %d", __func__, grp->hdr.name,
varid, attnum));
/* Get either the global or a variable attribute list. */
if (varid == NC_GLOBAL)
{
attlist = grp->att;
}
else
{
var = (NC_VAR_INFO_T*)ncindexith(grp->vars,varid);
if (!var) return NC_ENOTVAR;
attlist = var->att;
}
assert(attlist);
/* Now find the attribute by name or number. If a name is provided,
* ignore the attnum. */
if (name)
my_att = (NC_ATT_INFO_T *)ncindexlookup(attlist, name);
else
my_att = (NC_ATT_INFO_T *)ncindexith(attlist, attnum);
if (!my_att)
return NC_ENOTATT;
*att = my_att;
return NC_NOERR;
}
/**
* @internal Given an ncid, varid, and name or attnum, find and return
* pointer to NC_ATT_INFO_T metadata.
*
* @param ncid File and group ID.
* @param varid Variable ID.
* @param name Name to of attribute.
* @param attnum Number of attribute.
* @param att Pointer to pointer that gets attribute info struct.
*
* @return ::NC_NOERR No error.
* @return ::NC_ENOTVAR Variable not found.
* @return ::NC_ENOTATT Attribute not found.
* @author Ed Hartnett
*/
int
nc4_find_nc_att(int ncid, int varid, const char *name, int attnum,
NC_ATT_INFO_T **att)
{
NC_GRP_INFO_T *grp;
int retval;
LOG((4, "nc4_find_nc_att: ncid 0x%x varid %d name %s attnum %d",
ncid, varid, name, attnum));
/* Find info for this file and group, and set pointer to each. */
if ((retval = nc4_find_grp_h5(ncid, &grp, NULL)))
return retval;
assert(grp);
return nc4_find_grp_att(grp, varid, name, attnum, att);
}
/**
* @internal Add NC_OBJ to allXXX lists in a file
*
* @param file Pointer to the containing file
* @param obj Pointer to object to add.
*
* @author Dennis Heimbigner
*/
static void
obj_track(NC_FILE_INFO_T* file, NC_OBJ* obj)
{
NClist* list = NULL;
/* record the object in the file */
switch (obj->sort) {
case NCDIM: list = file->alldims; break;
case NCTYP: list = file->alltypes; break;
case NCGRP: list = file->allgroups; break;
default:
assert(NC_FALSE);
}
/* Insert at the appropriate point in the list */
nclistset(list,obj->id,obj);
}
/**
* @internal Create a new variable and insert into relevant
* lists. Dimensionality info need not be known.
*
* @param grp the containing group
* @param name the name for the new variable
* @param var Pointer in which to return a pointer to the new var.
*
* @return ::NC_NOERR No error.
* @return ::NC_ENOMEM Out of memory.
* @author Ed Hartnett
*/
int
nc4_var_list_add2(NC_GRP_INFO_T *grp, const char *name, NC_VAR_INFO_T **var)
{
NC_VAR_INFO_T *new_var = NULL;
NCglobalstate* gs = NC_getglobalstate();
/* Allocate storage for new variable. */
if (!(new_var = calloc(1, sizeof(NC_VAR_INFO_T))))
return NC_ENOMEM;
new_var->hdr.sort = NCVAR;
new_var->container = grp;
/* These are the HDF5-1.8.4 defaults. */
new_var->chunkcache.size = gs->chunkcache.size;
new_var->chunkcache.nelems = gs->chunkcache.nelems;
new_var->chunkcache.preemption = gs->chunkcache.preemption;
/* Now fill in the values in the var info structure. */
new_var->hdr.id = ncindexsize(grp->vars);
if (!(new_var->hdr.name = strdup(name))) {
if(new_var)
free(new_var);
return NC_ENOMEM;
}
/* Create an indexed list for the attributes. */
new_var->att = ncindexnew(0);
/* Officially track it */
ncindexadd(grp->vars, (NC_OBJ *)new_var);
/* Set the var pointer, if one was given */
if (var)
*var = new_var;
return NC_NOERR;
}
/**
* @internal Set the number of dims in an NC_VAR_INFO_T struct.
*
* @param var Pointer to the var.
* @param ndims Number of dimensions for this var.
*
* @return ::NC_NOERR No error.
* @return ::NC_ENOMEM Out of memory.
* @author Ed Hartnett
*/
int
nc4_var_set_ndims(NC_VAR_INFO_T *var, int ndims)
{
assert(var);
/* Remember the number of dimensions. */
var->ndims = ndims;
/* Allocate space for dimension information. */
if (ndims)
{
if (!(var->dim = calloc(ndims, sizeof(NC_DIM_INFO_T *))))
return NC_ENOMEM;
if (!(var->dimids = calloc(ndims, sizeof(int))))
return NC_ENOMEM;
/* Initialize dimids to illegal values (-1). See the comment
in nc4_rec_match_dimscales(). */
memset(var->dimids, -1, ndims * sizeof(int));
}
return NC_NOERR;
}
/**
* @internal Create a new variable and insert int relevant list.
*
* @param grp the containing group
* @param name the name for the new variable
* @param ndims the rank of the new variable
* @param var Pointer in which to return a pointer to the new var.
*
* @return ::NC_NOERR No error.
* @return ::NC_ENOMEM Out of memory.
* @author Ed Hartnett
*/
int
nc4_var_list_add(NC_GRP_INFO_T* grp, const char* name, int ndims,
NC_VAR_INFO_T **var)
{
int retval;
if ((retval = nc4_var_list_add2(grp, name, var)))
return retval;
if ((retval = nc4_var_set_ndims(*var, ndims)))
return retval;
return NC_NOERR;
}
/**
* @internal Add a dimension to the dimension list for a group.
*
* @param grp container for the dim
* @param name for the dim
* @param len for the dim
* @param assignedid override dimid if >= 0
* @param dim Pointer to pointer that gets the new dim info struct.
*
* @return ::NC_NOERR No error.
* @return ::NC_ENOMEM Out of memory.
* @author Ed Hartnett
*/
int
nc4_dim_list_add(NC_GRP_INFO_T *grp, const char *name, size_t len,
int assignedid, NC_DIM_INFO_T **dim)
{
NC_DIM_INFO_T *new_dim = NULL;
assert(grp && name);
/* Allocate memory for dim metadata. */
if (!(new_dim = calloc(1, sizeof(NC_DIM_INFO_T))))
return NC_ENOMEM;
new_dim->hdr.sort = NCDIM;
/* Assign the dimension ID. */
if (assignedid >= 0)
new_dim->hdr.id = assignedid;
else
new_dim->hdr.id = grp->nc4_info->next_dimid++;
/* Remember the name and create a hash. */
if (!(new_dim->hdr.name = strdup(name))) {
if(new_dim)
free(new_dim);
return NC_ENOMEM;
}
/* Is dimension unlimited? */
new_dim->len = len;
if (len == NC_UNLIMITED)
new_dim->unlimited = NC_TRUE;
/* Remember the containing group. */
new_dim->container = grp;
/* Add object to dimension list for this group. */
ncindexadd(grp->dim, (NC_OBJ *)new_dim);
obj_track(grp->nc4_info, (NC_OBJ *)new_dim);
/* Set the dim pointer, if one was given */
if (dim)
*dim = new_dim;
return NC_NOERR;
}
/**
* @internal Add to an attribute list.
*
* @param list NCindex of att info structs.
* @param name name of the new attribute
* @param att Pointer to pointer that gets the new att info
* struct. Ignored if NULL.
*
* @return ::NC_NOERR No error.
* @return ::NC_ENOMEM Out of memory.
* @author Ed Hartnett
*/
int
nc4_att_list_add(NCindex *list, const char *name, NC_ATT_INFO_T **att)
{
NC_ATT_INFO_T *new_att = NULL;
LOG((3, "%s: name %s ", __func__, name));
if (!(new_att = calloc(1, sizeof(NC_ATT_INFO_T))))
return NC_ENOMEM;
new_att->hdr.sort = NCATT;
/* Fill in the information we know. */
new_att->hdr.id = ncindexsize(list);
if (!(new_att->hdr.name = strdup(name))) {
if(new_att)
free(new_att);
return NC_ENOMEM;
}
/* Add object to list as specified by its number */
ncindexadd(list, (NC_OBJ *)new_att);
/* Set the attribute pointer, if one was given */
if (att)
*att = new_att;
return NC_NOERR;
}
/**
* @internal Add a group to a group list.
*
* @param h5 Pointer to the file info.
* @param parent Pointer to the parent group. Will be NULL when adding
* the root group.
* @param name Name of the group.
* @param grp Pointer to pointer that gets new group info
* struct. Ignored if NULL.
*
* @return ::NC_NOERR No error.
* @return ::NC_ENOMEM Out of memory.
* @author Ed Hartnett, Dennis Heimbigner
*/
int
nc4_grp_list_add(NC_FILE_INFO_T *h5, NC_GRP_INFO_T *parent, char *name,
NC_GRP_INFO_T **grp)
{
NC_GRP_INFO_T *new_grp;
/* Check inputs. */
assert(h5 && name);
LOG((3, "%s: name %s ", __func__, name));
/* Get the memory to store this groups info. */
if (!(new_grp = calloc(1, sizeof(NC_GRP_INFO_T))))
return NC_ENOMEM;
/* Fill in this group's information. */
new_grp->hdr.sort = NCGRP;
new_grp->nc4_info = h5;
new_grp->parent = parent;
/* Assign the group ID. The root group will get id 0. */
new_grp->hdr.id = h5->next_nc_grpid++;
assert(parent || !new_grp->hdr.id);
/* Handle the group name. */
if (!(new_grp->hdr.name = strdup(name)))
{
free(new_grp);
return NC_ENOMEM;
}
/* Set up new indexed lists for stuff this group can contain. */
new_grp->children = ncindexnew(0);
new_grp->dim = ncindexnew(0);
new_grp->att = ncindexnew(0);
new_grp->type = ncindexnew(0);
new_grp->vars = ncindexnew(0);
/* Add object to lists */
if (parent)
ncindexadd(parent->children, (NC_OBJ *)new_grp);
obj_track(h5, (NC_OBJ *)new_grp);
/* Set the group pointer, if one was given */
if (grp)
*grp = new_grp;
return NC_NOERR;
}
/**
* @internal Names for groups, variables, and types must not be the
* same. This function checks that a proposed name is not already in
* use. Normalzation of UTF8 strings should happen before this
* function is called.
*
* @param grp Pointer to group info struct.