diff --git a/examples/hdfgroup/H5D/CMakeLists.txt b/examples/hdfgroup/H5D/CMakeLists.txt index c764c493ea..a99a1a557c 100644 --- a/examples/hdfgroup/H5D/CMakeLists.txt +++ b/examples/hdfgroup/H5D/CMakeLists.txt @@ -12,4 +12,19 @@ add_executable(h5ex_d_compact h5ex_d_compact.cpp) target_link_libraries(h5ex_d_compact h5cpp) add_executable(h5ex_d_fillval h5ex_d_fillval.cpp) -target_link_libraries(h5ex_d_fillval h5cpp) \ No newline at end of file +target_link_libraries(h5ex_d_fillval h5cpp) + +add_executable(h5ex_d_hyper h5ex_d_hyper.cpp) +target_link_libraries(h5ex_d_hyper h5cpp) + +add_executable(h5ex_d_rdwr h5ex_d_rdwr.cpp) +target_link_libraries(h5ex_d_rdwr h5cpp) + +add_executable(h5ex_d_shuffle h5ex_d_shuffle.cpp) +target_link_libraries(h5ex_d_shuffle h5cpp) + +add_executable(h5ex_d_transform h5ex_d_transform.cpp) +target_link_libraries(h5ex_d_transform h5cpp) + +add_executable(h5ex_d_unlimadd h5ex_d_unlimadd.cpp) +target_link_libraries(h5ex_d_unlimadd h5cpp) \ No newline at end of file diff --git a/examples/hdfgroup/H5D/h5ex_d_gzip.cpp b/examples/hdfgroup/H5D/h5ex_d_gzip.cpp new file mode 100644 index 0000000000..c9f579fafb --- /dev/null +++ b/examples/hdfgroup/H5D/h5ex_d_gzip.cpp @@ -0,0 +1,181 @@ +/************************************************************ + + This example shows how to read and write data to a dataset + using gzip compression (also called zlib or deflate). The + program first checks if gzip compression is available, + then if it is it writes integers to a dataset using gzip, + then closes the file. Next, it reopens the file, reads + back the data, and outputs the type of compression and the + maximum value in the dataset to the screen. + + This file is intended for use with HDF5 Library version 1.8 + + ************************************************************/ + +#include "hdf5.h" +#include +#include + +#define FILE "h5ex_d_gzip.h5" +#define DATASET "DS1" +#define DIM0 32 +#define DIM1 64 +#define CHUNK0 4 +#define CHUNK1 8 + +int +main (void) +{ + hid_t file, space, dset, dcpl; /* Handles */ + herr_t status; + htri_t avail; + H5Z_filter_t filter_type; + hsize_t dims[2] = {DIM0, DIM1}, + chunk[2] = {CHUNK0, CHUNK1}; + size_t nelmts; + unsigned int flags, + filter_info; + int wdata[DIM0][DIM1], /* Write buffer */ + rdata[DIM0][DIM1], /* Read buffer */ + max, + i, j; + + /* + * Check if gzip compression is available and can be used for both + * compression and decompression. Normally we do not perform error + * checking in these examples for the sake of clarity, but in this + * case we will make an exception because this filter is an + * optional part of the hdf5 library. + */ + avail = H5Zfilter_avail(H5Z_FILTER_DEFLATE); + if (!avail) { + printf ("gzip filter not available.\n"); + return 1; + } + status = H5Zget_filter_info (H5Z_FILTER_DEFLATE, &filter_info); + if ( !(filter_info & H5Z_FILTER_CONFIG_ENCODE_ENABLED) || + !(filter_info & H5Z_FILTER_CONFIG_DECODE_ENABLED) ) { + printf ("gzip filter not available for encoding and decoding.\n"); + return 1; + } + + /* + * Initialize data. + */ + for (i=0; i +#include "matrix_hdf5.hpp" +#include + +#define FILE "h5ex_d_hyper.h5" +#define DATASET "DS1" +#define DIM0 6 +#define DIM1 8 + +using Int32Matrix = Matrix; +using namespace hdf5; + +void write () +{ + Int32Matrix wdata; + std::fill (wdata.begin (), wdata.end (), 1); + + // Print the data to the screen. + std::cout << "Original Data:" << std::endl; + std::cout << wdata << std::endl; + + // 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. We will use all default properties for this + // example. + node::Dataset dset (file.root (), DATASET, datatype::create (), + space); + + // Define and select the first part of the hyperslab selection. + dataspace::Hyperslab hyperslab1 ( { 0, 0 }, //offset + { 2, 2 }, //block + { 2, 3 }, //count + { 3, 3 }); //stride + + // Define and select the second part of the hyperslab selection. + dataspace::Hyperslab hyperslab2 ( { 0, 0 }, //offset + { 1, 1 }, //block + { 2, 3 }, //count + { 3, 3 }); //stride + + //apply the hyperslabs to the dataspace + space.selection (dataspace::SelectionOperation::SET, hyperslab1); + space.selection (dataspace::SelectionOperation::NOTB, hyperslab2); + + + //Write the data to the dataset. + dset.write (wdata, datatype::create (wdata), space, + space); +} + +void read() +{ + // Open file and dataset using the default properties. + file::File file = file::open(FILE,file::AccessFlags::READONLY); + node::Dataset dset = file.root().nodes[DATASET]; + + + // Read the data using the default properties. + Int32Matrix rdata; + dset.read(rdata); + + // Output the data to the screen. + + std::cout<<"Data as written to disk by hyberslabs:"< +#include "matrix_hdf5.hpp" + +#define FILE "h5ex_d_rdwr.h5" +#define DATASET "DS1" +#define DIM0 4 +#define DIM1 7 + +using Int32Matrix = Matrix; +using namespace hdf5; + +void write() +{ + Int32Matrix wdata; + + // Initialize data. + for (int32_t i = 0; i < DIM0; i++) + for (int32_t j = 0; j < DIM1; j++) + wdata (i, j) = i * j - j; + + + // 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. We will use all default properties for this + // example. + std::cout<<"Data to write:"< +#include "matrix_hdf5.hpp" +#include + +#define FILE "h5ex_d_shuffle.h5" +#define DATASET "DS1" +#define DIM0 32 +#define DIM1 64 +#define CHUNK0 4 +#define CHUNK1 8 + +using namespace hdf5; +using Int32Matrix = Matrix; + +void write() +{ + filter::Shuffle shuffle_filter; + filter::Deflate deflate_filter; + deflate_filter.level(9); + + Int32Matrix wdata; + // Initialize data. + for (size_t i=0; i (), + space, lcpl, dcpl); + + // Write the data to the dataset. + dset.write (wdata); +} + +void read() +{ + // Open file and dataset using the default properties. + file::File file = file::open (FILE,file::AccessFlags::READONLY); + node::Dataset dset = file.root().nodes[DATASET]; + + // Retrieve dataset creation property list + property::DatasetCreationList dcpl = dset.creation_list(); + + // Retrieve the number of filters, and retrieve and print the + // type of each. + unsigned int flags,filter_info; + int nfilters = H5Pget_nfilters (static_cast(dcpl)); + for (int i=0; i(dcpl), i, &flags, &nelmts, NULL, 0, NULL, + &filter_info); + std::cout<< "Filter "< +#include "matrix_hdf5.hpp" + +#define FILE "h5ex_d_transform.h5" +#define DATASET "DS1" +#define DIM0 4 +#define DIM1 7 +#define TRANSFORM "x+1" +#define RTRANSFORM "x-1" + +using namespace hdf5; +using Int32Matrix = Matrix; + +void write() +{ + // Initialize data. + Int32Matrix wdata; + for (int32_t i=0; i(); + + // Create the dataset transfer property list and define the + // transform expression. + property::DatasetTransferList dxpl; + dxpl.data_transform(TRANSFORM); + + // Create the dataset using the default properties. Unfortunately + // we must save as a native type or the transform operation will + // fail. + node::Dataset dset(file.root(), DATASET, memory_type,memory_space); + + + // Write the data to the dataset using the dataset transfer + // property list. + dset.write(wdata,memory_type,memory_space,memory_space,dxpl); +} + +void read() +{ + // Open file and dataset using the default properties. + file::File file = file::open (FILE, file::AccessFlags::READONLY); + node::Dataset dset = file.root().nodes[DATASET]; + + // Read the data using the default properties. + Int32Matrix rdata; + dset.read(rdata); + + // Output the data to the screen. + std::cout< +#include "matrix_hdf5.hpp" +#include + + +#define FILE "h5ex_d_unlimadd.h5" +#define DATASET "DS1" +#define DIM0 4 +#define DIM1 7 +#define EDIM0 6 +#define EDIM1 10 +#define CHUNK0 4 +#define CHUNK1 4 + +using namespace hdf5; +using Int32Matrix = Matrix; +using Int32MatrixExtended = Matrix; + +void write_initial_data() +{ + // Initialize data. + Int32Matrix wdata; + for (int32_t i=0; i(), space,lcpl, dcpl); + + // Write the data to the dataset. + dset.write(wdata); +} + +void append_data() +{ + // Open file and dataset using the default properties. + file::File file = file::open (FILE, file::AccessFlags::READWRITE); + node::Dataset dset = file.root().nodes[DATASET]; + + // Get dataspace and allocate memory for read buffer. This is a + // two dimensional dataset so the dynamic allocation must be done + // in steps. + dataspace::Simple space = dset.dataspace(); + + // Read the data using the default properties. + Int32Matrix rdata; + dset.read(rdata); + + // Output the data to the screen. + std::cout<<"Dataset before extension:"<