Skip to content

Commit

Permalink
Added compact example
Browse files Browse the repository at this point in the history
Update #318
  • Loading branch information
eugenwintersberger committed Dec 29, 2018
1 parent 8fb0dbb commit 96ebc09
Show file tree
Hide file tree
Showing 2 changed files with 105 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 @@ -6,4 +6,7 @@ add_executable(h5ex_d_checksum h5ex_d_checksum.cpp)
target_link_libraries(h5ex_d_checksum h5cpp)

add_executable(h5ex_d_chunk h5ex_d_chunk.cpp)
target_link_libraries(h5ex_d_chunk h5cpp)
target_link_libraries(h5ex_d_chunk h5cpp)

add_executable(h5ex_d_compact h5ex_d_compact.cpp)
target_link_libraries(h5ex_d_compact h5cpp)
101 changes: 101 additions & 0 deletions examples/hdfgroup/H5D/h5ex_d_compact.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,101 @@
/************************************************************
This example shows how to read and write data to a compact
dataset. The program first writes integers to a compact
dataset with dataspace dimensions of DIM0xDIM1, then
closes the file. Next, it reopens the file, reads back
the data, and outputs it to the screen.
This file is intended for use with HDF5 Library version 1.8
************************************************************/

#include <h5cpp/hdf5.hpp>
#include "matrix_hdf5.hpp"

#define FILE "h5ex_d_compact.h5"
#define DATASET "DS1"
#define DIM0 4
#define DIM1 7

using Int32Matrix = Matrix<int32_t,DIM0,DIM1>;

void write_data()
{
using namespace hdf5;

//
// 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 space({DIM0,DIM1});

//
// Create the dataset creation property list, set the layout to
// compact.
//
property::LinkCreationList lcpl;
property::DatasetCreationList dcpl;
dcpl.layout(property::DatasetLayout::COMPACT);

//
// Create the dataset. We will use all default properties for this
// example.
//
node::Dataset dset(file.root(), DATASET, datatype::create<Int32Matrix>(),
space,lcpl, dcpl);

//
// Initialize data
//
Int32Matrix data;
for (size_t i=0; i<DIM0; i++)
for (size_t j=0; j<DIM1; j++)
data(i,j) = i * j - j;

//
// write data to disk
//
dset.write(data);

std::cout<<"Data written to disk: "<<std::endl<<data<<std::endl;

}

void read_data()
{
using namespace hdf5;

//
// Open file and dataset using the default properties.
//
file::File file = file::open(FILE,file::AccessFlags::READONLY);
node::Dataset dset = file.root()[DATASET];

//
// Retrieve the dataset creation property list, and print the
// storage layout.
//
property::DatasetCreationList dcpl = dset.creation_list();
std::cout<<"Storage layout is: "<<dcpl.layout()<<std::endl<<std::endl;

Int32Matrix data;
dset.read(data);

std::cout<<"Read data: "<<std::endl<<data<<std::endl;

}

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

return 0;
}

0 comments on commit 96ebc09

Please sign in to comment.