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
73 NodeHi5() : exclude(false) {};
74
75 inline
76 NodeHi5(const NodeHi5 & n) : exclude(n.exclude) {
77 attributes.importMap(n.attributes);
78 };
79
80
81
82 // Required, but not much needed.
83 bool empty() const {
84 return (image.isEmpty() && attributes.empty());
85 }
86
87
88
89 inline
90 NodeHi5 & operator=(const hi5::NodeHi5 & node){
91 if (&node != this){
92 attributes.importMap(node.attributes);
93 }
94 return *this;
95 };
96
97 //void writeText(std::ostream & ostr = std::cout, const std::string & prefix = "") const;
98 void writeText(std::ostream & ostr, const std::string & prefix) const;
99
100 inline
101 void writeText(std::ostream & ostr = std::cout, const rack::ODIMPath & prefix = rack::ODIMPath()) const {
102 writeText(ostr, prefix.str());
103 }
104
105
106 void swap(NodeHi5 & n);
107
109
110 image_type image;
111
112 drain::VariableMap attributes;
113
115
120
122 typedef std::map<std::string, rack::ODIMPathElem> lookup_t;
123
124};
125
126
127
128}
129
130
132typedef drain::OrderedTree<hi5::NodeHi5, false, rack::ODIMPath> Hi5Tree;
133
134/*
135*/
136
137
138namespace drain {
139
140template <> inline
141void Hi5Tree::swapData(hi5::NodeHi5 & node){
142 data.swap(node);
143}
144
145
146template <> inline
147bool Hi5Tree::empty() const {
148 return ((data.empty() && !hasChildren()) || data.exclude);
149 //return (!hasChildren()) || data.exclude;
150}
151
152
153// Basic idea: a static map is maintained. TODO: check its contents upon rack exit.
154template <> // for T (Tree class)
155template <> // for K (path elem arg)
156const rack::ODIMPathElem & Hi5Tree::getKey(const std::string & key);
157
158// Experimental implementation based on static dictionary.
159template <> // for T (Tree class)
160template <> // for K (path elem arg)
161const rack::ODIMPathElem & Hi5Tree::getKey(const rack::ODIMPathElem::group_t & key);
162
163}
164
165namespace hi5 {
166
167
169class Hi5Base {
170
171public:
172
173 static
174 void handleStatus(herr_t status, const std::string & message, drain::Logger &mout, int lineNo=0);
175
176 template <int L, class T>
177 static inline
178 // void handleStatus(drain::Logger &mout, herr_t status, const std::string & message, const drain::Path<> & path, int lineNo=0);
179 void handleStatus(drain::Logger &mout, herr_t status, const std::string & message, const T & arg, int lineNo=0){
180
181 if (status >= 0)
182 return;
183
184 mout.start<L>() << message << ": " << arg;
185 if (lineNo)
186 mout << ", line=" << lineNo;
187 mout << ", status=" << status << mout.endl;
188
189 }
190
192 static
193 hid_t getH5StandardType(const std::type_info &type);
194
196 static
197 hid_t getH5NativeDataType(const std::type_info &t);
198
200 template <class T>
201 static
203 return getH5NativeDataType(typeid(T));
204 }
205
206 static
207 hid_t getH5StringVariableLength();
208
209
211 static
212 //Hi5Tree & getPalette(Hi5Tree & dst);
213 drain::image::Image & getPalette(Hi5Tree & dst);
214
216 static
217 void linkPalette(const Hi5Tree & palette, Hi5Tree & dst);
218
219
221 static
222 bool isExcluded(const Hi5Tree &src, const typename Hi5Tree::path_t & path);
223
225 static
226 void writeText(const Hi5Tree &src, const std::list<typename Hi5Tree::path_t> & paths, std::ostream & ostr = std::cout);
227
229 static
230 void writeText(const Hi5Tree &src, std::ostream & ostr = std::cout){
231 std::list<typename Hi5Tree::path_t> paths;
232 drain::TreeUtils::getPaths(src, paths); // Note: also accepts 'excluded' nodes (TreeUtils cannot check it)
233 writeText(src, paths, ostr);
234 };
235
236
238
241 static
242 void readText(Hi5Tree &src, std::istream & istr = std::cin);
243
244
246 // consider ValueReader, TextReader instead (skipping attrType)
258 static
259 void parsePath(const std::string & line, Hi5Tree::path_t & path, std::string & attrKey, std::string & attrValue);
260
261
263
267 static
268 void readTextLine(Hi5Tree &src, const std::string & line);
269
270 static
271 //void readTextLine(Hi5Tree & dst, const Hi5Tree::path_t & path, const std::string & assignment);
272 void assignAttribute(Hi5Tree & dst, const std::string & assignment);
273
274 // static void readTextLine(Hi5Tree & dst, const Hi5Tree::path_t & path, const std::string & key, const std::string & value);
275
277 /*
278 static
279 void markExcluded(Hi5Tree &src, bool exclude=true);
280 */
281
282
283
285 static
286 void deleteExcluded(Hi5Tree &src);
287
288
289};
290
291
293std::ostream & operator<<(std::ostream &ostr, const hi5::NodeHi5 &n);
294
296std::ostream & operator<<(std::ostream &ostr, const Hi5Tree & tree);
297
298
299/*
300struct lessAlphaNum {
301
302 enum comparison {LESS=-1, EQUAL=0, GREATER=+1};
303
304 inline
305 static
306 bool isNumeric(char c) {
307 return ((c >= '0') && (c <= '9'));
308 };
309
310 inline
311 static
312 comparison compareAlphaBetical(std::string::const_iterator & c1, std::string::const_iterator end1,
313 std::string::const_iterator & c2, std::string::const_iterator end2){
314
315
316 while (c1 != end1){
317
318 if (c2 == end2){
319 return GREATER; // str1 longer
320 }
321
322 // Stop if both are numeric
323 if (isNumeric(*c1) && isNumeric(*c2)){
324 return EQUAL; // that is, equal as to the alpha part.
325 }
326
327 if (*c1 < *c2)
328 return LESS;
329 else if (*c1 > *c2)
330 return GREATER;
331
332 // *c1 == *c2 : strs are equal, non-numeric this far.
333
334 ++c1;
335 ++c2;
336
337 }
338
339 if (c2 != end2) // str2 is longer
340 return LESS;
341 else
342 return EQUAL;
343
344 }
345
346 inline
347 static
348 comparison compareNumeric(std::string::const_iterator & c1, std::string::const_iterator end1,
349 std::string::const_iterator & c2, std::string::const_iterator end2){
350
351 const std::string::const_iterator n1 = c1;
352 const std::string::const_iterator n2 = c2;
353 size_t length1 = 0;
354 size_t length2 = 0;
355
356 // scan to end of numeric segment
357 while ((c1 != end1) && isNumeric(*c1)){
358 ++c1;
359 // todo: if undetectValue padding?
360 ++length1;
361 }
362
363 // scan to end of numeric segment
364 while ((c2 != end2) && isNumeric(*c2)){
365 ++c2;
366 // todo: if undetectValue padding?
367 ++length2;
368 }
369
370
371
372 if (length1 < length2)
373 return LESS;
374 else if (length1 > length2)
375 return GREATER;
376 else {
377 // std::cout << "Equal lengths. Compare element by element\n";
378 // end points of numsegs
379 const std::string::const_iterator ne1 = c1;
380 const std::string::const_iterator ne2 = c2;
381 // rescan from start
382 c1 = n1;
383 c2 = n2;
384 while ((c1 != ne1) && (c2 != ne2)){ //
385
386 if (*c1 < *c2)
387 return LESS;
388 else if (*c1 > *c2)
389 return GREATER;
390 ++c1;
391 ++c2;
392 }
393
394 return EQUAL;
395
396 }
397
398 }
399
400 // Main function
401 bool operator()(const std::string & s1, const std::string & s2) const {
402
403 const std::string::const_iterator end1 = s1.end();
404 const std::string::const_iterator end2 = s2.end();
405 std::string::const_iterator c1 = s1.begin();
406 std::string::const_iterator c2 = s2.begin();
407
408 comparison result;
409
410 // debugging
411 std::string d1, d2;
412
413 //while ((c1 != end1) && (c2 != end2)){
414 //int n = 10;
415 //while (n > 0){
416 //--n;
417 while (true){
418
419 //std::cout << "Alphabetic check\n";
420 //std::cout << d1.assign(c1, end1) << " vs. " << d2.assign(c2, end2) << '\n';
421
422 result = compareAlphaBetical(c1, end1, c2, end2);
423
424 if (result == LESS)
425 return true;
426 if (result == GREATER)
427 return false;
428
429 //if (c1 == end1) // s1 ends, so s2 same this far (ie. same or longer)
430 if (c2 == end2) // s2 gives up, so s1 same this far (ie. same or longer)
431 return false;
432
433 // std::cout << "Numeric part check\n";
434 //std::cout << d1.assign(c1, end1) << " vs. " << d2.assign(c2, end2) << '\n';
435
436
437 result = compareNumeric(c1, end1, c2, end2);
438
439 if (result == LESS)
440 return true;
441 if (result == GREATER)
442 return false;
443
444 //if (c1 == end1) // s1 ends, so s2 same this far (ie. same or longer)
445 if (c2 == end2) // s2 gives up, so s1 same this far (ie. same or longer)
446 return false;
447
448 // std::cout << "Equal, continuing \n";
449 }
450
451
452
453
454 } // end operator()
455
456}; // end class
457*/
458
459
460} // ::hi5
461
462
463#endif
Definition FileInfo.h:47
Handler for notifications sent by a Logger.
Definition Log.h:159
LogSourc e is the means for a function or any program segment to "connect" to a Log.
Definition Log.h:326
Logger & start(const TT &... args)
General.
Definition Log.h:378
Definition Path.h:135
void importMap(const std::map< std::string, S > &m)
Assign values from a map, overriding existing entries.
Definition SmartMap.h:253
static void getPaths(const TR &tree, S &container)
Retrieve all the paths.
Definition TreeUtils.h:62
A map of Variables.
Definition VariableMap.h:61
Class for multi-channel digital images. Supports dynamic typing with base types (char,...
Definition Image.h:183
Base class for Reader and Writer, essentially just wrapping some utilities.
Definition Hi5.h:169
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:408
static hid_t getH5StandardType(const std::type_info &type)
Give a native C++ type, returns a standard(?) HDF5 data type.
Definition Hi5.cpp:218
static drain::image::Image & getPalette(Hi5Tree &dst)
Creates a 256-element RGB palette to be referenced with linkPalette().
Definition Hi5.cpp:348
static void deleteExcluded(Hi5Tree &src)
Traverse subtree setting exclude=true .
Definition Hi5.cpp:563
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:548
static hid_t getH5NativeDataType()
Given a native C++ type, returns a native HDF5 data type.
Definition Hi5.h:202
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:230
static bool isExcluded(const Hi5Tree &src, const typename Hi5Tree::path_t &path)
Check is the path contains at least one exclude marker, meaning that data at the path should be bypas...
Definition Hi5.cpp:390
static void assignAttribute(Hi5Tree &dst, const std::string &assignment)
Definition Hi5.cpp:468
static void linkPalette(const Hi5Tree &palette, Hi5Tree &dst)
Links the palette that has been (or will be) created with createPalette().
Definition Hi5.cpp:383
static void readText(Hi5Tree &src, std::istream &istr=std::cin)
Constructs a tree from formatted text. See writeText().
Definition Hi5.cpp:426
static void readTextLine(Hi5Tree &src, const std::string &line)
Assign a value with optional type specification.
Definition Hi5.cpp:447
A map of radar data, indexed by quantity code (DBZH, VRAD, etc).
Definition Data.h:1293
DOC: Add class description.
Definition ODIMPath.h:82
Definition Data.h:1473
Rack's hi5 structure that uses Rack classes (Tree, Data, Image).
Definition Hi5.h:71
void writeText(std::ostream &ostr, const std::string &prefix) const
Definition Hi5.cpp:147
bool exclude
Marker for excluding from any "serious" file write.
Definition Hi5.h:119
std::map< std::string, rack::ODIMPathElem > lookup_t
Experimental.
Definition Hi5.h:122