Hi5Read.h
1 /*
2 
3 MIT License
4 
5 Copyright (c) 2017 FMI Open Development / Markus Peura, first.last@fmi.fi
6 
7 Permission is hereby granted, free of charge, to any person obtaining a copy
8 of this software and associated documentation files (the "Software"), to deal
9 in the Software without restriction, including without limitation the rights
10 to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
11 copies of the Software, and to permit persons to whom the Software is
12 furnished to do so, subject to the following conditions:
13 
14 The above copyright notice and this permission notice shall be included in all
15 copies or substantial portions of the Software.
16 
17 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
18 IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
19 FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
20 AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
21 LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
22 OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
23 SOFTWARE.
24 
25 */
26 /*
27 Part of Rack development has been done in the BALTRAD projects part-financed
28 by the European Union (European Regional Development Fund and European
29 Neighbourhood Partnership Instrument, Baltic Sea Region Programme 2007-2013)
30 */
31 
32 
33 #include "Hi5.h" // to define H5_USE_*_API_DEFAULT
34 
35 
36 #ifndef Hi5READ
37 #define Hi5READ
38 
39 
40 #include <hdf5.h>
41 
42 #include <string>
43 #include <list>
44 
45 #include "drain/util/TreeOrdered.h"
46 #include "drain/util/Flags.h"
47 #include "drain/image/Image.h"
48 
49 
50 
51 
52 // using namespace std;
53 
54 namespace hi5 {
55 
56 //extern const int ATTRIBUTES;
57 //extern const int DATASETS;
58 
59 
60 //extern
61 
62 class Reader : public Hi5Base {
63 
64 public:
65 
66  enum Mode {
67  ATTRIBUTES=1,
69  EXCLUSIVE=4
70  };
71 
73 
74 
75  static
76  void readFile(const std::string &filename, Hi5Tree &tree, ModeFlagger::dvalue_t mode=(ATTRIBUTES | DATASETS)); //(ATTRIBUTES|DATASETS));
77 
79 
86  static void
87  h5FileToTree(hid_t file_id, const Hi5Tree::path_t &path, Hi5Tree &tree, ModeFlagger::dvalue_t mode = (ATTRIBUTES | DATASETS));
88 
90 
95  static inline
96  void h5FileToTree(hid_t fid, Hi5Tree &tree, ModeFlagger::dvalue_t mode=(ATTRIBUTES | DATASETS)){ //(ATTRIBUTES|DATASETS)){
97  Hi5Tree::path_t path;
98  path.append(Hi5Tree::key_t::ROOT);
99  // path.appendElem(Hi5Tree::key_t::ROOT);
100  h5FileToTree(fid, path, tree, mode);
101  };
102 
103 
104  template <class T>
105  static
106  void h5AttributeToData(hid_t aid, hid_t datatype, drain::Variable & attribute, size_t elements=1){
107 
108  drain::Logger mout(getLogH5(), __FILE__, __FUNCTION__);
109 
110  attribute.setType(typeid(T));
111  attribute.setSize(elements);
112 
113  int status = H5Aread(aid, datatype, attribute.getPtr());
114  handleStatus<LOG_ERR>(mout, status, "H5Aread failed for attribute=", attribute, __LINE__);
115  /*
116  if (status < 0){
117  mout.error("h5AttributeToData: read failed ");
118  }
119  */
120  }
121 
122 
123 
124  static
125  void h5DatasetToImage(hid_t id, const Hi5Tree::path_t &path, drain::image::Image &image);
126 
127 protected:
128 
129  static
130  herr_t iterate_attribute(hid_t id, const char * attr_name, const H5A_info_t *ainfo, void *operator_data);
131 
132  static
133  herr_t iterate(hid_t group_id, const char * member_name, void *operator_data);
134 
135 };
136 
137 } // ::hi5
138 
139 //const drain::SingleFlagger<Reader::Mode>::dict_t Reader::dict = {{"ATTRIBUTES", ATTRIBUTES}, {"DATASETS", DATASETS}};
140 template <>
142 
143 
144 #endif /* ImageH5_H_ */
145 
146 // Rack
Flagger accepting values of enum type E.
Definition: Flags.h:763
LogSourc e is the means for a function or any program segment to "connect" to a Log.
Definition: Log.h:308
dict_t::value_t dvalue_t
Fundamental type of the bitvector - an integral type.
Definition: Flags.h:585
Class for multi-channel digital images. Supports dynamic typing with base types (char,...
Definition: Image.h:184
Base class for Reader and Writer, essentially just wrapping some utilities.
Definition: Hi5.h:122
Definition: Hi5Read.h:62
Mode
Definition: Hi5Read.h:66
@ EXCLUSIVE
Definition: Hi5Read.h:69
@ DATASETS
Definition: Hi5Read.h:68
static void h5FileToTree(hid_t file_id, const Hi5Tree::path_t &path, Hi5Tree &tree, ModeFlagger::dvalue_t mode=(ATTRIBUTES|DATASETS))
Conversion from native HDF5 structure to Rack's hi5 tree structure.
Definition: Hi5Read.cpp:84
static void h5FileToTree(hid_t fid, Hi5Tree &tree, ModeFlagger::dvalue_t mode=(ATTRIBUTES|DATASETS))
Conversion from native HDF5 structure to Rack's hi5 tree structure.
Definition: Hi5Read.h:96
static herr_t iterate(hid_t group_id, const char *member_name, void *operator_data)
Recursive traversal.
Definition: Hi5Read.cpp:212
static herr_t iterate_attribute(hid_t id, const char *attr_name, const H5A_info_t *ainfo, void *operator_data)
Definition: Hi5Read.cpp:220
static void h5DatasetToImage(hid_t id, const Hi5Tree::path_t &path, drain::image::Image &image)
const Hi5Tree::path_t &path
Definition: Hi5Read.cpp:382
DRAIN_VARIABLE Variable
Value container supporting dynamic type.
Definition: Variable.h:63
Definition: Flags.h:744