15 #include "nc4internal.h" 16 #include "nc4dispatch.h" 17 #include <H5DSpublic.h> 18 #include <H5Fpublic.h> 24 extern int nc4_vararray_add(NC_GRP_INFO_T *grp, NC_VAR_INFO_T *var);
30 #define CD_NELEMS_ZLIB 1 41 hdf5free(
void* memory)
46 #ifdef HDF5_HAS_H5FREE 47 if(memory != NULL) H5free_memory(memory);
50 if(memory != NULL) free(memory);
77 get_netcdf_type(NC_HDF5_FILE_INFO_T *h5, hid_t native_typeid,
82 htri_t is_str, equal = 0;
86 if ((
class = H5Tget_class(native_typeid)) < 0)
91 if (
class == H5T_STRING)
93 if ((is_str = H5Tis_variable_str(native_typeid)) < 0)
101 else if (
class == H5T_INTEGER ||
class == H5T_FLOAT)
105 if ((equal = H5Tequal(native_typeid, H5T_NATIVE_SCHAR)) < 0)
112 if ((equal = H5Tequal(native_typeid, H5T_NATIVE_SHORT)) < 0)
119 if ((equal = H5Tequal(native_typeid, H5T_NATIVE_INT)) < 0)
126 if ((equal = H5Tequal(native_typeid, H5T_NATIVE_FLOAT)) < 0)
133 if ((equal = H5Tequal(native_typeid, H5T_NATIVE_DOUBLE)) < 0)
140 if ((equal = H5Tequal(native_typeid, H5T_NATIVE_UCHAR)) < 0)
147 if ((equal = H5Tequal(native_typeid, H5T_NATIVE_USHORT)) < 0)
154 if ((equal = H5Tequal(native_typeid, H5T_NATIVE_UINT)) < 0)
161 if ((equal = H5Tequal(native_typeid, H5T_NATIVE_LLONG)) < 0)
168 if ((equal = H5Tequal(native_typeid, H5T_NATIVE_ULLONG)) < 0)
179 if((type = nc4_rec_find_hdf_type(h5->root_grp, native_typeid)))
181 *xtype = type->nc_typeid;
201 read_hdf5_att(NC_GRP_INFO_T *grp, hid_t attid, NC_ATT_INFO_T *att)
203 hid_t spaceid = 0, file_typeid = 0;
204 hsize_t dims[1] = {0};
208 hssize_t att_npoints;
209 H5T_class_t att_class;
210 int fixed_len_string = 0;
211 size_t fixed_size = 0;
214 LOG((5,
"%s: att->attnum %d att->name %s att->nc_typeid %d att->len %d",
215 __func__, att->attnum, att->name, (
int)att->nc_typeid, att->len));
218 if ((file_typeid = H5Aget_type(attid)) < 0)
220 if ((att->native_hdf_typeid = H5Tget_native_type(file_typeid, H5T_DIR_DEFAULT)) < 0)
222 if ((att_class = H5Tget_class(att->native_hdf_typeid)) < 0)
224 if (att_class == H5T_STRING && !H5Tis_variable_str(att->native_hdf_typeid))
227 if (!(fixed_size = H5Tget_size(att->native_hdf_typeid)))
230 if ((retval = get_netcdf_type(grp->nc4_info, att->native_hdf_typeid, &(att->nc_typeid))))
235 if ((spaceid = H5Aget_space(attid)) < 0)
237 if ((att_ndims = H5Sget_simple_extent_ndims(spaceid)) < 0)
239 if ((att_npoints = H5Sget_simple_extent_npoints(spaceid)) < 0)
244 if (att_ndims == 0 && att_npoints == 0)
247 dims[0] = att_npoints;
248 else if (att->nc_typeid ==
NC_CHAR)
254 if (!(dims[0] = H5Tget_size(file_typeid)))
261 dims[0] = att_npoints;
266 H5S_class_t space_class;
273 if ((space_class = H5Sget_simple_extent_type(spaceid)) < 0)
277 if (H5S_NULL == space_class)
281 if (H5S_SCALAR == space_class)
286 if (H5Sget_simple_extent_dims(spaceid, dims, NULL) < 0)
298 if ((retval = nc4_get_typelen_mem(grp->nc4_info, att->nc_typeid, 0,
301 if (att_class == H5T_VLEN)
303 if (!(att->vldata = malloc((
unsigned int)(att->len *
sizeof(hvl_t)))))
305 if (H5Aread(attid, att->native_hdf_typeid, att->vldata) < 0)
310 if (!(att->stdata = calloc(att->len,
sizeof(
char *))))
323 if (fixed_len_string)
326 char *contig_buf, *cur;
329 if (!(contig_buf = malloc(att->len * fixed_size *
sizeof(
char))))
333 if (H5Aread(attid, att->native_hdf_typeid, contig_buf) < 0) {
342 for (i = 0; i < att->len; i++)
344 if (!(att->stdata[i] = malloc(fixed_size))) {
348 strncpy(att->stdata[i], cur, fixed_size);
358 if (H5Aread(attid, att->native_hdf_typeid, att->stdata) < 0)
364 if (!(att->data = malloc((
unsigned int)(att->len * type_size))))
366 if (H5Aread(attid, att->native_hdf_typeid, att->data) < 0)
371 if (H5Tclose(file_typeid) < 0)
373 if (H5Sclose(spaceid) < 0)
379 if (H5Tclose(file_typeid) < 0)
381 if (spaceid > 0 && H5Sclose(spaceid) < 0)
401 att_read_var_callbk(hid_t loc_id,
const char *att_name,
const H5A_info_t *ainfo,
void *att_data)
407 att_iter_info *att_info = (att_iter_info *)att_data;
408 const char** reserved;
411 for(reserved=NC_RESERVED_VARATT_LIST;*reserved;reserved++) {
412 if (strcmp(att_name, *reserved)==0)
break;
415 if(*reserved == NULL) {
417 if ((attid = H5Aopen(loc_id, att_name, H5P_DEFAULT)) < 0)
419 LOG((4,
"%s:: att_name %s", __func__, att_name));
421 if ((retval = nc4_att_list_add(&att_info->var->att, &att)))
424 att->attnum = att_info->var->natts++;
425 if (!(att->name = strdup(att_name)))
430 if ((retval = read_hdf5_att(att_info->grp, attid, att)))
434 if ((retval = nc4_att_list_del(&att_info->var->att, att)))
443 att->created = NC_TRUE;
445 if (attid > 0 && H5Aclose(attid) < 0)
453 if (attid > 0 && H5Aclose(attid) < 0)
465 extern void reportopenobjects(
int log, hid_t);
471 typedef struct NC4_rec_read_metadata_obj_info
476 struct NC4_rec_read_metadata_obj_info *next;
477 } NC4_rec_read_metadata_obj_info_t;
484 typedef struct NC4_rec_read_metadata_ud
486 NC4_rec_read_metadata_obj_info_t *grps_head, *grps_tail;
488 } NC4_rec_read_metadata_ud_t;
491 static int NC4_enddef(
int ncid);
492 static int nc4_rec_read_metadata(NC_GRP_INFO_T *grp);
504 sync_netcdf4_file(NC_HDF5_FILE_INFO_T *h5)
509 LOG((3,
"%s", __func__));
513 if (h5->flags & NC_INDEF)
519 h5->flags ^= NC_INDEF;
522 h5->redef = NC_FALSE;
528 log_metadata_nc(h5->root_grp->nc4_info->controller);
534 nc_bool_t bad_coord_order = NC_FALSE;
536 if ((retval = nc4_rec_write_groups_types(h5->root_grp)))
538 if ((retval = nc4_rec_detect_need_to_preserve_dimids(h5->root_grp, &bad_coord_order)))
540 if ((retval = nc4_rec_write_metadata(h5->root_grp, bad_coord_order)))
544 if (H5Fflush(h5->hdfid, H5F_SCOPE_GLOBAL) < 0)
562 close_netcdf4_file(NC_HDF5_FILE_INFO_T *h5,
int abort)
566 assert(h5 && h5->root_grp);
567 LOG((3,
"%s: h5->path %s abort %d", __func__, h5->controller->path, abort));
570 if (h5->flags & NC_INDEF)
571 h5->flags ^= NC_INDEF;
575 if (!h5->no_write && !abort)
576 if ((retval = sync_netcdf4_file(h5)))
581 if ((retval = nc4_rec_grp_del(&h5->root_grp, h5->root_grp)))
598 if(MPI_COMM_NULL != h5->comm)
599 MPI_Comm_free(&h5->comm);
600 if(MPI_INFO_NULL != h5->info)
601 MPI_Info_free(&h5->info);
605 if(h5->fileinfo) free(h5->fileinfo);
607 if (H5Fclose(h5->hdfid) < 0)
611 nobjs = H5Fget_obj_count(h5->hdfid, H5F_OBJ_ALL);
615 }
else if(nobjs > 0) {
623 snprintf(msg,
sizeof(msg),
"There are %d HDF5 objects open!", nobjs);
630 reportopenobjects(logit,h5->hdfid);
638 if(!retval && h5 != NULL)
648 const char* NC_RESERVED_VARATT_LIST[] = {
649 NC_ATT_REFERENCE_LIST,
651 NC_ATT_DIMENSION_LIST,
663 const char* NC_RESERVED_ATT_LIST[] = {
676 const char* NC_RESERVED_SPECIAL_LIST[] = {
692 static hid_t h5_native_type_constant_g[NUM_TYPES]; 696 "int",
"float",
"double",
"ubyte",
697 "ushort",
"uint",
"int64",
707 static const int nc_type_size_g[
NUM_TYPES] = {
sizeof(char),
sizeof(
char),
sizeof(short),
708 sizeof(
int),
sizeof(float),
sizeof(
double),
sizeof(
unsigned char),
709 sizeof(
unsigned short),
sizeof(
unsigned int),
sizeof(
long long),
710 sizeof(
unsigned long long),
sizeof(
char *)};
727 if (preemption < 0 || preemption > 1)
772 nc_set_chunk_cache_ints(
int size,
int nelems,
int preemption)
774 if (size <= 0 || nelems <= 0 || preemption < 0 || preemption > 100)
794 nc_get_chunk_cache_ints(
int *sizep,
int *nelemsp,
int *preemptionp)
854 nc4_create_file(
const char *path,
int cmode, MPI_Comm comm, MPI_Info info,
857 hid_t fcpl_id, fapl_id = -1;
861 NC_HDF5_FILE_INFO_T* nc4_info = NULL;
870 LOG((3,
"%s: path %s mode 0x%x", __func__, path, cmode));
873 flags = H5F_ACC_TRUNC;
875 flags = H5F_ACC_EXCL;
877 flags = H5F_ACC_TRUNC;
881 if (cmode & NC_DISKLESS) {
882 #ifndef USE_PARALLEL4 886 }
else if ((cmode & NC_NOCLOBBER) && (fp = fopen(path,
"r"))) {
892 if ((retval = nc4_nc4f_list_add(nc, path, (
NC_WRITE | cmode))))
894 nc4_info = NC4_DATA(nc);
895 assert(nc4_info && nc4_info->root_grp);
900 if ((fapl_id = H5Pcreate(H5P_FILE_ACCESS)) < 0)
902 if (H5Pset_fclose_degree(fapl_id, H5F_CLOSE_SEMI))
910 nc4_info->parallel = NC_TRUE;
911 if (cmode & NC_MPIIO)
913 LOG((4,
"creating parallel file with MPI/IO"));
914 if (H5Pset_fapl_mpio(fapl_id, comm, info) < 0)
917 #ifdef USE_PARALLEL_POSIX 920 LOG((4,
"creating parallel file with MPI/posix"));
921 if (H5Pset_fapl_mpiposix(fapl_id, comm, 0) < 0)
934 if (MPI_SUCCESS != MPI_Comm_dup(comm, &nc4_info->comm))
937 if (MPI_INFO_NULL != info)
939 if (MPI_SUCCESS != MPI_Info_dup(info, &nc4_info->info))
946 nc4_info->info = info;
950 if(cmode & NC_DISKLESS) {
951 if (H5Pset_fapl_core(fapl_id, 4096, persist))
957 LOG((4,
"%s: set HDF raw chunk cache to size %d nelems %d preemption %f",
961 #ifdef HDF5_HAS_LIBVER_BOUNDS 962 if (H5Pset_libver_bounds(fapl_id, H5F_LIBVER_EARLIEST, H5F_LIBVER_LATEST) < 0)
967 if ((fcpl_id = H5Pcreate(H5P_FILE_CREATE)) < 0)
971 if (H5Pset_obj_track_times(fcpl_id,0)<0)
977 if (H5Pset_link_creation_order(fcpl_id, (H5P_CRT_ORDER_TRACKED |
978 H5P_CRT_ORDER_INDEXED)) < 0)
980 if (H5Pset_attr_creation_order(fcpl_id, (H5P_CRT_ORDER_TRACKED |
981 H5P_CRT_ORDER_INDEXED)) < 0)
985 #ifdef HDF5_HAS_COLL_METADATA_OPS 986 H5Pset_all_coll_metadata_ops(fapl_id, 1 );
987 H5Pset_coll_metadata_write(fapl_id, 1);
990 if ((nc4_info->hdfid = H5Fcreate(path, flags, fcpl_id, fapl_id)) < 0)
996 if ((nc4_info->root_grp->hdf_grpid = H5Gopen2(nc4_info->hdfid,
"/",
1001 if (H5Pclose(fapl_id) < 0 || H5Pclose(fcpl_id) < 0)
1005 nc4_info->flags |= NC_INDEF;
1008 NC4_put_propattr(nc4_info);
1013 #ifdef USE_PARALLEL4 1014 if (comm_duped) MPI_Comm_free(&nc4_info->comm);
1015 if (info_duped) MPI_Info_free(&nc4_info->info);
1017 if (fapl_id != H5P_DEFAULT) H5Pclose(fapl_id);
1018 if(!nc4_info)
return retval;
1019 close_netcdf4_file(nc4_info,1);
1043 NC4_create(
const char* path,
int cmode,
size_t initialsz,
int basepe,
1044 size_t *chunksizehintp,
int use_parallel,
void *parameters,
1045 NC_Dispatch *dispatch, NC* nc_file)
1047 MPI_Comm comm = MPI_COMM_WORLD;
1048 MPI_Info info = MPI_INFO_NULL;
1051 assert(nc_file && path);
1053 LOG((1,
"%s: path %s cmode 0x%x comm %d info %d",
1054 __func__, path, cmode, comm, info));
1056 #ifdef USE_PARALLEL4 1059 comm = ((NC_MPI_INFO *)parameters)->comm;
1060 info = ((NC_MPI_INFO *)parameters)->info;
1066 nc4_hdf5_initialize();
1069 if((cmode & ILLEGAL_CREATE_FLAGS) != 0)
1080 #ifndef USE_PARALLEL_POSIX 1086 cmode &= ~NC_MPIPOSIX;
1101 LOG((2,
"cmode after applying default format: 0x%x", cmode));
1103 nc_file->int_ncid = nc_file->ext_ncid;
1105 res = nc4_create_file(path, cmode, comm, info, nc_file);
1131 read_scale(NC_GRP_INFO_T *grp, hid_t datasetid,
const char *obj_name,
1132 const H5G_stat_t *statbuf, hsize_t scale_size, hsize_t max_scale_size,
1133 NC_DIM_INFO_T **dim)
1135 NC_DIM_INFO_T *new_dim;
1137 htri_t attr_exists = -1;
1139 int dimscale_created = 0;
1140 short initial_next_dimid = grp->nc4_info->next_dimid;
1144 if ((retval = nc4_dim_list_add(&grp->dim, &new_dim)))
1150 if ((attr_exists = H5Aexists(datasetid, NC_DIMID_ATT_NAME)) < 0)
1154 if ((attid = H5Aopen_name(datasetid, NC_DIMID_ATT_NAME)) < 0)
1157 if (H5Aread(attid, H5T_NATIVE_INT, &new_dim->dimid) < 0)
1161 if (new_dim->dimid >= grp->nc4_info->next_dimid)
1162 grp->nc4_info->next_dimid = new_dim->dimid + 1;
1167 new_dim->dimid = grp->nc4_info->next_dimid++;
1170 if (!(new_dim->name = strdup(obj_name)))
1172 if (SIZEOF_SIZE_T < 8 && scale_size >
NC_MAX_UINT)
1175 new_dim->too_long = NC_TRUE;
1178 new_dim->len = scale_size;
1179 new_dim->hdf5_objid.fileno[0] = statbuf->fileno[0];
1180 new_dim->hdf5_objid.fileno[1] = statbuf->fileno[1];
1181 new_dim->hdf5_objid.objno[0] = statbuf->objno[0];
1182 new_dim->hdf5_objid.objno[1] = statbuf->objno[1];
1183 new_dim->hash = hash_fast(obj_name, strlen(obj_name));
1187 if (max_scale_size == H5S_UNLIMITED)
1188 new_dim->unlimited = NC_TRUE;
1193 if (H5DSget_scale_name(datasetid, dimscale_name_att,
NC_MAX_NAME) >= 0)
1195 if (!strncmp(dimscale_name_att, DIM_WITHOUT_VARIABLE,
1196 strlen(DIM_WITHOUT_VARIABLE)))
1198 if (new_dim->unlimited)
1200 size_t len = 0, *lenp = &len;
1202 if ((retval = nc4_find_dim_len(grp, new_dim->dimid, &lenp)))
1204 new_dim->len = *lenp;
1208 new_dim->hdf_dimscaleid = datasetid;
1209 H5Iinc_ref(new_dim->hdf_dimscaleid);
1218 if (attid > 0 && H5Aclose(attid) < 0)
1222 if (retval < 0 && dimscale_created)
1225 if ((retval = nc4_dim_list_del(&grp->dim, new_dim)))
1229 grp->nc4_info->next_dimid = initial_next_dimid;
1246 read_coord_dimids(NC_GRP_INFO_T *grp, NC_VAR_INFO_T *var)
1248 hid_t coord_att_typeid = -1, coord_attid = -1, spaceid = -1;
1256 if ((coord_attid = H5Aopen_name(var->hdf_datasetid, COORDINATES)) < 0) ret++;
1257 if (!ret && (coord_att_typeid = H5Aget_type(coord_attid)) < 0) ret++;
1260 if (!ret && (spaceid = H5Aget_space(coord_attid)) < 0) ret++;
1261 if (!ret && (npoints = H5Sget_simple_extent_npoints(spaceid)) < 0) ret++;
1265 if (!ret && npoints != var->ndims) ret++;
1267 if (!ret && H5Aread(coord_attid, coord_att_typeid, var->dimids) < 0) ret++;
1268 LOG((4,
"dimscale %s is multidimensional and has coords", var->name));
1271 for (d = 0; d < var->ndims; d++) {
1273 nc4_find_dim(grp, var->dimids[d], &var->dim[d], NULL);
1277 if (spaceid >= 0 && H5Sclose(spaceid) < 0) ret++;
1278 if (coord_att_typeid >= 0 && H5Tclose(coord_att_typeid) < 0) ret++;
1279 if (coord_attid >= 0 && H5Aclose(coord_attid) < 0) ret++;
1296 dimscale_visitor(hid_t did,
unsigned dim, hid_t dsid,
1297 void *dimscale_hdf5_objids)
1302 if (H5Gget_objinfo(dsid,
".", 1, &statbuf) < 0)
1306 (*(HDF5_OBJID_T *)dimscale_hdf5_objids).fileno[0] = statbuf.fileno[0];
1307 (*(HDF5_OBJID_T *)dimscale_hdf5_objids).fileno[1] = statbuf.fileno[1];
1308 (*(HDF5_OBJID_T *)dimscale_hdf5_objids).objno[0] = statbuf.objno[0];
1309 (*(HDF5_OBJID_T *)dimscale_hdf5_objids).objno[1] = statbuf.objno[1];
1329 get_type_info2(NC_HDF5_FILE_INFO_T *h5, hid_t datasetid,
1330 NC_TYPE_INFO_T **type_info)
1332 htri_t is_str, equal = 0;
1334 hid_t native_typeid, hdf_typeid;
1338 assert(h5 && type_info);
1346 if (!h5_native_type_constant_g[1])
1348 h5_native_type_constant_g[1] = H5T_NATIVE_SCHAR;
1349 h5_native_type_constant_g[2] = H5T_NATIVE_SHORT;
1350 h5_native_type_constant_g[3] = H5T_NATIVE_INT;
1351 h5_native_type_constant_g[4] = H5T_NATIVE_FLOAT;
1352 h5_native_type_constant_g[5] = H5T_NATIVE_DOUBLE;
1353 h5_native_type_constant_g[6] = H5T_NATIVE_UCHAR;
1354 h5_native_type_constant_g[7] = H5T_NATIVE_USHORT;
1355 h5_native_type_constant_g[8] = H5T_NATIVE_UINT;
1356 h5_native_type_constant_g[9] = H5T_NATIVE_LLONG;
1357 h5_native_type_constant_g[10] = H5T_NATIVE_ULLONG;
1361 if ((hdf_typeid = H5Dget_type(datasetid)) < 0)
1366 if ((native_typeid = H5Tget_native_type(hdf_typeid, H5T_DIR_DEFAULT)) < 0)
1370 if ((
class = H5Tget_class(native_typeid)) < 0)
1374 if (
class == H5T_STRING ||
class == H5T_INTEGER ||
class == H5T_FLOAT)
1377 if (!(*type_info = calloc(1,
sizeof(NC_TYPE_INFO_T))))
1382 if (
class == H5T_STRING)
1384 if ((is_str = H5Tis_variable_str(native_typeid)) < 0)
1387 if (is_str || H5Tget_size(hdf_typeid) > 1)
1391 (*type_info)->nc_type_class =
NC_STRING;
1397 (*type_info)->nc_type_class =
NC_CHAR;
1400 else if (
class == H5T_INTEGER ||
class == H5T_FLOAT)
1404 if ((equal = H5Tequal(native_typeid, h5_native_type_constant_g[t])) < 0)
1416 if((order = H5Tget_order(hdf_typeid)) < 0)
1419 if(order == H5T_ORDER_LE)
1421 else if(order == H5T_ORDER_BE)
1426 if(
class == H5T_INTEGER)
1427 (*type_info)->nc_type_class =
NC_INT;
1429 (*type_info)->nc_type_class =
NC_FLOAT;
1431 (*type_info)->nc_typeid = nc_type_constant_g[t];
1432 (*type_info)->size = nc_type_size_g[t];
1433 if (!((*type_info)->name = strdup(nc_type_name_g[t])))
1435 (*type_info)->hdf_typeid = hdf_typeid;
1436 (*type_info)->native_hdf_typeid = native_typeid;
1441 NC_TYPE_INFO_T *type;
1444 if((type = nc4_rec_find_hdf_type(h5->root_grp, native_typeid)))
1450 if (H5Tclose(native_typeid) < 0)
1452 if (H5Tclose(hdf_typeid) < 0)
1477 read_type(NC_GRP_INFO_T *grp, hid_t hdf_typeid,
char *type_name)
1479 NC_TYPE_INFO_T *type;
1481 hid_t native_typeid;
1485 assert(grp && type_name);
1487 LOG((4,
"%s: type_name %s grp->name %s", __func__, type_name, grp->name));
1490 if ((native_typeid = H5Tget_native_type(hdf_typeid, H5T_DIR_DEFAULT)) < 0)
1494 if (!(type_size = H5Tget_size(native_typeid)))
1496 LOG((5,
"type_size %d", type_size));
1499 if ((retval = nc4_type_list_add(grp, type_size, type_name, &type)))
1503 type->committed = NC_TRUE;
1504 type->hdf_typeid = hdf_typeid;
1505 H5Iinc_ref(type->hdf_typeid);
1506 type->native_hdf_typeid = native_typeid;
1509 if ((
class = H5Tget_class(hdf_typeid)) < 0)
1521 char* member_name = NULL;
1528 if ((nmembers = H5Tget_nmembers(hdf_typeid)) < 0)
1530 LOG((5,
"compound type has %d members", nmembers));
1531 for (m = 0; m < nmembers; m++)
1533 hid_t member_hdf_typeid;
1534 hid_t member_native_typeid;
1535 size_t member_offset;
1536 H5T_class_t mem_class;
1542 if ((member_hdf_typeid = H5Tget_member_type(type->native_hdf_typeid, m)) < 0)
1545 if ((member_native_typeid = H5Tget_native_type(member_hdf_typeid, H5T_DIR_DEFAULT)) < 0)
1549 member_name = H5Tget_member_name(type->native_hdf_typeid, m);
1550 if (!member_name || strlen(member_name) >
NC_MAX_NAME) {
1556 strncpy(jna,member_name,1000);
1562 member_offset = H5Tget_member_offset(type->native_hdf_typeid, m);
1565 if ((mem_class = H5Tget_class(member_hdf_typeid)) < 0)
1567 if (mem_class == H5T_ARRAY)
1573 if ((ndims = H5Tget_array_ndims(member_hdf_typeid)) < 0) {
1577 if (H5Tget_array_dims(member_hdf_typeid, dims, NULL) != ndims) {
1581 for (d = 0; d < ndims; d++)
1582 dim_size[d] = dims[d];
1585 if ((retval = get_netcdf_type(grp->nc4_info, H5Tget_super(member_hdf_typeid),
1590 if ((retval = nc4_field_list_add(&type->u.c.field, type->u.c.num_fields++, member_name,
1591 member_offset, H5Tget_super(member_hdf_typeid),
1592 H5Tget_super(member_native_typeid),
1593 member_xtype, ndims, dim_size)))
1599 if ((retval = get_netcdf_type(grp->nc4_info, member_native_typeid,
1604 if ((retval = nc4_field_list_add(&type->u.c.field, type->u.c.num_fields++, member_name,
1605 member_offset, member_hdf_typeid, member_native_typeid,
1606 member_xtype, 0, NULL)))
1610 hdf5free(member_name);
1613 hdf5free(member_name);
1628 if ((ret = H5Tis_variable_str(hdf_typeid)) < 0)
1634 hid_t base_hdf_typeid;
1637 type->nc_type_class =
NC_VLEN;
1641 if (!(base_hdf_typeid = H5Tget_super(native_typeid)))
1645 if (!(type_size = H5Tget_size(base_hdf_typeid)))
1649 if ((retval = get_netcdf_type(grp->nc4_info, base_hdf_typeid,
1652 LOG((5,
"base_hdf_typeid 0x%x type_size %d base_nc_type %d",
1653 base_hdf_typeid, type_size, base_nc_type));
1656 type->u.v.base_nc_typeid = base_nc_type;
1657 type->u.v.base_hdf_typeid = base_hdf_typeid;
1668 hid_t base_hdf_typeid;
1672 char *member_name = NULL;
1677 type->nc_type_class =
NC_ENUM;
1681 if (!(base_hdf_typeid = H5Tget_super(hdf_typeid)))
1684 if (!(type_size = H5Tget_size(base_hdf_typeid)))
1687 if ((retval = get_netcdf_type(grp->nc4_info, base_hdf_typeid,
1690 LOG((5,
"base_hdf_typeid 0x%x type_size %d base_nc_type %d",
1691 base_hdf_typeid, type_size, base_nc_type));
1694 type->u.e.base_nc_typeid = base_nc_type;
1695 type->u.e.base_hdf_typeid = base_hdf_typeid;
1698 if ((type->u.e.num_members = H5Tget_nmembers(hdf_typeid)) < 0)
1702 if (!(value = calloc(1, type_size)))
1706 for (i = 0; i < type->u.e.num_members; i++)
1710 if (!(member_name = H5Tget_member_name(hdf_typeid, i)))
1716 strncpy(jna,member_name,1000);
1725 if (H5Tget_member_value(hdf_typeid, i, value) < 0)
1732 if ((retval = nc4_enum_member_add(&type->u.e.enum_member, type->size,
1733 member_name, value)))
1738 hdf5free(member_name);
1741 hdf5free(member_name);
1743 if(value) free(value);
1750 LOG((0,
"unknown class"));
1774 read_var(NC_GRP_INFO_T *grp, hid_t datasetid,
const char *obj_name,
1775 size_t ndims, NC_DIM_INFO_T *dim)
1777 NC_VAR_INFO_T *var = NULL;
1778 hid_t access_pid = 0;
1781 att_iter_info att_info;
1782 H5Z_filter_t filter;
1787 H5D_fill_value_t fill_status;
1788 H5D_layout_t layout;
1794 assert(obj_name && grp);
1795 LOG((4,
"%s: obj_name %s", __func__, obj_name));
1798 if ((retval = nc4_var_add(&var)))
1802 var->hdf_datasetid = datasetid;
1803 H5Iinc_ref(var->hdf_datasetid);
1805 var->varid = grp->nvars++;
1806 var->created = NC_TRUE;
1813 if (!(var->dim = calloc(var->ndims,
sizeof(NC_DIM_INFO_T *))))
1815 if (!(var->dimids = calloc(var->ndims,
sizeof(
int))))
1820 if ((access_pid = H5Dget_access_plist(datasetid)) < 0)
1824 if ((H5Pget_chunk_cache(access_pid, &(var->chunk_cache_nelems),
1825 &(var->chunk_cache_size), &rdcc_w0)) < 0)
1827 var->chunk_cache_preemption = rdcc_w0;
1832 if (strlen(obj_name) > strlen(NON_COORD_PREPEND) &&
1833 !strncmp(obj_name, NON_COORD_PREPEND, strlen(NON_COORD_PREPEND)))
1836 if (!(var->name = malloc(((strlen(obj_name) - strlen(NON_COORD_PREPEND))+ 1) *
sizeof(
char))))
1839 strcpy(var->name, &obj_name[strlen(NON_COORD_PREPEND)]);
1842 if (!(var->hdf5_name = malloc((strlen(obj_name) + 1) *
sizeof(
char))))
1845 strcpy(var->hdf5_name, obj_name);
1850 if (!(var->name = malloc((strlen(obj_name) + 1) *
sizeof(
char))))
1853 strcpy(var->name, obj_name);
1856 var->hash = hash_fast(var->name, strlen(var->name));
1860 if ((propid = H5Dget_create_plist(datasetid)) < 0)
1864 if ((layout = H5Pget_layout(propid)) < -1)
1866 if (layout == H5D_CHUNKED)
1870 if (!(var->chunksizes = malloc(var->ndims *
sizeof(
size_t))))
1872 for (d = 0; d < var->ndims; d++)
1873 var->chunksizes[d] = chunksize[d];
1875 else if (layout == H5D_CONTIGUOUS || layout == H5D_COMPACT)
1876 var->contiguous = NC_TRUE;
1880 if ((num_filters = H5Pget_nfilters(propid)) < 0)
1882 for (f = 0; f < num_filters; f++)
1884 if ((filter = H5Pget_filter2(propid, f, NULL, &cd_nelems,
1885 cd_values_zip, 0, NULL, NULL)) < 0)
1889 case H5Z_FILTER_SHUFFLE:
1890 var->shuffle = NC_TRUE;
1893 case H5Z_FILTER_FLETCHER32:
1894 var->fletcher32 = NC_TRUE;
1897 case H5Z_FILTER_DEFLATE:
1898 var->deflate = NC_TRUE;
1901 var->deflate_level = cd_values_zip[0];
1905 var->filterid = filter;
1906 var->nparams = cd_nelems;
1911 var->params = (
unsigned int*)calloc(1,
sizeof(
unsigned int)*var->nparams);
1912 if(var->params == NULL)
1914 if((filter = H5Pget_filter2(propid, f, NULL, &cd_nelems,
1915 var->params, 0, NULL, NULL)) < 0)
1923 if ((retval = get_type_info2(grp->nc4_info, datasetid,
1928 var->type_info->rc++;
1931 if (H5Pfill_value_defined(propid, &fill_status) < 0)
1935 if (fill_status == H5D_FILL_VALUE_USER_DEFINED)
1938 if (!var->fill_value)
1940 if (var->type_info->nc_type_class ==
NC_VLEN)
1942 if (!(var->fill_value = malloc(
sizeof(
nc_vlen_t))))
1945 else if (var->type_info->nc_type_class ==
NC_STRING)
1947 if (!(var->fill_value = malloc(
sizeof(
char *))))
1952 assert(var->type_info->size);
1953 if (!(var->fill_value = malloc(var->type_info->size)))
1959 if (H5Pget_fill_value(propid, var->type_info->native_hdf_typeid,
1960 var->fill_value) < 0)
1964 var->no_fill = NC_TRUE;
1970 var->dimscale = NC_TRUE;
1973 if ((retval = read_coord_dimids(grp, var)))
1979 assert(0 == strcmp(var->name, dim->name));
1981 var->dimids[0] = dim->dimid;
1984 dim->coord_var = var;
1996 num_scales = H5DSget_num_scales(datasetid, 0);
2000 if (num_scales && ndims)
2004 if (NULL == (var->dimscale_attached = calloc(ndims,
sizeof(nc_bool_t))))
2009 if (NULL == (var->dimscale_hdf5_objids = malloc(ndims *
sizeof(
struct hdf5_objid))))
2011 for (d = 0; d < var->ndims; d++)
2013 if (H5DSiterate_scales(var->hdf_datasetid, d, NULL, dimscale_visitor,
2014 &(var->dimscale_hdf5_objids[d])) < 0)
2016 var->dimscale_attached[d] = NC_TRUE;
2027 if ((H5Aiterate2(var->hdf_datasetid, H5_INDEX_CRT_ORDER, H5_ITER_INC, NULL,
2028 att_read_var_callbk, &att_info)) < 0)
2032 if ((retval = nc4_vararray_add(grp, var)))
2037 if ((retval = nc4_adjust_var_cache(grp, var)))
2043 if (incr_id_rc && H5Idec_ref(datasetid) < 0)
2045 if (var && nc4_var_del(var))
2048 if (access_pid && H5Pclose(access_pid) < 0)
2050 if (propid > 0 && H5Pclose(propid) < 0)
2067 read_grp_atts(NC_GRP_INFO_T *grp)
2072 NC_TYPE_INFO_T *type;
2073 char obj_name[NC_MAX_HDF5_NAME + 1];
2078 num_obj = H5Aget_num_attrs(grp->hdf_grpid);
2079 for (i = 0; i < num_obj; i++)
2081 if ((attid = H5Aopen_idx(grp->hdf_grpid, (
unsigned int)i)) < 0)
2083 if (H5Aget_name(attid,
NC_MAX_NAME + 1, obj_name) < 0)
2085 LOG((3,
"reading attribute of _netCDF group, named %s", obj_name));
2088 if(grp->nc4_info->root_grp == grp) {
2089 const char** reserved = NC_RESERVED_ATT_LIST;
2091 for(;*reserved;reserved++) {
2092 if(strcmp(*reserved,obj_name)==0) {
2103 if(strcmp(obj_name, NC3_STRICT_ATT_NAME)==0)
2107 if ((retval = nc4_att_list_add(&grp->att, &att)))
2112 if (!(att->name = malloc((max_len + 1) *
sizeof(
char))))
2114 strncpy(att->name, obj_name, max_len);
2115 att->name[max_len] = 0;
2116 att->attnum = grp->natts++;
2117 retval = read_hdf5_att(grp, attid, att);
2119 if((retval = nc4_att_list_del(&grp->att, att)))
2124 att->created = NC_TRUE;
2125 if ((retval = nc4_find_type(grp->nc4_info, att->nc_typeid, &type)))
2136 if(H5Aclose(attid) < 0)
2157 read_dataset(NC_GRP_INFO_T *grp, hid_t datasetid,
const char *obj_name,
2158 const H5G_stat_t *statbuf)
2160 NC_DIM_INFO_T *dim = NULL;
2167 if ((spaceid = H5Dget_space(datasetid)) < 0)
2169 if ((ndims = H5Sget_simple_extent_ndims(spaceid)) < 0)
2173 if ((is_scale = H5DSis_scale(datasetid)) < 0)
2177 hsize_t dims[H5S_MAX_RANK];
2178 hsize_t max_dims[H5S_MAX_RANK];
2181 if (H5Sget_simple_extent_dims(spaceid, dims, max_dims) < 0)
2185 if ((retval = read_scale(grp, datasetid, obj_name, statbuf, dims[0],
2186 max_dims[0], &dim)))
2193 if (NULL == dim || (dim && !dim->hdf_dimscaleid))
2194 if ((retval = read_var(grp, datasetid, obj_name, ndims, dim)))
2198 if (spaceid && H5Sclose(spaceid) <0)
2216 nc4_rec_read_metadata_cb_list_add(NC4_rec_read_metadata_obj_info_t **head,
2217 NC4_rec_read_metadata_obj_info_t **tail,
2218 const NC4_rec_read_metadata_obj_info_t *oinfo)
2220 NC4_rec_read_metadata_obj_info_t *new_oinfo;
2223 if (!(new_oinfo = calloc(1,
sizeof(*new_oinfo))))
2227 memcpy(new_oinfo, oinfo,
sizeof(*oinfo));
2232 (*tail)->next = new_oinfo;
2237 assert(NULL == *head);
2238 *head = *tail = new_oinfo;
2257 nc4_rec_read_metadata_cb(hid_t grpid,
const char *name,
const H5L_info_t *info,
2260 NC4_rec_read_metadata_ud_t *udata = (NC4_rec_read_metadata_ud_t *)_op_data;
2261 NC4_rec_read_metadata_obj_info_t oinfo;
2262 int retval = H5_ITER_CONT;
2265 memset(&oinfo, 0,
sizeof(oinfo));
2268 if ((oinfo.oid = H5Oopen(grpid, name, H5P_DEFAULT)) < 0)
2269 BAIL(H5_ITER_ERROR);
2272 if (H5Gget_objinfo(oinfo.oid,
".", 1, &oinfo.statbuf) < 0)
2273 BAIL(H5_ITER_ERROR);
2278 switch(oinfo.statbuf.type)
2281 LOG((3,
"found group %s", oinfo.oname));
2287 if (nc4_rec_read_metadata_cb_list_add(&udata->grps_head, &udata->grps_tail, &oinfo))
2288 BAIL(H5_ITER_ERROR);
2292 LOG((3,
"found dataset %s", oinfo.oname));
2296 if ((retval = read_dataset(udata->grp, oinfo.oid, oinfo.oname, &oinfo.statbuf)))
2304 BAIL(H5_ITER_ERROR);
2306 retval = H5_ITER_CONT;
2310 if (H5Oclose(oinfo.oid) < 0)
2311 BAIL(H5_ITER_ERROR);
2315 LOG((3,
"found datatype %s", oinfo.oname));
2318 if (read_type(udata->grp, oinfo.oid, oinfo.oname))
2319 BAIL(H5_ITER_ERROR);
2322 if (H5Oclose(oinfo.oid) < 0)
2323 BAIL(H5_ITER_ERROR);
2327 LOG((0,
"Unknown object class %d in %s!", oinfo.statbuf.type, __func__));
2328 BAIL(H5_ITER_ERROR);
2334 if (oinfo.oid > 0 && H5Oclose(oinfo.oid) < 0)
2335 BAIL2(H5_ITER_ERROR);
2356 nc4_rec_read_metadata(NC_GRP_INFO_T *grp)
2358 NC4_rec_read_metadata_ud_t udata;
2359 NC4_rec_read_metadata_obj_info_t *oinfo;
2362 unsigned crt_order_flags = 0;
2363 H5_index_t iter_index;
2366 assert(grp && grp->name);
2367 LOG((3,
"%s: grp->name %s", __func__, grp->name));
2370 memset(&udata, 0,
sizeof(udata));
2374 if (!grp->hdf_grpid)
2378 if ((grp->hdf_grpid = H5Gopen2(grp->parent->hdf_grpid,
2379 grp->name, H5P_DEFAULT)) < 0)
2384 if ((grp->hdf_grpid = H5Gopen2(grp->nc4_info->hdfid,
2385 "/", H5P_DEFAULT)) < 0)
2389 assert(grp->hdf_grpid > 0);
2392 pid = H5Gget_create_plist(grp->hdf_grpid);
2393 H5Pget_link_creation_order(pid, &crt_order_flags);
2394 if (H5Pclose(pid) < 0)
2398 if (crt_order_flags & H5P_CRT_ORDER_TRACKED)
2399 iter_index = H5_INDEX_CRT_ORDER;
2402 NC_HDF5_FILE_INFO_T *h5 = grp->nc4_info;
2408 iter_index = H5_INDEX_NAME;
2417 if (H5Literate(grp->hdf_grpid, iter_index, H5_ITER_INC, &idx,
2418 nc4_rec_read_metadata_cb, (
void *)&udata) < 0)
2425 for (oinfo = udata.grps_head; oinfo; oinfo = udata.grps_head)
2427 NC_GRP_INFO_T *child_grp;
2428 NC_HDF5_FILE_INFO_T *h5 = grp->nc4_info;
2431 if ((retval = nc4_grp_list_add(&(grp->children), h5->next_nc_grpid++,
2432 grp, grp->nc4_info->controller, oinfo->oname, &child_grp)))
2436 if ((retval = nc4_rec_read_metadata(child_grp)))
2440 if (H5Oclose(oinfo->oid) < 0)
2444 udata.grps_head = oinfo->next;
2449 if ((retval = read_grp_atts(grp)))
2453 for (i=0; i<grp->vars.nelems; i++)
2454 grp->vars.value[i]->written_to = NC_TRUE;
2460 for (oinfo = udata.grps_head; oinfo; oinfo = udata.grps_head)
2463 if (H5Oclose(oinfo->oid) < 0)
2467 udata.grps_head = oinfo->next;
2489 nc4_open_file(
const char *path,
int mode,
void* parameters, NC *nc)
2491 hid_t fapl_id = H5P_DEFAULT;
2492 unsigned flags = (mode &
NC_WRITE) ?
2493 H5F_ACC_RDWR : H5F_ACC_RDONLY;
2495 NC_HDF5_FILE_INFO_T* nc4_info = NULL;
2497 NC_MEM_INFO* meminfo = (NC_MEM_INFO*)parameters;
2498 #ifdef USE_PARALLEL4 2499 NC_MPI_INFO* mpiinfo = (NC_MPI_INFO*)parameters;
2504 LOG((3,
"%s: path %s mode %d", __func__, path, mode));
2508 if ((retval = nc4_nc4f_list_add(nc, path, mode)))
2510 nc4_info = NC4_DATA(nc);
2511 assert(nc4_info && nc4_info->root_grp);
2516 if ((fapl_id = H5Pcreate(H5P_FILE_ACCESS)) < 0)
2519 if (H5Pset_fclose_degree(fapl_id, H5F_CLOSE_SEMI))
2522 #ifdef USE_PARALLEL4 2527 nc4_info->parallel = NC_TRUE;
2530 LOG((4,
"opening parallel file with MPI/IO"));
2531 if (H5Pset_fapl_mpio(fapl_id, mpiinfo->comm, mpiinfo->info) < 0)
2534 #ifdef USE_PARALLEL_POSIX 2537 LOG((4,
"opening parallel file with MPI/posix"));
2538 if (H5Pset_fapl_mpiposix(fapl_id, mpiinfo->comm, 0) < 0)
2551 if (MPI_SUCCESS != MPI_Comm_dup(mpiinfo->comm, &nc4_info->comm))
2554 if (MPI_INFO_NULL != mpiinfo->info)
2556 if (MPI_SUCCESS != MPI_Info_dup(mpiinfo->info, &nc4_info->info))
2563 nc4_info->info = mpiinfo->info;
2570 LOG((4,
"%s: set HDF raw chunk cache to size %d nelems %d preemption %f",
2577 #ifdef HDF5_HAS_COLL_METADATA_OPS 2578 H5Pset_all_coll_metadata_ops(fapl_id, 1 );
2581 if((nc4_info->hdfid = H5LTopen_file_image(meminfo->memory,meminfo->size,
2582 H5LT_FILE_IMAGE_DONT_COPY|H5LT_FILE_IMAGE_DONT_RELEASE
2585 nc4_info->no_write = NC_TRUE;
2586 }
else if ((nc4_info->hdfid = H5Fopen(path, flags, fapl_id)) < 0)
2591 nc4_info->no_write = NC_TRUE;
2597 if ((retval = nc4_rec_read_metadata(nc4_info->root_grp)))
2602 if ((retval = nc4_rec_match_dimscales(nc4_info->root_grp)))
2608 log_metadata_nc(nc);
2612 if (H5Pclose(fapl_id) < 0)
2615 NC4_get_fileinfo(nc4_info,NULL);
2620 #ifdef USE_PARALLEL4 2621 if (comm_duped) MPI_Comm_free(&nc4_info->comm);
2622 if (info_duped) MPI_Info_free(&nc4_info->info);
2624 if (fapl_id != H5P_DEFAULT) H5Pclose(fapl_id);
2625 if (!nc4_info)
return retval;
2626 close_netcdf4_file(nc4_info,1);
2646 get_netcdf_type_from_hdf4(NC_HDF5_FILE_INFO_T *h5, int32 hdf4_typeid,
2647 nc_type *xtype, NC_TYPE_INFO_T *type_info)
2658 assert(h5 && xtype);
2746 if (hdf4_typeid == DFNT_FLOAT32)
2747 type_info->nc_type_class =
NC_FLOAT;
2748 else if (hdf4_typeid == DFNT_FLOAT64)
2750 else if (hdf4_typeid == DFNT_CHAR)
2753 type_info->nc_type_class =
NC_INT;
2754 type_info->endianness = endianness;
2755 type_info->nc_typeid = *xtype;
2756 type_info->size = nc_type_size_g[t];
2757 if (!(type_info->name = strdup(nc_type_name_g[t])))
2777 nc4_open_hdf4_file(
const char *path,
int mode, NC *nc)
2779 NC_HDF5_FILE_INFO_T *h5;
2782 int32 num_datasets, num_gatts;
2786 NC_HDF5_FILE_INFO_T* nc4_info = NULL;
2788 LOG((3,
"%s: path %s mode %d", __func__, path, mode));
2796 if ((retval = nc4_nc4f_list_add(nc, path, mode)))
2798 nc4_info = NC4_DATA(nc);
2799 assert(nc4_info && nc4_info->root_grp);
2803 h5->no_write = NC_TRUE;
2806 if ((h5->sdid = SDstart(path, DFACC_READ)) == FAIL)
2810 if (SDfileinfo(h5->sdid, &num_datasets, &num_gatts))
2814 for (a = 0; a < num_gatts; a++)
2816 int32 att_data_type, att_count;
2817 size_t att_type_size;
2820 if ((retval = nc4_att_list_add(&h5->root_grp->att, &att)))
2822 att->attnum = grp->natts++;
2823 att->created = NC_TRUE;
2828 if (SDattrinfo(h5->sdid, a, att->name, &att_data_type, &att_count))
2830 if ((retval = get_netcdf_type_from_hdf4(h5, att_data_type,
2831 &att->nc_typeid, NULL)))
2833 att->len = att_count;
2836 if ((retval = nc4_get_typelen_mem(h5, att->nc_typeid, 0, &att_type_size)))
2838 if (!(att->data = malloc(att_type_size * att->len)))
2842 if (SDreadattr(h5->sdid, a, att->data))
2847 for (v = 0; v < num_datasets; v++)
2850 int32 data_type, num_atts;
2859 int32 *dimsize = NULL;
2860 size_t var_type_size;
2864 if ((retval = nc4_var_add(&var)))
2867 var->varid = grp->nvars++;
2868 var->created = NC_TRUE;
2869 var->written_to = NC_TRUE;
2872 if ((retval = nc4_vararray_add(grp, var)))
2876 if ((var->sdsid = SDselect(h5->sdid, v)) == FAIL)
2884 if (SDgetinfo(var->sdsid, var->name, &rank, NULL, &data_type, &num_atts))
2887 var->hash = hash_fast(var->name, strlen(var->name));
2889 if(!(dimsize = (int32*)malloc(
sizeof(int32)*rank)))
2892 if (SDgetinfo(var->sdsid, var->name, &rank, dimsize, &data_type, &num_atts)) {
2893 if(dimsize) free(dimsize);
2898 var->hdf4_data_type = data_type;
2901 if (!(var->type_info = calloc(1,
sizeof(NC_TYPE_INFO_T)))) {
2902 if(dimsize) free(dimsize);
2906 if ((retval = get_netcdf_type_from_hdf4(h5, data_type, &var->type_info->nc_typeid, var->type_info))) {
2907 if(dimsize) free(dimsize);
2912 var->type_info->rc++;
2914 if ((retval = nc4_get_typelen_mem(h5, var->type_info->nc_typeid, 0, &var_type_size))) {
2915 if(dimsize) free(dimsize);
2919 var->type_info->size = var_type_size;
2920 LOG((3,
"reading HDF4 dataset %s, rank %d netCDF type %d", var->name,
2921 rank, var->type_info->nc_typeid));
2924 if (!(var->fill_value = malloc(var_type_size))) {
2925 if(dimsize) free(dimsize);
2929 if (SDgetfillvalue(var->sdsid, var->fill_value))
2932 free(var->fill_value);
2933 var->fill_value = NULL;
2939 if (!(var->dim = malloc(
sizeof(NC_DIM_INFO_T *) * var->ndims))) {
2940 if(dimsize) free(dimsize);
2944 if (!(var->dimids = malloc(
sizeof(
int) * var->ndims))) {
2945 if(dimsize) free(dimsize);
2952 for (d = 0; d < var->ndims; d++)
2954 int32 dimid, dim_len, dim_data_type, dim_num_attrs;
2958 if ((dimid = SDgetdimid(var->sdsid, d)) == FAIL) {
2959 if(dimsize) free(dimsize);
2962 if (SDdiminfo(dimid, dim_name, &dim_len, &dim_data_type,
2965 if(dimsize) free(dimsize);
2971 for (dim = grp->dim; dim; dim = dim->l.next)
2972 if (!strcmp(dim->name, dim_name))
2978 LOG((4,
"adding dimension %s for HDF4 dataset %s",
2979 dim_name, var->name));
2980 if ((retval = nc4_dim_list_add(&grp->dim, &dim)))
2982 dim->dimid = grp->nc4_info->next_dimid++;
2985 if (!(dim->name = strdup(dim_name)))
2990 dim->len = *dimsize;
2991 dim->hash = hash_fast(dim_name, strlen(dim_name));
2995 var->dimids[d] = dim->dimid;
3000 for (a = 0; a < num_atts; a++)
3002 int32 att_data_type, att_count;
3003 size_t att_type_size;
3006 if ((retval = nc4_att_list_add(&var->att, &att))) {
3007 if(dimsize) free(dimsize);
3010 att->attnum = var->natts++;
3011 att->created = NC_TRUE;
3015 if(dimsize) free(dimsize);
3018 if (SDattrinfo(var->sdsid, a, att->name, &att_data_type, &att_count)) {
3019 if(dimsize) free(dimsize);
3022 if ((retval = get_netcdf_type_from_hdf4(h5, att_data_type,
3023 &att->nc_typeid, NULL))) {
3024 if(dimsize) free(dimsize);
3028 att->len = att_count;
3031 if ((retval = nc4_get_typelen_mem(h5, att->nc_typeid, 0, &att_type_size))) {
3032 if(dimsize) free(dimsize);
3035 if (!(att->data = malloc(att_type_size * att->len))) {
3036 if(dimsize) free(dimsize);
3041 if (SDreadattr(var->sdsid, a, att->data)) {
3042 if(dimsize) free(dimsize);
3046 if(dimsize) free(dimsize);
3050 HDF_CHUNK_DEF chunkdefs;
3052 if(!SDgetchunkinfo(var->sdsid, &chunkdefs, &flag)) {
3053 if(flag == HDF_NONE)
3054 var->contiguous = NC_TRUE;
3055 else if((flag & HDF_CHUNK) != 0) {
3056 var->contiguous = NC_FALSE;
3057 if (!(var->chunksizes = malloc(var->ndims *
sizeof(
size_t))))
3059 for (d = 0; d < var->ndims; d++) {
3060 var->chunksizes[d] = chunkdefs.chunk_lengths[d];
3071 log_metadata_nc(h5->root_grp->nc4_info->controller);
3094 NC4_open(
const char *path,
int mode,
int basepe,
size_t *chunksizehintp,
3095 int use_parallel,
void *parameters, NC_Dispatch *dispatch, NC *nc_file)
3098 int is_hdf5_file = 0;
3100 int is_hdf4_file = 0;
3102 #ifdef USE_PARALLEL4 3103 NC_MPI_INFO mpidfalt = {MPI_COMM_WORLD, MPI_INFO_NULL};
3105 #if defined USE_PARALLEL4 || defined USE_HDF4 3109 assert(nc_file && path);
3111 LOG((1,
"%s: path %s mode %d params %x",
3112 __func__, path, mode, parameters));
3114 #ifdef USE_PARALLEL4 3115 if (!inmemory && use_parallel && parameters == NULL)
3116 parameters = &mpidfalt;
3121 nc4_hdf5_initialize();
3124 if((mode & ILLEGAL_OPEN_FLAGS) != 0)
3131 #ifndef USE_PARALLEL_POSIX 3137 mode &= ~NC_MPIPOSIX;
3150 nc_file->int_ncid = nc_file->ext_ncid;
3152 res = nc4_open_file(path, mode, parameters, nc_file);
3154 else if (is_hdf4_file && inmemory)
3156 else if (is_hdf4_file)
3157 res = nc4_open_hdf4_file(path, mode, nc_file);
3180 NC4_set_fill(
int ncid,
int fillmode,
int *old_modep)
3183 NC_HDF5_FILE_INFO_T* nc4_info;
3185 LOG((2,
"%s: ncid 0x%x fillmode %d", __func__, ncid, fillmode));
3187 if (!(nc = nc4_find_nc_file(ncid,&nc4_info)))
3192 if (nc4_info->no_write)
3201 *old_modep = nc4_info->fill_mode;
3203 nc4_info->fill_mode = fillmode;
3221 NC_HDF5_FILE_INFO_T* nc4_info;
3223 LOG((1,
"%s: ncid 0x%x", __func__, ncid));
3226 if (!(nc4_find_nc_file(ncid,&nc4_info)))
3231 if (nc4_info->flags & NC_INDEF)
3235 if (nc4_info->no_write)
3239 nc4_info->flags |= NC_INDEF;
3243 nc4_info->redef = NC_TRUE;
3262 NC4__enddef(
int ncid,
size_t h_minfree,
size_t v_align,
3263 size_t v_minfree,
size_t r_align)
3265 if (nc4_find_nc_file(ncid,NULL) == NULL)
3268 return NC4_enddef(ncid);
3280 static int NC4_enddef(
int ncid)
3283 NC_HDF5_FILE_INFO_T* nc4_info;
3287 LOG((1,
"%s: ncid 0x%x", __func__, ncid));
3289 if (!(nc = nc4_find_nc_file(ncid,&nc4_info)))
3294 if (!(grp = nc4_rec_find_grp(nc4_info->root_grp, (ncid & GRP_ID_MASK))))
3298 for (i=0; i<grp->vars.nelems; i++)
3299 grp->vars.value[i]->written_to = NC_TRUE;
3301 return nc4_enddef_netcdf4_file(nc4_info);
3318 NC_HDF5_FILE_INFO_T* nc4_info;
3320 LOG((2,
"%s: ncid 0x%x", __func__, ncid));
3322 if (!(nc = nc4_find_nc_file(ncid,&nc4_info)))
3327 if (nc4_info && nc4_info->flags & NC_INDEF)
3331 if ((retval = NC4_enddef(ncid)))
3335 return sync_netcdf4_file(nc4_info);
3355 int delete_file = 0;
3358 NC_HDF5_FILE_INFO_T* nc4_info;
3360 LOG((2,
"%s: ncid 0x%x", __func__, ncid));
3363 if (!(nc = nc4_find_nc_file(ncid,&nc4_info)))
3369 if (nc4_info->flags & NC_INDEF && !nc4_info->redef)
3377 if ((retval = close_netcdf4_file(nc4_info, 1)))
3382 if (
remove(path) < 0)
3401 NC_HDF5_FILE_INFO_T *h5;
3404 LOG((1,
"%s: ncid 0x%x", __func__, ncid));
3407 if ((retval = nc4_find_nc_grp_h5(ncid, &nc, &grp, &h5)))
3410 assert(nc && h5 && grp);
3417 if ((retval = close_netcdf4_file(grp->nc4_info, 0)))
3441 NC4_inq(
int ncid,
int *ndimsp,
int *nvarsp,
int *nattsp,
int *unlimdimidp)
3444 NC_HDF5_FILE_INFO_T *h5;
3450 LOG((2,
"%s: ncid 0x%x", __func__, ncid));
3453 if ((retval = nc4_find_nc_grp_h5(ncid, &nc, &grp, &h5)))
3456 assert(h5 && grp && nc);
3462 for (dim = grp->dim; dim; dim = dim->l.next)
3469 for (i=0; i < grp->vars.nelems; i++)
3471 if (grp->vars.value[i])
3478 for (att = grp->att; att; att = att->l.next)
3491 for (dim = grp->dim; dim; dim = dim->l.next)
3494 *unlimdimidp = dim->dimid;
3511 nc4_enddef_netcdf4_file(NC_HDF5_FILE_INFO_T *h5)
3514 LOG((3,
"%s", __func__));
3517 if (!(h5->flags & NC_INDEF))
3521 h5->flags ^= NC_INDEF;
3524 h5->redef = NC_FALSE;
3526 return sync_netcdf4_file(h5);
struct NCPROPINFO globalpropinfo
Global property info.
#define NC_ENOMEM
Memory allocation (malloc) failure.
#define NC_CHAR
ISO/ASCII character.
#define NC_ECANTWRITE
Can't write.
#define NC_FORMATX_NC4
alias
#define NC_UBYTE
unsigned 1 byte int
#define NC_CLASSIC_MODEL
Enforce classic model on netCDF-4.
#define NC_MAX_VAR_DIMS
max per variable dimensions
#define NC_UINT
unsigned 4-byte int
#define NC_NOCLOBBER
Don't destroy existing file.
#define NC_INMEMORY
Read from memory.
#define NC_EHDFERR
Error at HDF5 layer.
#define NC_OPAQUE
opaque types
#define NC_MPIIO
Turn on MPI I/O.
#define NC_INT64
signed 8-byte int
#define NC_ENOTINDEFINE
Operation not allowed in data mode.
#define NC_DOUBLE
double precision floating point number
#define NC_EBADCLASS
Bad class.
int nc_type
The nc_type type is just an int.
#define NC_64BIT_OFFSET
Use large (64-bit) file offsets.
#define NC_NOWRITE
Set read-only access for nc_open().
#define NC_BYTE
signed 1 byte integer
#define NC_EINDEFINE
Operation not allowed in define mode.
#define NC_ENOTNC
Not a netcdf file.
#define NC_FORMAT_CDF5
Format specifier for nc_set_default_format() and returned by nc_inq_format.
#define NC_ENDIAN_LITTLE
In HDF5 files you can set the endianness of variables with nc_def_var_endian().
#define NC_EATTMETA
Problem with attribute metadata.
#define NUM_TYPES
Number of netCDF atomic types.
#define NC_VLEN
vlen (variable-length) types
#define NC_EMPI
MPI operation failed.
#define NC_EDISKLESS
Error in using diskless access.
int nc4_hdf5_initialized
True if initialization has happened.
#define NC_EFILEMETA
Problem with file metadata.
#define NC_EBADTYPE
Not a netcdf data type.
#define NC_EBADNAME
Attribute or variable name contains illegal characters.
#define NC_EDIMMETA
Problem with dimension metadata.
#define NC_MAX_DEFLATE_LEVEL
Maximum deflate level.
size_t nc4_chunk_cache_nelems
Default chunk cache number of elements.
#define NC_EINVAL
Invalid Argument.
#define CD_NELEMS_ZLIB
Number of parameters needed for ZLIB filter.
size_t nc4_chunk_cache_size
Default chunk cache size.
#define NC_INT
signed 4 byte integer
#define NC_EBADGRPID
Bad group ID.
#define NC_NOFILL
Argument to nc_set_fill() to turn off filling of data.
#define NC_ENDIAN_BIG
In HDF5 files you can set the endianness of variables with nc_def_var_endian().
#define NC_MAX_NAME
Maximum for classic library.
#define NC_ECANTREMOVE
Can't remove file.
#define NC_NAT
Not A Type.
#define NC_EBADTYPID
Bad type ID.
#define NC_USHORT
unsigned 2-byte int
int nc_set_chunk_cache(size_t size, size_t nelems, float preemption)
Set chunk cache size.
#define NC_EPARINIT
Error initializing for parallel access.
#define NC_NETCDF4
Use netCDF-4/HDF5 format.
#define NC_EEXIST
netcdf file exists && NC_NOCLOBBER
#define NC_FORMAT_NETCDF4_CLASSIC
Format specifier for nc_set_default_format() and returned by nc_inq_format.
#define NC_FORMATX_NC_HDF4
netCDF-4 subset of HDF4
#define NC_EBADID
Not a netcdf id.
#define NC_EVARMETA
Problem with variable metadata.
This is the type of arrays of vlens.
#define NC_MAX_UINT
Max or min values for a type.
int nc_get_chunk_cache(size_t *sizep, size_t *nelemsp, float *preemptionp)
Get chunk cache size.
#define NC_SHORT
signed 2 byte integer
#define NC_CDF5
Alias NC_CDF5 to NC_64BIT_DATA.
float nc4_chunk_cache_preemption
Default chunk cache preemption.
#define NC_WRITE
Set read-write access for nc_open().
#define NC_EMAXNAME
NC_MAX_NAME exceeded.
#define NC_EPERM
Write to read only.
#define NC_MAX_HDF4_NAME
This is the max size of an SD dataset name in HDF4 (from HDF4 documentation).
#define NC_NOERR
No Error.
#define NC_ENUM
enum types
#define NC_DISKLESS
Use diskless file.
#define NC_COMPOUND
compound types
#define NC_FORMAT_64BIT_OFFSET
Format specifier for nc_set_default_format() and returned by nc_inq_format.
#define NC_MMAP
Use diskless file with mmap.
#define NC_FILL
Argument to nc_set_fill() to clear NC_NOFILL.
#define NC_FLOAT
single precision floating point number
#define NC_UINT64
unsigned 8-byte int
#define NC_MPIPOSIX
Turn on MPI POSIX I/O.