Hi,
I am writing out a block of floats 3D over several time steps.
The following is a two time steps sample:
HDF5 "/Users/nyue/hyperslab.h5" {
GROUP "/" {
DATASET "MBGuides" {
DATATYPE H5T_IEEE_F32BE
DATASPACE SIMPLE { ( 2, 3, 4, 3 ) / ( 2, 3, 4, 3 ) }
}
}
How should I retrieve the 3D block of data from the, say, first time step? Should I be using hyperslab ? Is that an overkill or is there a more straight forward way as I'd imagine this being pretty common.
Is there an example I can refer to ? Preferably C-code.
My current attempting has the following (non working)
#include <stdio.h>
#include <hdf5.h>
#include <string>
#include <vector>
#include <iostream>
#include <boost/multi_array.hpp>
int main(int argc, char **argv)
{
if (argc!=2) {
return 1;
}
std::string filename(argv[1]);
std::string dataset_name("MBGuides");
hid_t file = H5Fopen(argv[1], H5F_ACC_RDONLY, H5P_DEFAULT);
hid_t dataset = H5Dopen(file,dataset_name.c_str(), H5P_DEFAULT);
hid_t dataspace = H5Dget_space(dataset);
int rank = H5Sget_simple_extent_ndims (dataspace);
std::vector<hsize_t> dims_out(rank);
herr_t status = H5Sget_simple_extent_dims (dataspace, &dims_out[0], NULL);
std::cout << "Rank " << rank << std::endl;
std::cout << "Dimensions : ";
for (int i=0;i<rank;i++) {
if (i==0)
std::cout << dims_out[i];
else
std::cout << " x " << dims_out[i];
}
std::cout << std::endl;
int hyperslab_rank = rank-1;
std::vector<hsize_t> dims_hyperslab(rank-1);
for (int i=0;i<hyperslab_rank;i++) {
dims_hyperslab[i] = dims_out[i+1];
}
hid_t memspace = H5Screate_simple(hyperslab_rank,&dims_hyperslab[0],NULL);
// Allocate
typedef boost::multi_array<float, 3> array3D_type;
typedef array3D_type::index index3D;
array3D_type data_out(boost::extents[dims_hyperslab[0]][dims_hyperslab[1]][dims_hyperslab[2]]);
hsize_t offset_out[4] = {0,0,0,0};
hsize_t count_out[4] = {0,3,4,3};
// H5Sselect_hyperslab(hid_t space_id, H5S_seloper_t op, const hsize_t *start, const hsize_t *stride, const hsize_t *count, const hsize_t *block )
status = H5Sselect_hyperslab (memspace, H5S_SELECT_SET, offset_out, NULL, count_out, NULL);
status = H5Dread (dataset, H5T_NATIVE_INT, memspace, dataspace,
H5P_DEFAULT, data_out.data());
H5Sclose (dataspace);
H5Dclose (dataset);
H5Fclose (file);
return 0;
}
Regards