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 const drain::KeySelector & getQualitySelector() const {
232 return qualitySelector;
233 }
234 */
235
236 inline
237 void setMaxCount(unsigned int i){
238 count = i;
239 }
240
241 inline
242 unsigned int getMaxCount() const {
243 return count;
244 }
245
246
247 inline
248 void setPrf(const std::string & s){
249 this->prf = s;
250 this->prfSelector.set(s);
251 }
252
253 inline
254 void setPrf(Prf prf){
255 this->prfSelector.set(prf);
256 this->prf = this->prfSelector.str();
257 //this->updateBean();
258 }
259
260 template<typename ... TT>
261 inline
262 void setOrder(const TT &... args) {
263 this->order.set(args...);
264 }
265
266 inline
267 const DataOrder & getOrder() const {
268 return order;
269 }
270
271
273
276 // void selectPaths(const Hi5Tree & src, std::list<ODIMPath> & pathContainer) const;
277
278
279
281
284 void reset(); // bool flexible = true "inclusive"
285
286
288 // TODO: perhaps semantics unclear. Consider separating to updateParameters/ deriveImplicitParameters etc.
299
319 void getPaths(const Hi5Tree & src, std::list<ODIMPath> & pathContainer) const;
320
321
323
328 bool getPath(const Hi5Tree & src, ODIMPath & path) const;
329
330
331
334
336 bool getLastPath(const Hi5Tree & src, ODIMPath & path, ODIMPathElem::group_t group = ODIMPathElem::DATA ) const;
337
339 bool getNextPath(const Hi5Tree & src, ODIMPath & path, ODIMPathElem::group_t group = ODIMPathElem::DATA ) const;
340
341
342
343
345
353 static
354 void swapData(Hi5Tree & src,const ODIMPathElem &srcElem, Hi5Tree & dst);
355
357 static
358 void swapData(Hi5Tree & srcGroup, Hi5Tree & dst, ODIMPathElem::group_t groupType);
359
360 // TODO add filter, allowing ODIMPathElem::group_t == QUALITY
361 static
362 void getTimeMap(const Hi5Tree & srcRoot, ODIMPathElemMap & m);
363
365
370
371protected:
372
374
378 virtual
379 void updateBean() const override;
380
381
383
386 //mutable
387 std::string path; // temporary!
388
389 mutable
390 ODIMPathMatcher pathMatcher;
391
393 mutable
394 std::string quantities;
395
396 mutable
397 QuantitySelector quantitySelector;
398
399 //mutable
400 //drain::KeySelector qualitySelector;
401
402
404 unsigned int count;
405
406
409
410 mutable DataOrder order;
411
413 std::string prf;
414
415 mutable
417
420
422
431 void updateQuantities() const; // todo: rename (here only) quantities?
432 // void updateQuantities(const std::string & separators = ",") const ;
433
435 void init();
436
437
439
443 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;
444
445
447 void prunePaths(const Hi5Tree & src, std::list<ODIMPath> & pathContainer) const;
448
449};
450
451std::ostream & operator<<(std::ostream & ostr, const DataSelector &selector);
452
453
454
455// Experimental
456class DatasetSelector : public DataSelector {
457
458public:
459
461 drain::Logger mout(__FILE__, __FUNCTION__);
462 parameters.delink("path");
463 mout.experimental<LOG_NOTICE>("not resetting DATASET" );
464 //pathMatcher.setElems(ODIMPathElem::DATASET);
465 }
466
467};
468
469// Experimental
470/*
471class ImageSelector : public DataSelector {
472
473public:
474
475 ImageSelector() : DataSelector(ODIMPathElem::DATA | ODIMPathElem::QUALITY) {
476 parameters.link("path", dummy);
477 //parameters.delink("path");
478 // pathMatcher.setElems(ODIMPathElem::DATASET);
479 }
480
481
482 // Convert path and quantity strings to pathMatcher and quantity regexps, respectively.
483 virtual
484 void updateBean();
485
486private:
487
488 std::string dummy;
489
490};
491*/
492
493} // rack::
494
495namespace drain {
497}
498
499#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:342
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