16 #include "nc4internal.h" 17 #include "nc4dispatch.h" 19 extern int nc4_vararray_add(NC_GRP_INFO_T *grp,
23 #include <H5DSpublic.h> 24 #include <H5Fpublic.h> 38 extern int num_plists;
39 extern int num_spaces;
42 #define MIN_DEFLATE_LEVEL 0 43 #define MAX_DEFLATE_LEVEL 9 46 static int read_hdf5_att(NC_GRP_INFO_T *grp, hid_t attid, NC_ATT_INFO_T *att);
47 static void hdf5free(
void* memory);
56 att_read_var_callbk(hid_t loc_id,
const char *att_name,
const H5A_info_t *ainfo,
void *att_data)
62 att_iter_info *att_info = (att_iter_info *)att_data;
63 const char** reserved;
67 for(reserved=NC_RESERVED_VARATT_LIST;*reserved;reserved++) {
68 if (strcmp(att_name, *reserved)==0)
break;
71 if(*reserved == NULL) {
73 if ((attid = H5Aopen(loc_id, att_name, H5P_DEFAULT)) < 0)
75 LOG((4,
"%s:: att_name %s", __func__, att_name));
77 if ((retval = nc4_att_list_add(&att_info->var->att, &att)))
80 att->attnum = att_info->var->natts++;
81 if (!(att->name = strdup(att_name)))
86 if ((retval = read_hdf5_att(att_info->grp, attid, att)))
90 if ((retval = nc4_att_list_del(&att_info->var->att, att)))
97 att->created = NC_TRUE;
99 if (attid > 0 && H5Aclose(attid) < 0)
107 if (attid > 0 && H5Aclose(attid) < 0)
118 extern void reportopenobjects(
int log, hid_t);
125 typedef struct NC4_rec_read_metadata_obj_info
130 struct NC4_rec_read_metadata_obj_info *next;
139 typedef struct NC4_rec_read_metadata_ud
146 static int NC4_enddef(
int ncid);
147 static int nc4_rec_read_metadata(NC_GRP_INFO_T *grp);
148 static int close_netcdf4_file(NC_HDF5_FILE_INFO_T *h5,
int abort);
155 const char* NC_RESERVED_VARATT_LIST[] = {
156 NC_ATT_REFERENCE_LIST,
158 NC_ATT_DIMENSION_LIST,
169 const char* NC_RESERVED_ATT_LIST[] = {
179 const char* NC_RESERVED_SPECIAL_LIST[] = {
188 size_t nc4_chunk_cache_size = CHUNK_CACHE_SIZE;
189 size_t nc4_chunk_cache_nelems = CHUNK_CACHE_NELEMS;
190 float nc4_chunk_cache_preemption = CHUNK_CACHE_PREEMPTION;
195 static hid_t h5_native_type_constant_g[NUM_TYPES];
196 static const char nc_type_name_g[NUM_TYPES][
NC_MAX_NAME + 1] = {
"char",
"byte",
"short",
197 "int",
"float",
"double",
"ubyte",
198 "ushort",
"uint",
"int64",
204 static const int nc_type_size_g[NUM_TYPES] = {
sizeof(char),
sizeof(
char),
sizeof(short),
205 sizeof(
int),
sizeof(float),
sizeof(
double),
sizeof(
unsigned char),
206 sizeof(
unsigned short),
sizeof(
unsigned int),
sizeof(
long long),
207 sizeof(
unsigned long long),
sizeof(
char *)};
212 nc_set_chunk_cache(
size_t size,
size_t nelems,
float preemption)
214 if (preemption < 0 || preemption > 1)
216 nc4_chunk_cache_size = size;
217 nc4_chunk_cache_nelems = nelems;
218 nc4_chunk_cache_preemption = preemption;
225 nc_get_chunk_cache(
size_t *sizep,
size_t *nelemsp,
float *preemptionp)
228 *sizep = nc4_chunk_cache_size;
231 *nelemsp = nc4_chunk_cache_nelems;
234 *preemptionp = nc4_chunk_cache_preemption;
240 nc_set_chunk_cache_ints(
int size,
int nelems,
int preemption)
242 if (size <= 0 || nelems <= 0 || preemption < 0 || preemption > 100)
244 nc4_chunk_cache_size = size;
245 nc4_chunk_cache_nelems = nelems;
246 nc4_chunk_cache_preemption = (float)preemption / 100;
251 nc_get_chunk_cache_ints(
int *sizep,
int *nelemsp,
int *preemptionp)
254 *sizep = (int)nc4_chunk_cache_size;
256 *nelemsp = (int)nc4_chunk_cache_nelems;
258 *preemptionp = (int)(nc4_chunk_cache_preemption * 100);
288 #define MAGIC_NUMBER_LEN 4 289 #define NC_HDF5_FILE 1 290 #define NC_HDF4_FILE 2 292 nc_check_for_hdf(
const char *path,
int flags,
void* parameters,
int *hdf_file)
294 char blob[MAGIC_NUMBER_LEN];
297 NC_MPI_INFO* mpiinfo = (NC_MPI_INFO*)parameters;
298 MPI_Comm comm = MPI_COMM_WORLD;
299 MPI_Info info = MPI_INFO_NULL;
302 NC_MEM_INFO* meminfo = (NC_MEM_INFO*)parameters;
306 comm = mpiinfo->comm;
307 info = mpiinfo->info;
312 LOG((3,
"%s: path %s", __func__, path));
315 if(!inmemory && H5Fis_hdf5(path))
317 *hdf_file = NC_HDF5_FILE;
323 if (!inmemory && use_parallel)
328 if ((retval = MPI_File_open(comm, (
char *)path, MPI_MODE_RDONLY,
329 info, &fh)) != MPI_SUCCESS)
331 if ((retval = MPI_File_read(fh, blob, MAGIC_NUMBER_LEN, MPI_CHAR,
332 &status)) != MPI_SUCCESS)
334 if ((retval = MPI_File_close(&fh)) != MPI_SUCCESS)
341 if (!(fp = fopen(path,
"r")) ||
342 fread(blob, MAGIC_NUMBER_LEN, 1, fp) != 1) {
349 if(meminfo->size < MAGIC_NUMBER_LEN)
351 memcpy(blob,meminfo->memory,MAGIC_NUMBER_LEN);
355 if (memcmp(blob,
"\016\003\023\001", 4)==0)
356 *hdf_file = NC_HDF4_FILE;
357 else if (memcmp(blob,
"HDF", 3)==0)
358 *hdf_file = NC_HDF5_FILE;
368 nc4_create_file(
const char *path,
int cmode, MPI_Comm comm, MPI_Info info,
371 hid_t fcpl_id, fapl_id = -1;
375 NC_HDF5_FILE_INFO_T* nc4_info = NULL;
386 flags = H5F_ACC_TRUNC;
388 flags = H5F_ACC_EXCL;
390 flags = H5F_ACC_TRUNC;
392 LOG((3,
"%s: path %s mode 0x%x", __func__, path, cmode));
397 if (cmode & NC_DISKLESS) {
398 #ifndef USE_PARALLEL4 402 }
else if ((cmode & NC_NOCLOBBER) && (fp = fopen(path,
"r"))) {
408 if ((retval = nc4_nc4f_list_add(nc, path, (
NC_WRITE | cmode))))
410 nc4_info = NC4_DATA(nc);
411 assert(nc4_info && nc4_info->root_grp);
416 if ((fapl_id = H5Pcreate(H5P_FILE_ACCESS)) < 0)
422 if (H5Pset_fclose_degree(fapl_id, H5F_CLOSE_SEMI))
425 if (H5Pset_fclose_degree(fapl_id, H5F_CLOSE_STRONG))
434 nc4_info->parallel = NC_TRUE;
435 if (cmode & NC_MPIIO)
437 LOG((4,
"creating parallel file with MPI/IO"));
438 if (H5Pset_fapl_mpio(fapl_id, comm, info) < 0)
441 #ifdef USE_PARALLEL_POSIX 444 LOG((4,
"creating parallel file with MPI/posix"));
445 if (H5Pset_fapl_mpiposix(fapl_id, comm, 0) < 0)
458 if (MPI_SUCCESS != MPI_Comm_dup(comm, &nc4_info->comm))
461 if (MPI_INFO_NULL != info)
463 if (MPI_SUCCESS != MPI_Info_dup(info, &nc4_info->info))
470 nc4_info->info = info;
474 if(cmode & NC_DISKLESS) {
475 if (H5Pset_fapl_core(fapl_id, 4096, persist))
478 if (H5Pset_cache(fapl_id, 0, nc4_chunk_cache_nelems, nc4_chunk_cache_size,
479 nc4_chunk_cache_preemption) < 0)
481 LOG((4,
"%s: set HDF raw chunk cache to size %d nelems %d preemption %f",
482 __func__, nc4_chunk_cache_size, nc4_chunk_cache_nelems, nc4_chunk_cache_preemption));
485 #ifdef HDF5_HAS_LIBVER_BOUNDS 486 if (H5Pset_libver_bounds(fapl_id, H5F_LIBVER_EARLIEST, H5F_LIBVER_LATEST) < 0)
491 if ((fcpl_id = H5Pcreate(H5P_FILE_CREATE)) < 0)
498 if (H5Pset_obj_track_times(fcpl_id,0)<0)
504 if (H5Pset_link_creation_order(fcpl_id, (H5P_CRT_ORDER_TRACKED |
505 H5P_CRT_ORDER_INDEXED)) < 0)
507 if (H5Pset_attr_creation_order(fcpl_id, (H5P_CRT_ORDER_TRACKED |
508 H5P_CRT_ORDER_INDEXED)) < 0)
512 #ifdef HDF5_HAS_COLL_METADATA_OPS 513 H5Pset_all_coll_metadata_ops(fapl_id, 1 );
514 H5Pset_coll_metadata_write(fapl_id, 1);
517 if ((nc4_info->hdfid = H5Fcreate(path, flags, fcpl_id, fapl_id)) < 0)
523 if ((nc4_info->root_grp->hdf_grpid = H5Gopen2(nc4_info->hdfid,
"/",
528 if (H5Pclose(fapl_id) < 0 || H5Pclose(fcpl_id) < 0)
536 nc4_info->flags |= NC_INDEF;
538 NC4_get_fileinfo(nc4_info,&globalpropinfo);
539 NC4_put_propattr(nc4_info);
545 if (comm_duped) MPI_Comm_free(&nc4_info->comm);
546 if (info_duped) MPI_Info_free(&nc4_info->info);
551 if (fapl_id != H5P_DEFAULT) H5Pclose(fapl_id);
552 if(!nc4_info)
return retval;
553 close_netcdf4_file(nc4_info,1);
573 NC4_create(
const char* path,
int cmode,
size_t initialsz,
int basepe,
574 size_t *chunksizehintp,
int use_parallel,
void *parameters,
575 NC_Dispatch *dispatch, NC* nc_file)
577 MPI_Comm comm = MPI_COMM_WORLD;
578 MPI_Info info = MPI_INFO_NULL;
581 assert(nc_file && path);
583 LOG((1,
"%s: path %s cmode 0x%x comm %d info %d",
584 __func__, path, cmode, comm, info));
589 comm = ((NC_MPI_INFO *)parameters)->comm;
590 info = ((NC_MPI_INFO *)parameters)->info;
595 if (!nc4_hdf5_initialized)
596 nc4_hdf5_initialize();
599 if((cmode & ILLEGAL_CREATE_FLAGS) != 0)
603 if((cmode & (NC_MPIIO|NC_MPIPOSIX)) == (NC_MPIIO|NC_MPIPOSIX))
607 if((cmode & (NC_MPIIO | NC_MPIPOSIX)) && (cmode & NC_DISKLESS))
610 #ifndef USE_PARALLEL_POSIX 614 if(cmode & NC_MPIPOSIX)
616 cmode &= ~NC_MPIPOSIX;
631 LOG((2,
"cmode after applying default format: 0x%x", cmode));
633 nc_file->int_ncid = nc_file->ext_ncid;
635 res = nc4_create_file(path, cmode, comm, info, nc_file);
647 read_scale(NC_GRP_INFO_T *grp, hid_t datasetid,
const char *obj_name,
648 const H5G_stat_t *statbuf, hsize_t scale_size, hsize_t max_scale_size,
651 NC_DIM_INFO_T *new_dim;
653 htri_t attr_exists = -1;
655 int dimscale_created = 0;
656 short initial_next_dimid = grp->nc4_info->next_dimid;
660 if ((retval = nc4_dim_list_add(&grp->dim, &new_dim)))
666 if ((attr_exists = H5Aexists(datasetid, NC_DIMID_ATT_NAME)) < 0)
670 if ((attid = H5Aopen_name(datasetid, NC_DIMID_ATT_NAME)) < 0)
673 if (H5Aread(attid, H5T_NATIVE_INT, &new_dim->dimid) < 0)
677 if (new_dim->dimid >= grp->nc4_info->next_dimid)
678 grp->nc4_info->next_dimid = new_dim->dimid + 1;
683 new_dim->dimid = grp->nc4_info->next_dimid++;
686 if (!(new_dim->name = strdup(obj_name)))
691 new_dim->too_long = NC_TRUE;
694 new_dim->len = scale_size;
695 new_dim->hdf5_objid.fileno[0] = statbuf->fileno[0];
696 new_dim->hdf5_objid.fileno[1] = statbuf->fileno[1];
697 new_dim->hdf5_objid.objno[0] = statbuf->objno[0];
698 new_dim->hdf5_objid.objno[1] = statbuf->objno[1];
699 new_dim->hash = hash_fast(obj_name, strlen(obj_name));
703 if (max_scale_size == H5S_UNLIMITED)
704 new_dim->unlimited = NC_TRUE;
709 if (H5DSget_scale_name(datasetid, dimscale_name_att,
NC_MAX_NAME) >= 0)
711 if (!strncmp(dimscale_name_att, DIM_WITHOUT_VARIABLE,
712 strlen(DIM_WITHOUT_VARIABLE)))
714 if (new_dim->unlimited)
716 size_t len = 0, *lenp = &len;
718 if ((retval = nc4_find_dim_len(grp, new_dim->dimid, &lenp)))
720 new_dim->len = *lenp;
724 new_dim->hdf_dimscaleid = datasetid;
725 H5Iinc_ref(new_dim->hdf_dimscaleid);
734 if (attid > 0 && H5Aclose(attid) < 0)
738 if (retval < 0 && dimscale_created)
741 if ((retval = nc4_dim_list_del(&grp->dim, new_dim)))
745 grp->nc4_info->next_dimid = initial_next_dimid;
754 read_coord_dimids(NC_GRP_INFO_T *grp, NC_VAR_INFO_T *var)
756 hid_t coord_att_typeid = -1, coord_attid = -1, spaceid = -1;
764 if ((coord_attid = H5Aopen_name(var->hdf_datasetid, COORDINATES)) < 0) ret++;
765 if (!ret && (coord_att_typeid = H5Aget_type(coord_attid)) < 0) ret++;
768 if (!ret && (spaceid = H5Aget_space(coord_attid)) < 0) ret++;
772 if (!ret && (npoints = H5Sget_simple_extent_npoints(spaceid)) < 0) ret++;
776 if (!ret && npoints != var->ndims) ret++;
778 if (!ret && H5Aread(coord_attid, coord_att_typeid, var->dimids) < 0) ret++;
779 LOG((4,
"dimscale %s is multidimensional and has coords", var->name));
782 for (d = 0; d < var->ndims; d++) {
784 nc4_find_dim(grp, var->dimids[d], &var->dim[d], NULL);
788 if (spaceid >= 0 && H5Sclose(spaceid) < 0) ret++;
792 if (coord_att_typeid >= 0 && H5Tclose(coord_att_typeid) < 0) ret++;
793 if (coord_attid >= 0 && H5Aclose(coord_attid) < 0) ret++;
800 dimscale_visitor(hid_t did,
unsigned dim, hid_t dsid,
801 void *dimscale_hdf5_objids)
806 if (H5Gget_objinfo(dsid,
".", 1, &statbuf) < 0)
810 (*(HDF5_OBJID_T *)dimscale_hdf5_objids).fileno[0] = statbuf.fileno[0];
811 (*(HDF5_OBJID_T *)dimscale_hdf5_objids).fileno[1] = statbuf.fileno[1];
812 (*(HDF5_OBJID_T *)dimscale_hdf5_objids).objno[0] = statbuf.objno[0];
813 (*(HDF5_OBJID_T *)dimscale_hdf5_objids).objno[1] = statbuf.objno[1];
819 get_netcdf_type(NC_HDF5_FILE_INFO_T *h5, hid_t native_typeid,
822 NC_TYPE_INFO_T *type;
824 htri_t is_str, equal = 0;
828 if ((
class = H5Tget_class(native_typeid)) < 0)
833 if (
class == H5T_STRING)
835 if ((is_str = H5Tis_variable_str(native_typeid)) < 0)
843 else if (
class == H5T_INTEGER ||
class == H5T_FLOAT)
847 if ((equal = H5Tequal(native_typeid, H5T_NATIVE_SCHAR)) < 0)
854 if ((equal = H5Tequal(native_typeid, H5T_NATIVE_SHORT)) < 0)
861 if ((equal = H5Tequal(native_typeid, H5T_NATIVE_INT)) < 0)
868 if ((equal = H5Tequal(native_typeid, H5T_NATIVE_FLOAT)) < 0)
875 if ((equal = H5Tequal(native_typeid, H5T_NATIVE_DOUBLE)) < 0)
882 if ((equal = H5Tequal(native_typeid, H5T_NATIVE_UCHAR)) < 0)
889 if ((equal = H5Tequal(native_typeid, H5T_NATIVE_USHORT)) < 0)
896 if ((equal = H5Tequal(native_typeid, H5T_NATIVE_UINT)) < 0)
903 if ((equal = H5Tequal(native_typeid, H5T_NATIVE_LLONG)) < 0)
910 if ((equal = H5Tequal(native_typeid, H5T_NATIVE_ULLONG)) < 0)
921 if((type = nc4_rec_find_hdf_type(h5->root_grp, native_typeid)))
923 *xtype = type->nc_typeid;
935 get_type_info2(NC_HDF5_FILE_INFO_T *h5, hid_t datasetid,
936 NC_TYPE_INFO_T **type_info)
938 htri_t is_str, equal = 0;
940 hid_t native_typeid, hdf_typeid;
944 assert(h5 && type_info);
952 if (!h5_native_type_constant_g[1])
954 h5_native_type_constant_g[1] = H5T_NATIVE_SCHAR;
955 h5_native_type_constant_g[2] = H5T_NATIVE_SHORT;
956 h5_native_type_constant_g[3] = H5T_NATIVE_INT;
957 h5_native_type_constant_g[4] = H5T_NATIVE_FLOAT;
958 h5_native_type_constant_g[5] = H5T_NATIVE_DOUBLE;
959 h5_native_type_constant_g[6] = H5T_NATIVE_UCHAR;
960 h5_native_type_constant_g[7] = H5T_NATIVE_USHORT;
961 h5_native_type_constant_g[8] = H5T_NATIVE_UINT;
962 h5_native_type_constant_g[9] = H5T_NATIVE_LLONG;
963 h5_native_type_constant_g[10] = H5T_NATIVE_ULLONG;
967 if ((hdf_typeid = H5Dget_type(datasetid)) < 0)
972 if ((native_typeid = H5Tget_native_type(hdf_typeid, H5T_DIR_DEFAULT)) < 0)
976 if ((
class = H5Tget_class(native_typeid)) < 0)
980 if (
class == H5T_STRING ||
class == H5T_INTEGER ||
class == H5T_FLOAT)
983 if (!(*type_info = calloc(1,
sizeof(NC_TYPE_INFO_T))))
988 if (
class == H5T_STRING)
990 if ((is_str = H5Tis_variable_str(native_typeid)) < 0)
993 if (is_str || H5Tget_size(hdf_typeid) > 1)
1003 (*type_info)->nc_type_class =
NC_CHAR;
1006 else if (
class == H5T_INTEGER ||
class == H5T_FLOAT)
1008 for (t = 1; t < NUM_TYPES - 1; t++)
1010 if ((equal = H5Tequal(native_typeid, h5_native_type_constant_g[t])) < 0)
1022 if((order = H5Tget_order(hdf_typeid)) < 0)
1025 if(order == H5T_ORDER_LE)
1027 else if(order == H5T_ORDER_BE)
1032 if(
class == H5T_INTEGER)
1033 (*type_info)->nc_type_class =
NC_INT;
1035 (*type_info)->nc_type_class =
NC_FLOAT;
1037 (*type_info)->nc_typeid = nc_type_constant_g[t];
1038 (*type_info)->size = nc_type_size_g[t];
1039 if (!((*type_info)->name = strdup(nc_type_name_g[t])))
1041 (*type_info)->hdf_typeid = hdf_typeid;
1042 (*type_info)->native_hdf_typeid = native_typeid;
1047 NC_TYPE_INFO_T *type;
1050 if((type = nc4_rec_find_hdf_type(h5->root_grp, native_typeid)))
1056 if (H5Tclose(native_typeid) < 0)
1058 if (H5Tclose(hdf_typeid) < 0)
1070 read_hdf5_att(NC_GRP_INFO_T *grp, hid_t attid, NC_ATT_INFO_T *att)
1072 hid_t spaceid = 0, file_typeid = 0;
1073 hsize_t dims[1] = {0};
1077 hssize_t att_npoints;
1078 H5T_class_t att_class;
1079 int fixed_len_string = 0;
1080 size_t fixed_size = 0;
1083 LOG((5,
"%s: att->attnum %d att->name %s att->nc_typeid %d att->len %d",
1084 __func__, att->attnum, att->name, (
int)att->nc_typeid, att->len));
1087 if ((file_typeid = H5Aget_type(attid)) < 0)
1089 if ((att->native_hdf_typeid = H5Tget_native_type(file_typeid, H5T_DIR_DEFAULT)) < 0)
1091 if ((att_class = H5Tget_class(att->native_hdf_typeid)) < 0)
1093 if (att_class == H5T_STRING && !H5Tis_variable_str(att->native_hdf_typeid))
1096 if (!(fixed_size = H5Tget_size(att->native_hdf_typeid)))
1099 if ((retval = get_netcdf_type(grp->nc4_info, att->native_hdf_typeid, &(att->nc_typeid))))
1104 if ((spaceid = H5Aget_space(attid)) < 0)
1109 if ((att_ndims = H5Sget_simple_extent_ndims(spaceid)) < 0)
1111 if ((att_npoints = H5Sget_simple_extent_npoints(spaceid)) < 0)
1116 if (att_ndims == 0 && att_npoints == 0)
1118 else if (att->nc_typeid == NC_STRING)
1119 dims[0] = att_npoints;
1120 else if (att->nc_typeid ==
NC_CHAR)
1126 if (!(dims[0] = H5Tget_size(file_typeid)))
1133 dims[0] = att_npoints;
1138 H5S_class_t space_class;
1145 if ((space_class = H5Sget_simple_extent_type(spaceid)) < 0)
1149 if (H5S_NULL == space_class)
1153 if (H5S_SCALAR == space_class)
1158 if (H5Sget_simple_extent_dims(spaceid, dims, NULL) < 0)
1170 if ((retval = nc4_get_typelen_mem(grp->nc4_info, att->nc_typeid, 0,
1173 if (att_class == H5T_VLEN)
1175 if (!(att->vldata = malloc((
unsigned int)(att->len *
sizeof(hvl_t)))))
1177 if (H5Aread(attid, att->native_hdf_typeid, att->vldata) < 0)
1180 else if (att->nc_typeid == NC_STRING)
1182 if (!(att->stdata = calloc(att->len,
sizeof(
char *))))
1195 if (fixed_len_string)
1198 char *contig_buf, *cur;
1201 if (!(contig_buf = malloc(att->len * fixed_size *
sizeof(
char))))
1205 if (H5Aread(attid, att->native_hdf_typeid, contig_buf) < 0) {
1214 for (i = 0; i < att->len; i++)
1216 if (!(att->stdata[i] = malloc(fixed_size))) {
1220 strncpy(att->stdata[i], cur, fixed_size);
1230 if (H5Aread(attid, att->native_hdf_typeid, att->stdata) < 0)
1236 if (!(att->data = malloc((
unsigned int)(att->len * type_size))))
1238 if (H5Aread(attid, att->native_hdf_typeid, att->data) < 0)
1243 if (H5Tclose(file_typeid) < 0)
1245 if (H5Sclose(spaceid) < 0)
1254 if (H5Tclose(file_typeid) < 0)
1256 if (spaceid > 0 && H5Sclose(spaceid) < 0)
1267 read_type(NC_GRP_INFO_T *grp, hid_t hdf_typeid,
char *type_name)
1269 NC_TYPE_INFO_T *type;
1271 hid_t native_typeid;
1275 assert(grp && type_name);
1277 LOG((4,
"%s: type_name %s grp->name %s", __func__, type_name, grp->name));
1280 if ((native_typeid = H5Tget_native_type(hdf_typeid, H5T_DIR_DEFAULT)) < 0)
1284 if (!(type_size = H5Tget_size(native_typeid)))
1286 LOG((5,
"type_size %d", type_size));
1289 if ((retval = nc4_type_list_add(grp, type_size, type_name, &type)))
1293 type->committed = NC_TRUE;
1294 type->hdf_typeid = hdf_typeid;
1295 H5Iinc_ref(type->hdf_typeid);
1296 type->native_hdf_typeid = native_typeid;
1299 if ((
class = H5Tget_class(hdf_typeid)) < 0)
1311 char* member_name = NULL;
1318 if ((nmembers = H5Tget_nmembers(hdf_typeid)) < 0)
1320 LOG((5,
"compound type has %d members", nmembers));
1321 for (m = 0; m < nmembers; m++)
1323 hid_t member_hdf_typeid;
1324 hid_t member_native_typeid;
1325 size_t member_offset;
1326 H5T_class_t mem_class;
1332 if ((member_hdf_typeid = H5Tget_member_type(type->native_hdf_typeid, m)) < 0)
1335 if ((member_native_typeid = H5Tget_native_type(member_hdf_typeid, H5T_DIR_DEFAULT)) < 0)
1339 member_name = H5Tget_member_name(type->native_hdf_typeid, m);
1340 if (!member_name || strlen(member_name) >
NC_MAX_NAME) {
1346 strncpy(jna,member_name,1000);
1352 member_offset = H5Tget_member_offset(type->native_hdf_typeid, m);
1355 if ((mem_class = H5Tget_class(member_hdf_typeid)) < 0)
1357 if (mem_class == H5T_ARRAY)
1363 if ((ndims = H5Tget_array_ndims(member_hdf_typeid)) < 0) {
1367 if (H5Tget_array_dims(member_hdf_typeid, dims, NULL) != ndims) {
1371 for (d = 0; d < ndims; d++)
1372 dim_size[d] = dims[d];
1375 if ((retval = get_netcdf_type(grp->nc4_info, H5Tget_super(member_hdf_typeid),
1380 if ((retval = nc4_field_list_add(&type->u.c.field, type->u.c.num_fields++, member_name,
1381 member_offset, H5Tget_super(member_hdf_typeid),
1382 H5Tget_super(member_native_typeid),
1383 member_xtype, ndims, dim_size)))
1389 if ((retval = get_netcdf_type(grp->nc4_info, member_native_typeid,
1394 if ((retval = nc4_field_list_add(&type->u.c.field, type->u.c.num_fields++, member_name,
1395 member_offset, member_hdf_typeid, member_native_typeid,
1396 member_xtype, 0, NULL)))
1400 hdf5free(member_name);
1403 hdf5free(member_name);
1418 if ((ret = H5Tis_variable_str(hdf_typeid)) < 0)
1424 hid_t base_hdf_typeid;
1427 type->nc_type_class =
NC_VLEN;
1431 if (!(base_hdf_typeid = H5Tget_super(native_typeid)))
1435 if (!(type_size = H5Tget_size(base_hdf_typeid)))
1439 if ((retval = get_netcdf_type(grp->nc4_info, base_hdf_typeid,
1442 LOG((5,
"base_hdf_typeid 0x%x type_size %d base_nc_type %d",
1443 base_hdf_typeid, type_size, base_nc_type));
1446 type->u.v.base_nc_typeid = base_nc_type;
1447 type->u.v.base_hdf_typeid = base_hdf_typeid;
1458 hid_t base_hdf_typeid;
1462 char *member_name = NULL;
1467 type->nc_type_class =
NC_ENUM;
1471 if (!(base_hdf_typeid = H5Tget_super(hdf_typeid)))
1474 if (!(type_size = H5Tget_size(base_hdf_typeid)))
1477 if ((retval = get_netcdf_type(grp->nc4_info, base_hdf_typeid,
1480 LOG((5,
"base_hdf_typeid 0x%x type_size %d base_nc_type %d",
1481 base_hdf_typeid, type_size, base_nc_type));
1484 type->u.e.base_nc_typeid = base_nc_type;
1485 type->u.e.base_hdf_typeid = base_hdf_typeid;
1488 if ((type->u.e.num_members = H5Tget_nmembers(hdf_typeid)) < 0)
1492 if (!(value = calloc(1, type_size)))
1496 for (i = 0; i < type->u.e.num_members; i++)
1500 if (!(member_name = H5Tget_member_name(hdf_typeid, i)))
1506 strncpy(jna,member_name,1000);
1515 if (H5Tget_member_value(hdf_typeid, i, value) < 0)
1522 if ((retval = nc4_enum_member_add(&type->u.e.enum_member, type->size,
1523 member_name, value)))
1528 hdf5free(member_name);
1531 hdf5free(member_name);
1533 if(value) free(value);
1540 LOG((0,
"unknown class"));
1551 read_var(NC_GRP_INFO_T *grp, hid_t datasetid,
const char *obj_name,
1552 size_t ndims, NC_DIM_INFO_T *dim)
1554 NC_VAR_INFO_T *var = NULL;
1555 hid_t access_pid = 0;
1558 const char** reserved;
1561 att_iter_info att_info;
1562 char att_name[NC_MAX_HDF5_NAME + 1];
1564 #define CD_NELEMS_ZLIB 1 1565 #define CD_NELEMS_SZIP 4 1566 H5Z_filter_t filter;
1568 unsigned int cd_values[CD_NELEMS_SZIP];
1569 size_t cd_nelems = CD_NELEMS_SZIP;
1571 H5D_fill_value_t fill_status;
1572 H5D_layout_t layout;
1578 assert(obj_name && grp);
1579 LOG((4,
"%s: obj_name %s", __func__, obj_name));
1582 if ((retval = nc4_var_add(&var)))
1586 var->hdf_datasetid = datasetid;
1587 H5Iinc_ref(var->hdf_datasetid);
1589 var->varid = grp->nvars++;
1590 var->created = NC_TRUE;
1597 if (!(var->dim = calloc(var->ndims,
sizeof(NC_DIM_INFO_T *))))
1599 if (!(var->dimids = calloc(var->ndims,
sizeof(
int))))
1604 if ((access_pid = H5Dget_access_plist(datasetid)) < 0)
1611 if ((H5Pget_chunk_cache(access_pid, &(var->chunk_cache_nelems),
1612 &(var->chunk_cache_size), &rdcc_w0)) < 0)
1614 var->chunk_cache_preemption = rdcc_w0;
1619 if (strlen(obj_name) > strlen(NON_COORD_PREPEND) &&
1620 !strncmp(obj_name, NON_COORD_PREPEND, strlen(NON_COORD_PREPEND)))
1623 if (!(var->name = malloc(((strlen(obj_name) - strlen(NON_COORD_PREPEND))+ 1) *
sizeof(
char))))
1626 strcpy(var->name, &obj_name[strlen(NON_COORD_PREPEND)]);
1629 if (!(var->hdf5_name = malloc((strlen(obj_name) + 1) *
sizeof(
char))))
1632 strcpy(var->hdf5_name, obj_name);
1637 if (!(var->name = malloc((strlen(obj_name) + 1) *
sizeof(
char))))
1640 strcpy(var->name, obj_name);
1643 var->hash = hash_fast(var->name, strlen(var->name));
1647 if ((propid = H5Dget_create_plist(datasetid)) < 0)
1654 if ((layout = H5Pget_layout(propid)) < -1)
1656 if (layout == H5D_CHUNKED)
1660 if (!(var->chunksizes = malloc(var->ndims *
sizeof(
size_t))))
1662 for (d = 0; d < var->ndims; d++)
1663 var->chunksizes[d] = chunksize[d];
1665 else if (layout == H5D_CONTIGUOUS || layout == H5D_COMPACT)
1666 var->contiguous = NC_TRUE;
1670 if ((num_filters = H5Pget_nfilters(propid)) < 0)
1672 for (f = 0; f < num_filters; f++)
1674 if ((filter = H5Pget_filter2(propid, f, NULL, &cd_nelems,
1675 cd_values, 0, NULL, NULL)) < 0)
1679 case H5Z_FILTER_SHUFFLE:
1680 var->shuffle = NC_TRUE;
1683 case H5Z_FILTER_FLETCHER32:
1684 var->fletcher32 = NC_TRUE;
1687 case H5Z_FILTER_DEFLATE:
1688 var->deflate = NC_TRUE;
1689 if (cd_nelems != CD_NELEMS_ZLIB || cd_values[0] > MAX_DEFLATE_LEVEL)
1691 var->deflate_level = cd_values[0];
1694 case H5Z_FILTER_SZIP:
1695 var->szip = NC_TRUE;
1696 if (cd_nelems != CD_NELEMS_SZIP)
1698 var->options_mask = cd_values[0];
1699 var->pixels_per_block = cd_values[1];
1703 LOG((1,
"Yikes! Unknown filter type found on dataset!"));
1709 if ((retval = get_type_info2(grp->nc4_info, datasetid,
1714 var->type_info->rc++;
1717 if (H5Pfill_value_defined(propid, &fill_status) < 0)
1721 if (fill_status == H5D_FILL_VALUE_USER_DEFINED)
1724 if (!var->fill_value)
1726 if (var->type_info->nc_type_class ==
NC_VLEN)
1728 if (!(var->fill_value = malloc(
sizeof(
nc_vlen_t))))
1731 else if (var->type_info->nc_type_class == NC_STRING)
1733 if (!(var->fill_value = malloc(
sizeof(
char *))))
1738 assert(var->type_info->size);
1739 if (!(var->fill_value = malloc(var->type_info->size)))
1745 if (H5Pget_fill_value(propid, var->type_info->native_hdf_typeid,
1746 var->fill_value) < 0)
1750 var->no_fill = NC_TRUE;
1756 var->dimscale = NC_TRUE;
1759 if ((retval = read_coord_dimids(grp, var)))
1765 assert(0 == strcmp(var->name, dim->name));
1767 var->dimids[0] = dim->dimid;
1770 dim->coord_var = var;
1782 num_scales = H5DSget_num_scales(datasetid, 0);
1786 if (num_scales && ndims)
1790 if (NULL == (var->dimscale_attached = calloc(ndims,
sizeof(nc_bool_t))))
1795 if (NULL == (var->dimscale_hdf5_objids = malloc(ndims *
sizeof(
struct hdf5_objid))))
1797 for (d = 0; d < var->ndims; d++)
1799 if (H5DSiterate_scales(var->hdf_datasetid, d, NULL, dimscale_visitor,
1800 &(var->dimscale_hdf5_objids[d])) < 0)
1802 var->dimscale_attached[d] = NC_TRUE;
1813 if ((H5Aiterate2(var->hdf_datasetid, H5_INDEX_CRT_ORDER, H5_ITER_INC, NULL, att_read_var_callbk, &att_info)) < 0)
1816 nc4_vararray_add(grp, var);
1820 if ((retval = nc4_adjust_var_cache(grp, var)))
1826 if (incr_id_rc && H5Idec_ref(datasetid) < 0)
1828 if (var && nc4_var_del(var))
1831 if (access_pid && H5Pclose(access_pid) < 0)
1836 if (propid > 0 && H5Pclose(propid) < 0)
1847 read_grp_atts(NC_GRP_INFO_T *grp)
1852 NC_TYPE_INFO_T *type;
1853 char obj_name[NC_MAX_HDF5_NAME + 1];
1858 num_obj = H5Aget_num_attrs(grp->hdf_grpid);
1859 for (i = 0; i < num_obj; i++)
1861 if ((attid = H5Aopen_idx(grp->hdf_grpid, (
unsigned int)i)) < 0)
1863 if (H5Aget_name(attid,
NC_MAX_NAME + 1, obj_name) < 0)
1865 LOG((3,
"reading attribute of _netCDF group, named %s", obj_name));
1868 if(grp->nc4_info->root_grp == grp) {
1869 const char** reserved = NC_RESERVED_ATT_LIST;
1871 for(;*reserved;reserved++) {
1872 if(strcmp(*reserved,obj_name)==0) {
1883 if(strcmp(obj_name, NC3_STRICT_ATT_NAME)==0)
1887 if ((retval = nc4_att_list_add(&grp->att, &att)))
1892 if (!(att->name = malloc((max_len + 1) *
sizeof(
char))))
1894 strncpy(att->name, obj_name, max_len);
1895 att->name[max_len] = 0;
1896 att->attnum = grp->natts++;
1897 retval = read_hdf5_att(grp, attid, att);
1899 if((retval = nc4_att_list_del(&grp->att, att)))
1904 att->created = NC_TRUE;
1905 if ((retval = nc4_find_type(grp->nc4_info, att->nc_typeid, &type)))
1916 if(H5Aclose(attid) < 0)
1925 read_dataset(NC_GRP_INFO_T *grp, hid_t datasetid,
const char *obj_name,
1926 const H5G_stat_t *statbuf)
1928 NC_DIM_INFO_T *dim = NULL;
1935 if ((spaceid = H5Dget_space(datasetid)) < 0)
1940 if ((ndims = H5Sget_simple_extent_ndims(spaceid)) < 0)
1944 if ((is_scale = H5DSis_scale(datasetid)) < 0)
1948 hsize_t dims[H5S_MAX_RANK];
1949 hsize_t max_dims[H5S_MAX_RANK];
1952 if (H5Sget_simple_extent_dims(spaceid, dims, max_dims) < 0)
1956 if ((retval = read_scale(grp, datasetid, obj_name, statbuf, dims[0],
1957 max_dims[0], &dim)))
1964 if (NULL == dim || (dim && !dim->hdf_dimscaleid))
1965 if ((retval = read_var(grp, datasetid, obj_name, ndims, dim)))
1969 if (spaceid && H5Sclose(spaceid) <0)
1986 if (!(new_oinfo = calloc(1,
sizeof(*new_oinfo))))
1990 memcpy(new_oinfo, oinfo,
sizeof(*oinfo));
1995 (*tail)->next = new_oinfo;
2000 assert(NULL == *head);
2001 *head = *tail = new_oinfo;
2008 nc4_rec_read_metadata_cb(hid_t grpid,
const char *name,
const H5L_info_t *info,
2013 int retval = H5_ITER_CONT;
2016 memset(&oinfo, 0,
sizeof(oinfo));
2019 if ((oinfo.oid = H5Oopen(grpid, name, H5P_DEFAULT)) < 0)
2020 BAIL(H5_ITER_ERROR);
2023 if (H5Gget_objinfo(oinfo.oid,
".", 1, &oinfo.statbuf) < 0)
2024 BAIL(H5_ITER_ERROR);
2029 switch(oinfo.statbuf.type)
2032 LOG((3,
"found group %s", oinfo.oname));
2038 if (nc4_rec_read_metadata_cb_list_add(&udata->grps_head, &udata->grps_tail, &oinfo))
2039 BAIL(H5_ITER_ERROR);
2043 LOG((3,
"found dataset %s", oinfo.oname));
2047 if ((retval = read_dataset(udata->grp, oinfo.oid, oinfo.oname, &oinfo.statbuf)))
2055 BAIL(H5_ITER_ERROR);
2057 retval = H5_ITER_CONT;
2061 if (H5Oclose(oinfo.oid) < 0)
2062 BAIL(H5_ITER_ERROR);
2066 LOG((3,
"found datatype %s", oinfo.oname));
2069 if (read_type(udata->grp, oinfo.oid, oinfo.oname))
2070 BAIL(H5_ITER_ERROR);
2073 if (H5Oclose(oinfo.oid) < 0)
2074 BAIL(H5_ITER_ERROR);
2078 LOG((0,
"Unknown object class %d in %s!", oinfo.statbuf.type, __func__));
2079 BAIL(H5_ITER_ERROR);
2085 if (oinfo.oid > 0 && H5Oclose(oinfo.oid) < 0)
2086 BAIL2(H5_ITER_ERROR);
2100 nc4_rec_read_metadata(NC_GRP_INFO_T *grp)
2106 unsigned crt_order_flags = 0;
2107 H5_index_t iter_index;
2110 assert(grp && grp->name);
2111 LOG((3,
"%s: grp->name %s", __func__, grp->name));
2114 memset(&udata, 0,
sizeof(udata));
2118 if (!grp->hdf_grpid)
2122 if ((grp->hdf_grpid = H5Gopen2(grp->parent->hdf_grpid,
2123 grp->name, H5P_DEFAULT)) < 0)
2128 if ((grp->hdf_grpid = H5Gopen2(grp->nc4_info->hdfid,
2129 "/", H5P_DEFAULT)) < 0)
2133 assert(grp->hdf_grpid > 0);
2136 pid = H5Gget_create_plist(grp->hdf_grpid);
2137 H5Pget_link_creation_order(pid, &crt_order_flags);
2138 if (H5Pclose(pid) < 0)
2142 if (crt_order_flags & H5P_CRT_ORDER_TRACKED)
2143 iter_index = H5_INDEX_CRT_ORDER;
2146 NC_HDF5_FILE_INFO_T *h5 = grp->nc4_info;
2152 iter_index = H5_INDEX_NAME;
2161 if (H5Literate(grp->hdf_grpid, iter_index, H5_ITER_INC, &idx,
2162 nc4_rec_read_metadata_cb, (
void *)&udata) < 0)
2169 for (oinfo = udata.grps_head; oinfo; oinfo = udata.grps_head)
2171 NC_GRP_INFO_T *child_grp;
2172 NC_HDF5_FILE_INFO_T *h5 = grp->nc4_info;
2175 if ((retval = nc4_grp_list_add(&(grp->children), h5->next_nc_grpid++,
2176 grp, grp->nc4_info->controller, oinfo->oname, &child_grp)))
2180 if ((retval = nc4_rec_read_metadata(child_grp)))
2184 if (H5Oclose(oinfo->oid) < 0)
2188 udata.grps_head = oinfo->next;
2193 if ((retval = read_grp_atts(grp)))
2197 for (i=0; i<grp->vars.nelems; i++)
2198 grp->vars.value[i]->written_to = NC_TRUE;
2204 for (oinfo = udata.grps_head; oinfo; oinfo = udata.grps_head)
2207 if (H5Oclose(oinfo->oid) < 0)
2211 udata.grps_head = oinfo->next;
2223 nc4_open_file(
const char *path,
int mode,
void* parameters, NC *nc)
2225 hid_t fapl_id = H5P_DEFAULT;
2226 unsigned flags = (mode &
NC_WRITE) ?
2227 H5F_ACC_RDWR : H5F_ACC_RDONLY;
2229 NC_HDF5_FILE_INFO_T* nc4_info = NULL;
2231 NC_MEM_INFO* meminfo = (NC_MEM_INFO*)parameters;
2232 #ifdef USE_PARALLEL4 2233 NC_MPI_INFO* mpiinfo = (NC_MPI_INFO*)parameters;
2238 LOG((3,
"%s: path %s mode %d", __func__, path, mode));
2242 if ((retval = nc4_nc4f_list_add(nc, path, mode)))
2244 nc4_info = NC4_DATA(nc);
2245 assert(nc4_info && nc4_info->root_grp);
2250 if ((fapl_id = H5Pcreate(H5P_FILE_ACCESS)) < 0)
2257 if (H5Pset_fclose_degree(fapl_id, H5F_CLOSE_SEMI))
2260 if (H5Pset_fclose_degree(fapl_id, H5F_CLOSE_STRONG))
2264 #ifdef USE_PARALLEL4 2267 if (mode & NC_MPIIO || mode & NC_MPIPOSIX)
2269 nc4_info->parallel = NC_TRUE;
2270 if (mode & NC_MPIIO)
2272 LOG((4,
"opening parallel file with MPI/IO"));
2273 if (H5Pset_fapl_mpio(fapl_id, mpiinfo->comm, mpiinfo->info) < 0)
2276 #ifdef USE_PARALLEL_POSIX 2279 LOG((4,
"opening parallel file with MPI/posix"));
2280 if (H5Pset_fapl_mpiposix(fapl_id, mpiinfo->comm, 0) < 0)
2293 if (MPI_SUCCESS != MPI_Comm_dup(mpiinfo->comm, &nc4_info->comm))
2296 if (MPI_INFO_NULL != mpiinfo->info)
2298 if (MPI_SUCCESS != MPI_Info_dup(mpiinfo->info, &nc4_info->info))
2305 nc4_info->info = mpiinfo->info;
2309 if (H5Pset_cache(fapl_id, 0, nc4_chunk_cache_nelems, nc4_chunk_cache_size,
2310 nc4_chunk_cache_preemption) < 0)
2312 LOG((4,
"%s: set HDF raw chunk cache to size %d nelems %d preemption %f",
2313 __func__, nc4_chunk_cache_size, nc4_chunk_cache_nelems, nc4_chunk_cache_preemption));
2319 #ifdef HDF5_HAS_COLL_METADATA_OPS 2320 H5Pset_all_coll_metadata_ops(fapl_id, 1 );
2323 if((nc4_info->hdfid = H5LTopen_file_image(meminfo->memory,meminfo->size,
2324 H5LT_FILE_IMAGE_DONT_COPY|H5LT_FILE_IMAGE_DONT_RELEASE
2327 nc4_info->no_write = NC_TRUE;
2328 }
else if ((nc4_info->hdfid = H5Fopen(path, flags, fapl_id)) < 0)
2333 nc4_info->no_write = NC_TRUE;
2339 if ((retval = nc4_rec_read_metadata(nc4_info->root_grp)))
2344 if ((retval = nc4_rec_match_dimscales(nc4_info->root_grp)))
2350 log_metadata_nc(nc);
2354 if (H5Pclose(fapl_id) < 0)
2360 NC4_get_fileinfo(nc4_info,NULL);
2365 #ifdef USE_PARALLEL4 2366 if (comm_duped) MPI_Comm_free(&nc4_info->comm);
2367 if (info_duped) MPI_Info_free(&nc4_info->info);
2372 if (fapl_id != H5P_DEFAULT) H5Pclose(fapl_id);
2373 if (!nc4_info)
return retval;
2374 close_netcdf4_file(nc4_info,1);
2393 get_netcdf_type_from_hdf4(NC_HDF5_FILE_INFO_T *h5, int32 hdf4_typeid,
2394 nc_type *xtype, NC_TYPE_INFO_T *type_info)
2406 assert(h5 && xtype);
2494 if (hdf4_typeid == DFNT_FLOAT32)
2495 type_info->nc_type_class =
NC_FLOAT;
2496 else if (hdf4_typeid == DFNT_FLOAT64)
2498 else if (hdf4_typeid == DFNT_CHAR)
2501 type_info->nc_type_class =
NC_INT;
2502 type_info->endianness = endianness;
2503 type_info->nc_typeid = *xtype;
2504 type_info->size = nc_type_size_g[t];
2505 if (!(type_info->name = strdup(nc_type_name_g[t])))
2515 nc4_open_hdf4_file(
const char *path,
int mode, NC *nc)
2517 NC_HDF5_FILE_INFO_T *h5;
2520 int32 num_datasets, num_gatts;
2524 NC_HDF5_FILE_INFO_T* nc4_info = NULL;
2526 LOG((3,
"%s: path %s mode %d", __func__, path, mode));
2534 if ((retval = nc4_nc4f_list_add(nc, path, mode)))
2536 nc4_info = NC4_DATA(nc);
2537 assert(nc4_info && nc4_info->root_grp);
2541 h5->no_write = NC_TRUE;
2544 if ((h5->sdid = SDstart(path, DFACC_READ)) == FAIL)
2548 if (SDfileinfo(h5->sdid, &num_datasets, &num_gatts))
2552 for (a = 0; a < num_gatts; a++)
2554 int32 att_data_type, att_count;
2555 size_t att_type_size;
2558 if ((retval = nc4_att_list_add(&h5->root_grp->att, &att)))
2560 att->attnum = grp->natts++;
2561 att->created = NC_TRUE;
2566 if (SDattrinfo(h5->sdid, a, att->name, &att_data_type, &att_count))
2568 if ((retval = get_netcdf_type_from_hdf4(h5, att_data_type,
2569 &att->nc_typeid, NULL)))
2571 att->len = att_count;
2574 if ((retval = nc4_get_typelen_mem(h5, att->nc_typeid, 0, &att_type_size)))
2576 if (!(att->data = malloc(att_type_size * att->len)))
2580 if (SDreadattr(h5->sdid, a, att->data))
2585 for (v = 0; v < num_datasets; v++)
2588 int32 data_type, num_atts;
2597 int32 *dimsize = NULL;
2598 size_t var_type_size;
2602 if ((retval = nc4_var_add(&var)))
2605 var->varid = grp->nvars++;
2606 var->created = NC_TRUE;
2607 var->written_to = NC_TRUE;
2609 nc4_vararray_add(grp, var);
2612 if ((var->sdsid = SDselect(h5->sdid, v)) == FAIL)
2620 if (SDgetinfo(var->sdsid, var->name, &rank, NULL, &data_type, &num_atts))
2623 var->hash = hash_fast(var->name, strlen(var->name));
2625 if(!(dimsize = (int32*)malloc(
sizeof(int32)*rank)))
2628 if (SDgetinfo(var->sdsid, var->name, &rank, dimsize, &data_type, &num_atts)) {
2629 if(dimsize) free(dimsize);
2634 var->hdf4_data_type = data_type;
2637 if (!(var->type_info = calloc(1,
sizeof(NC_TYPE_INFO_T)))) {
2638 if(dimsize) free(dimsize);
2642 if ((retval = get_netcdf_type_from_hdf4(h5, data_type, &var->type_info->nc_typeid, var->type_info))) {
2643 if(dimsize) free(dimsize);
2648 var->type_info->rc++;
2650 if ((retval = nc4_get_typelen_mem(h5, var->type_info->nc_typeid, 0, &var_type_size))) {
2651 if(dimsize) free(dimsize);
2655 var->type_info->size = var_type_size;
2656 LOG((3,
"reading HDF4 dataset %s, rank %d netCDF type %d", var->name,
2657 rank, var->type_info->nc_typeid));
2660 if (!(var->fill_value = malloc(var_type_size))) {
2661 if(dimsize) free(dimsize);
2665 if (SDgetfillvalue(var->sdsid, var->fill_value))
2668 free(var->fill_value);
2669 var->fill_value = NULL;
2675 if (!(var->dim = malloc(
sizeof(NC_DIM_INFO_T *) * var->ndims))) {
2676 if(dimsize) free(dimsize);
2680 if (!(var->dimids = malloc(
sizeof(
int) * var->ndims))) {
2681 if(dimsize) free(dimsize);
2688 for (d = 0; d < var->ndims; d++)
2690 int32 dimid, dim_len, dim_data_type, dim_num_attrs;
2694 if ((dimid = SDgetdimid(var->sdsid, d)) == FAIL) {
2695 if(dimsize) free(dimsize);
2698 if (SDdiminfo(dimid, dim_name, &dim_len, &dim_data_type,
2701 if(dimsize) free(dimsize);
2707 for (dim = grp->dim; dim; dim = dim->l.next)
2708 if (!strcmp(dim->name, dim_name))
2714 LOG((4,
"adding dimension %s for HDF4 dataset %s",
2715 dim_name, var->name));
2716 if ((retval = nc4_dim_list_add(&grp->dim, &dim)))
2718 dim->dimid = grp->nc4_info->next_dimid++;
2721 if (!(dim->name = strdup(dim_name)))
2726 dim->len = *dimsize;
2727 dim->hash = hash_fast(dim_name, strlen(dim_name));
2731 var->dimids[d] = dim->dimid;
2736 for (a = 0; a < num_atts; a++)
2738 int32 att_data_type, att_count;
2739 size_t att_type_size;
2742 if ((retval = nc4_att_list_add(&var->att, &att))) {
2743 if(dimsize) free(dimsize);
2746 att->attnum = var->natts++;
2747 att->created = NC_TRUE;
2751 if(dimsize) free(dimsize);
2754 if (SDattrinfo(var->sdsid, a, att->name, &att_data_type, &att_count)) {
2755 if(dimsize) free(dimsize);
2758 if ((retval = get_netcdf_type_from_hdf4(h5, att_data_type,
2759 &att->nc_typeid, NULL))) {
2760 if(dimsize) free(dimsize);
2764 att->len = att_count;
2767 if ((retval = nc4_get_typelen_mem(h5, att->nc_typeid, 0, &att_type_size))) {
2768 if(dimsize) free(dimsize);
2771 if (!(att->data = malloc(att_type_size * att->len))) {
2772 if(dimsize) free(dimsize);
2777 if (SDreadattr(var->sdsid, a, att->data)) {
2778 if(dimsize) free(dimsize);
2782 if(dimsize) free(dimsize);
2786 HDF_CHUNK_DEF chunkdefs;
2788 if(!SDgetchunkinfo(var->sdsid, &chunkdefs, &flag)) {
2789 if(flag == HDF_NONE)
2790 var->contiguous = NC_TRUE;
2791 else if((flag & HDF_CHUNK) != 0) {
2792 var->contiguous = NC_FALSE;
2793 if (!(var->chunksizes = malloc(var->ndims *
sizeof(
size_t))))
2795 for (d = 0; d < var->ndims; d++) {
2796 var->chunksizes[d] = chunkdefs.chunk_lengths[d];
2807 log_metadata_nc(h5->root_grp->nc4_info->controller);
2815 NC4_open(
const char *path,
int mode,
int basepe,
size_t *chunksizehintp,
2816 int use_parallel,
void *parameters, NC_Dispatch *dispatch, NC *nc_file)
2820 #ifdef USE_PARALLEL4 2821 NC_MPI_INFO mpidfalt = {MPI_COMM_WORLD, MPI_INFO_NULL};
2823 #if defined USE_PARALLEL4 || defined USE_HDF4 2827 assert(nc_file && path);
2829 LOG((1,
"%s: path %s mode %d params %x",
2830 __func__, path, mode, parameters));
2832 #ifdef USE_PARALLEL4 2833 if (!inmemory && use_parallel && parameters == NULL)
2834 parameters = &mpidfalt;
2838 if (!nc4_hdf5_initialized)
2839 nc4_hdf5_initialize();
2842 if((mode & ILLEGAL_OPEN_FLAGS) != 0)
2846 if((mode & (NC_MPIIO|NC_MPIPOSIX)) == (NC_MPIIO|NC_MPIPOSIX))
2849 #ifndef USE_PARALLEL_POSIX 2853 if(mode & NC_MPIPOSIX)
2855 mode &= ~NC_MPIPOSIX;
2861 if ((res = nc_check_for_hdf(path, use_parallel, parameters, &hdf_file)))
2865 nc_file->int_ncid = nc_file->ext_ncid;
2866 if (hdf_file == NC_HDF5_FILE)
2867 res = nc4_open_file(path, mode, parameters, nc_file);
2869 else if (hdf_file == NC_HDF4_FILE && inmemory)
2871 else if (hdf_file == NC_HDF4_FILE)
2872 res = nc4_open_hdf4_file(path, mode, nc_file);
2887 NC4_set_fill(
int ncid,
int fillmode,
int *old_modep)
2890 NC_HDF5_FILE_INFO_T* nc4_info;
2892 LOG((2,
"%s: ncid 0x%x fillmode %d", __func__, ncid, fillmode));
2894 if (!(nc = nc4_find_nc_file(ncid,&nc4_info)))
2899 if (nc4_info->no_write)
2908 *old_modep = nc4_info->fill_mode;
2910 nc4_info->fill_mode = fillmode;
2921 NC_HDF5_FILE_INFO_T* nc4_info;
2923 LOG((1,
"%s: ncid 0x%x", __func__, ncid));
2926 if (!(nc4_find_nc_file(ncid,&nc4_info)))
2931 if (nc4_info->flags & NC_INDEF)
2935 if (nc4_info->no_write)
2939 nc4_info->flags |= NC_INDEF;
2943 nc4_info->redef = NC_TRUE;
2951 NC4__enddef(
int ncid,
size_t h_minfree,
size_t v_align,
2952 size_t v_minfree,
size_t r_align)
2954 if (nc4_find_nc_file(ncid,NULL) == NULL)
2957 return NC4_enddef(ncid);
2962 static int NC4_enddef(
int ncid)
2965 NC_HDF5_FILE_INFO_T* nc4_info;
2969 LOG((1,
"%s: ncid 0x%x", __func__, ncid));
2971 if (!(nc = nc4_find_nc_file(ncid,&nc4_info)))
2976 if (!(grp = nc4_rec_find_grp(nc4_info->root_grp, (ncid & GRP_ID_MASK))))
2980 for (i=0; i<grp->vars.nelems; i++)
2981 grp->vars.value[i]->written_to = NC_TRUE;
2983 return nc4_enddef_netcdf4_file(nc4_info);
2989 sync_netcdf4_file(NC_HDF5_FILE_INFO_T *h5)
2994 LOG((3,
"%s", __func__));
2998 if (h5->flags & NC_INDEF)
3004 h5->flags ^= NC_INDEF;
3007 h5->redef = NC_FALSE;
3013 log_metadata_nc(h5->root_grp->nc4_info->controller);
3019 nc_bool_t bad_coord_order = NC_FALSE;
3021 if ((retval = nc4_rec_write_groups_types(h5->root_grp)))
3023 if ((retval = nc4_rec_detect_need_to_preserve_dimids(h5->root_grp, &bad_coord_order)))
3025 if ((retval = nc4_rec_write_metadata(h5->root_grp, bad_coord_order)))
3029 if (H5Fflush(h5->hdfid, H5F_SCOPE_GLOBAL) < 0)
3042 NC_HDF5_FILE_INFO_T* nc4_info;
3044 LOG((2,
"%s: ncid 0x%x", __func__, ncid));
3046 if (!(nc = nc4_find_nc_file(ncid,&nc4_info)))
3051 if (nc4_info && nc4_info->flags & NC_INDEF)
3055 if ((retval = NC4_enddef(ncid)))
3059 return sync_netcdf4_file(nc4_info);
3066 close_netcdf4_file(NC_HDF5_FILE_INFO_T *h5,
int abort)
3070 assert(h5 && h5->root_grp);
3071 LOG((3,
"%s: h5->path %s abort %d", __func__, h5->controller->path, abort));
3074 if (h5->flags & NC_INDEF)
3075 h5->flags ^= NC_INDEF;
3079 if (!h5->no_write && !abort)
3080 if ((retval = sync_netcdf4_file(h5)))
3085 if ((retval = nc4_rec_grp_del(&h5->root_grp, h5->root_grp)))
3092 if (SDend(h5->sdid))
3098 #ifdef USE_PARALLEL4 3102 if(MPI_COMM_NULL != h5->comm)
3103 MPI_Comm_free(&h5->comm);
3104 if(MPI_INFO_NULL != h5->info)
3105 MPI_Info_free(&h5->info);
3109 if(h5->fileinfo) free(h5->fileinfo);
3111 if (H5Fclose(h5->hdfid) < 0)
3115 nobjs = H5Fget_obj_count(h5->hdfid, H5F_OBJ_ALL);
3119 }
else if(nobjs > 0) {
3127 snprintf(msg,
sizeof(msg),
"There are %d HDF5 objects open!", nobjs);
3131 fprintf(stdout,msg);
3134 reportopenobjects(logit,h5->hdfid);
3157 int delete_file = 0;
3160 NC_HDF5_FILE_INFO_T* nc4_info;
3162 LOG((2,
"%s: ncid 0x%x", __func__, ncid));
3165 if (!(nc = nc4_find_nc_file(ncid,&nc4_info)))
3171 if (nc4_info->flags & NC_INDEF && !nc4_info->redef)
3179 if ((retval = close_netcdf4_file(nc4_info, 1)))
3184 if (
remove(path) < 0)
3196 NC_HDF5_FILE_INFO_T *h5;
3199 LOG((1,
"%s: ncid 0x%x", __func__, ncid));
3202 if ((retval = nc4_find_nc_grp_h5(ncid, &nc, &grp, &h5)))
3205 assert(nc && h5 && grp);
3212 if ((retval = close_netcdf4_file(grp->nc4_info, 0)))
3221 NC4_inq(
int ncid,
int *ndimsp,
int *nvarsp,
int *nattsp,
int *unlimdimidp)
3224 NC_HDF5_FILE_INFO_T *h5;
3230 LOG((2,
"%s: ncid 0x%x", __func__, ncid));
3233 if ((retval = nc4_find_nc_grp_h5(ncid, &nc, &grp, &h5)))
3236 assert(h5 && grp && nc);
3242 for (dim = grp->dim; dim; dim = dim->l.next)
3249 for (i=0; i < grp->vars.nelems; i++)
3251 if (grp->vars.value[i])
3258 for (att = grp->att; att; att = att->l.next)
3271 for (dim = grp->dim; dim; dim = dim->l.next)
3274 *unlimdimidp = dim->dimid;
3284 nc4_enddef_netcdf4_file(NC_HDF5_FILE_INFO_T *h5)
3287 LOG((3,
"%s", __func__));
3290 if (!(h5->flags & NC_INDEF))
3294 h5->flags ^= NC_INDEF;
3297 h5->redef = NC_FALSE;
3299 return sync_netcdf4_file(h5);
3306 if (num_plists || num_spaces)
3313 #ifdef USE_PARALLEL4 3315 nc_use_parallel_enabled()
3327 hdf5free(
void* memory)
3332 #ifdef HDF5_HAS_H5FREE 3333 if(memory != NULL) H5free_memory(memory);
3336 if(memory != NULL) free(memory);
#define NC_ENOMEM
Memory allocation (malloc) failure.
#define NC_CHAR
ISO/ASCII character.
#define NC_ECANTWRITE
Can't write.
int NC4_create(const char *path, int cmode, size_t initialsz, int basepe, size_t *chunksizehintp, int use_parallel, void *parameters, NC_Dispatch *dispatch, NC *nc_file)
Create a netCDF-4/HDF5 file.
#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_ENOTBUILT
Attempt to use feature that was not turned on when netCDF was built.
#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 NC_VLEN
vlen (variable-length) types
#define NC_EMPI
MPI operation failed.
#define NC_EDISKLESS
Error in using diskless access.
#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_EINVAL
Invalid Argument.
#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
#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_EBADID
Not a netcdf id.
#define NC_EVARMETA
Problem with variable metadata.
This is the type of arrays of vlens.
struct NC4_rec_read_metadata_ud NC4_rec_read_metadata_ud_t
User data struct for call to H5Literate() in nc4_rec_read_metadata()
#define NC_MAX_UINT
Max or min values for a type.
#define NC_SHORT
signed 2 byte integer
#define NC_CDF5
Alias NC_CDF5 to NC_64BIT_DATA.
#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.
struct NC4_rec_read_metadata_obj_info NC4_rec_read_metadata_obj_info_t
Struct to track information about objects in a group, for nc4_rec_read_metadata() ...
#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.