19 #include "nc4internal.h"
21 #include "ncdispatch.h"
33 int nc_log_level = NC_TURN_OFF_LOGGING;
49 nc4_check_name(
const char *name,
char *norm_name)
63 if ((retval = NC_check_name(name)))
67 if ((retval = nc_utf8_normalize((
const unsigned char *)name,
68 (
unsigned char **)&temp)))
79 strcpy(norm_name, temp);
106 nc4_file_list_add(
int ncid,
const char *path,
int mode,
void **dispatchdata)
112 if ((ret = NC_check_id(ncid, &nc)))
117 if ((ret = nc4_nc4f_list_add(nc, path, mode)))
123 *dispatchdata = nc->dispatchdata;
142 nc4_file_change_ncid(
int ncid,
unsigned short new_ncid_index)
147 LOG((2,
"%s: ncid %d new_ncid_index %d", __func__, ncid, new_ncid_index));
150 if ((ret = NC_check_id(ncid, &nc)))
155 LOG((3,
"moving nc->ext_ncid %d nc->ext_ncid >> ID_SHIFT %d",
156 nc->ext_ncid, nc->ext_ncid >> ID_SHIFT));
157 if (move_in_NCList(nc, new_ncid_index))
159 LOG((3,
"moved to new_ncid_index %d new nc->ext_ncid %d", new_ncid_index,
185 nc4_file_list_get(
int ncid,
char **path,
int *mode,
void **dispatchdata)
191 if ((ret = NC_check_id(ncid, &nc)))
204 *dispatchdata = nc->dispatchdata;
224 nc4_nc4f_list_add(NC *nc,
const char *path,
int mode)
229 assert(nc && !NC4_DATA(nc) && path);
233 if (!(h5 = calloc(1,
sizeof(NC_FILE_INFO_T))))
235 nc->dispatchdata = h5;
239 h5->cmode = mode | NC_INDEF;
243 h5->next_typeid = NC_FIRSTUSERTYPEID;
246 h5->alldims = nclistnew();
247 h5->alltypes = nclistnew();
248 h5->allgroups = nclistnew();
253 if ((retval = nc4_grp_list_add(h5, NULL, NC_GROUP_NAME, &h5->root_grp)))
272 nc4_find_nc4_grp(
int ncid, NC_GRP_INFO_T **grp)
274 return nc4_find_nc_grp_h5(ncid, NULL, grp, NULL);
293 nc4_find_grp_h5(
int ncid, NC_GRP_INFO_T **grp, NC_FILE_INFO_T **h5)
295 return nc4_find_nc_grp_h5(ncid, NULL, grp, h5);
313 nc4_find_nc_grp_h5(
int ncid, NC **nc, NC_GRP_INFO_T **grp, NC_FILE_INFO_T **h5)
315 NC_GRP_INFO_T *my_grp = NULL;
316 NC_FILE_INFO_T *my_h5 = NULL;
321 if ((retval = NC_check_id(ncid, &my_nc)))
323 my_h5 = my_nc->dispatchdata;
324 assert(my_h5 && my_h5->root_grp);
327 if (!(my_grp = nclistget(my_h5->allgroups, (ncid & GRP_ID_MASK))))
357 nc4_find_grp_h5_var(
int ncid,
int varid, NC_FILE_INFO_T **h5, NC_GRP_INFO_T **grp,
360 NC_FILE_INFO_T *my_h5;
361 NC_GRP_INFO_T *my_grp;
362 NC_VAR_INFO_T *my_var;
366 if ((retval = nc4_find_grp_h5(ncid, &my_grp, &my_h5)))
368 assert(my_grp && my_h5);
371 if (!(my_var = (NC_VAR_INFO_T *)ncindexith(my_grp->vars, varid)))
373 assert(my_var && my_var->hdr.id == varid);
400 nc4_find_dim(NC_GRP_INFO_T *grp,
int dimid, NC_DIM_INFO_T **dim,
401 NC_GRP_INFO_T **dim_grp)
403 assert(grp && grp->nc4_info && dim);
404 LOG((4,
"%s: dimid %d", __func__, dimid));
407 if (!((*dim) = nclistget(grp->nc4_info->alldims, dimid)))
412 *dim_grp = (*dim)->container;
428 nc4_find_var(NC_GRP_INFO_T *grp,
const char *name, NC_VAR_INFO_T **var)
430 assert(grp && var && name);
433 *var = (NC_VAR_INFO_T*)ncindexlookup(grp->vars,name);
447 nc4_rec_find_named_type(NC_GRP_INFO_T *start_grp,
char *name)
450 NC_TYPE_INFO_T *type, *res;
456 type = (NC_TYPE_INFO_T*)ncindexlookup(start_grp->type,name);
461 for(i=0;i<ncindexsize(start_grp->children);i++) {
462 g = (NC_GRP_INFO_T*)ncindexith(start_grp->children,i);
463 if(g == NULL)
continue;
464 if ((res = nc4_rec_find_named_type(g, name)))
483 nc4_find_type(
const NC_FILE_INFO_T *h5,
nc_type typeid, NC_TYPE_INFO_T **type)
487 if (
typeid < 0 || !type)
497 if (!(*type = nclistget(h5->alltypes,
typeid)))
519 nc4_find_grp_att(NC_GRP_INFO_T *grp,
int varid,
const char *name,
int attnum,
523 NC_ATT_INFO_T *my_att;
524 NCindex *attlist = NULL;
526 assert(grp && grp->hdr.name && att);
528 LOG((4,
"%s: grp->name %s varid %d attnum %d", __func__, grp->hdr.name,
538 var = (NC_VAR_INFO_T*)ncindexith(grp->vars,varid);
548 my_att = (NC_ATT_INFO_T *)ncindexlookup(attlist, name);
550 my_att = (NC_ATT_INFO_T *)ncindexith(attlist, attnum);
576 nc4_find_nc_att(
int ncid,
int varid,
const char *name,
int attnum,
582 LOG((4,
"nc4_find_nc_att: ncid 0x%x varid %d name %s attnum %d",
583 ncid, varid, name, attnum));
586 if ((retval = nc4_find_grp_h5(ncid, &grp, NULL)))
590 return nc4_find_grp_att(grp, varid, name, attnum, att);
602 obj_track(NC_FILE_INFO_T* file, NC_OBJ* obj)
607 case NCDIM: list = file->alldims;
break;
608 case NCTYP: list = file->alltypes;
break;
609 case NCGRP: list = file->allgroups;
break;
614 nclistset(list,obj->id,obj);
632 nc4_var_list_add2(NC_GRP_INFO_T *grp,
const char *name, NC_VAR_INFO_T **var)
634 NC_VAR_INFO_T *new_var = NULL;
637 if (!(new_var = calloc(1,
sizeof(NC_VAR_INFO_T))))
639 new_var->hdr.sort = NCVAR;
640 new_var->container = grp;
648 new_var->hdr.id = ncindexsize(grp->vars);
649 if (!(new_var->hdr.name = strdup(name))) {
655 new_var->hdr.hashkey = NC_hashmapkey(new_var->hdr.name,
656 strlen(new_var->hdr.name));
659 new_var->att = ncindexnew(0);
662 ncindexadd(grp->vars, (NC_OBJ *)new_var);
684 nc4_var_set_ndims(NC_VAR_INFO_T *var,
int ndims)
694 if (!(var->dim = calloc(ndims,
sizeof(NC_DIM_INFO_T *))))
696 if (!(var->dimids = calloc(ndims,
sizeof(
int))))
701 memset(var->dimids, -1, ndims *
sizeof(
int));
722 nc4_var_list_add(NC_GRP_INFO_T* grp,
const char* name,
int ndims,
727 if ((retval = nc4_var_list_add2(grp, name, var)))
729 if ((retval = nc4_var_set_ndims(*var, ndims)))
749 nc4_dim_list_add(NC_GRP_INFO_T *grp,
const char *name,
size_t len,
750 int assignedid, NC_DIM_INFO_T **dim)
752 NC_DIM_INFO_T *new_dim = NULL;
757 if (!(new_dim = calloc(1,
sizeof(NC_DIM_INFO_T))))
760 new_dim->hdr.sort = NCDIM;
764 new_dim->hdr.id = assignedid;
766 new_dim->hdr.id = grp->nc4_info->next_dimid++;
769 if (!(new_dim->hdr.name = strdup(name))) {
775 new_dim->hdr.hashkey = NC_hashmapkey(new_dim->hdr.name,
776 strlen(new_dim->hdr.name));
781 new_dim->unlimited = NC_TRUE;
784 new_dim->container = grp;
787 ncindexadd(grp->dim, (NC_OBJ *)new_dim);
788 obj_track(grp->nc4_info, (NC_OBJ *)new_dim);
810 nc4_att_list_add(NCindex *list,
const char *name, NC_ATT_INFO_T **att)
812 NC_ATT_INFO_T *new_att = NULL;
814 LOG((3,
"%s: name %s ", __func__, name));
816 if (!(new_att = calloc(1,
sizeof(NC_ATT_INFO_T))))
818 new_att->hdr.sort = NCATT;
821 new_att->hdr.id = ncindexsize(list);
822 if (!(new_att->hdr.name = strdup(name))) {
828 new_att->hdr.hashkey = NC_hashmapkey(name, strlen(name));
831 ncindexadd(list, (NC_OBJ *)new_att);
855 nc4_grp_list_add(NC_FILE_INFO_T *h5, NC_GRP_INFO_T *parent,
char *name,
858 NC_GRP_INFO_T *new_grp;
862 LOG((3,
"%s: name %s ", __func__, name));
865 if (!(new_grp = calloc(1,
sizeof(NC_GRP_INFO_T))))
869 new_grp->hdr.sort = NCGRP;
870 new_grp->nc4_info = h5;
871 new_grp->parent = parent;
874 new_grp->hdr.id = h5->next_nc_grpid++;
875 assert(parent || !new_grp->hdr.id);
878 if (!(new_grp->hdr.name = strdup(name)))
883 new_grp->hdr.hashkey = NC_hashmapkey(new_grp->hdr.name,
884 strlen(new_grp->hdr.name));
887 new_grp->children = ncindexnew(0);
888 new_grp->dim = ncindexnew(0);
889 new_grp->att = ncindexnew(0);
890 new_grp->type = ncindexnew(0);
891 new_grp->vars = ncindexnew(0);
895 ncindexadd(parent->children, (NC_OBJ *)new_grp);
896 obj_track(h5, (NC_OBJ *)new_grp);
919 nc4_check_dup_name(NC_GRP_INFO_T *grp,
char *name)
921 NC_TYPE_INFO_T *type;
926 type = (NC_TYPE_INFO_T*)ncindexlookup(grp->type,name);
931 g = (NC_GRP_INFO_T*)ncindexlookup(grp->children,name);
936 var = (NC_VAR_INFO_T*)ncindexlookup(grp->vars,name);
957 nc4_type_new(
size_t size,
const char *name,
int assignedid,
958 NC_TYPE_INFO_T **type)
960 NC_TYPE_INFO_T *new_type;
962 LOG((4,
"%s: size %d name %s assignedid %d", __func__, size, name, assignedid));
968 if (!(new_type = calloc(1,
sizeof(NC_TYPE_INFO_T))))
970 new_type->hdr.sort = NCTYP;
973 new_type->hdr.id = assignedid;
974 new_type->size = size;
975 if (!(new_type->hdr.name = strdup(name))) {
980 new_type->hdr.hashkey = NC_hashmapkey(name, strlen(name));
1002 nc4_type_list_add(NC_GRP_INFO_T *grp,
size_t size,
const char *name,
1003 NC_TYPE_INFO_T **type)
1005 NC_TYPE_INFO_T *new_type;
1009 assert(grp && name && type);
1010 LOG((4,
"%s: size %d name %s", __func__, size, name));
1013 if ((retval = nc4_type_new(size, name, grp->nc4_info->next_typeid,
1016 grp->nc4_info->next_typeid++;
1022 ncindexadd(grp->type, (NC_OBJ *)new_type);
1023 obj_track(grp->nc4_info,(NC_OBJ*)new_type);
1045 nc4_field_list_add(NC_TYPE_INFO_T *parent,
const char *name,
1046 size_t offset,
nc_type xtype,
int ndims,
1047 const int *dim_sizesp)
1049 NC_FIELD_INFO_T *field;
1056 if (!(field = calloc(1,
sizeof(NC_FIELD_INFO_T))))
1058 field->hdr.sort = NCFLD;
1061 if (!(field->hdr.name = strdup(name)))
1066 field->hdr.hashkey = NC_hashmapkey(field->hdr.name,strlen(field->hdr.name));
1067 field->nc_typeid = xtype;
1068 field->offset = offset;
1069 field->ndims = ndims;
1073 if (!(field->dim_size = malloc(ndims *
sizeof(
int))))
1075 free(field->hdr.name);
1079 for (i = 0; i < ndims; i++)
1080 field->dim_size[i] = dim_sizesp[i];
1084 field->hdr.id = nclistlength(parent->u.c.field);
1085 nclistpush(parent->u.c.field,field);
1103 nc4_enum_member_add(NC_TYPE_INFO_T *parent,
size_t size,
1104 const char *name,
const void *value)
1106 NC_ENUM_MEMBER_INFO_T *member;
1109 assert(name && size > 0 && value);
1110 LOG((4,
"%s: size %d name %s", __func__, size, name));
1113 if (!(member = calloc(1,
sizeof(NC_ENUM_MEMBER_INFO_T))))
1115 if (!(member->value = malloc(size))) {
1119 if (!(member->name = strdup(name))) {
1120 free(member->value);
1126 memcpy(member->value, value, size);
1129 nclistpush(parent->u.e.enum_member,member);
1143 field_free(NC_FIELD_INFO_T *field)
1146 if (field->hdr.name)
1147 free(field->hdr.name);
1148 if (field->dim_size)
1149 free(field->dim_size);
1165 nc4_type_free(NC_TYPE_INFO_T *type)
1169 assert(type && type->rc && type->hdr.name);
1177 LOG((4,
"%s: deleting type %s", __func__, type->hdr.name));
1180 free(type->hdr.name);
1183 switch (type->nc_type_class)
1187 NC_FIELD_INFO_T *field;
1191 for(i=0;i<nclistlength(type->u.c.field);i++) {
1192 field = nclistget(type->u.c.field,i);
1195 nclistfree(type->u.c.field);
1201 NC_ENUM_MEMBER_INFO_T *enum_member;
1204 for(i=0;i<nclistlength(type->u.e.enum_member);i++) {
1205 enum_member = nclistget(type->u.e.enum_member,i);
1206 free(enum_member->value);
1207 free(enum_member->name);
1210 nclistfree(type->u.e.enum_member);
1219 if (type->format_type_info)
1220 free(type->format_type_info);
1238 att_free(NC_ATT_INFO_T *att)
1243 LOG((3,
"%s: name %s ", __func__, att->hdr.name));
1252 free(att->hdr.name);
1262 for (i = 0; i < att->len; i++)
1264 free(att->stdata[i]);
1271 for (i = 0; i < att->len; i++)
1278 if (att->format_att_info)
1279 free(att->format_att_info);
1295 var_free(NC_VAR_INFO_T *var)
1301 LOG((4,
"%s: deleting var %s", __func__, var->hdr.name));
1304 for (i = 0; i < ncindexsize(var->att); i++)
1305 if ((retval = att_free((NC_ATT_INFO_T *)ncindexith(var->att, i))))
1307 ncindexfree(var->att);
1310 if (var->chunksizes)
1311 free(var->chunksizes);
1314 free(var->hdf5_name);
1317 free(var->hdr.name);
1326 if (var->fill_value)
1327 free(var->fill_value);
1331 if ((retval = nc4_type_free(var->type_info)))
1335 if (var->dimscale_attached)
1336 free(var->dimscale_attached);
1343 if (var->format_var_info)
1344 free(var->format_var_info);
1362 nc4_var_list_del(NC_GRP_INFO_T *grp, NC_VAR_INFO_T *var)
1369 i = ncindexfind(grp->vars, (NC_OBJ *)var);
1371 ncindexidel(grp->vars, i);
1373 return var_free(var);
1385 dim_free(NC_DIM_INFO_T *dim)
1388 LOG((4,
"%s: deleting dim %s", __func__, dim->hdr.name));
1392 free(dim->hdr.name);
1395 if (dim->format_dim_info)
1396 free(dim->format_dim_info);
1412 nc4_dim_list_del(NC_GRP_INFO_T *grp, NC_DIM_INFO_T *dim)
1416 int pos = ncindexfind(grp->dim, (NC_OBJ *)dim);
1418 ncindexidel(grp->dim, pos);
1421 return dim_free(dim);
1434 nc4_rec_grp_del(NC_GRP_INFO_T *grp)
1440 LOG((3,
"%s: grp->name %s", __func__, grp->hdr.name));
1444 for (i = 0; i < ncindexsize(grp->children); i++)
1445 if ((retval = nc4_rec_grp_del((NC_GRP_INFO_T *)ncindexith(grp->children,
1448 ncindexfree(grp->children);
1451 for (i = 0; i < ncindexsize(grp->att); i++)
1452 if ((retval = att_free((NC_ATT_INFO_T *)ncindexith(grp->att, i))))
1454 ncindexfree(grp->att);
1457 for (i = 0; i < ncindexsize(grp->vars); i++)
1458 if ((retval = var_free((NC_VAR_INFO_T *)ncindexith(grp->vars, i))))
1460 ncindexfree(grp->vars);
1463 for (i = 0; i < ncindexsize(grp->dim); i++)
1464 if ((retval = dim_free((NC_DIM_INFO_T *)ncindexith(grp->dim, i))))
1466 ncindexfree(grp->dim);
1469 for (i = 0; i < ncindexsize(grp->type); i++)
1470 if ((retval = nc4_type_free((NC_TYPE_INFO_T *)ncindexith(grp->type, i))))
1472 ncindexfree(grp->type);
1475 free(grp->hdr.name);
1478 if (grp->format_grp_info)
1479 free(grp->format_grp_info);
1498 nc4_att_list_del(NCindex *list, NC_ATT_INFO_T *att)
1500 assert(att && list);
1501 ncindexidel(list, ((NC_OBJ *)att)->
id);
1502 return att_free(att);
1519 nc4_file_list_del(
int ncid)
1525 if ((retval = nc4_find_grp_h5(ncid, NULL, &h5)))
1530 if ((retval = nc4_nc4f_list_del(h5)))
1546 nc4_nc4f_list_del(NC_FILE_INFO_T *h5)
1554 if ((retval = nc4_rec_grp_del(h5->root_grp)))
1558 nclistfree(h5->alldims);
1559 nclistfree(h5->allgroups);
1560 nclistfree(h5->alltypes);
1582 nc4_normalize_name(
const char *name,
char *norm_name)
1585 int stat = nc_utf8_normalize((
const unsigned char *)name,(
unsigned char **)&temp_name);
1593 strcpy(norm_name, temp_name);
1598 #ifdef ENABLE_SET_LOG_LEVEL
1615 nc_set_log_level(
int new_level)
1619 nc_log_level = new_level;
1620 LOG((4,
"log_level changed to %d", nc_log_level));
1627 #define MAX_NESTS 10
1638 rec_print_metadata(NC_GRP_INFO_T *grp,
int tab_count)
1643 NC_TYPE_INFO_T *type;
1644 NC_FIELD_INFO_T *field;
1645 char tabs[MAX_NESTS+1] =
"";
1646 char *dims_string = NULL;
1647 char temp_string[10];
1648 int t, retval, d, i;
1651 for (t = 0; t < tab_count && t < MAX_NESTS; t++)
1655 LOG((2,
"%s GROUP - %s nc_grpid: %d nvars: %d natts: %d",
1656 tabs, grp->hdr.name, grp->hdr.id, ncindexsize(grp->vars), ncindexsize(grp->att)));
1658 for (i = 0; i < ncindexsize(grp->att); i++)
1660 att = (NC_ATT_INFO_T *)ncindexith(grp->att, i);
1662 LOG((2,
"%s GROUP ATTRIBUTE - attnum: %d name: %s type: %d len: %d",
1663 tabs, att->hdr.id, att->hdr.name, att->nc_typeid, att->len));
1666 for (i = 0; i < ncindexsize(grp->dim); i++)
1668 dim = (NC_DIM_INFO_T *)ncindexith(grp->dim, i);
1670 LOG((2,
"%s DIMENSION - dimid: %d name: %s len: %d unlimited: %d",
1671 tabs, dim->hdr.id, dim->hdr.name, dim->len, dim->unlimited));
1674 for (i = 0; i < ncindexsize(grp->vars); i++)
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 LOG((2,
"%s VARIABLE - varid: %d name: %s ndims: %d dimscale: %d dimids:%s",
1691 tabs, var->hdr.id, var->hdr.name, var->ndims, (
int)var->dimscale,
1692 (dims_string ? dims_string :
" -")));
1693 for (j = 0; j < ncindexsize(var->att); j++)
1695 att = (NC_ATT_INFO_T *)ncindexith(var->att, j);
1697 LOG((2,
"%s VAR ATTRIBUTE - attnum: %d name: %s type: %d len: %d",
1698 tabs, att->hdr.id, att->hdr.name, att->nc_typeid, att->len));
1704 for (i = 0; i < ncindexsize(grp->type); i++)
1706 type = (NC_TYPE_INFO_T*)ncindexith(grp->type, i);
1708 LOG((2,
"%s TYPE - nc_typeid: %d size: %d committed: %d name: %s",
1709 tabs, type->hdr.id, type->size, (
int)type->committed, type->hdr.name));
1714 LOG((3,
"compound type"));
1715 for (j = 0; j < nclistlength(type->u.c.field); j++)
1717 field = (NC_FIELD_INFO_T *)nclistget(type->u.c.field, j);
1718 LOG((4,
"field %s offset %d nctype %d ndims %d", field->hdr.name,
1719 field->offset, field->nc_typeid, field->ndims));
1722 else if (type->nc_type_class ==
NC_VLEN)
1724 LOG((3,
"VLEN type"));
1725 LOG((4,
"base_nc_type: %d", type->u.v.base_nc_typeid));
1727 else if (type->nc_type_class ==
NC_OPAQUE)
1728 LOG((3,
"Opaque type"));
1729 else if (type->nc_type_class ==
NC_ENUM)
1731 LOG((3,
"Enum type"));
1732 LOG((4,
"base_nc_type: %d", type->u.e.base_nc_typeid));
1736 LOG((0,
"Unknown class: %d", type->nc_type_class));
1742 for (i = 0; i < ncindexsize(grp->children); i++)
1743 if ((retval = rec_print_metadata((NC_GRP_INFO_T *)ncindexith(grp->children, i),
1761 log_metadata_nc(NC_FILE_INFO_T *h5)
1763 LOG((2,
"*** NetCDF-4 Internal Metadata: int_ncid 0x%x ext_ncid 0x%x",
1764 h5->root_grp->nc4_info->controller->int_ncid,
1765 h5->root_grp->nc4_info->controller->ext_ncid));
1768 LOG((2,
"This is a netCDF-3 file."));
1771 LOG((2,
"FILE - path: %s cmode: 0x%x parallel: %d redef: %d "
1772 "fill_mode: %d no_write: %d next_nc_grpid: %d", h5->root_grp->nc4_info->controller->path,
1773 h5->cmode, (
int)h5->parallel, (
int)h5->redef, h5->fill_mode, (
int)h5->no_write,
1774 h5->next_nc_grpid));
1775 if(nc_log_level >= 2)
1776 return rec_print_metadata(h5->root_grp, 0);
1794 NC4_show_metadata(
int ncid)
1799 int old_log_level = nc_log_level;
1802 if ((retval = nc4_find_grp_h5(ncid, NULL, &h5)))
1807 retval = log_metadata_nc(h5);
1808 nc_log_level = old_log_level;