简体   繁体   中英

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

One way to solve your issue could be through the usage of HDFql in C++ as follows:

// declare variables 'arr1' and 'arr2'
float arr1[240][320];
float arr2[240][320];

// populate variable 'arr1' with values

// populate variable 'arr2' with values

// register variable 'arr1' for subsequent usage (by HDFql)
HDFql::variableTransientRegister(&arr1);

// create dataset 'dset1' of data type float (size 240x320) populated with values from 'arr1'
HDFql::execute("create dataset dset1 as float(240, 320) values from memory 0");

// register variable 'arr2' for subsequent usage (by HDFql)
HDFql::variableTransientRegister(&arr2);

// create dataset 'dset2' of data type float (size 240x320) populated with values from 'arr2'
HDFql::execute("create dataset dset2 as float(240, 320) values from memory 0");

Additional info can be found in HDFql reference manual .

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();

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM