Loading...
Searching...
No Matches
ODIMPath.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#ifndef ODIM_PATH
32#define ODIM_PATH
33
34
35#include <drain/Log.h>
36#include <ostream>
37//#include <cmath>
38#include <string>
39#include <string.h>
40#include <map>
41//#include <algorithm>
42
43#include <drain/util/Path.h>
44#include <drain/util/Flags.h>
45
46
47namespace rack {
48
49
73//struct odim_id {};
74//typedef drain::GlobalFlags<odim_id> odim_flags;
75
76class ODIMPathElem;
77
78std::ostream & operator<<(std::ostream & ostr, const ODIMPathElem & p);
79
80
81
83
84public:
85
86
87 // typedef drain::Flagger flag_t;
88 // typedef drain::Flagger::dict_t dict_t;
89 // typedef drain::Flagger::ivalue_t group_t;
90
92 typedef unsigned int group_t;
93 //typedef drain::Flagger2<drain::SingleFlagger<group_t> > flag_t;
95 typedef flag_t::dict_t dict_t;
96
98 static const group_t NONE = 0;
99
102 static const group_t ROOT = 1;
103
104
106 static const group_t DATASET = 2; // 8; // one bit (16) must be unique
107
109 static const group_t DATA = 4; // 16;
110
112 static const group_t ARRAY = 8; // low index, to appear before QUALITY (below)
113
115 static const group_t QUALITY = 16;
116
119
121 static const group_t ALL_LEVELS = (ROOT | DATASET | DATA); // | QUALITY
122
123
124
125
127 static const group_t WHAT = 32;
128
130 static const group_t WHERE = 64;
131
133 static const group_t HOW = 128; // ylennysmerkki
134
137
138
140 static const group_t ALL_GROUPS = 255; // 511!
141
143 static const group_t PALETTE = 256;
144
146 static const group_t LEGEND = 512;
147
149
152 //static const group_t OTHER = 255 ^ IS_INDEXED; // OTHER is not indexed...
153 static const group_t OTHER = 1024; // Assume OTHER is not indexed.
154 //static const group_t _SCAN = 2048; // Extension
155 //static const group_t _MOMENT = 4096; // Extension
156
157
159 static const group_t IS_INDEXED = (DATASET | DATA | QUALITY ); // | _SCAN | _MOMENT);
160
161
162 static inline
163 bool isIndexed(group_t group){
164 return (group & IS_INDEXED) > 0;
165 }
166
167 static inline
168 bool isQuality(group_t group){
169 return (group == QUALITY);
170 }
171
172
173 static
174 const dict_t & getDictionary();
175
176 group_t group;
177
178
179 // Running index of the element. Applied also as a lower limit in path matching.
180 typedef int index_t;
181 index_t index;
182
183 static
184 const index_t INDEX_MAX;
185
186
187 // Change 2024/01: default index 0->1
188 inline
189 ODIMPathElem(group_t group = ROOT, index_t index = 1) : group(group), index(index){
190 }
191
192 /*
193 inline
194 ODIMPathElem(group_t group, index_t index) : group(group), index(index){ //, indexMax(index) { // root=NONE
195 }
196 */
197
198 inline
199 ODIMPathElem(const ODIMPathElem &e) : group(e.group), index(e.index){ // , indexMax(e.indexMax) {
200 if (e.group == OTHER)
201 currentStr = e.currentStr;
202 }
203
204 inline
205 ODIMPathElem(const std::string &s) : group(ROOT), index(0){ //, indexMax(0){
206 set(s);
207 }
208
209 inline
210 ODIMPathElem(const char *s): group(ROOT), index(0){ //, indexMax(0) {
211 set(s);
212 }
213
214 virtual inline
215 ~ODIMPathElem(){};
216
217 void reset();
218
220
225 template <class T>
226 inline
227 ODIMPathElem & operator=(const T &s){
228 set(s);
229 return *this;
230 }
231
232
234
237 inline
238 ODIMPathElem & operator=(const char *s){
239 set(s);
240 return *this;
241 }
242
243 // ?? Require explicit indexing.
244 // ?? ODIMPathMatcher uses mixed groups, so "data2|what" should result index=2, not index=0.
245
247 /*
248 virtual inline
249 bool set(group_t g, index_t i = 0){
250 group = g;
251 index = i;
252 //indexMax = i; // not relevant, or check
253 if ((i>0) && !isIndexed(g)){
254 drain::Logger mout(__FILE__, __FUNCTION__);
255 mout.note("index (" , i , ") given for non-indexed element:" , *this );
256 return false;
257 }
258 return (g != ODIMPathElem::OTHER);
259 }
260 */
261
263 virtual inline
264 //bool
265 void set(group_t g, index_t i = 0){
266 group = g;
267 index = i;
268 //indexMax = i; // not relevant, or check
269 if ((i>0) && !isIndexed(g)){
270 drain::Logger mout(__FILE__, __FUNCTION__);
271 mout.note("index (" , i , ") given for non-indexed element:" , *this );
272 return; // false;
273 }
274 return; // (g != ODIMPathElem::OTHER);
275 }
276
278
285 virtual inline
286 void set(const std::string &s){
287 reset();
288 //return
289 add(s);
290 }
291
293 inline
294 bool is(group_t g) const {
295 return (group == g);
296 }
297
299 inline
300 bool isRoot() const {
301 return (group == ROOT);
302 }
303
305 inline
306 bool empty() const {
307 return isRoot();
308 }
309
310
312 /*
313 inline
314 bool isUnset() const {
315 return (group == NONE);
316 }
317 */
318
320 inline
321 bool isIndexed() const {
322 return isIndexed(this->group);
323 }
324
326 /*
327 * Warning: DATA and DATASET "belong" to quality?... (QUALITY = DATASET | DATA )
328 */
329 inline
330 bool belongsTo(int groupFilter) const {
331 // Notice: filter must fully "cover" group bits (especially because QUALITY = DATASET | DATA )
332 return ((this->group & groupFilter) == this->group);
333 //return ((this->group & groupFilter) != 0);
334 }
335
336 inline
337 group_t getType() const {
338 return this->group;
339 }
340
341 inline
342 index_t getIndex() const {
343 return index;
344 }
345
347 const std::string & getPrefix() const;
348
349 char getCharCode() const;
350
352 static
353 const std::string & getKey(group_t g);
354
356 inline
357 const std::string & getKey() const {
358 return getKey(this->group);
359 }
360
362 virtual
363 std::ostream & toStream(std::ostream & sstr) const;
364
366 // virtual std::ostream & toStream(std::ostream & sstr) const;
367
368
369 const std::string & str() const {
370 if (this->group != OTHER){ // for OTHER, its already set.
371 std::stringstream sstr;
372 toStream(sstr);
373 //sstr << *this;
374 currentStr = sstr.str();
375 }
376 return currentStr;
377 }
378
379 /*
380 inline
381 const std::string & getStr() const {
382 std::stringstream sstr;
383 toStream(sstr);
384 //sstr << *this;
385 str = sstr.str();
386 return str;
387 }
388 */
389 inline
390 operator const std::string &() const {
391 return str();
392 }
393
395 virtual
396 bool extractPrefix(const std::string &s, bool indexed);
397
398protected:
399
400 //bool
401 void add(const std::string &s);
402
403
405 // In ODIMPathMatcher, an index range will be extracted.
406 virtual
407 void extractIndex(const std::string &s);
408
409 // Experimental. for index int and Range!
410 template <class T>
411 static
412 void extractIndex(const std::string &s, T & idx);
413
414 mutable
415 std::string currentStr;
416
417
418};
419
420template <>
421void ODIMPathElem::extractIndex(const std::string &s, ODIMPathElem::index_t & idx);
422
423
424extern ODIMPathElem odimROOT;
425extern ODIMPathElem odimWHERE;
426extern ODIMPathElem odimWHAT;
427extern ODIMPathElem odimARRAY;
428
430bool operator<(const ODIMPathElem & e1, const ODIMPathElem & e2);
431
432bool operator==(const ODIMPathElem & e1, const ODIMPathElem & e2);
433
434inline
435bool operator!=(const ODIMPathElem & e1, const ODIMPathElem & e2){
436 return ! operator==(e1, e2);
437}
438
439
440inline
441std::ostream & operator<<(std::ostream & ostr, const ODIMPathElem & p) {
442 p.toStream(ostr);
443 return ostr;
444}
445
446inline
447std::istream & operator>>(std::istream & istr, ODIMPathElem & p) {
448 std::string s;
449 drain::StringTools::read<512>(istr, s);
450 //istr >> s;
451 p.set(s);
452 return istr;
453}
454
455
456typedef drain::Path<ODIMPathElem,'/',true,false,true> ODIMPath;
457
458typedef std::list<ODIMPath> ODIMPathList;
459
460typedef std::vector<ODIMPathElem> ODIMPathElemSeq;
461
465typedef std::map<std::string,ODIMPathElem> ODIMPathElemMap;
466
468
469 // Main function
470 inline
471 bool operator()(const ODIMPathElem & p1, const ODIMPathElem & p2) const {
472 // return (p1<p2);
473 return !(p1<p2);
474 }
475
476}; // end class
477
478
480
481public:
482
483 inline
484 ODIMPathElem2(): elangle(0.0) {};
485
486 virtual inline
487 ~ODIMPathElem2(){};
488
489 inline
490 ODIMPathElem2(const ODIMPathElem & elem, const double elangle, const std::string & date, const std::string & time):
491 ODIMPathElem(elem), elangle(elangle), timestamp(date+time) {
492 }
493
494 double elangle;
495 std::string timestamp;
496
497
498};
499
500
501inline
502std::ostream & operator<<(std::ostream & ostr, const ODIMPathElem2 & elem) {
503 return ostr << (const ODIMPathElem &)elem << '-' << elem.timestamp << '-' << elem.elangle;
504}
505
506
508 inline
509 bool operator()(const ODIMPathElem2 & e1, const ODIMPathElem2 & e2) const {
510 return e1.timestamp < e2.timestamp;
511 }
512};
513
515 inline
516 bool operator()(const ODIMPathElem2 & e1, const ODIMPathElem2 & e2) const {
517 return e1.elangle < e2.elangle;
518 }
519};
520
521
522/*
523inline
524std::ostream & operator<<(std::ostream & ostr, const ODIMPath & p) {
525 return p.toStream(ostr);
526}
527*/
528
529
530} // namespace rack
531
532
533#endif
534
535// Rack
Two-way mapping between strings and objects of template class T.
Definition Dictionary.h:63
LogSourc e is the means for a function or any program segment to "connect" to a Log.
Definition Log.h:312
Logger & note(const TT &... args)
For top-level information.
Definition Log.h:489
Definition Path.h:112
Definition Flags.h:70
Definition ODIMPath.h:479
Definition ODIMPath.h:82
const std::string & getKey() const
Debugging/logging. Returns standard name. Does not check if type is OTHER.
Definition ODIMPath.h:357
static const group_t WHAT
Metadata group /what , at any depth.
Definition ODIMPath.h:127
static const group_t IS_INDEXED
Group index mask for groups that have an index.
Definition ODIMPath.h:159
virtual void set(const std::string &s)
Assign a string to this path element.
Definition ODIMPath.h:286
static const group_t LEGEND
Metadata array describing values in the image data (DATA). EXPERIMENTAL.
Definition ODIMPath.h:146
static const group_t DATASET
First level group, /dataset + digit .
Definition ODIMPath.h:106
const std::string & getPrefix() const
Returns the name without the index.
Definition ODIMPath.cpp:198
static const group_t ALL_LEVELS
Abbreviation for linking (referencing) attributes at different levels (tree depths).
Definition ODIMPath.h:121
static const group_t PALETTE
Palette data (to be linked). EXPERIMENTAL.
Definition ODIMPath.h:143
bool is(group_t g) const
Abbreviation of (group == NONE)
Definition ODIMPath.h:294
bool empty() const
Equivalent to isRoot(). This method is required in recognizing the leading empty string....
Definition ODIMPath.h:306
virtual std::ostream & toStream(std::ostream &sstr) const
For string presentation.
Definition ODIMPath.cpp:320
bool isRoot() const
Abbreviation of (group == ROOT)
Definition ODIMPath.h:300
bool belongsTo(int groupFilter) const
Checks if the element belongs to any of groups given.
Definition ODIMPath.h:330
ODIMPathElem & operator=(const char *s)
Calls set(const std::string &) .
Definition ODIMPath.h:238
virtual bool extractPrefix(const std::string &s, bool indexed)
Given the non-numeric prefix of a group, like "dataset" or "data", set the group.
Definition ODIMPath.cpp:144
virtual void set(group_t g, index_t i=0)
The fundamental assignment operator.
Definition ODIMPath.h:265
static const group_t ROOT
Definition ODIMPath.h:102
static const group_t OTHER
User defined group, name stored as a separate string. The string may still contain numbers,...
Definition ODIMPath.h:153
static const group_t HOW
Metadata group /how , at any depth.
Definition ODIMPath.h:133
static const group_t ATTRIBUTE_GROUPS
Group index mask for groups that contain only meta data.
Definition ODIMPath.h:136
static const group_t NONE
None (undefined)
Definition ODIMPath.h:98
unsigned int group_t
In H5, "groups" correspond to directories or folders in file system.
Definition ODIMPath.h:92
bool isIndexed() const
Abbreviation of (group == NONE)
Definition ODIMPath.h:321
const std::string & str() const
Writes the name, including the index, to output stream.
Definition ODIMPath.h:369
static const group_t QUALITY
Special group on first or second level, /quality + digit , used for storing quality data.
Definition ODIMPath.h:115
static const group_t DATA_GROUPS
Group index mask for groups under which data arrays (ARRAY type) are found.
Definition ODIMPath.h:118
static const group_t ALL_GROUPS
User defined group, name stored as a separate string. Index allowed, but only catenated in the string...
Definition ODIMPath.h:140
static const group_t DATA
Second level group, /data + digit .
Definition ODIMPath.h:109
static const group_t WHERE
Metadata group /where , at any depth.
Definition ODIMPath.h:130
static const group_t ARRAY
Data group "data", at deepest level, like /dataset4/data2/quality1/data.
Definition ODIMPath.h:112
virtual void extractIndex(const std::string &s)
Given a string starting with a numeral, try to extract the index.
Definition ODIMPath.cpp:138
ODIMPathElem & operator=(const T &s)
Redirects to set(const std::string &) .
Definition ODIMPath.h:227
Definition DataSelector.cpp:44
bool operator<(const ODIMPathElem &e1, const ODIMPathElem &e2)
Important!
Definition ODIMPath.cpp:259
std::map< std::string, ODIMPathElem > ODIMPathElemMap
Definition ODIMPath.h:465
Definition ODIMPath.h:514
Definition ODIMPath.h:507
Definition ODIMPath.h:467