iRoCS Toolbox  1.1.0
BlitzHdf5Light.hh
Go to the documentation of this file.
1 /**************************************************************************
2  *
3  * Copyright (C) 2015 Thorsten Falk
4  *
5  * Image Analysis Lab, University of Freiburg, Germany
6  *
7  * This program is free software; you can redistribute it and/or modify
8  * it under the terms of the GNU General Public License as published by
9  * the Free Software Foundation; either version 3 of the License, or
10  * (at your option) any later version.
11  *
12  * This program is distributed in the hope that it will be useful,
13  * but WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15  * GNU General Public License for more details.
16  *
17  * You should have received a copy of the GNU General Public License
18  * along with this program; if not, write to the Free Software Foundation,
19  * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
20  *
21  **************************************************************************/
22 
23 /**************************************************************************
24  ** Title: Lightweight blitz to HDF5 file reader/writer
25  ** $RCSfile$
26  ** $Revision: $ $Name$
27  ** $Date: $
28  ** Copyright: GPL $Author: $
29  ** Description:
30  **
31  ** Lightweight alternative to libBlitzHDF5 providing its basic functionality.
32  **
33  **************************************************************************/
34 
35 /*======================================================================*/
41 /*======================================================================*/
42 
43 #ifndef BLITZHDF5LIGHT_HH
44 #define BLITZHDF5LIGHT_HH
45 
46 #ifdef HAVE_CONFIG_H
47 #include <config.hh>
48 #endif
49 
50 // general includes
51 #include <cstring>
52 #include <sstream>
53 #include <fstream>
54 
55 // For modification time stamps of datasets
56 #include <ctime>
57 
58 // Blitz++
59 #include <blitz/array.h>
60 
61 // HDF5 includes
62 #include <hdf5.h>
63 
65 
66 #include "BlitzH5Traits.hh"
67 
68 /*======================================================================*/
73 /*======================================================================*/
74 class BlitzH5Error : public std::exception
75 {
76 
77 public:
78 
79  /*======================================================================*/
85  /*======================================================================*/
86  BlitzH5Error();
87 
88  /*======================================================================*/
96  /*======================================================================*/
97  BlitzH5Error( const std::string& message);
98 
99  /*======================================================================*/
103  /*======================================================================*/
104  ~BlitzH5Error() throw();
105 
106  /*======================================================================*/
112  /*======================================================================*/
113  template<typename DataT>
114  BlitzH5Error &operator<<(DataT const &data);
115 
116  /*======================================================================*/
122  /*======================================================================*/
123  std::string const &str() const;
124 
125  /*======================================================================*/
131  /*======================================================================*/
132  char const *what() const throw();
133 
134 private:
135 
136  std::string _message;
137 
138 };
139 
141 {
142 
143 public:
144 
145  enum FileMode { ReadOnly, Write, Replace, New, WriteOrNew };
146 
147  /*======================================================================*/
151  /*======================================================================*/
152  BlitzH5File();
153 
154  /*======================================================================*/
170  /*======================================================================*/
171  BlitzH5File(std::string const &fileName, FileMode mode = ReadOnly);
172 
173  /*-----------------------------------------------------------------------
174  * Destructor
175  *-----------------------------------------------------------------------*/
176  ~BlitzH5File();
177 
178  /*======================================================================*/
187  /*======================================================================*/
188  hid_t id()
189  {
190  if (_fileId < 0)
191  throw BlitzH5Error() << "Invalid file id. The file is not open.";
192  return _fileId;
193  }
194 
195  /*======================================================================*/
201  /*======================================================================*/
202  std::string const &name() const
203  {
204  return _fileName;
205  }
206 
207  /*======================================================================*/
221  /*======================================================================*/
222  static std::string simplifyGroupDescriptor(std::string const& group);
223 
224  /*======================================================================*/
232  /*======================================================================*/
233  bool existsDataset(std::string const &name) const;
234 
235  /*======================================================================*/
244  /*=======================================================================*/
245  std::vector<hsize_t> getDatasetShape(std::string const &name) const;
246 
247  /*======================================================================*/
256  /*=======================================================================*/
257  std::vector<std::string>
258  allDatasets(std::string const &startGroup = "/") const;
259 
260  /*======================================================================*/
269  /*=======================================================================*/
270  hid_t getDatasetType(std::string const &name) const;
271 
272  /*======================================================================*/
286  /*======================================================================*/
287  template<typename FixedNumericT>
288  void readDataset(FixedNumericT &data, std::string const &name) const;
289 
290  /*======================================================================*/
304  /*======================================================================*/
305  template<typename DataT>
306  void readDataset(
307  std::vector<DataT> &data, std::string const &name) const;
308 
309  /*======================================================================*/
324  /*======================================================================*/
325  template<typename DataT, int Rank>
326  void readDataset(
327  blitz::Array<DataT,Rank> &data, std::string const &name,
328  iRoCS::ProgressReporter *pr = NULL) const;
329 
330  /*======================================================================*/
343  /*======================================================================*/
344  void readDataset(
345  std::string &data, std::string const &name,
346  iRoCS::ProgressReporter *pr = NULL) const;
347 
348  /*======================================================================*/
361  /*======================================================================*/
362  template<typename DataT>
363  void writeDataset(DataT const &data, std::string const &name);
364 
365  /*======================================================================*/
384  /*======================================================================*/
385  template<typename DataT, int Rank>
386  void writeDataset(
387  blitz::Array<DataT,Rank> const &data, std::string const &name,
388  int compression = 1, iRoCS::ProgressReporter *pr = NULL);
389 
390  /*======================================================================*/
406  /*======================================================================*/
407  void writeDataset(
408  std::string const &data, std::string const &name,
409  iRoCS::ProgressReporter *pr = NULL);
410 
411  /*======================================================================*/
427  /*======================================================================*/
428  void deleteDataset(std::string const &name);
429 
430  /*======================================================================*/
440  /*======================================================================*/
441  bool existsAttribute(
442  std::string const &attName, std::string const &objectName = "/") const;
443 
444  /*======================================================================*/
454  /*======================================================================*/
455  std::vector<hsize_t> getAttributeShape(
456  std::string const &attName, std::string const &objectName = "/") const;
457 
458  /*======================================================================*/
467  /*=======================================================================*/
468  hid_t getAttributeType(
469  const std::string& attName, const std::string& objectName = "/") const;
470 
471  /*======================================================================*/
479  /*======================================================================*/
480  template<typename DataT>
481  void readAttribute(
482  DataT &data, std::string const &attName,
483  std::string const &objectName = "/") const;
484 
485  /*======================================================================*/
493  /*======================================================================*/
494  template<typename DataT, int Dim>
495  void readAttribute(
496  blitz::TinyVector<DataT,Dim> &data, std::string const &attName,
497  std::string const &objectName = "/") const;
498 
499  /*======================================================================*/
507  /*======================================================================*/
508  template<typename DataT, int NRows, int NCols>
509  void readAttribute(
510  blitz::TinyMatrix<DataT,NRows,NCols> &data, std::string const &attName,
511  std::string const &objectName = "/") const;
512 
513  /*======================================================================*/
521  /*======================================================================*/
522  template<typename DataT>
523  void readAttribute(
524  std::vector<DataT> &data, std::string const &attName,
525  std::string const &objectName = "/") const;
526 
527  /*======================================================================*/
535  /*======================================================================*/
536  void readAttribute(
537  std::string &data, std::string const &attName,
538  std::string const &objectName = "/") const;
539 
540  /*======================================================================*/
548  /*======================================================================*/
549  template<typename DataT>
550  void writeAttribute(
551  DataT const &in, std::string const &attName,
552  std::string const &objectName = "/");
553 
554  /*======================================================================*/
562  /*======================================================================*/
563  void writeAttribute(
564  std::string const &in, std::string const &attName,
565  std::string const &objectName = "/");
566 
567  /*======================================================================*/
574  /*======================================================================*/
575  void deleteAttribute(
576  std::string const &attName, std::string const &objectName = "/");
577 
578  /*======================================================================*/
587  /*======================================================================*/
588  std::vector<std::string> getObjects(std::string const &groupName = "/") const;
589 
590  /*======================================================================*/
598  /*======================================================================*/
599  std::vector<std::string> getGroups(std::string const &groupName = "/") const;
600 
601  /*======================================================================*/
610  /*=======================================================================*/
611  std::vector<std::string> getAttributes(std::string const &objectName = "/")
612  const;
613 
614  /*======================================================================*/
622  /*======================================================================*/
623  bool existsGroup(std::string const &groupName) const;
624 
625  /*======================================================================*/
633  /*======================================================================*/
634  void createGroup(std::string const &groupName);
635 
636  /*======================================================================*/
643  /*======================================================================*/
644  void deleteGroup(std::string const &groupName);
645 
646  /*======================================================================*/
658  /*=======================================================================*/
659  void copyObject(
660  std::string const &objectName, BlitzH5File &outFile,
661  int compression = -1);
662 
663 private:
664 
665  void _copyAttribute(hid_t attributeId, hid_t targetId);
666 
667  template<typename SourceT, typename DestT>
668  void _loadAttribute(hid_t attributeId, DestT *target) const;
669 
670  template<typename SourceT, typename DestT>
671  void _loadChunkedDataset(
672  hid_t datasetId, std::vector<hsize_t> const &datasetDims,
673  std::vector<hsize_t> const &chunkDims, DestT *target,
674  iRoCS::ProgressReporter *pr = NULL) const;
675 
676  template<typename SourceT, typename DestT>
677  void _loadContiguousDataset(hid_t datasetId, DestT *target) const;
678 
679  std::string _fileName;
680  hid_t _fileId;
681  FileMode _mode;
682 
683 };
684 
685 #include "BlitzHdf5Light.icc"
686 
687 #endif
BlitzH5Error & operator<<(DataT const &data)
Appends the given data to the message string.
std::string const & name() const
Get the file name.
The main error class for the BlitzHDF5 interface.
void readDataset(blitz::Array< DataT, Dim > &data, blitz::TinyVector< ElSizeT, Dim > &elSize, std::string const &fileName, std::string const &dsName, bool throwErrors=false, iRoCS::ProgressReporter *pr=NULL)
Read a scalar n-dimensional dataset with element size.
char const * what() const
Returns the error message as c-string.
~BlitzH5Error()
Default destructor.
void writeDataset(blitz::Array< blitz::TinyVector< DataT, InnerDim >, OuterDim > const &data, blitz::TinyVector< ElSizeT, OuterDim > const &elSize, std::string const &fileName, std::string const &dsName, DataT displayMin, DataT displayMax, bool throwErrors=false, int compression=0, iRoCS::ProgressReporter *pr=NULL, std::string const &dim_interpretation="")
Write a vectorial n-dimensional dataset with element size and additional meta-information for dataset...
std::string const & str() const
Returns the error message.
BlitzH5Error()
Default constructor.
hid_t id()
Get HDF5 id for this file.