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;
173 nc4_file_list_get(
int ncid,
char **path,
int *mode,
void **dispatchdata)
179 if ((ret = NC_check_id(ncid, &nc)))
192 *dispatchdata = nc->dispatchdata;
212 nc4_nc4f_list_add(NC *nc,
const char *path,
int mode)
217 assert(nc && !NC4_DATA(nc) && path);
221 if (!(h5 = calloc(1,
sizeof(NC_FILE_INFO_T))))
223 nc->dispatchdata = h5;
227 h5->cmode = mode | NC_INDEF;
231 h5->next_typeid = NC_FIRSTUSERTYPEID;
234 h5->alldims = nclistnew();
235 h5->alltypes = nclistnew();
236 h5->allgroups = nclistnew();
241 if ((retval = nc4_grp_list_add(h5, NULL, NC_GROUP_NAME, &h5->root_grp)))
260 nc4_find_nc4_grp(
int ncid, NC_GRP_INFO_T **grp)
262 return nc4_find_nc_grp_h5(ncid, NULL, grp, NULL);
281 nc4_find_grp_h5(
int ncid, NC_GRP_INFO_T **grp, NC_FILE_INFO_T **h5)
283 return nc4_find_nc_grp_h5(ncid, NULL, grp, h5);
301 nc4_find_nc_grp_h5(
int ncid, NC **nc, NC_GRP_INFO_T **grp, NC_FILE_INFO_T **h5)
303 NC_GRP_INFO_T *my_grp = NULL;
304 NC_FILE_INFO_T *my_h5 = NULL;
309 if ((retval = NC_check_id(ncid, &my_nc)))
311 my_h5 = my_nc->dispatchdata;
312 assert(my_h5 && my_h5->root_grp);
315 if (!(my_grp = nclistget(my_h5->allgroups, (ncid & GRP_ID_MASK))))
345 nc4_find_grp_h5_var(
int ncid,
int varid, NC_FILE_INFO_T **h5, NC_GRP_INFO_T **grp,
348 NC_FILE_INFO_T *my_h5;
349 NC_GRP_INFO_T *my_grp;
350 NC_VAR_INFO_T *my_var;
354 if ((retval = nc4_find_grp_h5(ncid, &my_grp, &my_h5)))
356 assert(my_grp && my_h5);
359 if (!(my_var = (NC_VAR_INFO_T *)ncindexith(my_grp->vars, varid)))
361 assert(my_var && my_var->hdr.id == varid);
388 nc4_find_dim(NC_GRP_INFO_T *grp,
int dimid, NC_DIM_INFO_T **dim,
389 NC_GRP_INFO_T **dim_grp)
391 assert(grp && grp->nc4_info && dim);
392 LOG((4,
"%s: dimid %d", __func__, dimid));
395 if (!((*dim) = nclistget(grp->nc4_info->alldims, dimid)))
400 *dim_grp = (*dim)->container;
416 nc4_find_var(NC_GRP_INFO_T *grp,
const char *name, NC_VAR_INFO_T **var)
418 assert(grp && var && name);
421 *var = (NC_VAR_INFO_T*)ncindexlookup(grp->vars,name);
435 nc4_rec_find_named_type(NC_GRP_INFO_T *start_grp,
char *name)
438 NC_TYPE_INFO_T *type, *res;
444 type = (NC_TYPE_INFO_T*)ncindexlookup(start_grp->type,name);
449 for(i=0;i<ncindexsize(start_grp->children);i++) {
450 g = (NC_GRP_INFO_T*)ncindexith(start_grp->children,i);
451 if(g == NULL)
continue;
452 if ((res = nc4_rec_find_named_type(g, name)))
471 nc4_find_type(
const NC_FILE_INFO_T *h5,
nc_type typeid, NC_TYPE_INFO_T **type)
475 if (
typeid < 0 || !type)
485 if (!(*type = nclistget(h5->alltypes,
typeid)))
507 nc4_find_grp_att(NC_GRP_INFO_T *grp,
int varid,
const char *name,
int attnum,
511 NC_ATT_INFO_T *my_att;
512 NCindex *attlist = NULL;
514 assert(grp && grp->hdr.name && att);
516 LOG((4,
"%s: grp->name %s varid %d attnum %d", __func__, grp->hdr.name,
526 var = (NC_VAR_INFO_T*)ncindexith(grp->vars,varid);
536 my_att = (NC_ATT_INFO_T *)ncindexlookup(attlist, name);
538 my_att = (NC_ATT_INFO_T *)ncindexith(attlist, attnum);
564 nc4_find_nc_att(
int ncid,
int varid,
const char *name,
int attnum,
570 LOG((4,
"nc4_find_nc_att: ncid 0x%x varid %d name %s attnum %d",
571 ncid, varid, name, attnum));
574 if ((retval = nc4_find_grp_h5(ncid, &grp, NULL)))
578 return nc4_find_grp_att(grp, varid, name, attnum, att);
590 obj_track(NC_FILE_INFO_T* file, NC_OBJ* obj)
595 case NCDIM: list = file->alldims;
break;
596 case NCTYP: list = file->alltypes;
break;
597 case NCGRP: list = file->allgroups;
break;
602 nclistset(list,obj->id,obj);
620 nc4_var_list_add2(NC_GRP_INFO_T *grp,
const char *name, NC_VAR_INFO_T **var)
622 NC_VAR_INFO_T *new_var;
625 if (!(new_var = calloc(1,
sizeof(NC_VAR_INFO_T))))
627 new_var->hdr.sort = NCVAR;
628 new_var->container = grp;
636 new_var->hdr.id = ncindexsize(grp->vars);
637 if (!(new_var->hdr.name = strdup(name)))
639 new_var->hdr.hashkey = NC_hashmapkey(new_var->hdr.name,
640 strlen(new_var->hdr.name));
643 new_var->att = ncindexnew(0);
646 ncindexadd(grp->vars, (NC_OBJ *)new_var);
668 nc4_var_set_ndims(NC_VAR_INFO_T *var,
int ndims)
678 if (!(var->dim = calloc(ndims,
sizeof(NC_DIM_INFO_T *))))
680 if (!(var->dimids = calloc(ndims,
sizeof(
int))))
685 memset(var->dimids, -1, ndims *
sizeof(
int));
706 nc4_var_list_add(NC_GRP_INFO_T* grp,
const char* name,
int ndims,
711 if ((retval = nc4_var_list_add2(grp, name, var)))
713 if ((retval = nc4_var_set_ndims(*var, ndims)))
733 nc4_dim_list_add(NC_GRP_INFO_T *grp,
const char *name,
size_t len,
734 int assignedid, NC_DIM_INFO_T **dim)
736 NC_DIM_INFO_T *new_dim;
741 if (!(new_dim = calloc(1,
sizeof(NC_DIM_INFO_T))))
744 new_dim->hdr.sort = NCDIM;
748 new_dim->hdr.id = assignedid;
750 new_dim->hdr.id = grp->nc4_info->next_dimid++;
753 if (!(new_dim->hdr.name = strdup(name)))
755 new_dim->hdr.hashkey = NC_hashmapkey(new_dim->hdr.name,
756 strlen(new_dim->hdr.name));
761 new_dim->unlimited = NC_TRUE;
764 new_dim->container = grp;
767 ncindexadd(grp->dim, (NC_OBJ *)new_dim);
768 obj_track(grp->nc4_info, (NC_OBJ *)new_dim);
790 nc4_att_list_add(NCindex *list,
const char *name, NC_ATT_INFO_T **att)
792 NC_ATT_INFO_T *new_att;
794 LOG((3,
"%s: name %s ", __func__, name));
796 if (!(new_att = calloc(1,
sizeof(NC_ATT_INFO_T))))
798 new_att->hdr.sort = NCATT;
801 new_att->hdr.id = ncindexsize(list);
802 if (!(new_att->hdr.name = strdup(name)))
806 new_att->hdr.hashkey = NC_hashmapkey(name, strlen(name));
809 ncindexadd(list, (NC_OBJ *)new_att);
833 nc4_grp_list_add(NC_FILE_INFO_T *h5, NC_GRP_INFO_T *parent,
char *name,
836 NC_GRP_INFO_T *new_grp;
840 LOG((3,
"%s: name %s ", __func__, name));
843 if (!(new_grp = calloc(1,
sizeof(NC_GRP_INFO_T))))
847 new_grp->hdr.sort = NCGRP;
848 new_grp->nc4_info = h5;
849 new_grp->parent = parent;
852 new_grp->hdr.id = h5->next_nc_grpid++;
853 assert(parent || !new_grp->hdr.id);
856 if (!(new_grp->hdr.name = strdup(name)))
861 new_grp->hdr.hashkey = NC_hashmapkey(new_grp->hdr.name,
862 strlen(new_grp->hdr.name));
865 new_grp->children = ncindexnew(0);
866 new_grp->dim = ncindexnew(0);
867 new_grp->att = ncindexnew(0);
868 new_grp->type = ncindexnew(0);
869 new_grp->vars = ncindexnew(0);
873 ncindexadd(parent->children, (NC_OBJ *)new_grp);
874 obj_track(h5, (NC_OBJ *)new_grp);
897 nc4_check_dup_name(NC_GRP_INFO_T *grp,
char *name)
899 NC_TYPE_INFO_T *type;
904 type = (NC_TYPE_INFO_T*)ncindexlookup(grp->type,name);
909 g = (NC_GRP_INFO_T*)ncindexlookup(grp->children,name);
914 var = (NC_VAR_INFO_T*)ncindexlookup(grp->vars,name);
935 nc4_type_new(
size_t size,
const char *name,
int assignedid,
936 NC_TYPE_INFO_T **type)
938 NC_TYPE_INFO_T *new_type;
940 LOG((4,
"%s: size %d name %s assignedid %d", __func__, size, name, assignedid));
946 if (!(new_type = calloc(1,
sizeof(NC_TYPE_INFO_T))))
948 new_type->hdr.sort = NCTYP;
951 new_type->hdr.id = assignedid;
952 new_type->size = size;
953 if (!(new_type->hdr.name = strdup(name))) {
958 new_type->hdr.hashkey = NC_hashmapkey(name, strlen(name));
980 nc4_type_list_add(NC_GRP_INFO_T *grp,
size_t size,
const char *name,
981 NC_TYPE_INFO_T **type)
983 NC_TYPE_INFO_T *new_type;
987 assert(grp && name && type);
988 LOG((4,
"%s: size %d name %s", __func__, size, name));
991 if ((retval = nc4_type_new(size, name, grp->nc4_info->next_typeid,
994 grp->nc4_info->next_typeid++;
1000 ncindexadd(grp->type, (NC_OBJ *)new_type);
1001 obj_track(grp->nc4_info,(NC_OBJ*)new_type);
1023 nc4_field_list_add(NC_TYPE_INFO_T *parent,
const char *name,
1024 size_t offset,
nc_type xtype,
int ndims,
1025 const int *dim_sizesp)
1027 NC_FIELD_INFO_T *field;
1034 if (!(field = calloc(1,
sizeof(NC_FIELD_INFO_T))))
1036 field->hdr.sort = NCFLD;
1039 if (!(field->hdr.name = strdup(name)))
1044 field->hdr.hashkey = NC_hashmapkey(field->hdr.name,strlen(field->hdr.name));
1045 field->nc_typeid = xtype;
1046 field->offset = offset;
1047 field->ndims = ndims;
1051 if (!(field->dim_size = malloc(ndims *
sizeof(
int))))
1053 free(field->hdr.name);
1057 for (i = 0; i < ndims; i++)
1058 field->dim_size[i] = dim_sizesp[i];
1062 field->hdr.id = nclistlength(parent->u.c.field);
1063 nclistpush(parent->u.c.field,field);
1081 nc4_enum_member_add(NC_TYPE_INFO_T *parent,
size_t size,
1082 const char *name,
const void *value)
1084 NC_ENUM_MEMBER_INFO_T *member;
1087 assert(name && size > 0 && value);
1088 LOG((4,
"%s: size %d name %s", __func__, size, name));
1091 if (!(member = calloc(1,
sizeof(NC_ENUM_MEMBER_INFO_T))))
1093 if (!(member->value = malloc(size))) {
1097 if (!(member->name = strdup(name))) {
1098 free(member->value);
1104 memcpy(member->value, value, size);
1107 nclistpush(parent->u.e.enum_member,member);
1121 field_free(NC_FIELD_INFO_T *field)
1124 if (field->hdr.name)
1125 free(field->hdr.name);
1126 if (field->dim_size)
1127 free(field->dim_size);
1143 nc4_type_free(NC_TYPE_INFO_T *type)
1147 assert(type && type->rc && type->hdr.name);
1155 LOG((4,
"%s: deleting type %s", __func__, type->hdr.name));
1158 free(type->hdr.name);
1161 switch (type->nc_type_class)
1165 NC_FIELD_INFO_T *field;
1169 for(i=0;i<nclistlength(type->u.c.field);i++) {
1170 field = nclistget(type->u.c.field,i);
1173 nclistfree(type->u.c.field);
1179 NC_ENUM_MEMBER_INFO_T *enum_member;
1182 for(i=0;i<nclistlength(type->u.e.enum_member);i++) {
1183 enum_member = nclistget(type->u.e.enum_member,i);
1184 free(enum_member->value);
1185 free(enum_member->name);
1188 nclistfree(type->u.e.enum_member);
1197 if (type->format_type_info)
1198 free(type->format_type_info);
1216 att_free(NC_ATT_INFO_T *att)
1221 LOG((3,
"%s: name %s ", __func__, att->hdr.name));
1230 free(att->hdr.name);
1240 for (i = 0; i < att->len; i++)
1242 free(att->stdata[i]);
1249 for (i = 0; i < att->len; i++)
1256 if (att->format_att_info)
1257 free(att->format_att_info);
1273 var_free(NC_VAR_INFO_T *var)
1279 LOG((4,
"%s: deleting var %s", __func__, var->hdr.name));
1282 for (i = 0; i < ncindexsize(var->att); i++)
1283 if ((retval = att_free((NC_ATT_INFO_T *)ncindexith(var->att, i))))
1285 ncindexfree(var->att);
1288 if (var->chunksizes)
1289 free(var->chunksizes);
1292 free(var->hdf5_name);
1295 free(var->hdr.name);
1304 if (var->fill_value)
1305 free(var->fill_value);
1309 if ((retval = nc4_type_free(var->type_info)))
1313 if (var->dimscale_attached)
1314 free(var->dimscale_attached);
1321 if (var->format_var_info)
1322 free(var->format_var_info);
1340 nc4_var_list_del(NC_GRP_INFO_T *grp, NC_VAR_INFO_T *var)
1347 i = ncindexfind(grp->vars, (NC_OBJ *)var);
1349 ncindexidel(grp->vars, i);
1351 return var_free(var);
1363 dim_free(NC_DIM_INFO_T *dim)
1366 LOG((4,
"%s: deleting dim %s", __func__, dim->hdr.name));
1370 free(dim->hdr.name);
1373 if (dim->format_dim_info)
1374 free(dim->format_dim_info);
1390 nc4_dim_list_del(NC_GRP_INFO_T *grp, NC_DIM_INFO_T *dim)
1394 int pos = ncindexfind(grp->dim, (NC_OBJ *)dim);
1396 ncindexidel(grp->dim, pos);
1399 return dim_free(dim);
1412 nc4_rec_grp_del(NC_GRP_INFO_T *grp)
1418 LOG((3,
"%s: grp->name %s", __func__, grp->hdr.name));
1422 for (i = 0; i < ncindexsize(grp->children); i++)
1423 if ((retval = nc4_rec_grp_del((NC_GRP_INFO_T *)ncindexith(grp->children,
1426 ncindexfree(grp->children);
1429 for (i = 0; i < ncindexsize(grp->att); i++)
1430 if ((retval = att_free((NC_ATT_INFO_T *)ncindexith(grp->att, i))))
1432 ncindexfree(grp->att);
1435 for (i = 0; i < ncindexsize(grp->vars); i++)
1436 if ((retval = var_free((NC_VAR_INFO_T *)ncindexith(grp->vars, i))))
1438 ncindexfree(grp->vars);
1441 for (i = 0; i < ncindexsize(grp->dim); i++)
1442 if ((retval = dim_free((NC_DIM_INFO_T *)ncindexith(grp->dim, i))))
1444 ncindexfree(grp->dim);
1447 for (i = 0; i < ncindexsize(grp->type); i++)
1448 if ((retval = nc4_type_free((NC_TYPE_INFO_T *)ncindexith(grp->type, i))))
1450 ncindexfree(grp->type);
1453 free(grp->hdr.name);
1456 if (grp->format_grp_info)
1457 free(grp->format_grp_info);
1476 nc4_att_list_del(NCindex *list, NC_ATT_INFO_T *att)
1478 assert(att && list);
1479 ncindexidel(list, ((NC_OBJ *)att)->
id);
1480 return att_free(att);
1497 nc4_file_list_del(
int ncid)
1503 if ((retval = nc4_find_grp_h5(ncid, NULL, &h5)))
1508 if ((retval = nc4_nc4f_list_del(h5)))
1524 nc4_nc4f_list_del(NC_FILE_INFO_T *h5)
1532 if ((retval = nc4_rec_grp_del(h5->root_grp)))
1536 nclistfree(h5->alldims);
1537 nclistfree(h5->allgroups);
1538 nclistfree(h5->alltypes);
1560 nc4_normalize_name(
const char *name,
char *norm_name)
1563 int stat = nc_utf8_normalize((
const unsigned char *)name,(
unsigned char **)&temp_name);
1571 strcpy(norm_name, temp_name);
1576 #ifdef ENABLE_SET_LOG_LEVEL 1593 nc_set_log_level(
int new_level)
1597 nc_log_level = new_level;
1598 LOG((4,
"log_level changed to %d", nc_log_level));
1605 #define MAX_NESTS 10 1616 rec_print_metadata(NC_GRP_INFO_T *grp,
int tab_count)
1621 NC_TYPE_INFO_T *type;
1622 NC_FIELD_INFO_T *field;
1623 char tabs[MAX_NESTS+1] =
"";
1624 char *dims_string = NULL;
1625 char temp_string[10];
1626 int t, retval, d, i;
1629 for (t = 0; t < tab_count && t < MAX_NESTS; t++)
1633 LOG((2,
"%s GROUP - %s nc_grpid: %d nvars: %d natts: %d",
1634 tabs, grp->hdr.name, grp->hdr.id, ncindexsize(grp->vars), ncindexsize(grp->att)));
1636 for (i = 0; i < ncindexsize(grp->att); i++)
1638 att = (NC_ATT_INFO_T *)ncindexith(grp->att, i);
1640 LOG((2,
"%s GROUP ATTRIBUTE - attnum: %d name: %s type: %d len: %d",
1641 tabs, att->hdr.id, att->hdr.name, att->nc_typeid, att->len));
1644 for (i = 0; i < ncindexsize(grp->dim); i++)
1646 dim = (NC_DIM_INFO_T *)ncindexith(grp->dim, i);
1648 LOG((2,
"%s DIMENSION - dimid: %d name: %s len: %d unlimited: %d",
1649 tabs, dim->hdr.id, dim->hdr.name, dim->len, dim->unlimited));
1652 for (i = 0; i < ncindexsize(grp->vars); i++)
1655 var = (NC_VAR_INFO_T*)ncindexith(grp->vars,i);
1659 if (!(dims_string = malloc(
sizeof(
char) * var->ndims * 4)))
1661 strcpy(dims_string,
"");
1662 for (d = 0; d < var->ndims; d++)
1664 sprintf(temp_string,
" %d", var->dimids[d]);
1665 strcat(dims_string, temp_string);
1668 LOG((2,
"%s VARIABLE - varid: %d name: %s ndims: %d dimscale: %d dimids:%s",
1669 tabs, var->hdr.id, var->hdr.name, var->ndims, (
int)var->dimscale,
1670 (dims_string ? dims_string :
" -")));
1671 for (j = 0; j < ncindexsize(var->att); j++)
1673 att = (NC_ATT_INFO_T *)ncindexith(var->att, j);
1675 LOG((2,
"%s VAR ATTRIBUTE - attnum: %d name: %s type: %d len: %d",
1676 tabs, att->hdr.id, att->hdr.name, att->nc_typeid, att->len));
1682 for (i = 0; i < ncindexsize(grp->type); i++)
1684 type = (NC_TYPE_INFO_T*)ncindexith(grp->type, i);
1686 LOG((2,
"%s TYPE - nc_typeid: %d size: %d committed: %d name: %s",
1687 tabs, type->hdr.id, type->size, (
int)type->committed, type->hdr.name));
1692 LOG((3,
"compound type"));
1693 for (j = 0; j < nclistlength(type->u.c.field); j++)
1695 field = (NC_FIELD_INFO_T *)nclistget(type->u.c.field, j);
1696 LOG((4,
"field %s offset %d nctype %d ndims %d", field->hdr.name,
1697 field->offset, field->nc_typeid, field->ndims));
1700 else if (type->nc_type_class ==
NC_VLEN)
1702 LOG((3,
"VLEN type"));
1703 LOG((4,
"base_nc_type: %d", type->u.v.base_nc_typeid));
1705 else if (type->nc_type_class ==
NC_OPAQUE)
1706 LOG((3,
"Opaque type"));
1707 else if (type->nc_type_class ==
NC_ENUM)
1709 LOG((3,
"Enum type"));
1710 LOG((4,
"base_nc_type: %d", type->u.e.base_nc_typeid));
1714 LOG((0,
"Unknown class: %d", type->nc_type_class));
1720 for (i = 0; i < ncindexsize(grp->children); i++)
1721 if ((retval = rec_print_metadata((NC_GRP_INFO_T *)ncindexith(grp->children, i),
1739 log_metadata_nc(NC_FILE_INFO_T *h5)
1741 LOG((2,
"*** NetCDF-4 Internal Metadata: int_ncid 0x%x ext_ncid 0x%x",
1742 h5->root_grp->nc4_info->controller->int_ncid,
1743 h5->root_grp->nc4_info->controller->ext_ncid));
1746 LOG((2,
"This is a netCDF-3 file."));
1749 LOG((2,
"FILE - path: %s cmode: 0x%x parallel: %d redef: %d " 1750 "fill_mode: %d no_write: %d next_nc_grpid: %d", h5->root_grp->nc4_info->controller->path,
1751 h5->cmode, (
int)h5->parallel, (
int)h5->redef, h5->fill_mode, (
int)h5->no_write,
1752 h5->next_nc_grpid));
1753 if(nc_log_level >= 2)
1754 return rec_print_metadata(h5->root_grp, 0);
1772 NC4_show_metadata(
int ncid)
1777 int old_log_level = nc_log_level;
1780 if ((retval = nc4_find_grp_h5(ncid, NULL, &h5)))
1785 retval = log_metadata_nc(h5);
1786 nc_log_level = old_log_level;
#define NC_ENOMEM
Memory allocation (malloc) failure.
#define NC_OPAQUE
opaque types
size_t nc4_chunk_cache_nelems
Default chunk cache number of elements.
int nc_type
The nc_type type is just an int.
#define NC_EBADDIM
Invalid dimension id or name.
#define NC_ENAMEINUSE
String match to name in use.
#define NC_VLEN
vlen (variable-length) types
#define NC_EBADTYPE
Not a netcdf data type.
#define NC_EINVAL
Invalid Argument.
#define NC_MAX_NAME
Maximum for classic library.
#define NC_EBADTYPID
Bad type ID.
EXTERNL int nc_free_vlen(nc_vlen_t *vl)
Free memory in a VLEN object.
#define NC_EBADID
Not a netcdf id.
#define NC_UNLIMITED
Size argument to nc_def_dim() for an unlimited dimension.
size_t nc4_chunk_cache_size
Default chunk cache size.
#define NC_ENOTVAR
Variable not found.
#define NC_EMAXNAME
NC_MAX_NAME exceeded.
#define NC_NOERR
No Error.
#define NC_ENUM
enum types
float nc4_chunk_cache_preemption
Default chunk cache preemption.
#define NC_COMPOUND
compound types
#define NC_GLOBAL
Attribute id to put/get a global attribute.
#define NC_ENOTATT
Attribute not found.