Search code examples
c++hdf5

C++ Array to HDF5


I need to store data from two float32 arrays in an .h5-file. The arrays both have the size 76800 (240x320) and represent an image each. I would be happy to just store the two arrays as they are in an .h5 file, but since I'm a total beginner with c++, I have no clue how to do this.

I looked here, but the conversion to a multi-array does not seem to be necessary for my case. Even though this seems like a really simple problem, I couldn't find a simple code example for this.

Here is my code so far:

H5::H5File file("/home/m/Desktop/tryout/file.h5", H5F_ACC_TRUNC);

// Vector X
hsize_t dims_pol[1] = {f->flow_x.size()};
H5::DataSpace dataspace_x(1, dims_pol);
H5::IntType datatype_x(H5::PredType::NATIVE_FLOAT);
H5::DataSet dataset_x(file.createDataSet("p", datatype_x, dataspace_x));
dataset_x.write(f->flow_x.data(), H5::PredType::NATIVE_UINT8);
dataset_x.close();

However, this only writes the one vector into the file, and additionally, I can't open the file in python (with pandas). It works with h5dump though.

Thanks for your help


Solution

  • I think I found the solution, although I'm not super happy with it because pandas (python) can't open it and I have to use h5py.

    However, here's my code. If you see any improvements, please let me know.

    #include "H5Cpp.h"
    
    H5::H5File file("/home/m/Desktop/tryout/file.h5", H5F_ACC_TRUNC);
    
    // Vector X
    hsize_t dims_pol[1] = {f->flow_x.size()};
    H5::DataSpace dataspace_x(1, dims_pol);
    H5::IntType datatype_x(H5::PredType::NATIVE_FLOAT);
    H5::DataSet dataset_x(file.createDataSet("x", datatype_x, dataspace_x));
    dataset_x.write(f->flow_x.data(), H5::PredType::NATIVE_FLOAT);
    dataset_x.close();
    
    // Vector Y
    H5::DataSpace dataspace_y(1, dims_pol);
    H5::IntType datatype_y(H5::PredType::NATIVE_FLOAT);
    H5::DataSet dataset_y(file.createDataSet("y", datatype_y, dataspace_y));
    dataset_y.write(f->flow_y.data(), H5::PredType::NATIVE_FLOAT);
    dataset_y.close();