Grid 0.7.0
Hdf5IO.cc
Go to the documentation of this file.
1/*************************************************************************************
2
3 Grid physics library, www.github.com/paboyle/Grid
4
5 Source file: ./Grid/serialisation/VectorUtils.h
6
7 Copyright (C) 2015
8
9 Author: Antonin Portelli <antonin.portelli@me.com>
10 Author: Peter Boyle <paboyle@ed.ac.uk>
11 Author: Guido Cossu <guido.cossu@ed.ac.uk>
12 Author: Michael Marshall <michael.marshall@ed.ac.uk>
13
14 This program is free software; you can redistribute it and/or modify
15 it under the terms of the GNU General Public License as published by
16 the Free Software Foundation; either version 2 of the License, or
17 (at your option) any later version.
18
19 This program is distributed in the hope that it will be useful,
20 but WITHOUT ANY WARRANTY; without even the implied warranty of
21 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
22 GNU General Public License for more details.
23
24 You should have received a copy of the GNU General Public License along
25 with this program; if not, write to the Free Software Foundation, Inc.,
26 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
27
28 See the full license in the file "LICENSE" in the top level distribution directory
29 *************************************************************************************/
30/* END LEGAL */
31
32#include <Grid/Grid.h>
33
34using namespace Grid;
35#ifndef H5_NO_NAMESPACE
36using namespace H5NS; // Compile error here? Try adding --enable-cxx to hdf5 configure
37#endif
38
39// Writer implementation ///////////////////////////////////////////////////////
40Hdf5Writer::Hdf5Writer(const std::string &fileName)
41: fileName_(fileName)
42, file_(fileName.c_str(), H5F_ACC_TRUNC)
43{
44 group_ = file_.openGroup("/");
47}
48
49void Hdf5Writer::push(const std::string &s)
50{
51 group_ = group_.createGroup(s);
52 path_.push_back(s);
53}
54
56{
57 path_.pop_back();
58 if (path_.empty())
59 {
60 group_ = file_.openGroup("/");
61 }
62 else
63 {
64 auto binOp = [](const std::string &a, const std::string &b)->std::string
65 {
66 return a + "/" + b;
67 };
68
69 group_ = group_.openGroup(std::accumulate(path_.begin(), path_.end(),
70 std::string(""), binOp));
71 }
72}
73
74template <>
75void Hdf5Writer::writeDefault(const std::string &s, const std::string &x)
76{
77 StrType strType(PredType::C_S1, x.size());
78
79 writeSingleAttribute(*(x.data()), s, strType);
80}
81
82void Hdf5Writer::writeDefault(const std::string &s, const char *x)
83{
84 std::string sx(x);
85
86 writeDefault(s, sx);
87}
88
90{
91 return group_;
92}
93
94// Reader implementation ///////////////////////////////////////////////////////
95Hdf5Reader::Hdf5Reader(const std::string &fileName, const bool readOnly)
96: fileName_(fileName)
97, file_(fileName.c_str(), readOnly ? H5F_ACC_RDONLY : H5F_ACC_RDWR)
98{
99 group_ = file_.openGroup("/");
102}
103
104bool Hdf5Reader::push(const std::string &s)
105{
106 group_ = group_.openGroup(s);
107 path_.push_back(s);
108
109 return true;
110}
111
113{
114 path_.pop_back();
115 if (path_.empty())
116 {
117 group_ = file_.openGroup("/");
118 }
119 else
120 {
121 auto binOp = [](const std::string &a, const std::string &b)->std::string
122 {
123 return a + "/" + b;
124 };
125
126 group_ = group_.openGroup(std::accumulate(path_.begin(), path_.end(),
127 std::string(""), binOp));
128 }
129}
130
131template <>
132void Hdf5Reader::readDefault(const std::string &s, std::string &x)
133{
134 Attribute attribute;
135
136 attribute = group_.openAttribute(s);
137 StrType strType = attribute.getStrType();
138
139 x.resize(strType.getSize());
140 attribute.read(strType, &(x[0]));
141}
142
144{
145 return group_;
146}
#define HDF5_GRID_GUARD
Definition Hdf5IO.h:49
#define H5NS
Definition Hdf5Type.h:11
std::string fileName_
Definition Hdf5IO.h:111
unsigned int dataSetThres_
Definition Hdf5IO.h:115
H5NS::H5File file_
Definition Hdf5IO.h:113
bool push(const std::string &s)
Definition Hdf5IO.cc:104
void readSingleAttribute(U &x, const std::string &name, const H5NS::DataType &type)
Definition Hdf5IO.h:247
H5NS::Group group_
Definition Hdf5IO.h:114
std::vector< std::string > path_
Definition Hdf5IO.h:112
void readDefault(const std::string &s, U &output)
Definition Hdf5IO.h:257
void pop(void)
Definition Hdf5IO.cc:112
H5NS::Group & getGroup(void)
Definition Hdf5IO.cc:143
Hdf5Reader(const std::string &fileName, const bool readOnly=true)
Definition Hdf5IO.cc:95
void pop(void)
Definition Hdf5IO.cc:55
const unsigned int dataSetThres_
Definition Hdf5IO.h:83
H5NS::Group & getGroup(void)
Definition Hdf5IO.cc:89
void writeDefault(const std::string &s, const char *x)
Definition Hdf5IO.cc:82
std::string fileName_
Definition Hdf5IO.h:79
H5NS::H5File file_
Definition Hdf5IO.h:81
Hdf5Writer(const std::string &fileName)
Definition Hdf5IO.cc:40
std::vector< std::string > path_
Definition Hdf5IO.h:80
void writeSingleAttribute(const U &x, const std::string &name, const H5NS::DataType &type)
Definition Hdf5IO.h:120
H5NS::Group group_
Definition Hdf5IO.h:82
void push(const std::string &s)
Definition Hdf5IO.cc:49