int main(int argc, char *argv[]) { float *two_theta = NULL; int *counts = NULL, rank, i; hid_t fid, dataid, fapl; hsize_t *dim = NULL; hid_t datatype, dataspace, memdataspace; /* * Open file, thereby enforcing proper file close * semantics */ fapl = H5Pcreate(H5P_FILE_ACCESS); H5Pset_fclose_degree(fapl,H5F_CLOSE_STRONG); fid = H5Fopen("NXfile.h5", H5F_ACC_RDONLY,fapl); H5Pclose(fapl); /* * open and read the counts dataset */ dataid = H5Dopen(fid,"/scan/data/counts"); dataspace = H5Dget_space(dataid); rank = H5Sget_simple_extent_ndims(dataspace); dim = malloc(rank*sizeof(hsize_t)); H5Sget_simple_extent_dims(dataspace, dim, NULL); counts = malloc(dim[0]*sizeof(int)); memdataspace = H5Tcopy(H5T_NATIVE_INT32); H5Dread(dataid,memdataspace,H5S_ALL, H5S_ALL,H5P_DEFAULT, counts); H5Dclose(dataid); H5Sclose(dataspace); H5Tclose(memdataspace); /* * open and read the two_theta data set */ dataid = H5Dopen(fid,"/scan/data/two_theta"); dataspace = H5Dget_space(dataid); rank = H5Sget_simple_extent_ndims(dataspace); dim = malloc(rank*sizeof(hsize_t)); H5Sget_simple_extent_dims(dataspace, dim, NULL); two_theta = malloc(dim[0]*sizeof(float)); memdataspace = H5Tcopy(H5T_NATIVE_FLOAT); H5Dread(dataid,memdataspace,H5S_ALL, H5S_ALL,H5P_DEFAULT, two_theta); H5Dclose(dataid); H5Sclose(dataspace); H5Tclose(memdataspace); H5Fclose(fid); for(i = 0; i < dim[0]; i++){ printf("%8.2f %10d\n", two_theta[i], counts[i]); } }
int_f nh5sselect_elements_c ( hid_t_f *space_id , int_f *op, size_t_f *nelements, hsize_t_f *coord) { int ret_value = -1; hid_t c_space_id; H5S_seloper_t c_op; herr_t status; int rank; int i, j; hsize_t *c_coord; size_t c_nelements; /* if (*op != H5S_SELECT_SET_F) return ret_value; */ if (*op != H5S_SELECT_SET) return ret_value; c_op = H5S_SELECT_SET; c_space_id = *space_id; rank = H5Sget_simple_extent_ndims(c_space_id); c_coord = malloc(sizeof(hsize_t)*rank*(*nelements)); if(!c_coord) return ret_value; for (i=0; i< *nelements; i++) { for (j = 0; j < rank; j++) { c_coord[j+i*rank] = (hsize_t)coord[j + i*rank]; } } c_nelements = *nelements; status = H5Sselect_elements(c_space_id, c_op, c_nelements, (const hsize_t **)c_coord); if ( status >= 0 ) ret_value = 0; HDfree(c_coord); return ret_value; }
int_f nh5sget_select_elem_pointlist_c( hid_t_f *space_id ,hsize_t_f * startpoint, hsize_t_f * numpoints, hsize_t_f * buf) { int ret_value = -1; hid_t c_space_id; hsize_t c_num_points; hsize_t c_startpoint,* c_buf; int i, rank; c_space_id = *space_id; c_num_points = (hsize_t)* numpoints; rank = H5Sget_simple_extent_ndims(c_space_id); if (rank < 0 ) return ret_value; c_startpoint = (hsize_t)*startpoint; c_buf = (hsize_t*)malloc(sizeof(hsize_t)*(size_t)(c_num_points*rank)); if (!c_buf) return ret_value; ret_value = H5Sget_select_elem_pointlist(c_space_id, c_startpoint, c_num_points, c_buf); for (i = c_num_points*rank-1; i >= 0; i--) { buf[i] = (hsize_t_f)(c_buf[i]+1); } if (ret_value >= 0 ) ret_value = 0; HDfree(c_buf); return ret_value; }
int_f nh5sget_select_hyper_blocklist_c( hid_t_f *space_id ,hsize_t_f * startblock, hsize_t_f * num_blocks, hsize_t_f * buf) { int ret_value = -1; hid_t c_space_id; hsize_t c_num_blocks; hsize_t i; int rank; hsize_t c_startblock, *c_buf; c_space_id = *space_id; c_num_blocks = * num_blocks; rank = H5Sget_simple_extent_ndims(c_space_id); if (rank < 0 ) return ret_value; c_startblock = (hsize_t)*startblock; c_buf = (hsize_t*)malloc(sizeof(hsize_t)*(size_t)(c_num_blocks*2*rank)); if (!c_buf) return ret_value; ret_value = H5Sget_select_hyper_blocklist(c_space_id, c_startblock, c_num_blocks, c_buf); for(i = 0; i < c_num_blocks*2*rank; i++) { buf[i] = (hsize_t_f)c_buf[i] +1; } HDfree(c_buf); if (ret_value >= 0 ) ret_value = 0; return ret_value; }
//----------------------------------------------------------------------------- int MGL_EXPORT mgl_datac_read_hdf(HADT d,const char *fname,const char *data) { hid_t hf,hd,hs; hsize_t dims[4]; long rank, res = H5Fis_hdf5(fname); if(res<=0) { return false; } hf = H5Fopen(fname, H5F_ACC_RDONLY, H5P_DEFAULT); if(hf<0) return false; hd = H5Dopen(hf,data); if(hd<0) return false; hs = H5Dget_space(hd); rank = H5Sget_simple_extent_ndims(hs); if(rank>0 && rank<=3) { H5Sget_simple_extent_dims(hs,dims,0); if(rank==2) { dims[2]=dims[0]; dims[0]=dims[1]=1; } else if(rank==3) { dims[2]=dims[1]; dims[1]=dims[0]; dims[0]=1; } // else if(rank>3) continue; mgl_datac_create(d,dims[2],dims[1],dims[0]); #if MGL_USE_DOUBLE H5Dread(hd, H5T_NATIVE_DOUBLE, H5S_ALL, H5S_ALL, H5P_DEFAULT, d->a); #else H5Dread(hd, H5T_NATIVE_FLOAT, H5S_ALL, H5S_ALL, H5P_DEFAULT, d->a); #endif } H5Sclose(hs); H5Dclose(hd); H5Fclose(hf); return true; }
int read_vector(hid_t loc_id,const char *name,T *value,int len) { hid_t space_id,dset_id,dti_file; int hdferr,ndims; hsize_t dims[2], maxdims[2]; printf("Attempting to read vector '%s'\n",name); dset_id= H5Dopen2(loc_id,name,H5P_DEFAULT); space_id= H5Dget_space(dset_id); ndims= H5Sget_simple_extent_ndims(space_id); if(ndims!=1) return -1; H5Sget_simple_extent_dims(space_id,dims,maxdims); if(dims[0]>len) return -1; hdferr= H5Dread(dset_id,TypeTraits<T>::hdf5Type(),H5S_ALL,H5S_ALL,H5P_DEFAULT,value); if(hdferr<0) return -1; H5Sclose(space_id); H5Dclose(dset_id); return dims[0]; }
cv::Mat hdf5file::load(const std::string& name) { hdf5dataset hdataset(*this, name); hid_t dataset_id = hdataset.handle(); hid_t space_id = H5Dget_space(dataset_id); hid_t type_id = hdataset.type(); //hid_t type_id = H5Tget_native_type(type_id_nnative, H5T_DIR_DEFAULT); hsize_t rank = H5Sget_simple_extent_ndims(space_id); std::vector<hsize_t> dims_out(rank); H5Sget_simple_extent_dims(space_id, &(dims_out[0]), NULL); //due to different types, the dimension sizes are copied in a vector of different type std::vector<int> dims_out_i(rank); std::copy(dims_out.begin(), dims_out.end(), dims_out_i.begin()); int channels = 1; if(rank == 3 && dims_out[rank-1] <= 4) { channels = dims_out[rank-1]; --rank; } cv::Mat dataset = cv::Mat(rank, &(dims_out_i[0]), CV_MAKETYPE(h5types::typeid_to_ocv(type_id), channels)); hdataset.read(dataset.data); H5Sclose(space_id); return dataset; }
int_f h5dset_extent_c ( hid_t_f *dset_id , hsize_t_f *dims) /******/ { hid_t c_space_id; hsize_t c_dims[H5S_MAX_RANK]; int rank; int i; int status; int ret_value = -1; if((c_space_id = H5Dget_space((hid_t)*dset_id)) < 0) return ret_value; rank = H5Sget_simple_extent_ndims(c_space_id); H5Sclose(c_space_id); if(rank < 0 ) return ret_value; /* * Reverse dimensions due to C-FORTRAN storage order. */ for(i = 0; i < rank; i++) c_dims[i] = (hsize_t)dims[rank - i - 1]; status = H5Dset_extent((hid_t)*dset_id, c_dims); if(status >= 0) ret_value = 0; return ret_value; }
// JRC: This fat interface may not scale? What about // scalar attributes? herr_t VsH5Attribute::getDoubleVectorValue(std::vector<double>* dvals) { herr_t err = 0; size_t npoints; hid_t atype = H5Aget_type(getId()); H5T_class_t type = H5Tget_class(atype); hid_t aspace = H5Aget_space(getId()); size_t rank = H5Sget_simple_extent_ndims(aspace); if (type != H5T_FLOAT) { VsLog::warningLog() <<"VsH5Attribute::getDoubleVectorValue() - Requested attribute " <<getShortName() <<" is not a floating point vector." <<std::endl; dvals->resize(0); return -1; } if (rank == 0) { dvals->resize(1); double v; err = H5Aread(getId(), H5T_NATIVE_DOUBLE, &v); (*dvals)[0] = v; return err; } // rank>0 npoints = H5Sget_simple_extent_npoints(aspace); double* v = new double[npoints]; err = H5Aread(getId(), H5T_NATIVE_DOUBLE, v); dvals->resize(npoints); for (size_t i = 0; i<npoints; ++i) { (*dvals)[i] = v[i]; } delete [] v; return err; }
int luaC_h5_get_ndims(lua_State *L) { const char *dsetnm = luaL_checkstring(L, 1); if (PresentFile < 0) { luaL_error(L, "no open file"); } hid_t dset = H5Dopen(PresentFile, dsetnm, H5P_DEFAULT); if (dset < 0) { luaL_error(L, "no data set named %s", dsetnm); } hid_t fspc = H5Dget_space(dset); size_t Nd = H5Sget_simple_extent_ndims(fspc); hsize_t *dims = (hsize_t*) malloc(Nd*sizeof(hsize_t)); int *dims_int = (int*) malloc(Nd*sizeof(int)); H5Sget_simple_extent_dims(fspc, dims, NULL); int i; for (i=0; i<Nd; ++i) { dims_int[i] = dims[i]; } _pusharray_i(L, dims_int, Nd); H5Sclose(fspc); H5Dclose(dset); free(dims); free(dims_int); return 1; }
void h5_value_doubles(hid_t file_id, char *group, char *name, double **values, int *numValues) { int kk; hid_t group_id = H5Gopen(file_id, group, H5P_DEFAULT); hid_t data_id = H5Dopen(group_id, name, H5P_DEFAULT); hid_t data_space = H5Dget_space(data_id); int rank = H5Sget_simple_extent_ndims(data_space); hsize_t dims[H5S_MAX_RANK], maxdim[H5S_MAX_RANK]; H5Sget_simple_extent_dims(data_space, dims, maxdim); hid_t data_type = H5Dget_type(data_id); H5T_class_t data_class = H5Tget_native_type(data_type, H5T_DIR_DEFAULT); size_t data_size = H5Tget_size(data_class); hsize_t elements = 1; for (kk=0; kk<rank; kk++) elements *= dims[kk]; void *buf = (void *) MALLOC((size_t)(elements*data_size)); H5Dread(data_id, data_class, H5S_ALL, H5S_ALL, H5P_DEFAULT, buf); *values = buf; *numValues = elements; H5Tclose(data_type); H5Tclose(data_class); H5Sclose(data_space); H5Dclose(data_id); H5Gclose(group_id); }
int_f nh5sget_simple_extent_dims_c ( hid_t_f *space_id , hsize_t_f *dims, hsize_t_f *maxdims) { int ret_value = -1; hid_t c_space_id; hsize_t *c_dims; hsize_t *c_maxdims; int status; int rank; int i; c_space_id = *space_id; rank = H5Sget_simple_extent_ndims(c_space_id); if (rank < 0) return ret_value; c_dims = malloc(sizeof(hsize_t)*rank); if (!c_dims) return ret_value; c_maxdims = malloc(sizeof(hsize_t)*rank); if (!c_maxdims) return ret_value; status = H5Sget_simple_extent_dims(c_space_id, c_dims, c_maxdims); /* * Reverse dimensions due to C-FORTRAN storage order. */ for (i=0; i < rank; i++) { dims[rank - i - 1] = (hsize_t_f)c_dims[i]; maxdims[rank - i - 1] = (hsize_t_f)c_maxdims[i]; } if ( status >= 0 ) ret_value = rank; HDfree(c_dims); HDfree(c_maxdims); return ret_value; }
int_f nh5sget_select_bounds_c( hid_t_f *space_id , hsize_t_f * start, hsize_t_f * end) { int ret_value = -1; hid_t c_space_id; hsize_t* c_start, *c_end; int i, rank; c_space_id = *space_id; rank = H5Sget_simple_extent_ndims(c_space_id); if (rank < 0 ) return ret_value; c_start =(hsize_t*) malloc(sizeof(hsize_t)*rank); if (!c_start) return ret_value; c_end = (hsize_t*)malloc(sizeof(hsize_t)*rank); if(!c_end) return ret_value; ret_value = H5Sget_select_bounds(c_space_id, c_start, c_end); for(i = 0; i < rank; i++) { start[i] = (hsize_t_f)(c_start[rank-i-1]+1); end[i] = (hsize_t_f)(c_end[rank-i-1]+1); } if (ret_value >= 0 ) ret_value = 0; HDfree(c_start); HDfree(c_end); return ret_value; }
//hyperslab write void hdf5IoDataModel::writeHyperslab(const QString &dataset_name, DataType type, quint64 *offset, quint64 *stride, quint64 *count, quint64 *block, quint64 *values_shape, void *values) { hid_t dataset_id = d->datasetId(dataset_name); //the selection within the file dataset's dataspace hid_t file_dataspace = H5Dget_space(dataset_id); if(H5Sselect_hyperslab(file_dataspace, H5S_SELECT_SET, offset, stride, count, block)<0) { dtkError() << "ERROR selecting hyperslab" << dataset_name; } //set the dimensions of values. memory dataspace and the selection within it hid_t values_dataspace = H5Screate_simple(H5Sget_simple_extent_ndims(file_dataspace), values_shape, NULL); switch(type) { case dtkIoDataModel::Int: // TODO put d->prop_list_id instead of H5P_DEFAULT ???????? d->status = H5Dwrite(dataset_id, H5T_NATIVE_INT, values_dataspace, file_dataspace, H5P_DEFAULT, values); break; case dtkIoDataModel::LongLongInt: d->status = H5Dwrite(dataset_id, H5T_NATIVE_LLONG, values_dataspace, file_dataspace, H5P_DEFAULT, values); break; case dtkIoDataModel::Double: d->status = H5Dwrite(dataset_id, H5T_NATIVE_DOUBLE, values_dataspace, file_dataspace, H5P_DEFAULT, values); break; default: dtkError() << "write method: Datatype not supported"; }; if(d->status<0) { dtkError() << "error writing hyperslab" << dataset_name; } H5Sclose(file_dataspace); H5Sclose(values_dataspace); }
//-***************************************************************************** // Get the dimensions directly off of the dataspace on the dataset // This isn't suitable for string and wstring void ReadDataSetDimensions( hid_t iParent, const std::string &iName, hsize_t iExtent, Dimensions &oDims ) { // Open the data set. hid_t dsetId = H5Dopen( iParent, iName.c_str(), H5P_DEFAULT ); ABCA_ASSERT( dsetId >= 0, "Cannot open dataset: " << iName ); DsetCloser dsetCloser( dsetId ); // Read the data space. hid_t dspaceId = H5Dget_space( dsetId ); ABCA_ASSERT( dspaceId >= 0, "Could not get dataspace for dataSet: " << iName ); DspaceCloser dspaceCloser( dspaceId ); H5S_class_t dspaceClass = H5Sget_simple_extent_type( dspaceId ); if ( dspaceClass == H5S_SIMPLE ) { // Get the dimensions int rank = H5Sget_simple_extent_ndims( dspaceId ); ABCA_ASSERT( rank == 1, "H5Sget_simple_extent_ndims() must be 1." ); hsize_t hdim = 0; rank = H5Sget_simple_extent_dims( dspaceId, &hdim, NULL ); oDims.setRank(1); oDims[0] = hdim / iExtent; } else { oDims.setRank(1); oDims[0] = 0; } }
inline size_t DataSpace::getNumberDimensions() const{ const int ndim = H5Sget_simple_extent_ndims(_hid); if(ndim < 0){ HDF5ErrMapper::ToException<DataSetException>("Unable to get dataspace number of dimensions"); } return size_t(ndim); }
/* read attribute value */ char* H5Dataset_read_attr_value(hid_t aid) { hid_t asid=-1, atid=-1; int i, rank; hsize_t *dims; size_t size=1; char *attr_buf=NULL; asid = H5Aget_space(aid); atid = H5Aget_type(aid); rank = H5Sget_simple_extent_ndims(asid); if (rank > 0) { dims = (hsize_t *)malloc(rank * sizeof(hsize_t)); H5Sget_simple_extent_dims(asid, dims, NULL); for (i=0; i<rank; i++) { size *= (size_t)dims[i]; free(dims); } size *= H5Tget_size(atid); attr_buf = (char *)malloc(size); if (H5Aread( aid, atid, attr_buf) < 0) { free(attr_buf); attr_buf = NULL; } } if( atid > 0) H5Tclose(atid); if (asid > 0) H5Sclose(asid); return attr_buf; }
bool Hdf5Dataset::getSphereRI(MapVecDouble &mvec) { hsize_t dims_out[2], count[2], offset[2], dimsm[2]; hid_t dataspace = H5Dget_space(this->sphere_dataset_); // dataspace handle int rank = H5Sget_simple_extent_ndims(dataspace); herr_t status_n = H5Sget_simple_extent_dims(dataspace, dims_out, NULL); herr_t status; offset[0] = 0; offset[1] = 0; count[0] = dims_out[0]; count[1] = 4; double data_out[count[0]][count[1]]; status = H5Sselect_hyperslab(dataspace, H5S_SELECT_SET, offset, NULL, count, NULL); dimsm[0] = count[0]; dimsm[1] = count[1]; hid_t memspace; memspace = H5Screate_simple(RANK_OUT, dimsm, NULL); status = H5Dread(this->sphere_dataset_, H5T_NATIVE_DOUBLE, memspace, dataspace, H5P_DEFAULT, data_out); for (int i = 0; i < count[0]; i++) { std::vector< double > sphere_center(3); double ri; for (int j = 0; j < 3; j++) { sphere_center[j] = data_out[i][j]; } for (int k = 3; k < 4; k++) { ri = data_out[i][k]; } mvec.insert(std::pair< std::vector< double >, double >(sphere_center, ri)); } return 0; }
static void ufo_hdf5_reader_open (UfoReader *reader, const gchar *filename) { UfoHdf5ReaderPrivate *priv; gchar *h5_filename; gchar *h5_dataset; gchar **components; priv = UFO_HDF5_READER_GET_PRIVATE (reader); components = g_strsplit (filename, ":", 2); if (components[1] == NULL) { g_warning ("hdf5: must specify dataset name after color"); return; } h5_filename = components[0]; h5_dataset = components[1]; priv->file_id = H5Fopen (h5_filename, H5F_ACC_RDWR, H5P_DEFAULT); priv->dataset_id = H5Dopen (priv->file_id, h5_dataset, H5P_DEFAULT); priv->src_dataspace_id = H5Dget_space (priv->dataset_id); priv->n_dims = H5Sget_simple_extent_ndims (priv->src_dataspace_id); if (priv->n_dims > 3) g_error ("read:hdf5: no support for four-dimensional data"); H5Sget_simple_extent_dims (priv->src_dataspace_id, priv->dims, NULL); priv->current = 0; g_strfreev (components); }
ls2_hdf5_read_anchors(hid_t file, vector2** anchors, size_t *no_anchors) { hid_t dataset, dataspace, memspace; hsize_t dims[2]; int rank; dataset = H5Dopen2(file, "/Anchors", H5P_DEFAULT); dataspace = H5Dget_space(dataset); rank = H5Sget_simple_extent_ndims(dataspace); if (rank != 2) { fprintf(stderr, "/Anchors wrong rank %d\n", (int) rank); return -1; } H5Sget_simple_extent_dims(dataspace, dims, NULL); if (dims[1] != 2) { fprintf(stderr, "/Anchors dimension (%d, %d)\n", (int) dims[0], (int) dims[1]); return -1; } *no_anchors = (size_t) dims[0]; *anchors = (vector2 *) calloc(*no_anchors, sizeof(vector2)); memspace = H5Screate_simple(2, dims, NULL); H5Dread(dataset, H5T_NATIVE_FLOAT, memspace, dataspace, H5P_DEFAULT, *anchors); H5Dclose(dataset); H5Sclose(dataspace); H5Sclose(memspace); return 0; }
int luaC_h5_read_array(lua_State *L) { const char *dsetnm = luaL_checkstring(L, 1); if (PresentFile < 0) { luaL_error(L, "no open file"); } hid_t dset = H5Dopen(PresentFile, dsetnm, H5P_DEFAULT); if (dset < 0) { luaL_error(L, "no data set named %s", dsetnm); } hid_t fspc = H5Dget_space(dset); size_t Nd = H5Sget_simple_extent_ndims(fspc); hsize_t *dims = (hsize_t*) malloc(Nd*sizeof(hsize_t)); int *dims_int = (int*) malloc(Nd*sizeof(int)); H5Sget_simple_extent_dims(fspc, dims, NULL); int i, ntot=1; for (i=0; i<Nd; ++i) { dims_int[i] = dims[i]; ntot *= dims[i]; } double *data = (double*) malloc(ntot * sizeof(double)); H5Dread(dset, H5T_NATIVE_DOUBLE, fspc, fspc, H5P_DEFAULT, data); _pusharray_wshape(L, data, dims_int, Nd); H5Sclose(fspc); H5Dclose(dset); free(data); free(dims); free(dims_int); return 1; }
int_f nh5soffset_simple_c ( hid_t_f *space_id , hssize_t_f *offset) { int ret_value = -1; hid_t c_space_id; int rank; hssize_t *c_offset; herr_t status; int i; c_space_id = *space_id; rank = H5Sget_simple_extent_ndims(c_space_id); if (rank < 0) return ret_value; c_offset = malloc(sizeof(hssize_t)*rank); if (!c_offset) return ret_value; /* * Reverse dimensions due to C-FORTRAN storage order. */ for (i=0; i < rank; i++) c_offset[i] = offset[rank - i - 1]; status = H5Soffset_simple(c_space_id, c_offset); if ( status >= 0 ) ret_value = 0; HDfree(c_offset); return ret_value; }
std::vector<hsize_t> HdfDataset::dims() const { hid_t sid = H5Dget_space( d->id ); std::vector<hsize_t> ret( static_cast<size_t>( H5Sget_simple_extent_ndims( sid ) ) ); H5Sget_simple_extent_dims( sid, ret.data(), nullptr ); H5Sclose( sid ); return ret; }
int_f nh5sselect_hyperslab_c ( hid_t_f *space_id , int_f *op, hsize_t_f *start, hsize_t_f *count, hsize_t_f *stride, hsize_t_f *block) { int ret_value = -1; hid_t c_space_id; hsize_t *c_start = NULL; hsize_t *c_count = NULL; hsize_t *c_stride = NULL; hsize_t *c_block = NULL; H5S_seloper_t c_op; herr_t status; int rank; int i; rank = H5Sget_simple_extent_ndims(*space_id); if (rank < 0 ) return ret_value; c_start = (hsize_t *)HDmalloc(sizeof(hsize_t)*rank); if (c_start == NULL) goto DONE; c_count = (hsize_t *)HDmalloc(sizeof(hsize_t)*rank); if (c_count == NULL) goto DONE; c_stride = (hsize_t *)HDmalloc(sizeof(hsize_t)*rank); if (c_stride == NULL) goto DONE; c_block = (hsize_t *)HDmalloc(sizeof(hsize_t)*rank); if (c_block == NULL) goto DONE; /* * Reverse dimensions due to C-FORTRAN storage order. */ for (i=0; i < rank; i++) { int t= (rank - i) - 1; c_start[i] = (hsize_t)start[t]; c_count[i] = (hsize_t)count[t]; c_stride[i] = (hsize_t)stride[t]; c_block[i] = (hsize_t)block[t]; } c_op = (H5S_seloper_t)*op; /* if (*op == H5S_SELECT_SET_F) c_op = H5S_SELECT_SET; if (*op == H5S_SELECT_OR_F) c_op = H5S_SELECT_OR; */ c_space_id = *space_id; status = H5Sselect_hyperslab(c_space_id, c_op, c_start, c_stride, c_count, c_block); if ( status >= 0 ) ret_value = 0; DONE: if(c_start != NULL) HDfree(c_start); if(c_count != NULL) HDfree(c_count); if(c_stride!= NULL) HDfree(c_stride); if(c_block != NULL) HDfree(c_block); return ret_value; }
/*+++++++++++++++++++++++++ .IDENTifer PYTABLE_write_records .PURPOSE Write records to an HDF5 array .INPUT/OUTPUT call as stat = PYTABLE_write_records( locID, dset_name, start, step, count, buffer ); input: hid_t locID : HDF5 identifier of file or group char *dset_name : name of dataset hsize_t *start : index of first row to overwrite hsize_t *step : hsize_t *count : number of rows to write void *buffer : data to write .RETURNS A negative value is returned on failure. .COMMENTS none -------------------------*/ herr_t PYTABLE_write_records( hid_t locID, const char *dset_name, hsize_t *start, hsize_t *step, hsize_t *count, const void *buffer ) { int rank; hid_t dataID; hid_t spaceID = -1; hid_t mem_spaceID = -1; hid_t typeID = -1; /* open the dataset. */ if ( (dataID = H5Dopen( locID, dset_name, H5P_DEFAULT )) < 0 ) return -1; /* get the dataspace handle */ if ( (spaceID = H5Dget_space( dataID )) < 0 ) goto done; /* get rank */ if ( (rank = H5Sget_simple_extent_ndims( spaceID )) <= 0 ) goto done; /* create a simple memory data space */ if ( (mem_spaceID = H5Screate_simple( rank, count, NULL )) < 0 ) goto done; /* define a hyperslab in the dataset */ if ( H5Sselect_hyperslab( spaceID, H5S_SELECT_SET, start, step, count, NULL ) < 0 ) goto done; /* get an identifier for the datatype. */ if ( (typeID = H5Dget_type( dataID )) < 0 ) goto done; /* write data to hyperslap */ if ( H5Dwrite( dataID, typeID, mem_spaceID, spaceID, H5P_DEFAULT, buffer ) < 0 ) goto done; /* terminate access to the datatype */ if ( H5Tclose( typeID ) < 0 ) goto done; /* end access to the dataset */ if ( H5Dclose( dataID ) ) goto done; /* terminate access to the dataspace */ if ( H5Sclose( mem_spaceID ) < 0 ) goto done; if ( H5Sclose( spaceID ) < 0 ) goto done; return 0; done: if ( typeID > 0 ) (void) H5Tclose( typeID ); if ( spaceID > 0 ) (void) H5Sclose( spaceID ); if ( mem_spaceID > 0 ) (void) H5Sclose( mem_spaceID ); if ( dataID > 0 ) (void) H5Dclose( dataID ); return -1; }
/*-------------------------------------------------------------*/ static void validateFloat3Attribute(pNXVcontext self, hid_t dpField, char *name) { hid_t attID, attType, attSpace; H5T_class_t h5class; hsize_t dims[2], maxDims[2]; char fname[512]; memset(fname,0,sizeof(fname)); H5Iget_name(dpField,fname,sizeof(fname)); if(!H5LTfind_attribute(dpField,name)){ NXVsetLog(self,"sev","error"); NXVprintLog(self,"message", "Missing attribute %s on %s", name, fname); NXVlog(self); self->errCount++; } else { attID = H5Aopen(dpField,name,H5P_DEFAULT); assert(attID >= 0); attType = H5Aget_type(attID); assert(attType >= 0); h5class = H5Tget_class(attType); if(h5class != H5T_FLOAT){ NXVsetLog(self,"sev","error"); NXVprintLog(self,"message", "%s attribute on %s is of wrong type, expected float", name, fname); NXVlog(self); self->errCount++; } else { attSpace = H5Aget_space(attID); if(H5Sget_simple_extent_ndims(attSpace) != 1){ NXVsetLog(self,"sev","error"); NXVprintLog(self,"message", "%s attribute on %s is of wrong rank, expected 1", name, fname); NXVlog(self); self->errCount++; } else { H5Sget_simple_extent_dims(attSpace,dims,maxDims); if(dims[0] != 3){ NXVsetLog(self,"sev","error"); NXVprintLog(self,"message", "%s attribute on %s is of wrong size, expected 3", name, fname); NXVlog(self); self->errCount++; } } H5Sclose(attSpace); } H5Tclose(attType); H5Aclose(attID); } }
bool seissol::checkpoint::h5::Wavefield::validate(hid_t h5file) const { // Turn of error printing H5ErrHandler errHandler; // Check #partitions hid_t h5attr = H5Aopen(h5file, "partitions", H5P_DEFAULT); if (h5attr < 0) { logWarning(rank()) << "Checkpoint does not have a partition attribute."; return false; } int p; herr_t err = H5Aread(h5attr, H5T_NATIVE_INT, &p); checkH5Err(H5Aclose(h5attr)); if (err < 0 || p != partitions()) { logWarning(rank()) << "Partitions in checkpoint do not match."; return false; } // Check dimensions hid_t h5data = H5Dopen(h5file, "values", H5P_DEFAULT); if (h5data < 0) { logWarning(rank()) << "Checkpoint does not contains a data array."; return false; } hid_t h5space = H5Dget_space(h5data); checkH5Err(H5Dclose(h5data)); if (h5space < 0) { logWarning(rank()) << "Could not get space identifier in checkpoint."; return false; } bool isValid = true; int dims = H5Sget_simple_extent_ndims(h5space); if (dims != 1) { isValid = false; logWarning(rank()) << "Number of dimensions in checkpoint does not match."; } else { hsize_t dimSize; if (H5Sget_simple_extent_dims(h5space, &dimSize, 0L) != 1) { isValid = false; logWarning(rank()) << "Could not get dimension sizes of checkpoint."; } else { if (dimSize != numTotalElems()) { isValid = false; logWarning(rank()) << "Number of elements in checkpoint does not match."; } } } checkH5Err(H5Sclose(h5space)); return isValid; }
static int is_scalar(hid_t dataset){ hid_t s = H5Dget_space(dataset); int dims = H5Sget_simple_extent_ndims(s); H5Sclose(s); if(dims == 0){ return 1; }else{ return 0; } }
void FWSingle::readInFloat(int step, vector<float>& data_out) { int RANK=1; hid_t dataset; hid_t filespace; hid_t memspace; hid_t cparms; hsize_t dims[2]; /* dataset and chunk dimensions*/ hsize_t chunk_dims[1]; hsize_t col_dims[1]; hsize_t count[2]; hsize_t offset[2]; herr_t status, status_n; int rank, rank_chunk; hsize_t hi, hj; c_file = H5Fopen(fname.str().c_str(),H5F_ACC_RDONLY,H5P_DEFAULT); stringstream gname(""); gname<<"Block_"<< step; hid_t d_file = H5Gopen(c_file,gname.str().c_str()); dataset = H5Dopen(d_file, "Positions"); filespace = H5Dget_space(dataset); rank = H5Sget_simple_extent_ndims(filespace); status_n = H5Sget_simple_extent_dims(filespace, dims, NULL); // if (verbose>1) printf("dataset rank %d, dimensions %lu x %lu\n", rank, (unsigned long)(dims[0]), (unsigned long)(dims[1])); data_out.resize(dims[0]); cparms = H5Dget_create_plist(dataset); if (H5D_CHUNKED == H5Pget_layout(cparms)) { rank_chunk = H5Pget_chunk(cparms, 2, chunk_dims); // if (verbose>1) printf("chunk rank %d, dimensions %lu \n", rank_chunk, (unsigned long)(chunk_dims[0]) ); } memspace = H5Screate_simple(RANK,dims,NULL); status = H5Dread(dataset, H5T_NATIVE_FLOAT, memspace, filespace, H5P_DEFAULT, &(data_out[0])); if(verbose>2) { printf("\n"); printf("Dataset: \n"); for (int j = 0; j < dims[0]; j++) app_log()<<data_out[j]<<" "; app_log()<<endl; } H5Pclose(cparms); H5Dclose(dataset); H5Sclose(filespace); H5Sclose(memspace); H5Gclose(d_file); H5Fclose(c_file); }
void HdfDataspace::selectHyperslab( hsize_t start, hsize_t count ) { // this function works only for 1D arrays assert( H5Sget_simple_extent_ndims( d->id ) == 1 ); herr_t status = H5Sselect_hyperslab( d->id, H5S_SELECT_SET, &start, NULL, &count, NULL ); if ( status < 0 ) { MDAL::debug( "Failed to select 1D hyperslab!" ); } }