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