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