diff --git a/examples/hdfgroup/H5D/CMakeLists.txt b/examples/hdfgroup/H5D/CMakeLists.txt index c06676613f..424f583344 100644 --- a/examples/hdfgroup/H5D/CMakeLists.txt +++ b/examples/hdfgroup/H5D/CMakeLists.txt @@ -3,4 +3,7 @@ add_executable(h5ex_d_alloc h5ex_d_alloc.cpp) target_link_libraries(h5ex_d_alloc h5cpp) add_executable(h5ex_d_checksum h5ex_d_checksum.cpp) -target_link_libraries(h5ex_d_checksum h5cpp) \ No newline at end of file +target_link_libraries(h5ex_d_checksum h5cpp) + +add_executable(h5ex_d_chunk h5ex_d_chunk.cpp) +target_link_libraries(h5ex_d_chunk h5cpp) \ No newline at end of file diff --git a/examples/hdfgroup/H5D/h5ex_d_chunk.cpp b/examples/hdfgroup/H5D/h5ex_d_chunk.cpp new file mode 100644 index 0000000000..964f77d02d --- /dev/null +++ b/examples/hdfgroup/H5D/h5ex_d_chunk.cpp @@ -0,0 +1,105 @@ +/************************************************************ + + This example shows how to create a chunked dataset. The + program first writes integers in a hyperslab selection to + a chunked dataset with dataspace dimensions of DIM0xDIM1 + and chunk size of CHUNK0xCHUNK1, then closes the file. + Next, it reopens the file, reads back the data, and + outputs it to the screen. Finally it reads the data again + using a different hyperslab selection, and outputs + the result to the screen. + + This file is intended for use with HDF5 Library version 1.8 + + ************************************************************/ + +#include +#include +#include "matrix_hdf5.hpp" + +#define FILE "h5ex_d_chunk.h5" +#define DATASET "DS1" +#define DIM0 6 +#define DIM1 8 +#define CHUNK0 4 +#define CHUNK1 4 + +using namespace hdf5; + +using Int32Matrix = Matrix; + +void write_data() +{ + /* + * Create a new file using the default properties. + */ + + file::File file = file::create (FILE, file::AccessFlags::TRUNCATE); + + /* + * Create dataspace. Setting maximum size to NULL sets the maximum + * size to be the current size. + */ + dataspace::Simple file_space({ DIM0, DIM1 }); + + /* + * Create the dataset creation property list, and set the chunk + * size. + */ + property::DatasetCreationList dcpl; + dcpl.layout (property::DatasetLayout::CHUNKED); + dcpl.chunk (Dimensions{ CHUNK0, CHUNK1 }); + + /* + * Create the chunked dataset. + */ + property::LinkCreationList lcpl; + node::Dataset dset (file.root (), DATASET, datatype::create (), + file_space, lcpl, dcpl); + + /* + * Define and select the first part of the hyperslab selection. + */ + file_space.selection(dataspace::SelectionOperation::SET, + dataspace::Hyperslab({ 0, 0 },{ 2, 2 },{ 2, 3 },{ 3, 3 })); + file_space.selection(dataspace::SelectionOperation::NOTB, + dataspace::Hyperslab({ 0, 0 },{ 1, 1 },{ 2, 3 },{ 3, 3 })); + + + /* + * Write the data to the dataset. + */ + Int32Matrix matrix(1); + std::cout<<"Original data: "<(),file_space,file_space); + +} + +void read_data() +{ + file::File file = file::open(FILE,file::AccessFlags::READONLY); + node::Dataset dset = file.root()[DATASET]; + + property::DatasetCreationList dcpl = dset.creation_list(); + std::cout<<"Dataset layout: "<(),file_space,file_space); + std::cout<