15 #include "nc4internal.h"
16 #include "nc4dispatch.h"
18 #include "H5DSpublic.h"
31 extern int num_plists;
32 extern int num_spaces;
35 #define MIN_DEFLATE_LEVEL 0
36 #define MAX_DEFLATE_LEVEL 9
40 #define REFERENCE_LIST "REFERENCE_LIST"
42 #define DIMENSION_LIST "DIMENSION_LIST"
63 static int NC4_enddef(
int ncid);
64 static int nc4_rec_read_metadata(NC_GRP_INFO_T *grp);
65 static int close_netcdf4_file(NC_HDF5_FILE_INFO_T *h5,
int abort);
69 size_t nc4_chunk_cache_size = CHUNK_CACHE_SIZE;
70 size_t nc4_chunk_cache_nelems = CHUNK_CACHE_NELEMS;
71 float nc4_chunk_cache_preemption = CHUNK_CACHE_PREEMPTION;
75 static int virgin = 1;
80 static hid_t h5_native_type_constant_g[NUM_TYPES];
81 static const char nc_type_name_g[NUM_TYPES][
NC_MAX_NAME + 1] = {
"char",
"byte",
"short",
82 "int",
"float",
"double",
"ubyte",
83 "ushort",
"uint",
"int64",
89 static const int nc_type_size_g[NUM_TYPES] = {
sizeof(char),
sizeof(
char),
sizeof(short),
90 sizeof(
int),
sizeof(float),
sizeof(
double),
sizeof(
unsigned char),
91 sizeof(
unsigned short),
sizeof(
unsigned int),
sizeof(
long long),
92 sizeof(
unsigned long long),
sizeof(
char *)};
97 nc_set_chunk_cache(
size_t size,
size_t nelems,
float preemption)
99 if (preemption < 0 || preemption > 1)
101 nc4_chunk_cache_size = size;
102 nc4_chunk_cache_nelems = nelems;
103 nc4_chunk_cache_preemption = preemption;
110 nc_get_chunk_cache(
size_t *sizep,
size_t *nelemsp,
float *preemptionp)
113 *sizep = nc4_chunk_cache_size;
116 *nelemsp = nc4_chunk_cache_nelems;
119 *preemptionp = nc4_chunk_cache_preemption;
125 nc_set_chunk_cache_ints(
int size,
int nelems,
int preemption)
127 if (size <= 0 || nelems <= 0 || preemption < 0 || preemption > 100)
129 nc4_chunk_cache_size = size;
130 nc4_chunk_cache_nelems = nelems;
131 nc4_chunk_cache_preemption = (float)preemption / 100;
136 nc_get_chunk_cache_ints(
int *sizep,
int *nelemsp,
int *preemptionp)
139 *sizep = (int)nc4_chunk_cache_size;
141 *nelemsp = (int)nc4_chunk_cache_nelems;
143 *preemptionp = (int)(nc4_chunk_cache_preemption * 100);
173 #define MAGIC_NUMBER_LEN 4
174 #define NC_HDF5_FILE 1
175 #define NC_HDF4_FILE 2
177 nc_check_for_hdf(
const char *path,
int use_parallel, MPI_Comm comm, MPI_Info info,
180 char blob[MAGIC_NUMBER_LEN];
182 assert(hdf_file && path);
183 LOG((3,
"%s: path %s", __func__, path));
188 *hdf_file = NC_HDF5_FILE;
199 if ((retval = MPI_File_open(comm, (
char *)path, MPI_MODE_RDONLY,
200 info, &fh)) != MPI_SUCCESS)
202 if ((retval = MPI_File_read(fh, blob, MAGIC_NUMBER_LEN, MPI_CHAR,
203 &status)) != MPI_SUCCESS)
205 if ((retval = MPI_File_close(&fh)) != MPI_SUCCESS)
212 if (!(fp = fopen(path,
"r")) ||
213 fread(blob, MAGIC_NUMBER_LEN, 1, fp) != 1) {
222 if (!strncmp(blob,
"\016\003\023\001", MAGIC_NUMBER_LEN))
223 *hdf_file = NC_HDF4_FILE;
233 nc4_create_file(
const char *path,
int cmode, MPI_Comm comm, MPI_Info info,
236 hid_t fcpl_id, fapl_id = -1;
240 NC_HDF5_FILE_INFO_T* nc4_info = NULL;
251 flags = H5F_ACC_TRUNC;
253 flags = H5F_ACC_EXCL;
255 flags = H5F_ACC_TRUNC;
257 LOG((3,
"%s: path %s mode 0x%x", __func__, path, cmode));
262 if (cmode & NC_DISKLESS) {
267 }
else if ((cmode & NC_NOCLOBBER) && (fp = fopen(path,
"r"))) {
273 if ((retval = nc4_nc4f_list_add(nc, path, (
NC_WRITE | cmode))))
275 nc4_info = NC4_DATA(nc);
276 assert(nc4_info && nc4_info->root_grp);
286 if ((fapl_id = H5Pcreate(H5P_FILE_ACCESS)) < 0)
292 if (H5Pset_fclose_degree(fapl_id, H5F_CLOSE_SEMI))
295 if (H5Pset_fclose_degree(fapl_id, H5F_CLOSE_STRONG))
304 nc4_info->parallel = NC_TRUE;
305 if (cmode & NC_MPIIO)
307 LOG((4,
"creating parallel file with MPI/IO"));
308 if (H5Pset_fapl_mpio(fapl_id, comm, info) < 0)
313 LOG((4,
"creating parallel file with MPI/posix"));
314 if (H5Pset_fapl_mpiposix(fapl_id, comm, 0) < 0)
319 if (MPI_SUCCESS != MPI_Comm_dup(comm, &nc4_info->comm))
322 if (MPI_INFO_NULL != info)
324 if (MPI_SUCCESS != MPI_Info_dup(info, &nc4_info->info))
331 nc4_info->info = info;
335 if(cmode & NC_DISKLESS) {
336 if (H5Pset_fapl_core(fapl_id, 4096, persist))
339 if (H5Pset_cache(fapl_id, 0, nc4_chunk_cache_nelems, nc4_chunk_cache_size,
340 nc4_chunk_cache_preemption) < 0)
342 LOG((4,
"%s: set HDF raw chunk cache to size %d nelems %d preemption %f",
343 __func__, nc4_chunk_cache_size, nc4_chunk_cache_nelems, nc4_chunk_cache_preemption));
346 if (H5Pset_libver_bounds(fapl_id, H5F_LIBVER_LATEST, H5F_LIBVER_LATEST) < 0)
350 if ((fcpl_id = H5Pcreate(H5P_FILE_CREATE)) < 0)
357 if (H5Pset_obj_track_times(fcpl_id,0)<0)
363 if (H5Pset_link_creation_order(fcpl_id, (H5P_CRT_ORDER_TRACKED |
364 H5P_CRT_ORDER_INDEXED)) < 0)
366 if (H5Pset_attr_creation_order(fcpl_id, (H5P_CRT_ORDER_TRACKED |
367 H5P_CRT_ORDER_INDEXED)) < 0)
371 if ((nc4_info->hdfid = H5Fcreate(path, flags, fcpl_id, fapl_id)) < 0)
377 if ((nc4_info->root_grp->hdf_grpid = H5Gopen2(nc4_info->hdfid,
"/",
382 if (H5Pclose(fapl_id) < 0 ||
383 H5Pclose(fcpl_id) < 0)
391 nc4_info->flags |= NC_INDEF;
397 if (comm_duped) MPI_Comm_free(&nc4_info->comm);
398 if (info_duped) MPI_Info_free(&nc4_info->info);
403 if (fapl_id != H5P_DEFAULT) H5Pclose(fapl_id);
404 if(!nc4_info)
return retval;
405 close_netcdf4_file(nc4_info,1);
425 NC4_create(
const char* path,
int cmode,
size_t initialsz,
int basepe,
426 size_t *chunksizehintp,
int use_parallel,
void *mpidata,
427 NC_Dispatch *dispatch, NC* nc_file)
429 MPI_Comm comm = MPI_COMM_WORLD;
430 MPI_Info info = MPI_INFO_NULL;
433 assert(nc_file && path);
435 LOG((1,
"%s: path %s cmode 0x%x comm %d info %d",
436 __func__, path, cmode, comm, info));
441 comm = ((NC_MPI_INFO *)mpidata)->comm;
442 info = ((NC_MPI_INFO *)mpidata)->info;
449 if (H5Eset_auto(NULL, NULL) < 0)
450 LOG((0,
"Couldn't turn off HDF5 error messages!"));
451 LOG((1,
"HDF5 error messages have been turned off."));
462 || (cmode & NC_MPIIO && cmode & NC_MPIPOSIX)
464 || (cmode & (NC_MPIIO | NC_MPIPOSIX) && cmode & NC_DISKLESS)
476 LOG((2,
"cmode after applying default format: 0x%x", cmode));
478 nc_file->int_ncid = nc_file->ext_ncid;
479 res = nc4_create_file(path, cmode, comm, info, nc_file);
482 if (cmode & NC_PNETCDF)
484 NC_HDF5_FILE_INFO_T* nc4_info;
485 nc4_info = NC4_DATA(nc_file);
488 nc4_info->pnetcdf_file++;
489 res = ncmpi_create(comm, path, cmode, info, &(nc_file->int_ncid));
502 read_scale(NC_GRP_INFO_T *grp, hid_t datasetid,
const char *obj_name,
503 const H5G_stat_t *statbuf, hsize_t scale_size, hsize_t max_scale_size,
506 NC_DIM_INFO_T *new_dim;
508 htri_t attr_exists = -1;
510 int dimscale_created = 0;
511 int initial_grp_ndims = grp->ndims;
512 short initial_next_dimid = grp->nc4_info->next_dimid;
516 if ((retval = nc4_dim_list_add(&grp->dim, &new_dim)))
522 if ((attr_exists = H5Aexists(datasetid, NC_DIMID_ATT_NAME)) < 0)
526 if ((attid = H5Aopen_by_name(datasetid,
".", NC_DIMID_ATT_NAME,
527 H5P_DEFAULT, H5P_DEFAULT)) < 0)
530 if (H5Aread(attid, H5T_NATIVE_INT, &new_dim->dimid) < 0)
534 if (new_dim->dimid >= grp->nc4_info->next_dimid)
535 grp->nc4_info->next_dimid = new_dim->dimid + 1;
540 new_dim->dimid = grp->nc4_info->next_dimid++;
546 if (!(new_dim->name = strdup(obj_name)))
551 new_dim->too_long = NC_TRUE;
554 new_dim->len = scale_size;
555 new_dim->hdf5_objid.fileno[0] = statbuf->fileno[0];
556 new_dim->hdf5_objid.fileno[1] = statbuf->fileno[1];
557 new_dim->hdf5_objid.objno[0] = statbuf->objno[0];
558 new_dim->hdf5_objid.objno[1] = statbuf->objno[1];
562 if (max_scale_size == H5S_UNLIMITED)
563 new_dim->unlimited = NC_TRUE;
568 if (H5DSget_scale_name(datasetid, dimscale_name_att,
NC_MAX_NAME) >= 0)
570 if (!strncmp(dimscale_name_att, DIM_WITHOUT_VARIABLE,
571 strlen(DIM_WITHOUT_VARIABLE)))
573 if (new_dim->unlimited)
575 size_t len = 0, *lenp = &len;
577 if ((retval = nc4_find_dim_len(grp, new_dim->dimid, &lenp)))
579 new_dim->len = *lenp;
583 new_dim->hdf_dimscaleid = datasetid;
584 H5Iinc_ref(new_dim->hdf_dimscaleid);
593 if (attid > 0 && H5Aclose(attid) < 0)
597 if (retval < 0 && dimscale_created)
600 if ((retval = nc4_dim_list_del(&grp->dim, new_dim)))
604 grp->ndims = initial_grp_ndims;
605 grp->nc4_info->next_dimid = initial_next_dimid;
614 read_coord_dimids(NC_VAR_INFO_T *var)
616 hid_t coord_att_typeid = -1, coord_attid = -1, spaceid = -1;
623 if ((coord_attid = H5Aopen_name(var->hdf_datasetid, COORDINATES)) < 0) ret++;
624 if (!ret && (coord_att_typeid = H5Aget_type(coord_attid)) < 0) ret++;
627 if (!ret && (spaceid = H5Aget_space(coord_attid)) < 0) ret++;
631 if (!ret && (npoints = H5Sget_simple_extent_npoints(spaceid)) < 0) ret++;
635 if (!ret && npoints != var->ndims) ret++;
637 if (!ret && H5Aread(coord_attid, coord_att_typeid, var->dimids) < 0) ret++;
638 LOG((4,
"dimscale %s is multidimensional and has coords", var->name));
641 if (spaceid >= 0 && H5Sclose(spaceid) < 0) ret++;
645 if (coord_att_typeid >= 0 && H5Tclose(coord_att_typeid) < 0) ret++;
646 if (coord_attid >= 0 && H5Aclose(coord_attid) < 0) ret++;
653 dimscale_visitor(hid_t did,
unsigned dim, hid_t dsid,
654 void *dimscale_hdf5_objids)
659 if (H5Gget_objinfo(dsid,
".", 1, &statbuf) < 0)
663 (*(HDF5_OBJID_T *)dimscale_hdf5_objids).fileno[0] = statbuf.fileno[0];
664 (*(HDF5_OBJID_T *)dimscale_hdf5_objids).fileno[1] = statbuf.fileno[1];
665 (*(HDF5_OBJID_T *)dimscale_hdf5_objids).objno[0] = statbuf.objno[0];
666 (*(HDF5_OBJID_T *)dimscale_hdf5_objids).objno[1] = statbuf.objno[1];
672 get_netcdf_type(NC_HDF5_FILE_INFO_T *h5, hid_t native_typeid,
675 NC_TYPE_INFO_T *type;
677 htri_t is_str, equal = 0;
681 if ((
class = H5Tget_class(native_typeid)) < 0)
686 if (
class == H5T_STRING)
688 if ((is_str = H5Tis_variable_str(native_typeid)) < 0)
696 else if (
class == H5T_INTEGER ||
class == H5T_FLOAT)
700 if ((equal = H5Tequal(native_typeid, H5T_NATIVE_SCHAR)) < 0)
707 if ((equal = H5Tequal(native_typeid, H5T_NATIVE_SHORT)) < 0)
714 if ((equal = H5Tequal(native_typeid, H5T_NATIVE_INT)) < 0)
721 if ((equal = H5Tequal(native_typeid, H5T_NATIVE_FLOAT)) < 0)
728 if ((equal = H5Tequal(native_typeid, H5T_NATIVE_DOUBLE)) < 0)
735 if ((equal = H5Tequal(native_typeid, H5T_NATIVE_UCHAR)) < 0)
742 if ((equal = H5Tequal(native_typeid, H5T_NATIVE_USHORT)) < 0)
749 if ((equal = H5Tequal(native_typeid, H5T_NATIVE_UINT)) < 0)
756 if ((equal = H5Tequal(native_typeid, H5T_NATIVE_LLONG)) < 0)
763 if ((equal = H5Tequal(native_typeid, H5T_NATIVE_ULLONG)) < 0)
774 if((type = nc4_rec_find_hdf_type(h5->root_grp, native_typeid)))
776 *xtype = type->nc_typeid;
788 get_type_info2(NC_HDF5_FILE_INFO_T *h5, hid_t datasetid,
789 NC_TYPE_INFO_T **type_info)
791 htri_t is_str, equal = 0;
793 hid_t native_typeid, hdf_typeid;
797 assert(h5 && type_info);
805 if (!h5_native_type_constant_g[1])
807 h5_native_type_constant_g[1] = H5T_NATIVE_SCHAR;
808 h5_native_type_constant_g[2] = H5T_NATIVE_SHORT;
809 h5_native_type_constant_g[3] = H5T_NATIVE_INT;
810 h5_native_type_constant_g[4] = H5T_NATIVE_FLOAT;
811 h5_native_type_constant_g[5] = H5T_NATIVE_DOUBLE;
812 h5_native_type_constant_g[6] = H5T_NATIVE_UCHAR;
813 h5_native_type_constant_g[7] = H5T_NATIVE_USHORT;
814 h5_native_type_constant_g[8] = H5T_NATIVE_UINT;
815 h5_native_type_constant_g[9] = H5T_NATIVE_LLONG;
816 h5_native_type_constant_g[10] = H5T_NATIVE_ULLONG;
820 if ((hdf_typeid = H5Dget_type(datasetid)) < 0)
825 if ((native_typeid = H5Tget_native_type(hdf_typeid, H5T_DIR_DEFAULT)) < 0)
829 if ((
class = H5Tget_class(native_typeid)) < 0)
833 if (
class == H5T_STRING ||
class == H5T_INTEGER ||
class == H5T_FLOAT)
836 if (!(*type_info = calloc(1,
sizeof(NC_TYPE_INFO_T))))
841 if (
class == H5T_STRING)
843 if ((is_str = H5Tis_variable_str(native_typeid)) < 0)
846 if (is_str || H5Tget_size(hdf_typeid) > 1)
856 (*type_info)->nc_type_class =
NC_CHAR;
859 else if (
class == H5T_INTEGER ||
class == H5T_FLOAT)
861 for (t = 1; t < NUM_TYPES - 1; t++)
863 if ((equal = H5Tequal(native_typeid, h5_native_type_constant_g[t])) < 0)
870 if (
class == H5T_INTEGER)
872 if ((order = H5Tget_order(hdf_typeid)) < 0)
876 if (order == H5T_ORDER_LE)
878 else if (order == H5T_ORDER_BE)
885 (*type_info)->nc_type_class =
NC_INT;
891 (*type_info)->nc_type_class =
NC_FLOAT;
894 (*type_info)->nc_typeid = nc_type_constant_g[t];
895 (*type_info)->size = nc_type_size_g[t];
896 if (!((*type_info)->name = strdup(nc_type_name_g[t])))
898 (*type_info)->hdf_typeid = hdf_typeid;
899 (*type_info)->native_hdf_typeid = native_typeid;
904 NC_TYPE_INFO_T *type;
907 if((type = nc4_rec_find_hdf_type(h5->root_grp, native_typeid)))
913 if (H5Tclose(native_typeid) < 0)
915 if (H5Tclose(hdf_typeid) < 0)
927 read_hdf5_att(NC_GRP_INFO_T *grp, hid_t attid, NC_ATT_INFO_T *att)
929 hid_t spaceid = 0, file_typeid = 0;
930 hsize_t dims[1] = {0};
934 hssize_t att_npoints;
935 H5T_class_t att_class;
936 int fixed_len_string = 0;
937 size_t fixed_size = 0;
940 LOG((5,
"%s: att->attnum %d att->name %s att->nc_typeid %d att->len %d",
941 __func__, att->attnum, att->name, (
int)att->nc_typeid, att->len));
944 if ((file_typeid = H5Aget_type(attid)) < 0)
946 if ((att->native_hdf_typeid = H5Tget_native_type(file_typeid, H5T_DIR_DEFAULT)) < 0)
948 if ((att_class = H5Tget_class(att->native_hdf_typeid)) < 0)
950 if (att_class == H5T_STRING && !H5Tis_variable_str(att->native_hdf_typeid))
953 if (!(fixed_size = H5Tget_size(att->native_hdf_typeid)))
956 if ((retval = get_netcdf_type(grp->nc4_info, att->native_hdf_typeid, &(att->nc_typeid))))
961 if ((spaceid = H5Aget_space(attid)) < 0)
966 if ((att_ndims = H5Sget_simple_extent_ndims(spaceid)) < 0)
968 if ((att_npoints = H5Sget_simple_extent_npoints(spaceid)) < 0)
973 if (att_ndims == 0 && att_npoints == 0)
976 dims[0] = att_npoints;
977 else if (att->nc_typeid ==
NC_CHAR)
983 if (!(dims[0] = H5Tget_size(file_typeid)))
990 dims[0] = att_npoints;
995 H5S_class_t space_class;
1002 if ((space_class = H5Sget_simple_extent_type(spaceid)) < 0)
1006 if (H5S_NULL == space_class)
1010 if (H5S_SCALAR == space_class)
1015 if (H5Sget_simple_extent_dims(spaceid, dims, NULL) < 0)
1027 if ((retval = nc4_get_typelen_mem(grp->nc4_info, att->nc_typeid, 0,
1030 if (att_class == H5T_VLEN)
1032 if (!(att->vldata = malloc((
unsigned int)(att->len *
sizeof(hvl_t)))))
1034 if (H5Aread(attid, att->native_hdf_typeid, att->vldata) < 0)
1039 if (!(att->stdata = calloc(att->len,
sizeof(
char *))))
1052 if (fixed_len_string)
1055 char *contig_buf, *cur;
1058 if (!(contig_buf = malloc(att->len * fixed_size *
sizeof(
char))))
1062 if (H5Aread(attid, att->native_hdf_typeid, contig_buf) < 0)
1069 for (i = 0; i < att->len; i++)
1071 if (!(att->stdata[i] = malloc(fixed_size)))
1073 strncpy(att->stdata[i], cur, fixed_size);
1083 if (H5Aread(attid, att->native_hdf_typeid, att->stdata) < 0)
1089 if (!(att->data = malloc((
unsigned int)(att->len * type_size))))
1091 if (H5Aread(attid, att->native_hdf_typeid, att->data) < 0)
1096 if (H5Tclose(file_typeid) < 0)
1098 if (H5Sclose(spaceid) < 0)
1107 if (H5Tclose(file_typeid) < 0)
1109 if (spaceid > 0 && H5Sclose(spaceid) < 0)
1120 read_type(NC_GRP_INFO_T *grp, hid_t hdf_typeid,
char *type_name)
1122 NC_TYPE_INFO_T *type;
1124 hid_t native_typeid;
1128 assert(grp && type_name);
1130 LOG((4,
"%s: type_name %s grp->name %s", __func__, type_name, grp->name));
1133 if ((native_typeid = H5Tget_native_type(hdf_typeid, H5T_DIR_DEFAULT)) < 0)
1137 if (!(type_size = H5Tget_size(native_typeid)))
1139 LOG((5,
"type_size %d", type_size));
1142 if ((retval = nc4_type_list_add(grp, type_size, type_name, &type)))
1146 type->committed = NC_TRUE;
1147 type->hdf_typeid = hdf_typeid;
1148 H5Iinc_ref(type->hdf_typeid);
1149 type->native_hdf_typeid = native_typeid;
1152 if ((
class = H5Tget_class(hdf_typeid)) < 0)
1164 char* member_name = NULL;
1171 if ((nmembers = H5Tget_nmembers(hdf_typeid)) < 0)
1173 LOG((5,
"compound type has %d members", nmembers));
1174 for (m = 0; m < nmembers; m++)
1176 hid_t member_hdf_typeid;
1177 hid_t member_native_typeid;
1178 size_t member_offset;
1179 H5T_class_t mem_class;
1186 if ((member_hdf_typeid = H5Tget_member_type(type->native_hdf_typeid, m)) < 0)
1189 if ((member_native_typeid = H5Tget_native_type(member_hdf_typeid, H5T_DIR_DEFAULT)) < 0)
1193 member_name = H5Tget_member_name(type->native_hdf_typeid, m);
1194 if (!member_name || strlen(member_name) >
NC_MAX_NAME) {
1200 strncpy(jna,member_name,1000);
1206 member_offset = H5Tget_member_offset(type->native_hdf_typeid, m);
1209 if ((mem_class = H5Tget_class(member_hdf_typeid)) < 0)
1211 if (mem_class == H5T_ARRAY)
1217 if ((ndims = H5Tget_array_ndims(member_hdf_typeid)) < 0) {
1221 if (H5Tget_array_dims(member_hdf_typeid, dims, NULL) != ndims) {
1225 for (d = 0; d < ndims; d++)
1226 dim_size[d] = dims[d];
1229 if ((retval = get_netcdf_type(grp->nc4_info, H5Tget_super(member_hdf_typeid),
1234 if ((retval = nc4_field_list_add(&type->u.c.field, type->u.c.num_fields++, member_name,
1235 member_offset, H5Tget_super(member_hdf_typeid),
1236 H5Tget_super(member_native_typeid),
1237 member_xtype, ndims, dim_size)))
1243 if ((retval = get_netcdf_type(grp->nc4_info, member_native_typeid,
1248 if ((retval = nc4_field_list_add(&type->u.c.field, type->u.c.num_fields++, member_name,
1249 member_offset, member_hdf_typeid, member_native_typeid,
1250 member_xtype, 0, NULL)))
1256 if(member_name != NULL) free(member_name);
1261 if(member_name != NULL)
1277 if ((ret = H5Tis_variable_str(hdf_typeid)) < 0)
1283 hid_t base_hdf_typeid;
1286 type->nc_type_class =
NC_VLEN;
1290 if (!(base_hdf_typeid = H5Tget_super(native_typeid)))
1294 if (!(type_size = H5Tget_size(base_hdf_typeid)))
1298 if ((retval = get_netcdf_type(grp->nc4_info, base_hdf_typeid,
1301 LOG((5,
"base_hdf_typeid 0x%x type_size %d base_nc_type %d",
1302 base_hdf_typeid, type_size, base_nc_type));
1305 type->u.v.base_nc_typeid = base_nc_type;
1306 type->u.v.base_hdf_typeid = base_hdf_typeid;
1317 hid_t base_hdf_typeid;
1321 char *member_name = NULL;
1326 type->nc_type_class =
NC_ENUM;
1330 if (!(base_hdf_typeid = H5Tget_super(hdf_typeid)))
1333 if (!(type_size = H5Tget_size(base_hdf_typeid)))
1336 if ((retval = get_netcdf_type(grp->nc4_info, base_hdf_typeid,
1339 LOG((5,
"base_hdf_typeid 0x%x type_size %d base_nc_type %d",
1340 base_hdf_typeid, type_size, base_nc_type));
1343 type->u.e.base_nc_typeid = base_nc_type;
1344 type->u.e.base_hdf_typeid = base_hdf_typeid;
1347 if ((type->u.e.num_members = H5Tget_nmembers(hdf_typeid)) < 0)
1351 if (!(value = calloc(1, type_size)))
1355 for (i = 0; i < type->u.e.num_members; i++)
1359 if (!(member_name = H5Tget_member_name(hdf_typeid, i)))
1365 strncpy(jna,member_name,1000);
1374 if (H5Tget_member_value(hdf_typeid, i, value) < 0)
1381 if ((retval = nc4_enum_member_add(&type->u.e.enum_member, type->size,
1382 member_name, value)))
1389 if(member_name != NULL) free(member_name);
1395 if(member_name != NULL)
1398 if(value) free(value);
1405 LOG((0,
"unknown class"));
1416 read_var(NC_GRP_INFO_T *grp, hid_t datasetid,
const char *obj_name,
1417 size_t ndims, NC_DIM_INFO_T *dim)
1419 NC_VAR_INFO_T *var = NULL;
1420 hid_t access_pid = 0;
1426 char att_name[NC_MAX_HDF5_NAME + 1];
1428 #define CD_NELEMS_ZLIB 1
1429 #define CD_NELEMS_SZIP 4
1430 H5Z_filter_t filter;
1432 unsigned int cd_values[CD_NELEMS_SZIP];
1433 size_t cd_nelems = CD_NELEMS_SZIP;
1435 H5D_fill_value_t fill_status;
1436 H5D_layout_t layout;
1442 assert(obj_name && grp);
1443 LOG((4,
"%s: obj_name %s", __func__, obj_name));
1446 if ((retval = nc4_var_list_add(&grp->var, &var)))
1450 var->hdf_datasetid = datasetid;
1451 H5Iinc_ref(var->hdf_datasetid);
1453 var->varid = grp->nvars++;
1454 var->created = NC_TRUE;
1461 if (!(var->dim = calloc(var->ndims,
sizeof(NC_DIM_INFO_T *))))
1463 if (!(var->dimids = calloc(var->ndims,
sizeof(
int))))
1468 if ((access_pid = H5Dget_access_plist(datasetid)) < 0)
1475 if ((H5Pget_chunk_cache(access_pid, &(var->chunk_cache_nelems),
1476 &(var->chunk_cache_size), &rdcc_w0)) < 0)
1478 var->chunk_cache_preemption = rdcc_w0;
1483 if (strlen(obj_name) > strlen(NON_COORD_PREPEND) &&
1484 !strncmp(obj_name, NON_COORD_PREPEND, strlen(NON_COORD_PREPEND)))
1487 if (!(var->name = malloc(((strlen(obj_name) - strlen(NON_COORD_PREPEND))+ 1) *
sizeof(
char))))
1490 strcpy(var->name, &obj_name[strlen(NON_COORD_PREPEND)]);
1493 if (!(var->hdf5_name = malloc((strlen(obj_name) + 1) *
sizeof(
char))))
1496 strcpy(var->hdf5_name, obj_name);
1501 if (!(var->name = malloc((strlen(obj_name) + 1) *
sizeof(
char))))
1504 strcpy(var->name, obj_name);
1510 if ((propid = H5Dget_create_plist(datasetid)) < 0)
1517 if ((layout = H5Pget_layout(propid)) < -1)
1519 if (layout == H5D_CHUNKED)
1523 if (!(var->chunksizes = malloc(var->ndims *
sizeof(
size_t))))
1525 for (d = 0; d < var->ndims; d++)
1526 var->chunksizes[d] = chunksize[d];
1528 else if (layout == H5D_CONTIGUOUS || layout == H5D_COMPACT)
1529 var->contiguous = NC_TRUE;
1533 if ((num_filters = H5Pget_nfilters(propid)) < 0)
1535 for (f = 0; f < num_filters; f++)
1537 if ((filter = H5Pget_filter2(propid, f, NULL, &cd_nelems,
1538 cd_values, 0, NULL, NULL)) < 0)
1542 case H5Z_FILTER_SHUFFLE:
1543 var->shuffle = NC_TRUE;
1546 case H5Z_FILTER_FLETCHER32:
1547 var->fletcher32 = NC_TRUE;
1550 case H5Z_FILTER_DEFLATE:
1551 var->deflate = NC_TRUE;
1552 if (cd_nelems != CD_NELEMS_ZLIB || cd_values[0] > MAX_DEFLATE_LEVEL)
1554 var->deflate_level = cd_values[0];
1557 case H5Z_FILTER_SZIP:
1558 var->szip = NC_TRUE;
1559 if (cd_nelems != CD_NELEMS_SZIP)
1561 var->options_mask = cd_values[0];
1562 var->pixels_per_block = cd_values[1];
1566 LOG((1,
"Yikes! Unknown filter type found on dataset!"));
1572 if ((retval = get_type_info2(grp->nc4_info, datasetid,
1577 var->type_info->rc++;
1580 if (H5Pfill_value_defined(propid, &fill_status) < 0)
1584 if (fill_status == H5D_FILL_VALUE_USER_DEFINED)
1587 if (!var->fill_value)
1589 if (var->type_info->nc_type_class ==
NC_VLEN)
1591 if (!(var->fill_value = malloc(
sizeof(
nc_vlen_t))))
1594 else if (var->type_info->nc_type_class ==
NC_STRING)
1596 if (!(var->fill_value = malloc(
sizeof(
char *))))
1601 assert(var->type_info->size);
1602 if (!(var->fill_value = malloc(var->type_info->size)))
1608 if (H5Pget_fill_value(propid, var->type_info->native_hdf_typeid,
1609 var->fill_value) < 0)
1613 var->no_fill = NC_TRUE;
1619 var->dimscale = NC_TRUE;
1622 if ((retval = read_coord_dimids(var)))
1628 assert(0 == strcmp(var->name, dim->name));
1630 var->dimids[0] = dim->dimid;
1633 dim->coord_var = var;
1645 num_scales = H5DSget_num_scales(datasetid, 0);
1649 if (num_scales && ndims)
1653 if (NULL == (var->dimscale_attached = calloc(ndims,
sizeof(nc_bool_t))))
1658 if (NULL == (var->dimscale_hdf5_objids = malloc(ndims *
sizeof(
struct hdf5_objid))))
1660 for (d = 0; d < var->ndims; d++)
1662 if (H5DSiterate_scales(var->hdf_datasetid, d, NULL, dimscale_visitor,
1663 &(var->dimscale_hdf5_objids[d])) < 0)
1665 var->dimscale_attached[d] = NC_TRUE;
1672 if ((natts = H5Aget_num_attrs(var->hdf_datasetid)) < 0)
1674 for (a = 0; a < natts; a++)
1679 if (attid && H5Aclose(attid) < 0)
1683 if ((attid = H5Aopen_idx(var->hdf_datasetid, (
unsigned int)a)) < 0)
1685 if (H5Aget_name(attid, NC_MAX_HDF5_NAME, att_name) < 0)
1687 LOG((4,
"%s:: a %d att_name %s", __func__, a, att_name));
1690 if (strcmp(att_name, REFERENCE_LIST) &&
1691 strcmp(att_name, CLASS) &&
1692 strcmp(att_name, DIMENSION_LIST) &&
1693 strcmp(att_name, NAME) &&
1694 strcmp(att_name, COORDINATES) &&
1695 strcmp(att_name, NC_DIMID_ATT_NAME))
1698 if ((retval = nc4_att_list_add(&var->att, &att)))
1702 att->attnum = var->natts++;
1703 if (!(att->name = strdup(att_name)))
1708 if ((retval = read_hdf5_att(grp, attid, att)))
1712 if ((retval = nc4_att_list_del(&var->att, att)))
1720 att->created = NC_TRUE;
1726 if ((retval = nc4_adjust_var_cache(grp, var)))
1732 if (incr_id_rc && H5Idec_ref(datasetid) < 0)
1734 if (var && nc4_var_list_del(&grp->var, var))
1737 if (access_pid && H5Pclose(access_pid) < 0)
1742 if (propid > 0 && H5Pclose(propid) < 0)
1747 if (attid > 0 && H5Aclose(attid) < 0)
1755 read_grp_atts(NC_GRP_INFO_T *grp)
1760 NC_TYPE_INFO_T *type;
1761 char obj_name[NC_MAX_HDF5_NAME + 1];
1765 num_obj = H5Aget_num_attrs(grp->hdf_grpid);
1766 for (i = 0; i < num_obj; i++)
1770 if (attid && H5Aclose(attid) < 0)
1773 if ((attid = H5Aopen_idx(grp->hdf_grpid, (
unsigned int)i)) < 0)
1775 if (H5Aget_name(attid,
NC_MAX_NAME + 1, obj_name) < 0)
1777 LOG((3,
"reading attribute of _netCDF group, named %s", obj_name));
1783 if (!strcmp(obj_name, NC3_STRICT_ATT_NAME))
1788 if ((retval = nc4_att_list_add(&grp->att, &att)))
1793 if (!(att->name = malloc((max_len + 1) *
sizeof(
char))))
1795 strncpy(att->name, obj_name, max_len);
1796 att->name[max_len] = 0;
1797 att->attnum = grp->natts++;
1798 if ((retval = read_hdf5_att(grp, attid, att)))
1802 if ((retval = nc4_att_list_del(&grp->att, att)))
1809 att->created = NC_TRUE;
1810 if ((retval = nc4_find_type(grp->nc4_info, att->nc_typeid, &type)))
1816 if (attid > 0 && H5Aclose(attid) < 0)
1824 read_dataset(NC_GRP_INFO_T *grp, hid_t datasetid,
const char *obj_name,
1825 const H5G_stat_t *statbuf)
1827 NC_DIM_INFO_T *dim = NULL;
1834 if ((spaceid = H5Dget_space(datasetid)) < 0)
1839 if ((ndims = H5Sget_simple_extent_ndims(spaceid)) < 0)
1843 if ((is_scale = H5DSis_scale(datasetid)) < 0)
1847 hsize_t dims[H5S_MAX_RANK];
1848 hsize_t max_dims[H5S_MAX_RANK];
1851 if (H5Sget_simple_extent_dims(spaceid, dims, max_dims) < 0)
1855 if ((retval = read_scale(grp, datasetid, obj_name, statbuf, dims[0],
1856 max_dims[0], &dim)))
1863 if (NULL == dim || (dim && !dim->hdf_dimscaleid))
1864 if ((retval = read_var(grp, datasetid, obj_name, ndims, dim)))
1868 if (spaceid && H5Sclose(spaceid) <0)
1885 if (!(new_oinfo = calloc(1,
sizeof(*new_oinfo))))
1889 memcpy(new_oinfo, oinfo,
sizeof(*oinfo));
1894 (*tail)->next = new_oinfo;
1899 assert(NULL == *head);
1900 *head = *tail = new_oinfo;
1907 nc4_rec_read_metadata_cb(hid_t grpid,
const char *name,
const H5L_info_t *info,
1912 int retval = H5_ITER_CONT;
1915 memset(&oinfo, 0,
sizeof(oinfo));
1918 if ((oinfo.oid = H5Oopen(grpid, name, H5P_DEFAULT)) < 0)
1919 BAIL(H5_ITER_ERROR);
1922 if (H5Gget_objinfo(oinfo.oid,
".", 1, &oinfo.statbuf) < 0)
1923 BAIL(H5_ITER_ERROR);
1928 switch(oinfo.statbuf.type)
1931 LOG((3,
"found group %s", oinfo.oname));
1937 if (nc4_rec_read_metadata_cb_list_add(&udata->grps_head, &udata->grps_tail, &oinfo))
1938 BAIL(H5_ITER_ERROR);
1942 LOG((3,
"found dataset %s", oinfo.oname));
1946 if ((retval = read_dataset(udata->grp, oinfo.oid, oinfo.oname, &oinfo.statbuf)))
1954 BAIL(H5_ITER_ERROR);
1956 retval = H5_ITER_CONT;
1960 if (H5Oclose(oinfo.oid) < 0)
1961 BAIL(H5_ITER_ERROR);
1965 LOG((3,
"found datatype %s", oinfo.oname));
1968 if (read_type(udata->grp, oinfo.oid, oinfo.oname))
1969 BAIL(H5_ITER_ERROR);
1972 if (H5Oclose(oinfo.oid) < 0)
1973 BAIL(H5_ITER_ERROR);
1977 LOG((0,
"Unknown object class %d in %s!", oinfo.statbuf.type, __func__));
1978 BAIL(H5_ITER_ERROR);
1984 if (oinfo.oid > 0 && H5Oclose(oinfo.oid) < 0)
1985 BAIL2(H5_ITER_ERROR);
1999 nc4_rec_read_metadata(NC_GRP_INFO_T *grp)
2005 unsigned crt_order_flags = 0;
2006 H5_index_t iter_index;
2009 assert(grp && grp->name);
2010 LOG((3,
"%s: grp->name %s", __func__, grp->name));
2013 memset(&udata, 0,
sizeof(udata));
2017 if (!grp->hdf_grpid)
2021 if ((grp->hdf_grpid = H5Gopen2(grp->parent->hdf_grpid,
2022 grp->name, H5P_DEFAULT)) < 0)
2027 if ((grp->hdf_grpid = H5Gopen2(grp->nc4_info->hdfid,
2028 "/", H5P_DEFAULT)) < 0)
2032 assert(grp->hdf_grpid > 0);
2035 pid = H5Gget_create_plist(grp->hdf_grpid);
2036 H5Pget_link_creation_order(pid, &crt_order_flags);
2037 if (H5Pclose(pid) < 0)
2041 if (crt_order_flags & H5P_CRT_ORDER_TRACKED)
2042 iter_index = H5_INDEX_CRT_ORDER;
2045 NC_HDF5_FILE_INFO_T *h5 = grp->nc4_info;
2051 iter_index = H5_INDEX_NAME;
2060 if (H5Literate(grp->hdf_grpid, iter_index, H5_ITER_INC, &idx,
2061 nc4_rec_read_metadata_cb, (
void *)&udata) < 0)
2068 for (oinfo = udata.grps_head; oinfo; oinfo = udata.grps_head)
2070 NC_GRP_INFO_T *child_grp;
2071 NC_HDF5_FILE_INFO_T *h5 = grp->nc4_info;
2074 if ((retval = nc4_grp_list_add(&(grp->children), h5->next_nc_grpid++,
2075 grp, grp->nc4_info->controller, oinfo->oname, &child_grp)))
2079 if ((retval = nc4_rec_read_metadata(child_grp)))
2083 if (H5Oclose(oinfo->oid) < 0)
2087 udata.grps_head = oinfo->next;
2092 if ((retval = read_grp_atts(grp)))
2099 for (oinfo = udata.grps_head; oinfo; oinfo = udata.grps_head)
2102 if (H5Oclose(oinfo->oid) < 0)
2106 udata.grps_head = oinfo->next;
2118 nc4_open_file(
const char *path,
int mode, MPI_Comm comm,
2119 MPI_Info info, NC *nc)
2121 hid_t fapl_id = H5P_DEFAULT;
2122 unsigned flags = (mode &
NC_WRITE) ?
2123 H5F_ACC_RDWR : H5F_ACC_RDONLY;
2125 NC_HDF5_FILE_INFO_T* nc4_info = NULL;
2131 LOG((3,
"%s: path %s mode %d", __func__, path, mode));
2134 if(mode & NC_DISKLESS)
2138 if ((retval = nc4_nc4f_list_add(nc, path, mode)))
2140 nc4_info = NC4_DATA(nc);
2141 assert(nc4_info && nc4_info->root_grp);
2146 if ((fapl_id = H5Pcreate(H5P_FILE_ACCESS)) < 0)
2152 if (H5Pset_fclose_degree(fapl_id, H5F_CLOSE_SEMI))
2155 if (H5Pset_fclose_degree(fapl_id, H5F_CLOSE_STRONG))
2162 if (mode & NC_MPIIO || mode & NC_MPIPOSIX)
2164 nc4_info->parallel = NC_TRUE;
2165 if (mode & NC_MPIIO)
2167 LOG((4,
"opening parallel file with MPI/IO"));
2168 if (H5Pset_fapl_mpio(fapl_id, comm, info) < 0)
2173 LOG((4,
"opening parallel file with MPI/posix"));
2174 if (H5Pset_fapl_mpiposix(fapl_id, comm, 0) < 0)
2179 if (MPI_SUCCESS != MPI_Comm_dup(comm, &nc4_info->comm))
2182 if (MPI_INFO_NULL != info)
2184 if (MPI_SUCCESS != MPI_Info_dup(info, &nc4_info->info))
2191 nc4_info->info = info;
2195 if (H5Pset_cache(fapl_id, 0, nc4_chunk_cache_nelems, nc4_chunk_cache_size,
2196 nc4_chunk_cache_preemption) < 0)
2198 LOG((4,
"%s: set HDF raw chunk cache to size %d nelems %d preemption %f",
2199 __func__, nc4_chunk_cache_size, nc4_chunk_cache_nelems, nc4_chunk_cache_preemption));
2205 if ((nc4_info->hdfid = H5Fopen(path, flags, fapl_id)) < 0)
2210 nc4_info->no_write = NC_TRUE;
2216 if ((retval = nc4_rec_read_metadata(nc4_info->root_grp)))
2221 if ((retval = nc4_rec_match_dimscales(nc4_info->root_grp)))
2227 log_metadata_nc(nc);
2231 if (H5Pclose(fapl_id) < 0)
2241 if (comm_duped) MPI_Comm_free(&nc4_info->comm);
2242 if (info_duped) MPI_Info_free(&nc4_info->info);
2247 if (fapl_id != H5P_DEFAULT) H5Pclose(fapl_id);
2248 if (!nc4_info)
return retval;
2249 close_netcdf4_file(nc4_info,1);
2256 get_netcdf_type_from_hdf4(NC_HDF5_FILE_INFO_T *h5, int32 hdf4_typeid,
2257 nc_type *xtype, NC_TYPE_INFO_T *type_info)
2260 assert(h5 && xtype);
2308 if (hdf4_typeid == DFNT_FLOAT32)
2309 type_info->nc_type_class =
NC_FLOAT;
2310 else if (hdf4_typeid == DFNT_FLOAT64)
2312 else if (hdf4_typeid == DFNT_CHAR)
2315 type_info->nc_type_class =
NC_INT;
2317 type_info->nc_typeid = *xtype;
2318 type_info->size = nc_type_size_g[t];
2319 if (!(type_info->name = strdup(nc_type_name_g[t])))
2329 nc4_open_hdf4_file(
const char *path,
int mode, NC *nc)
2331 NC_HDF5_FILE_INFO_T *h5;
2334 int32 num_datasets, num_gatts;
2338 NC_HDF5_FILE_INFO_T* nc4_info = NULL;
2340 LOG((3,
"%s: path %s mode %d", __func__, path, mode));
2344 if (mode & NC_WRITE)
2348 if ((retval = nc4_nc4f_list_add(nc, path, mode)))
2350 nc4_info = NC4_DATA(nc);
2351 assert(nc4_info && nc4_info->root_grp);
2355 h5->no_write = NC_TRUE;
2358 if ((h5->sdid = SDstart(path, DFACC_READ)) == FAIL)
2362 if (SDfileinfo(h5->sdid, &num_datasets, &num_gatts))
2366 for (a = 0; a < num_gatts; a++)
2368 int32 att_data_type, att_count;
2369 size_t att_type_size;
2372 if ((retval = nc4_att_list_add(&h5->root_grp->att, &att)))
2374 att->attnum = grp->natts++;
2375 att->created = NC_TRUE;
2380 if (SDattrinfo(h5->sdid, a, att->name, &att_data_type, &att_count))
2382 if ((retval = get_netcdf_type_from_hdf4(h5, att_data_type,
2383 &att->nc_typeid, NULL)))
2385 att->len = att_count;
2388 if ((retval = nc4_get_typelen_mem(h5, att->nc_typeid, 0, &att_type_size)))
2390 if (!(att->data = malloc(att_type_size * att->len)))
2394 if (SDreadattr(h5->sdid, a, att->data))
2399 for (v = 0; v < num_datasets; v++)
2402 int32 data_type, num_atts;
2411 int32 *dimsize = NULL;
2412 size_t var_type_size;
2416 if ((retval = nc4_var_list_add(&grp->var, &var)))
2419 var->varid = grp->nvars++;
2420 var->created = NC_TRUE;
2421 var->written_to = NC_TRUE;
2424 if ((var->sdsid = SDselect(h5->sdid, v)) == FAIL)
2432 if (SDgetinfo(var->sdsid, var->name, &rank, NULL, &data_type, &num_atts))
2435 if(!(dimsize = (int32*)malloc(
sizeof(int32)*rank)))
2438 if (SDgetinfo(var->sdsid, var->name, &rank, dimsize, &data_type, &num_atts)) {
2439 if(dimsize) free(dimsize);
2444 var->hdf4_data_type = data_type;
2447 if (!(var->type_info = calloc(1,
sizeof(NC_TYPE_INFO_T)))) {
2448 if(dimsize) free(dimsize);
2452 if ((retval = get_netcdf_type_from_hdf4(h5, data_type, &var->type_info->nc_typeid, var->type_info))) {
2453 if(dimsize) free(dimsize);
2458 var->type_info->rc++;
2460 if ((retval = nc4_get_typelen_mem(h5, var->type_info->nc_typeid, 0, &var_type_size))) {
2461 if(dimsize) free(dimsize);
2465 var->type_info->size = var_type_size;
2466 LOG((3,
"reading HDF4 dataset %s, rank %d netCDF type %d", var->name,
2467 rank, var->type_info->nc_typeid));
2470 if (!(var->fill_value = malloc(var_type_size))) {
2471 if(dimsize) free(dimsize);
2475 if (SDgetfillvalue(var->sdsid, var->fill_value))
2478 free(var->fill_value);
2479 var->fill_value = NULL;
2485 if (!(var->dim = malloc(
sizeof(NC_DIM_INFO_T *) * var->ndims))) {
2486 if(dimsize) free(dimsize);
2490 if (!(var->dimids = malloc(
sizeof(
int) * var->ndims))) {
2491 if(dimsize) free(dimsize);
2498 for (d = 0; d < var->ndims; d++)
2500 int32 dimid, dim_len, dim_data_type, dim_num_attrs;
2504 if ((dimid = SDgetdimid(var->sdsid, d)) == FAIL) {
2505 if(dimsize) free(dimsize);
2508 if (SDdiminfo(dimid, dim_name, &dim_len, &dim_data_type,
2511 if(dimsize) free(dimsize);
2517 for (dim = grp->dim; dim; dim = dim->l.next)
2518 if (!strcmp(dim->name, dim_name))
2524 LOG((4,
"adding dimension %s for HDF4 dataset %s",
2525 dim_name, var->name));
2526 if ((retval = nc4_dim_list_add(&grp->dim, &dim)))
2529 dim->dimid = grp->nc4_info->next_dimid++;
2532 if (!(dim->name = strdup(dim_name)))
2537 dim->len = *dimsize;
2541 var->dimids[d] = dim->dimid;
2545 for (a = 0; a < num_atts; a++)
2547 int32 att_data_type, att_count;
2548 size_t att_type_size;
2551 if ((retval = nc4_att_list_add(&var->att, &att))) {
2552 if(dimsize) free(dimsize);
2555 att->attnum = var->natts++;
2556 att->created = NC_TRUE;
2560 if(dimsize) free(dimsize);
2563 if (SDattrinfo(var->sdsid, a, att->name, &att_data_type, &att_count)) {
2564 if(dimsize) free(dimsize);
2567 if ((retval = get_netcdf_type_from_hdf4(h5, att_data_type,
2568 &att->nc_typeid, NULL))) {
2569 if(dimsize) free(dimsize);
2573 att->len = att_count;
2576 if ((retval = nc4_get_typelen_mem(h5, att->nc_typeid, 0, &att_type_size))) {
2577 if(dimsize) free(dimsize);
2580 if (!(att->data = malloc(att_type_size * att->len))) {
2581 if(dimsize) free(dimsize);
2586 if (SDreadattr(var->sdsid, a, att->data)) {
2587 if(dimsize) free(dimsize);
2591 if(dimsize) free(dimsize);
2597 log_metadata_nc(h5->root_grp->nc4_info->controller);
2605 NC4_open(
const char *path,
int mode,
int basepe,
size_t *chunksizehintp,
2606 int use_parallel,
void *mpidata, NC_Dispatch *dispatch, NC *nc_file)
2609 MPI_Comm comm = MPI_COMM_WORLD;
2610 MPI_Info info = MPI_INFO_NULL;
2613 assert(nc_file && path);
2615 LOG((1,
"%s: path %s mode %d comm %d info %d",
2616 __func__, path, mode, comm, info));
2621 comm = ((NC_MPI_INFO *)mpidata)->comm;
2622 info = ((NC_MPI_INFO *)mpidata)->info;
2629 if (H5Eset_auto(NULL, NULL) < 0)
2630 LOG((0,
"Couldn't turn off HDF5 error messages!"));
2631 LOG((1,
"HDF5 error messages turned off!"));
2638 if (mode & ~(NC_WRITE |
NC_SHARE | NC_MPIIO | NC_MPIPOSIX |
2640 (mode & NC_MPIIO && mode & NC_MPIPOSIX))
2647 if(mode & NC_PNETCDF) {
2649 int pnetcdf_nvars, i;
2650 NC_HDF5_FILE_INFO_T* nc4_info;
2653 res = nc4_nc4f_list_add(nc_file, path, mode);
2655 nc4_info = NC4_DATA(nc_file);
2658 res = ncmpi_open(comm, path, mode, info, &(nc_file->int_ncid));
2659 nc4_info->pnetcdf_file++;
2663 res = ncmpi_begin_indep_data(nc_file->int_ncid);
2669 res = ncmpi_inq_nvars(nc_file->int_ncid, &pnetcdf_nvars);
2670 for (i = 0; i < pnetcdf_nvars; i++)
2671 res = ncmpi_inq_varndims(nc_file->int_ncid, i,
2672 &(nc4_info->pnetcdf_ndims[i]));
2679 if ((res = nc_check_for_hdf(path, use_parallel, comm, info, &hdf_file)))
2682 if (hdf_file == NC_HDF5_FILE)
2684 nc_file->int_ncid = nc_file->ext_ncid;
2685 res = nc4_open_file(path, mode, comm, info, nc_file);
2688 else if (hdf_file == NC_HDF4_FILE)
2690 nc_file->int_ncid = nc_file->ext_ncid;
2691 res = nc4_open_hdf4_file(path, mode, nc_file);
2710 NC4_set_fill(
int ncid,
int fillmode,
int *old_modep)
2713 NC_HDF5_FILE_INFO_T* nc4_info;
2715 LOG((2,
"%s: ncid 0x%x fillmode %d", __func__, ncid, fillmode));
2717 if (!(nc = nc4_find_nc_file(ncid,&nc4_info)))
2722 if (nc4_info->no_write)
2731 *old_modep = nc4_info->fill_mode;
2733 nc4_info->fill_mode = fillmode;
2737 if (nc4_info->pnetcdf_file)
2738 return ncmpi_set_fill(nc->int_ncid, fillmode, old_modep);
2751 NC_HDF5_FILE_INFO_T* nc4_info;
2753 LOG((1,
"%s: ncid 0x%x", __func__, ncid));
2756 if (!(nc4_find_nc_file(ncid,&nc4_info)))
2762 if (nc4_info->pnetcdf_file)
2763 return ncmpi_redef(nc->int_ncid);
2767 if (nc4_info->flags & NC_INDEF)
2771 if (nc4_info->no_write)
2775 nc4_info->flags |= NC_INDEF;
2779 nc4_info->redef = NC_TRUE;
2787 NC4__enddef(
int ncid,
size_t h_minfree,
size_t v_align,
2788 size_t v_minfree,
size_t r_align)
2790 if (nc4_find_nc_file(ncid,NULL) == NULL)
2793 return NC4_enddef(ncid);
2798 static int NC4_enddef(
int ncid)
2801 NC_HDF5_FILE_INFO_T* nc4_info;
2803 LOG((1,
"%s: ncid 0x%x", __func__, ncid));
2805 if (!(nc = nc4_find_nc_file(ncid,&nc4_info)))
2810 if (nc4_info->pnetcdf_file)
2813 res = ncmpi_enddef(nc->int_ncid);
2816 if (nc4_info->pnetcdf_access_mode == NC_INDEPENDENT)
2817 res = ncmpi_begin_indep_data(nc->int_ncid);
2823 return nc4_enddef_netcdf4_file(nc4_info);
2829 sync_netcdf4_file(NC_HDF5_FILE_INFO_T *h5)
2834 LOG((3,
"%s", __func__));
2838 if (h5->flags & NC_INDEF)
2844 h5->flags ^= NC_INDEF;
2847 h5->redef = NC_FALSE;
2853 log_metadata_nc(h5->root_grp->nc4_info->controller);
2859 int bad_coord_order = 0;
2861 if ((retval = nc4_rec_write_groups_types(h5->root_grp)))
2863 if ((retval = nc4_rec_detect_need_to_preserve_dimids(h5->root_grp, &bad_coord_order)))
2865 if ((retval = nc4_rec_write_metadata(h5->root_grp, bad_coord_order)))
2869 if (H5Fflush(h5->hdfid, H5F_SCOPE_GLOBAL) < 0)
2882 NC_HDF5_FILE_INFO_T* nc4_info;
2884 LOG((2,
"%s: ncid 0x%x", __func__, ncid));
2886 if (!(nc = nc4_find_nc_file(ncid,&nc4_info)))
2892 if (nc4_info->pnetcdf_file)
2893 return ncmpi_sync(nc->int_ncid);
2897 if (nc4_info && nc4_info->flags & NC_INDEF)
2901 if ((retval = NC4_enddef(ncid)))
2905 return sync_netcdf4_file(nc4_info);
2912 close_netcdf4_file(NC_HDF5_FILE_INFO_T *h5,
int abort)
2916 assert(h5 && h5->root_grp);
2917 LOG((3,
"%s: h5->path %s abort %d", __func__, h5->controller->path, abort));
2920 if (h5->flags & NC_INDEF)
2921 h5->flags ^= NC_INDEF;
2925 if (!h5->no_write && !abort)
2926 if ((retval = sync_netcdf4_file(h5)))
2931 if ((retval = nc4_rec_grp_del(&h5->root_grp, h5->root_grp)))
2938 if (SDend(h5->sdid))
2948 MPI_Comm_free(&h5->comm);
2949 if(MPI_INFO_NULL != h5->info)
2950 MPI_Info_free(&h5->info);
2953 if (H5Fclose(h5->hdfid) < 0)
2957 nobjs = H5Fget_obj_count(h5->hdfid, H5F_OBJ_ALL);
2961 }
else if(nobjs > 0) {
2967 LOG((0,
"There are %d HDF5 objects open!", nobjs));
2993 int delete_file = 0;
2996 NC_HDF5_FILE_INFO_T* nc4_info;
2998 LOG((2,
"%s: ncid 0x%x", __func__, ncid));
3001 if (!(nc = nc4_find_nc_file(ncid,&nc4_info)))
3008 if (nc4_info->pnetcdf_file)
3009 return ncmpi_abort(nc->int_ncid);
3013 if (nc4_info->flags & NC_INDEF && !nc4_info->redef)
3021 if ((retval = close_netcdf4_file(nc4_info, 1)))
3026 if (
remove(path) < 0)
3038 NC_HDF5_FILE_INFO_T *h5;
3041 LOG((1,
"%s: ncid 0x%x", __func__, ncid));
3044 if ((retval = nc4_find_nc_grp_h5(ncid, &nc, &grp, &h5)))
3047 assert(nc && h5 && grp);
3055 if (h5->pnetcdf_file)
3056 return ncmpi_close(nc->int_ncid);
3060 if ((retval = close_netcdf4_file(grp->nc4_info, 0)))
3069 NC4_inq(
int ncid,
int *ndimsp,
int *nvarsp,
int *nattsp,
int *unlimdimidp)
3072 NC_HDF5_FILE_INFO_T *h5;
3079 LOG((2,
"%s: ncid 0x%x", __func__, ncid));
3082 if ((retval = nc4_find_nc_grp_h5(ncid, &nc, &grp, &h5)))
3085 assert(h5 && grp && nc);
3089 if (h5->pnetcdf_file)
3090 return ncmpi_inq(nc->int_ncid, ndimsp, nvarsp, nattsp, unlimdimidp);
3097 for (dim = grp->dim; dim; dim = dim->l.next)
3103 for (var = grp->var; var; var= var->l.next)
3109 for (att = grp->att; att; att = att->l.next)
3122 for (dim = grp->dim; dim; dim = dim->l.next)
3125 *unlimdimidp = dim->dimid;
3136 nc4_enddef_netcdf4_file(NC_HDF5_FILE_INFO_T *h5)
3139 LOG((3,
"%s", __func__));
3142 if (!(h5->flags & NC_INDEF))
3146 h5->flags ^= NC_INDEF;
3149 h5->redef = NC_FALSE;
3151 return sync_netcdf4_file(h5);
3158 if (num_plists || num_spaces)
3167 nc_use_parallel_enabled()
#define NC_PNETCDF
Use parallel-netcdf library.
#define NC_ENOMEM
Memory allocation (malloc) failure.
#define NC_CHAR
ISO/ASCII character.
#define NC_ECANTWRITE
Can't write.
#define NC_SHARE
Share updates, limit cacheing.
#define NC_UBYTE
unsigned 1 byte int
#define NC_CLASSIC_MODEL
Enforce classic model.
#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_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_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_FORMAT_64BIT
Format specifier for nc_set_default_format() and returned by nc_inq_format.
This is the type of arrays of vlens.
#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.
#define NC_MAX_UINT
Max or min values for a type.
int NC4_create(const char *path, int cmode, size_t initialsz, int basepe, size_t *chunksizehintp, int use_parallel, void *mpidata, NC_Dispatch *dispatch, NC *nc_file)
Create a netCDF-4/HDF5 file.
#define NC_SHORT
signed 2 byte integer
#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_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.