Skip to content

Commit

Permalink
Added first enumeration example
Browse files Browse the repository at this point in the history
Update #318
  • Loading branch information
eugenwintersberger committed Jan 13, 2019
1 parent 949e0f6 commit c0c0991
Show file tree
Hide file tree
Showing 4 changed files with 186 additions and 1 deletion.
5 changes: 4 additions & 1 deletion examples/hdfgroup/H5T/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -5,4 +5,7 @@ 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)
target_link_libraries(h5ex_t_cmpd h5cpp)

add_executable(h5ex_t_enum h5ex_t_enum.cpp phase.cpp)
target_link_libraries(h5ex_t_enum h5cpp)
73 changes: 73 additions & 0 deletions examples/hdfgroup/H5T/h5ex_t_enum.cpp
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;
}
42 changes: 42 additions & 0 deletions examples/hdfgroup/H5T/phase.cpp
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;
}
}



67 changes: 67 additions & 0 deletions examples/hdfgroup/H5T/phase.hpp
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

0 comments on commit c0c0991

Please sign in to comment.