NetCDF 4.10.0
Loading...
Searching...
No Matches
nc4internal.c
Go to the documentation of this file.
1/* Copyright 2003-2018, University Corporation for Atmospheric
2 * Research. See the COPYRIGHT file for copying and redistribution
3 * conditions.
4 */
18#include "config.h"
19#include <stdarg.h>
20#include <stddef.h>
21#include "netcdf.h"
22#include "netcdf_filter.h"
23#include "netcdf_meta.h"
24#include "nc4internal.h"
25#include "nc.h" /* from libsrc */
26#include "ncdispatch.h" /* from libdispatch */
27#include "ncutf8.h"
28#include "ncrc.h"
29#include "nc4internal.h"
30
43static NC_reservedatt NC_reserved[] = {
44 {NC_ATT_CLASS, READONLYFLAG|HIDDENATTRFLAG}, /*CLASS*/
45 {NC_ATT_DIMENSION_LIST, READONLYFLAG|HIDDENATTRFLAG}, /*DIMENSION_LIST*/
46 {NC_ATT_NAME, READONLYFLAG|HIDDENATTRFLAG}, /*NAME*/
47 {NC_ATT_REFERENCE_LIST, READONLYFLAG|HIDDENATTRFLAG}, /*REFERENCE_LIST*/
48 {NC_XARRAY_DIMS, READONLYFLAG|HIDDENATTRFLAG}, /*_ARRAY_DIMENSIONS*/
49 {NC_ATT_CODECS, VARFLAG|READONLYFLAG|NAMEONLYFLAG}, /*_Codecs*/
50 {NC_ATT_FORMAT, READONLYFLAG}, /*_Format*/
51 {ISNETCDF4ATT, READONLYFLAG|NAMEONLYFLAG|VIRTUALFLAG}, /*_IsNetcdf4*/
52 {NCPROPS,READONLYFLAG|NAMEONLYFLAG|HIDDENATTRFLAG}, /*_NCProperties*/
53 {NC_ATT_COORDINATES, READONLYFLAG|HIDDENATTRFLAG}, /*_Netcdf4Coordinates*/
54 {NC_ATT_DIMID_NAME, READONLYFLAG|HIDDENATTRFLAG}, /*_Netcdf4Dimid*/
55 {SUPERBLOCKATT, READONLYFLAG|NAMEONLYFLAG|VIRTUALFLAG}, /*_SuperblockVersion*/
56 {NC_ATT_NC3_STRICT_NAME, READONLYFLAG}, /*_nc3_strict*/
57 {NC_NCZARR_ATTR, READONLYFLAG|HIDDENATTRFLAG}, /*_nczarr_attr */
58 {NC_NCZARR_GROUP, READONLYFLAG|HIDDENATTRFLAG}, /*_nczarr_group */
59 {NC_NCZARR_ARRAY, READONLYFLAG|HIDDENATTRFLAG}, /*_nczarr_array */
60 {NC_NCZARR_SUPERBLOCK, READONLYFLAG|HIDDENATTRFLAG}, /*_nczarr_superblock */
61};
62#define NRESERVED (sizeof(NC_reserved) / sizeof(NC_reservedatt)) /*|NC_reservedatt*/
63
64/*Forward */
65static int NC4_move_in_NCList(NC* nc, int new_id);
66static int bincmp(const void* arg1, const void* arg2);
67static int sortcmp(const void* arg1, const void* arg2);
68
69#if LOGGING
70/* This is the severity level of messages which will be logged. Use
71 severity 0 for errors, 1 for important log messages, 2 for less
72 important, etc. */
73int nc_log_level = NC_TURN_OFF_LOGGING;
74
75
76#if NC_HAS_PARALLEL4
77/* File pointer for the parallel I/O log file. */
78FILE *LOG_FILE = NULL;
79#endif /* NC_HAS_PARALLEL4 */
80
81/* This function prints out a message, if the severity of
82 * the message is lower than the global nc_log_level. To use it, do
83 * something like this:
84 *
85 * nc_log(0, "this computer will explode in %d seconds", i);
86 *
87 * After the first arg (the severity), use the rest like a normal
88 * printf statement. Output will appear on stderr for sequential
89 * builds, and in a file nc4_log_R.log for each process for a parallel
90 * build, where R is the rank of the process.
91 *
92 * Ed Hartnett
93 */
94void
95nc_log(int severity, const char *fmt, ...)
96{
97 va_list argp;
98 int t;
99 FILE *f = stderr;
100
101 const char* envv = NULL;
102 envv = getenv("NC4LOGGING");
103 if(envv != NULL) {
104 nc_log_level = atoi(envv);
105 }
106
107 /* If the severity is greater than the log level, we don't print
108 * this message. */
109 if (severity > nc_log_level)
110 return;
111
112#if NC_HAS_PARALLEL4
113 /* For parallel I/O build, if MPI has been initialized, instead of
114 * printing logging output to stderr, it goes to a file for each
115 * process. */
116 {
117 int mpi_initialized;
118 int mpierr;
119
120 /* Check to see if MPI has been initialized. */
121 if ((mpierr = MPI_Initialized(&mpi_initialized)))
122 return;
123
124 /* If MPI has been initialized use a log file. */
125 assert(LOG_FILE);
126 if (mpi_initialized)
127 f = LOG_FILE;
128 }
129#endif /* NC_HAS_PARALLEL4 */
130
131 /* If the severity is zero, this is an error. Otherwise insert that
132 many tabs before the message. */
133 if (!severity)
134 fprintf(f, "ERROR: ");
135 for (t = 0; t < severity; t++)
136 fprintf(f, "\t");
137
138 /* Print out the variable list of args with vprintf. */
139 va_start(argp, fmt);
140 vfprintf(f, fmt, argp);
141 va_end(argp);
142
143 /* Put on a final linefeed. */
144 fprintf(f, "\n");
145 fflush(f);
146}
147#endif /* LOGGING */
148
161int
162nc4_check_name(const char *name, char *norm_name)
163{
164 char *temp;
165 int retval;
166
167 assert(norm_name);
168
169 /* Check for NULL. */
170 if (!name)
171 return NC_EINVAL;
172
173 /* Make sure this is a valid netcdf name. This should be done
174 * before the name is normalized, because it gives better error
175 * codes for bad utf8 strings. */
176 if ((retval = NC_check_name(name)))
177 return retval;
178
179 /* Normalize the name. */
180 if ((retval = nc_utf8_normalize((const unsigned char *)name,
181 (unsigned char **)&temp)))
182 return retval;
183
184 /* Check length of normalized name. */
185 if (strlen(temp) > NC_MAX_NAME)
186 {
187 free(temp);
188 return NC_EMAXNAME;
189 }
190
191 /* Copy the normalized name. */
192 strcpy(norm_name, temp);
193 free(temp);
194
195 return NC_NOERR;
196}
197
218int
219nc4_file_list_add(int ncid, const char *path, int mode, void **dispatchdata)
220{
221 NC *nc;
222 int ret;
223
224 /* Find NC pointer for this file. */
225 if ((ret = NC_check_id(ncid, &nc)))
226 return ret;
227
228 /* Add necessary structs to hold netcdf-4 file data. This is where
229 * the NC_FILE_INFO_T struct is allocated for the file. */
230 if ((ret = nc4_nc4f_list_add(nc, path, mode)))
231 return ret;
232
233 /* If the user wants a pointer to the NC_FILE_INFO_T, then provide
234 * it. */
235 if (dispatchdata)
236 *dispatchdata = nc->dispatchdata;
237
238 return NC_NOERR;
239}
240
254int
255nc4_file_change_ncid(int ncid, unsigned short new_ncid_index)
256{
257 NC *nc;
258 int ret;
259
260 LOG((2, "%s: ncid %d new_ncid_index %d", __func__, ncid, new_ncid_index));
261
262 /* Find NC pointer for this file. */
263 if ((ret = NC_check_id(ncid, &nc)))
264 return ret;
265
266 /* Move it in the list. It will fail if list spot is already
267 * occupied. */
268 LOG((3, "moving nc->ext_ncid %d nc->ext_ncid >> ID_SHIFT %d",
269 nc->ext_ncid, nc->ext_ncid >> ID_SHIFT));
270 if (NC4_move_in_NCList(nc, new_ncid_index))
271 return NC_EIO;
272 LOG((3, "moved to new_ncid_index %d new nc->ext_ncid %d", new_ncid_index,
273 nc->ext_ncid));
274
275 return NC_NOERR;
276}
277
297int
298nc4_file_list_get(int ncid, char **path, int *mode, void **dispatchdata)
299{
300 NC *nc;
301 int ret;
302
303 /* Find NC pointer for this file. */
304 if ((ret = NC_check_id(ncid, &nc)))
305 return ret;
306
307 /* If the user wants path, give it. */
308 if (path)
309 strncpy(*path, nc->path, NC_MAX_NAME);
310
311 /* If the user wants mode, give it. */
312 if (mode)
313 *mode = nc->mode;
314
315 /* If the user wants dispatchdata, give it. */
316 if (dispatchdata)
317 *dispatchdata = nc->dispatchdata;
318
319 return NC_NOERR;
320}
321
336int
337nc4_nc4f_list_add(NC *nc, const char *path, int mode)
338{
339 NC_FILE_INFO_T *h5;
340 int retval;
341
342 assert(nc && !NC4_DATA(nc) && path);
343
344 /* We need to malloc and initialize the substructure
345 NC_FILE_INFO_T. */
346 if (!(h5 = calloc(1, sizeof(NC_FILE_INFO_T))))
347 return NC_ENOMEM;
348 nc->dispatchdata = h5;
349 h5->controller = nc;
350
351 h5->hdr.sort = NCFIL;
352 h5->hdr.name = strdup(path);
353 h5->hdr.id = nc->ext_ncid;
354
355 /* Hang on to cmode, and note that we're in define mode. */
356 h5->cmode = mode | NC_INDEF;
357
358 /* The next_typeid needs to be set beyond the end of our atomic
359 * types. */
360 h5->next_typeid = NC_FIRSTUSERTYPEID;
361
362 /* Initialize lists for dimensions, types, and groups. */
363 h5->alldims = nclistnew();
364 h5->alltypes = nclistnew();
365 h5->allgroups = nclistnew();
366
367 /* There's always at least one open group - the root
368 * group. Allocate space for one group's worth of information. Set
369 * its grp id, name, and allocate associated empty lists. */
370 if ((retval = nc4_grp_list_add(h5, NULL, NC_GROUP_NAME, &h5->root_grp)))
371 return retval;
372
373 return NC_NOERR;
374}
375
388int
389nc4_find_nc4_grp(int ncid, NC_GRP_INFO_T **grp)
390{
391 return nc4_find_nc_grp_h5(ncid, NULL, grp, NULL);
392}
393
409int
410nc4_find_grp_h5(int ncid, NC_GRP_INFO_T **grp, NC_FILE_INFO_T **h5)
411{
412 return nc4_find_nc_grp_h5(ncid, NULL, grp, h5);
413}
414
429int
430nc4_find_nc_grp_h5(int ncid, NC **nc, NC_GRP_INFO_T **grp, NC_FILE_INFO_T **h5)
431{
432 NC_GRP_INFO_T *my_grp = NULL;
433 NC_FILE_INFO_T *my_h5 = NULL;
434 NC *my_nc;
435 int retval;
436 size_t index;
437
438 /* Look up file metadata. */
439 if ((retval = NC_check_id(ncid, &my_nc)))
440 return retval;
441 my_h5 = my_nc->dispatchdata;
442 assert(my_h5 && my_h5->root_grp);
443
444 /* If we can't find it, the grp id part of ncid is bad. */
445 index = (ncid & GRP_ID_MASK);
446 if (!(my_grp = nclistget(my_h5->allgroups,index)))
447 return NC_EBADID;
448
449 /* Return pointers to caller, if desired. */
450 if (nc)
451 *nc = my_nc;
452 if (h5)
453 *h5 = my_h5;
454 if (grp)
455 *grp = my_grp;
456
457 return NC_NOERR;
458}
459
475int
476nc4_find_grp_h5_var(int ncid, int varid, NC_FILE_INFO_T **h5, NC_GRP_INFO_T **grp,
477 NC_VAR_INFO_T **var)
478{
479 NC_FILE_INFO_T *my_h5;
480 NC_GRP_INFO_T *my_grp;
481 NC_VAR_INFO_T *my_var;
482 int retval;
483
484 /* Look up file and group metadata. */
485 if ((retval = nc4_find_grp_h5(ncid, &my_grp, &my_h5)))
486 return retval;
487 assert(my_grp && my_h5);
488
489 /* Find the var. */
490 if (!(my_var = (NC_VAR_INFO_T *)ncindexith(my_grp->vars, (size_t)varid)))
491 return NC_ENOTVAR;
492 assert(my_var && my_var->hdr.id == varid);
493
494 /* Return pointers that caller wants. */
495 if (h5)
496 *h5 = my_h5;
497 if (grp)
498 *grp = my_grp;
499 if (var)
500 *var = my_var;
501
502 return NC_NOERR;
503}
504
518int
519nc4_find_dim(NC_GRP_INFO_T *grp, int dimid, NC_DIM_INFO_T **dim,
520 NC_GRP_INFO_T **dim_grp)
521{
522 assert(grp && grp->nc4_info && dim);
523 LOG((4, "%s: dimid %d", __func__, dimid));
524
525 /* Find the dim info. */
526 if (!((*dim) = nclistget(grp->nc4_info->alldims, (size_t)dimid)))
527 return NC_EBADDIM;
528
529 /* Give the caller the group the dimension is in. */
530 if (dim_grp)
531 *dim_grp = (*dim)->container;
532
533 return NC_NOERR;
534}
535
546int
547nc4_find_var(NC_GRP_INFO_T *grp, const char *name, NC_VAR_INFO_T **var)
548{
549 assert(grp && var && name);
550
551 /* Find the var info. */
552 *var = (NC_VAR_INFO_T*)ncindexlookup(grp->vars,name);
553 return NC_NOERR;
554}
555
565NC_TYPE_INFO_T *
566nc4_rec_find_named_type(NC_GRP_INFO_T *start_grp, char *name)
567{
568 NC_GRP_INFO_T *g;
569 NC_TYPE_INFO_T *type, *res;
570
571 assert(start_grp);
572
573 /* Does this group have the type we are searching for? */
574 type = (NC_TYPE_INFO_T*)ncindexlookup(start_grp->type,name);
575 if(type != NULL)
576 return type;
577
578 /* Search subgroups. */
579 for(size_t i=0;i<ncindexsize(start_grp->children);i++) {
580 g = (NC_GRP_INFO_T*)ncindexith(start_grp->children,i);
581 if(g == NULL) continue;
582 if ((res = nc4_rec_find_named_type(g, name)))
583 return res;
584 }
585 /* Can't find it. Oh, woe is me! */
586 return NULL;
587}
588
600int
601nc4_find_type(const NC_FILE_INFO_T *h5, nc_type typeid, NC_TYPE_INFO_T **type)
602{
603 /* Check inputs. */
604 assert(h5);
605 if (typeid < 0 || !type)
606 return NC_EINVAL;
607 *type = NULL;
608
609 /* Atomic types don't have associated NC_TYPE_INFO_T struct, just
610 * return NOERR. */
611 if (typeid <= NC_STRING)
612 return NC_NOERR;
613
614 /* Find the type. */
615 if (!(*type = nclistget(h5->alltypes, (size_t)typeid)))
616 return NC_EBADTYPID;
617
618 return NC_NOERR;
619}
620
636int
637nc4_find_grp_att(NC_GRP_INFO_T *grp, int varid, const char *name, int attnum,
638 NC_ATT_INFO_T **att)
639{
640 NC_VAR_INFO_T *var;
641 NC_ATT_INFO_T *my_att;
642 NCindex *attlist = NULL;
643
644 assert(grp && grp->hdr.name && att);
645
646 LOG((4, "%s: grp->name %s varid %d attnum %d", __func__, grp->hdr.name,
647 varid, attnum));
648
649 /* Get either the global or a variable attribute list. */
650 if (varid == NC_GLOBAL)
651 {
652 attlist = grp->att;
653 }
654 else
655 {
656 var = (NC_VAR_INFO_T*)ncindexith(grp->vars,(size_t)varid);
657 if (!var) return NC_ENOTVAR;
658
659 attlist = var->att;
660 }
661 assert(attlist);
662
663 /* Now find the attribute by name or number. If a name is provided,
664 * ignore the attnum. */
665 if (name)
666 my_att = (NC_ATT_INFO_T *)ncindexlookup(attlist, name);
667 else
668 my_att = (NC_ATT_INFO_T *)ncindexith(attlist, (size_t)attnum);
669
670 if (!my_att)
671 return NC_ENOTATT;
672
673 *att = my_att;
674 return NC_NOERR;
675}
676
693int
694nc4_find_nc_att(int ncid, int varid, const char *name, int attnum,
695 NC_ATT_INFO_T **att)
696{
697 NC_GRP_INFO_T *grp;
698 int retval;
699
700 LOG((4, "nc4_find_nc_att: ncid 0x%x varid %d name %s attnum %d",
701 ncid, varid, name, attnum));
702
703 /* Find info for this file and group, and set pointer to each. */
704 if ((retval = nc4_find_grp_h5(ncid, &grp, NULL)))
705 return retval;
706 assert(grp);
707
708 return nc4_find_grp_att(grp, varid, name, attnum, att);
709}
710
719static void
720obj_track(NC_FILE_INFO_T* file, NC_OBJ* obj)
721{
722 NClist* list = NULL;
723 /* record the object in the file */
724 switch (obj->sort) {
725 case NCDIM: list = file->alldims; break;
726 case NCTYP: list = file->alltypes; break;
727 case NCGRP: list = file->allgroups; break;
728 default:
729 assert(NC_FALSE);
730 }
731 /* Insert at the appropriate point in the list */
732 nclistset(list,(size_t)obj->id,obj);
733}
734
747int
748nc4_var_list_add2(NC_GRP_INFO_T *grp, const char *name, NC_VAR_INFO_T **var)
749{
750 NC_VAR_INFO_T *new_var = NULL;
751 NCglobalstate* gs = NC_getglobalstate();
752
753 /* Allocate storage for new variable. */
754 if (!(new_var = calloc(1, sizeof(NC_VAR_INFO_T))))
755 return NC_ENOMEM;
756 new_var->hdr.sort = NCVAR;
757 new_var->container = grp;
758
759 memset(&new_var->chunkcache,0,sizeof(struct ChunkCache));
760
761 /* These are the HDF5-1.8.4 defaults. */
762 new_var->chunkcache.size = gs->chunkcache.size;
763 new_var->chunkcache.nelems = gs->chunkcache.nelems;
764 new_var->chunkcache.preemption = gs->chunkcache.preemption;
765
766 /* Now fill in the values in the var info structure. */
767 new_var->hdr.id = (int)ncindexsize(grp->vars);
768 if (!(new_var->hdr.name = strdup(name))) {
769 if(new_var)
770 free(new_var);
771 return NC_ENOMEM;
772 }
773
774 /* Create an indexed list for the attributes. */
775 new_var->att = ncindexnew(0);
776
777 /* Officially track it */
778 ncindexadd(grp->vars, (NC_OBJ *)new_var);
779
780 /* Set the var pointer, if one was given */
781 if (var)
782 *var = new_var;
783
784 return NC_NOERR;
785}
786
797int
798nc4_var_set_ndims(NC_VAR_INFO_T *var, int ndims)
799{
800 assert(var);
801
802 /* Remember the number of dimensions. */
803 var->ndims = (size_t)ndims;
804
805 /* Allocate space for dimension information. */
806 if (ndims)
807 {
808 if (!(var->dim = calloc((size_t)ndims, sizeof(NC_DIM_INFO_T *))))
809 return NC_ENOMEM;
810 if (!(var->dimids = calloc((size_t)ndims, sizeof(int))))
811 return NC_ENOMEM;
812
813 /* Initialize dimids to illegal values (-1). See the comment
814 in nc4_rec_match_dimscales(). */
815 memset(var->dimids, -1, (size_t)ndims * sizeof(int));
816 }
817
818 return NC_NOERR;
819}
820
833int
834nc4_var_list_add(NC_GRP_INFO_T* grp, const char* name, int ndims,
835 NC_VAR_INFO_T **var)
836{
837 int retval;
838
839 if ((retval = nc4_var_list_add2(grp, name, var)))
840 return retval;
841 if ((retval = nc4_var_set_ndims(*var, ndims)))
842 return retval;
843
844 return NC_NOERR;
845}
846
860int
861nc4_dim_list_add(NC_GRP_INFO_T *grp, const char *name, size_t len,
862 int assignedid, NC_DIM_INFO_T **dim)
863{
864 NC_DIM_INFO_T *new_dim = NULL;
865
866 assert(grp && name);
867
868 /* Allocate memory for dim metadata. */
869 if (!(new_dim = calloc(1, sizeof(NC_DIM_INFO_T))))
870 return NC_ENOMEM;
871
872 new_dim->hdr.sort = NCDIM;
873
874 /* Assign the dimension ID. */
875 if (assignedid >= 0)
876 new_dim->hdr.id = assignedid;
877 else
878 new_dim->hdr.id = grp->nc4_info->next_dimid++;
879
880 /* Remember the name and create a hash. */
881 if (!(new_dim->hdr.name = strdup(name))) {
882 if(new_dim)
883 free(new_dim);
884
885 return NC_ENOMEM;
886 }
887
888 /* Is dimension unlimited? */
889 new_dim->len = len;
890 if (len == NC_UNLIMITED)
891 new_dim->unlimited = NC_TRUE;
892
893 /* Remember the containing group. */
894 new_dim->container = grp;
895
896 /* Add object to dimension list for this group. */
897 ncindexadd(grp->dim, (NC_OBJ *)new_dim);
898 obj_track(grp->nc4_info, (NC_OBJ *)new_dim);
899
900 /* Set the dim pointer, if one was given */
901 if (dim)
902 *dim = new_dim;
903
904 return NC_NOERR;
905}
906
919int
920nc4_att_list_add(NCindex *list, const char *name, NC_ATT_INFO_T **att)
921{
922 NC_ATT_INFO_T *new_att = NULL;
923
924 LOG((3, "%s: name %s ", __func__, name));
925
926 if (!(new_att = calloc(1, sizeof(NC_ATT_INFO_T))))
927 return NC_ENOMEM;
928 new_att->hdr.sort = NCATT;
929
930 /* Fill in the information we know. */
931 new_att->hdr.id = (int)ncindexsize(list);
932 if (!(new_att->hdr.name = strdup(name))) {
933 if(new_att)
934 free(new_att);
935 return NC_ENOMEM;
936 }
937
938 /* Add object to list as specified by its number */
939 ncindexadd(list, (NC_OBJ *)new_att);
940
941 /* Set the attribute pointer, if one was given */
942 if (att)
943 *att = new_att;
944
945 return NC_NOERR;
946}
947
962int
963nc4_grp_list_add(NC_FILE_INFO_T *h5, NC_GRP_INFO_T *parent, char *name,
964 NC_GRP_INFO_T **grp)
965{
966 NC_GRP_INFO_T *new_grp;
967
968 /* Check inputs. */
969 assert(h5 && name);
970 LOG((3, "%s: name %s ", __func__, name));
971
972 /* Get the memory to store this groups info. */
973 if (!(new_grp = calloc(1, sizeof(NC_GRP_INFO_T))))
974 return NC_ENOMEM;
975
976 /* Fill in this group's information. */
977 new_grp->hdr.sort = NCGRP;
978 new_grp->nc4_info = h5;
979 new_grp->parent = parent;
980
981 /* Assign the group ID. The root group will get id 0. */
982 new_grp->hdr.id = h5->next_nc_grpid++;
983 assert(parent || !new_grp->hdr.id);
984
985 /* Handle the group name. */
986 if (!(new_grp->hdr.name = strdup(name)))
987 {
988 free(new_grp);
989 return NC_ENOMEM;
990 }
991
992 /* Set up new indexed lists for stuff this group can contain. */
993 new_grp->children = ncindexnew(0);
994 new_grp->dim = ncindexnew(0);
995 new_grp->att = ncindexnew(0);
996 new_grp->type = ncindexnew(0);
997 new_grp->vars = ncindexnew(0);
998
999 /* Add object to lists */
1000 if (parent)
1001 ncindexadd(parent->children, (NC_OBJ *)new_grp);
1002 obj_track(h5, (NC_OBJ *)new_grp);
1003
1004 /* Set the group pointer, if one was given */
1005 if (grp)
1006 *grp = new_grp;
1007
1008 return NC_NOERR;
1009}
1010
1024int
1025nc4_check_dup_name(NC_GRP_INFO_T *grp, char *name)
1026{
1027 NC_TYPE_INFO_T *type;
1028 NC_GRP_INFO_T *g;
1029 NC_VAR_INFO_T *var;
1030
1031 /* Any types of this name? */
1032 type = (NC_TYPE_INFO_T*)ncindexlookup(grp->type,name);
1033 if(type != NULL)
1034 return NC_ENAMEINUSE;
1035
1036 /* Any child groups of this name? */
1037 g = (NC_GRP_INFO_T*)ncindexlookup(grp->children,name);
1038 if(g != NULL)
1039 return NC_ENAMEINUSE;
1040
1041 /* Any variables of this name? */
1042 var = (NC_VAR_INFO_T*)ncindexlookup(grp->vars,name);
1043 if(var != NULL)
1044 return NC_ENAMEINUSE;
1045
1046 return NC_NOERR;
1047}
1048
1062int
1063nc4_type_new(size_t size, const char *name, int assignedid,
1064 NC_TYPE_INFO_T **type)
1065{
1066 NC_TYPE_INFO_T *new_type;
1067
1068 LOG((4, "%s: size %d name %s assignedid %d", __func__, size, name, assignedid));
1069
1070 /* Check inputs. */
1071 assert(type);
1072
1073 /* Allocate memory for the type */
1074 if (!(new_type = calloc(1, sizeof(NC_TYPE_INFO_T))))
1075 return NC_ENOMEM;
1076 new_type->hdr.sort = NCTYP;
1077 new_type->hdr.id = assignedid;
1078
1079 /* Remember info about this type. */
1080 new_type->size = size;
1081 if (!(new_type->hdr.name = strdup(name))) {
1082 free(new_type);
1083 return NC_ENOMEM;
1084 }
1085
1086 /* Return a pointer to the new type. */
1087 *type = new_type;
1088
1089 return NC_NOERR;
1090}
1091
1105int
1106nc4_type_list_add(NC_GRP_INFO_T *grp, size_t size, const char *name,
1107 NC_TYPE_INFO_T **type)
1108{
1109 NC_TYPE_INFO_T *new_type;
1110 int retval;
1111
1112 /* Check inputs. */
1113 assert(grp && name && type);
1114 LOG((4, "%s: size %d name %s", __func__, size, name));
1115
1116 /* Create the new TYPE_INFO struct. */
1117 if ((retval = nc4_type_new(size, name, grp->nc4_info->next_typeid,
1118 &new_type)))
1119 return retval;
1120 grp->nc4_info->next_typeid++;
1121
1122 /* Increment the ref. count on the type */
1123 new_type->rc++;
1124
1125 /* Add object to lists */
1126 ncindexadd(grp->type, (NC_OBJ *)new_type);
1127 obj_track(grp->nc4_info,(NC_OBJ*)new_type);
1128
1129 /* back link */
1130 new_type->container = grp;
1131
1132 /* Return a pointer to the new type. */
1133 *type = new_type;
1134
1135 return NC_NOERR;
1136}
1137
1151int
1152nc4_field_list_add(NC_TYPE_INFO_T *parent, const char *name,
1153 size_t offset, nc_type xtype, int ndims,
1154 const int *dim_sizesp)
1155{
1156 NC_FIELD_INFO_T *field;
1157
1158 /* Name has already been checked and UTF8 normalized. */
1159 if (!name)
1160 return NC_EINVAL;
1161
1162 /* Allocate storage for this field information. */
1163 if (!(field = calloc(1, sizeof(NC_FIELD_INFO_T))))
1164 return NC_ENOMEM;
1165 field->hdr.sort = NCFLD;
1166
1167 /* Store the information about this field. */
1168 if (!(field->hdr.name = strdup(name)))
1169 {
1170 free(field);
1171 return NC_ENOMEM;
1172 }
1173 field->nc_typeid = xtype;
1174 field->offset = offset;
1175 field->ndims = ndims;
1176 if (ndims)
1177 {
1178 int i;
1179 if (!(field->dim_size = malloc((size_t)ndims * sizeof(int))))
1180 {
1181 free(field->hdr.name);
1182 free(field);
1183 return NC_ENOMEM;
1184 }
1185 for (i = 0; i < ndims; i++)
1186 field->dim_size[i] = dim_sizesp[i];
1187 }
1188
1189 /* Add object to lists */
1190 field->hdr.id = (int)nclistlength(parent->u.c.field);
1191 nclistpush(parent->u.c.field,field);
1192
1193 return NC_NOERR;
1194}
1195
1208int
1209nc4_enum_member_add(NC_TYPE_INFO_T *parent, size_t size,
1210 const char *name, const void *value)
1211{
1212 NC_ENUM_MEMBER_INFO_T *member;
1213
1214 /* Name has already been checked. */
1215 assert(name && size > 0 && value);
1216 LOG((4, "%s: size %d name %s", __func__, size, name));
1217
1218 /* Allocate storage for this field information. */
1219 if (!(member = calloc(1, sizeof(NC_ENUM_MEMBER_INFO_T))))
1220 return NC_ENOMEM;
1221 if (!(member->value = malloc(size))) {
1222 free(member);
1223 return NC_ENOMEM;
1224 }
1225 if (!(member->name = strdup(name))) {
1226 free(member->value);
1227 free(member);
1228 return NC_ENOMEM;
1229 }
1230
1231 /* Store the value for this member. */
1232 memcpy(member->value, value, size);
1233
1234 /* Add object to list */
1235 nclistpush(parent->u.e.enum_member,member);
1236
1237 return NC_NOERR;
1238}
1239
1247static void
1248field_free(NC_FIELD_INFO_T *field)
1249{
1250 /* Free some stuff. */
1251 if (field->hdr.name)
1252 free(field->hdr.name);
1253 if (field->dim_size)
1254 free(field->dim_size);
1255
1256 /* Nc_Free the memory. */
1257 free(field);
1258}
1259
1269int
1270nc4_type_free(NC_TYPE_INFO_T *type)
1271{
1272 size_t i;
1273
1274 assert(type && type->rc && type->hdr.name);
1275
1276 /* Decrement the ref. count on the type */
1277 type->rc--;
1278
1279 /* Release the type, if the ref. count drops to zero */
1280 if (type->rc == 0)
1281 {
1282 LOG((4, "%s: deleting type %s", __func__, type->hdr.name));
1283
1284 /* Free the name. */
1285 free(type->hdr.name);
1286
1287 /* Enums and compound types have lists of fields to clean up. */
1288 switch (type->nc_type_class)
1289 {
1290 case NC_COMPOUND:
1291 {
1292 NC_FIELD_INFO_T *field;
1293
1294 /* Delete all the fields in this type (there will be some if its a
1295 * compound). */
1296 for(i=0;i<nclistlength(type->u.c.field);i++) {
1297 field = nclistget(type->u.c.field,i);
1298 field_free(field);
1299 }
1300 nclistfree(type->u.c.field);
1301 }
1302 break;
1303
1304 case NC_ENUM:
1305 {
1306 NC_ENUM_MEMBER_INFO_T *enum_member;
1307
1308 /* Delete all the enum_members, if any. */
1309 for(i=0;i<nclistlength(type->u.e.enum_member);i++) {
1310 enum_member = nclistget(type->u.e.enum_member,i);
1311 free(enum_member->value);
1312 free(enum_member->name);
1313 free(enum_member);
1314 }
1315 nclistfree(type->u.e.enum_member);
1316 }
1317 break;
1318
1319 default:
1320 break;
1321 }
1322
1323 /* Release the memory. */
1324 free(type);
1325 }
1326
1327 return NC_NOERR;
1328}
1329
1338int
1339nc4_att_free(NC_ATT_INFO_T *att)
1340{
1341 int stat = NC_NOERR;
1342
1343 assert(att);
1344 LOG((3, "%s: name %s ", __func__, att->hdr.name));
1345
1346 /* Free the name. */
1347 if (att->hdr.name)
1348 free(att->hdr.name);
1349
1350 if (att->data) {
1351 NC_OBJ* parent;
1352 NC_FILE_INFO_T* h5 = NULL;
1353
1354 /* Locate relevant objects */
1355 parent = att->container;
1356 if(parent->sort == NCVAR) parent = (NC_OBJ*)(((NC_VAR_INFO_T*)parent)->container);
1357 assert(parent->sort == NCGRP);
1358 h5 = ((NC_GRP_INFO_T*)parent)->nc4_info;
1359 /* Reclaim the attribute data */
1360 if((stat = NC_reclaim_data(h5->controller,att->nc_typeid,att->data,att->len))) goto done;
1361 free(att->data); /* reclaim top level */
1362 att->data = NULL;
1363 }
1364
1365done:
1366 free(att);
1367 return stat;
1368}
1369
1379static int
1380var_free(NC_VAR_INFO_T *var)
1381{
1382 int retval;
1383
1384 assert(var);
1385 LOG((4, "%s: deleting var %s", __func__, var->hdr.name));
1386
1387 /* First delete all the attributes attached to this var. */
1388 for (size_t i = 0; i < ncindexsize(var->att); i++)
1389 if ((retval = nc4_att_free((NC_ATT_INFO_T *)ncindexith(var->att, i))))
1390 return retval;
1391 ncindexfree(var->att);
1392
1393 /* Free some things that may be allocated. */
1394 if (var->chunksizes)
1395 free(var->chunksizes);
1396
1397 if (var->alt_name)
1398 free(var->alt_name);
1399
1400 if (var->dimids)
1401 free(var->dimids);
1402
1403 if (var->dim)
1404 free(var->dim);
1405
1406 memset(&var->chunkcache,0,sizeof(struct ChunkCache));
1407
1408 /* Delete any fill value allocation. */
1409 if (var->fill_value) {
1410 int tid = var->type_info->hdr.id;
1411 if((retval = NC_reclaim_data_all(var->container->nc4_info->controller, tid, var->fill_value, 1))) return retval;
1412 var->fill_value = NULL;
1413 }
1414
1415 /* Release type information */
1416 if (var->type_info)
1417 if ((retval = nc4_type_free(var->type_info)))
1418 return retval;
1419
1420 /* Do this last because debugging may need it */
1421 if (var->hdr.name)
1422 free(var->hdr.name);
1423
1424 /* Delete the var. */
1425 free(var);
1426
1427 return NC_NOERR;
1428}
1429
1439int
1440nc4_var_list_del(NC_GRP_INFO_T *grp, NC_VAR_INFO_T *var)
1441{
1442 int i;
1443
1444 assert(var && grp);
1445
1446 /* Remove from lists */
1447 i = ncindexfind(grp->vars, (NC_OBJ *)var);
1448 if (i >= 0)
1449 ncindexidel(grp->vars, (size_t)i);
1450
1451 return var_free(var);
1452}
1453
1462static int
1463dim_free(NC_DIM_INFO_T *dim)
1464{
1465 assert(dim);
1466 LOG((4, "%s: deleting dim %s", __func__, dim->hdr.name));
1467
1468 /* Free memory allocated for names. */
1469 if (dim->hdr.name)
1470 free(dim->hdr.name);
1471
1472 free(dim);
1473 return NC_NOERR;
1474}
1475
1485int
1486nc4_dim_list_del(NC_GRP_INFO_T *grp, NC_DIM_INFO_T *dim)
1487{
1488 if (grp && dim)
1489 {
1490 int pos = ncindexfind(grp->dim, (NC_OBJ *)dim);
1491 if(pos >= 0)
1492 ncindexidel(grp->dim, (size_t)pos);
1493 }
1494
1495 return dim_free(dim);
1496}
1497
1507int
1508nc4_rec_grp_del(NC_GRP_INFO_T *grp)
1509{
1510 int retval;
1511
1512 assert(grp);
1513 LOG((3, "%s: grp->name %s", __func__, grp->hdr.name));
1514
1515 /* Recursively call this function for each child, if any, stopping
1516 * if there is an error. */
1517 for (size_t i = 0; i < ncindexsize(grp->children); i++)
1518 if ((retval = nc4_rec_grp_del((NC_GRP_INFO_T *)ncindexith(grp->children,
1519 i))))
1520 return retval;
1521 ncindexfree(grp->children);
1522
1523 /* Free attributes */
1524 for (size_t i = 0; i < ncindexsize(grp->att); i++)
1525 if ((retval = nc4_att_free((NC_ATT_INFO_T *)ncindexith(grp->att, i))))
1526 return retval;
1527 ncindexfree(grp->att);
1528
1529 /* Delete all vars. */
1530 for (size_t i = 0; i < ncindexsize(grp->vars); i++) {
1531 NC_VAR_INFO_T* v = (NC_VAR_INFO_T *)ncindexith(grp->vars, i);
1532 if ((retval = var_free(v)))
1533 return retval;
1534 }
1535 ncindexfree(grp->vars);
1536
1537 /* Delete all dims, and free the list of dims. */
1538 for (size_t i = 0; i < ncindexsize(grp->dim); i++)
1539 if ((retval = dim_free((NC_DIM_INFO_T *)ncindexith(grp->dim, i))))
1540 return retval;
1541 ncindexfree(grp->dim);
1542
1543 /* Delete all types. */
1544 for (size_t i = 0; i < ncindexsize(grp->type); i++)
1545 if ((retval = nc4_type_free((NC_TYPE_INFO_T *)ncindexith(grp->type, i))))
1546 return retval;
1547 ncindexfree(grp->type);
1548
1549 /* Free the name. */
1550 free(grp->hdr.name);
1551
1552 /* Free up this group */
1553 free(grp);
1554
1555 return NC_NOERR;
1556}
1557
1567int
1568nc4_rec_grp_del_att_data(NC_GRP_INFO_T *grp)
1569{
1570 int retval;
1571
1572 assert(grp);
1573 LOG((3, "%s: grp->name %s", __func__, grp->hdr.name));
1574
1575 /* Recursively call this function for each child, if any, stopping
1576 * if there is an error. */
1577 for (size_t i = 0; i < ncindexsize(grp->children); i++)
1578 if ((retval = nc4_rec_grp_del_att_data((NC_GRP_INFO_T *)ncindexith(grp->children, i))))
1579 return retval;
1580
1581 /* Free attribute data in this group */
1582 for (size_t i = 0; i < ncindexsize(grp->att); i++) {
1583 NC_ATT_INFO_T * att = (NC_ATT_INFO_T*)ncindexith(grp->att, i);
1584 if((retval = NC_reclaim_data_all(grp->nc4_info->controller,att->nc_typeid,att->data,att->len)))
1585 return retval;
1586 att->data = NULL;
1587 att->len = 0;
1588 att->dirty = 0;
1589 }
1590
1591 /* Delete att data from all contained vars in this group */
1592 for (size_t i = 0; i < ncindexsize(grp->vars); i++) {
1593 NC_VAR_INFO_T* v = (NC_VAR_INFO_T *)ncindexith(grp->vars, i);
1594 for(size_t j=0;j<ncindexsize(v->att);j++) {
1595 NC_ATT_INFO_T* att = (NC_ATT_INFO_T*)ncindexith(v->att, j);
1596 if((retval = NC_reclaim_data_all(grp->nc4_info->controller,att->nc_typeid,att->data,att->len)))
1597 return retval;
1598 att->data = NULL;
1599 att->len = 0;
1600 att->dirty = 0;
1601 }
1602 }
1603
1604 return NC_NOERR;
1605}
1606
1617int
1618nc4_att_list_del(NCindex *list, NC_ATT_INFO_T *att)
1619{
1620 assert(att && list);
1621 ncindexidel(list, (size_t)((NC_OBJ *)att)->id);
1622 return nc4_att_free(att);
1623}
1624
1638int
1639nc4_file_list_del(int ncid)
1640{
1641 NC_FILE_INFO_T *h5;
1642 int retval;
1643
1644 /* Find our metadata for this file. */
1645 if ((retval = nc4_find_grp_h5(ncid, NULL, &h5)))
1646 return retval;
1647 assert(h5);
1648
1649 /* Delete the file resources. */
1650 if ((retval = nc4_nc4f_list_del(h5)))
1651 return retval;
1652
1653 return NC_NOERR;
1654}
1655
1665int
1666nc4_nc4f_list_del(NC_FILE_INFO_T *h5)
1667{
1668 int retval;
1669
1670 assert(h5);
1671
1672 /* Order is important here. We must delete the attribute contents
1673 before deleting any metadata because nc_reclaim_data depends
1674 on the existence of the type info.
1675 */
1676
1677 /* Delete all the attribute data contents in each group and variable. */
1678 if ((retval = nc4_rec_grp_del_att_data(h5->root_grp)))
1679 return retval;
1680
1681 /* Delete all the list contents for vars, dims, and atts, in each
1682 * group. */
1683 if ((retval = nc4_rec_grp_del(h5->root_grp)))
1684 return retval;
1685
1686 /* Cleanup these (extra) lists of all dims, groups, and types. */
1687 nclistfree(h5->alldims);
1688 nclistfree(h5->allgroups);
1689 nclistfree(h5->alltypes);
1690
1691 /* Free the NC_FILE_INFO_T struct. */
1692 nullfree(h5->hdr.name);
1693 free(h5);
1694
1695 return NC_NOERR;
1696}
1697
1711int
1712nc4_normalize_name(const char *name, char *norm_name)
1713{
1714 char *temp_name;
1715 int stat = nc_utf8_normalize((const unsigned char *)name,(unsigned char **)&temp_name);
1716 if(stat != NC_NOERR)
1717 return stat;
1718 if (strlen(temp_name) > NC_MAX_NAME)
1719 {
1720 free(temp_name);
1721 return NC_EMAXNAME;
1722 }
1723 strcpy(norm_name, temp_name);
1724 free(temp_name);
1725 return NC_NOERR;
1726}
1727
1728#ifdef NETCDF_ENABLE_SET_LOG_LEVEL
1729
1736int
1737nc4_init_logging(void)
1738{
1739 int ret = NC_NOERR;
1740
1741#if LOGGING
1742#if NC_HAS_PARALLEL4
1743 if (!LOG_FILE && nc_log_level >= 0)
1744 {
1745 char log_filename[NC_MAX_NAME];
1746 int my_rank = 0;
1747 int mpierr;
1748 int mpi_initialized;
1749
1750 /* If MPI has been initialized find the rank. */
1751 if ((mpierr = MPI_Initialized(&mpi_initialized)))
1752 return NC_EMPI;
1753 if (mpi_initialized)
1754 {
1755 if ((mpierr = MPI_Comm_rank(MPI_COMM_WORLD, &my_rank)))
1756 return NC_EMPI;
1757 }
1758
1759 /* Create a filename with the rank in it. */
1760 snprintf(log_filename, sizeof(log_filename), "nc4_log_%d.log", my_rank);
1761
1762 /* Open a file for this rank to log messages. */
1763 if (!(LOG_FILE = fopen(log_filename, "w")))
1764 return NC_EINTERNAL;
1765 }
1766#endif /* NC_HAS_PARALLEL4 */
1767#endif /* LOGGING */
1768
1769 return ret;
1770}
1771
1778void
1779nc4_finalize_logging(void)
1780{
1781#if LOGGING
1782#if NC_HAS_PARALLEL4
1783 if (LOG_FILE)
1784 {
1785 fclose(LOG_FILE);
1786 LOG_FILE = NULL;
1787 }
1788#endif /* NC_HAS_PARALLEL4 */
1789#endif /* LOGGING */
1790}
1791
1805int
1806nc_set_log_level(int new_level)
1807{
1808#if LOGGING
1809 /* Remember the new level. */
1810 nc_log_level = new_level;
1811
1812#if NC_HAS_PARALLEL4
1813 /* For parallel I/O builds, call the log init/finalize functions
1814 * as needed, to open and close the log files. */
1815 if (new_level >= 0)
1816 {
1817 if (!LOG_FILE)
1818 nc4_init_logging();
1819 }
1820 else
1821 nc4_finalize_logging();
1822#endif /* NC_HAS_PARALLEL4 */
1823
1824 LOG((1, "log_level changed to %d", nc_log_level));
1825#endif /* LOGGING */
1826
1827 return NC_NOERR;
1828}
1829#endif /* NETCDF_ENABLE_SET_LOG_LEVEL */
1830
1831#if LOGGING
1832#define MAX_NESTS 10
1842static int
1843rec_print_metadata(NC_GRP_INFO_T *grp, int tab_count)
1844{
1845 NC_ATT_INFO_T *att;
1846 NC_VAR_INFO_T *var;
1847 NC_DIM_INFO_T *dim;
1848 NC_TYPE_INFO_T *type;
1849 NC_FIELD_INFO_T *field;
1850 char tabs[MAX_NESTS+1] = "";
1851 char temp_string[10];
1852 int t, retval, d, i;
1853
1854 /* Come up with a number of tabs relative to the group. */
1855 for (t = 0; t < tab_count && t < MAX_NESTS; t++)
1856 tabs[t] = '\t';
1857 tabs[t] = '\0';
1858
1859 LOG((2, "%s GROUP - %s nc_grpid: %d nvars: %d natts: %d",
1860 tabs, grp->hdr.name, grp->hdr.id, ncindexsize(grp->vars), ncindexsize(grp->att)));
1861
1862 for (i = 0; i < ncindexsize(grp->att); i++)
1863 {
1864 att = (NC_ATT_INFO_T *)ncindexith(grp->att, i);
1865 assert(att);
1866 LOG((2, "%s GROUP ATTRIBUTE - attnum: %d name: %s type: %d len: %d",
1867 tabs, att->hdr.id, att->hdr.name, att->nc_typeid, att->len));
1868 }
1869
1870 for (i = 0; i < ncindexsize(grp->dim); i++)
1871 {
1872 dim = (NC_DIM_INFO_T *)ncindexith(grp->dim, i);
1873 assert(dim);
1874 LOG((2, "%s DIMENSION - dimid: %d name: %s len: %d unlimited: %d",
1875 tabs, dim->hdr.id, dim->hdr.name, dim->len, dim->unlimited));
1876 }
1877
1878 for (i = 0; i < ncindexsize(grp->vars); i++)
1879 {
1880 int j;
1881 char storage_str[NC_MAX_NAME] = "";
1882 char *dims_string = NULL;
1883
1884 var = (NC_VAR_INFO_T*)ncindexith(grp->vars,i);
1885 assert(var);
1886 if (var->ndims > 0)
1887 {
1888 if (!(dims_string = malloc(sizeof(char) * var->ndims * 4)))
1889 return NC_ENOMEM;
1890 strcpy(dims_string, "");
1891 for (d = 0; d < var->ndims; d++)
1892 {
1893 snprintf(temp_string, sizeof(temp_string), " %d", var->dimids[d]);
1894 strcat(dims_string, temp_string);
1895 }
1896 }
1897 if (!var->meta_read)
1898 strcat(storage_str, "unknown");
1899 else if (var->storage == NC_CONTIGUOUS)
1900 strcat(storage_str, "contiguous");
1901 else if (var->storage == NC_COMPACT)
1902 strcat(storage_str, "compact");
1903 else if (var->storage == NC_CHUNKED)
1904 strcat(storage_str, "chunked");
1905 else if (var->storage == NC_VIRTUAL)
1906 strcat(storage_str, "virtual");
1907 else
1908 strcat(storage_str, "unknown");
1909 LOG((2, "%s VARIABLE - varid: %d name: %s ndims: %d "
1910 "dimids:%s storage: %s", tabs, var->hdr.id, var->hdr.name,
1911 var->ndims,
1912 (dims_string ? dims_string : " -"), storage_str));
1913 for (j = 0; j < ncindexsize(var->att); j++)
1914 {
1915 att = (NC_ATT_INFO_T *)ncindexith(var->att, j);
1916 assert(att);
1917 LOG((2, "%s VAR ATTRIBUTE - attnum: %d name: %s type: %d len: %d",
1918 tabs, att->hdr.id, att->hdr.name, att->nc_typeid, att->len));
1919 }
1920 if (dims_string)
1921 free(dims_string);
1922 }
1923
1924 for (i = 0; i < ncindexsize(grp->type); i++)
1925 {
1926 type = (NC_TYPE_INFO_T*)ncindexith(grp->type, i);
1927 assert(type);
1928 LOG((2, "%s TYPE - nc_typeid: %d size: %d committed: %d name: %s",
1929 tabs, type->hdr.id, type->size, (int)type->committed, type->hdr.name));
1930 /* Is this a compound type? */
1931 if (type->nc_type_class == NC_COMPOUND)
1932 {
1933 int j;
1934 LOG((3, "compound type"));
1935 for (j = 0; j < nclistlength(type->u.c.field); j++)
1936 {
1937 field = (NC_FIELD_INFO_T *)nclistget(type->u.c.field, j);
1938 LOG((4, "field %s offset %d nctype %d ndims %d", field->hdr.name,
1939 field->offset, field->nc_typeid, field->ndims));
1940 }
1941 }
1942 else if (type->nc_type_class == NC_VLEN)
1943 {
1944 LOG((3, "VLEN type"));
1945 LOG((4, "base_nc_type: %d", type->u.v.base_nc_typeid));
1946 }
1947 else if (type->nc_type_class == NC_OPAQUE)
1948 LOG((3, "Opaque type"));
1949 else if (type->nc_type_class == NC_ENUM)
1950 {
1951 LOG((3, "Enum type"));
1952 LOG((4, "base_nc_type: %d", type->u.e.base_nc_typeid));
1953 }
1954 else
1955 {
1956 LOG((0, "Unknown class: %d", type->nc_type_class));
1957 return NC_EBADTYPE;
1958 }
1959 }
1960
1961 /* Call self for each child of this group. */
1962 for (i = 0; i < ncindexsize(grp->children); i++)
1963 if ((retval = rec_print_metadata((NC_GRP_INFO_T *)ncindexith(grp->children, i),
1964 tab_count + 1)))
1965 return retval;
1966
1967 return NC_NOERR;
1968}
1969
1980int
1981log_metadata_nc(NC_FILE_INFO_T *h5)
1982{
1983 LOG((2, "*** NetCDF-4 Internal Metadata: int_ncid 0x%x ext_ncid 0x%x",
1984 h5->root_grp->nc4_info->controller->int_ncid,
1985 h5->root_grp->nc4_info->controller->ext_ncid));
1986 if (!h5)
1987 {
1988 LOG((2, "This is a netCDF-3 file."));
1989 return NC_NOERR;
1990 }
1991 LOG((2, "FILE - path: %s cmode: 0x%x parallel: %d redef: %d "
1992 "fill_mode: %d no_write: %d next_nc_grpid: %d", h5->root_grp->nc4_info->controller->path,
1993 h5->cmode, (int)h5->parallel, (int)h5->redef, h5->fill_mode, (int)h5->no_write,
1994 h5->next_nc_grpid));
1995 if(nc_log_level >= 2)
1996 return rec_print_metadata(h5->root_grp, 0);
1997 return NC_NOERR;
1998}
1999
2000#endif /*LOGGING */
2001
2013int
2014NC4_show_metadata(int ncid)
2015{
2016 int retval = NC_NOERR;
2017#if LOGGING
2018 NC_FILE_INFO_T *h5;
2019 int old_log_level = nc_log_level;
2020
2021 /* Find file metadata. */
2022 if ((retval = nc4_find_grp_h5(ncid, NULL, &h5)))
2023 return retval;
2024
2025 /* Log level must be 2 to see metadata. */
2026 nc_log_level = 2;
2027 retval = log_metadata_nc(h5);
2028 nc_log_level = old_log_level;
2029#endif /*LOGGING*/
2030 return retval;
2031}
2032
2040const NC_reservedatt*
2041NC_findreserved(const char* name)
2042{
2043#if 0
2044 int n = NRESERVED;
2045 int L = 0;
2046 int R = (n - 1);
2047
2048 for(;;) {
2049 if(L > R) break;
2050 int m = (L + R) / 2;
2051 const NC_reservedatt* p = &NC_reserved[m];
2052 int cmp = strcmp(p->name,name);
2053 if(cmp == 0) return p;
2054 if(cmp < 0)
2055 L = (m + 1);
2056 else /*cmp > 0*/
2057 R = (m - 1);
2058 }
2059 return NULL;
2060#else
2061 return (const NC_reservedatt*)bsearch(name,NC_reserved,NRESERVED,sizeof(NC_reservedatt),bincmp);
2062#endif
2063}
2064
2065/* Ed Hartness requires this function */
2066static int
2067NC4_move_in_NCList(NC* nc, int new_id)
2068{
2069 int stat = move_in_NCList(nc,new_id);
2070 if(stat == NC_NOERR) {
2071 /* Synchronize header */
2072 if(nc->dispatchdata)
2073 ((NC_OBJ*)nc->dispatchdata)->id = nc->ext_ncid;
2074 }
2075 return stat;
2076}
2077
2078static int
2079sortcmp(const void* arg1, const void* arg2)
2080{
2081 NC_reservedatt* r1 = (NC_reservedatt*)arg1;
2082 NC_reservedatt* r2 = (NC_reservedatt*)arg2;
2083 return strcmp(r1->name,r2->name);
2084}
2085
2086static int
2087bincmp(const void* arg1, const void* arg2)
2088{
2089 const char* name = (const char*)arg1;
2090 NC_reservedatt* ra = (NC_reservedatt*)arg2;
2091 return strcmp(name,ra->name);
2092}
2093
2094void
2095NC_initialize_reserved(void)
2096{
2097 /* Guarantee the reserved attribute list is sorted */
2098 qsort((void*)NC_reserved,NRESERVED,sizeof(NC_reservedatt),sortcmp);
2099}
Main header file for the C API.
#define NC_UNLIMITED
Size argument to nc_def_dim() for an unlimited dimension.
Definition netcdf.h:300
#define NC_EBADTYPE
Not a netcdf data type.
Definition netcdf.h:459
#define NC_VIRTUAL
In HDF5 files you can set storage for each variable to be either contiguous or chunked,...
Definition netcdf.h:357
#define NC_VLEN
vlen (variable-length) types
Definition netcdf.h:53
#define NC_ENAMEINUSE
String match to name in use.
Definition netcdf.h:456
#define NC_ENOMEM
Memory allocation (malloc) failure.
Definition netcdf.h:497
#define NC_COMPOUND
compound types
Definition netcdf.h:56
#define NC_EINTERNAL
NetCDF Library Internal Error.
Definition netcdf.h:523
#define NC_CHUNKED
In HDF5 files you can set storage for each variable to be either contiguous or chunked,...
Definition netcdf.h:353
#define NC_ENUM
enum types
Definition netcdf.h:55
#define NC_CONTIGUOUS
In HDF5 files you can set storage for each variable to be either contiguous or chunked,...
Definition netcdf.h:354
#define NC_GLOBAL
Attribute id to put/get a global attribute.
Definition netcdf.h:303
#define NC_EMAXNAME
NC_MAX_NAME exceeded.
Definition netcdf.h:475
#define NC_ENOTATT
Attribute not found.
Definition netcdf.h:457
#define NC_COMPACT
In HDF5 files you can set storage for each variable to be either contiguous or chunked,...
Definition netcdf.h:355
#define NC_ENOTVAR
Variable not found.
Definition netcdf.h:471
#define NC_EINVAL
Invalid Argument.
Definition netcdf.h:427
#define NC_MAX_NAME
Maximum for classic library.
Definition netcdf.h:330
#define NC_NOERR
No Error.
Definition netcdf.h:417
#define NC_EMPI
MPI operation failed.
Definition netcdf.h:560
#define NC_OPAQUE
opaque types
Definition netcdf.h:54
#define NC_EIO
Generic IO error.
Definition netcdf.h:506
#define NC_STRING
string
Definition netcdf.h:47
#define NC_EBADID
Not a netcdf id.
Definition netcdf.h:424
#define NC_EBADTYPID
Bad type ID.
Definition netcdf.h:546
#define NC_EBADDIM
Invalid dimension id or name.
Definition netcdf.h:460
int nc_type
The nc_type type is just an int.
Definition netcdf.h:25