Field3D
Hdf5Util.h
Go to the documentation of this file.
1 //----------------------------------------------------------------------------//
2 
3 /*
4  * Copyright (c) 2009 Sony Pictures Imageworks Inc
5  *
6  * All rights reserved.
7  *
8  * Redistribution and use in source and binary forms, with or without
9  * modification, are permitted provided that the following conditions
10  * are met:
11  *
12  * Redistributions of source code must retain the above copyright
13  * notice, this list of conditions and the following disclaimer.
14  * Redistributions in binary form must reproduce the above copyright
15  * notice, this list of conditions and the following disclaimer in the
16  * documentation and/or other materials provided with the
17  * distribution. Neither the name of Sony Pictures Imageworks nor the
18  * names of its contributors may be used to endorse or promote
19  * products derived from this software without specific prior written
20  * permission.
21  *
22  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
23  * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
24  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
25  * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
26  * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
27  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
28  * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
29  * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
30  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
31  * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
32  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
33  * OF THE POSSIBILITY OF SUCH DAMAGE.
34  */
35 
36 //----------------------------------------------------------------------------//
37 
45 //----------------------------------------------------------------------------//
46 
47 #ifndef _INCLUDED_Field3D_Hdf5Util_H_
48 #define _INCLUDED_Field3D_Hdf5Util_H_
49 
50 //----------------------------------------------------------------------------//
51 
52 #include <string>
53 #include <exception>
54 #include <vector>
55 
56 #include <boost/lexical_cast.hpp>
57 
58 #include <hdf5.h>
59 
60 #include "Exception.h"
61 #include "Traits.h"
62 #include "Field.h"
63 
64 //----------------------------------------------------------------------------//
65 
66 #include "ns.h"
67 
69 
70 //----------------------------------------------------------------------------//
71 // Hdf5Util classes
72 //----------------------------------------------------------------------------//
73 
76 namespace Hdf5Util {
77 
78 //----------------------------------------------------------------------------//
79 
82 class H5Base
83 {
84 public:
85  // Constructor
87  : m_id(-1)
88  { /* Empty */ }
90  hid_t id() const
91  { return m_id; }
93  operator hid_t ()
94  { return m_id; }
95 protected:
96  hid_t m_id;
97 };
98 
99 //----------------------------------------------------------------------------//
100 
103 class H5ScopedAopen : public H5Base
104 {
105 public:
106  H5ScopedAopen(hid_t location, const std::string &name)
107  {
108  m_id = H5Aopen(location, name.c_str(), H5P_DEFAULT);
109  if (m_id < 0)
110  throw Exc::MissingAttributeException("Couldn't open attribute " + name);
111  }
112  H5ScopedAopen(hid_t location, const std::string &name, hid_t aapl_id)
113  {
114  m_id = H5Aopen(location, name.c_str(), aapl_id);
115  if (m_id < 0)
116  throw Exc::MissingAttributeException("Couldn't open attribute " + name);
117  }
119  {
120  if (m_id >= 0)
121  H5Aclose(m_id);
122  }
123 };
124 
125 //----------------------------------------------------------------------------//
126 
129 class H5ScopedAopenIdx : public H5Base
130 {
131 public:
132  H5ScopedAopenIdx(hid_t location, unsigned idx)
133  {
134  m_id = H5Aopen_idx(location, idx);
135  if (m_id < 0)
136  throw Exc::MissingAttributeException("Couldn't open attribute at index: " +
137  boost::lexical_cast<std::string>(idx));
138  }
140  {
141  if (m_id >= 0)
142  H5Aclose(m_id);
143  }
144 };
145 
146 //----------------------------------------------------------------------------//
147 
150 class H5ScopedGcreate : public H5Base
151 {
152 public:
153  H5ScopedGcreate(hid_t parentLocation, const std::string &name)
154  {
155  m_id = H5Gcreate(parentLocation, name.c_str(),
156  H5P_DEFAULT, H5P_DEFAULT, H5P_DEFAULT);
157  }
158  H5ScopedGcreate(hid_t parentLocation, const std::string &name,
159  hid_t lcpl_id, hid_t gcpl_id, hid_t gapl_id)
160  {
161  m_id = H5Gcreate(parentLocation, name.c_str(),
162  lcpl_id, gcpl_id, gapl_id);
163  }
164 
166  {
167  if (m_id >= 0)
168  H5Gclose(m_id);
169  }
170 };
171 
172 //----------------------------------------------------------------------------//
173 
176 class H5ScopedGopen : public H5Base
177 {
178 public:
180  : H5Base()
181  {
182  // Empty
183  }
184  H5ScopedGopen(hid_t parentLocation, const std::string &name)
185  {
186  open(parentLocation, name);
187  }
188  H5ScopedGopen(hid_t parentLocation, const std::string &name, hid_t gapl_id)
189  {
190  open(parentLocation, name, gapl_id);
191  }
192  void open(hid_t parentLocation, const std::string &name)
193  {
194  m_id = H5Gopen(parentLocation, name.c_str(), H5P_DEFAULT);
195  }
196  void open(hid_t parentLocation, const std::string &name, hid_t gapl_id)
197  {
198  m_id = H5Gopen(parentLocation, name.c_str(), gapl_id);
199  }
200 
202  {
203  if (m_id >= 0)
204  H5Gclose(m_id);
205  }
206 };
207 
208 //----------------------------------------------------------------------------//
209 
213 class H5ScopedScreate : public H5Base
214 {
215 public:
217  : H5Base()
218  {
219  // Empty
220  }
221  H5ScopedScreate(H5S_class_t type)
222  {
223  create(type);
224  }
225  void create(H5S_class_t type)
226  {
227  m_id = H5Screate(type);
228  }
230  {
231  if (m_id >= 0)
232  H5Sclose(m_id);
233  }
234 };
235 
236 //----------------------------------------------------------------------------//
237 
240 class H5ScopedDcreate : public H5Base
241 {
242 public:
243  H5ScopedDcreate(hid_t parentLocation, const std::string &name,
244  hid_t dtype_id, hid_t space_id, hid_t lcpl_id,
245  hid_t dcpl_id, hid_t dapl_id)
246  {
247  m_id = H5Dcreate(parentLocation, name.c_str(), dtype_id, space_id,
248  lcpl_id, dcpl_id, dapl_id);
249  }
251  {
252  if (m_id >= 0)
253  H5Dclose(m_id);
254  }
255 };
256 
257 //----------------------------------------------------------------------------//
258 
263 {
264 public:
265  H5ScopedAget_space(hid_t dataset_id)
266  {
267  m_id = H5Aget_space(dataset_id);
268  if (m_id < 0)
269  throw Exc::AttrGetSpaceException("Couldn't get attribute space");
270  }
272  {
273  if (m_id >= 0)
274  H5Sclose(m_id);
275  }
276 };
277 
278 //----------------------------------------------------------------------------//
279 
283 class H5ScopedAget_type : public H5Base
284 {
285 public:
286  H5ScopedAget_type(hid_t dataset_id)
287  {
288  m_id = H5Aget_type(dataset_id);
289  if (m_id < 0)
290  throw Exc::AttrGetTypeException("Couldn't get attribute type");
291  }
293  {
294  if (m_id >= 0)
295  H5Tclose(m_id);
296  }
297 };
298 
299 //----------------------------------------------------------------------------//
300 
305 {
306 public:
307  H5ScopedTget_native_type(hid_t dataset_id, H5T_direction_t direction)
308  {
309  m_id = H5Tget_native_type(dataset_id, direction);
310  if (m_id < 0)
311  throw Exc::AttrGetNativeTypeException("Couldn't get native attribute type");
312  }
314  {
315  if (m_id >= 0)
316  H5Tclose(m_id);
317  }
318 };
319 
320 //----------------------------------------------------------------------------//
321 
325 class H5ScopedDopen : public H5Base
326 {
327 public:
329  : H5Base()
330  {
331  // Empty
332  }
333  H5ScopedDopen(hid_t parentLocation, const std::string &name, hid_t dapl_id)
334  {
335  open(parentLocation, name, dapl_id);
336  }
337  void open(hid_t parentLocation, const std::string &name, hid_t dapl_id)
338  {
339  m_id = H5Dopen(parentLocation, name.c_str(), dapl_id);
340  }
342  {
343  if (m_id >= 0) {
344  H5Dclose(m_id);
345  }
346  }
347 };
348 
349 //----------------------------------------------------------------------------//
350 
354 {
355 public:
357  : H5Base()
358  {
359  // Empty
360  }
361  H5ScopedDget_space(hid_t dataset_id)
362  {
363  open(dataset_id);
364  }
365  void open(hid_t dataset_id)
366  {
367  m_id = H5Dget_space(dataset_id);
368  }
370  {
371  if (m_id >= 0)
372  H5Sclose(m_id);
373  }
374 };
375 
376 //----------------------------------------------------------------------------//
377 
381 class H5ScopedDget_type : public H5Base
382 {
383 public:
385  : H5Base()
386  {
387  // Empty
388  }
389  H5ScopedDget_type(hid_t dataset_id)
390  {
391  open(dataset_id);
392  }
393  void open(hid_t dataset_id)
394  {
395  m_id = H5Dget_type(dataset_id);
396  }
398  {
399  if (m_id >= 0)
400  H5Tclose(m_id);
401  }
402 };
403 
404 //----------------------------------------------------------------------------//
405 // Hdf5Util functions
406 //----------------------------------------------------------------------------//
407 
412 template <typename T>
415 void writeSimpleData(hid_t location, const std::string &name,
416  const std::vector<T> &data);
417 
420 template <typename T>
421 void readSimpleData(hid_t location, const std::string &name,
422  std::vector<T> &data);
423 
425 
426 //----------------------------------------------------------------------------//
427 
432 bool readAttribute(hid_t location, const std::string& attrName,
435  std::string& value);
436 
439 bool readAttribute(hid_t location, const std::string& attrName,
440  unsigned int attrSize, int &value);
441 
444 bool readAttribute(hid_t location, const std::string& attrName,
445  unsigned int attrSize, float &value);
446 
449 bool readAttribute(hid_t location, const std::string& attrName,
450  unsigned int attrSize, double &value);
451 
454 bool readAttribute(hid_t location, const std::string& attrName,
455  std::vector<unsigned int> &attrSize, int &value);
456 
459 bool readAttribute(hid_t location, const std::string& attrName,
460  std::vector<unsigned int> &attrSize, float &value);
461 
464 bool readAttribute(hid_t location, const std::string& attrName,
465  std::vector<unsigned int> &attrSize, double &value);
466 
467 
469 
470 //----------------------------------------------------------------------------//
471 
476 bool writeAttribute(hid_t location, const std::string& attrName,
479  const std::string& value);
480 
483 bool writeAttribute(hid_t location, const std::string& attrName,
484  unsigned int attrSize, const int &value);
485 
488 bool writeAttribute(hid_t location, const std::string& attrName,
489  unsigned int attrSize, const float &value);
490 
493 bool writeAttribute(hid_t location, const std::string& attrName,
494  unsigned int attrSize, const double &value);
495 
498 bool writeAttribute(hid_t location, const std::string& attrName,
499  std::vector<unsigned int> &attrSize, const int &value);
500 
501 
504 bool writeAttribute(hid_t location, const std::string& attrName,
505  std::vector<unsigned int> &attrSize, const int &value);
506 
509 bool writeAttribute(hid_t location, const std::string& attrName,
510  std::vector<unsigned int> &attrSize, const float &value);
511 
514 bool writeAttribute(hid_t location, const std::string& attrName,
515  std::vector<unsigned int> &attrSize, const double &value);
516 
518 
519 //----------------------------------------------------------------------------//
520 
523 bool checkHdf5Gzip();
524 
525 //----------------------------------------------------------------------------//
526 // Templated functions and classes
527 //----------------------------------------------------------------------------//
528 
529 template <typename T>
530 void writeSimpleData(hid_t location, const std::string &name,
531  const std::vector<T> &data)
532 {
533  using namespace Exc;
534 
535  // Calculate the total number of entries. This factors in that
536  // V3f uses 3 components per value, etc.
537  hsize_t totalSize[1];
538  int components = FieldTraits<T>::dataDims();
539  totalSize[0] = data.size() * components;
540 
541  // Get the internal data type
542  hid_t type = DataTypeTraits<T>::h5type();
543 
544  H5ScopedScreate dataSpace(H5S_SIMPLE);
545 
546  if (dataSpace.id() < 0)
547  throw WriteSimpleDataException("Couldn't create data space");
548 
549  H5Sset_extent_simple(dataSpace.id(), 1, totalSize, NULL);
550 
551  H5ScopedDcreate dataSet(location, name.c_str(), type, dataSpace.id(),
552  H5P_DEFAULT, H5P_DEFAULT, H5P_DEFAULT);
553 
554  if (dataSet.id() < 0)
555  throw WriteSimpleDataException("Couldn't create data set");
556 
557  hid_t err = H5Dwrite(dataSet.id(), type, H5S_ALL, H5S_ALL,
558  H5P_DEFAULT, &data[0]);
559 
560  if (err < 0)
561  throw WriteSimpleDataException("Couldn't write data");
562 }
563 
564 //----------------------------------------------------------------------------//
565 
566 template <typename T>
567 void readSimpleData(hid_t location, const std::string &name,
568  std::vector<T> &data)
569 {
570  using namespace Exc;
571 
572  int components = FieldTraits<T>::dataDims();
573  hsize_t dims[1];
574 
575  H5ScopedDopen dataSet(location, name.c_str(), H5P_DEFAULT);
576 
577  if (dataSet.id() < 0)
578  throw OpenDataSetException("Couldn't open data set: " + name);
579 
580  H5ScopedDget_space dataSpace(dataSet.id());
581  H5ScopedDget_type dataType(dataSet.id());
582  H5Sget_simple_extent_dims(dataSpace.id(), dims, NULL);
583 
584  if (dataSpace.id() < 0)
585  throw GetDataSpaceException("Couldn't get data space");
586 
587  if (dataType.id() < 0)
588  throw GetDataTypeException("Couldn't get data type");
589 
590  int reportedSize = dims[0] / components;
591 
592  // Resize target
593  data.clear();
594  data.resize(reportedSize);
595 
596  // Get the internal data type
597  hid_t type = DataTypeTraits<T>::h5type();
598 
599  if (H5Dread(dataSet.id(), type, H5S_ALL, H5S_ALL,
600  H5P_DEFAULT, &data[0]) < 0) {
601  throw Hdf5DataReadException("Couldn't read simple data");
602  }
603 }
604 
605 //----------------------------------------------------------------------------//
606 
607 } // namespace Hdf5Util
608 
609 //----------------------------------------------------------------------------//
610 
612 
613 //----------------------------------------------------------------------------//
614 
615 #endif
void open(hid_t dataset_id)
Definition: Hdf5Util.h:365
#define FIELD3D_NAMESPACE_HEADER_CLOSE
Definition: ns.h:58
Scoped object - opens an attribute data type on creation and closes it on destruction.
Definition: Hdf5Util.h:283
Contains utility functions and classes for Hdf5 files.
Definition: Hdf5Util.h:76
Scoped object - opens a dataset on creation and closes it on destruction.
Definition: Hdf5Util.h:381
H5ScopedGcreate(hid_t parentLocation, const std::string &name)
Definition: Hdf5Util.h:153
Scoped object - opens an native type id on creation and closes it on destruction. ...
Definition: Hdf5Util.h:304
static int dataDims()
Dimensions of the given data type. i.e. 3 for V3f, 1 for float.
Namespace for Exception objects.
Definition: Exception.h:57
Scoped object - opens a dataset on creation and closes it on destruction.
Definition: Hdf5Util.h:325
H5ScopedAopen(hid_t location, const std::string &name)
Definition: Hdf5Util.h:106
H5ScopedScreate(H5S_class_t type)
Definition: Hdf5Util.h:221
H5ScopedTget_native_type(hid_t dataset_id, H5T_direction_t direction)
Definition: Hdf5Util.h:307
Scoped object - creates a group on creation and closes it on destruction.
Definition: Hdf5Util.h:150
H5ScopedGopen(hid_t parentLocation, const std::string &name)
Definition: Hdf5Util.h:184
H5ScopedAget_type(hid_t dataset_id)
Definition: Hdf5Util.h:286
void open(hid_t dataset_id)
Definition: Hdf5Util.h:393
Scoped object - Opens attribute by name and closes it on destruction.
Definition: Hdf5Util.h:103
Scoped object - Opens attribute by index and closes it on destruction.
Definition: Hdf5Util.h:129
bool readAttribute(hid_t location, const std::string &attrName, std::string &value)
Reads a string attribute.
bool checkHdf5Gzip()
Checks whether gzip is available in the current hdf5 library.
Definition: Hdf5Util.cpp:680
H5ScopedAopenIdx(hid_t location, unsigned idx)
Definition: Hdf5Util.h:132
void open(hid_t parentLocation, const std::string &name, hid_t dapl_id)
Definition: Hdf5Util.h:337
void readSimpleData(hid_t location, const std::string &name, std::vector< T > &data)
Reads a simple linear data set from the given location.
Definition: Hdf5Util.h:567
static hid_t h5type()
H5ScopedGopen(hid_t parentLocation, const std::string &name, hid_t gapl_id)
Definition: Hdf5Util.h:188
void open(hid_t parentLocation, const std::string &name, hid_t gapl_id)
Definition: Hdf5Util.h:196
bool writeAttribute(hid_t location, const std::string &attrName, const std::string &value)
Writes a string attribute.
Scoped object - opens an attribute data space on creation and closes it on destruction.
Definition: Hdf5Util.h:262
H5ScopedAopen(hid_t location, const std::string &name, hid_t aapl_id)
Definition: Hdf5Util.h:112
Scoped object - creates a dataspace on creation and closes it on destruction.
Definition: Hdf5Util.h:213
Base class for all scoped Hdf5 util classes.
Definition: Hdf5Util.h:82
Contains Field, WritableField and ResizableField classes.
H5ScopedGcreate(hid_t parentLocation, const std::string &name, hid_t lcpl_id, hid_t gcpl_id, hid_t gapl_id)
Definition: Hdf5Util.h:158
H5ScopedDget_space(hid_t dataset_id)
Definition: Hdf5Util.h:361
Scoped object - creates a dataset on creation and closes it on destruction.
Definition: Hdf5Util.h:240
H5ScopedAget_space(hid_t dataset_id)
Definition: Hdf5Util.h:265
Scoped object - opens a group on creation and closes it on destruction.
Definition: Hdf5Util.h:176
Contains Exception base class.
void create(H5S_class_t type)
Definition: Hdf5Util.h:225
H5ScopedDget_type(hid_t dataset_id)
Definition: Hdf5Util.h:389
void open(hid_t parentLocation, const std::string &name)
Definition: Hdf5Util.h:192
Scoped object - opens a dataset on creation and closes it on destruction.
Definition: Hdf5Util.h:353
void writeSimpleData(hid_t location, const std::string &name, const std::vector< T > &data)
Writes a simple linear data set to the given location.
Definition: Hdf5Util.h:530
H5ScopedDopen(hid_t parentLocation, const std::string &name, hid_t dapl_id)
Definition: Hdf5Util.h:333
H5ScopedDcreate(hid_t parentLocation, const std::string &name, hid_t dtype_id, hid_t space_id, hid_t lcpl_id, hid_t dcpl_id, hid_t dapl_id)
Definition: Hdf5Util.h:243
hid_t id() const
Query the hid_t value.
Definition: Hdf5Util.h:90