steven-varga / h5cpp

C++17 templates between [stl::vector | armadillo | eigen3 | ublas | blitz++] and HDF5 datasets
http://h5cpp.org
Other
142 stars 32 forks source link

Example Provided - Packet Table and Dataset Attributes #36

Closed ChrisDrozdowski closed 5 years ago

ChrisDrozdowski commented 5 years ago

Below is an example of how one can add attributes to a pack table-generated dataset. This tripped me up for a bit, so an example may be useful to others.

Please adapt as you see fit to work with packet-table example.

I didn't add to the packet-table example myself because it needs to be tested in Linux which I can't currently do.

The important points are creating ds_t, adding attributes to it, then casting to pt_t for data appending.

#include <h5cpp/all>
#include <cstddef>
#include <vector>

int main(){

    const size_t count = 2048;
    const std::size_t chunk_size = 1024;

    try
    {
        h5::fd_t fd = h5::create("example.h5", H5F_ACC_TRUNC);

        // Create a dataset.
        h5::ds_t ds = h5::create<double>(fd, "some_path", h5::max_dims{ H5S_UNLIMITED },
            h5::chunk{ chunk_size } | h5::gzip{ 6 } | h5::fill_value<double>(0));

        // Add attributes to dataset.
        ds["attr1"] = 1.23;
        ds["attr2"] = "string";

        // Cast dataset to packet table.
        h5::pt_t pt = static_cast<h5::pt_t>(ds);

        // Append data to packet table.
        for (std::size_t i = 0; i < count; ++i)
            h5::append(pt, i * 0.999);

    }
    catch (const std::exception& e)
    {
        std::cout << e.what() << std::endl;
    }
}
steven-varga commented 5 years ago

Correct. Packet table is a dataset plus some extra code for chunk size buffering of data. Once the chunk/bucket is full it gets dumped to actual dataset. The last remaining part gets flushed when h5::pt_t pt goes out of scope, and resources get closed according to RAII.

Conversion from packet table to dataset should also work.