DataTools.h
1 /*
2 
3 MIT License
4 
5 Copyright (c) 2017 FMI Open Development / Markus Peura, first.last@fmi.fi
6 
7 Permission is hereby granted, free of charge, to any person obtaining a copy
8 of this software and associated documentation files (the "Software"), to deal
9 in the Software without restriction, including without limitation the rights
10 to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
11 copies of the Software, and to permit persons to whom the Software is
12 furnished to do so, subject to the following conditions:
13 
14 The above copyright notice and this permission notice shall be included in all
15 copies or substantial portions of the Software.
16 
17 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
18 IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
19 FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
20 AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
21 LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
22 OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
23 SOFTWARE.
24 
25 */
26 /*
27 Part of Rack development has been done in the BALTRAD projects part-financed
28 by the European Union (European Regional Development Fund and European
29 Neighbourhood Partnership Instrument, Baltic Sea Region Programme 2007-2013)
30 */
31 
32 #ifndef DATATOOLS_H_
33 #define DATATOOLS_H_
34 
35 #include <drain/image/CoordinatePolicy.h>
36 #include <set>
37 #include <list>
38 #include <map>
39 
40 
41 #include <drain/RegExp.h>
42 
43 #include <drain/util/BeanLike.h>
44 
45 #include <drain/util/ReferenceMap.h>
46 
47 //#include <drain/util/Variable.h>
48 
49 #include "ODIM.h"
50 #include "PolarODIM.h" // elangle
51 
52 //#include "DataSelector.h" // Range
53 
54 
55 namespace rack {
56 
58 
62 class DataTools { //: public drain::BeanLike {
63 
64 public:
65 
66 
68 
74  // static
75  // const drain::Variable & getAttribute(const Hi5Tree &src, const std::string & path, const std::string & group, const std::string & attributeName);
76 
78 
86  // consider: path is not a reference, it will be copied.
87  template <class M>
88  static
89  void getAttributes(const Hi5Tree &src, const Hi5Tree::path_t & path, M & attributes, bool updateOnly = false);
90 
91  static
92  void getQuantityMap(const Hi5Tree & srcDataset, ODIMPathElemMap & m);
93 
95 
105  static
106  void updateInternalAttributes(Hi5Tree & src);
107  /*
108  {
109  src.data.image.properties.clear();
110  src.data.image.setCoordinatePolicy(4,3,2,1);
111  updateInternalAttributes(src, drain::FlexVariableMap());
112  //updateInternalAttributes(src, drain::VariableMap());
113  }
114  */
115 
117 
120  static inline
121  void updateInternalAttributes(const Hi5Tree & src){ // const drain::FlexVariableMap & attributes = drain::FlexVariableMap()){
122  // drain::Logger mout("DataTools", __FUNCTION__);
123  // mout.warn("somebody called me");
124  };
125 
126 
127  static
128  void updateCoordinatePolicy(Hi5Tree & src, const drain::image::CoordinatePolicy & policy = drain::image::CoordinatePolicy(drain::image::EdgePolicy::LIMIT));
129  //void updateCoordinatePolicy(Hi5Tree & src, const CoordinatePolicy & policy = CoordinatePolicy(EdgePolicy::LIMIT));
130 
131  // static
132  // bool dataToStream(const Hi5Tree::node_data_t & data, std::ostream &ostr);
133 
134  typedef std::map<std::string, ODIMPathElem> quantity_map;
135 
136 
138 
143  static
144  void markExcluded(Hi5Tree &src, bool exclude=true);
145 
147  static
148  void markExcluded(Hi5Tree &src, const Hi5Tree::path_t & path, bool exclude=true);
149 
151 
162  static
163  bool treeToStream(const Hi5Tree::node_data_t & data, std::ostream &ostr);
164 
165 
166  static
167  std::list<std::string> & getMainAttributes();
168 
170 
173  static
175  // std::list<std::string>
176  // std::map<std::string,std::string>
177 
178 protected:
179 
180  // static
181  // drain::VariableMap attributeStyles;
182 
184 
191 
194  static
195  bool removeIfExcluded(const Hi5Tree & src){
196  return false;
197  };
198 
199 
201 
211  //const drain::image::CoordinatePolicy & policy = drain::image::CoordinatePolicy(),
212  static
213  void updateInternalAttributes(Hi5Tree & src, const drain::FlexVariableMap & attributes);
214  //void updateInternalAttributes(Hi5Tree & src, const drain::VariableMap & attributes);
215 
216 
217 
218 
219 };
220 
221 
222 template <class M>
223 void DataTools::getAttributes(const Hi5Tree &src, const Hi5Tree::path_t & p, M & attributes, bool updateOnly){
224 
225  drain::Logger mout(__FILE__, __FUNCTION__);
226 
227  /*
228  if (p.hasRoot()){
229  mout.debug("add root and restart with path= '" , p , "' (revised code)" );
230  Hi5Tree::path_t pRooted(p);
231  pRooted.ensureRoot();
232  DataTools::getAttributes(src, pRooted, attributes, updateOnly);
233  return;
234  }
235  */
236 
237  if (p.empty() || !p.front().isRoot()){
238  mout.debug("add root and restart with path= '" , p , "' (revised code)" );
239  //Hi5Tree::path_t pRooted(Hi5Tree::path_t(Hi5Tree::path_t::elem_t::ROOT), p);
240  Hi5Tree::path_t pRooted;
241  pRooted.appendElem(Hi5Tree::path_t::elem_t::ROOT);
242  pRooted.append(p);
243  // pRooted.push_front(Hi5Tree::path_t::elem_t::ROOT);
244  DataTools::getAttributes(src, pRooted, attributes, updateOnly);
245  return;
246  }
247 
248 
249  mout.debug("path= '" , p , "'" );
250 
251  Hi5Tree::path_t path;
252  std::stringstream sstr;
253 
254  //for (Hi5Tree::path_t::const_iterator pit = p.begin(); pit != p.end(); ++pit){
255  for (Hi5Tree::path_t::elem_t elem: p){
256 
257  path.appendElem(elem); // *pit;
258 
259  mout.debug("check='" , path , "'" );
260 
261  const Hi5Tree & s = src(path);
262 
263  //for (ODIMPathElemSeq::const_iterator git = EncodingODIM::attributeGroups.begin(); git != EncodingODIM::attributeGroups.end(); ++git){
264  for (const ODIMPathElem & elem: EncodingODIM::attributeGroups){
265 
266  const hi5::NodeHi5 & group = s[elem].data;
267 
268  //for(drain::VariableMap::const_iterator ait = group.attributes.begin(); ait != group.attributes.end(); ait++){
269  for(const auto & entry: group.attributes){
270 
271  sstr.str("");
272  sstr << elem << ':' << entry.first; // key
273  //mout.debug(8) << "getAttributes: " << sstr.toStr() << '=' << it->second << mout.endl;
274 
275  //drain::SmartMapTools::setValue(attributes, sstr.str(), entry.second);
276  drain::SmartMapTools::setValue(attributes, sstr.str(), entry.second, !updateOnly);
277  /*
278  if (updateOnly){
279  //drain::SmartMapTools::setValue<false>(attributes, sstr.str(), entry.second);
280 
281  typename M::iterator it = attributes.find(sstr.str());
282  if (it != attributes.end())
283  it->second = entry.second;
284  }
285  else {
286  // drain::SmartMapTools::setValue<true>(attributes, sstr.str(), entry.second);
287  attributes[sstr.str()] = entry.second; // value
288  }
289  */
290  }
291  }
292 
293  // ++pit;
294  // if (pit == p.end())
295  // return;
296  // path << *pit;
297 
298  }
299 
300 }
301 
302 
303 
304 /*
305 template <class M>
306 void DataTools::getAttributesOLD(const Hi5Tree &src, const std::string & path, M & attributes, bool updateOnly){
307 
308  drain::Logger mout("DataTools", __FUNCTION__);
309 
310  //drain::VariableMap::const_iterator it;
311 
312  int iStart = 0;
313  if (!path.empty())
314  if (path[0] == '/')
315  iStart = 1;
316 
317  mout.debug(5) << "'" << path << "'" << mout.endl;
318 
319  std::stringstream sstr; // for speed
320  std::string s;
321  size_t i = 0;
322  while(true){
323 
324  const std::string subpath = path.substr(iStart, i); // with i=npos
325  mout.debug(5) << "'" << subpath << "'\t" << i << mout.endl;
326  //attributes[std::string("@")+subpath] = 0;
327 
328  const Hi5Tree & s = src(subpath);
329 
331  for (std::set<ODIMPathElem>::const_iterator git = EncodingODIM::attributeGroups.begin(); git != EncodingODIM::attributeGroups.end(); ++git){
332  const hi5::NodeHi5 & group = s[*git].data;
333  for(drain::VariableMap::const_iterator ait = group.attributes.begin(); ait != group.attributes.end(); ait++){
334  sstr.str("");
335  sstr << *git << ':' << ait->first;
336  //mout.debug(8) << "getAttributes: " << sstr.toStr() << '=' << it->second << mout.endl;
337  if (!updateOnly)
338  attributes[sstr.str()] = ait->second;
339  else {
340  typename M::iterator it = attributes.find(sstr.str());
341  if (it != attributes.end())
342  it->second = ait->second;
343  }
344  }
345  }
346 
347 
348  if (i == std::string::npos){
349  //if (drain::Debug > 4){ std::cerr << "attributes " << attributes << '\n'; }
350  return;
351  }
352 
353  i = path.find('/', i+1);
354  }
355  //while (i < path.size()); //(i != std::string::npos);
356 }
357 */
358 
359 
360 } // rack::
361 
362 #endif /* DATASELECTOR_H_ */
363 
364 // 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:308
Logger & debug(const TT &... args)
Public, yet typically used "internally", when TIMING=true.
Definition: Log.h:676
static void setValue(M &dst, const std::string &key, const V &value)
Utility for "guessing" key order. Derived classes will redefine this e.g. for ordered maps.
Definition: SmartMapTools.h:133
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:62
static bool removeIfExcluded(const Hi5Tree &src)
Removes the children of the tree if Node::exclude is set.
Definition: DataTools.h:195
static void updateInternalAttributes(const Hi5Tree &src)
This const version does nothing, but is needed for Data:: #supdateTree3()
Definition: DataTools.h:121
static void markExcluded(Hi5Tree &src, bool exclude=true)
Mark/unmark whole tree to be deleted with #Hi5Base::deleteExcluded()
Definition: DataTools.cpp:320
static drain::VariableMap & getAttributeStyles()
List of most important ODIM attributes (with style suggestion).
Definition: DataTools.cpp:84
static void updateInternalAttributes(Hi5Tree &src)
Copies values of what , where and how attributes to internal attributes down to data[n] groups.
Definition: DataTools.cpp:179
static bool treeToStream(const Hi5Tree::node_data_t &data, std::ostream &ostr)
Tree attribute formatter.
Definition: DataTools.cpp:121
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:223
static const ODIMPathElemSeq & attributeGroups
Copies contents of this to a h5 group.
Definition: EncodingODIM.h:321
Definition: ODIMPath.h:82
Definition: DataSelector.cpp:44
std::map< std::string, ODIMPathElem > ODIMPathElemMap
Definition: ODIMPath.h:462
Rack's hi5 structure that uses Rack classes (Tree, Data, Image).
Definition: Hi5.h:74