16 #include "nc4internal.h"
17 #include "nc4dispatch.h"
19 #include "H5DSpublic.h"
32 extern int num_plists;
33 extern int num_spaces;
36 #define MIN_DEFLATE_LEVEL 0
37 #define MAX_DEFLATE_LEVEL 9
41 #define REFERENCE_LIST "REFERENCE_LIST"
43 #define DIMENSION_LIST "DIMENSION_LIST"
72 static int NC4_enddef(
int ncid);
73 static int nc4_rec_read_metadata(NC_GRP_INFO_T *grp);
74 static int close_netcdf4_file(NC_HDF5_FILE_INFO_T *h5,
int abort);
78 size_t nc4_chunk_cache_size = CHUNK_CACHE_SIZE;
79 size_t nc4_chunk_cache_nelems = CHUNK_CACHE_NELEMS;
80 float nc4_chunk_cache_preemption = CHUNK_CACHE_PREEMPTION;
84 static int virgin = 1;
89 static hid_t h5_native_type_constant_g[NUM_TYPES];
90 static const char nc_type_name_g[NUM_TYPES][
NC_MAX_NAME + 1] = {
"char",
"byte",
"short",
91 "int",
"float",
"double",
"ubyte",
92 "ushort",
"uint",
"int64",
98 static const int nc_type_size_g[NUM_TYPES] = {
sizeof(char),
sizeof(
char),
sizeof(short),
99 sizeof(
int),
sizeof(float),
sizeof(
double),
sizeof(
unsigned char),
100 sizeof(
unsigned short),
sizeof(
unsigned int),
sizeof(
long long),
101 sizeof(
unsigned long long),
sizeof(
char *)};
106 nc_set_chunk_cache(
size_t size,
size_t nelems,
float preemption)
108 if (preemption < 0 || preemption > 1)
110 nc4_chunk_cache_size = size;
111 nc4_chunk_cache_nelems = nelems;
112 nc4_chunk_cache_preemption = preemption;
119 nc_get_chunk_cache(
size_t *sizep,
size_t *nelemsp,
float *preemptionp)
122 *sizep = nc4_chunk_cache_size;
125 *nelemsp = nc4_chunk_cache_nelems;
128 *preemptionp = nc4_chunk_cache_preemption;
134 nc_set_chunk_cache_ints(
int size,
int nelems,
int preemption)
136 if (size <= 0 || nelems <= 0 || preemption < 0 || preemption > 100)
138 nc4_chunk_cache_size = size;
139 nc4_chunk_cache_nelems = nelems;
140 nc4_chunk_cache_preemption = (float)preemption / 100;
145 nc_get_chunk_cache_ints(
int *sizep,
int *nelemsp,
int *preemptionp)
148 *sizep = (int)nc4_chunk_cache_size;
150 *nelemsp = (int)nc4_chunk_cache_nelems;
152 *preemptionp = (int)(nc4_chunk_cache_preemption * 100);
182 #define MAGIC_NUMBER_LEN 4
183 #define NC_HDF5_FILE 1
184 #define NC_HDF4_FILE 2
186 nc_check_for_hdf(
const char *path,
int use_parallel, MPI_Comm comm, MPI_Info info,
189 char blob[MAGIC_NUMBER_LEN];
191 assert(hdf_file && path);
192 LOG((3,
"%s: path %s", __func__, path));
197 *hdf_file = NC_HDF5_FILE;
208 if ((retval = MPI_File_open(comm, (
char *)path, MPI_MODE_RDONLY,
209 info, &fh)) != MPI_SUCCESS)
211 if ((retval = MPI_File_read(fh, blob, MAGIC_NUMBER_LEN, MPI_CHAR,
212 &status)) != MPI_SUCCESS)
214 if ((retval = MPI_File_close(&fh)) != MPI_SUCCESS)
221 if (!(fp = fopen(path,
"r")) ||
222 fread(blob, MAGIC_NUMBER_LEN, 1, fp) != 1) {
231 if (!strncmp(blob,
"\016\003\023\001", MAGIC_NUMBER_LEN))
232 *hdf_file = NC_HDF4_FILE;
242 nc4_create_file(
const char *path,
int cmode, MPI_Comm comm, MPI_Info info,
245 hid_t fcpl_id, fapl_id = -1;
249 NC_HDF5_FILE_INFO_T* nc4_info = NULL;
260 flags = H5F_ACC_TRUNC;
262 flags = H5F_ACC_EXCL;
264 flags = H5F_ACC_TRUNC;
266 LOG((3,
"%s: path %s mode 0x%x", __func__, path, cmode));
271 if (cmode & NC_DISKLESS) {
276 }
else if ((cmode & NC_NOCLOBBER) && (fp = fopen(path,
"r"))) {
282 if ((retval = nc4_nc4f_list_add(nc, path, (
NC_WRITE | cmode))))
284 nc4_info = NC4_DATA(nc);
285 assert(nc4_info && nc4_info->root_grp);
295 if ((fapl_id = H5Pcreate(H5P_FILE_ACCESS)) < 0)
301 if (H5Pset_fclose_degree(fapl_id, H5F_CLOSE_SEMI))
304 if (H5Pset_fclose_degree(fapl_id, H5F_CLOSE_STRONG))
313 nc4_info->parallel = NC_TRUE;
314 if (cmode & NC_MPIIO)
316 LOG((4,
"creating parallel file with MPI/IO"));
317 if (H5Pset_fapl_mpio(fapl_id, comm, info) < 0)
320 #ifdef USE_PARALLEL_POSIX
323 LOG((4,
"creating parallel file with MPI/posix"));
324 if (H5Pset_fapl_mpiposix(fapl_id, comm, 0) < 0)
337 if (MPI_SUCCESS != MPI_Comm_dup(comm, &nc4_info->comm))
340 if (MPI_INFO_NULL != info)
342 if (MPI_SUCCESS != MPI_Info_dup(info, &nc4_info->info))
349 nc4_info->info = info;
353 if(cmode & NC_DISKLESS) {
354 if (H5Pset_fapl_core(fapl_id, 4096, persist))
357 if (H5Pset_cache(fapl_id, 0, nc4_chunk_cache_nelems, nc4_chunk_cache_size,
358 nc4_chunk_cache_preemption) < 0)
360 LOG((4,
"%s: set HDF raw chunk cache to size %d nelems %d preemption %f",
361 __func__, nc4_chunk_cache_size, nc4_chunk_cache_nelems, nc4_chunk_cache_preemption));
364 if (H5Pset_libver_bounds(fapl_id, H5F_LIBVER_LATEST, H5F_LIBVER_LATEST) < 0)
368 if ((fcpl_id = H5Pcreate(H5P_FILE_CREATE)) < 0)
375 if (H5Pset_obj_track_times(fcpl_id,0)<0)
381 if (H5Pset_link_creation_order(fcpl_id, (H5P_CRT_ORDER_TRACKED |
382 H5P_CRT_ORDER_INDEXED)) < 0)
384 if (H5Pset_attr_creation_order(fcpl_id, (H5P_CRT_ORDER_TRACKED |
385 H5P_CRT_ORDER_INDEXED)) < 0)
389 if ((nc4_info->hdfid = H5Fcreate(path, flags, fcpl_id, fapl_id)) < 0)
395 if ((nc4_info->root_grp->hdf_grpid = H5Gopen2(nc4_info->hdfid,
"/",
400 if (H5Pclose(fapl_id) < 0 ||
401 H5Pclose(fcpl_id) < 0)
409 nc4_info->flags |= NC_INDEF;
415 if (comm_duped) MPI_Comm_free(&nc4_info->comm);
416 if (info_duped) MPI_Info_free(&nc4_info->info);
421 if (fapl_id != H5P_DEFAULT) H5Pclose(fapl_id);
422 if(!nc4_info)
return retval;
423 close_netcdf4_file(nc4_info,1);
443 NC4_create(
const char* path,
int cmode,
size_t initialsz,
int basepe,
444 size_t *chunksizehintp,
int use_parallel,
void *mpidata,
445 NC_Dispatch *dispatch, NC* nc_file)
447 MPI_Comm comm = MPI_COMM_WORLD;
448 MPI_Info info = MPI_INFO_NULL;
451 assert(nc_file && path);
453 LOG((1,
"%s: path %s cmode 0x%x comm %d info %d",
454 __func__, path, cmode, comm, info));
459 comm = ((NC_MPI_INFO *)mpidata)->comm;
460 info = ((NC_MPI_INFO *)mpidata)->info;
467 if (H5Eset_auto(NULL, NULL) < 0)
468 LOG((0,
"Couldn't turn off HDF5 error messages!"));
469 LOG((1,
"HDF5 error messages have been turned off."));
480 || (cmode & NC_MPIIO && cmode & NC_MPIPOSIX)
482 || (cmode & (NC_MPIIO | NC_MPIPOSIX) && cmode & NC_DISKLESS)
486 #ifndef USE_PARALLEL_POSIX
490 if(cmode & NC_MPIPOSIX)
492 cmode &= ~NC_MPIPOSIX;
505 LOG((2,
"cmode after applying default format: 0x%x", cmode));
507 nc_file->int_ncid = nc_file->ext_ncid;
508 res = nc4_create_file(path, cmode, comm, info, nc_file);
511 if (cmode & NC_PNETCDF)
513 NC_HDF5_FILE_INFO_T* nc4_info;
514 nc4_info = NC4_DATA(nc_file);
517 nc4_info->pnetcdf_file++;
518 res = ncmpi_create(comm, path, cmode, info, &(nc_file->int_ncid));
531 read_scale(NC_GRP_INFO_T *grp, hid_t datasetid,
const char *obj_name,
532 const H5G_stat_t *statbuf, hsize_t scale_size, hsize_t max_scale_size,
535 NC_DIM_INFO_T *new_dim;
537 htri_t attr_exists = -1;
539 int dimscale_created = 0;
540 int initial_grp_ndims = grp->ndims;
541 short initial_next_dimid = grp->nc4_info->next_dimid;
545 if ((retval = nc4_dim_list_add(&grp->dim, &new_dim)))
551 if ((attr_exists = H5Aexists(datasetid, NC_DIMID_ATT_NAME)) < 0)
555 if ((attid = H5Aopen_by_name(datasetid,
".", NC_DIMID_ATT_NAME,
556 H5P_DEFAULT, H5P_DEFAULT)) < 0)
559 if (H5Aread(attid, H5T_NATIVE_INT, &new_dim->dimid) < 0)
563 if (new_dim->dimid >= grp->nc4_info->next_dimid)
564 grp->nc4_info->next_dimid = new_dim->dimid + 1;
569 new_dim->dimid = grp->nc4_info->next_dimid++;
575 if (!(new_dim->name = strdup(obj_name)))
580 new_dim->too_long = NC_TRUE;
583 new_dim->len = scale_size;
584 new_dim->hdf5_objid.fileno[0] = statbuf->fileno[0];
585 new_dim->hdf5_objid.fileno[1] = statbuf->fileno[1];
586 new_dim->hdf5_objid.objno[0] = statbuf->objno[0];
587 new_dim->hdf5_objid.objno[1] = statbuf->objno[1];
591 if (max_scale_size == H5S_UNLIMITED)
592 new_dim->unlimited = NC_TRUE;
597 if (H5DSget_scale_name(datasetid, dimscale_name_att,
NC_MAX_NAME) >= 0)
599 if (!strncmp(dimscale_name_att, DIM_WITHOUT_VARIABLE,
600 strlen(DIM_WITHOUT_VARIABLE)))
602 if (new_dim->unlimited)
604 size_t len = 0, *lenp = &len;
606 if ((retval = nc4_find_dim_len(grp, new_dim->dimid, &lenp)))
608 new_dim->len = *lenp;
612 new_dim->hdf_dimscaleid = datasetid;
613 H5Iinc_ref(new_dim->hdf_dimscaleid);
622 if (attid > 0 && H5Aclose(attid) < 0)
626 if (retval < 0 && dimscale_created)
629 if ((retval = nc4_dim_list_del(&grp->dim, new_dim)))
633 grp->ndims = initial_grp_ndims;
634 grp->nc4_info->next_dimid = initial_next_dimid;
643 read_coord_dimids(NC_VAR_INFO_T *var)
645 hid_t coord_att_typeid = -1, coord_attid = -1, spaceid = -1;
652 if ((coord_attid = H5Aopen_name(var->hdf_datasetid, COORDINATES)) < 0) ret++;
653 if (!ret && (coord_att_typeid = H5Aget_type(coord_attid)) < 0) ret++;
656 if (!ret && (spaceid = H5Aget_space(coord_attid)) < 0) ret++;
660 if (!ret && (npoints = H5Sget_simple_extent_npoints(spaceid)) < 0) ret++;
664 if (!ret && npoints != var->ndims) ret++;
666 if (!ret && H5Aread(coord_attid, coord_att_typeid, var->dimids) < 0) ret++;
667 LOG((4,
"dimscale %s is multidimensional and has coords", var->name));
670 if (spaceid >= 0 && H5Sclose(spaceid) < 0) ret++;
674 if (coord_att_typeid >= 0 && H5Tclose(coord_att_typeid) < 0) ret++;
675 if (coord_attid >= 0 && H5Aclose(coord_attid) < 0) ret++;
682 dimscale_visitor(hid_t did,
unsigned dim, hid_t dsid,
683 void *dimscale_hdf5_objids)
688 if (H5Gget_objinfo(dsid,
".", 1, &statbuf) < 0)
692 (*(HDF5_OBJID_T *)dimscale_hdf5_objids).fileno[0] = statbuf.fileno[0];
693 (*(HDF5_OBJID_T *)dimscale_hdf5_objids).fileno[1] = statbuf.fileno[1];
694 (*(HDF5_OBJID_T *)dimscale_hdf5_objids).objno[0] = statbuf.objno[0];
695 (*(HDF5_OBJID_T *)dimscale_hdf5_objids).objno[1] = statbuf.objno[1];
701 get_netcdf_type(NC_HDF5_FILE_INFO_T *h5, hid_t native_typeid,
704 NC_TYPE_INFO_T *type;
706 htri_t is_str, equal = 0;
710 if ((
class = H5Tget_class(native_typeid)) < 0)
715 if (
class == H5T_STRING)
717 if ((is_str = H5Tis_variable_str(native_typeid)) < 0)
725 else if (
class == H5T_INTEGER ||
class == H5T_FLOAT)
729 if ((equal = H5Tequal(native_typeid, H5T_NATIVE_SCHAR)) < 0)
736 if ((equal = H5Tequal(native_typeid, H5T_NATIVE_SHORT)) < 0)
743 if ((equal = H5Tequal(native_typeid, H5T_NATIVE_INT)) < 0)
750 if ((equal = H5Tequal(native_typeid, H5T_NATIVE_FLOAT)) < 0)
757 if ((equal = H5Tequal(native_typeid, H5T_NATIVE_DOUBLE)) < 0)
764 if ((equal = H5Tequal(native_typeid, H5T_NATIVE_UCHAR)) < 0)
771 if ((equal = H5Tequal(native_typeid, H5T_NATIVE_USHORT)) < 0)
778 if ((equal = H5Tequal(native_typeid, H5T_NATIVE_UINT)) < 0)
785 if ((equal = H5Tequal(native_typeid, H5T_NATIVE_LLONG)) < 0)
792 if ((equal = H5Tequal(native_typeid, H5T_NATIVE_ULLONG)) < 0)
803 if((type = nc4_rec_find_hdf_type(h5->root_grp, native_typeid)))
805 *xtype = type->nc_typeid;
817 get_type_info2(NC_HDF5_FILE_INFO_T *h5, hid_t datasetid,
818 NC_TYPE_INFO_T **type_info)
820 htri_t is_str, equal = 0;
822 hid_t native_typeid, hdf_typeid;
826 assert(h5 && type_info);
834 if (!h5_native_type_constant_g[1])
836 h5_native_type_constant_g[1] = H5T_NATIVE_SCHAR;
837 h5_native_type_constant_g[2] = H5T_NATIVE_SHORT;
838 h5_native_type_constant_g[3] = H5T_NATIVE_INT;
839 h5_native_type_constant_g[4] = H5T_NATIVE_FLOAT;
840 h5_native_type_constant_g[5] = H5T_NATIVE_DOUBLE;
841 h5_native_type_constant_g[6] = H5T_NATIVE_UCHAR;
842 h5_native_type_constant_g[7] = H5T_NATIVE_USHORT;
843 h5_native_type_constant_g[8] = H5T_NATIVE_UINT;
844 h5_native_type_constant_g[9] = H5T_NATIVE_LLONG;
845 h5_native_type_constant_g[10] = H5T_NATIVE_ULLONG;
849 if ((hdf_typeid = H5Dget_type(datasetid)) < 0)
854 if ((native_typeid = H5Tget_native_type(hdf_typeid, H5T_DIR_DEFAULT)) < 0)
858 if ((
class = H5Tget_class(native_typeid)) < 0)
862 if (
class == H5T_STRING ||
class == H5T_INTEGER ||
class == H5T_FLOAT)
865 if (!(*type_info = calloc(1,
sizeof(NC_TYPE_INFO_T))))
870 if (
class == H5T_STRING)
872 if ((is_str = H5Tis_variable_str(native_typeid)) < 0)
875 if (is_str || H5Tget_size(hdf_typeid) > 1)
885 (*type_info)->nc_type_class =
NC_CHAR;
888 else if (
class == H5T_INTEGER ||
class == H5T_FLOAT)
890 for (t = 1; t < NUM_TYPES - 1; t++)
892 if ((equal = H5Tequal(native_typeid, h5_native_type_constant_g[t])) < 0)
899 if (
class == H5T_INTEGER)
901 if ((order = H5Tget_order(hdf_typeid)) < 0)
905 if (order == H5T_ORDER_LE)
907 else if (order == H5T_ORDER_BE)
914 (*type_info)->nc_type_class =
NC_INT;
920 (*type_info)->nc_type_class =
NC_FLOAT;
923 (*type_info)->nc_typeid = nc_type_constant_g[t];
924 (*type_info)->size = nc_type_size_g[t];
925 if (!((*type_info)->name = strdup(nc_type_name_g[t])))
927 (*type_info)->hdf_typeid = hdf_typeid;
928 (*type_info)->native_hdf_typeid = native_typeid;
933 NC_TYPE_INFO_T *type;
936 if((type = nc4_rec_find_hdf_type(h5->root_grp, native_typeid)))
942 if (H5Tclose(native_typeid) < 0)
944 if (H5Tclose(hdf_typeid) < 0)
956 read_hdf5_att(NC_GRP_INFO_T *grp, hid_t attid, NC_ATT_INFO_T *att)
958 hid_t spaceid = 0, file_typeid = 0;
959 hsize_t dims[1] = {0};
963 hssize_t att_npoints;
964 H5T_class_t att_class;
965 int fixed_len_string = 0;
966 size_t fixed_size = 0;
969 LOG((5,
"%s: att->attnum %d att->name %s att->nc_typeid %d att->len %d",
970 __func__, att->attnum, att->name, (
int)att->nc_typeid, att->len));
973 if ((file_typeid = H5Aget_type(attid)) < 0)
975 if ((att->native_hdf_typeid = H5Tget_native_type(file_typeid, H5T_DIR_DEFAULT)) < 0)
977 if ((att_class = H5Tget_class(att->native_hdf_typeid)) < 0)
979 if (att_class == H5T_STRING && !H5Tis_variable_str(att->native_hdf_typeid))
982 if (!(fixed_size = H5Tget_size(att->native_hdf_typeid)))
985 if ((retval = get_netcdf_type(grp->nc4_info, att->native_hdf_typeid, &(att->nc_typeid))))
990 if ((spaceid = H5Aget_space(attid)) < 0)
995 if ((att_ndims = H5Sget_simple_extent_ndims(spaceid)) < 0)
997 if ((att_npoints = H5Sget_simple_extent_npoints(spaceid)) < 0)
1002 if (att_ndims == 0 && att_npoints == 0)
1005 dims[0] = att_npoints;
1006 else if (att->nc_typeid ==
NC_CHAR)
1012 if (!(dims[0] = H5Tget_size(file_typeid)))
1019 dims[0] = att_npoints;
1024 H5S_class_t space_class;
1031 if ((space_class = H5Sget_simple_extent_type(spaceid)) < 0)
1035 if (H5S_NULL == space_class)
1039 if (H5S_SCALAR == space_class)
1044 if (H5Sget_simple_extent_dims(spaceid, dims, NULL) < 0)
1056 if ((retval = nc4_get_typelen_mem(grp->nc4_info, att->nc_typeid, 0,
1059 if (att_class == H5T_VLEN)
1061 if (!(att->vldata = malloc((
unsigned int)(att->len *
sizeof(hvl_t)))))
1063 if (H5Aread(attid, att->native_hdf_typeid, att->vldata) < 0)
1068 if (!(att->stdata = calloc(att->len,
sizeof(
char *))))
1081 if (fixed_len_string)
1084 char *contig_buf, *cur;
1087 if (!(contig_buf = malloc(att->len * fixed_size *
sizeof(
char))))
1091 if (H5Aread(attid, att->native_hdf_typeid, contig_buf) < 0) {
1100 for (i = 0; i < att->len; i++)
1102 if (!(att->stdata[i] = malloc(fixed_size))) {
1106 strncpy(att->stdata[i], cur, fixed_size);
1116 if (H5Aread(attid, att->native_hdf_typeid, att->stdata) < 0)
1122 if (!(att->data = malloc((
unsigned int)(att->len * type_size))))
1124 if (H5Aread(attid, att->native_hdf_typeid, att->data) < 0)
1129 if (H5Tclose(file_typeid) < 0)
1131 if (H5Sclose(spaceid) < 0)
1140 if (H5Tclose(file_typeid) < 0)
1142 if (spaceid > 0 && H5Sclose(spaceid) < 0)
1153 read_type(NC_GRP_INFO_T *grp, hid_t hdf_typeid,
char *type_name)
1155 NC_TYPE_INFO_T *type;
1157 hid_t native_typeid;
1161 assert(grp && type_name);
1163 LOG((4,
"%s: type_name %s grp->name %s", __func__, type_name, grp->name));
1166 if ((native_typeid = H5Tget_native_type(hdf_typeid, H5T_DIR_DEFAULT)) < 0)
1170 if (!(type_size = H5Tget_size(native_typeid)))
1172 LOG((5,
"type_size %d", type_size));
1175 if ((retval = nc4_type_list_add(grp, type_size, type_name, &type)))
1179 type->committed = NC_TRUE;
1180 type->hdf_typeid = hdf_typeid;
1181 H5Iinc_ref(type->hdf_typeid);
1182 type->native_hdf_typeid = native_typeid;
1185 if ((
class = H5Tget_class(hdf_typeid)) < 0)
1197 char* member_name = NULL;
1204 if ((nmembers = H5Tget_nmembers(hdf_typeid)) < 0)
1206 LOG((5,
"compound type has %d members", nmembers));
1207 for (m = 0; m < nmembers; m++)
1209 hid_t member_hdf_typeid;
1210 hid_t member_native_typeid;
1211 size_t member_offset;
1212 H5T_class_t mem_class;
1218 if ((member_hdf_typeid = H5Tget_member_type(type->native_hdf_typeid, m)) < 0)
1221 if ((member_native_typeid = H5Tget_native_type(member_hdf_typeid, H5T_DIR_DEFAULT)) < 0)
1225 member_name = H5Tget_member_name(type->native_hdf_typeid, m);
1226 if (!member_name || strlen(member_name) >
NC_MAX_NAME) {
1232 strncpy(jna,member_name,1000);
1238 member_offset = H5Tget_member_offset(type->native_hdf_typeid, m);
1241 if ((mem_class = H5Tget_class(member_hdf_typeid)) < 0)
1243 if (mem_class == H5T_ARRAY)
1249 if ((ndims = H5Tget_array_ndims(member_hdf_typeid)) < 0) {
1253 if (H5Tget_array_dims(member_hdf_typeid, dims, NULL) != ndims) {
1257 for (d = 0; d < ndims; d++)
1258 dim_size[d] = dims[d];
1261 if ((retval = get_netcdf_type(grp->nc4_info, H5Tget_super(member_hdf_typeid),
1266 if ((retval = nc4_field_list_add(&type->u.c.field, type->u.c.num_fields++, member_name,
1267 member_offset, H5Tget_super(member_hdf_typeid),
1268 H5Tget_super(member_native_typeid),
1269 member_xtype, ndims, dim_size)))
1275 if ((retval = get_netcdf_type(grp->nc4_info, member_native_typeid,
1280 if ((retval = nc4_field_list_add(&type->u.c.field, type->u.c.num_fields++, member_name,
1281 member_offset, member_hdf_typeid, member_native_typeid,
1282 member_xtype, 0, NULL)))
1291 if(member_name != NULL) free(member_name);
1297 if(member_name != NULL)
1313 if ((ret = H5Tis_variable_str(hdf_typeid)) < 0)
1319 hid_t base_hdf_typeid;
1322 type->nc_type_class =
NC_VLEN;
1326 if (!(base_hdf_typeid = H5Tget_super(native_typeid)))
1330 if (!(type_size = H5Tget_size(base_hdf_typeid)))
1334 if ((retval = get_netcdf_type(grp->nc4_info, base_hdf_typeid,
1337 LOG((5,
"base_hdf_typeid 0x%x type_size %d base_nc_type %d",
1338 base_hdf_typeid, type_size, base_nc_type));
1341 type->u.v.base_nc_typeid = base_nc_type;
1342 type->u.v.base_hdf_typeid = base_hdf_typeid;
1353 hid_t base_hdf_typeid;
1357 char *member_name = NULL;
1362 type->nc_type_class =
NC_ENUM;
1366 if (!(base_hdf_typeid = H5Tget_super(hdf_typeid)))
1369 if (!(type_size = H5Tget_size(base_hdf_typeid)))
1372 if ((retval = get_netcdf_type(grp->nc4_info, base_hdf_typeid,
1375 LOG((5,
"base_hdf_typeid 0x%x type_size %d base_nc_type %d",
1376 base_hdf_typeid, type_size, base_nc_type));
1379 type->u.e.base_nc_typeid = base_nc_type;
1380 type->u.e.base_hdf_typeid = base_hdf_typeid;
1383 if ((type->u.e.num_members = H5Tget_nmembers(hdf_typeid)) < 0)
1387 if (!(value = calloc(1, type_size)))
1391 for (i = 0; i < type->u.e.num_members; i++)
1395 if (!(member_name = H5Tget_member_name(hdf_typeid, i)))
1401 strncpy(jna,member_name,1000);
1410 if (H5Tget_member_value(hdf_typeid, i, value) < 0)
1417 if ((retval = nc4_enum_member_add(&type->u.e.enum_member, type->size,
1418 member_name, value)))
1425 if(member_name != NULL) free(member_name);
1431 if(member_name != NULL)
1434 if(value) free(value);
1441 LOG((0,
"unknown class"));
1452 read_var(NC_GRP_INFO_T *grp, hid_t datasetid,
const char *obj_name,
1453 size_t ndims, NC_DIM_INFO_T *dim)
1455 NC_VAR_INFO_T *var = NULL;
1456 hid_t access_pid = 0;
1462 char att_name[NC_MAX_HDF5_NAME + 1];
1464 #define CD_NELEMS_ZLIB 1
1465 #define CD_NELEMS_SZIP 4
1466 H5Z_filter_t filter;
1468 unsigned int cd_values[CD_NELEMS_SZIP];
1469 size_t cd_nelems = CD_NELEMS_SZIP;
1471 H5D_fill_value_t fill_status;
1472 H5D_layout_t layout;
1478 assert(obj_name && grp);
1479 LOG((4,
"%s: obj_name %s", __func__, obj_name));
1482 if ((retval = nc4_var_list_add(&grp->var, &var)))
1486 var->hdf_datasetid = datasetid;
1487 H5Iinc_ref(var->hdf_datasetid);
1489 var->varid = grp->nvars++;
1490 var->created = NC_TRUE;
1497 if (!(var->dim = calloc(var->ndims,
sizeof(NC_DIM_INFO_T *))))
1499 if (!(var->dimids = calloc(var->ndims,
sizeof(
int))))
1504 if ((access_pid = H5Dget_access_plist(datasetid)) < 0)
1511 if ((H5Pget_chunk_cache(access_pid, &(var->chunk_cache_nelems),
1512 &(var->chunk_cache_size), &rdcc_w0)) < 0)
1514 var->chunk_cache_preemption = rdcc_w0;
1519 if (strlen(obj_name) > strlen(NON_COORD_PREPEND) &&
1520 !strncmp(obj_name, NON_COORD_PREPEND, strlen(NON_COORD_PREPEND)))
1523 if (!(var->name = malloc(((strlen(obj_name) - strlen(NON_COORD_PREPEND))+ 1) *
sizeof(
char))))
1526 strcpy(var->name, &obj_name[strlen(NON_COORD_PREPEND)]);
1529 if (!(var->hdf5_name = malloc((strlen(obj_name) + 1) *
sizeof(
char))))
1532 strcpy(var->hdf5_name, obj_name);
1537 if (!(var->name = malloc((strlen(obj_name) + 1) *
sizeof(
char))))
1540 strcpy(var->name, obj_name);
1546 if ((propid = H5Dget_create_plist(datasetid)) < 0)
1553 if ((layout = H5Pget_layout(propid)) < -1)
1555 if (layout == H5D_CHUNKED)
1559 if (!(var->chunksizes = malloc(var->ndims *
sizeof(
size_t))))
1561 for (d = 0; d < var->ndims; d++)
1562 var->chunksizes[d] = chunksize[d];
1564 else if (layout == H5D_CONTIGUOUS || layout == H5D_COMPACT)
1565 var->contiguous = NC_TRUE;
1569 if ((num_filters = H5Pget_nfilters(propid)) < 0)
1571 for (f = 0; f < num_filters; f++)
1573 if ((filter = H5Pget_filter2(propid, f, NULL, &cd_nelems,
1574 cd_values, 0, NULL, NULL)) < 0)
1578 case H5Z_FILTER_SHUFFLE:
1579 var->shuffle = NC_TRUE;
1582 case H5Z_FILTER_FLETCHER32:
1583 var->fletcher32 = NC_TRUE;
1586 case H5Z_FILTER_DEFLATE:
1587 var->deflate = NC_TRUE;
1588 if (cd_nelems != CD_NELEMS_ZLIB || cd_values[0] > MAX_DEFLATE_LEVEL)
1590 var->deflate_level = cd_values[0];
1593 case H5Z_FILTER_SZIP:
1594 var->szip = NC_TRUE;
1595 if (cd_nelems != CD_NELEMS_SZIP)
1597 var->options_mask = cd_values[0];
1598 var->pixels_per_block = cd_values[1];
1602 LOG((1,
"Yikes! Unknown filter type found on dataset!"));
1608 if ((retval = get_type_info2(grp->nc4_info, datasetid,
1613 var->type_info->rc++;
1616 if (H5Pfill_value_defined(propid, &fill_status) < 0)
1620 if (fill_status == H5D_FILL_VALUE_USER_DEFINED)
1623 if (!var->fill_value)
1625 if (var->type_info->nc_type_class ==
NC_VLEN)
1627 if (!(var->fill_value = malloc(
sizeof(
nc_vlen_t))))
1630 else if (var->type_info->nc_type_class ==
NC_STRING)
1632 if (!(var->fill_value = malloc(
sizeof(
char *))))
1637 assert(var->type_info->size);
1638 if (!(var->fill_value = malloc(var->type_info->size)))
1644 if (H5Pget_fill_value(propid, var->type_info->native_hdf_typeid,
1645 var->fill_value) < 0)
1649 var->no_fill = NC_TRUE;
1655 var->dimscale = NC_TRUE;
1658 if ((retval = read_coord_dimids(var)))
1664 assert(0 == strcmp(var->name, dim->name));
1666 var->dimids[0] = dim->dimid;
1669 dim->coord_var = var;
1681 num_scales = H5DSget_num_scales(datasetid, 0);
1685 if (num_scales && ndims)
1689 if (NULL == (var->dimscale_attached = calloc(ndims,
sizeof(nc_bool_t))))
1694 if (NULL == (var->dimscale_hdf5_objids = malloc(ndims *
sizeof(
struct hdf5_objid))))
1696 for (d = 0; d < var->ndims; d++)
1698 if (H5DSiterate_scales(var->hdf_datasetid, d, NULL, dimscale_visitor,
1699 &(var->dimscale_hdf5_objids[d])) < 0)
1701 var->dimscale_attached[d] = NC_TRUE;
1708 if ((natts = H5Aget_num_attrs(var->hdf_datasetid)) < 0)
1710 for (a = 0; a < natts; a++)
1715 if (attid && H5Aclose(attid) < 0)
1719 if ((attid = H5Aopen_idx(var->hdf_datasetid, (
unsigned int)a)) < 0)
1721 if (H5Aget_name(attid, NC_MAX_HDF5_NAME, att_name) < 0)
1723 LOG((4,
"%s:: a %d att_name %s", __func__, a, att_name));
1726 if (strcmp(att_name, REFERENCE_LIST) &&
1727 strcmp(att_name, CLASS) &&
1728 strcmp(att_name, DIMENSION_LIST) &&
1729 strcmp(att_name, NAME) &&
1730 strcmp(att_name, COORDINATES) &&
1731 strcmp(att_name, NC_DIMID_ATT_NAME))
1734 if ((retval = nc4_att_list_add(&var->att, &att)))
1738 att->attnum = var->natts++;
1739 if (!(att->name = strdup(att_name)))
1744 if ((retval = read_hdf5_att(grp, attid, att)))
1748 if ((retval = nc4_att_list_del(&var->att, att)))
1756 att->created = NC_TRUE;
1762 if ((retval = nc4_adjust_var_cache(grp, var)))
1768 if (incr_id_rc && H5Idec_ref(datasetid) < 0)
1770 if (var && nc4_var_list_del(&grp->var, var))
1773 if (access_pid && H5Pclose(access_pid) < 0)
1778 if (propid > 0 && H5Pclose(propid) < 0)
1783 if (attid > 0 && H5Aclose(attid) < 0)
1791 read_grp_atts(NC_GRP_INFO_T *grp)
1796 NC_TYPE_INFO_T *type;
1797 char obj_name[NC_MAX_HDF5_NAME + 1];
1801 num_obj = H5Aget_num_attrs(grp->hdf_grpid);
1802 for (i = 0; i < num_obj; i++)
1806 if (attid && H5Aclose(attid) < 0)
1809 if ((attid = H5Aopen_idx(grp->hdf_grpid, (
unsigned int)i)) < 0)
1811 if (H5Aget_name(attid,
NC_MAX_NAME + 1, obj_name) < 0)
1813 LOG((3,
"reading attribute of _netCDF group, named %s", obj_name));
1819 if (!strcmp(obj_name, NC3_STRICT_ATT_NAME))
1824 if ((retval = nc4_att_list_add(&grp->att, &att)))
1829 if (!(att->name = malloc((max_len + 1) *
sizeof(
char))))
1831 strncpy(att->name, obj_name, max_len);
1832 att->name[max_len] = 0;
1833 att->attnum = grp->natts++;
1834 if ((retval = read_hdf5_att(grp, attid, att)))
1838 if ((retval = nc4_att_list_del(&grp->att, att)))
1845 att->created = NC_TRUE;
1846 if ((retval = nc4_find_type(grp->nc4_info, att->nc_typeid, &type)))
1852 if (attid > 0 && H5Aclose(attid) < 0)
1860 read_dataset(NC_GRP_INFO_T *grp, hid_t datasetid,
const char *obj_name,
1861 const H5G_stat_t *statbuf)
1863 NC_DIM_INFO_T *dim = NULL;
1870 if ((spaceid = H5Dget_space(datasetid)) < 0)
1875 if ((ndims = H5Sget_simple_extent_ndims(spaceid)) < 0)
1879 if ((is_scale = H5DSis_scale(datasetid)) < 0)
1883 hsize_t dims[H5S_MAX_RANK];
1884 hsize_t max_dims[H5S_MAX_RANK];
1887 if (H5Sget_simple_extent_dims(spaceid, dims, max_dims) < 0)
1891 if ((retval = read_scale(grp, datasetid, obj_name, statbuf, dims[0],
1892 max_dims[0], &dim)))
1899 if (NULL == dim || (dim && !dim->hdf_dimscaleid))
1900 if ((retval = read_var(grp, datasetid, obj_name, ndims, dim)))
1904 if (spaceid && H5Sclose(spaceid) <0)
1921 if (!(new_oinfo = calloc(1,
sizeof(*new_oinfo))))
1925 memcpy(new_oinfo, oinfo,
sizeof(*oinfo));
1930 (*tail)->next = new_oinfo;
1935 assert(NULL == *head);
1936 *head = *tail = new_oinfo;
1943 nc4_rec_read_metadata_cb(hid_t grpid,
const char *name,
const H5L_info_t *info,
1948 int retval = H5_ITER_CONT;
1951 memset(&oinfo, 0,
sizeof(oinfo));
1954 if ((oinfo.oid = H5Oopen(grpid, name, H5P_DEFAULT)) < 0)
1955 BAIL(H5_ITER_ERROR);
1958 if (H5Gget_objinfo(oinfo.oid,
".", 1, &oinfo.statbuf) < 0)
1959 BAIL(H5_ITER_ERROR);
1964 switch(oinfo.statbuf.type)
1967 LOG((3,
"found group %s", oinfo.oname));
1973 if (nc4_rec_read_metadata_cb_list_add(&udata->grps_head, &udata->grps_tail, &oinfo))
1974 BAIL(H5_ITER_ERROR);
1978 LOG((3,
"found dataset %s", oinfo.oname));
1982 if ((retval = read_dataset(udata->grp, oinfo.oid, oinfo.oname, &oinfo.statbuf)))
1990 BAIL(H5_ITER_ERROR);
1992 retval = H5_ITER_CONT;
1996 if (H5Oclose(oinfo.oid) < 0)
1997 BAIL(H5_ITER_ERROR);
2001 LOG((3,
"found datatype %s", oinfo.oname));
2004 if (read_type(udata->grp, oinfo.oid, oinfo.oname))
2005 BAIL(H5_ITER_ERROR);
2008 if (H5Oclose(oinfo.oid) < 0)
2009 BAIL(H5_ITER_ERROR);
2013 LOG((0,
"Unknown object class %d in %s!", oinfo.statbuf.type, __func__));
2014 BAIL(H5_ITER_ERROR);
2020 if (oinfo.oid > 0 && H5Oclose(oinfo.oid) < 0)
2021 BAIL2(H5_ITER_ERROR);
2035 nc4_rec_read_metadata(NC_GRP_INFO_T *grp)
2041 unsigned crt_order_flags = 0;
2042 H5_index_t iter_index;
2045 assert(grp && grp->name);
2046 LOG((3,
"%s: grp->name %s", __func__, grp->name));
2049 memset(&udata, 0,
sizeof(udata));
2053 if (!grp->hdf_grpid)
2057 if ((grp->hdf_grpid = H5Gopen2(grp->parent->hdf_grpid,
2058 grp->name, H5P_DEFAULT)) < 0)
2063 if ((grp->hdf_grpid = H5Gopen2(grp->nc4_info->hdfid,
2064 "/", H5P_DEFAULT)) < 0)
2068 assert(grp->hdf_grpid > 0);
2071 pid = H5Gget_create_plist(grp->hdf_grpid);
2072 H5Pget_link_creation_order(pid, &crt_order_flags);
2073 if (H5Pclose(pid) < 0)
2077 if (crt_order_flags & H5P_CRT_ORDER_TRACKED)
2078 iter_index = H5_INDEX_CRT_ORDER;
2081 NC_HDF5_FILE_INFO_T *h5 = grp->nc4_info;
2087 iter_index = H5_INDEX_NAME;
2096 if (H5Literate(grp->hdf_grpid, iter_index, H5_ITER_INC, &idx,
2097 nc4_rec_read_metadata_cb, (
void *)&udata) < 0)
2104 for (oinfo = udata.grps_head; oinfo; oinfo = udata.grps_head)
2106 NC_GRP_INFO_T *child_grp;
2107 NC_HDF5_FILE_INFO_T *h5 = grp->nc4_info;
2110 if ((retval = nc4_grp_list_add(&(grp->children), h5->next_nc_grpid++,
2111 grp, grp->nc4_info->controller, oinfo->oname, &child_grp)))
2115 if ((retval = nc4_rec_read_metadata(child_grp)))
2119 if (H5Oclose(oinfo->oid) < 0)
2123 udata.grps_head = oinfo->next;
2128 if ((retval = read_grp_atts(grp)))
2135 for (oinfo = udata.grps_head; oinfo; oinfo = udata.grps_head)
2138 if (H5Oclose(oinfo->oid) < 0)
2142 udata.grps_head = oinfo->next;
2154 nc4_open_file(
const char *path,
int mode, MPI_Comm comm,
2155 MPI_Info info, NC *nc)
2157 hid_t fapl_id = H5P_DEFAULT;
2158 unsigned flags = (mode &
NC_WRITE) ?
2159 H5F_ACC_RDWR : H5F_ACC_RDONLY;
2161 NC_HDF5_FILE_INFO_T* nc4_info = NULL;
2167 LOG((3,
"%s: path %s mode %d", __func__, path, mode));
2170 if(mode & NC_DISKLESS)
2174 if ((retval = nc4_nc4f_list_add(nc, path, mode)))
2176 nc4_info = NC4_DATA(nc);
2177 assert(nc4_info && nc4_info->root_grp);
2182 if ((fapl_id = H5Pcreate(H5P_FILE_ACCESS)) < 0)
2188 if (H5Pset_fclose_degree(fapl_id, H5F_CLOSE_SEMI))
2191 if (H5Pset_fclose_degree(fapl_id, H5F_CLOSE_STRONG))
2198 if (mode & NC_MPIIO || mode & NC_MPIPOSIX)
2200 nc4_info->parallel = NC_TRUE;
2201 if (mode & NC_MPIIO)
2203 LOG((4,
"opening parallel file with MPI/IO"));
2204 if (H5Pset_fapl_mpio(fapl_id, comm, info) < 0)
2207 #ifdef USE_PARALLEL_POSIX
2210 LOG((4,
"opening parallel file with MPI/posix"));
2211 if (H5Pset_fapl_mpiposix(fapl_id, comm, 0) < 0)
2224 if (MPI_SUCCESS != MPI_Comm_dup(comm, &nc4_info->comm))
2227 if (MPI_INFO_NULL != info)
2229 if (MPI_SUCCESS != MPI_Info_dup(info, &nc4_info->info))
2236 nc4_info->info = info;
2240 if (H5Pset_cache(fapl_id, 0, nc4_chunk_cache_nelems, nc4_chunk_cache_size,
2241 nc4_chunk_cache_preemption) < 0)
2243 LOG((4,
"%s: set HDF raw chunk cache to size %d nelems %d preemption %f",
2244 __func__, nc4_chunk_cache_size, nc4_chunk_cache_nelems, nc4_chunk_cache_preemption));
2250 if ((nc4_info->hdfid = H5Fopen(path, flags, fapl_id)) < 0)
2255 nc4_info->no_write = NC_TRUE;
2261 if ((retval = nc4_rec_read_metadata(nc4_info->root_grp)))
2266 if ((retval = nc4_rec_match_dimscales(nc4_info->root_grp)))
2272 log_metadata_nc(nc);
2276 if (H5Pclose(fapl_id) < 0)
2286 if (comm_duped) MPI_Comm_free(&nc4_info->comm);
2287 if (info_duped) MPI_Info_free(&nc4_info->info);
2292 if (fapl_id != H5P_DEFAULT) H5Pclose(fapl_id);
2293 if (!nc4_info)
return retval;
2294 close_netcdf4_file(nc4_info,1);
2301 get_netcdf_type_from_hdf4(NC_HDF5_FILE_INFO_T *h5, int32 hdf4_typeid,
2302 nc_type *xtype, NC_TYPE_INFO_T *type_info)
2305 assert(h5 && xtype);
2353 if (hdf4_typeid == DFNT_FLOAT32)
2354 type_info->nc_type_class =
NC_FLOAT;
2355 else if (hdf4_typeid == DFNT_FLOAT64)
2357 else if (hdf4_typeid == DFNT_CHAR)
2360 type_info->nc_type_class =
NC_INT;
2362 type_info->nc_typeid = *xtype;
2363 type_info->size = nc_type_size_g[t];
2364 if (!(type_info->name = strdup(nc_type_name_g[t])))
2374 nc4_open_hdf4_file(
const char *path,
int mode, NC *nc)
2376 NC_HDF5_FILE_INFO_T *h5;
2379 int32 num_datasets, num_gatts;
2383 NC_HDF5_FILE_INFO_T* nc4_info = NULL;
2385 LOG((3,
"%s: path %s mode %d", __func__, path, mode));
2389 if (mode & NC_WRITE)
2393 if ((retval = nc4_nc4f_list_add(nc, path, mode)))
2395 nc4_info = NC4_DATA(nc);
2396 assert(nc4_info && nc4_info->root_grp);
2400 h5->no_write = NC_TRUE;
2403 if ((h5->sdid = SDstart(path, DFACC_READ)) == FAIL)
2407 if (SDfileinfo(h5->sdid, &num_datasets, &num_gatts))
2411 for (a = 0; a < num_gatts; a++)
2413 int32 att_data_type, att_count;
2414 size_t att_type_size;
2417 if ((retval = nc4_att_list_add(&h5->root_grp->att, &att)))
2419 att->attnum = grp->natts++;
2420 att->created = NC_TRUE;
2425 if (SDattrinfo(h5->sdid, a, att->name, &att_data_type, &att_count))
2427 if ((retval = get_netcdf_type_from_hdf4(h5, att_data_type,
2428 &att->nc_typeid, NULL)))
2430 att->len = att_count;
2433 if ((retval = nc4_get_typelen_mem(h5, att->nc_typeid, 0, &att_type_size)))
2435 if (!(att->data = malloc(att_type_size * att->len)))
2439 if (SDreadattr(h5->sdid, a, att->data))
2444 for (v = 0; v < num_datasets; v++)
2447 int32 data_type, num_atts;
2456 int32 *dimsize = NULL;
2457 size_t var_type_size;
2461 if ((retval = nc4_var_list_add(&grp->var, &var)))
2464 var->varid = grp->nvars++;
2465 var->created = NC_TRUE;
2466 var->written_to = NC_TRUE;
2469 if ((var->sdsid = SDselect(h5->sdid, v)) == FAIL)
2477 if (SDgetinfo(var->sdsid, var->name, &rank, NULL, &data_type, &num_atts))
2480 if(!(dimsize = (int32*)malloc(
sizeof(int32)*rank)))
2483 if (SDgetinfo(var->sdsid, var->name, &rank, dimsize, &data_type, &num_atts)) {
2484 if(dimsize) free(dimsize);
2489 var->hdf4_data_type = data_type;
2492 if (!(var->type_info = calloc(1,
sizeof(NC_TYPE_INFO_T)))) {
2493 if(dimsize) free(dimsize);
2497 if ((retval = get_netcdf_type_from_hdf4(h5, data_type, &var->type_info->nc_typeid, var->type_info))) {
2498 if(dimsize) free(dimsize);
2503 var->type_info->rc++;
2505 if ((retval = nc4_get_typelen_mem(h5, var->type_info->nc_typeid, 0, &var_type_size))) {
2506 if(dimsize) free(dimsize);
2510 var->type_info->size = var_type_size;
2511 LOG((3,
"reading HDF4 dataset %s, rank %d netCDF type %d", var->name,
2512 rank, var->type_info->nc_typeid));
2515 if (!(var->fill_value = malloc(var_type_size))) {
2516 if(dimsize) free(dimsize);
2520 if (SDgetfillvalue(var->sdsid, var->fill_value))
2523 free(var->fill_value);
2524 var->fill_value = NULL;
2530 if (!(var->dim = malloc(
sizeof(NC_DIM_INFO_T *) * var->ndims))) {
2531 if(dimsize) free(dimsize);
2535 if (!(var->dimids = malloc(
sizeof(
int) * var->ndims))) {
2536 if(dimsize) free(dimsize);
2543 for (d = 0; d < var->ndims; d++)
2545 int32 dimid, dim_len, dim_data_type, dim_num_attrs;
2549 if ((dimid = SDgetdimid(var->sdsid, d)) == FAIL) {
2550 if(dimsize) free(dimsize);
2553 if (SDdiminfo(dimid, dim_name, &dim_len, &dim_data_type,
2556 if(dimsize) free(dimsize);
2562 for (dim = grp->dim; dim; dim = dim->l.next)
2563 if (!strcmp(dim->name, dim_name))
2569 LOG((4,
"adding dimension %s for HDF4 dataset %s",
2570 dim_name, var->name));
2571 if ((retval = nc4_dim_list_add(&grp->dim, &dim)))
2574 dim->dimid = grp->nc4_info->next_dimid++;
2577 if (!(dim->name = strdup(dim_name)))
2582 dim->len = *dimsize;
2586 var->dimids[d] = dim->dimid;
2590 for (a = 0; a < num_atts; a++)
2592 int32 att_data_type, att_count;
2593 size_t att_type_size;
2596 if ((retval = nc4_att_list_add(&var->att, &att))) {
2597 if(dimsize) free(dimsize);
2600 att->attnum = var->natts++;
2601 att->created = NC_TRUE;
2605 if(dimsize) free(dimsize);
2608 if (SDattrinfo(var->sdsid, a, att->name, &att_data_type, &att_count)) {
2609 if(dimsize) free(dimsize);
2612 if ((retval = get_netcdf_type_from_hdf4(h5, att_data_type,
2613 &att->nc_typeid, NULL))) {
2614 if(dimsize) free(dimsize);
2618 att->len = att_count;
2621 if ((retval = nc4_get_typelen_mem(h5, att->nc_typeid, 0, &att_type_size))) {
2622 if(dimsize) free(dimsize);
2625 if (!(att->data = malloc(att_type_size * att->len))) {
2626 if(dimsize) free(dimsize);
2631 if (SDreadattr(var->sdsid, a, att->data)) {
2632 if(dimsize) free(dimsize);
2636 if(dimsize) free(dimsize);
2640 HDF_CHUNK_DEF chunkdefs;
2642 if(!SDgetchunkinfo(var->sdsid, &chunkdefs, &flag)) {
2643 if(flag == HDF_NONE)
2644 var->contiguous = NC_TRUE;
2645 else if((flag & HDF_CHUNK) != 0) {
2646 var->contiguous = NC_FALSE;
2647 if (!(var->chunksizes = malloc(var->ndims *
sizeof(
size_t))))
2649 for (d = 0; d < var->ndims; d++) {
2650 var->chunksizes[d] = chunkdefs.chunk_lengths[d];
2661 log_metadata_nc(h5->root_grp->nc4_info->controller);
2669 NC4_open(
const char *path,
int mode,
int basepe,
size_t *chunksizehintp,
2670 int use_parallel,
void *mpidata, NC_Dispatch *dispatch, NC *nc_file)
2673 MPI_Comm comm = MPI_COMM_WORLD;
2674 MPI_Info info = MPI_INFO_NULL;
2677 assert(nc_file && path);
2679 LOG((1,
"%s: path %s mode %d comm %d info %d",
2680 __func__, path, mode, comm, info));
2685 comm = ((NC_MPI_INFO *)mpidata)->comm;
2686 info = ((NC_MPI_INFO *)mpidata)->info;
2693 if (H5Eset_auto(NULL, NULL) < 0)
2694 LOG((0,
"Couldn't turn off HDF5 error messages!"));
2695 LOG((1,
"HDF5 error messages turned off!"));
2702 if (mode & ~(NC_WRITE |
NC_SHARE | NC_MPIIO | NC_MPIPOSIX |
2704 (mode & NC_MPIIO && mode & NC_MPIPOSIX))
2707 #ifndef USE_PARALLEL_POSIX
2711 if(mode & NC_MPIPOSIX)
2713 mode &= ~NC_MPIPOSIX;
2722 if(mode & NC_PNETCDF) {
2724 int pnetcdf_nvars, i;
2725 NC_HDF5_FILE_INFO_T* nc4_info;
2728 res = nc4_nc4f_list_add(nc_file, path, mode);
2730 nc4_info = NC4_DATA(nc_file);
2733 res = ncmpi_open(comm, path, mode, info, &(nc_file->int_ncid));
2734 nc4_info->pnetcdf_file++;
2738 res = ncmpi_begin_indep_data(nc_file->int_ncid);
2744 res = ncmpi_inq_nvars(nc_file->int_ncid, &pnetcdf_nvars);
2745 for (i = 0; i < pnetcdf_nvars; i++)
2746 res = ncmpi_inq_varndims(nc_file->int_ncid, i,
2747 &(nc4_info->pnetcdf_ndims[i]));
2754 if ((res = nc_check_for_hdf(path, use_parallel, comm, info, &hdf_file)))
2757 if (hdf_file == NC_HDF5_FILE)
2759 nc_file->int_ncid = nc_file->ext_ncid;
2760 res = nc4_open_file(path, mode, comm, info, nc_file);
2763 else if (hdf_file == NC_HDF4_FILE)
2765 nc_file->int_ncid = nc_file->ext_ncid;
2766 res = nc4_open_hdf4_file(path, mode, nc_file);
2785 NC4_set_fill(
int ncid,
int fillmode,
int *old_modep)
2788 NC_HDF5_FILE_INFO_T* nc4_info;
2790 LOG((2,
"%s: ncid 0x%x fillmode %d", __func__, ncid, fillmode));
2792 if (!(nc = nc4_find_nc_file(ncid,&nc4_info)))
2797 if (nc4_info->no_write)
2806 *old_modep = nc4_info->fill_mode;
2808 nc4_info->fill_mode = fillmode;
2812 if (nc4_info->pnetcdf_file)
2813 return ncmpi_set_fill(nc->int_ncid, fillmode, old_modep);
2825 NC_HDF5_FILE_INFO_T* nc4_info;
2827 LOG((1,
"%s: ncid 0x%x", __func__, ncid));
2830 if (!(nc4_find_nc_file(ncid,&nc4_info)))
2836 if (nc4_info->pnetcdf_file)
2837 return ncmpi_redef(nc->int_ncid);
2841 if (nc4_info->flags & NC_INDEF)
2845 if (nc4_info->no_write)
2849 nc4_info->flags |= NC_INDEF;
2853 nc4_info->redef = NC_TRUE;
2861 NC4__enddef(
int ncid,
size_t h_minfree,
size_t v_align,
2862 size_t v_minfree,
size_t r_align)
2864 if (nc4_find_nc_file(ncid,NULL) == NULL)
2867 return NC4_enddef(ncid);
2872 static int NC4_enddef(
int ncid)
2875 NC_HDF5_FILE_INFO_T* nc4_info;
2877 LOG((1,
"%s: ncid 0x%x", __func__, ncid));
2879 if (!(nc = nc4_find_nc_file(ncid,&nc4_info)))
2884 if (nc4_info->pnetcdf_file)
2887 res = ncmpi_enddef(nc->int_ncid);
2890 if (nc4_info->pnetcdf_access_mode == NC_INDEPENDENT)
2891 res = ncmpi_begin_indep_data(nc->int_ncid);
2897 return nc4_enddef_netcdf4_file(nc4_info);
2903 sync_netcdf4_file(NC_HDF5_FILE_INFO_T *h5)
2908 LOG((3,
"%s", __func__));
2912 if (h5->flags & NC_INDEF)
2918 h5->flags ^= NC_INDEF;
2921 h5->redef = NC_FALSE;
2927 log_metadata_nc(h5->root_grp->nc4_info->controller);
2933 nc_bool_t bad_coord_order = NC_FALSE;
2935 if ((retval = nc4_rec_write_groups_types(h5->root_grp)))
2937 if ((retval = nc4_rec_detect_need_to_preserve_dimids(h5->root_grp, &bad_coord_order)))
2939 if ((retval = nc4_rec_write_metadata(h5->root_grp, bad_coord_order)))
2943 if (H5Fflush(h5->hdfid, H5F_SCOPE_GLOBAL) < 0)
2956 NC_HDF5_FILE_INFO_T* nc4_info;
2958 LOG((2,
"%s: ncid 0x%x", __func__, ncid));
2960 if (!(nc = nc4_find_nc_file(ncid,&nc4_info)))
2966 if (nc4_info->pnetcdf_file)
2967 return ncmpi_sync(nc->int_ncid);
2971 if (nc4_info && nc4_info->flags & NC_INDEF)
2975 if ((retval = NC4_enddef(ncid)))
2979 return sync_netcdf4_file(nc4_info);
2986 close_netcdf4_file(NC_HDF5_FILE_INFO_T *h5,
int abort)
2990 assert(h5 && h5->root_grp);
2991 LOG((3,
"%s: h5->path %s abort %d", __func__, h5->controller->path, abort));
2994 if (h5->flags & NC_INDEF)
2995 h5->flags ^= NC_INDEF;
2999 if (!h5->no_write && !abort)
3000 if ((retval = sync_netcdf4_file(h5)))
3005 if ((retval = nc4_rec_grp_del(&h5->root_grp, h5->root_grp)))
3012 if (SDend(h5->sdid))
3022 MPI_Comm_free(&h5->comm);
3023 if(MPI_INFO_NULL != h5->info)
3024 MPI_Info_free(&h5->info);
3027 if (H5Fclose(h5->hdfid) < 0)
3031 nobjs = H5Fget_obj_count(h5->hdfid, H5F_OBJ_ALL);
3035 }
else if(nobjs > 0) {
3041 LOG((0,
"There are %d HDF5 objects open!", nobjs));
3067 int delete_file = 0;
3070 NC_HDF5_FILE_INFO_T* nc4_info;
3072 LOG((2,
"%s: ncid 0x%x", __func__, ncid));
3075 if (!(nc = nc4_find_nc_file(ncid,&nc4_info)))
3082 if (nc4_info->pnetcdf_file)
3083 return ncmpi_abort(nc->int_ncid);
3087 if (nc4_info->flags & NC_INDEF && !nc4_info->redef)
3095 if ((retval = close_netcdf4_file(nc4_info, 1)))
3100 if (
remove(path) < 0)
3112 NC_HDF5_FILE_INFO_T *h5;
3115 LOG((1,
"%s: ncid 0x%x", __func__, ncid));
3118 if ((retval = nc4_find_nc_grp_h5(ncid, &nc, &grp, &h5)))
3121 assert(nc && h5 && grp);
3129 if (h5->pnetcdf_file)
3130 return ncmpi_close(nc->int_ncid);
3134 if ((retval = close_netcdf4_file(grp->nc4_info, 0)))
3143 NC4_inq(
int ncid,
int *ndimsp,
int *nvarsp,
int *nattsp,
int *unlimdimidp)
3146 NC_HDF5_FILE_INFO_T *h5;
3153 LOG((2,
"%s: ncid 0x%x", __func__, ncid));
3156 if ((retval = nc4_find_nc_grp_h5(ncid, &nc, &grp, &h5)))
3159 assert(h5 && grp && nc);
3163 if (h5->pnetcdf_file)
3164 return ncmpi_inq(nc->int_ncid, ndimsp, nvarsp, nattsp, unlimdimidp);
3171 for (dim = grp->dim; dim; dim = dim->l.next)
3177 for (var = grp->var; var; var= var->l.next)
3183 for (att = grp->att; att; att = att->l.next)
3196 for (dim = grp->dim; dim; dim = dim->l.next)
3199 *unlimdimidp = dim->dimid;
3210 nc4_enddef_netcdf4_file(NC_HDF5_FILE_INFO_T *h5)
3213 LOG((3,
"%s", __func__));
3216 if (!(h5->flags & NC_INDEF))
3220 h5->flags ^= NC_INDEF;
3223 h5->redef = NC_FALSE;
3225 return sync_netcdf4_file(h5);
3232 if (num_plists || num_spaces)
3241 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.
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.
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.
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_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.