-
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
4 changed files
with
186 additions
and
1 deletion.
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
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,73 @@ | ||
/************************************************************ | ||
This example shows how to read and write enumerated | ||
datatypes to a dataset. The program first writes | ||
enumerated values to a dataset with a dataspace 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 "phase.hpp" | ||
#include "../H5D/matrix_hdf5.hpp" | ||
#include <vector> | ||
|
||
#define FILE "h5ex_t_enum.h5" | ||
#define DATASET "DS1" | ||
#define DIM0 4 | ||
#define DIM1 7 | ||
#define F_BASET H5T_STD_I16BE /* File base type */ | ||
#define M_BASET H5T_NATIVE_INT /* Memory base type */ | ||
#define NAME_BUF_SIZE 16 | ||
|
||
using namespace hdf5; | ||
using PhaseMatrix = Matrix<Phase,DIM0,DIM1>; | ||
|
||
void write_data() | ||
{ | ||
// Initialize data. | ||
PhaseMatrix phases; | ||
for (unsigned char i = 0; i < DIM0; i++) | ||
for (unsigned char j = 0; j < DIM1; j++) | ||
phases(i,j) = static_cast<Phase>(((i + 1) * j - j) % ((unsigned char) (Phase::PLASMA) + 1)); | ||
|
||
// 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 and write the enumerated data to it. | ||
node::Dataset dset(file.root(), DATASET, datatype::create<Phase>(), space); | ||
dset.write(phases); | ||
} | ||
|
||
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. This is a | ||
// two dimensional dataset so the dynamic allocation must be done | ||
// in steps. | ||
dataspace::Simple space = dset.dataspace(); | ||
|
||
// Read the data. | ||
PhaseMatrix phases; | ||
dset.read(phases); | ||
|
||
// Output the data to the screen. | ||
std::cout<<phases<<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,42 @@ | ||
// | ||
// (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 "phase.hpp" | ||
|
||
std::ostream &operator<<(std::ostream &stream,const Phase &phase) | ||
{ | ||
switch(phase) | ||
{ | ||
case Phase::SOLID: return stream<<"SOLID"; | ||
case Phase::LIQUID: return stream<<"LIQUID"; | ||
case Phase::GAS: return stream<<"GAS"; | ||
case Phase::PLASMA: return stream<<"PLASMA"; | ||
default: | ||
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,67 @@ | ||
// | ||
// (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 <h5cpp/hdf5.hpp> | ||
#include <h5cpp/datatype/enum.hpp> | ||
#include <iostream> | ||
#include <cstdint> | ||
|
||
enum class Phase : unsigned char { | ||
SOLID, | ||
LIQUID, | ||
GAS, | ||
PLASMA | ||
}; | ||
|
||
std::ostream &operator<<(std::ostream &stream,const Phase &phase); | ||
|
||
|
||
namespace hdf5 { | ||
namespace datatype { | ||
|
||
template<> | ||
class TypeTrait<Phase> | ||
{ | ||
public: | ||
using Type = Phase; | ||
using TypeClass = Enum; | ||
|
||
static TypeClass create(const Type& = Type()) | ||
{ | ||
Enum type = Enum::create(Phase()); | ||
type.insert("SOLID",Phase::SOLID); | ||
type.insert("LIQUID",Phase::LIQUID); | ||
type.insert("GAS",Phase::GAS); | ||
type.insert("PLASMA",Phase::PLASMA); | ||
|
||
return type; | ||
} | ||
|
||
}; | ||
|
||
} // end of namespace datatype | ||
} // end of namespace hdf5 |