Loading...
Searching...
No Matches
DataTools.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_DATATOOLS_H
33#define RACK_DATATOOLS_H
34
35#include <map>
36
37
38#include <drain/image/CoordinatePolicy.h>
39#include <drain/util/ReferenceMap.h>
40
41
42#include "ODIM.h"
43#include "PolarODIM.h" // elangle
44
45// #include "Data.h"
46
47
48namespace rack {
49
50
53
54public:
55
56 inline
58 }
59
60
61 bool attributeColors = true;
62 bool skipExcluded = true;
63
64};
65
67
71class DataTools { //: public drain::BeanLike {
72
73public:
74
75 static
76 TreeFileInfo & getTreeFileInfo();
77
79
83 // static
84 // const drain::Variable & getAttribute(const Hi5Tree &src, const std::string & path, const std::string & group, const std::string & attributeName);
85
87
91 // template <class M>
92 // static
93 // void getAttributesOLD(const Hi5Tree &src, const std::string & path, M & attributes, bool updateOnly = false);
94
95 // consider: path is not a reference, it will be copied.
96 template <class M>
97 static
98 void getAttributes(const Hi5Tree &src, const Hi5Tree::path_t & path, M & attributes, bool updateOnly = false);
99
100 static
101 void getQuantityMap(const Hi5Tree & srcDataset, ODIMPathElemMap & m);
102
104
114 static
115 void updateInternalAttributes(Hi5Tree & src);
116
118
121 static inline
122 void updateInternalAttributes(const Hi5Tree &){ // const drain::FlexVariableMap & attributes = drain::FlexVariableMap()){
123 // drain::Logger mout("DataTools", __FUNCTION__);
124 // mout.warn("somebody called me");
125 };
126
127
128 static
129 void updateCoordinatePolicy(Hi5Tree & src, const drain::image::CoordinatePolicy & policy = drain::image::CoordinatePolicy(drain::image::EdgePolicy::LIMIT));
130 //void updateCoordinatePolicy(Hi5Tree & src, const CoordinatePolicy & policy = CoordinatePolicy(EdgePolicy::LIMIT));
131
136 static
137 int getMaxRange(const Hi5Tree & src, bool projected=true);
138
139 // static
140 // bool dataToStream(const Hi5Tree::node_data_t & data, std::ostream &ostr);
141
142 typedef std::map<std::string, ODIMPathElem> quantity_map;
143
145 /*
146 *
147 */
148 static
149 void superDump(const Hi5Tree & src, std::ostream & ostr = std::cerr);
150
152 static
153 void adjustWhatObject(Hi5Tree & src);
154
155
157 /*
158 static
159 void markExcluded(Hi5Tree &src, const Hi5Tree::path_t & path, bool exclude=true);
160 */
161
163
174 static
175 bool treeToStream(const Hi5Tree::node_data_t & data, std::ostream &ostr);
176
177
178 //static
179 // std::list<std::string> & getMainAttributes();
180
182
185 static
187 // std::list<std::string>
188 // std::map<std::string,std::string>
189
190 // static
191 // void createSimpleQualityData(const PlainData<DT> & data, drain::image::Image & qualityImage, double dataQuality=1.0, double undetectQuality=0.5, double nodataQuality=0.0);
192
193 /* cyclic
194 template <class DT>
195 static inline
196 void createSimpleQualityData(const PlainData<DT> & data, PlainData<DT> & qualityData, double dataQuality=1.0, double undetectQuality=0.5, double nodataQuality=0.0);//, double dataQuality=1.0, double nodataQuality=0.0); // const {
197 //qualityData.odim.scaling.scale = qualityData.data.getScaling().scale;
198 //qualityData.odim.scaling.offset = qualityData.data.getScaling().offset;
199 // }
200 *
201 */
202
203protected:
204
205 // static
206 // drain::VariableMap attributeStyles;
207
209
216
227
237 //const drain::image::CoordinatePolicy & policy = drain::image::CoordinatePolicy(),
238 static
239 void updateInternalAttributes(Hi5Tree & src, const drain::FlexVariableMap & attributes);
240 //void updateInternalAttributes(Hi5Tree & src, const drain::VariableMap & attributes);
241
242
243
244
245};
246
247/* cyclic include with Data.h
248template <class DT>
249void DataTools::createSimpleQualityData(const PlainData<DT> & data, PlainData<DT> & qualityData, double dataQuality, double undetectQuality, double nodataQuality){
250
251 qualityData.setEncoding(typeid(unsigned char));
252 // createSimpleQualityData(data, qualityData.data, dataQuality, undetectQuality, nodataQuality);
253 qualityData.odim.scaling.set(qualityData.data.getScaling());
254
255 qualityyData.data.setPhysicalRange(0.0, 1.0, true);
256
257 const drain::ValueScaling & scaling = qualityData.data.getScaling();
258
259 const bool DATA = !std::isnan(dataQuality);
260 const bool UNDETECT = !std::isnan(undetectQuality);
261 const bool NODATA = !std::isnan(nodataQuality);
262
263 // Default ie. unset values are non_signaling_NAN's, but maybe more elegant to skip calculations:
264 const double dataCode = DATA ? scaling.inv(dataQuality) : 0.0;
265 const double undetectCode = UNDETECT ? scaling.inv(undetectQuality) : 0.0;
266 const double nodataCode = NODATA ? scaling.inv(nodataQuality) : 0.0;
267
268 qualityData.data.setGeometry(data.getWidth(), data.getHeight());
269
270 drain::image::Image::iterator it = data.data.begin();
271 drain::image::Image::iterator wit = qualityData.data.begin();
272 while (it != data.end()){
273 //if ((*it != odim.nodata) && (*it != odim.undetect))
274 if (UNDETECT && (*it == data.odim.undetect))
275 *wit = undetectCode;
276 else if (NODATA && (*it == data.odim.nodata))
277 *wit = nodataCode;
278 else if (DATA)
279 *wit = dataCode;
280 ++it;
281 ++wit;
282 }
283}
284*/
285
286template <class M>
287void DataTools::getAttributes(const Hi5Tree &src, const Hi5Tree::path_t & p, M & attributes, bool updateOnly){
288
290
291 /*
292 if (p.hasRoot()){
293 mout.debug("add root and restart with path= '" , p , "' (revised code)" );
294 Hi5Tree::path_t pRooted(p);
295 pRooted.ensureRoot();
296 DataTools::getAttributes(src, pRooted, attributes, updateOnly);
297 return;
298 }
299 */
300
301 if (p.empty() || !p.front().isRoot()){
302 mout.debug("add root and restart with path= '" , p , "' (revised code)" );
303 //Hi5Tree::path_t pRooted(Hi5Tree::path_t(Hi5Tree::path_t::elem_t::ROOT), p);
304 Hi5Tree::path_t pRooted;
305 pRooted.appendElem(Hi5Tree::path_t::elem_t::ROOT);
306 pRooted.append(p);
307 // pRooted.push_front(Hi5Tree::path_t::elem_t::ROOT);
309 return;
310 }
311
312
313 mout.debug("path= '" , p , "'" );
314
315 Hi5Tree::path_t path;
316 std::stringstream sstr;
317
318 //for (Hi5Tree::path_t::const_iterator pit = p.begin(); pit != p.end(); ++pit){
319 for (Hi5Tree::path_t::elem_t elem: p){
320
321 path.appendElem(elem); // *pit;
322
323 mout.debug("check='" , path , "'" );
324
325 const Hi5Tree & s = src(path);
326
327 //for (ODIMPathElemSeq::const_iterator git = EncodingODIM::attributeGroups.begin(); git != EncodingODIM::attributeGroups.end(); ++git){
329
330 const hi5::NodeHi5 & group = s[elem].data;
331
332 //for(drain::VariableMap::const_iterator ait = group.attributes.begin(); ait != group.attributes.end(); ait++){
333 for(const auto & entry: group.attributes){
334
335 sstr.str("");
336 sstr << elem << ':' << entry.first; // key
337 //mout.debug(8) << "getAttributes: " << sstr.toStr() << '=' << it->second << mout.endl;
338
339 //drain::SmartMapTools::setValue(attributes, sstr.str(), entry.second);
340 drain::SmartMapTools::setValue(attributes, sstr.str(), entry.second, !updateOnly);
341 /*
342 if (updateOnly){
343 //drain::SmartMapTools::setValue<false>(attributes, sstr.str(), entry.second);
344
345 typename M::iterator it = attributes.find(sstr.str());
346 if (it != attributes.end())
347 it->second = entry.second;
348 }
349 else {
350 // drain::SmartMapTools::setValue<true>(attributes, sstr.str(), entry.second);
351 attributes[sstr.str()] = entry.second; // value
352 }
353 */
354 }
355 }
356
357 // ++pit;
358 // if (pit == p.end())
359 // return;
360 // path << *pit;
361
362 }
363
364}
365
366
367
368/*
369template <class M>
370void DataTools::getAttributesOLD(const Hi5Tree &src, const std::string & path, M & attributes, bool updateOnly){
371
372 drain::Logger mout("DataTools", __FUNCTION__);
373
374 //drain::VariableMap::const_iterator it;
375
376 int iStart = 0;
377 if (!path.empty())
378 if (path[0] == '/')
379 iStart = 1;
380
381 mout.debug(5) << "'" << path << "'" << mout.endl;
382
383 std::stringstream sstr; // for speed
384 std::string s;
385 size_t i = 0;
386 while(true){
387
388 const std::string subpath = path.substr(iStart, i); // with i=npos
389 mout.debug(5) << "'" << subpath << "'\t" << i << mout.endl;
390 //attributes[std::string("@")+subpath] = 0;
391
392 const Hi5Tree & s = src(subpath);
393
395 for (std::set<ODIMPathElem>::const_iterator git = EncodingODIM::attributeGroups.begin(); git != EncodingODIM::attributeGroups.end(); ++git){
396 const hi5::NodeHi5 & group = s[*git].data;
397 for(drain::VariableMap::const_iterator ait = group.attributes.begin(); ait != group.attributes.end(); ait++){
398 sstr.str("");
399 sstr << *git << ':' << ait->first;
400 //mout.debug(8) << "getAttributes: " << sstr.toStr() << '=' << it->second << mout.endl;
401 if (!updateOnly)
402 attributes[sstr.str()] = ait->second;
403 else {
404 typename M::iterator it = attributes.find(sstr.str());
405 if (it != attributes.end())
406 it->second = ait->second;
407 }
408 }
409 }
410
411
412 if (i == std::string::npos){
413 //if (drain::Debug > 4){ std::cerr << "attributes " << attributes << '\n'; }
414 return;
415 }
416
417 i = path.find('/', i+1);
418 }
419 //while (i < path.size()); //(i != std::string::npos);
420}
421*/
422
423
424} // rack::
425
426#endif /* DATASELECTOR_H_ */
427
428// Rack
Definition FileInfo.h:47
A map of FlexVariable:s.
Definition VariableMap.h:144
LogSourc e is the means for a function or any program segment to "connect" to a Log.
Definition Log.h:326
static void setValue(M &dst, const std::string &key, const V &value)
Set value of an element. If not STRICT, set only if key exists ie update.
Definition MapTools.h:168
A map of Variables.
Definition VariableMap.h:61
Policies for coordinate underflows and overflows.
Definition CoordinatePolicy.h:100
A map of radar data, indexed by quantity code (DBZH, VRAD, etc).
Definition Data.h:1293
Tool for selecting datasets based on paths, quantities and min/max elevations.
Definition DataTools.h:71
static void superDump(const Hi5Tree &src, std::ostream &ostr=std::cerr)
Tree output designed above all for debugging.
Definition DataTools.cpp:174
static void adjustWhatObject(Hi5Tree &src)
If polar data contains single or several elevations, mark SCAN or PVOL, respectively.
Definition DataTools.cpp:77
static drain::VariableMap & getAttributeStyles()
List of most important ODIM attributes (with style suggestion).
Definition DataTools.cpp:134
static void updateInternalAttributes(Hi5Tree &src)
Copies values of what , where and how attributes to internal attributes down to data[n] groups.
Definition DataTools.cpp:293
static void updateInternalAttributes(const Hi5Tree &)
This const version does nothing, but is needed for Data:: #supdateTree3()
Definition DataTools.h:122
static bool treeToStream(const Hi5Tree::node_data_t &data, std::ostream &ostr)
(Un)mark groups along a path for deletion by Hi5Base::deleteExcluded()
Definition DataTools.cpp:222
static void getAttributes(const Hi5Tree &src, const Hi5Tree::path_t &path, M &attributes, bool updateOnly=false)
Traverses upward in hierachy to find the value of the given PolarODIM where, what,...
Definition DataTools.h:287
static int getMaxRange(const Hi5Tree &src, bool projected=true)
Definition DataTools.cpp:439
static const ODIMPathElemSeq & attributeGroups
Copies contents of this to a h5 group.
Definition EncodingODIM.h:337
DOC: Add class description.
Definition ODIMPath.h:82
DOC: Add class description.
Definition DataTools.h:52
Definition AltitudeOp.cpp:42
std::map< std::string, ODIMPathElem > ODIMPathElemMap
Definition ODIMPath.h:474
QuantityMap & getQuantityMap()
Definition QuantityMap.cpp:282
Rack's hi5 structure that uses Rack classes (Tree, Data, Image).
Definition Hi5.h:71