20 #include "netcdf_filter.h"
21 #include "nc4internal.h"
23 #include "ncdispatch.h"
25 #include "netcdf_aux.h"
40 {NC_ATT_CLASS, READONLYFLAG|DIMSCALEFLAG},
41 {NC_ATT_DIMENSION_LIST, READONLYFLAG|DIMSCALEFLAG},
42 {NC_ATT_NAME, READONLYFLAG|DIMSCALEFLAG},
43 {NC_ATT_REFERENCE_LIST, READONLYFLAG|DIMSCALEFLAG},
44 {NC_ATT_FORMAT, READONLYFLAG},
45 {ISNETCDF4ATT, READONLYFLAG|NAMEONLYFLAG},
46 {NCPROPS, READONLYFLAG|NAMEONLYFLAG|MATERIALIZEDFLAG},
47 {NC_ATT_COORDINATES, READONLYFLAG|DIMSCALEFLAG|MATERIALIZEDFLAG},
48 {NC_ATT_DIMID_NAME, READONLYFLAG|DIMSCALEFLAG|MATERIALIZEDFLAG},
49 {SUPERBLOCKATT, READONLYFLAG|NAMEONLYFLAG},
50 {NC_ATT_NC3_STRICT_NAME, READONLYFLAG|MATERIALIZEDFLAG},
58 static int NC4_move_in_NCList(NC* nc,
int new_id);
64 int nc_log_level = NC_TURN_OFF_LOGGING;
94 if ((retval = NC_check_name(name)))
98 if ((retval = nc_utf8_normalize((
const unsigned char *)name,
99 (
unsigned char **)&temp)))
110 strcpy(norm_name, temp);
143 if ((ret = NC_check_id(ncid, &nc)))
154 *dispatchdata = nc->dispatchdata;
178 LOG((2,
"%s: ncid %d new_ncid_index %d", __func__, ncid, new_ncid_index));
181 if ((ret = NC_check_id(ncid, &nc)))
186 LOG((3,
"moving nc->ext_ncid %d nc->ext_ncid >> ID_SHIFT %d",
187 nc->ext_ncid, nc->ext_ncid >> ID_SHIFT));
188 if (NC4_move_in_NCList(nc, new_ncid_index))
190 LOG((3,
"moved to new_ncid_index %d new nc->ext_ncid %d", new_ncid_index,
222 if ((ret = NC_check_id(ncid, &nc)))
235 *dispatchdata = nc->dispatchdata;
260 assert(nc && !NC4_DATA(nc) && path);
264 if (!(h5 = calloc(1,
sizeof(NC_FILE_INFO_T))))
266 nc->dispatchdata = h5;
269 h5->hdr.sort = NCFIL;
270 h5->hdr.name = strdup(path);
271 h5->hdr.id = nc->ext_ncid;
274 h5->cmode = mode | NC_INDEF;
281 h5->alldims = nclistnew();
282 h5->alltypes = nclistnew();
283 h5->allgroups = nclistnew();
350 NC_GRP_INFO_T *my_grp = NULL;
351 NC_FILE_INFO_T *my_h5 = NULL;
356 if ((retval = NC_check_id(ncid, &my_nc)))
358 my_h5 = my_nc->dispatchdata;
359 assert(my_h5 && my_h5->root_grp);
362 if (!(my_grp = nclistget(my_h5->allgroups, (ncid & GRP_ID_MASK))))
395 NC_FILE_INFO_T *my_h5;
396 NC_GRP_INFO_T *my_grp;
397 NC_VAR_INFO_T *my_var;
403 assert(my_grp && my_h5);
406 if (!(my_var = (NC_VAR_INFO_T *)ncindexith(my_grp->vars, varid)))
408 assert(my_var && my_var->hdr.id == varid);
436 NC_GRP_INFO_T **dim_grp)
438 assert(grp && grp->nc4_info && dim);
439 LOG((4,
"%s: dimid %d", __func__, dimid));
442 if (!((*dim) = nclistget(grp->nc4_info->alldims, dimid)))
447 *dim_grp = (*dim)->container;
463 nc4_find_var(NC_GRP_INFO_T *grp,
const char *name, NC_VAR_INFO_T **var)
465 assert(grp && var && name);
468 *var = (NC_VAR_INFO_T*)ncindexlookup(grp->vars,name);
485 NC_TYPE_INFO_T *type, *res;
491 type = (NC_TYPE_INFO_T*)ncindexlookup(start_grp->type,name);
496 for(i=0;i<ncindexsize(start_grp->children);i++) {
497 g = (NC_GRP_INFO_T*)ncindexith(start_grp->children,i);
498 if(g == NULL)
continue;
522 if (
typeid < 0 || !type)
532 if (!(*type = nclistget(h5->alltypes,
typeid)))
558 NC_ATT_INFO_T *my_att;
559 NCindex *attlist = NULL;
561 assert(grp && grp->hdr.name && att);
563 LOG((4,
"%s: grp->name %s varid %d attnum %d", __func__, grp->hdr.name,
573 var = (NC_VAR_INFO_T*)ncindexith(grp->vars,varid);
583 my_att = (NC_ATT_INFO_T *)ncindexlookup(attlist, name);
585 my_att = (NC_ATT_INFO_T *)ncindexith(attlist, attnum);
617 LOG((4,
"nc4_find_nc_att: ncid 0x%x varid %d name %s attnum %d",
618 ncid, varid, name, attnum));
642 case NCDIM: list = file->alldims;
break;
643 case NCTYP: list = file->alltypes;
break;
644 case NCGRP: list = file->allgroups;
break;
649 nclistset(list,obj->id,obj);
669 NC_VAR_INFO_T *new_var = NULL;
672 if (!(new_var = calloc(1,
sizeof(NC_VAR_INFO_T))))
674 new_var->hdr.sort = NCVAR;
675 new_var->container = grp;
683 new_var->hdr.id = ncindexsize(grp->vars);
684 if (!(new_var->hdr.name = strdup(name))) {
691 new_var->att = ncindexnew(0);
694 ncindexadd(grp->vars, (NC_OBJ *)new_var);
726 if (!(var->dim = calloc(ndims,
sizeof(NC_DIM_INFO_T *))))
728 if (!(var->dimids = calloc(ndims,
sizeof(
int))))
733 memset(var->dimids, -1, ndims *
sizeof(
int));
782 int assignedid, NC_DIM_INFO_T **dim)
784 NC_DIM_INFO_T *new_dim = NULL;
789 if (!(new_dim = calloc(1,
sizeof(NC_DIM_INFO_T))))
792 new_dim->hdr.sort = NCDIM;
796 new_dim->hdr.id = assignedid;
798 new_dim->hdr.id = grp->nc4_info->next_dimid++;
801 if (!(new_dim->hdr.name = strdup(name))) {
811 new_dim->unlimited = NC_TRUE;
814 new_dim->container = grp;
817 ncindexadd(grp->dim, (NC_OBJ *)new_dim);
818 obj_track(grp->nc4_info, (NC_OBJ *)new_dim);
842 NC_ATT_INFO_T *new_att = NULL;
844 LOG((3,
"%s: name %s ", __func__, name));
846 if (!(new_att = calloc(1,
sizeof(NC_ATT_INFO_T))))
848 new_att->hdr.sort = NCATT;
851 new_att->hdr.id = ncindexsize(list);
852 if (!(new_att->hdr.name = strdup(name))) {
859 ncindexadd(list, (NC_OBJ *)new_att);
886 NC_GRP_INFO_T *new_grp;
890 LOG((3,
"%s: name %s ", __func__, name));
893 if (!(new_grp = calloc(1,
sizeof(NC_GRP_INFO_T))))
897 new_grp->hdr.sort = NCGRP;
898 new_grp->nc4_info = h5;
899 new_grp->parent = parent;
902 new_grp->hdr.id = h5->next_nc_grpid++;
903 assert(parent || !new_grp->hdr.id);
906 if (!(new_grp->hdr.name = strdup(name)))
913 new_grp->children = ncindexnew(0);
914 new_grp->dim = ncindexnew(0);
915 new_grp->att = ncindexnew(0);
916 new_grp->type = ncindexnew(0);
917 new_grp->vars = ncindexnew(0);
921 ncindexadd(parent->children, (NC_OBJ *)new_grp);
947 NC_TYPE_INFO_T *type;
952 type = (NC_TYPE_INFO_T*)ncindexlookup(grp->type,name);
957 g = (NC_GRP_INFO_T*)ncindexlookup(grp->children,name);
962 var = (NC_VAR_INFO_T*)ncindexlookup(grp->vars,name);
984 NC_TYPE_INFO_T **type)
986 NC_TYPE_INFO_T *new_type;
988 LOG((4,
"%s: size %d name %s assignedid %d", __func__, size, name, assignedid));
994 if (!(new_type = calloc(1,
sizeof(NC_TYPE_INFO_T))))
996 new_type->hdr.sort = NCTYP;
997 new_type->hdr.id = assignedid;
1000 new_type->size = size;
1001 if (!(new_type->hdr.name = strdup(name))) {
1027 NC_TYPE_INFO_T **type)
1029 NC_TYPE_INFO_T *new_type;
1033 assert(grp && name && type);
1034 LOG((4,
"%s: size %d name %s", __func__, size, name));
1037 if ((retval =
nc4_type_new(size, name, grp->nc4_info->next_typeid,
1040 grp->nc4_info->next_typeid++;
1046 ncindexadd(grp->type, (NC_OBJ *)new_type);
1047 obj_track(grp->nc4_info,(NC_OBJ*)new_type);
1070 size_t offset,
nc_type xtype,
int ndims,
1071 const int *dim_sizesp)
1073 NC_FIELD_INFO_T *field;
1080 if (!(field = calloc(1,
sizeof(NC_FIELD_INFO_T))))
1082 field->hdr.sort = NCFLD;
1085 if (!(field->hdr.name = strdup(name)))
1090 field->nc_typeid = xtype;
1091 field->offset = offset;
1092 field->ndims = ndims;
1096 if (!(field->dim_size = malloc(ndims *
sizeof(
int))))
1098 free(field->hdr.name);
1102 for (i = 0; i < ndims; i++)
1103 field->dim_size[i] = dim_sizesp[i];
1107 field->hdr.id = nclistlength(parent->u.c.field);
1108 nclistpush(parent->u.c.field,field);
1127 const char *name,
const void *value)
1129 NC_ENUM_MEMBER_INFO_T *member;
1132 assert(name && size > 0 && value);
1133 LOG((4,
"%s: size %d name %s", __func__, size, name));
1136 if (!(member = calloc(1,
sizeof(NC_ENUM_MEMBER_INFO_T))))
1138 if (!(member->value = malloc(size))) {
1142 if (!(member->name = strdup(name))) {
1143 free(member->value);
1149 memcpy(member->value, value, size);
1152 nclistpush(parent->u.e.enum_member,member);
1169 if (field->hdr.name)
1170 free(field->hdr.name);
1171 if (field->dim_size)
1172 free(field->dim_size);
1192 assert(type && type->rc && type->hdr.name);
1200 LOG((4,
"%s: deleting type %s", __func__, type->hdr.name));
1203 free(type->hdr.name);
1206 switch (type->nc_type_class)
1210 NC_FIELD_INFO_T *field;
1214 for(i=0;i<nclistlength(type->u.c.field);i++) {
1215 field = nclistget(type->u.c.field,i);
1218 nclistfree(type->u.c.field);
1224 NC_ENUM_MEMBER_INFO_T *enum_member;
1227 for(i=0;i<nclistlength(type->u.e.enum_member);i++) {
1228 enum_member = nclistget(type->u.e.enum_member,i);
1229 free(enum_member->value);
1230 free(enum_member->name);
1233 nclistfree(type->u.e.enum_member);
1262 LOG((3,
"%s: name %s ", __func__, att->hdr.name));
1271 free(att->hdr.name);
1281 for (i = 0; i < att->len; i++)
1283 free(att->stdata[i]);
1290 for (i = 0; i < att->len; i++)
1315 LOG((4,
"%s: deleting var %s", __func__, var->hdr.name));
1318 for (i = 0; i < ncindexsize(var->att); i++)
1319 if ((retval =
nc4_att_free((NC_ATT_INFO_T *)ncindexith(var->att, i))))
1321 ncindexfree(var->att);
1324 if (var->chunksizes)
1325 free(var->chunksizes);
1328 free(var->alt_name);
1337 if (var->fill_value)
1338 free(var->fill_value);
1347 free(var->hdr.name);
1372 i = ncindexfind(grp->vars, (NC_OBJ *)var);
1374 ncindexidel(grp->vars, i);
1391 LOG((4,
"%s: deleting dim %s", __func__, dim->hdr.name));
1395 free(dim->hdr.name);
1415 int pos = ncindexfind(grp->dim, (NC_OBJ *)dim);
1417 ncindexidel(grp->dim, pos);
1439 LOG((3,
"%s: grp->name %s", __func__, grp->hdr.name));
1443 for (i = 0; i < ncindexsize(grp->children); i++)
1444 if ((retval =
nc4_rec_grp_del((NC_GRP_INFO_T *)ncindexith(grp->children,
1447 ncindexfree(grp->children);
1450 for (i = 0; i < ncindexsize(grp->att); i++)
1451 if ((retval =
nc4_att_free((NC_ATT_INFO_T *)ncindexith(grp->att, i))))
1453 ncindexfree(grp->att);
1456 for (i = 0; i < ncindexsize(grp->vars); i++) {
1457 NC_VAR_INFO_T* v = (NC_VAR_INFO_T *)ncindexith(grp->vars, i);
1461 ncindexfree(grp->vars);
1464 for (i = 0; i < ncindexsize(grp->dim); i++)
1465 if ((retval =
dim_free((NC_DIM_INFO_T *)ncindexith(grp->dim, i))))
1467 ncindexfree(grp->dim);
1470 for (i = 0; i < ncindexsize(grp->type); i++)
1471 if ((retval =
nc4_type_free((NC_TYPE_INFO_T *)ncindexith(grp->type, i))))
1473 ncindexfree(grp->type);
1476 free(grp->hdr.name);
1497 assert(att && list);
1498 ncindexidel(list, ((NC_OBJ *)att)->
id);
1555 nclistfree(h5->alldims);
1556 nclistfree(h5->allgroups);
1557 nclistfree(h5->alltypes);
1560 nullfree(h5->hdr.name);
1583 int stat = nc_utf8_normalize((
const unsigned char *)name,(
unsigned char **)&temp_name);
1591 strcpy(norm_name, temp_name);
1596 #ifdef ENABLE_SET_LOG_LEVEL
1613 nc_set_log_level(
int new_level)
1617 nc_log_level = new_level;
1618 LOG((4,
"log_level changed to %d", nc_log_level));
1625 #define MAX_NESTS 10
1636 rec_print_metadata(NC_GRP_INFO_T *grp,
int tab_count)
1641 NC_TYPE_INFO_T *type;
1642 NC_FIELD_INFO_T *field;
1643 char tabs[MAX_NESTS+1] =
"";
1644 char temp_string[10];
1645 int t, retval, d, i;
1648 for (t = 0; t < tab_count && t < MAX_NESTS; t++)
1652 LOG((2,
"%s GROUP - %s nc_grpid: %d nvars: %d natts: %d",
1653 tabs, grp->hdr.name, grp->hdr.id, ncindexsize(grp->vars), ncindexsize(grp->att)));
1655 for (i = 0; i < ncindexsize(grp->att); i++)
1657 att = (NC_ATT_INFO_T *)ncindexith(grp->att, i);
1659 LOG((2,
"%s GROUP ATTRIBUTE - attnum: %d name: %s type: %d len: %d",
1660 tabs, att->hdr.id, att->hdr.name, att->nc_typeid, att->len));
1663 for (i = 0; i < ncindexsize(grp->dim); i++)
1665 dim = (NC_DIM_INFO_T *)ncindexith(grp->dim, i);
1667 LOG((2,
"%s DIMENSION - dimid: %d name: %s len: %d unlimited: %d",
1668 tabs, dim->hdr.id, dim->hdr.name, dim->len, dim->unlimited));
1671 for (i = 0; i < ncindexsize(grp->vars); i++)
1675 char *dims_string = NULL;
1677 var = (NC_VAR_INFO_T*)ncindexith(grp->vars,i);
1681 if (!(dims_string = malloc(
sizeof(
char) * var->ndims * 4)))
1683 strcpy(dims_string,
"");
1684 for (d = 0; d < var->ndims; d++)
1686 sprintf(temp_string,
" %d", var->dimids[d]);
1687 strcat(dims_string, temp_string);
1690 if (!var->meta_read)
1691 strcat(storage_str,
"unknown");
1693 strcat(storage_str,
"contiguous");
1695 strcat(storage_str,
"compact");
1697 strcat(storage_str,
"chunked");
1698 LOG((2,
"%s VARIABLE - varid: %d name: %s ndims: %d "
1699 "dimids:%s storage: %s", tabs, var->hdr.id, var->hdr.name,
1701 (dims_string ? dims_string :
" -"), storage_str));
1702 for (j = 0; j < ncindexsize(var->att); j++)
1704 att = (NC_ATT_INFO_T *)ncindexith(var->att, j);
1706 LOG((2,
"%s VAR ATTRIBUTE - attnum: %d name: %s type: %d len: %d",
1707 tabs, att->hdr.id, att->hdr.name, att->nc_typeid, att->len));
1713 for (i = 0; i < ncindexsize(grp->type); i++)
1715 type = (NC_TYPE_INFO_T*)ncindexith(grp->type, i);
1717 LOG((2,
"%s TYPE - nc_typeid: %d size: %d committed: %d name: %s",
1718 tabs, type->hdr.id, type->size, (
int)type->committed, type->hdr.name));
1723 LOG((3,
"compound type"));
1724 for (j = 0; j < nclistlength(type->u.c.field); j++)
1726 field = (NC_FIELD_INFO_T *)nclistget(type->u.c.field, j);
1727 LOG((4,
"field %s offset %d nctype %d ndims %d", field->hdr.name,
1728 field->offset, field->nc_typeid, field->ndims));
1731 else if (type->nc_type_class ==
NC_VLEN)
1733 LOG((3,
"VLEN type"));
1734 LOG((4,
"base_nc_type: %d", type->u.v.base_nc_typeid));
1736 else if (type->nc_type_class ==
NC_OPAQUE)
1737 LOG((3,
"Opaque type"));
1738 else if (type->nc_type_class ==
NC_ENUM)
1740 LOG((3,
"Enum type"));
1741 LOG((4,
"base_nc_type: %d", type->u.e.base_nc_typeid));
1745 LOG((0,
"Unknown class: %d", type->nc_type_class));
1751 for (i = 0; i < ncindexsize(grp->children); i++)
1752 if ((retval = rec_print_metadata((NC_GRP_INFO_T *)ncindexith(grp->children, i),
1770 log_metadata_nc(NC_FILE_INFO_T *h5)
1772 LOG((2,
"*** NetCDF-4 Internal Metadata: int_ncid 0x%x ext_ncid 0x%x",
1773 h5->root_grp->nc4_info->controller->int_ncid,
1774 h5->root_grp->nc4_info->controller->ext_ncid));
1777 LOG((2,
"This is a netCDF-3 file."));
1780 LOG((2,
"FILE - path: %s cmode: 0x%x parallel: %d redef: %d "
1781 "fill_mode: %d no_write: %d next_nc_grpid: %d", h5->root_grp->nc4_info->controller->path,
1782 h5->cmode, (
int)h5->parallel, (
int)h5->redef, h5->fill_mode, (
int)h5->no_write,
1783 h5->next_nc_grpid));
1784 if(nc_log_level >= 2)
1785 return rec_print_metadata(h5->root_grp, 0);
1808 int old_log_level = nc_log_level;
1816 retval = log_metadata_nc(h5);
1817 nc_log_level = old_log_level;
1829 const NC_reservedatt*
1837 int m = (L + R) / 2;
1839 int cmp = strcmp(p->name,name);
1840 if(cmp == 0)
return p;
1850 NC4_move_in_NCList(NC* nc,
int new_id)
1852 int stat = move_in_NCList(nc,new_id);
1855 if(nc->dispatchdata)
1856 ((NC_OBJ*)nc->dispatchdata)->id = nc->ext_ncid;