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 <set>
37#include <list>
38#include <map>
39#include <stdexcept>
40
41#include <drain/RegExp.h>
42#include <drain/Sprinter.h>
43#include <drain/Type.h>
44#include <drain/util/BeanLike.h>
45#include <drain/util/Range.h>
46#include <drain/util/ReferenceMap.h>
47#include <drain/util/StringMatcherList.h>
48//#include <drain/util/Variable.h>
49
50#include "ODIM.h"
51#include "ODIMPathTools.h"
52#include "ODIMPathMatcher.h"
53#include "PolarODIM.h" // elangle
54
55
56namespace rack {
57
58
59struct DataOrder { //: public drain::BeanLike {
60
61 enum Crit {DATA, ELANGLE, TIME}; // ALTITUDE
62 enum Oper {MIN, MAX};
63
64 const char separator = ':';
65
67 CritFlagger criterion;
68
70 OperFlagger operation;
71
72 template<typename ... TT>
73 void set(Crit crit, const TT &... args) {
74 criterion = crit;
75 set(args...);
76 };
77
78 template<typename ... TT>
79 void set(Oper oper, const TT &... args) {
80 operation = oper;
81 set(args...);
82 };
83
85 inline
86 void set(const std::string s) {
87 std::string s1, s2;
88 drain::StringTools::split2(s, s1, s2, separator);
89 criterion.set(s1);
90 operation.set(s2);
91 set();
92 };
93
94 void set(){
95 str = criterion.str() + separator + operation.str();
96 }
97
98 std::string str;
99
100};
101
102
103inline
104std::ostream & operator<<(std::ostream & ostr, const DataOrder & order){
105 ostr << order.str;
106 return ostr;
107}
108
110 // QuantitySelector,
113class DataSelector: public drain::BeanLike {
114public:
115
116 friend class drain::ReferenceMap;
117
119 enum Prf {SINGLE=1, DOUBLE=2, ANY=3};
120
121 // TODO: string => ODIMPath
122 DataSelector(const std::string & path, const std::string & quantity,
123 unsigned int count = 1000, drain::Range<double> elangle = {-90.0, 90.0},
124 DataSelector::Prf prf=Prf::ANY //int dualPRF = 0,
125 //drain::Range<int> timespan={0,0}
126 );
127 // double elangleMin = -90.0, double elangleMax = 90.0);
128
129 DataSelector(const std::string & parameters = "");
130
132 // DataSelector(ODIMPathElem::group_t e, ODIMPathElem::group_t e2=ODIMPathElem::ROOT, ODIMPathElem::group_t e3=ODIMPathElem::ROOT);
133
134 template<typename ... T>
135 DataSelector(const ODIMPathElem & elem, const T &... args): BeanLike(__FUNCTION__){ //, orderFlags(orderDict,':') {
136 init();
137 //pathMatcher.setElems(elem, rest...);
138 pathMatcher.set(elem, args...);
139 updateBean();
140 }
141
142 // Either this or previous is unneeded?
143 template<typename ... T>
144 DataSelector(ODIMPathElem::group_t e, const T &... args): BeanLike(__FUNCTION__){ // , orderFlags(orderDict,':') {
145 init();
146 //pathMatcher.setElems(e, rest...);
147 pathMatcher.set(e, args...);
148 updateBean();
149 }
150
151
152 DataSelector(const DataSelector & selector);
153
154 virtual ~DataSelector();
155
156
158 // * - \c index - integer value, only changing the \c index member. ???
168 // Experimental. Raise to beanlike?
170
175 inline
176 bool consumeParameters(std::string & args){
177 if (args.empty()){
178 return false;
179 }
180 else {
181 setParameters(args);
182 args.clear();
183 return true;
184 }
185 }
186
187 template<typename ... TT>
188 inline
189 void setPathMatcher(TT... args){
190 pathMatcher.set(args...);
191 }
192
193 inline
194 const ODIMPathMatcher & getPathMatcher() const {
195 return pathMatcher;
196 }
197
199 inline
201 return pathMatcher.trimHead(true);
202 }
203
205 void setQuantities(const std::string & s);
206
207 // void setQuantityRegExp(const std::string & s); // todo: rename (here only) quantities?
208
209 inline
210 bool quantityIsSet() const {
211 return quantitySelector.isSet(); // || qualitySelector.isSet();
212 }
213
215
219 inline
220 const std::string & getQuantity() const {
221 return quantities;
222 }
223
224 inline
225 const QuantitySelector & getQuantitySelector() const {
226 return quantitySelector;
227 }
228
229
230 inline
231 void setMaxCount(unsigned int i){
232 count = i;
233 }
234
235 inline
236 unsigned int getMaxCount() const {
237 return count;
238 }
239
240
241 inline
242 void setPrf(const std::string & s){
243 this->prf = s;
244 this->prfSelector.set(s);
245 }
246
247 inline
248 void setPrf(Prf prf){
249 this->prfSelector.set(prf);
250 this->prf = this->prfSelector.str();
251 //this->updateBean();
252 }
253
254 template<typename ... TT>
255 inline
256 void setOrder(const TT &... args) {
257 this->order.set(args...);
258 }
259
260 inline
261 const DataOrder & getOrder() const {
262 return order;
263 }
264
265
267
270 // void selectPaths(const Hi5Tree & src, std::list<ODIMPath> & pathContainer) const;
271
272
273
275
278 void reset(); // bool flexible = true "inclusive"
279
280
282 // TODO: perhaps semantics unclear. Consider separating to updateParameters/ deriveImplicitParameters etc.
293
313 void getPaths(const Hi5Tree & src, std::list<ODIMPath> & pathContainer) const;
314
315
317
322 bool getPath(const Hi5Tree & src, ODIMPath & path) const;
323
324
325
328
330 bool getLastPath(const Hi5Tree & src, ODIMPath & path, ODIMPathElem::group_t group = ODIMPathElem::DATA ) const;
331
333 bool getNextPath(const Hi5Tree & src, ODIMPath & path, ODIMPathElem::group_t group = ODIMPathElem::DATA ) const;
334
335
336
337
339
347 static
348 void swapData(Hi5Tree & src,const ODIMPathElem &srcElem, Hi5Tree & dst);
349
351 static
352 void swapData(Hi5Tree & srcGroup, Hi5Tree & dst, ODIMPathElem::group_t groupType);
353
354 // TODO add filter, allowing ODIMPathElem::group_t == QUALITY
355 static
356 void getTimeMap(const Hi5Tree & srcRoot, ODIMPathElemMap & m);
357
359
364
365protected:
366
368
372 virtual
373 void updateBean() const override;
374
375
377
380 //mutable
381 std::string path; // temporary!
382
383 mutable
384 ODIMPathMatcher pathMatcher;
385
387 mutable
388 std::string quantities;
389
390 mutable
391 QuantitySelector quantitySelector;
392
393 //mutable
394 //drain::KeySelector qualitySelector;
395
396
398 unsigned int count;
399
400
403
404 mutable DataOrder order;
405
407 std::string prf;
408
409 mutable
411
414
416
425 void updateQuantities() const; // todo: rename (here only) quantities?
426 // void updateQuantities(const std::string & separators = ",") const ;
427
429 void init();
430
431
433
437 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;
438
439
441 void prunePaths(const Hi5Tree & src, std::list<ODIMPath> & pathContainer) const;
442
443};
444
445std::ostream & operator<<(std::ostream & ostr, const DataSelector &selector);
446
447
448
449// Experimental
450class DatasetSelector : public DataSelector {
451
452public:
453
455 drain::Logger mout(__FILE__, __FUNCTION__);
456 parameters.delink("path");
457 mout.experimental<LOG_NOTICE>("not resetting DATASET" );
458 //pathMatcher.setElems(ODIMPathElem::DATASET);
459 }
460
461};
462
463// Experimental
464/*
465class ImageSelector : public DataSelector {
466
467public:
468
469 ImageSelector() : DataSelector(ODIMPathElem::DATA | ODIMPathElem::QUALITY) {
470 parameters.link("path", dummy);
471 //parameters.delink("path");
472 // pathMatcher.setElems(ODIMPathElem::DATASET);
473 }
474
475
476 // Convert path and quantity strings to pathMatcher and quantity regexps, respectively.
477 virtual
478 void updateBean();
479
480private:
481
482 std::string dummy;
483
484};
485*/
486
487} // rack::
488
489namespace drain {
491}
492
493#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:144
Flagger accepting values of enum type E.
Definition EnumFlags.h:190
LogSourc e is the means for a function or any program segment to "connect" to a Log.
Definition Log.h:312
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:329
Definition Range.h:52
Definition ReferenceMap.h:207
void delink(const std::string &key)
Removes an entry from the map.
Definition ReferenceMap.h:350
virtual const key_t & str() const override
String corresponding the current value. Returns empty, if not found.
Definition Flags.h:159
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:135
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:176
void trimPathMatcher()
"Drop leading slashes", ie. remove leading empty elements.
Definition DataSelector.h:200
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:220
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:82
unsigned int group_t
In H5, "groups" correspond to directories or folders in file system.
Definition ODIMPath.h:92
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
Structure for testing if a path matches a given sequence of path elements.
Definition ODIMPathMatcher.h:186
Definition DataSelector.cpp:1277
DRAIN_TYPENAME(void)
Add a specialization for each type of those you want to support.
Definition DataSelector.cpp:44
std::map< std::string, ODIMPathElem > ODIMPathElemMap
Definition ODIMPath.h:465
Definition DataSelector.h:58
void set(const std::string s)
Expects <crit>[:<oper>].
Definition DataSelector.h:86