Hi5.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 // #define H5_USE_16_API_DEFAULT 1
33 #define H5_USE_18_API_DEFAULT 1
34 //#define H5_USE_110_API_DEFAULT 1
35 
36 #ifndef HI5_BASE
37 #define HI5_BASE
38 
39 
40 
41 #include <hdf5.h>
42 
43 #include <string>
44 #include <list>
45 
46 #include <drain/Log.h>
47 // #include <drain/VariableAssign.h>
48 // #include "drain/Variable.h"
49 #include <drain/image/Image.h>
50 #include <drain/util/FileInfo.h>
51 #include <drain/util/TreeOrdered.h>
52 
53 #include "data/ODIMPath.h"
54 
55 
56 namespace hi5 {
57 
58 extern
59 drain::Log & getLogH5();
60 
61 
62 extern
63 const drain::FileInfo fileInfo;
64 
66 
74 struct NodeHi5 {
75 
77 
78  drain::image::Image image;
79 
80  // Required, but not much needed.
81  bool empty() const {
82  return (image.isEmpty() && attributes.empty());
83  }
84 
85  drain::VariableMap attributes;
86 
87  NodeHi5() : exclude(false) {};
88 
89  inline
90  NodeHi5(const NodeHi5 & n) : exclude(n.exclude) {
91  attributes.importMap(n.attributes);
92  };
93 
94  inline
95  NodeHi5 & operator=(const hi5::NodeHi5 & node){
96  if (&node != this){
97  attributes.importMap(node.attributes);
98  }
99  return *this;
100  };
101 
102  //void writeText(std::ostream & ostr = std::cout, const std::string & prefix = "") const;
103  void writeText(std::ostream & ostr = std::cout, const rack::ODIMPath & prefix = rack::ODIMPath()) const;
104 
106  bool exclude; // OK!
107 
108 };
109 
110 }
111 
112 
114 typedef drain::OrderedTree<hi5::NodeHi5, false, rack::ODIMPath> Hi5Tree;
115 
116 
117 
118 namespace hi5 {
119 
120 
122 class Hi5Base {
123 
124 public:
125 
126  static
127  void handleStatus(herr_t status, const std::string & message, drain::Logger &mout, int lineNo=0);
128 
129  template <int L, class T>
130  static inline
131  // void handleStatus(drain::Logger &mout, herr_t status, const std::string & message, const drain::Path<> & path, int lineNo=0);
132  void handleStatus(drain::Logger &mout, herr_t status, const std::string & message, const T & arg, int lineNo=0){
133 
134  if (status >= 0)
135  return;
136 
137  mout.start<L>() << message << ": " << arg;
138  if (lineNo)
139  mout << ", line=" << lineNo;
140  mout << ", status=" << status << mout.endl;
141 
142  }
143 
145  static
146  hid_t getH5StandardType(const std::type_info &type);
147 
149  static
150  hid_t getH5NativeDataType(const std::type_info &t);
151 
153  template <class T>
154  static
156  return getH5NativeDataType(typeid(T));
157  }
158 
159  static
160  hid_t getH5StringVariableLength();
161 
162 
164  static
165  //Hi5Tree & getPalette(Hi5Tree & dst);
166  drain::image::Image & getPalette(Hi5Tree & dst);
167 
169  static
170  void linkPalette(const Hi5Tree & palette, Hi5Tree & dst);
171 
172 
174  static
175  void writeText(const Hi5Tree &src, const std::list<typename Hi5Tree::path_t> & paths, std::ostream & ostr = std::cout);
176 
178  static
179  void writeText(const Hi5Tree &src, std::ostream & ostr = std::cout){
180  std::list<typename Hi5Tree::path_t> paths;
181  drain::TreeUtils::getPaths(src, paths);
182  writeText(src, paths, ostr);
183  };
184 
185 
187 
190  static
191  void readText(Hi5Tree &src, std::istream & istr = std::cin);
192 
193 
195  // consider ValueReader, TextReader instead (skipping attrType)
207  static
208  void parsePath(const std::string & line, Hi5Tree::path_t & path, std::string & attrKey, std::string & attrValue);
209 
210 
212 
216  static
217  void readTextLine(Hi5Tree &src, const std::string & line);
218 
219  static
220  //void readTextLine(Hi5Tree & dst, const Hi5Tree::path_t & path, const std::string & assignment);
221  void assignAttribute(Hi5Tree & dst, const std::string & assignment);
222 
223  // static void readTextLine(Hi5Tree & dst, const Hi5Tree::path_t & path, const std::string & key, const std::string & value);
224 
226  /*
227  static
228  void markExcluded(Hi5Tree &src, bool exclude=true);
229  */
230 
231 
232 
234  static
235  void deleteExcluded(Hi5Tree &src);
236 
237 
238 };
239 
240 
242 std::ostream & operator<<(std::ostream &ostr, const hi5::NodeHi5 &n);
243 
245 std::ostream & operator<<(std::ostream &ostr, const Hi5Tree & tree);
246 
247 
248 /*
249 struct lessAlphaNum {
250 
251  enum comparison {LESS=-1, EQUAL=0, GREATER=+1};
252 
253  inline
254  static
255  bool isNumeric(char c) {
256  return ((c >= '0') && (c <= '9'));
257  };
258 
259  inline
260  static
261  comparison compareAlphaBetical(std::string::const_iterator & c1, std::string::const_iterator end1,
262  std::string::const_iterator & c2, std::string::const_iterator end2){
263 
264 
265  while (c1 != end1){
266 
267  if (c2 == end2){
268  return GREATER; // str1 longer
269  }
270 
271  // Stop if both are numeric
272  if (isNumeric(*c1) && isNumeric(*c2)){
273  return EQUAL; // that is, equal as to the alpha part.
274  }
275 
276  if (*c1 < *c2)
277  return LESS;
278  else if (*c1 > *c2)
279  return GREATER;
280 
281  // *c1 == *c2 : strs are equal, non-numeric this far.
282 
283  ++c1;
284  ++c2;
285 
286  }
287 
288  if (c2 != end2) // str2 is longer
289  return LESS;
290  else
291  return EQUAL;
292 
293  }
294 
295  inline
296  static
297  comparison compareNumeric(std::string::const_iterator & c1, std::string::const_iterator end1,
298  std::string::const_iterator & c2, std::string::const_iterator end2){
299 
300  const std::string::const_iterator n1 = c1;
301  const std::string::const_iterator n2 = c2;
302  size_t length1 = 0;
303  size_t length2 = 0;
304 
305  // scan to end of numeric segment
306  while ((c1 != end1) && isNumeric(*c1)){
307  ++c1;
308  // todo: if undetectValue padding?
309  ++length1;
310  }
311 
312  // scan to end of numeric segment
313  while ((c2 != end2) && isNumeric(*c2)){
314  ++c2;
315  // todo: if undetectValue padding?
316  ++length2;
317  }
318 
319 
320 
321  if (length1 < length2)
322  return LESS;
323  else if (length1 > length2)
324  return GREATER;
325  else {
326  // std::cout << "Equal lengths. Compare element by element\n";
327  // end points of numsegs
328  const std::string::const_iterator ne1 = c1;
329  const std::string::const_iterator ne2 = c2;
330  // rescan from start
331  c1 = n1;
332  c2 = n2;
333  while ((c1 != ne1) && (c2 != ne2)){ //
334 
335  if (*c1 < *c2)
336  return LESS;
337  else if (*c1 > *c2)
338  return GREATER;
339  ++c1;
340  ++c2;
341  }
342 
343  return EQUAL;
344 
345  }
346 
347  }
348 
349  // Main function
350  bool operator()(const std::string & s1, const std::string & s2) const {
351 
352  const std::string::const_iterator end1 = s1.end();
353  const std::string::const_iterator end2 = s2.end();
354  std::string::const_iterator c1 = s1.begin();
355  std::string::const_iterator c2 = s2.begin();
356 
357  comparison result;
358 
359  // debugging
360  std::string d1, d2;
361 
362  //while ((c1 != end1) && (c2 != end2)){
363  //int n = 10;
364  //while (n > 0){
365  //--n;
366  while (true){
367 
368  //std::cout << "Alphabetic check\n";
369  //std::cout << d1.assign(c1, end1) << " vs. " << d2.assign(c2, end2) << '\n';
370 
371  result = compareAlphaBetical(c1, end1, c2, end2);
372 
373  if (result == LESS)
374  return true;
375  if (result == GREATER)
376  return false;
377 
378  //if (c1 == end1) // s1 ends, so s2 same this far (ie. same or longer)
379  if (c2 == end2) // s2 gives up, so s1 same this far (ie. same or longer)
380  return false;
381 
382  // std::cout << "Numeric part check\n";
383  //std::cout << d1.assign(c1, end1) << " vs. " << d2.assign(c2, end2) << '\n';
384 
385 
386  result = compareNumeric(c1, end1, c2, end2);
387 
388  if (result == LESS)
389  return true;
390  if (result == GREATER)
391  return false;
392 
393  //if (c1 == end1) // s1 ends, so s2 same this far (ie. same or longer)
394  if (c2 == end2) // s2 gives up, so s1 same this far (ie. same or longer)
395  return false;
396 
397  // std::cout << "Equal, continuing \n";
398  }
399 
400 
401 
402 
403  } // end operator()
404 
405 }; // end class
406 */
407 
408 
409 } // ::hi5
410 
411 
412 #endif
Definition: FileInfo.h:48
Handler for notifications sent by a Logger.
Definition: Log.h:147
LogSourc e is the means for a function or any program segment to "connect" to a Log.
Definition: Log.h:308
Logger & start(const TT &... args)
General.
Definition: Log.h:364
Definition: Path.h:112
void importMap(const std::map< std::string, S > &m)
Assign values from a map, overriding existing entries.
Definition: SmartMap.h:252
static void getPaths(const TR &tree, S &container)
Returns a list of the node names matching a pattern. The trailing '/' is NOT appended ie....
Definition: TreeUtils.h:98
A map of Variables.
Definition: VariableMap.h:61
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
static void writeText(const Hi5Tree &src, const std::list< typename Hi5Tree::path_t > &paths, std::ostream &ostr=std::cout)
Dumps the H5 structure, attributes and data properties.
Definition: Hi5.cpp:302
static hid_t getH5StandardType(const std::type_info &type)
Give a native C++ type, returns a standard(?) HDF5 data type.
Definition: Hi5.cpp:128
static drain::image::Image & getPalette(Hi5Tree &dst)
Creates a 256-element RGB palette to be referenced with linkPalette().
Definition: Hi5.cpp:259
static void deleteExcluded(Hi5Tree &src)
Traverse subtree setting exclude=true .
Definition: Hi5.cpp:448
static void parsePath(const std::string &line, Hi5Tree::path_t &path, std::string &attrKey, std::string &attrValue)
Split full path string to path object and attribute key.
Definition: Hi5.cpp:433
static hid_t getH5NativeDataType()
Given a native C++ type, returns a native HDF5 data type.
Definition: Hi5.h:155
static void writeText(const Hi5Tree &src, std::ostream &ostr=std::cout)
Dumps the H5 structure, attributes and data properties. (Calls writeText(src, src....
Definition: Hi5.h:179
static void assignAttribute(Hi5Tree &dst, const std::string &assignment)
Definition: Hi5.cpp:353
static void linkPalette(const Hi5Tree &palette, Hi5Tree &dst)
Links the palette that has been (or will be) created with createPalette().
Definition: Hi5.cpp:294
static void readText(Hi5Tree &src, std::istream &istr=std::cin)
Constructs a tree from formatted text. See writeText().
Definition: Hi5.cpp:311
static void readTextLine(Hi5Tree &src, const std::string &line)
Assign a value with optional type specification.
Definition: Hi5.cpp:332
Rack's hi5 structure that uses Rack classes (Tree, Data, Image).
Definition: Hi5.h:74
bool exclude
Experimental.
Definition: Hi5.h:106
void writeText(std::ostream &ostr=std::cout, const rack::ODIMPath &prefix=rack::ODIMPath()) const
Definition: Hi5.cpp:58