17 #include "nc4internal.h"
18 #include "nc4dispatch.h"
21 #include <H5DSpublic.h>
34 extern int num_plists;
35 extern int num_spaces;
38 #define MIN_DEFLATE_LEVEL 0
39 #define MAX_DEFLATE_LEVEL 9
43 #define REFERENCE_LIST "REFERENCE_LIST"
45 #define DIMENSION_LIST "DIMENSION_LIST"
49 #define ILLEGAL_OPEN_FLAGS (NC_MMAP|NC_64BIT_OFFSET)
51 #define ILLEGAL_CREATE_FLAGS (NC_NOWRITE|NC_MMAP|NC_INMEMORY|NC_64BIT_OFFSET|NC_PNETCDF)
79 static int NC4_enddef(
int ncid);
80 static int nc4_rec_read_metadata(NC_GRP_INFO_T *grp);
81 static int close_netcdf4_file(NC_HDF5_FILE_INFO_T *h5,
int abort);
85 size_t nc4_chunk_cache_size = CHUNK_CACHE_SIZE;
86 size_t nc4_chunk_cache_nelems = CHUNK_CACHE_NELEMS;
87 float nc4_chunk_cache_preemption = CHUNK_CACHE_PREEMPTION;
91 static int virgin = 1;
96 static hid_t h5_native_type_constant_g[NUM_TYPES];
97 static const char nc_type_name_g[NUM_TYPES][
NC_MAX_NAME + 1] = {
"char",
"byte",
"short",
98 "int",
"float",
"double",
"ubyte",
99 "ushort",
"uint",
"int64",
105 static const int nc_type_size_g[NUM_TYPES] = {
sizeof(char),
sizeof(
char),
sizeof(short),
106 sizeof(
int),
sizeof(float),
sizeof(
double),
sizeof(
unsigned char),
107 sizeof(
unsigned short),
sizeof(
unsigned int),
sizeof(
long long),
108 sizeof(
unsigned long long),
sizeof(
char *)};
113 nc_set_chunk_cache(
size_t size,
size_t nelems,
float preemption)
115 if (preemption < 0 || preemption > 1)
117 nc4_chunk_cache_size = size;
118 nc4_chunk_cache_nelems = nelems;
119 nc4_chunk_cache_preemption = preemption;
126 nc_get_chunk_cache(
size_t *sizep,
size_t *nelemsp,
float *preemptionp)
129 *sizep = nc4_chunk_cache_size;
132 *nelemsp = nc4_chunk_cache_nelems;
135 *preemptionp = nc4_chunk_cache_preemption;
141 nc_set_chunk_cache_ints(
int size,
int nelems,
int preemption)
143 if (size <= 0 || nelems <= 0 || preemption < 0 || preemption > 100)
145 nc4_chunk_cache_size = size;
146 nc4_chunk_cache_nelems = nelems;
147 nc4_chunk_cache_preemption = (float)preemption / 100;
152 nc_get_chunk_cache_ints(
int *sizep,
int *nelemsp,
int *preemptionp)
155 *sizep = (int)nc4_chunk_cache_size;
157 *nelemsp = (int)nc4_chunk_cache_nelems;
159 *preemptionp = (int)(nc4_chunk_cache_preemption * 100);
189 #define MAGIC_NUMBER_LEN 4
190 #define NC_HDF5_FILE 1
191 #define NC_HDF4_FILE 2
193 nc_check_for_hdf(
const char *path,
int flags,
void* parameters,
int *hdf_file)
195 char blob[MAGIC_NUMBER_LEN];
198 NC_MPI_INFO* mpiinfo = (NC_MPI_INFO*)parameters;
199 MPI_Comm comm = MPI_COMM_WORLD;
200 MPI_Info info = MPI_INFO_NULL;
204 NC_MEM_INFO* meminfo = (NC_MEM_INFO*)parameters;
209 comm = mpiinfo->comm;
210 info = mpiinfo->info;
215 LOG((3,
"%s: path %s", __func__, path));
218 if(!inmemory && H5Fis_hdf5(path))
220 *hdf_file = NC_HDF5_FILE;
226 if (!inmemory && use_parallel)
231 if ((retval = MPI_File_open(comm, (
char *)path, MPI_MODE_RDONLY,
232 info, &fh)) != MPI_SUCCESS)
234 if ((retval = MPI_File_read(fh, blob, MAGIC_NUMBER_LEN, MPI_CHAR,
235 &status)) != MPI_SUCCESS)
237 if ((retval = MPI_File_close(&fh)) != MPI_SUCCESS)
244 if (!(fp = fopen(path,
"r")) ||
245 fread(blob, MAGIC_NUMBER_LEN, 1, fp) != 1) {
252 if(meminfo->size < MAGIC_NUMBER_LEN)
254 memcpy(blob,meminfo->memory,MAGIC_NUMBER_LEN);
258 if (memcmp(blob,
"\016\003\023\001", 4)==0)
259 *hdf_file = NC_HDF4_FILE;
260 else if (memcmp(blob,
"HDF", 3)==0)
261 *hdf_file = NC_HDF5_FILE;
271 nc4_create_file(
const char *path,
int cmode, MPI_Comm comm, MPI_Info info,
274 hid_t fcpl_id, fapl_id = -1;
278 NC_HDF5_FILE_INFO_T* nc4_info = NULL;
289 flags = H5F_ACC_TRUNC;
291 flags = H5F_ACC_EXCL;
293 flags = H5F_ACC_TRUNC;
295 LOG((3,
"%s: path %s mode 0x%x", __func__, path, cmode));
300 if (cmode & NC_DISKLESS) {
305 }
else if ((cmode & NC_NOCLOBBER) && (fp = fopen(path,
"r"))) {
311 if ((retval = nc4_nc4f_list_add(nc, path, (
NC_WRITE | cmode))))
313 nc4_info = NC4_DATA(nc);
314 assert(nc4_info && nc4_info->root_grp);
324 if ((fapl_id = H5Pcreate(H5P_FILE_ACCESS)) < 0)
330 if (H5Pset_fclose_degree(fapl_id, H5F_CLOSE_SEMI))
333 if (H5Pset_fclose_degree(fapl_id, H5F_CLOSE_STRONG))
342 nc4_info->parallel = NC_TRUE;
343 if (cmode & NC_MPIIO)
345 LOG((4,
"creating parallel file with MPI/IO"));
346 if (H5Pset_fapl_mpio(fapl_id, comm, info) < 0)
349 #ifdef USE_PARALLEL_POSIX
352 LOG((4,
"creating parallel file with MPI/posix"));
353 if (H5Pset_fapl_mpiposix(fapl_id, comm, 0) < 0)
366 if (MPI_SUCCESS != MPI_Comm_dup(comm, &nc4_info->comm))
369 if (MPI_INFO_NULL != info)
371 if (MPI_SUCCESS != MPI_Info_dup(info, &nc4_info->info))
378 nc4_info->info = info;
382 if(cmode & NC_DISKLESS) {
383 if (H5Pset_fapl_core(fapl_id, 4096, persist))
386 if (H5Pset_cache(fapl_id, 0, nc4_chunk_cache_nelems, nc4_chunk_cache_size,
387 nc4_chunk_cache_preemption) < 0)
389 LOG((4,
"%s: set HDF raw chunk cache to size %d nelems %d preemption %f",
390 __func__, nc4_chunk_cache_size, nc4_chunk_cache_nelems, nc4_chunk_cache_preemption));
393 if (H5Pset_libver_bounds(fapl_id, H5F_LIBVER_LATEST, H5F_LIBVER_LATEST) < 0)
397 if ((fcpl_id = H5Pcreate(H5P_FILE_CREATE)) < 0)
404 if (H5Pset_obj_track_times(fcpl_id,0)<0)
410 if (H5Pset_link_creation_order(fcpl_id, (H5P_CRT_ORDER_TRACKED |
411 H5P_CRT_ORDER_INDEXED)) < 0)
413 if (H5Pset_attr_creation_order(fcpl_id, (H5P_CRT_ORDER_TRACKED |
414 H5P_CRT_ORDER_INDEXED)) < 0)
418 if ((nc4_info->hdfid = H5Fcreate(path, flags, fcpl_id, fapl_id)) < 0)
424 if ((nc4_info->root_grp->hdf_grpid = H5Gopen2(nc4_info->hdfid,
"/",
429 if (H5Pclose(fapl_id) < 0 ||
430 H5Pclose(fcpl_id) < 0)
438 nc4_info->flags |= NC_INDEF;
444 if (comm_duped) MPI_Comm_free(&nc4_info->comm);
445 if (info_duped) MPI_Info_free(&nc4_info->info);
450 if (fapl_id != H5P_DEFAULT) H5Pclose(fapl_id);
451 if(!nc4_info)
return retval;
452 close_netcdf4_file(nc4_info,1);
472 NC4_create(
const char* path,
int cmode,
size_t initialsz,
int basepe,
473 size_t *chunksizehintp,
int use_parallel,
void *parameters,
474 NC_Dispatch *dispatch, NC* nc_file)
476 MPI_Comm comm = MPI_COMM_WORLD;
477 MPI_Info info = MPI_INFO_NULL;
480 assert(nc_file && path);
482 LOG((1,
"%s: path %s cmode 0x%x comm %d info %d",
483 __func__, path, cmode, comm, info));
488 comm = ((NC_MPI_INFO *)parameters)->comm;
489 info = ((NC_MPI_INFO *)parameters)->info;
496 if (H5Eset_auto(NULL, NULL) < 0)
497 LOG((0,
"Couldn't turn off HDF5 error messages!"));
498 LOG((1,
"HDF5 error messages have been turned off."));
503 if((cmode & ILLEGAL_CREATE_FLAGS) != 0)
507 if((cmode & (NC_MPIIO|NC_MPIPOSIX)) == (NC_MPIIO|NC_MPIPOSIX))
511 if((cmode & (NC_MPIIO | NC_MPIPOSIX)) && (cmode & NC_DISKLESS))
514 #ifndef USE_PARALLEL_POSIX
518 if(cmode & NC_MPIPOSIX)
520 cmode &= ~NC_MPIPOSIX;
533 LOG((2,
"cmode after applying default format: 0x%x", cmode));
535 nc_file->int_ncid = nc_file->ext_ncid;
536 res = nc4_create_file(path, cmode, comm, info, nc_file);
539 if (cmode & NC_PNETCDF)
541 NC_HDF5_FILE_INFO_T* nc4_info;
542 nc4_info = NC4_DATA(nc_file);
545 nc4_info->pnetcdf_file++;
546 res = ncmpi_create(comm, path, cmode, info, &(nc_file->int_ncid));
559 read_scale(NC_GRP_INFO_T *grp, hid_t datasetid,
const char *obj_name,
560 const H5G_stat_t *statbuf, hsize_t scale_size, hsize_t max_scale_size,
563 NC_DIM_INFO_T *new_dim;
565 htri_t attr_exists = -1;
567 int dimscale_created = 0;
568 int initial_grp_ndims = grp->ndims;
569 short initial_next_dimid = grp->nc4_info->next_dimid;
573 if ((retval = nc4_dim_list_add(&grp->dim, &new_dim)))
579 if ((attr_exists = H5Aexists(datasetid, NC_DIMID_ATT_NAME)) < 0)
583 if ((attid = H5Aopen_by_name(datasetid,
".", NC_DIMID_ATT_NAME,
584 H5P_DEFAULT, H5P_DEFAULT)) < 0)
587 if (H5Aread(attid, H5T_NATIVE_INT, &new_dim->dimid) < 0)
591 if (new_dim->dimid >= grp->nc4_info->next_dimid)
592 grp->nc4_info->next_dimid = new_dim->dimid + 1;
597 new_dim->dimid = grp->nc4_info->next_dimid++;
603 if (!(new_dim->name = strdup(obj_name)))
608 new_dim->too_long = NC_TRUE;
611 new_dim->len = scale_size;
612 new_dim->hdf5_objid.fileno[0] = statbuf->fileno[0];
613 new_dim->hdf5_objid.fileno[1] = statbuf->fileno[1];
614 new_dim->hdf5_objid.objno[0] = statbuf->objno[0];
615 new_dim->hdf5_objid.objno[1] = statbuf->objno[1];
619 if (max_scale_size == H5S_UNLIMITED)
620 new_dim->unlimited = NC_TRUE;
625 if (H5DSget_scale_name(datasetid, dimscale_name_att,
NC_MAX_NAME) >= 0)
627 if (!strncmp(dimscale_name_att, DIM_WITHOUT_VARIABLE,
628 strlen(DIM_WITHOUT_VARIABLE)))
630 if (new_dim->unlimited)
632 size_t len = 0, *lenp = &len;
634 if ((retval = nc4_find_dim_len(grp, new_dim->dimid, &lenp)))
636 new_dim->len = *lenp;
640 new_dim->hdf_dimscaleid = datasetid;
641 H5Iinc_ref(new_dim->hdf_dimscaleid);
650 if (attid > 0 && H5Aclose(attid) < 0)
654 if (retval < 0 && dimscale_created)
657 if ((retval = nc4_dim_list_del(&grp->dim, new_dim)))
661 grp->ndims = initial_grp_ndims;
662 grp->nc4_info->next_dimid = initial_next_dimid;
671 read_coord_dimids(NC_VAR_INFO_T *var)
673 hid_t coord_att_typeid = -1, coord_attid = -1, spaceid = -1;
680 if ((coord_attid = H5Aopen_name(var->hdf_datasetid, COORDINATES)) < 0) ret++;
681 if (!ret && (coord_att_typeid = H5Aget_type(coord_attid)) < 0) ret++;
684 if (!ret && (spaceid = H5Aget_space(coord_attid)) < 0) ret++;
688 if (!ret && (npoints = H5Sget_simple_extent_npoints(spaceid)) < 0) ret++;
692 if (!ret && npoints != var->ndims) ret++;
694 if (!ret && H5Aread(coord_attid, coord_att_typeid, var->dimids) < 0) ret++;
695 LOG((4,
"dimscale %s is multidimensional and has coords", var->name));
698 if (spaceid >= 0 && H5Sclose(spaceid) < 0) ret++;
702 if (coord_att_typeid >= 0 && H5Tclose(coord_att_typeid) < 0) ret++;
703 if (coord_attid >= 0 && H5Aclose(coord_attid) < 0) ret++;
710 dimscale_visitor(hid_t did,
unsigned dim, hid_t dsid,
711 void *dimscale_hdf5_objids)
716 if (H5Gget_objinfo(dsid,
".", 1, &statbuf) < 0)
720 (*(HDF5_OBJID_T *)dimscale_hdf5_objids).fileno[0] = statbuf.fileno[0];
721 (*(HDF5_OBJID_T *)dimscale_hdf5_objids).fileno[1] = statbuf.fileno[1];
722 (*(HDF5_OBJID_T *)dimscale_hdf5_objids).objno[0] = statbuf.objno[0];
723 (*(HDF5_OBJID_T *)dimscale_hdf5_objids).objno[1] = statbuf.objno[1];
729 get_netcdf_type(NC_HDF5_FILE_INFO_T *h5, hid_t native_typeid,
732 NC_TYPE_INFO_T *type;
734 htri_t is_str, equal = 0;
738 if ((
class = H5Tget_class(native_typeid)) < 0)
743 if (
class == H5T_STRING)
745 if ((is_str = H5Tis_variable_str(native_typeid)) < 0)
753 else if (
class == H5T_INTEGER ||
class == H5T_FLOAT)
757 if ((equal = H5Tequal(native_typeid, H5T_NATIVE_SCHAR)) < 0)
764 if ((equal = H5Tequal(native_typeid, H5T_NATIVE_SHORT)) < 0)
771 if ((equal = H5Tequal(native_typeid, H5T_NATIVE_INT)) < 0)
778 if ((equal = H5Tequal(native_typeid, H5T_NATIVE_FLOAT)) < 0)
785 if ((equal = H5Tequal(native_typeid, H5T_NATIVE_DOUBLE)) < 0)
792 if ((equal = H5Tequal(native_typeid, H5T_NATIVE_UCHAR)) < 0)
799 if ((equal = H5Tequal(native_typeid, H5T_NATIVE_USHORT)) < 0)
806 if ((equal = H5Tequal(native_typeid, H5T_NATIVE_UINT)) < 0)
813 if ((equal = H5Tequal(native_typeid, H5T_NATIVE_LLONG)) < 0)
820 if ((equal = H5Tequal(native_typeid, H5T_NATIVE_ULLONG)) < 0)
831 if((type = nc4_rec_find_hdf_type(h5->root_grp, native_typeid)))
833 *xtype = type->nc_typeid;
845 get_type_info2(NC_HDF5_FILE_INFO_T *h5, hid_t datasetid,
846 NC_TYPE_INFO_T **type_info)
848 htri_t is_str, equal = 0;
850 hid_t native_typeid, hdf_typeid;
854 assert(h5 && type_info);
862 if (!h5_native_type_constant_g[1])
864 h5_native_type_constant_g[1] = H5T_NATIVE_SCHAR;
865 h5_native_type_constant_g[2] = H5T_NATIVE_SHORT;
866 h5_native_type_constant_g[3] = H5T_NATIVE_INT;
867 h5_native_type_constant_g[4] = H5T_NATIVE_FLOAT;
868 h5_native_type_constant_g[5] = H5T_NATIVE_DOUBLE;
869 h5_native_type_constant_g[6] = H5T_NATIVE_UCHAR;
870 h5_native_type_constant_g[7] = H5T_NATIVE_USHORT;
871 h5_native_type_constant_g[8] = H5T_NATIVE_UINT;
872 h5_native_type_constant_g[9] = H5T_NATIVE_LLONG;
873 h5_native_type_constant_g[10] = H5T_NATIVE_ULLONG;
877 if ((hdf_typeid = H5Dget_type(datasetid)) < 0)
882 if ((native_typeid = H5Tget_native_type(hdf_typeid, H5T_DIR_DEFAULT)) < 0)
886 if ((
class = H5Tget_class(native_typeid)) < 0)
890 if (
class == H5T_STRING ||
class == H5T_INTEGER ||
class == H5T_FLOAT)
893 if (!(*type_info = calloc(1,
sizeof(NC_TYPE_INFO_T))))
898 if (
class == H5T_STRING)
900 if ((is_str = H5Tis_variable_str(native_typeid)) < 0)
903 if (is_str || H5Tget_size(hdf_typeid) > 1)
913 (*type_info)->nc_type_class =
NC_CHAR;
916 else if (
class == H5T_INTEGER ||
class == H5T_FLOAT)
918 for (t = 1; t < NUM_TYPES - 1; t++)
920 if ((equal = H5Tequal(native_typeid, h5_native_type_constant_g[t])) < 0)
932 if((order = H5Tget_order(hdf_typeid)) < 0)
935 if(order == H5T_ORDER_LE)
937 else if(order == H5T_ORDER_BE)
942 if(
class == H5T_INTEGER)
943 (*type_info)->nc_type_class =
NC_INT;
945 (*type_info)->nc_type_class =
NC_FLOAT;
947 (*type_info)->nc_typeid = nc_type_constant_g[t];
948 (*type_info)->size = nc_type_size_g[t];
949 if (!((*type_info)->name = strdup(nc_type_name_g[t])))
951 (*type_info)->hdf_typeid = hdf_typeid;
952 (*type_info)->native_hdf_typeid = native_typeid;
957 NC_TYPE_INFO_T *type;
960 if((type = nc4_rec_find_hdf_type(h5->root_grp, native_typeid)))
966 if (H5Tclose(native_typeid) < 0)
968 if (H5Tclose(hdf_typeid) < 0)
980 read_hdf5_att(NC_GRP_INFO_T *grp, hid_t attid, NC_ATT_INFO_T *att)
982 hid_t spaceid = 0, file_typeid = 0;
983 hsize_t dims[1] = {0};
987 hssize_t att_npoints;
988 H5T_class_t att_class;
989 int fixed_len_string = 0;
990 size_t fixed_size = 0;
993 LOG((5,
"%s: att->attnum %d att->name %s att->nc_typeid %d att->len %d",
994 __func__, att->attnum, att->name, (
int)att->nc_typeid, att->len));
997 if ((file_typeid = H5Aget_type(attid)) < 0)
999 if ((att->native_hdf_typeid = H5Tget_native_type(file_typeid, H5T_DIR_DEFAULT)) < 0)
1001 if ((att_class = H5Tget_class(att->native_hdf_typeid)) < 0)
1003 if (att_class == H5T_STRING && !H5Tis_variable_str(att->native_hdf_typeid))
1006 if (!(fixed_size = H5Tget_size(att->native_hdf_typeid)))
1009 if ((retval = get_netcdf_type(grp->nc4_info, att->native_hdf_typeid, &(att->nc_typeid))))
1014 if ((spaceid = H5Aget_space(attid)) < 0)
1019 if ((att_ndims = H5Sget_simple_extent_ndims(spaceid)) < 0)
1021 if ((att_npoints = H5Sget_simple_extent_npoints(spaceid)) < 0)
1026 if (att_ndims == 0 && att_npoints == 0)
1029 dims[0] = att_npoints;
1030 else if (att->nc_typeid ==
NC_CHAR)
1036 if (!(dims[0] = H5Tget_size(file_typeid)))
1043 dims[0] = att_npoints;
1048 H5S_class_t space_class;
1055 if ((space_class = H5Sget_simple_extent_type(spaceid)) < 0)
1059 if (H5S_NULL == space_class)
1063 if (H5S_SCALAR == space_class)
1068 if (H5Sget_simple_extent_dims(spaceid, dims, NULL) < 0)
1080 if ((retval = nc4_get_typelen_mem(grp->nc4_info, att->nc_typeid, 0,
1083 if (att_class == H5T_VLEN)
1085 if (!(att->vldata = malloc((
unsigned int)(att->len *
sizeof(hvl_t)))))
1087 if (H5Aread(attid, att->native_hdf_typeid, att->vldata) < 0)
1092 if (!(att->stdata = calloc(att->len,
sizeof(
char *))))
1105 if (fixed_len_string)
1108 char *contig_buf, *cur;
1111 if (!(contig_buf = malloc(att->len * fixed_size *
sizeof(
char))))
1115 if (H5Aread(attid, att->native_hdf_typeid, contig_buf) < 0) {
1124 for (i = 0; i < att->len; i++)
1126 if (!(att->stdata[i] = malloc(fixed_size))) {
1130 strncpy(att->stdata[i], cur, fixed_size);
1140 if (H5Aread(attid, att->native_hdf_typeid, att->stdata) < 0)
1146 if (!(att->data = malloc((
unsigned int)(att->len * type_size))))
1148 if (H5Aread(attid, att->native_hdf_typeid, att->data) < 0)
1153 if (H5Tclose(file_typeid) < 0)
1155 if (H5Sclose(spaceid) < 0)
1164 if (H5Tclose(file_typeid) < 0)
1166 if (spaceid > 0 && H5Sclose(spaceid) < 0)
1177 read_type(NC_GRP_INFO_T *grp, hid_t hdf_typeid,
char *type_name)
1179 NC_TYPE_INFO_T *type;
1181 hid_t native_typeid;
1185 assert(grp && type_name);
1187 LOG((4,
"%s: type_name %s grp->name %s", __func__, type_name, grp->name));
1190 if ((native_typeid = H5Tget_native_type(hdf_typeid, H5T_DIR_DEFAULT)) < 0)
1194 if (!(type_size = H5Tget_size(native_typeid)))
1196 LOG((5,
"type_size %d", type_size));
1199 if ((retval = nc4_type_list_add(grp, type_size, type_name, &type)))
1203 type->committed = NC_TRUE;
1204 type->hdf_typeid = hdf_typeid;
1205 H5Iinc_ref(type->hdf_typeid);
1206 type->native_hdf_typeid = native_typeid;
1209 if ((
class = H5Tget_class(hdf_typeid)) < 0)
1221 char* member_name = NULL;
1228 if ((nmembers = H5Tget_nmembers(hdf_typeid)) < 0)
1230 LOG((5,
"compound type has %d members", nmembers));
1231 for (m = 0; m < nmembers; m++)
1233 hid_t member_hdf_typeid;
1234 hid_t member_native_typeid;
1235 size_t member_offset;
1236 H5T_class_t mem_class;
1242 if ((member_hdf_typeid = H5Tget_member_type(type->native_hdf_typeid, m)) < 0)
1245 if ((member_native_typeid = H5Tget_native_type(member_hdf_typeid, H5T_DIR_DEFAULT)) < 0)
1249 member_name = H5Tget_member_name(type->native_hdf_typeid, m);
1250 if (!member_name || strlen(member_name) >
NC_MAX_NAME) {
1256 strncpy(jna,member_name,1000);
1262 member_offset = H5Tget_member_offset(type->native_hdf_typeid, m);
1265 if ((mem_class = H5Tget_class(member_hdf_typeid)) < 0)
1267 if (mem_class == H5T_ARRAY)
1273 if ((ndims = H5Tget_array_ndims(member_hdf_typeid)) < 0) {
1277 if (H5Tget_array_dims(member_hdf_typeid, dims, NULL) != ndims) {
1281 for (d = 0; d < ndims; d++)
1282 dim_size[d] = dims[d];
1285 if ((retval = get_netcdf_type(grp->nc4_info, H5Tget_super(member_hdf_typeid),
1290 if ((retval = nc4_field_list_add(&type->u.c.field, type->u.c.num_fields++, member_name,
1291 member_offset, H5Tget_super(member_hdf_typeid),
1292 H5Tget_super(member_native_typeid),
1293 member_xtype, ndims, dim_size)))
1299 if ((retval = get_netcdf_type(grp->nc4_info, member_native_typeid,
1304 if ((retval = nc4_field_list_add(&type->u.c.field, type->u.c.num_fields++, member_name,
1305 member_offset, member_hdf_typeid, member_native_typeid,
1306 member_xtype, 0, NULL)))
1317 #ifdef HDF5_HAS_H5FREE
1318 if(member_name != NULL) H5free_memory(member_name);
1321 if(member_name != NULL) free(member_name);
1328 if(member_name != NULL)
1344 if ((ret = H5Tis_variable_str(hdf_typeid)) < 0)
1350 hid_t base_hdf_typeid;
1353 type->nc_type_class =
NC_VLEN;
1357 if (!(base_hdf_typeid = H5Tget_super(native_typeid)))
1361 if (!(type_size = H5Tget_size(base_hdf_typeid)))
1365 if ((retval = get_netcdf_type(grp->nc4_info, base_hdf_typeid,
1368 LOG((5,
"base_hdf_typeid 0x%x type_size %d base_nc_type %d",
1369 base_hdf_typeid, type_size, base_nc_type));
1372 type->u.v.base_nc_typeid = base_nc_type;
1373 type->u.v.base_hdf_typeid = base_hdf_typeid;
1384 hid_t base_hdf_typeid;
1388 char *member_name = NULL;
1393 type->nc_type_class =
NC_ENUM;
1397 if (!(base_hdf_typeid = H5Tget_super(hdf_typeid)))
1400 if (!(type_size = H5Tget_size(base_hdf_typeid)))
1403 if ((retval = get_netcdf_type(grp->nc4_info, base_hdf_typeid,
1406 LOG((5,
"base_hdf_typeid 0x%x type_size %d base_nc_type %d",
1407 base_hdf_typeid, type_size, base_nc_type));
1410 type->u.e.base_nc_typeid = base_nc_type;
1411 type->u.e.base_hdf_typeid = base_hdf_typeid;
1414 if ((type->u.e.num_members = H5Tget_nmembers(hdf_typeid)) < 0)
1418 if (!(value = calloc(1, type_size)))
1422 for (i = 0; i < type->u.e.num_members; i++)
1426 if (!(member_name = H5Tget_member_name(hdf_typeid, i)))
1432 strncpy(jna,member_name,1000);
1441 if (H5Tget_member_value(hdf_typeid, i, value) < 0)
1448 if ((retval = nc4_enum_member_add(&type->u.e.enum_member, type->size,
1449 member_name, value)))
1456 if(member_name != NULL) free(member_name);
1462 if(member_name != NULL)
1465 if(value) free(value);
1472 LOG((0,
"unknown class"));
1483 read_var(NC_GRP_INFO_T *grp, hid_t datasetid,
const char *obj_name,
1484 size_t ndims, NC_DIM_INFO_T *dim)
1486 NC_VAR_INFO_T *var = NULL;
1487 hid_t access_pid = 0;
1493 char att_name[NC_MAX_HDF5_NAME + 1];
1495 #define CD_NELEMS_ZLIB 1
1496 #define CD_NELEMS_SZIP 4
1497 H5Z_filter_t filter;
1499 unsigned int cd_values[CD_NELEMS_SZIP];
1500 size_t cd_nelems = CD_NELEMS_SZIP;
1502 H5D_fill_value_t fill_status;
1503 H5D_layout_t layout;
1509 assert(obj_name && grp);
1510 LOG((4,
"%s: obj_name %s", __func__, obj_name));
1513 if ((retval = nc4_var_list_add(&grp->var, &var)))
1517 var->hdf_datasetid = datasetid;
1518 H5Iinc_ref(var->hdf_datasetid);
1520 var->varid = grp->nvars++;
1521 var->created = NC_TRUE;
1528 if (!(var->dim = calloc(var->ndims,
sizeof(NC_DIM_INFO_T *))))
1530 if (!(var->dimids = calloc(var->ndims,
sizeof(
int))))
1535 if ((access_pid = H5Dget_access_plist(datasetid)) < 0)
1542 if ((H5Pget_chunk_cache(access_pid, &(var->chunk_cache_nelems),
1543 &(var->chunk_cache_size), &rdcc_w0)) < 0)
1545 var->chunk_cache_preemption = rdcc_w0;
1550 if (strlen(obj_name) > strlen(NON_COORD_PREPEND) &&
1551 !strncmp(obj_name, NON_COORD_PREPEND, strlen(NON_COORD_PREPEND)))
1554 if (!(var->name = malloc(((strlen(obj_name) - strlen(NON_COORD_PREPEND))+ 1) *
sizeof(
char))))
1557 strcpy(var->name, &obj_name[strlen(NON_COORD_PREPEND)]);
1560 if (!(var->hdf5_name = malloc((strlen(obj_name) + 1) *
sizeof(
char))))
1563 strcpy(var->hdf5_name, obj_name);
1568 if (!(var->name = malloc((strlen(obj_name) + 1) *
sizeof(
char))))
1571 strcpy(var->name, obj_name);
1577 if ((propid = H5Dget_create_plist(datasetid)) < 0)
1584 if ((layout = H5Pget_layout(propid)) < -1)
1586 if (layout == H5D_CHUNKED)
1590 if (!(var->chunksizes = malloc(var->ndims *
sizeof(
size_t))))
1592 for (d = 0; d < var->ndims; d++)
1593 var->chunksizes[d] = chunksize[d];
1595 else if (layout == H5D_CONTIGUOUS || layout == H5D_COMPACT)
1596 var->contiguous = NC_TRUE;
1600 if ((num_filters = H5Pget_nfilters(propid)) < 0)
1602 for (f = 0; f < num_filters; f++)
1604 if ((filter = H5Pget_filter2(propid, f, NULL, &cd_nelems,
1605 cd_values, 0, NULL, NULL)) < 0)
1609 case H5Z_FILTER_SHUFFLE:
1610 var->shuffle = NC_TRUE;
1613 case H5Z_FILTER_FLETCHER32:
1614 var->fletcher32 = NC_TRUE;
1617 case H5Z_FILTER_DEFLATE:
1618 var->deflate = NC_TRUE;
1619 if (cd_nelems != CD_NELEMS_ZLIB || cd_values[0] > MAX_DEFLATE_LEVEL)
1621 var->deflate_level = cd_values[0];
1624 case H5Z_FILTER_SZIP:
1625 var->szip = NC_TRUE;
1626 if (cd_nelems != CD_NELEMS_SZIP)
1628 var->options_mask = cd_values[0];
1629 var->pixels_per_block = cd_values[1];
1633 LOG((1,
"Yikes! Unknown filter type found on dataset!"));
1639 if ((retval = get_type_info2(grp->nc4_info, datasetid,
1644 var->type_info->rc++;
1647 if (H5Pfill_value_defined(propid, &fill_status) < 0)
1651 if (fill_status == H5D_FILL_VALUE_USER_DEFINED)
1654 if (!var->fill_value)
1656 if (var->type_info->nc_type_class ==
NC_VLEN)
1658 if (!(var->fill_value = malloc(
sizeof(
nc_vlen_t))))
1661 else if (var->type_info->nc_type_class ==
NC_STRING)
1663 if (!(var->fill_value = malloc(
sizeof(
char *))))
1668 assert(var->type_info->size);
1669 if (!(var->fill_value = malloc(var->type_info->size)))
1675 if (H5Pget_fill_value(propid, var->type_info->native_hdf_typeid,
1676 var->fill_value) < 0)
1680 var->no_fill = NC_TRUE;
1686 var->dimscale = NC_TRUE;
1689 if ((retval = read_coord_dimids(var)))
1695 assert(0 == strcmp(var->name, dim->name));
1697 var->dimids[0] = dim->dimid;
1700 dim->coord_var = var;
1712 num_scales = H5DSget_num_scales(datasetid, 0);
1716 if (num_scales && ndims)
1720 if (NULL == (var->dimscale_attached = calloc(ndims,
sizeof(nc_bool_t))))
1725 if (NULL == (var->dimscale_hdf5_objids = malloc(ndims *
sizeof(
struct hdf5_objid))))
1727 for (d = 0; d < var->ndims; d++)
1729 if (H5DSiterate_scales(var->hdf_datasetid, d, NULL, dimscale_visitor,
1730 &(var->dimscale_hdf5_objids[d])) < 0)
1732 var->dimscale_attached[d] = NC_TRUE;
1739 if ((natts = H5Aget_num_attrs(var->hdf_datasetid)) < 0)
1741 for (a = 0; a < natts; a++)
1746 if (attid && H5Aclose(attid) < 0)
1750 if ((attid = H5Aopen_idx(var->hdf_datasetid, (
unsigned int)a)) < 0)
1752 if (H5Aget_name(attid, NC_MAX_HDF5_NAME, att_name) < 0)
1754 LOG((4,
"%s:: a %d att_name %s", __func__, a, att_name));
1757 if (strcmp(att_name, REFERENCE_LIST) &&
1758 strcmp(att_name, CLASS) &&
1759 strcmp(att_name, DIMENSION_LIST) &&
1760 strcmp(att_name, NAME) &&
1761 strcmp(att_name, COORDINATES) &&
1762 strcmp(att_name, NC_DIMID_ATT_NAME))
1765 if ((retval = nc4_att_list_add(&var->att, &att)))
1769 att->attnum = var->natts++;
1770 if (!(att->name = strdup(att_name)))
1775 if ((retval = read_hdf5_att(grp, attid, att)))
1779 if ((retval = nc4_att_list_del(&var->att, att)))
1787 att->created = NC_TRUE;
1793 if ((retval = nc4_adjust_var_cache(grp, var)))
1799 if (incr_id_rc && H5Idec_ref(datasetid) < 0)
1801 if (var && nc4_var_list_del(&grp->var, var))
1804 if (access_pid && H5Pclose(access_pid) < 0)
1809 if (propid > 0 && H5Pclose(propid) < 0)
1814 if (attid > 0 && H5Aclose(attid) < 0)
1822 read_grp_atts(NC_GRP_INFO_T *grp)
1827 NC_TYPE_INFO_T *type;
1828 char obj_name[NC_MAX_HDF5_NAME + 1];
1832 num_obj = H5Aget_num_attrs(grp->hdf_grpid);
1833 for (i = 0; i < num_obj; i++)
1837 if (attid && H5Aclose(attid) < 0)
1840 if ((attid = H5Aopen_idx(grp->hdf_grpid, (
unsigned int)i)) < 0)
1842 if (H5Aget_name(attid,
NC_MAX_NAME + 1, obj_name) < 0)
1844 LOG((3,
"reading attribute of _netCDF group, named %s", obj_name));
1850 if (!strcmp(obj_name, NC3_STRICT_ATT_NAME))
1855 if ((retval = nc4_att_list_add(&grp->att, &att)))
1860 if (!(att->name = malloc((max_len + 1) *
sizeof(
char))))
1862 strncpy(att->name, obj_name, max_len);
1863 att->name[max_len] = 0;
1864 att->attnum = grp->natts++;
1865 if ((retval = read_hdf5_att(grp, attid, att)))
1869 if ((retval = nc4_att_list_del(&grp->att, att)))
1876 att->created = NC_TRUE;
1877 if ((retval = nc4_find_type(grp->nc4_info, att->nc_typeid, &type)))
1883 if (attid > 0 && H5Aclose(attid) < 0)
1891 read_dataset(NC_GRP_INFO_T *grp, hid_t datasetid,
const char *obj_name,
1892 const H5G_stat_t *statbuf)
1894 NC_DIM_INFO_T *dim = NULL;
1901 if ((spaceid = H5Dget_space(datasetid)) < 0)
1906 if ((ndims = H5Sget_simple_extent_ndims(spaceid)) < 0)
1910 if ((is_scale = H5DSis_scale(datasetid)) < 0)
1914 hsize_t dims[H5S_MAX_RANK];
1915 hsize_t max_dims[H5S_MAX_RANK];
1918 if (H5Sget_simple_extent_dims(spaceid, dims, max_dims) < 0)
1922 if ((retval = read_scale(grp, datasetid, obj_name, statbuf, dims[0],
1923 max_dims[0], &dim)))
1930 if (NULL == dim || (dim && !dim->hdf_dimscaleid))
1931 if ((retval = read_var(grp, datasetid, obj_name, ndims, dim)))
1935 if (spaceid && H5Sclose(spaceid) <0)
1952 if (!(new_oinfo = calloc(1,
sizeof(*new_oinfo))))
1956 memcpy(new_oinfo, oinfo,
sizeof(*oinfo));
1961 (*tail)->next = new_oinfo;
1966 assert(NULL == *head);
1967 *head = *tail = new_oinfo;
1974 nc4_rec_read_metadata_cb(hid_t grpid,
const char *name,
const H5L_info_t *info,
1979 int retval = H5_ITER_CONT;
1982 memset(&oinfo, 0,
sizeof(oinfo));
1985 if ((oinfo.oid = H5Oopen(grpid, name, H5P_DEFAULT)) < 0)
1986 BAIL(H5_ITER_ERROR);
1989 if (H5Gget_objinfo(oinfo.oid,
".", 1, &oinfo.statbuf) < 0)
1990 BAIL(H5_ITER_ERROR);
1995 switch(oinfo.statbuf.type)
1998 LOG((3,
"found group %s", oinfo.oname));
2004 if (nc4_rec_read_metadata_cb_list_add(&udata->grps_head, &udata->grps_tail, &oinfo))
2005 BAIL(H5_ITER_ERROR);
2009 LOG((3,
"found dataset %s", oinfo.oname));
2013 if ((retval = read_dataset(udata->grp, oinfo.oid, oinfo.oname, &oinfo.statbuf)))
2021 BAIL(H5_ITER_ERROR);
2023 retval = H5_ITER_CONT;
2027 if (H5Oclose(oinfo.oid) < 0)
2028 BAIL(H5_ITER_ERROR);
2032 LOG((3,
"found datatype %s", oinfo.oname));
2035 if (read_type(udata->grp, oinfo.oid, oinfo.oname))
2036 BAIL(H5_ITER_ERROR);
2039 if (H5Oclose(oinfo.oid) < 0)
2040 BAIL(H5_ITER_ERROR);
2044 LOG((0,
"Unknown object class %d in %s!", oinfo.statbuf.type, __func__));
2045 BAIL(H5_ITER_ERROR);
2051 if (oinfo.oid > 0 && H5Oclose(oinfo.oid) < 0)
2052 BAIL2(H5_ITER_ERROR);
2066 nc4_rec_read_metadata(NC_GRP_INFO_T *grp)
2072 unsigned crt_order_flags = 0;
2073 H5_index_t iter_index;
2076 assert(grp && grp->name);
2077 LOG((3,
"%s: grp->name %s", __func__, grp->name));
2080 memset(&udata, 0,
sizeof(udata));
2084 if (!grp->hdf_grpid)
2088 if ((grp->hdf_grpid = H5Gopen2(grp->parent->hdf_grpid,
2089 grp->name, H5P_DEFAULT)) < 0)
2094 if ((grp->hdf_grpid = H5Gopen2(grp->nc4_info->hdfid,
2095 "/", H5P_DEFAULT)) < 0)
2099 assert(grp->hdf_grpid > 0);
2102 pid = H5Gget_create_plist(grp->hdf_grpid);
2103 H5Pget_link_creation_order(pid, &crt_order_flags);
2104 if (H5Pclose(pid) < 0)
2108 if (crt_order_flags & H5P_CRT_ORDER_TRACKED)
2109 iter_index = H5_INDEX_CRT_ORDER;
2112 NC_HDF5_FILE_INFO_T *h5 = grp->nc4_info;
2118 iter_index = H5_INDEX_NAME;
2127 if (H5Literate(grp->hdf_grpid, iter_index, H5_ITER_INC, &idx,
2128 nc4_rec_read_metadata_cb, (
void *)&udata) < 0)
2135 for (oinfo = udata.grps_head; oinfo; oinfo = udata.grps_head)
2137 NC_GRP_INFO_T *child_grp;
2138 NC_HDF5_FILE_INFO_T *h5 = grp->nc4_info;
2141 if ((retval = nc4_grp_list_add(&(grp->children), h5->next_nc_grpid++,
2142 grp, grp->nc4_info->controller, oinfo->oname, &child_grp)))
2146 if ((retval = nc4_rec_read_metadata(child_grp)))
2150 if (H5Oclose(oinfo->oid) < 0)
2154 udata.grps_head = oinfo->next;
2159 if ((retval = read_grp_atts(grp)))
2166 for (oinfo = udata.grps_head; oinfo; oinfo = udata.grps_head)
2169 if (H5Oclose(oinfo->oid) < 0)
2173 udata.grps_head = oinfo->next;
2185 nc4_open_file(
const char *path,
int mode,
void* parameters, NC *nc)
2187 hid_t fapl_id = H5P_DEFAULT;
2188 unsigned flags = (mode &
NC_WRITE) ?
2189 H5F_ACC_RDWR : H5F_ACC_RDONLY;
2191 NC_HDF5_FILE_INFO_T* nc4_info = NULL;
2194 NC_MEM_INFO* meminfo = (NC_MEM_INFO*)parameters;
2197 NC_MPI_INFO* mpiinfo = (NC_MPI_INFO*)parameters;
2202 LOG((3,
"%s: path %s mode %d", __func__, path, mode));
2206 if ((retval = nc4_nc4f_list_add(nc, path, mode)))
2208 nc4_info = NC4_DATA(nc);
2209 assert(nc4_info && nc4_info->root_grp);
2214 if ((fapl_id = H5Pcreate(H5P_FILE_ACCESS)) < 0)
2220 if (H5Pset_fclose_degree(fapl_id, H5F_CLOSE_SEMI))
2223 if (H5Pset_fclose_degree(fapl_id, H5F_CLOSE_STRONG))
2230 if (mode & NC_MPIIO || mode & NC_MPIPOSIX)
2232 nc4_info->parallel = NC_TRUE;
2233 if (mode & NC_MPIIO)
2235 LOG((4,
"opening parallel file with MPI/IO"));
2236 if (H5Pset_fapl_mpio(fapl_id, mpiinfo->comm, mpiinfo->info) < 0)
2239 #ifdef USE_PARALLEL_POSIX
2242 LOG((4,
"opening parallel file with MPI/posix"));
2243 if (H5Pset_fapl_mpiposix(fapl_id, mpiinfo->comm, 0) < 0)
2256 if (MPI_SUCCESS != MPI_Comm_dup(mpiinfo->comm, &nc4_info->comm))
2259 if (MPI_INFO_NULL != mpiinfo->info)
2261 if (MPI_SUCCESS != MPI_Info_dup(mpiinfo->info, &nc4_info->info))
2268 nc4_info->info = mpiinfo->info;
2272 if (H5Pset_cache(fapl_id, 0, nc4_chunk_cache_nelems, nc4_chunk_cache_size,
2273 nc4_chunk_cache_preemption) < 0)
2275 LOG((4,
"%s: set HDF raw chunk cache to size %d nelems %d preemption %f",
2276 __func__, nc4_chunk_cache_size, nc4_chunk_cache_nelems, nc4_chunk_cache_preemption));
2283 if((nc4_info->hdfid = H5LTopen_file_image(meminfo->memory,meminfo->size,
2284 H5LT_FILE_IMAGE_DONT_COPY|H5LT_FILE_IMAGE_DONT_RELEASE
2287 nc4_info->no_write = NC_TRUE;
2288 }
else if ((nc4_info->hdfid = H5Fopen(path, flags, fapl_id)) < 0)
2293 nc4_info->no_write = NC_TRUE;
2299 if ((retval = nc4_rec_read_metadata(nc4_info->root_grp)))
2304 if ((retval = nc4_rec_match_dimscales(nc4_info->root_grp)))
2310 log_metadata_nc(nc);
2314 if (H5Pclose(fapl_id) < 0)
2324 if (comm_duped) MPI_Comm_free(&nc4_info->comm);
2325 if (info_duped) MPI_Info_free(&nc4_info->info);
2330 if (fapl_id != H5P_DEFAULT) H5Pclose(fapl_id);
2331 if (!nc4_info)
return retval;
2332 close_netcdf4_file(nc4_info,1);
2351 get_netcdf_type_from_hdf4(NC_HDF5_FILE_INFO_T *h5, int32 hdf4_typeid,
2352 nc_type *xtype, NC_TYPE_INFO_T *type_info)
2364 assert(h5 && xtype);
2452 if (hdf4_typeid == DFNT_FLOAT32)
2453 type_info->nc_type_class =
NC_FLOAT;
2454 else if (hdf4_typeid == DFNT_FLOAT64)
2456 else if (hdf4_typeid == DFNT_CHAR)
2459 type_info->nc_type_class =
NC_INT;
2460 type_info->endianness = endianness;
2461 type_info->nc_typeid = *xtype;
2462 type_info->size = nc_type_size_g[t];
2463 if (!(type_info->name = strdup(nc_type_name_g[t])))
2473 nc4_open_hdf4_file(
const char *path,
int mode, NC *nc)
2475 NC_HDF5_FILE_INFO_T *h5;
2478 int32 num_datasets, num_gatts;
2482 NC_HDF5_FILE_INFO_T* nc4_info = NULL;
2484 LOG((3,
"%s: path %s mode %d", __func__, path, mode));
2488 if (mode & NC_WRITE)
2492 if ((retval = nc4_nc4f_list_add(nc, path, mode)))
2494 nc4_info = NC4_DATA(nc);
2495 assert(nc4_info && nc4_info->root_grp);
2499 h5->no_write = NC_TRUE;
2502 if ((h5->sdid = SDstart(path, DFACC_READ)) == FAIL)
2506 if (SDfileinfo(h5->sdid, &num_datasets, &num_gatts))
2510 for (a = 0; a < num_gatts; a++)
2512 int32 att_data_type, att_count;
2513 size_t att_type_size;
2516 if ((retval = nc4_att_list_add(&h5->root_grp->att, &att)))
2518 att->attnum = grp->natts++;
2519 att->created = NC_TRUE;
2524 if (SDattrinfo(h5->sdid, a, att->name, &att_data_type, &att_count))
2526 if ((retval = get_netcdf_type_from_hdf4(h5, att_data_type,
2527 &att->nc_typeid, NULL)))
2529 att->len = att_count;
2532 if ((retval = nc4_get_typelen_mem(h5, att->nc_typeid, 0, &att_type_size)))
2534 if (!(att->data = malloc(att_type_size * att->len)))
2538 if (SDreadattr(h5->sdid, a, att->data))
2543 for (v = 0; v < num_datasets; v++)
2546 int32 data_type, num_atts;
2555 int32 *dimsize = NULL;
2556 size_t var_type_size;
2560 if ((retval = nc4_var_list_add(&grp->var, &var)))
2563 var->varid = grp->nvars++;
2564 var->created = NC_TRUE;
2565 var->written_to = NC_TRUE;
2568 if ((var->sdsid = SDselect(h5->sdid, v)) == FAIL)
2576 if (SDgetinfo(var->sdsid, var->name, &rank, NULL, &data_type, &num_atts))
2579 if(!(dimsize = (int32*)malloc(
sizeof(int32)*rank)))
2582 if (SDgetinfo(var->sdsid, var->name, &rank, dimsize, &data_type, &num_atts)) {
2583 if(dimsize) free(dimsize);
2588 var->hdf4_data_type = data_type;
2591 if (!(var->type_info = calloc(1,
sizeof(NC_TYPE_INFO_T)))) {
2592 if(dimsize) free(dimsize);
2596 if ((retval = get_netcdf_type_from_hdf4(h5, data_type, &var->type_info->nc_typeid, var->type_info))) {
2597 if(dimsize) free(dimsize);
2602 var->type_info->rc++;
2604 if ((retval = nc4_get_typelen_mem(h5, var->type_info->nc_typeid, 0, &var_type_size))) {
2605 if(dimsize) free(dimsize);
2609 var->type_info->size = var_type_size;
2610 LOG((3,
"reading HDF4 dataset %s, rank %d netCDF type %d", var->name,
2611 rank, var->type_info->nc_typeid));
2614 if (!(var->fill_value = malloc(var_type_size))) {
2615 if(dimsize) free(dimsize);
2619 if (SDgetfillvalue(var->sdsid, var->fill_value))
2622 free(var->fill_value);
2623 var->fill_value = NULL;
2629 if (!(var->dim = malloc(
sizeof(NC_DIM_INFO_T *) * var->ndims))) {
2630 if(dimsize) free(dimsize);
2634 if (!(var->dimids = malloc(
sizeof(
int) * var->ndims))) {
2635 if(dimsize) free(dimsize);
2642 for (d = 0; d < var->ndims; d++)
2644 int32 dimid, dim_len, dim_data_type, dim_num_attrs;
2648 if ((dimid = SDgetdimid(var->sdsid, d)) == FAIL) {
2649 if(dimsize) free(dimsize);
2652 if (SDdiminfo(dimid, dim_name, &dim_len, &dim_data_type,
2655 if(dimsize) free(dimsize);
2661 for (dim = grp->dim; dim; dim = dim->l.next)
2662 if (!strcmp(dim->name, dim_name))
2668 LOG((4,
"adding dimension %s for HDF4 dataset %s",
2669 dim_name, var->name));
2670 if ((retval = nc4_dim_list_add(&grp->dim, &dim)))
2673 dim->dimid = grp->nc4_info->next_dimid++;
2676 if (!(dim->name = strdup(dim_name)))
2681 dim->len = *dimsize;
2685 var->dimids[d] = dim->dimid;
2689 for (a = 0; a < num_atts; a++)
2691 int32 att_data_type, att_count;
2692 size_t att_type_size;
2695 if ((retval = nc4_att_list_add(&var->att, &att))) {
2696 if(dimsize) free(dimsize);
2699 att->attnum = var->natts++;
2700 att->created = NC_TRUE;
2704 if(dimsize) free(dimsize);
2707 if (SDattrinfo(var->sdsid, a, att->name, &att_data_type, &att_count)) {
2708 if(dimsize) free(dimsize);
2711 if ((retval = get_netcdf_type_from_hdf4(h5, att_data_type,
2712 &att->nc_typeid, NULL))) {
2713 if(dimsize) free(dimsize);
2717 att->len = att_count;
2720 if ((retval = nc4_get_typelen_mem(h5, att->nc_typeid, 0, &att_type_size))) {
2721 if(dimsize) free(dimsize);
2724 if (!(att->data = malloc(att_type_size * att->len))) {
2725 if(dimsize) free(dimsize);
2730 if (SDreadattr(var->sdsid, a, att->data)) {
2731 if(dimsize) free(dimsize);
2735 if(dimsize) free(dimsize);
2739 HDF_CHUNK_DEF chunkdefs;
2741 if(!SDgetchunkinfo(var->sdsid, &chunkdefs, &flag)) {
2742 if(flag == HDF_NONE)
2743 var->contiguous = NC_TRUE;
2744 else if((flag & HDF_CHUNK) != 0) {
2745 var->contiguous = NC_FALSE;
2746 if (!(var->chunksizes = malloc(var->ndims *
sizeof(
size_t))))
2748 for (d = 0; d < var->ndims; d++) {
2749 var->chunksizes[d] = chunkdefs.chunk_lengths[d];
2760 log_metadata_nc(h5->root_grp->nc4_info->controller);
2768 NC4_open(
const char *path,
int mode,
int basepe,
size_t *chunksizehintp,
2769 int use_parallel,
void *parameters, NC_Dispatch *dispatch, NC *nc_file)
2774 NC_MPI_INFO mpidfalt = {MPI_COMM_WORLD, MPI_INFO_NULL};
2776 #if defined USE_PARALLEL || defined USE_HDF4
2780 assert(nc_file && path);
2782 LOG((1,
"%s: path %s mode %d params %x",
2783 __func__, path, mode, parameters));
2786 if (!inmemory && use_parallel && parameters == NULL)
2787 parameters = &mpidfalt;
2793 if (H5Eset_auto(NULL, NULL) < 0)
2794 LOG((0,
"Couldn't turn off HDF5 error messages!"));
2795 LOG((1,
"HDF5 error messages turned off!"));
2801 if((mode & ILLEGAL_OPEN_FLAGS) != 0)
2805 if((mode & (NC_MPIIO|NC_MPIPOSIX)) == (NC_MPIIO|NC_MPIPOSIX))
2808 #ifndef USE_PARALLEL_POSIX
2812 if(mode & NC_MPIPOSIX)
2814 mode &= ~NC_MPIPOSIX;
2820 if ((res = nc_check_for_hdf(path, use_parallel, parameters, &hdf_file)))
2824 nc_file->int_ncid = nc_file->ext_ncid;
2825 if (hdf_file == NC_HDF5_FILE)
2826 res = nc4_open_file(path, mode, parameters, nc_file);
2828 else if (hdf_file == NC_HDF4_FILE && inmemory)
2830 else if (hdf_file == NC_HDF4_FILE)
2831 res = nc4_open_hdf4_file(path, mode, nc_file);
2846 NC4_set_fill(
int ncid,
int fillmode,
int *old_modep)
2849 NC_HDF5_FILE_INFO_T* nc4_info;
2851 LOG((2,
"%s: ncid 0x%x fillmode %d", __func__, ncid, fillmode));
2853 if (!(nc = nc4_find_nc_file(ncid,&nc4_info)))
2858 if (nc4_info->no_write)
2867 *old_modep = nc4_info->fill_mode;
2869 nc4_info->fill_mode = fillmode;
2873 if (nc4_info->pnetcdf_file)
2874 return ncmpi_set_fill(nc->int_ncid, fillmode, old_modep);
2886 NC_HDF5_FILE_INFO_T* nc4_info;
2888 LOG((1,
"%s: ncid 0x%x", __func__, ncid));
2891 if (!(nc4_find_nc_file(ncid,&nc4_info)))
2897 if (nc4_info->pnetcdf_file)
2898 return ncmpi_redef(nc->int_ncid);
2902 if (nc4_info->flags & NC_INDEF)
2906 if (nc4_info->no_write)
2910 nc4_info->flags |= NC_INDEF;
2914 nc4_info->redef = NC_TRUE;
2922 NC4__enddef(
int ncid,
size_t h_minfree,
size_t v_align,
2923 size_t v_minfree,
size_t r_align)
2925 if (nc4_find_nc_file(ncid,NULL) == NULL)
2928 return NC4_enddef(ncid);
2933 static int NC4_enddef(
int ncid)
2936 NC_HDF5_FILE_INFO_T* nc4_info;
2938 LOG((1,
"%s: ncid 0x%x", __func__, ncid));
2940 if (!(nc = nc4_find_nc_file(ncid,&nc4_info)))
2945 if (nc4_info->pnetcdf_file)
2948 res = ncmpi_enddef(nc->int_ncid);
2951 if (nc4_info->pnetcdf_access_mode == NC_INDEPENDENT)
2952 res = ncmpi_begin_indep_data(nc->int_ncid);
2958 return nc4_enddef_netcdf4_file(nc4_info);
2964 sync_netcdf4_file(NC_HDF5_FILE_INFO_T *h5)
2969 LOG((3,
"%s", __func__));
2973 if (h5->flags & NC_INDEF)
2979 h5->flags ^= NC_INDEF;
2982 h5->redef = NC_FALSE;
2988 log_metadata_nc(h5->root_grp->nc4_info->controller);
2994 nc_bool_t bad_coord_order = NC_FALSE;
2996 if ((retval = nc4_rec_write_groups_types(h5->root_grp)))
2998 if ((retval = nc4_rec_detect_need_to_preserve_dimids(h5->root_grp, &bad_coord_order)))
3000 if ((retval = nc4_rec_write_metadata(h5->root_grp, bad_coord_order)))
3004 if (H5Fflush(h5->hdfid, H5F_SCOPE_GLOBAL) < 0)
3017 NC_HDF5_FILE_INFO_T* nc4_info;
3019 LOG((2,
"%s: ncid 0x%x", __func__, ncid));
3021 if (!(nc = nc4_find_nc_file(ncid,&nc4_info)))
3027 if (nc4_info->pnetcdf_file)
3028 return ncmpi_sync(nc->int_ncid);
3032 if (nc4_info && nc4_info->flags & NC_INDEF)
3036 if ((retval = NC4_enddef(ncid)))
3040 return sync_netcdf4_file(nc4_info);
3047 close_netcdf4_file(NC_HDF5_FILE_INFO_T *h5,
int abort)
3051 assert(h5 && h5->root_grp);
3052 LOG((3,
"%s: h5->path %s abort %d", __func__, h5->controller->path, abort));
3055 if (h5->flags & NC_INDEF)
3056 h5->flags ^= NC_INDEF;
3060 if (!h5->no_write && !abort)
3061 if ((retval = sync_netcdf4_file(h5)))
3066 if ((retval = nc4_rec_grp_del(&h5->root_grp, h5->root_grp)))
3073 if (SDend(h5->sdid))
3083 MPI_Comm_free(&h5->comm);
3084 if(MPI_INFO_NULL != h5->info)
3085 MPI_Info_free(&h5->info);
3088 if (H5Fclose(h5->hdfid) < 0)
3092 nobjs = H5Fget_obj_count(h5->hdfid, H5F_OBJ_ALL);
3096 }
else if(nobjs > 0) {
3102 LOG((0,
"There are %d HDF5 objects open!", nobjs));
3128 int delete_file = 0;
3131 NC_HDF5_FILE_INFO_T* nc4_info;
3133 LOG((2,
"%s: ncid 0x%x", __func__, ncid));
3136 if (!(nc = nc4_find_nc_file(ncid,&nc4_info)))
3143 if (nc4_info->pnetcdf_file)
3144 return ncmpi_abort(nc->int_ncid);
3148 if (nc4_info->flags & NC_INDEF && !nc4_info->redef)
3156 if ((retval = close_netcdf4_file(nc4_info, 1)))
3161 if (
remove(path) < 0)
3173 NC_HDF5_FILE_INFO_T *h5;
3176 LOG((1,
"%s: ncid 0x%x", __func__, ncid));
3179 if ((retval = nc4_find_nc_grp_h5(ncid, &nc, &grp, &h5)))
3182 assert(nc && h5 && grp);
3190 if (h5->pnetcdf_file)
3191 return ncmpi_close(nc->int_ncid);
3195 if ((retval = close_netcdf4_file(grp->nc4_info, 0)))
3204 NC4_inq(
int ncid,
int *ndimsp,
int *nvarsp,
int *nattsp,
int *unlimdimidp)
3207 NC_HDF5_FILE_INFO_T *h5;
3214 LOG((2,
"%s: ncid 0x%x", __func__, ncid));
3217 if ((retval = nc4_find_nc_grp_h5(ncid, &nc, &grp, &h5)))
3220 assert(h5 && grp && nc);
3224 if (h5->pnetcdf_file)
3225 return ncmpi_inq(nc->int_ncid, ndimsp, nvarsp, nattsp, unlimdimidp);
3232 for (dim = grp->dim; dim; dim = dim->l.next)
3238 for (var = grp->var; var; var= var->l.next)
3244 for (att = grp->att; att; att = att->l.next)
3257 for (dim = grp->dim; dim; dim = dim->l.next)
3260 *unlimdimidp = dim->dimid;
3270 NC4_set_content(
int ncid,
size_t size,
void* memory)
3275 NC_HDF5_FILE_INFO_T *h5;
3278 LOG((4,
"%s: ncid 0x%x size %ld memory 0x%x", __func__, ncid, size, memory));
3281 if ((retval = nc4_find_nc_grp_h5(ncid, &nc, &grp, &h5)))
3283 assert(h5 && grp && nc);
3286 herr = H5Pset_file_image(h5->hdfid,memory,size);
3300 nc4_enddef_netcdf4_file(NC_HDF5_FILE_INFO_T *h5)
3303 LOG((3,
"%s", __func__));
3306 if (!(h5->flags & NC_INDEF))
3310 h5->flags ^= NC_INDEF;
3313 h5->redef = NC_FALSE;
3315 return sync_netcdf4_file(h5);
3322 if (num_plists || num_spaces)
3331 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.
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.
#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_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.
#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.