Loading...
Searching...
No Matches
DataSelector.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#ifndef RACK_DATASELECTOR
33#define RACK_DATASELECTOR
34
35#include <data/QuantitySelector.h>
36#include <list>
37
38#include <drain/util/BeanLike.h>
39#include <drain/util/Range.h>
40#include <drain/util/ReferenceMap.h>
41//#include <drain/util/Variable.h>
42
43#include "ODIM.h"
44#include "ODIMPathTools.h"
45#include "ODIMPathMatcher.h"
46
47
48namespace rack {
49
50
51struct DataOrder { //: public drain::BeanLike {
52
53 enum Crit {DATA, ELANGLE, TIME}; // ALTITUDE
54 enum Oper {MIN, MAX};
55
56 const char separator = ':';
57
59 CritFlagger criterion;
60
62 OperFlagger operation;
63
64 template<typename ... TT>
65 void set(Crit crit, const TT &... args) {
66 criterion = crit;
67 set(args...);
68 };
69
70 template<typename ... TT>
71 void set(Oper oper, const TT &... args) {
72 operation = oper;
73 set(args...);
74 };
75
77 inline
78 void set(const std::string s) {
79 std::string s1, s2;
80 drain::StringTools::split2(s, s1, s2, separator);
81 criterion.set(s1);
82 operation.set(s2);
83 set();
84 };
85
86 void set(){
87 str = criterion.str() + separator + operation.str();
88 }
89
90 std::string str;
91
92};
93
94
95inline
96std::ostream & operator<<(std::ostream & ostr, const DataOrder & order){
97 ostr << order.str;
98 return ostr;
99}
100
102 // QuantitySelector,
105class DataSelector: public drain::BeanLike {
106public:
107
108 friend class drain::ReferenceMap;
109
111 enum Prf {SINGLE=1, DOUBLE=2, ANY=3};
112
113 // TODO: string => ODIMPath
114 DataSelector(const std::string & path, const std::string & quantity,
115 unsigned int count = 1000, drain::Range<double> elangle = {-90.0, 90.0},
116 DataSelector::Prf prf=Prf::ANY //int dualPRF = 0,
117 //drain::Range<int> timespan={0,0}
118 );
119 // double elangleMin = -90.0, double elangleMax = 90.0);
120
121 DataSelector(const std::string & parameters = "");
122
124 // DataSelector(ODIMPathElem::group_t e, ODIMPathElem::group_t e2=ODIMPathElem::ROOT, ODIMPathElem::group_t e3=ODIMPathElem::ROOT);
125
126 template<typename ... T>
127 DataSelector(const ODIMPathElem & elem, const T &... args): BeanLike(__FUNCTION__){ //, orderFlags(orderDict,':') {
128 init();
129 //pathMatcher.setElems(elem, rest...);
130 pathMatcher.set(elem, args...);
131 updateBean();
132 }
133
134 // Either this or previous is unneeded?
135 template<typename ... T>
136 DataSelector(ODIMPathElem::group_t e, const T &... args): BeanLike(__FUNCTION__){ // , orderFlags(orderDict,':') {
137 init();
138 //pathMatcher.setElems(e, rest...);
139 pathMatcher.set(e, args...);
140 updateBean();
141 }
142
143
144 DataSelector(const DataSelector & selector);
145
146 virtual ~DataSelector();
147
148
150 // * - \c index - integer value, only changing the \c index member. ???
160 // Experimental. Raise to beanlike?
162
167 inline
168 bool consumeParameters(std::string & args){
169 if (args.empty()){
170 return false;
171 }
172 else {
173 setParameters(args);
174 args.clear();
175 return true;
176 }
177 }
178
179 template<typename ... TT>
180 inline
181 void setPathMatcher(TT... args){
182 pathMatcher.set(args...);
183 }
184
185 inline
186 const ODIMPathMatcher & getPathMatcher() const {
187 return pathMatcher;
188 }
189
191 inline
193 return pathMatcher.trimHead(true);
194 }
195
197
210 void setQuantities(const std::string & s);
211
212 // void setQuantityRegExp(const std::string & s); // todo: rename (here only) quantities?
213
214 inline
215 bool quantityIsSet() const {
216 return quantitySelector.isSet(); // || qualitySelector.isSet();
217 }
218
220
224 inline
225 const std::string & getQuantity() const {
226 return quantities;
227 }
228
229 inline
230 const QuantitySelector & getQuantitySelector() const {
231 return quantitySelector;
232 }
233
234
235 inline
236 void setMaxCount(unsigned int i){
237 count = i;
238 }
239
240 inline
241 unsigned int getMaxCount() const {
242 return count;
243 }
244
245
246 inline
247 void setPrf(const std::string & s){
248 this->prf = s;
249 this->prfSelector.set(s);
250 }
251
252 inline
253 void setPrf(Prf prf){
254 this->prfSelector.set(prf);
255 this->prf = this->prfSelector.str();
256 //this->updateBean();
257 }
258
259 template<typename ... TT>
260 inline
261 void setOrder(const TT &... args) {
262 this->order.set(args...);
263 }
264
265 inline
266 const DataOrder & getOrder() const {
267 return order;
268 }
269
270
272
275 // void selectPaths(const Hi5Tree & src, std::list<ODIMPath> & pathContainer) const;
276
277
278
280
283 void reset(); // bool flexible = true "inclusive"
284
285
287 // TODO: perhaps semantics unclear. Consider separating to updateParameters/ deriveImplicitParameters etc.
298
318 void getPaths(const Hi5Tree & src, std::list<ODIMPath> & pathContainer) const;
319
320
322
327 bool getPath(const Hi5Tree & src, ODIMPath & path) const;
328
329
330
333
335 bool getLastPath(const Hi5Tree & src, ODIMPath & path, ODIMPathElem::group_t group = ODIMPathElem::DATA ) const;
336
338 bool getNextPath(const Hi5Tree & src, ODIMPath & path, ODIMPathElem::group_t group = ODIMPathElem::DATA ) const;
339
340
341
342
344
352 static
353 void swapData(Hi5Tree & src,const ODIMPathElem &srcElem, Hi5Tree & dst);
354
356 static
357 void swapData(Hi5Tree & srcGroup, Hi5Tree & dst, ODIMPathElem::group_t groupType);
358
359 // TODO add filter, allowing ODIMPathElem::group_t == QUALITY
360 static
361 void getTimeMap(const Hi5Tree & srcRoot, ODIMPathElemMap & m);
362
364
369
370protected:
371
373
377 virtual
378 void updateBean() const override;
379
380
382
385 //mutable
386 std::string path; // temporary!
387
388 mutable
389 ODIMPathMatcher pathMatcher;
390
392 mutable
393 std::string quantities;
394
395 mutable
396 QuantitySelector quantitySelector;
397
398 //mutable
399 //drain::KeySelector qualitySelector;
400
401
403 unsigned int count;
404
405
408
409 mutable DataOrder order;
410
412 std::string prf;
413
414 mutable
416
419
421
430 void updateQuantities() const; // todo: rename (here only) quantities?
431 // void updateQuantities(const std::string & separators = ",") const ;
432
434 void init();
435
436
438
442 bool collectPaths(const Hi5Tree & src, std::list<ODIMPath> & pathContainer, const ODIMPath & basepath = ODIMPath(), const std::string & parentQuantity="", ODIMPathElem::group_t filter = ODIMPathElem::ALL_GROUPS) const;
443
444
446 void prunePaths(const Hi5Tree & src, std::list<ODIMPath> & pathContainer) const;
447
448};
449
450std::ostream & operator<<(std::ostream & ostr, const DataSelector &selector);
451
452
453
454// Experimental
455class DatasetSelector : public DataSelector {
456
457public:
458
460 drain::Logger mout(__FILE__, __FUNCTION__);
461 parameters.delink("path");
462 mout.experimental<LOG_NOTICE>("not resetting DATASET" );
463 //pathMatcher.setElems(ODIMPathElem::DATASET);
464 }
465
466};
467
468// Experimental
469/*
470class ImageSelector : public DataSelector {
471
472public:
473
474 ImageSelector() : DataSelector(ODIMPathElem::DATA | ODIMPathElem::QUALITY) {
475 parameters.link("path", dummy);
476 //parameters.delink("path");
477 // pathMatcher.setElems(ODIMPathElem::DATASET);
478 }
479
480
481 // Convert path and quantity strings to pathMatcher and quantity regexps, respectively.
482 virtual
483 void updateBean();
484
485private:
486
487 std::string dummy;
488
489};
490*/
491
492} // rack::
493
494
495DRAIN_ENUM_DICT(rack::DataOrder::Crit);
496DRAIN_ENUM_DICT(rack::DataOrder::Oper);
497DRAIN_ENUM_DICT(rack::DataSelector::Prf);
498
499DRAIN_TYPENAME(rack::DataSelector);
500
501namespace drain {
502 // DRAIN_TYPENAME(rack::DataSelector);
503
504
505}
506
507#endif /* DATASELECTOR_H_ */
Something which has a name, a description and possibly some parameters of varying type.
Definition BeanLike.h:58
void setParameters(std::initializer_list< Variable::init_pair_t > args)
Grants access to (if above hidden)
Definition BeanLike.h:142
Flagger accepting values of enum type E.
Definition EnumFlagger.h:56
LogSourc e is the means for a function or any program segment to "connect" to a Log.
Definition Log.h:313
Definition Path.h:135
void trimHead(bool COMPLETE=false)
Removes leading empty elements. The resulting string presentation will not start with the separator.
Definition Path.h:373
Definition Range.h:52
Definition ReferenceMap.h:215
void delink(const std::string &key)
Removes an entry from the map.
Definition ReferenceMap.h:358
virtual const key_t & str() const override
String corresponding the current value. Returns empty, if not found.
Definition Flags.h:151
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:127
static void swapData(Hi5Tree &src, const ODIMPathElem &srcElem, Hi5Tree &dst)
Swap branches such that dst gets a /dataset or /data with a new index.
void setQuantities(const std::string &s)
Sets basic quantities and quality quantities. These sets are separated by '/'.
std::string prf
Reject or accept VRAD(VH)
Definition DataSelector.h:405
std::string path
Regular expression of accepted paths, for example ".*‍/data$". Deprecated.
Definition DataSelector.h:379
static void swapData(Hi5Tree &srcGroup, Hi5Tree &dst, ODIMPathElem::group_t groupType)
Like swapData(Hi5Tree & src,const ODIMPathElem &srcElem, Hi5Tree & dst), but src already at the level...
void updateQuantities() const
Continue path matching started with getMainPaths()
void getPaths(const Hi5Tree &src, std::list< ODIMPath > &pathContainer) const
Sets given parameters and implicitly determines missing parameters.
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.
bool consumeParameters(std::string &args)
Sets parameters in predefined order or sets specified parameters. (Python style calling alternatives....
Definition DataSelector.h:168
void trimPathMatcher()
"Drop leading slashes", ie. remove leading empty elements.
Definition DataSelector.h:192
virtual void updateBean() const override
Traverses the parameters and updates the corresponding member objects.
void ensureDataGroup()
In path, ensure trailing DATA or QUANTITY element.
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 .
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 .
void reset()
Collect paths with all the criteria: path, elevation(range), PRF, quantity...
const std::string & getQuantity() const
Retrieve quantity list and regular expression, if defined.
Definition DataSelector.h:225
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.
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:81
unsigned int group_t
In H5, "groups" correspond to directories or folders in file system.
Definition ODIMPath.h:91
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
Structure for testing if a path matches a given sequence of path elements.
Definition ODIMPathMatcher.h:186
Definition DataSelector.cpp:1277
Definition DataSelector.cpp:44
std::map< std::string, ODIMPathElem > ODIMPathElemMap
Definition ODIMPath.h:464
Definition DataSelector.h:58
void set(const std::string s)
Expects <crit>[:<oper>].
Definition DataSelector.h:78