ess-dmsc / h5cpp

C++ wrapper for the HDF5 C-library
https://ess-dmsc.github.io/h5cpp/
GNU Lesser General Public License v2.1
116 stars 19 forks source link

Problem of writing a list of 2D arrays into one Dataset. #628

Closed ZL-Su closed 1 year ago

ZL-Su commented 1 year ago

Hi,

I'm trying to write a set of 2D arrays into a dataset, but an error is reported as follows: "Failure to write contiguous data to dataset [/data]!" Could anyone provide an impl. to write the 2D array data? Thx!

The code I used is attached:

        auto mat = cv::Mat_<float>(5, 20);
        using namespace hdf5;
        auto lc_list = property::LinkCreationList();
        auto dc_list = property::DatasetCreationList();
        dc_list.layout(property::DatasetLayout::Chunked);
        dc_list.chunk({ size_t(mat.rows), size_t(mat.cols), 100});

        const auto hpath = std::filesystem::path(path.toStdString() + "/data.h5");
        using std::filesystem::exists;
        auto f = file::create(hpath, file::AccessFlags::Truncate);
        auto root_group = f.root();

        dataspace::Simple space{ {0,0,0},{size_t(mat.rows), size_t(mat.cols), dataspace::Simple::unlimited} };
        auto type = datatype::create(mat);
        auto dataset = node::Dataset(root_group, "data", type, space, lc_list, dc_list);

        dataspace::Hyperslab sel({ 0,0,0 }, { size_t(mat.rows), size_t(mat.cols), 1 }, { 1,1,1 });
        for (auto i = 0; i < 10; ++i) {
            mat = float(i);
            dataset.extent(2, 1);
            dataset.write(mat, sel);
            sel.offset(2, i);
        }
        f.close();
ZL-Su commented 1 year ago

Solved.