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
46
47namespace rack {
48
50
54class DataTools { //: public drain::BeanLike {
55
56public:
57
58
60
66 // static
67 // const drain::Variable & getAttribute(const Hi5Tree &src, const std::string & path, const std::string & group, const std::string & attributeName);
68
70
78 // consider: path is not a reference, it will be copied.
79 template <class M>
80 static
81 void getAttributes(const Hi5Tree &src, const Hi5Tree::path_t & path, M & attributes, bool updateOnly = false);
82
83 static
84 void getQuantityMap(const Hi5Tree & srcDataset, ODIMPathElemMap & m);
85
87
97 static
98 void updateInternalAttributes(Hi5Tree & src);
99
101
104 static inline
105 void updateInternalAttributes(const Hi5Tree & src){ // const drain::FlexVariableMap & attributes = drain::FlexVariableMap()){
106 // drain::Logger mout("DataTools", __FUNCTION__);
107 // mout.warn("somebody called me");
108 };
109
110
111 static
112 void updateCoordinatePolicy(Hi5Tree & src, const drain::image::CoordinatePolicy & policy = drain::image::CoordinatePolicy(drain::image::EdgePolicy::LIMIT));
113 //void updateCoordinatePolicy(Hi5Tree & src, const CoordinatePolicy & policy = CoordinatePolicy(EdgePolicy::LIMIT));
114
119 static
120 int getMaxRange(const Hi5Tree & src, bool projected=true);
121
122 // static
123 // bool dataToStream(const Hi5Tree::node_data_t & data, std::ostream &ostr);
124
125 typedef std::map<std::string, ODIMPathElem> quantity_map;
126
127
129
134 static
135 void markExcluded(Hi5Tree &src, bool exclude=true);
136
138 static
139 void markExcluded(Hi5Tree &src, const Hi5Tree::path_t & path, bool exclude=true);
140
142
153 static
154 bool treeToStream(const Hi5Tree::node_data_t & data, std::ostream &ostr);
155
156
157 //static
158 // std::list<std::string> & getMainAttributes();
159
161
164 static
166 // std::list<std::string>
167 // std::map<std::string,std::string>
168
169protected:
170
171 // static
172 // drain::VariableMap attributeStyles;
173
175
182
185 static
186 bool removeIfExcluded(const Hi5Tree & src){
187 return false;
188 };
189
190
192
202 //const drain::image::CoordinatePolicy & policy = drain::image::CoordinatePolicy(),
203 static
204 void updateInternalAttributes(Hi5Tree & src, const drain::FlexVariableMap & attributes);
205 //void updateInternalAttributes(Hi5Tree & src, const drain::VariableMap & attributes);
206
207
208
209
210};
211
212
213template <class M>
214void DataTools::getAttributes(const Hi5Tree &src, const Hi5Tree::path_t & p, M & attributes, bool updateOnly){
215
216 drain::Logger mout(__FILE__, __FUNCTION__);
217
218 /*
219 if (p.hasRoot()){
220 mout.debug("add root and restart with path= '" , p , "' (revised code)" );
221 Hi5Tree::path_t pRooted(p);
222 pRooted.ensureRoot();
223 DataTools::getAttributes(src, pRooted, attributes, updateOnly);
224 return;
225 }
226 */
227
228 if (p.empty() || !p.front().isRoot()){
229 mout.debug("add root and restart with path= '" , p , "' (revised code)" );
230 //Hi5Tree::path_t pRooted(Hi5Tree::path_t(Hi5Tree::path_t::elem_t::ROOT), p);
231 Hi5Tree::path_t pRooted;
232 pRooted.appendElem(Hi5Tree::path_t::elem_t::ROOT);
233 pRooted.append(p);
234 // pRooted.push_front(Hi5Tree::path_t::elem_t::ROOT);
235 DataTools::getAttributes(src, pRooted, attributes, updateOnly);
236 return;
237 }
238
239
240 mout.debug("path= '" , p , "'" );
241
242 Hi5Tree::path_t path;
243 std::stringstream sstr;
244
245 //for (Hi5Tree::path_t::const_iterator pit = p.begin(); pit != p.end(); ++pit){
246 for (Hi5Tree::path_t::elem_t elem: p){
247
248 path.appendElem(elem); // *pit;
249
250 mout.debug("check='" , path , "'" );
251
252 const Hi5Tree & s = src(path);
253
254 //for (ODIMPathElemSeq::const_iterator git = EncodingODIM::attributeGroups.begin(); git != EncodingODIM::attributeGroups.end(); ++git){
256
257 const hi5::NodeHi5 & group = s[elem].data;
258
259 //for(drain::VariableMap::const_iterator ait = group.attributes.begin(); ait != group.attributes.end(); ait++){
260 for(const auto & entry: group.attributes){
261
262 sstr.str("");
263 sstr << elem << ':' << entry.first; // key
264 //mout.debug(8) << "getAttributes: " << sstr.toStr() << '=' << it->second << mout.endl;
265
266 //drain::SmartMapTools::setValue(attributes, sstr.str(), entry.second);
267 drain::SmartMapTools::setValue(attributes, sstr.str(), entry.second, !updateOnly);
268 /*
269 if (updateOnly){
270 //drain::SmartMapTools::setValue<false>(attributes, sstr.str(), entry.second);
271
272 typename M::iterator it = attributes.find(sstr.str());
273 if (it != attributes.end())
274 it->second = entry.second;
275 }
276 else {
277 // drain::SmartMapTools::setValue<true>(attributes, sstr.str(), entry.second);
278 attributes[sstr.str()] = entry.second; // value
279 }
280 */
281 }
282 }
283
284 // ++pit;
285 // if (pit == p.end())
286 // return;
287 // path << *pit;
288
289 }
290
291}
292
293
294
295/*
296template <class M>
297void DataTools::getAttributesOLD(const Hi5Tree &src, const std::string & path, M & attributes, bool updateOnly){
298
299 drain::Logger mout("DataTools", __FUNCTION__);
300
301 //drain::VariableMap::const_iterator it;
302
303 int iStart = 0;
304 if (!path.empty())
305 if (path[0] == '/')
306 iStart = 1;
307
308 mout.debug(5) << "'" << path << "'" << mout.endl;
309
310 std::stringstream sstr; // for speed
311 std::string s;
312 size_t i = 0;
313 while(true){
314
315 const std::string subpath = path.substr(iStart, i); // with i=npos
316 mout.debug(5) << "'" << subpath << "'\t" << i << mout.endl;
317 //attributes[std::string("@")+subpath] = 0;
318
319 const Hi5Tree & s = src(subpath);
320
322 for (std::set<ODIMPathElem>::const_iterator git = EncodingODIM::attributeGroups.begin(); git != EncodingODIM::attributeGroups.end(); ++git){
323 const hi5::NodeHi5 & group = s[*git].data;
324 for(drain::VariableMap::const_iterator ait = group.attributes.begin(); ait != group.attributes.end(); ait++){
325 sstr.str("");
326 sstr << *git << ':' << ait->first;
327 //mout.debug(8) << "getAttributes: " << sstr.toStr() << '=' << it->second << mout.endl;
328 if (!updateOnly)
329 attributes[sstr.str()] = ait->second;
330 else {
331 typename M::iterator it = attributes.find(sstr.str());
332 if (it != attributes.end())
333 it->second = ait->second;
334 }
335 }
336 }
337
338
339 if (i == std::string::npos){
340 //if (drain::Debug > 4){ std::cerr << "attributes " << attributes << '\n'; }
341 return;
342 }
343
344 i = path.find('/', i+1);
345 }
346 //while (i < path.size()); //(i != std::string::npos);
347}
348*/
349
350
351} // rack::
352
353#endif /* DATASELECTOR_H_ */
354
355// Rack
A map of FlexVariable:s.
Definition VariableMap.h:138
LogSourc e is the means for a function or any program segment to "connect" to a Log.
Definition Log.h:313
Logger & debug(const TT &... args)
Debug information.
Definition Log.h:667
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
Tool for selecting datasets based on paths, quantities and min/max elevations.
Definition DataTools.h:54
static bool removeIfExcluded(const Hi5Tree &src)
Removes the children of the tree if Node::exclude is set.
Definition DataTools.h:186
static void updateInternalAttributes(const Hi5Tree &src)
This const version does nothing, but is needed for Data:: #supdateTree3()
Definition DataTools.h:105
static void markExcluded(Hi5Tree &src, bool exclude=true)
Mark/unmark whole tree to be deleted with Hi5Base::deleteExcluded()
Definition DataTools.cpp:353
static drain::VariableMap & getAttributeStyles()
List of most important ODIM attributes (with style suggestion).
Definition DataTools.cpp:85
static void updateInternalAttributes(Hi5Tree &src)
Copies values of what , where and how attributes to internal attributes down to data[n] groups.
Definition DataTools.cpp:183
static bool treeToStream(const Hi5Tree::node_data_t &data, std::ostream &ostr)
Tree attribute formatter.
Definition DataTools.cpp:125
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:214
static int getMaxRange(const Hi5Tree &src, bool projected=true)
Definition DataTools.cpp:323
static const ODIMPathElemSeq & attributeGroups
Copies contents of this to a h5 group.
Definition EncodingODIM.h:337
Definition ODIMPath.h:81
Definition DataSelector.cpp:44
std::map< std::string, ODIMPathElem > ODIMPathElemMap
Definition ODIMPath.h:464
QuantityMap & getQuantityMap()
Definition QuantityMap.cpp:279
Rack's hi5 structure that uses Rack classes (Tree, Data, Image).
Definition Hi5.h:71