DataSelector.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 
32 #ifndef RACK_DATASELECTOR
33 #define RACK_DATASELECTOR
34 
35 #include <set>
36 #include <list>
37 #include <map>
38 #include <stdexcept>
39 
40 #include <drain/RegExp.h>
41 #include <drain/Sprinter.h>
42 #include <drain/Type.h>
43 #include <drain/util/BeanLike.h>
44 #include <drain/util/KeySelector.h>
45 #include <drain/util/Range.h>
46 #include <drain/util/ReferenceMap.h>
47 //#include <drain/util/Variable.h>
48 
49 #include "ODIM.h"
50 #include "ODIMPathTools.h"
51 #include "ODIMPathMatcher.h"
52 #include "PolarODIM.h" // elangle
53 
54 
55 namespace rack {
56 
57 
58 struct DataOrder { //: public drain::BeanLike {
59 
60  enum Crit {DATA, ELANGLE, TIME}; // ALTITUDE
61  enum Oper {MIN, MAX};
62 
63  const char separator = ':';
64 
66  CritFlagger criterion;
67 
69  OperFlagger operation;
70 
71  template<typename ... TT>
72  void set(Crit crit, const TT &... args) {
73  criterion = crit;
74  set(args...);
75  };
76 
77  template<typename ... TT>
78  void set(Oper oper, const TT &... args) {
79  operation = oper;
80  set(args...);
81  };
82 
84  inline
85  void set(const std::string s) {
86  std::string s1, s2;
87  drain::StringTools::split2(s, s1, s2, separator);
88  criterion.set(s1);
89  operation.set(s2);
90  set();
91  };
92 
93  void set(){
94  str = criterion.str() + separator + operation.str();
95  }
96 
97  std::string str;
98 
99 };
100 
101 
102 inline
103 std::ostream & operator<<(std::ostream & ostr, const DataOrder & order){
104  ostr << order.str;
105  return ostr;
106 }
107 
109  // QuantitySelector,
113 public:
114 
115  friend class drain::ReferenceMap;
116 
118  enum Prf {SINGLE=1, DOUBLE=2, ANY=3};
119 
120  // TODO: string => ODIMPath
121  DataSelector(const std::string & path, const std::string & quantity,
122  unsigned int count = 1000, drain::Range<double> elangle = {-90.0, 90.0}, int dualPRF = 0,
124  // double elangleMin = -90.0, double elangleMax = 90.0);
125 
126  DataSelector(const std::string & parameters = "");
127 
129  // DataSelector(ODIMPathElem::group_t e, ODIMPathElem::group_t e2=ODIMPathElem::ROOT, ODIMPathElem::group_t e3=ODIMPathElem::ROOT);
130 
131  template<typename ... T>
132  DataSelector(const ODIMPathElem & elem, const T &... args): BeanLike(__FUNCTION__){ //, orderFlags(orderDict,':') {
133  init();
134  //pathMatcher.setElems(elem, rest...);
135  pathMatcher.set(elem, args...);
136  updateBean();
137  }
138 
139  // Either this or previous is unneeded?
140  template<typename ... T>
141  DataSelector(ODIMPathElem::group_t e, const T &... args): BeanLike(__FUNCTION__){ // , orderFlags(orderDict,':') {
142  init();
143  //pathMatcher.setElems(e, rest...);
144  pathMatcher.set(e, args...);
145  updateBean();
146  }
147 
148 
149  DataSelector(const DataSelector & selector);
150 
151  virtual ~DataSelector();
152 
153 
155  // * - \c index - integer value, only changing the \c index member. ???
165  // Experimental. Raise to beanlike?
167 
172  inline
173  bool consumeParameters(std::string & args){
174  if (args.empty()){
175  return false;
176  }
177  else {
178  setParameters(args);
179  args.clear();
180  return true;
181  }
182  }
183 
184  template<typename ... TT>
185  inline
186  void setPathMatcher(TT... args){
187  pathMatcher.set(args...);
188  }
189 
190  inline
191  const ODIMPathMatcher & getPathMatcher() const {
192  return pathMatcher;
193  }
194 
196  inline
198  return pathMatcher.trimHead(true);
199  }
200 
202  void setQuantities(const std::string & s); // , const std::string & separators = ","); // todo: rename (here only) quantities?
203 
204  void setQuantityRegExp(const std::string & s); // todo: rename (here only) quantities?
205 
206  inline
207  bool quantityIsSet() const {
208  return quantitySelector.isSet() || qualitySelector.isSet();
209  }
210 
212 
216  inline
217  const std::string & getQuantity() const {
218  return quantities;
219  }
220 
221  inline
222  const drain::KeySelector & getQuantitySelector() const {
223  return quantitySelector;
224  }
225 
226  inline
227  const drain::KeySelector & getQualitySelector() const {
228  return qualitySelector;
229  }
230 
231  inline
232  void setMaxCount(unsigned int i){
233  count = i;
234  }
235 
236  inline
237  unsigned int getMaxCount() const {
238  return count;
239  }
240 
241 
242  inline
243  void setPrf(const std::string & s){
244  this->prf = s;
245  this->prfSelector.set(s);
246  }
247 
248  inline
249  void setPrf(Prf prf){
250  this->prfSelector.set(prf);
251  this->prf = this->prfSelector.str();
252  //this->updateBean();
253  }
254 
255  template<typename ... TT>
256  inline
257  void setOrder(const TT &... args) {
258  this->order.set(args...);
259  }
260 
261  inline
262  const DataOrder & getOrder() const {
263  return order;
264  }
265 
266 
268 
271  void selectPaths(const Hi5Tree & src, std::list<ODIMPath> & pathContainer) const;
272 
273 
274  // TODO add filter, allowing ODIMPathElem::group_t == QUALITY
275  static
276  void getTimeMap(const Hi5Tree & srcRoot, ODIMPathElemMap & m);
277 
278 
280 
284  virtual
285  void updateBean() const;
286 
288  void ensureDataGroup();
289 
291 
294  void reset(); // bool flexible = true "inclusive"
295 
296 
298  // TODO: perhaps semantics unclear. Consider separating to updateParameters/ deriveImplicitParameters etc.
309 
329  void getPaths(const Hi5Tree & src, std::list<ODIMPath> & pathContainer) const;
330 
331 
333 
338  bool getPath(const Hi5Tree & src, ODIMPath & path) const;
339 
340 
341 
344 
346  bool getLastPath(const Hi5Tree & src, ODIMPath & path, ODIMPathElem::group_t group = ODIMPathElem::DATA ) const;
347 
349  bool getNextPath(const Hi5Tree & src, ODIMPath & path, ODIMPathElem::group_t group = ODIMPathElem::DATA ) const;
350 
351 
352 
353 
355 
363  static
364  void swapData(Hi5Tree & src,const ODIMPathElem &srcElem, Hi5Tree & dst);
365 
367  static
368  void swapData(Hi5Tree & srcGroup, Hi5Tree & dst, ODIMPathElem::group_t groupType);
369 
370 
371 protected:
372 
373 
375 
378  //mutable
379  std::string path; // temporary!
380 
381  mutable
382  ODIMPathMatcher pathMatcher;
383 
385  mutable
386  std::string quantities;
387 
388  mutable
389  drain::KeySelector quantitySelector;
390 
391  mutable
392  drain::KeySelector qualitySelector;
393 
394 
396  unsigned int count;
397 
398 
401 
402  mutable DataOrder order;
403 
405  std::string prf;
406 
407  mutable
409 
412 
414 
423  void updateQuantities() const; // todo: rename (here only) quantities?
424  // void updateQuantities(const std::string & separators = ",") const ;
425 
427  void init();
428 
429 
431 
435  bool collectPaths(const Hi5Tree & src, std::list<ODIMPath> & pathContainer, const ODIMPath & basepath = ODIMPath(), const std::string & parentQuantity="") const;
436 
437  bool collectPathsOLD(const Hi5Tree & src, std::list<ODIMPath> & pathContainer, const ODIMPath & basepath = ODIMPath()) const;
438 
439 
441  void prunePaths(const Hi5Tree & src, std::list<ODIMPath> & pathContainer) const;
442 
443 
444 
445 };
446 
447 std::ostream & operator<<(std::ostream & ostr, const DataSelector &selector);
448 
449 
450 
451 // Experimental
453 
454 public:
455 
457  drain::Logger mout(__FILE__, __FUNCTION__);
458  parameters.delink("path");
459  mout.info("experimental: not re-setting DATASET" );
460  //pathMatcher.setElems(ODIMPathElem::DATASET);
461  }
462 
463 };
464 
465 // Experimental
466 /*
467 class ImageSelector : public DataSelector {
468 
469 public:
470 
471  ImageSelector() : DataSelector(ODIMPathElem::DATA | ODIMPathElem::QUALITY) {
472  parameters.link("path", dummy);
473  //parameters.delink("path");
474  // pathMatcher.setElems(ODIMPathElem::DATASET);
475  }
476 
477 
478  // Convert path and quantity strings to pathMatcher and quantity regexps, respectively.
479  virtual
480  void updateBean();
481 
482 private:
483 
484  std::string dummy;
485 
486 };
487 */
488 
489 } // rack::
490 
491 namespace drain {
492  DRAIN_TYPENAME(rack::DataSelector);
493 }
494 
495 #endif /* DATASELECTOR_H_ */
Something which has a name, a description and possibly some parameters of varying type.
Definition: BeanLike.h:60
void setParameters(std::initializer_list< Variable::init_pair_t > args)
Grants access to (if above hidden)
Definition: BeanLike.h:131
Flagger accepting values of enum type E.
Definition: Flags.h:763
LogSourc e is the means for a function or any program segment to "connect" to a Log.
Definition: Log.h:308
Definition: Path.h:112
void trimHead(bool COMPLETE=false)
Removes leading empty elements. The resulting string presentation will not start with the separator.
Definition: Path.h:321
Definition: ReferenceMap.h:207
void delink(const std::string &key)
Removes an entry from the map.
Definition: ReferenceMap.h:342
virtual const key_t & str() const
String corresponding the current value. Returns empty, if not found.
Definition: Flags.h:555
static bool split2(const std::string &s, T1 &first, T2 &second, const C &separators, const std::string &trimChars=" \t\n")
Splits and trims a given std::string to a std Sequence.
Tool for selecting datasets based on paths, quantities and min/max elevations.
Definition: DataSelector.h:112
void init()
Sets the default values and sets references.
Definition: DataSelector.cpp:135
Prf
Pulse repetition frequency mode.
Definition: DataSelector.h:118
unsigned int count
The maximum length of the list of matching keys.
Definition: DataSelector.h:396
virtual void updateBean() const
Updates member objects with their corresponding variable values .
Definition: DataSelector.cpp:184
DataSelector(const ODIMPathElem &elem, const T &... args)
Inits pathmatcher.
Definition: DataSelector.h:132
void setQuantities(const std::string &s)
Sets basic quantities and quality quantities. These sets are separated by '/'.
Definition: DataSelector.cpp:282
std::string prf
Reject or accept VRAD(VH)
Definition: DataSelector.h:405
const std::string & getQuantity() const
Retrieve quantity list and regular expression, if defined.
Definition: DataSelector.h:217
std::string path
Regular expression of accepted paths, for example ".*‍/data$". Deprecated.
Definition: DataSelector.h:379
void updateQuantities() const
Continue path matching started with getMainPaths()
Definition: DataSelector.cpp:290
void getPaths(const Hi5Tree &src, std::list< ODIMPath > &pathContainer) const
Sets given parameters and implicitly determines missing parameters.
Definition: DataSelector.cpp:1126
bool getNextPath(const Hi5Tree &src, ODIMPath &path, ODIMPathElem::group_t group=ODIMPathElem::DATA) const
Returns the a path with index one greater than the retrieved last path.
Definition: DataSelector.cpp:1214
bool consumeParameters(std::string &args)
Sets parameters in predefined order or sets specified parameters. (Python style calling alternatives....
Definition: DataSelector.h:173
void selectPaths(const Hi5Tree &src, std::list< ODIMPath > &pathContainer) const
Collect paths with all the criteria: path, elevation(range), PRF, quantity...
Definition: DataSelector.cpp:359
static void swapData(Hi5Tree &src, const ODIMPathElem &srcElem, Hi5Tree &dst)
Swap branches such that dst gets a /dataset or /data with a new index.
Definition: DataSelector.cpp:1240
void trimPathMatcher()
"Drop leading slashes", ie. remove leading empty elements.
Definition: DataSelector.h:197
void ensureDataGroup()
In path, ensure trailing DATA or QUANTITY element.
Definition: DataSelector.cpp:230
drain::Range< int > timespan
Time in seconds, compared to nominal time.
Definition: DataSelector.h:411
bool getPath(const Hi5Tree &src, ODIMPath &path) const
Returns the first path encountered with selector attributes and given groupFilter .
Definition: DataSelector.cpp:1135
bool getLastPath(const Hi5Tree &src, ODIMPath &path, ODIMPathElem::group_t group=ODIMPathElem::DATA) const
Returns the last path encountered with selector attributes and given groupFilter .
Definition: DataSelector.cpp:1173
void reset()
Restore default values.
Definition: DataSelector.cpp:156
void prunePaths(const Hi5Tree &src, std::list< ODIMPath > &pathContainer) const
Use #DataOrder::criterion DATA , TIME or ELANGLE and #DataOrder::order MIN or MAX to sort paths.
Definition: DataSelector.cpp:930
drain::Range< double > elangle
The minimum and maximum elevation angle (applicable with volume scan data only).
Definition: DataSelector.h:400
bool collectPaths(const Hi5Tree &src, std::list< ODIMPath > &pathContainer, const ODIMPath &basepath=ODIMPath(), const std::string &parentQuantity="") const
Collect paths (only) with criteria: path, elevation(range), PRF, quantity.
Definition: DataSelector.cpp:375
std::string quantities
Comma-separated list of conventional quantities, optionally followed by '/', and quality quantities.
Definition: DataSelector.h:386
Definition: DataSelector.h:452
Definition: ODIMPath.h:82
static const group_t DATASET
First level group, /dataset + digit .
Definition: ODIMPath.h:103
drain::Flagger::ivalue_t group_t
In H5, "groups" correspond to "directories" or "folders" in Unix and Windows.
Definition: ODIMPath.h:91
static const group_t DATA
Second level group, /data + digit .
Definition: ODIMPath.h:106
Structure for testing if a path matches a given sequence of path elements.
Definition: ODIMPathMatcher.h:182
Definition: DataSelector.cpp:1277
Definition: DataSelector.cpp:44
std::map< std::string, ODIMPathElem > ODIMPathElemMap
Definition: ODIMPath.h:462
Definition: DataSelector.h:58
void set(const std::string s)
Expects <crit>[:<oper>].
Definition: DataSelector.h:85