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 <set>
36#include <list>
37#include <map>
38
39
40#include <drain/RegExp.h>
41#include <drain/image/CoordinatePolicy.h>
42#include <drain/util/BeanLike.h>
43#include <drain/util/ReferenceMap.h>
44
45
46#include "ODIM.h"
47#include "PolarODIM.h" // elangle
48
49
50
51namespace rack {
52
54
58class DataTools { //: public drain::BeanLike {
59
60public:
61
62
64
70 // static
71 // const drain::Variable & getAttribute(const Hi5Tree &src, const std::string & path, const std::string & group, const std::string & attributeName);
72
74
82 // consider: path is not a reference, it will be copied.
83 template <class M>
84 static
85 void getAttributes(const Hi5Tree &src, const Hi5Tree::path_t & path, M & attributes, bool updateOnly = false);
86
87 static
88 void getQuantityMap(const Hi5Tree & srcDataset, ODIMPathElemMap & m);
89
91
101 static
102 void updateInternalAttributes(Hi5Tree & src);
103
105
108 static inline
109 void updateInternalAttributes(const Hi5Tree & src){ // const drain::FlexVariableMap & attributes = drain::FlexVariableMap()){
110 // drain::Logger mout("DataTools", __FUNCTION__);
111 // mout.warn("somebody called me");
112 };
113
114
115 static
116 void updateCoordinatePolicy(Hi5Tree & src, const drain::image::CoordinatePolicy & policy = drain::image::CoordinatePolicy(drain::image::EdgePolicy::LIMIT));
117 //void updateCoordinatePolicy(Hi5Tree & src, const CoordinatePolicy & policy = CoordinatePolicy(EdgePolicy::LIMIT));
118
123 static
124 int getMaxRange(const Hi5Tree & src, bool projected=true);
125
126 // static
127 // bool dataToStream(const Hi5Tree::node_data_t & data, std::ostream &ostr);
128
129 typedef std::map<std::string, ODIMPathElem> quantity_map;
130
131
133
138 static
139 void markExcluded(Hi5Tree &src, bool exclude=true);
140
142 static
143 void markExcluded(Hi5Tree &src, const Hi5Tree::path_t & path, bool exclude=true);
144
146
157 static
158 bool treeToStream(const Hi5Tree::node_data_t & data, std::ostream &ostr);
159
160
161 //static
162 // std::list<std::string> & getMainAttributes();
163
165
168 static
170 // std::list<std::string>
171 // std::map<std::string,std::string>
172
173protected:
174
175 // static
176 // drain::VariableMap attributeStyles;
177
179
186
189 static
190 bool removeIfExcluded(const Hi5Tree & src){
191 return false;
192 };
193
194
196
206 //const drain::image::CoordinatePolicy & policy = drain::image::CoordinatePolicy(),
207 static
208 void updateInternalAttributes(Hi5Tree & src, const drain::FlexVariableMap & attributes);
209 //void updateInternalAttributes(Hi5Tree & src, const drain::VariableMap & attributes);
210
211
212
213
214};
215
216
217template <class M>
218void DataTools::getAttributes(const Hi5Tree &src, const Hi5Tree::path_t & p, M & attributes, bool updateOnly){
219
220 drain::Logger mout(__FILE__, __FUNCTION__);
221
222 /*
223 if (p.hasRoot()){
224 mout.debug("add root and restart with path= '" , p , "' (revised code)" );
225 Hi5Tree::path_t pRooted(p);
226 pRooted.ensureRoot();
227 DataTools::getAttributes(src, pRooted, attributes, updateOnly);
228 return;
229 }
230 */
231
232 if (p.empty() || !p.front().isRoot()){
233 mout.debug("add root and restart with path= '" , p , "' (revised code)" );
234 //Hi5Tree::path_t pRooted(Hi5Tree::path_t(Hi5Tree::path_t::elem_t::ROOT), p);
235 Hi5Tree::path_t pRooted;
236 pRooted.appendElem(Hi5Tree::path_t::elem_t::ROOT);
237 pRooted.append(p);
238 // pRooted.push_front(Hi5Tree::path_t::elem_t::ROOT);
239 DataTools::getAttributes(src, pRooted, attributes, updateOnly);
240 return;
241 }
242
243
244 mout.debug("path= '" , p , "'" );
245
246 Hi5Tree::path_t path;
247 std::stringstream sstr;
248
249 //for (Hi5Tree::path_t::const_iterator pit = p.begin(); pit != p.end(); ++pit){
250 for (Hi5Tree::path_t::elem_t elem: p){
251
252 path.appendElem(elem); // *pit;
253
254 mout.debug("check='" , path , "'" );
255
256 const Hi5Tree & s = src(path);
257
258 //for (ODIMPathElemSeq::const_iterator git = EncodingODIM::attributeGroups.begin(); git != EncodingODIM::attributeGroups.end(); ++git){
260
261 const hi5::NodeHi5 & group = s[elem].data;
262
263 //for(drain::VariableMap::const_iterator ait = group.attributes.begin(); ait != group.attributes.end(); ait++){
264 for(const auto & entry: group.attributes){
265
266 sstr.str("");
267 sstr << elem << ':' << entry.first; // key
268 //mout.debug(8) << "getAttributes: " << sstr.toStr() << '=' << it->second << mout.endl;
269
270 //drain::SmartMapTools::setValue(attributes, sstr.str(), entry.second);
271 drain::SmartMapTools::setValue(attributes, sstr.str(), entry.second, !updateOnly);
272 /*
273 if (updateOnly){
274 //drain::SmartMapTools::setValue<false>(attributes, sstr.str(), entry.second);
275
276 typename M::iterator it = attributes.find(sstr.str());
277 if (it != attributes.end())
278 it->second = entry.second;
279 }
280 else {
281 // drain::SmartMapTools::setValue<true>(attributes, sstr.str(), entry.second);
282 attributes[sstr.str()] = entry.second; // value
283 }
284 */
285 }
286 }
287
288 // ++pit;
289 // if (pit == p.end())
290 // return;
291 // path << *pit;
292
293 }
294
295}
296
297
298
299/*
300template <class M>
301void DataTools::getAttributesOLD(const Hi5Tree &src, const std::string & path, M & attributes, bool updateOnly){
302
303 drain::Logger mout("DataTools", __FUNCTION__);
304
305 //drain::VariableMap::const_iterator it;
306
307 int iStart = 0;
308 if (!path.empty())
309 if (path[0] == '/')
310 iStart = 1;
311
312 mout.debug(5) << "'" << path << "'" << mout.endl;
313
314 std::stringstream sstr; // for speed
315 std::string s;
316 size_t i = 0;
317 while(true){
318
319 const std::string subpath = path.substr(iStart, i); // with i=npos
320 mout.debug(5) << "'" << subpath << "'\t" << i << mout.endl;
321 //attributes[std::string("@")+subpath] = 0;
322
323 const Hi5Tree & s = src(subpath);
324
326 for (std::set<ODIMPathElem>::const_iterator git = EncodingODIM::attributeGroups.begin(); git != EncodingODIM::attributeGroups.end(); ++git){
327 const hi5::NodeHi5 & group = s[*git].data;
328 for(drain::VariableMap::const_iterator ait = group.attributes.begin(); ait != group.attributes.end(); ait++){
329 sstr.str("");
330 sstr << *git << ':' << ait->first;
331 //mout.debug(8) << "getAttributes: " << sstr.toStr() << '=' << it->second << mout.endl;
332 if (!updateOnly)
333 attributes[sstr.str()] = ait->second;
334 else {
335 typename M::iterator it = attributes.find(sstr.str());
336 if (it != attributes.end())
337 it->second = ait->second;
338 }
339 }
340 }
341
342
343 if (i == std::string::npos){
344 //if (drain::Debug > 4){ std::cerr << "attributes " << attributes << '\n'; }
345 return;
346 }
347
348 i = path.find('/', i+1);
349 }
350 //while (i < path.size()); //(i != std::string::npos);
351}
352*/
353
354
355} // rack::
356
357#endif /* DATASELECTOR_H_ */
358
359// 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:89
A map of Variables.
Definition VariableMap.h:61
Policies for coordinate underflows and overflows.
Definition CoordinatePolicy.h:106
Tool for selecting datasets based on paths, quantities and min/max elevations.
Definition DataTools.h:58
static bool removeIfExcluded(const Hi5Tree &src)
Removes the children of the tree if Node::exclude is set.
Definition DataTools.h:190
static void updateInternalAttributes(const Hi5Tree &src)
This const version does nothing, but is needed for Data:: #supdateTree3()
Definition DataTools.h:109
static void markExcluded(Hi5Tree &src, bool exclude=true)
Mark/unmark whole tree to be deleted with Hi5Base::deleteExcluded()
Definition DataTools.cpp:356
static drain::VariableMap & getAttributeStyles()
List of most important ODIM attributes (with style suggestion).
Definition DataTools.cpp:88
static void updateInternalAttributes(Hi5Tree &src)
Copies values of what , where and how attributes to internal attributes down to data[n] groups.
Definition DataTools.cpp:186
static bool treeToStream(const Hi5Tree::node_data_t &data, std::ostream &ostr)
Tree attribute formatter.
Definition DataTools.cpp:128
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:218
static int getMaxRange(const Hi5Tree &src, bool projected=true)
Definition DataTools.cpp:326
static const ODIMPathElemSeq & attributeGroups
Copies contents of this to a h5 group.
Definition EncodingODIM.h:342
Definition ODIMPath.h:82
Definition DataSelector.cpp:44
std::map< std::string, ODIMPathElem > ODIMPathElemMap
Definition ODIMPath.h:465
QuantityMap & getQuantityMap()
Definition QuantityMap.cpp:279
Rack's hi5 structure that uses Rack classes (Tree, Data, Image).
Definition Hi5.h:74