JEMRIS  2.8.1
open-source MRI simulations
HDF5IO.h
Go to the documentation of this file.
1 
5 /*
6  * JEMRIS Copyright (C)
7  * 2006-2015 Tony Stoecker
8  * 2007-2015 Kaveh Vahedipour
9  * 2009-2015 Daniel Pflugfelder
10  *
11  * This program is free software; you can redistribute it and/or modify
12  * it under the terms of the GNU General Public License as published by
13  * the Free Software Foundation; either version 2 of the License, or
14  * (at your option) any later version.
15  *
16  * This program is distributed in the hope that it will be useful,
17  * but WITHOUT ANY WARRANTY; without even the implied warranty of
18  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19  * GNU General Public License for more details.
20  *
21  * You should have received a copy of the GNU General Public License
22  * along with this program; if not, write to the Free Software
23  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
24  */
25 
26 #ifndef HDF5_IO_H_
27 #define HDF5_IO_H_
28 
29 #include "BinaryIO.h"
30 
31 //#define VERBOSE
32 
33 #include <H5Cpp.h>
34 #include <algorithm>
35 #include <string.h>
36 
37 template<class T> struct HDF5Types;
38 
39 template<> struct HDF5Types<size_t> {
40  static const H5::DataType Type() {
41  return H5::PredType::NATIVE_ULONG;
42  }
43 };
44 template<> struct HDF5Types<double> {
45  static const H5::DataType Type() {
46  return H5::PredType::NATIVE_DOUBLE;
47  }
48 };
49 template<> struct HDF5Types<float> {
50  static const H5::DataType Type() {
51  return H5::PredType::NATIVE_FLOAT;
52  }
53 };
54 template<> struct HDF5Types<int> {
55  static const H5::DataType Type() {
56  return H5::PredType::NATIVE_INT;
57  }
58 };
59 template<> struct HDF5Types<long> {
60  static const H5::DataType Type() {
61  return H5::PredType::NATIVE_LONG;
62  }
63 };
64 
68 class HDF5IO : public BinaryIO {
69 
70 public:
71 
75  HDF5IO (const std::string& fname, const IO::Mode mode) {
76  m_fname = fname;
77  m_mode = mode;
78  m_type = IO::HDF5;
79  this->FileAccess();
80  }
81 
85  virtual ~HDF5IO () {
86  m_file.close();
87  }
88 
94  template<class T> IO::Status
95  Write (const NDData<T>& data, const std::string& urn, const std::string& url = "") {
96 
97  try {
98 
99 #ifndef VERBOSE
100  H5::Exception::dontPrint();
101 #endif
102 
103  std::vector<hsize_t> dims(data.NDim());
104  for (int i = 0; i < data.NDim(); i++)
105  dims[i] = data.Dim(i);
106 
107  //std::reverse(dims.begin(),dims.end());
108 
109  H5::Group group;
110 
111  try {
112  group = m_file.openGroup(url);
113 #ifdef VERBOSE
114  printf ("Group %s opened for writing\n", url.c_str()) ;
115 #endif
116  } catch (const H5::Exception& e) {
117  group = CreateGroup (url);
118  }
119 
120  H5::DataSpace dspace (data.NDim(), dims.data());
121  H5::DataType dtype (HDF5Types<T>::Type());
122  H5::DataSet dset = group.createDataSet(urn, dtype, dspace);
123 
124  dset.write(data.Ptr(), dtype);
125  dset.close();
126  dspace.close();
127  group.close();
128 
129  } catch (const H5::FileIException& e) {
130  return ReportException (e, IO::HDF5_FILE_I_EXCEPTION);
131  } catch (const H5::GroupIException& e) {
132  return ReportException (e, IO::HDF5_FILE_I_EXCEPTION);
133  } catch (const H5::DataSetIException& e) {
134  return ReportException (e, IO::HDF5_DATASET_I_EXCEPTION);
135  } catch (const H5::DataSpaceIException& e) {
136  return ReportException (e, IO::HDF5_DATASPACE_I_EXCEPTION);
137  } catch (const H5::DataTypeIException& e) {
138  return ReportException (e, IO::HDF5_DATATYPE_I_EXCEPTION);
139  }
140 
141  return IO::OK;
142 
143  }
144 
145 
146 
147  template<class T> IO::Status
148  Read (NDData<T>& data, const std::string& urn, const std::string& url = "") {
149 
150  try {
151 
152 #ifndef VERBOSE
153  H5::Exception::dontPrint();
154 #endif
155  H5::DataSet dset = m_file.openDataSet(URI(url,urn));
156  H5::DataType dtype = dset.getFloatType();
157  H5::DataSpace dspace = dset.getSpace();
158  std::vector<hsize_t> dims (dspace.getSimpleExtentNdims());
159  size_t ndim = dspace.getSimpleExtentDims(&dims[0], NULL);
160  data = NDData<T> (dims);
161 
162  dset.read(data.Ptr(), HDF5Types<T>::Type());
163  dspace.close();
164  dset.close();
165 
166  } catch (const H5::FileIException& e) {
167  return ReportException (e, IO::HDF5_FILE_I_EXCEPTION);
168  } catch (const H5::DataSetIException& e) {
169  return ReportException (e, IO::HDF5_DATASET_I_EXCEPTION);
170  } catch (const H5::DataSpaceIException& e) {
171  return ReportException (e, IO::HDF5_DATASPACE_I_EXCEPTION);
172  } catch (const H5::DataTypeIException& e) {
173  return ReportException (e, IO::HDF5_DATATYPE_I_EXCEPTION);
174  }
175 
176  return IO::OK;
177 
178  }
179 
180  virtual IO::Status
182 
183  if (m_status != IO::OK)
184  return m_status;
185 
186  if (m_mode == IO::IN) {
187  try {
188  m_file = H5::H5File (m_fname, H5F_ACC_RDONLY);
189  } catch (const H5::FileIException& e) {
190  printf ("\n Error: cannot open file %s!", m_fname.c_str());
191  m_status = IO::FILE_NOT_FOUND;
192  }
193 #ifdef VERBOSE
194  printf ("\nFile %s opened for RO\n", m_fname.c_str());
195 #endif
196  } else if (m_mode == IO::OUT) {
197 
198  try {
199  m_file = H5::H5File (m_fname, H5F_ACC_TRUNC);
200 #ifdef VERBOSE
201  printf ("\nFile %s opened for RW\n", m_fname.c_str());
202 #endif
203  } catch (const H5::Exception& e) {
204  m_file = H5::H5File (m_fname, H5F_ACC_RDWR);
205 #ifdef VERBOSE
206  printf ("\nFile %s created for RW\n", m_fname.c_str());
207 #endif
208  }
209  } else if (m_mode == IO::APPEND) {
210  try {
211  m_file = H5::H5File (m_fname, H5F_ACC_RDWR);
212 #ifdef VERBOSE
213  printf ("\nFile %s created for RW\n", m_fname.c_str());
214 #endif
215  } catch (const H5::Exception& e) {
216  printf ("\nOops!\n");
217  }
218  }
219 
220  return m_status;
221 
222  }
223 
224 
225 private:
226 
227  H5::Group CreateGroup (const std::string& url);
228 
229 
236  const IO::Status
237  ReportException (const H5::Exception& e, const IO::Status ios);
238 
239  H5::H5File m_file;
240 
241 
242 };
243 
244 #endif
IO::Status Write(const NDData< T > &data, const std::string &urn, const std::string &url="")
Write data from container to file.
Definition: HDF5IO.h:95
virtual IO::Status FileAccess()
File access?
Definition: HDF5IO.h:181
HDF5 IO interface.
Definition: HDF5IO.h:68
Definition: XMLIO.h:49
Mode
Definition: Declarations.h:176
Implementation of binary I/O.
HDF5IO(const std::string &fname, const IO::Mode mode)
Contructor.
Definition: HDF5IO.h:75
virtual ~HDF5IO()
Destructor.
Definition: HDF5IO.h:85
mode
Definition: Declarations.h:112
Base class for binary IO strategies.
Definition: BinaryIO.h:51
Simple nd-data structure.
Definition: NDData.h:53
Status
Definition: Declarations.h:148
Definition: HDF5IO.h:37

-- last change 17.06.2016 | Tony Stoecker | Imprint --