Loading...
Searching...
No Matches
Hi5.h
1/*
2
3MIT License
4
5Copyright (c) 2017 FMI Open Development / Markus Peura, first.last@fmi.fi
6
7Permission is hereby granted, free of charge, to any person obtaining a copy
8of this software and associated documentation files (the "Software"), to deal
9in the Software without restriction, including without limitation the rights
10to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
11copies of the Software, and to permit persons to whom the Software is
12furnished to do so, subject to the following conditions:
13
14The above copyright notice and this permission notice shall be included in all
15copies or substantial portions of the Software.
16
17THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
18IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
19FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
20AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
21LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
22OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
23SOFTWARE.
24
25*/
26/*
27Part of Rack development has been done in the BALTRAD projects part-financed
28by the European Union (European Regional Development Fund and European
29Neighbourhood 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#include <list>
40#include <map>
41#include <string>
42
43#include <hdf5.h>
44
45#include <drain/Log.h>
46#include <drain/image/Image.h>
47#include <drain/util/FileInfo.h>
48#include <drain/util/TreeOrdered.h>
49
50#include "data/ODIMPath.h"
51
52
53namespace hi5 {
54
55extern
56drain::Log & getLogH5();
57
58
59extern
60const drain::FileInfo fileInfo;
61
63
71struct NodeHi5 {
72
74
76
77 // Required, but not much needed.
78 bool empty() const {
79 return (image.isEmpty() && attributes.empty());
80 }
81
82 drain::VariableMap attributes;
83
84 NodeHi5() : exclude(false) {};
85
86 inline
87 NodeHi5(const NodeHi5 & n) : exclude(n.exclude) {
88 attributes.importMap(n.attributes);
89 };
90
91 inline
92 NodeHi5 & operator=(const hi5::NodeHi5 & node){
93 if (&node != this){
94 attributes.importMap(node.attributes);
95 }
96 return *this;
97 };
98
99 //void writeText(std::ostream & ostr = std::cout, const std::string & prefix = "") const;
100 void writeText(std::ostream & ostr = std::cout, const rack::ODIMPath & prefix = rack::ODIMPath()) const;
101
103
107
109 typedef std::map<std::string, rack::ODIMPathElem> lookup_t;
110 // static
111 // lookup_t lookUp;
112
113};
114
115}
116
117
119typedef drain::OrderedTree<hi5::NodeHi5, false, rack::ODIMPath> Hi5Tree;
120
121namespace drain {
122
123
124/*
125template <> // for T (Tree class)
126template <> // for K (path elem arg)
127inline
128const image::TreeSVG::key_t & image::TreeSVG::getKey(const ClassXML & cls){
129 // return Enum<image::svg::tag_t>::dict.getKey(type, false);
130 return image::TreeSVG::getKey(cls.strPrefixed());
131}
132*/
133
134// Experimental implementations, based on above example
135
136// Basic idea: a static map is maintained. TODO: check its contents upon rack exit.
137template <> // for T (Tree class)
138template <> // for K (path elem arg)
139const rack::ODIMPathElem & Hi5Tree::getKey(const std::string & key);
140
141template <> // for T (Tree class)
142template <> // for K (path elem arg)
143const rack::ODIMPathElem & Hi5Tree::getKey(const rack::ODIMPathElem::group_t & key);
144
145
146/*
147template <>
148template <typename K> // for K (path elem arg)
149const Hi5Tree::key_t & Hi5Tree::getKey(const K & key){
150 static const rack::ODIMPathElem elem(key); // dangerous
151 return elem;
152}
153*/
154
155/*
156template <>
157template <> // for K (path elem arg)
158inline
159const Hi5Tree::key_t & Hi5Tree::getKey(const rack::ODIMPathElem::group_t & key){
160 static const rack::ODIMPathElem elem(key); // dangerous
161 return elem;
162}
163
164template <> // for T (Tree class)
165template <> // for K (path elem arg)
166inline
167const Hi5Tree & Hi5Tree::operator[](const std::string & s) const {
168 return (*this)[rack::ODIMPathElem(s)];
169 //return (*this)[Enum<rack::RackSVG::TitleClass>::dict.getKey(x, false)];
170}
171
172template <> // for T (Tree class)
173template <> // for K (path elem arg)
174inline
175Hi5Tree & Hi5Tree::operator[](const std::string & s) {
176 return (*this)[rack::ODIMPathElem(s)];
177 //return (*this)[Enum<rack::RackSVG::TitleClass>::dict.getKey(x, false)];
178}
179*/
180
181}
182
183namespace hi5 {
184
185
187class Hi5Base {
188
189public:
190
191 static
192 void handleStatus(herr_t status, const std::string & message, drain::Logger &mout, int lineNo=0);
193
194 template <int L, class T>
195 static inline
196 // void handleStatus(drain::Logger &mout, herr_t status, const std::string & message, const drain::Path<> & path, int lineNo=0);
197 void handleStatus(drain::Logger &mout, herr_t status, const std::string & message, const T & arg, int lineNo=0){
198
199 if (status >= 0)
200 return;
201
202 mout.start<L>() << message << ": " << arg;
203 if (lineNo)
204 mout << ", line=" << lineNo;
205 mout << ", status=" << status << mout.endl;
206
207 }
208
210 static
211 hid_t getH5StandardType(const std::type_info &type);
212
214 static
215 hid_t getH5NativeDataType(const std::type_info &t);
216
218 template <class T>
219 static
221 return getH5NativeDataType(typeid(T));
222 }
223
224 static
225 hid_t getH5StringVariableLength();
226
227
229 static
230 //Hi5Tree & getPalette(Hi5Tree & dst);
231 drain::image::Image & getPalette(Hi5Tree & dst);
232
234 static
235 void linkPalette(const Hi5Tree & palette, Hi5Tree & dst);
236
237
239 static
240 void writeText(const Hi5Tree &src, const std::list<typename Hi5Tree::path_t> & paths, std::ostream & ostr = std::cout);
241
243 static
244 void writeText(const Hi5Tree &src, std::ostream & ostr = std::cout){
245 std::list<typename Hi5Tree::path_t> paths;
246 drain::TreeUtils::getPaths(src, paths);
247 writeText(src, paths, ostr);
248 };
249
250
252
255 static
256 void readText(Hi5Tree &src, std::istream & istr = std::cin);
257
258
260 // consider ValueReader, TextReader instead (skipping attrType)
272 static
273 void parsePath(const std::string & line, Hi5Tree::path_t & path, std::string & attrKey, std::string & attrValue);
274
275
277
281 static
282 void readTextLine(Hi5Tree &src, const std::string & line);
283
284 static
285 //void readTextLine(Hi5Tree & dst, const Hi5Tree::path_t & path, const std::string & assignment);
286 void assignAttribute(Hi5Tree & dst, const std::string & assignment);
287
288 // static void readTextLine(Hi5Tree & dst, const Hi5Tree::path_t & path, const std::string & key, const std::string & value);
289
291 /*
292 static
293 void markExcluded(Hi5Tree &src, bool exclude=true);
294 */
295
296
297
299 static
300 void deleteExcluded(Hi5Tree &src);
301
302
303};
304
305
307std::ostream & operator<<(std::ostream &ostr, const hi5::NodeHi5 &n);
308
310std::ostream & operator<<(std::ostream &ostr, const Hi5Tree & tree);
311
312
313/*
314struct lessAlphaNum {
315
316 enum comparison {LESS=-1, EQUAL=0, GREATER=+1};
317
318 inline
319 static
320 bool isNumeric(char c) {
321 return ((c >= '0') && (c <= '9'));
322 };
323
324 inline
325 static
326 comparison compareAlphaBetical(std::string::const_iterator & c1, std::string::const_iterator end1,
327 std::string::const_iterator & c2, std::string::const_iterator end2){
328
329
330 while (c1 != end1){
331
332 if (c2 == end2){
333 return GREATER; // str1 longer
334 }
335
336 // Stop if both are numeric
337 if (isNumeric(*c1) && isNumeric(*c2)){
338 return EQUAL; // that is, equal as to the alpha part.
339 }
340
341 if (*c1 < *c2)
342 return LESS;
343 else if (*c1 > *c2)
344 return GREATER;
345
346 // *c1 == *c2 : strs are equal, non-numeric this far.
347
348 ++c1;
349 ++c2;
350
351 }
352
353 if (c2 != end2) // str2 is longer
354 return LESS;
355 else
356 return EQUAL;
357
358 }
359
360 inline
361 static
362 comparison compareNumeric(std::string::const_iterator & c1, std::string::const_iterator end1,
363 std::string::const_iterator & c2, std::string::const_iterator end2){
364
365 const std::string::const_iterator n1 = c1;
366 const std::string::const_iterator n2 = c2;
367 size_t length1 = 0;
368 size_t length2 = 0;
369
370 // scan to end of numeric segment
371 while ((c1 != end1) && isNumeric(*c1)){
372 ++c1;
373 // todo: if undetectValue padding?
374 ++length1;
375 }
376
377 // scan to end of numeric segment
378 while ((c2 != end2) && isNumeric(*c2)){
379 ++c2;
380 // todo: if undetectValue padding?
381 ++length2;
382 }
383
384
385
386 if (length1 < length2)
387 return LESS;
388 else if (length1 > length2)
389 return GREATER;
390 else {
391 // std::cout << "Equal lengths. Compare element by element\n";
392 // end points of numsegs
393 const std::string::const_iterator ne1 = c1;
394 const std::string::const_iterator ne2 = c2;
395 // rescan from start
396 c1 = n1;
397 c2 = n2;
398 while ((c1 != ne1) && (c2 != ne2)){ //
399
400 if (*c1 < *c2)
401 return LESS;
402 else if (*c1 > *c2)
403 return GREATER;
404 ++c1;
405 ++c2;
406 }
407
408 return EQUAL;
409
410 }
411
412 }
413
414 // Main function
415 bool operator()(const std::string & s1, const std::string & s2) const {
416
417 const std::string::const_iterator end1 = s1.end();
418 const std::string::const_iterator end2 = s2.end();
419 std::string::const_iterator c1 = s1.begin();
420 std::string::const_iterator c2 = s2.begin();
421
422 comparison result;
423
424 // debugging
425 std::string d1, d2;
426
427 //while ((c1 != end1) && (c2 != end2)){
428 //int n = 10;
429 //while (n > 0){
430 //--n;
431 while (true){
432
433 //std::cout << "Alphabetic check\n";
434 //std::cout << d1.assign(c1, end1) << " vs. " << d2.assign(c2, end2) << '\n';
435
436 result = compareAlphaBetical(c1, end1, c2, end2);
437
438 if (result == LESS)
439 return true;
440 if (result == GREATER)
441 return false;
442
443 //if (c1 == end1) // s1 ends, so s2 same this far (ie. same or longer)
444 if (c2 == end2) // s2 gives up, so s1 same this far (ie. same or longer)
445 return false;
446
447 // std::cout << "Numeric part check\n";
448 //std::cout << d1.assign(c1, end1) << " vs. " << d2.assign(c2, end2) << '\n';
449
450
451 result = compareNumeric(c1, end1, c2, end2);
452
453 if (result == LESS)
454 return true;
455 if (result == GREATER)
456 return false;
457
458 //if (c1 == end1) // s1 ends, so s2 same this far (ie. same or longer)
459 if (c2 == end2) // s2 gives up, so s1 same this far (ie. same or longer)
460 return false;
461
462 // std::cout << "Equal, continuing \n";
463 }
464
465
466
467
468 } // end operator()
469
470}; // end class
471*/
472
473
474} // ::hi5
475
476
477#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:313
Logger & start(const TT &... args)
General.
Definition Log.h:369
Definition Path.h:137
void importMap(const std::map< std::string, S > &m)
Assign values from a map, overriding existing entries.
Definition SmartMap.h:255
static void getPaths(const TR &tree, S &container)
Retrieve all the paths.
Definition TreeUtils.h:68
A map of Variables.
Definition VariableMap.h:61
Class for multi-channel digital images. Supports dynamic typing with base types (char,...
Definition Image.h:193
Base class for Reader and Writer, essentially just wrapping some utilities.
Definition Hi5.h:187
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:380
static hid_t getH5StandardType(const std::type_info &type)
Give a native C++ type, returns a standard(?) HDF5 data type.
Definition Hi5.cpp:207
static drain::image::Image & getPalette(Hi5Tree &dst)
Creates a 256-element RGB palette to be referenced with linkPalette().
Definition Hi5.cpp:337
static void deleteExcluded(Hi5Tree &src)
Traverse subtree setting exclude=true .
Definition Hi5.cpp:526
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:511
static hid_t getH5NativeDataType()
Given a native C++ type, returns a native HDF5 data type.
Definition Hi5.h:220
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:244
static void assignAttribute(Hi5Tree &dst, const std::string &assignment)
Definition Hi5.cpp:431
static void linkPalette(const Hi5Tree &palette, Hi5Tree &dst)
Links the palette that has been (or will be) created with createPalette().
Definition Hi5.cpp:372
static void readText(Hi5Tree &src, std::istream &istr=std::cin)
Constructs a tree from formatted text. See writeText().
Definition Hi5.cpp:389
static void readTextLine(Hi5Tree &src, const std::string &line)
Assign a value with optional type specification.
Definition Hi5.cpp:410
Definition ODIMPath.h:82
unsigned int group_t
In H5, "groups" correspond to directories or folders in file system.
Definition ODIMPath.h:92
Definition DataSelector.cpp:1277
Rack's hi5 structure that uses Rack classes (Tree, Data, Image).
Definition Hi5.h:71
bool exclude
Marker for excluding from any "serious" file write.
Definition Hi5.h:106
std::map< std::string, rack::ODIMPathElem > lookup_t
Experimental.
Definition Hi5.h:109
void writeText(std::ostream &ostr=std::cout, const rack::ODIMPath &prefix=rack::ODIMPath()) const
Definition Hi5.cpp:137