-
Notifications
You must be signed in to change notification settings - Fork 20
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Showing
9 changed files
with
565 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
add_executable(h5ex_t_array h5ex_t_array.cpp) | ||
target_link_libraries(h5ex_t_array h5cpp) | ||
|
||
add_executable(h5ex_t_arrayatt h5ex_t_arrayatt.cpp) | ||
target_link_libraries(h5ex_t_arrayatt h5cpp) | ||
|
||
add_executable(h5ex_t_cmpd h5ex_t_cmpd.cpp measurement.cpp) | ||
target_link_libraries(h5ex_t_cmpd h5cpp) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,97 @@ | ||
/************************************************************ | ||
This example shows how to read and write array datatypes | ||
to a dataset. The program first writes integers arrays of | ||
dimension ADIM0xADIM1 to a dataset with a dataspace of | ||
DIM0, 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 <vector> | ||
#include <algorithm> | ||
#include "t_array.hpp" | ||
|
||
#define FILE "h5ex_t_array.h5" | ||
#define DATASET "DS1" | ||
|
||
#define DIM0 4 | ||
#define ADIM0 3 | ||
#define ADIM1 5 | ||
|
||
using namespace hdf5; | ||
using Int32Item = DataItem<int32_t,ADIM0,ADIM1>; | ||
using Int32Items = std::vector<Int32Item>; | ||
|
||
void write_data() | ||
{ | ||
// Initialize data. i is the element in the dataspace, j and k the | ||
// elements within the array datatype. | ||
Int32Items wdata(DIM0); | ||
for (int32_t i=0; i<DIM0; i++) | ||
for (int32_t j=0; j<ADIM0; j++) | ||
for (int32_t k=0; k<ADIM1; k++) | ||
wdata[i](j,k) = i * j - j * k + i * k; | ||
|
||
// Create a new file using the default properties. | ||
file::File file = file::create (FILE, file::AccessFlags::TRUNCATE); | ||
|
||
// Create array datatypes for file and memory. | ||
datatype::Array filetype = datatype::Array::create(datatype::create<int64_t>(),{ADIM0,ADIM1}); | ||
|
||
// Create dataspace. Setting maximum size to NULL sets the maximum | ||
// size to be the current size. | ||
dataspace::Simple space({DIM0}); | ||
|
||
// Create the dataset and write the array data to it. | ||
node::Dataset dset(file.root(), DATASET, filetype, space); | ||
std::cout<<"Write data"<<std::endl; | ||
std::for_each(wdata.begin(),wdata.end(), | ||
[](const Int32Item &item) { std::cout<<item<<std::endl; } | ||
); | ||
|
||
dset.write(wdata); | ||
} | ||
|
||
void read_data() | ||
{ | ||
// Open file and dataset. | ||
file::File file = file::open (FILE, file::AccessFlags::READONLY); | ||
node::Dataset dset = file.root().nodes[DATASET]; | ||
|
||
// Get the datatype and its dimensions. | ||
datatype::Array filetype = dset.datatype (); | ||
|
||
// Get dataspace and allocate memory for read buffer. This is a | ||
// three dimensional dataset when the array datatype is included so | ||
// the dynamic allocation must be done in steps. | ||
dataspace::Simple space = dset.dataspace (); | ||
|
||
// Allocate array of pointers to two-dimensional arrays (the | ||
// elements of the dataset. | ||
Int32Items rdata(space.size()); | ||
|
||
// Read the data. | ||
dset.read(rdata); | ||
|
||
// Output the data to the screen. | ||
std::for_each(rdata.begin(),rdata.end(), | ||
[](const Int32Item &item) { std::cout<<item<<std::endl; }); | ||
} | ||
|
||
int main (void) | ||
{ | ||
write_data(); | ||
|
||
// Now we begin the read section of this example. Here we assume | ||
// the dataset and array have the same name and rank, but can have | ||
// any size. Therefore we must allocate a new array to read in | ||
// data using malloc(). | ||
std::cout<<"Read data"<<std::endl; | ||
read_data(); | ||
|
||
return 0; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,85 @@ | ||
/************************************************************ | ||
This example shows how to read and write array datatypes | ||
to an attribute. The program first writes integers arrays | ||
of dimension ADIM0xADIM1 to an attribute with a dataspace | ||
of DIM0, 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 <vector> | ||
#include <algorithm> | ||
#include "t_array.hpp" | ||
|
||
#define FILE "h5ex_t_arrayatt.h5" | ||
#define DATASET "DS1" | ||
#define ATTRIBUTE "A1" | ||
#define DIM0 4 | ||
#define ADIM0 3 | ||
#define ADIM1 5 | ||
|
||
using namespace hdf5; | ||
using Int32Item = DataItem<int32_t,ADIM0,ADIM1>; | ||
using Int32Items = std::vector<Int32Item>; | ||
|
||
void write_attribute() | ||
{ | ||
// Initialize data. i is the element in the dataspace, j and k the | ||
// elements within the array datatype. | ||
Int32Items wdata(DIM0); | ||
for (int32_t i = 0; i < DIM0; i++) | ||
for (int32_t j = 0; j < ADIM0; j++) | ||
for (int32_t k = 0; k < ADIM1; k++) | ||
wdata[i](j,k) = i * j - j * k + i * k; | ||
|
||
|
||
// Create a new file using the default properties. | ||
file::File file = file::create (FILE, file::AccessFlags::TRUNCATE); | ||
|
||
// Create array datatypes for file and memory. | ||
datatype::Array filetype = datatype::Array::create (datatype::create<int>(),{ADIM0,ADIM1}); | ||
|
||
// Create dataspace. Setting maximum size to NULL sets the maximum | ||
// size to be the current size. | ||
dataspace::Simple space({DIM0}); | ||
|
||
// Create the attribute and write the array data to it. | ||
attribute::Attribute attribute = file.root().attributes.create(ATTRIBUTE,filetype,space); | ||
attribute.write(wdata); | ||
} | ||
|
||
void read_attribute() | ||
{ | ||
// Open file, dataset, and attribute. | ||
file::File file = file::open (FILE, file::AccessFlags::READONLY); | ||
attribute::Attribute attribute = file.root().attributes[ATTRIBUTE]; | ||
|
||
// Get the datatype and its dimensions. | ||
datatype::Array filetype = attribute.datatype(); | ||
|
||
// Get dataspace and allocate memory for read buffer. This is a | ||
// three dimensional attribute when the array datatype is included | ||
// so the dynamic allocation must be done in steps. | ||
dataspace::Simple space = attribute.dataspace(); | ||
|
||
// Allocate array of pointers to two-dimensional arrays (the | ||
// elements of the attribute. | ||
Int32Items rdata(space.size()); | ||
|
||
// Read the data. | ||
attribute.read(rdata); | ||
std::for_each(rdata.begin(),rdata.end(), | ||
[](const Int32Item &item) { std::cout<<item<<std::endl;}); | ||
} | ||
|
||
int main (void) | ||
{ | ||
write_attribute(); | ||
read_attribute(); | ||
|
||
return 0; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,76 @@ | ||
/************************************************************ | ||
This example shows how to read and write compound | ||
datatypes to a dataset. The program first writes | ||
compound structures to a dataset with a dataspace of DIM0, | ||
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 <vector> | ||
#include <algorithm> | ||
#include "measurement_hdf5.hpp" | ||
|
||
#define FILE "h5ex_t_cmpd.h5" | ||
#define DATASET "DS1" | ||
#define DIM0 4 | ||
|
||
using namespace hdf5; | ||
using Measurements = std::vector<Measurement>; | ||
|
||
void write_data() | ||
{ | ||
Measurements measurements{ | ||
Measurement(1153,"Exterior (static)",53.23,24.57), | ||
Measurement(1184,"Intake",55.12,22.95), | ||
Measurement(1027,"Intake manifold",103.55,31.23), | ||
Measurement(1313,"Exhaust manifold",1252.89,84.11) | ||
}; | ||
|
||
// 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}); | ||
|
||
// Create the dataset and write the compound data to it. | ||
node::Dataset dset(file.root(), DATASET, datatype::create<Measurement>(), space); | ||
dset.write(measurements); | ||
} | ||
|
||
void read_data() | ||
{ | ||
/* | ||
* Open file and dataset. | ||
*/ | ||
file::File file = file::open (FILE, file::AccessFlags::READONLY); | ||
node::Dataset dset = file.root().nodes[DATASET]; | ||
|
||
/* | ||
* Get dataspace and allocate memory for read buffer. | ||
*/ | ||
dataspace::Simple file_space = dset.dataspace(); | ||
Measurements measurements(file_space.size()); | ||
|
||
/* | ||
* Read the data. | ||
*/ | ||
dset.read(measurements); | ||
std::for_each(measurements.begin(),measurements.end(), | ||
[](const Measurement &measurement) { std::cout<<measurement<<std::endl; } | ||
); | ||
|
||
} | ||
|
||
int main (void) | ||
{ | ||
write_data(); | ||
read_data(); | ||
|
||
return 0; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,65 @@ | ||
// | ||
// (c) Copyright 2019 DESY,ESS, Eugen Wintersberger <[email protected]> | ||
// | ||
// This file is part of h5pp. | ||
// | ||
// This library is free software; you can redistribute it and/or modify it | ||
// under the terms of the GNU Lesser General Public License as published | ||
// by the Free Software Foundation; either version 2.1 of the License, or | ||
// (at your option) any later version. | ||
// | ||
// This library is distributed in the hope that it will be useful, but | ||
// WITHOUT ANY WARRANTY; without even the implied warranty ofMERCHANTABILITY | ||
// or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public | ||
// License for more details. | ||
// | ||
// You should have received a copy of the GNU Lesser General Public License | ||
// along with this library; if not, write to the | ||
// Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor | ||
// Boston, MA 02110-1301 USA | ||
// =========================================================================== | ||
// | ||
// Author: Eugen Wintersberger <[email protected]> | ||
// Created on: Jan 13, 2019 | ||
// | ||
|
||
#include "measurement.hpp" | ||
|
||
Measurement::Measurement (int serial_no, const char *location, | ||
double temperature, double pressure) : | ||
serial_no_ (serial_no), location_ (const_cast<char*>(location)), temperature_ (temperature), pressure_ ( | ||
pressure) | ||
{ | ||
} | ||
|
||
int Measurement::serial_no () const noexcept | ||
{ | ||
return serial_no_; | ||
} | ||
|
||
std::string Measurement::location () const | ||
{ | ||
return location_; | ||
} | ||
|
||
double Measurement::temperature () const noexcept | ||
{ | ||
return temperature_; | ||
} | ||
|
||
double Measurement::pressure () const noexcept | ||
{ | ||
return pressure_; | ||
} | ||
|
||
std::ostream &operator<<(std::ostream &stream,const Measurement &measurement) | ||
{ | ||
stream<<"Serial number: "<<measurement.serial_no()<<", "; | ||
stream<<"Location: "<<measurement.location()<<", "; | ||
stream<<"Temperature: "<<measurement.temperature()<<", "; | ||
stream<<"Pressure: "<<measurement.pressure(); | ||
|
||
return stream; | ||
|
||
} | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,48 @@ | ||
// | ||
// (c) Copyright 2019 DESY,ESS, Eugen Wintersberger <[email protected]> | ||
// | ||
// This file is part of h5pp. | ||
// | ||
// This library is free software; you can redistribute it and/or modify it | ||
// under the terms of the GNU Lesser General Public License as published | ||
// by the Free Software Foundation; either version 2.1 of the License, or | ||
// (at your option) any later version. | ||
// | ||
// This library is distributed in the hope that it will be useful, but | ||
// WITHOUT ANY WARRANTY; without even the implied warranty ofMERCHANTABILITY | ||
// or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public | ||
// License for more details. | ||
// | ||
// You should have received a copy of the GNU Lesser General Public License | ||
// along with this library; if not, write to the | ||
// Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor | ||
// Boston, MA 02110-1301 USA | ||
// =========================================================================== | ||
// | ||
// Author: Eugen Wintersberger <[email protected]> | ||
// Created on: Jan 13, 2019 | ||
// | ||
|
||
#pragma once | ||
|
||
#include <string> | ||
#include <iostream> | ||
|
||
class Measurement | ||
{ | ||
private: | ||
int serial_no_; | ||
char* location_; | ||
double temperature_; | ||
double pressure_; | ||
public: | ||
Measurement() = default; | ||
Measurement(int serial_no,const char *location,double temperature,double pressure); | ||
|
||
int serial_no() const noexcept; | ||
std::string location() const; | ||
double temperature() const noexcept; | ||
double pressure() const noexcept; | ||
}; | ||
|
||
std::ostream &operator<<(std::ostream &stream,const Measurement &measurement); |
Oops, something went wrong.