Skip to content

Commit

Permalink
Added chunk example
Browse files Browse the repository at this point in the history
Update #318
  • Loading branch information
eugenwintersberger committed Dec 29, 2018
1 parent 48d1ac2 commit 8fb0dbb
Show file tree
Hide file tree
Showing 2 changed files with 109 additions and 1 deletion.
5 changes: 4 additions & 1 deletion examples/hdfgroup/H5D/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -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)
target_link_libraries(h5ex_d_checksum h5cpp)

add_executable(h5ex_d_chunk h5ex_d_chunk.cpp)
target_link_libraries(h5ex_d_chunk h5cpp)
105 changes: 105 additions & 0 deletions examples/hdfgroup/H5D/h5ex_d_chunk.cpp
Original file line number Diff line number Diff line change
@@ -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 <h5cpp/hdf5.hpp>
#include <array>
#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<int32_t,DIM0,DIM1>;

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<int32_t> (),
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: "<<std::endl<<matrix<<std::endl;

dset.write (matrix,datatype::create<Int32Matrix>(),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: "<<dcpl.layout()<<std::endl;

Int32Matrix matrix;
dset.read(matrix);
std::cout<<std::endl<<matrix<<std::endl;

matrix = Int32Matrix(0); //reinit array
dataspace::Simple file_space = dset.dataspace();
file_space.selection(dataspace::SelectionOperation::SET,dataspace::Hyperslab({0,1},{2,3},{2,2},{4,4}));
dset.read(matrix,datatype::create<Int32Matrix>(),file_space,file_space);
std::cout<<matrix<<std::endl;
}

int
main (void)
{
write_data();
read_data();

return 0;
}

0 comments on commit 8fb0dbb

Please sign in to comment.