herr_t write_byteoffset_data(hid_t group_id, const std::string &name, const pyublas::numpy_vector<int> &data, int width, int height) { hsize_t dim[2] = {height, width}; H5::DataSpace space2D(2, dim); unsigned int cd_values[CBF_H5Z_FILTER_CBF_NELMTS]; hsize_t chunk2[2] = {dim[0], dim[1]}; cd_values[CBF_H5Z_FILTER_CBF_COMPRESSION] = CBF_BYTE_OFFSET; // byte_offset cd_values[CBF_H5Z_FILTER_CBF_RESERVED] = 0; cd_values[CBF_H5Z_FILTER_CBF_BINARY_ID] = 1; // ? cd_values[CBF_H5Z_FILTER_CBF_PADDING] = 4095; // ? cd_values[CBF_H5Z_FILTER_CBF_ELSIZE] = 4; // always 32 bit? cd_values[CBF_H5Z_FILTER_CBF_ELSIGN] = 1; // 1 for signed, 0 if unsigned cd_values[CBF_H5Z_FILTER_CBF_REAL] = 0; // 1 if a real array, 0 if an integer array cd_values[CBF_H5Z_FILTER_CBF_DIMFAST] = dim[1]; cd_values[CBF_H5Z_FILTER_CBF_DIMMID] = dim[0]; cd_values[CBF_H5Z_FILTER_CBF_DIMSLOW] = 1; hid_t valprop = H5Pcreate(H5P_DATASET_CREATE); herr_t status = H5Pset_chunk(valprop, 2, chunk2); status = H5Pset_filter(valprop, CBF_H5Z_FILTER_CBF, H5Z_FLAG_OPTIONAL, CBF_H5Z_FILTER_CBF_NELMTS, cd_values); // Don't forget to set HDF5_PLUGIN_PATH! if (status < 0) { std::cout << "Error in H5Pset_filter\n==============================\n"; H5Eprint(H5E_DEFAULT, stdout); std::cout << "==============================\n"; } hid_t dset = H5Dcreate(group_id, name.c_str(), H5T_STD_I32LE, // always 32 bit? space2D.getId(), H5P_DEFAULT, valprop, H5P_DEFAULT); status = H5Dwrite(dset, H5T_NATIVE_INT, H5S_ALL, H5S_ALL, H5P_DEFAULT, // always 32 bit? &data[0]); // is this really a safe way?? return status; }
int main(int argc, char **argv) { H5Eprint(stderr); init(argc,argv); if(test_bzip2() != NC_NOERR) ERRR; exit(nerrs > 0?1:0); }
void* load_H5_Data_4D(const char* filename, int datatype, const char* datasetname, int W, int H, int D, int ox, int oy, int oz, int timestep, int comp, bool fit) { // fprintf(stderr, "%s:%s()\n", __FILE__, __func__); //only handle h5 data with one component for now assert(comp == 1); hid_t input_file = H5Fopen(filename, H5F_ACC_RDONLY, H5P_DEFAULT); if (input_file < 0) { fprintf(stderr, "%s:%s(): Error opening input file %s\n", __FILE__, __func__, filename); exit(1); } hid_t dataset = H5Dopen(input_file, datasetname); if (dataset < 0) { H5Eprint(NULL); H5Fclose(input_file); fprintf(stderr, "Error opening dataset on file %s\n", filename); //throw IOException(string("Error opening dataset")); exit(1); } hid_t dataspace = H5Dget_space(dataset); assert(dataspace >=0); hsize_t maxdims[4]; hsize_t dims[4]; int dimensions = H5Sget_simple_extent_dims(dataspace, dims, maxdims); assert (dimensions == 4); #ifdef _DEBUG fprintf(stderr, "data dimension(%d, %d, %d, %d)\n", (int)dims[0], (int)dims[1], (int)dims[2], (int)dims[3]); fprintf(stderr, "origin(%d,%d,%d,%d), count(%d,%d,%d,%d)\n", ox, oy, oz, timestep, W, H, D, 1); if(fit) fprintf(stderr, "read data to fit\n"); #endif hsize_t start[4]; hsize_t start_out[4]; hsize_t stride[4]; hsize_t stride_out[4]; hsize_t count[4]; hsize_t count_out[4]; hsize_t block[4]; hsize_t block_out[4]; start[0] = timestep; start_out[0] = 0; start[1] = oz; start_out[1] = 0; start[2] = oy; start_out[2] = 0; start[3] = ox; start_out[3] = 0; stride[0] = 1; stride_out[0] = 1; stride[1] = fit? dims[0]/D : 1; stride_out[1] = 1; stride[2] = fit? dims[1]/H : 1; stride_out[2] = 1; stride[3] = fit? dims[2]/W : 1; stride_out[3] = 1; count[0] = 1; count_out[0] = count[0]; count[1] = D; count_out[1] = count[1]; count[2] = H; count_out[2] = count[2]; count[3] = W; count_out[3] = count[3]; block[0] = 1; block_out[0] = 1; block[1] = 1; block_out[1] = 1; block[2] = 1; block_out[2] = 1; block[3] = 1; block_out[3] = 1; herr_t status; status = H5Sselect_hyperslab(dataspace, H5S_SELECT_SET, start, stride, count, block); int b = get_depth(datatype); size_t my_val = count[0] * count[1] * count[2] * count[3] * b; //printf("set to read %ld bytes data\n", my_val); assert(H5Sget_select_npoints(dataspace) * b == my_val); //allocate the output buffer void *wholedata = malloc(my_val); if(wholedata == NULL) { fprintf(stderr, "can't allocate output buffer\n"); exit(1); } if ( status < 0 ) { H5Eprint(NULL); fprintf(stderr, "Cannot select data slab\n"); //throw IOException("Cannot select data slab"); exit(1); } hid_t memspace = H5Screate_simple(4, count_out, NULL); status = H5Sselect_hyperslab(memspace, H5S_SELECT_SET, start_out , stride_out, count_out, block_out); if ( status < 0 ) { H5Eprint(NULL); fprintf(stderr, "Cannot select mem slab\n"); //throw IOException("Cannot select mem slab"); exit(1); } assert(H5Sget_select_npoints(memspace) * b == my_val); hid_t plist = H5Pcreate(H5P_DATASET_XFER); //is this hint really useful? status = H5Pset_buffer(plist, my_val, NULL, NULL); if ( status < 0 ) { H5Eprint(NULL); fprintf(stderr, "Cannot set data buffer\n"); //throw IOException("Cannot set buffer"); exit(1); } //read data into "data" buffer, fortran ordering! hid_t mem_type; switch(datatype) { case 0: mem_type = H5T_NATIVE_CHAR; break; case 1: mem_type = H5T_NATIVE_SHORT; break; case 2: mem_type = H5T_NATIVE_FLOAT; break; //H5T_IEEE_F32LE case 3: mem_type = H5T_NATIVE_DOUBLE; break; default: mem_type = H5T_NATIVE_CHAR; } status = H5Dread (dataset, mem_type, memspace, dataspace, plist, wholedata); if ( status < 0 ) { H5Eprint(NULL); fprintf(stderr, "READ ERROR!\n"); //throw IOException("Read error"); exit(1); } H5Pclose (plist); H5Sclose (memspace); H5Sclose( dataspace ); H5Dclose( dataset ); H5Fclose( input_file ); return wholedata; }
void nc_log_hdf5(void) { H5Eprint(NULL); }