ODIM.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 #ifndef ODIM_STRUCT
32 #define ODIM_STRUCT
33 
34 #include <ostream>
35 #include <cmath>
36 #include <string>
37 #include <set>
38 #include <algorithm>
39 //
40 #include <drain/util/VariableFormatter.h> // for VariableHandler
41 #include <drain/util/ReferenceMap.h>
42 #include <drain/util/Rectangle.h>
43 #include <drain/util/Time.h>
44 #include <drain/image/Geometry.h>
45 
46 #include "hi5/Hi5.h"
47 #include "radar/Constants.h"
48 
49 #include "EncodingODIM.h"
50 
51 namespace rack {
52 
53 
79 class ODIM : public EncodingODIM { //, public {
80 
81 public:
82 
83  typedef enum {
84  KILOMETRES = 1,
85  RACK_EXTENSIONS = 8, // like where:BBOX
86  ODIM_2_2 = 16 | KILOMETRES,
87  ODIM_2_3 = 32 | KILOMETRES,
88  // ODIM_2_3_6 = 64
89  ODIM_2_4 = 64
91 
92  //typedef drain::EnumFlagger<drain::SingleFlagger<Version> > VersionFlagger;
93  typedef drain::EnumFlagger<drain::MultiFlagger<Version> > VersionFlagger; // Multi, for KM's
94 
95  static VersionFlagger versionFlagger;
96 
97  typedef std::set<std::string> nameSet;
98 
99  static
100  const nameSet timeKeys; // = {"time", "starttime", "endtime"};
101 
102  static
103  const nameSet dateKeys; // = {"date", "startdate", "enddate"};
104 
105  static
106  const nameSet locationKeys; // = {"site", "src", "lat", "lon", "PLC", "NOD", "WMO"};
107 
108 
109 
110  inline
111  ODIM(group_t initialize = ODIMPathElem::ALL_LEVELS) : EncodingODIM(initialize){
112  init(initialize);
113  };
114 
115  inline
116  ODIM(const ODIM & odim) : NI(odim.NI){
117  initFromMap(odim);
118  };
119 
120  inline
121  ODIM(const drain::image::Image & image, const std::string & quantity="") : EncodingODIM(image) {
122  initFromImage(image, quantity);
123  };
124 
125  inline
126  ODIM(drain::image::Image & image, const std::string & quantity="") : EncodingODIM(image) {
127  initFromImage(image, quantity);
128  };
129 
130  /*
131  inline
132  ODIM(const drain::image::Image & image, const std::string & quantity="") : EncodingODIM(image) {
133  initFromImage(image, quantity);
134  };
135  */
136 
137  /*
138  inline
139  ODIM(drain::image::Image & image, const std::string & quantity="") : geometryREF(image.geometry) {
140  initFromImage(image, quantity);
141  };
142  */
143 
144  inline
145  ~ODIM(){};
146 
147  //const drain::image::AreaGeometry geometry;
148 
150 
152 
157 
158  //const drain::image::AreaGeometry & geometryTEST;
159 
161  static
162  const std::string dateformat;
163 
165  static
166  const std::string timeformat;
167 
168 
171  std::string object;
172  std::string version;
173 
175  std::string date;
177  std::string time;
178  std::string source;
179 
181  std::string quantity; // raise?
182  std::string product; // raise?
183  std::string prodpar; // raise?
184  std::string startdate;
185  std::string starttime;
186  std::string enddate;
187  std::string endtime;
188 
189  // Elevation angles.
190  // In ODIM, defined for Cartesian but used more generally - for polar products - in Rack
191  std::vector<double> angles;
192 
193 
194  // Number of images used in precipitation accumulation (lenient, not linked)
195  long ACCnum = 0;
196 
198  virtual inline
199  void setGeometry(size_t cols, size_t rows){
200  area.set(cols, rows);
201  };
202 
203 
204  virtual
205  void setGeometry(const drain::image::AreaGeometry & g){
206  setGeometry(g.getWidth(), g.getHeight());
207  };
208 
209  virtual // ?
210  const drain::image::AreaGeometry & getGeometry() const {
211  return area;
212  };
213 
215  virtual
216  void adjustGeometry(size_t cols, size_t rows);
217 
218 
219 
221 
224  virtual
225  void updateLenient(const ODIM & odim);
226 
228  inline
229  bool getTime(drain::Time & t) const {
230  return getTime(t, date, time);
231  }
232 
234  inline
235  bool getStartTime(drain::Time & t) const {
236  return getTime(t, startdate, starttime);
237  }
238 
240  inline
241  bool getEndTime(drain::Time & t) const {
242  return getTime(t, enddate, endtime);
243  }
244 
246  bool setTime(const drain::Time & t);
247 
249  bool setTime(const std::string & s);
250 
251 
253  virtual inline
255  //using namespace drain::image;
256  static const drain::image::CoordinatePolicy policy(drain::image::EdgePolicy::LIMIT);
257  return policy;
258  //return rack::limit;
259  }
260 
262 
267  bool distinguishNodata(const std::string & quantityPrefix = "");
268 
269 
271 
284  template <group_t G, class T>
285  static inline
286  void updateH5AttributeGroups(const T &odim, Hi5Tree & dst) {
287  static const T odimLimited(G);
288  static const std::list<std::string> & keys = odimLimited.getKeyList();
289  //odim.copyTo(odimLimited.getKeyList(), dst);
290  odim.copyTo(keys, dst);
291  }
292 
293  template <group_t G, class T>
294  static inline
295  void updateH5AttributeGroups(const T &odim, const Hi5Tree & dst) {
296  //static T odimLimited(G);
297  //odim.copyTo(odimLimited.getKeyList(), dst);
298  }
299 
300 
301 
302 
303  // Maximum Nyquist velocity, mutable because may be updated with PolarODIM::getNyquist()
304  mutable
305  double NI;
306 
307 
308 
309 //protected:
310 
311  static
313  bool getTime(drain::Time & t, const std::string &dateStr, const std::string &timeStr); // const;
314 
315 protected:
316 
318  void copyTo(const std::list<std::string> & keys, Hi5Tree & dst) const;
319 
320 
321  template <class T>
322  inline
323  void initFromMap(const std::map<std::string,T> & m){
325  updateFromMap(m);
326  }
327 
328  virtual inline
329  void initFromImage(const drain::image::Image & img){ // =""
331  this->quantity = img.getProperties().get("what:quantity", "");
332  copyFrom(img);
333  }
334 
335  // deprec
336  virtual inline
337  void initFromImage(const drain::image::Image & img, const std::string & quantity){ // =""
338  drain::Logger mout(__FILE__, __FUNCTION__);
340  this->quantity = quantity;
341  copyFrom(img);
342  //mout.warn("guantity finally=(" , this->quantity , '<' , quantity , ")" );
343  //mout.warn(*this );
344  }
345 
346 private:
347 
348 
349  virtual // must
350  void init(group_t initialize =ODIMPathElem::ALL_LEVELS);
351 
352 
353 
354 };
355 
356 
357 inline
358 std::ostream & operator<<(std::ostream &ostr, const ODIM & odim){
359  odim.drain::SmartMap<drain::Reference>::toStream(ostr);
360  return ostr;
361 }
362 
363 
364 
365 
366 } // namespace rack
367 
368 
369 #endif
Flagger accepting values of enum type E.
Definition: Flags.h:763
LogSourc e is the means for a function or any program segment to "connect" to a Log.
Definition: Log.h:308
void updateFromMap(const std::map< std::string, T2 > &m)
Assign values from a map. Updates existing entries only.
Definition: SmartMap.h:294
std::string get(const std::string &key, const std::string &defaultValue) const
Retrieves a value, or default value if value is unset.
Definition: SmartMap.h:127
Utility for handling time. Internally, uses tm (C time structure).
Definition: Time.h:54
Policies for coordinate underflows and overflows.
Definition: CoordinatePolicy.h:106
Class for multi-channel digital images. Supports dynamic typing with base types (char,...
Definition: Image.h:184
Structure for data storage type, scaling and marker codes. Does not contain quantity.
Definition: EncodingODIM.h:75
EncodingODIM(group_t initialize=ODIMPathElem::ALL_LEVELS)
Default constructor.
Definition: EncodingODIM.h:99
void copyFrom(const drain::image::Image &data)
Copies image attributes and type . Experimental.
Definition: EncodingODIM.cpp:250
static const group_t ALL_LEVELS
Abbreviation for linking (referencing) attributes at different levels (tree depths).
Definition: ODIMPath.h:118
ODIM metadata (quantity, gain, offset, undetect, nodata, date, time)
Definition: ODIM.h:79
virtual void updateLenient(const ODIM &odim)
Updates object, quantity, product and time information.
Definition: ODIM.cpp:289
Version
Definition: ODIM.h:83
@ ODIM_2_3
Definition: ODIM.h:87
@ ODIM_2_4
Definition: ODIM.h:89
drain::Point2D< double > resolution
Spatial resolution in metres.
Definition: ODIM.h:156
virtual void adjustGeometry(size_t cols, size_t rows)
Change geometry and adjust spatial resolution respectively.
Definition: ODIM.cpp:279
virtual const drain::image::CoordinatePolicy & getCoordinatePolicy() const
Returns recommended coordinate policy. Redefined in PolarODIM.
Definition: ODIM.h:254
bool getTime(drain::Time &t) const
Retrieves the stored time. Returns true if successful. // consider: throws error if fail.
Definition: ODIM.h:229
virtual void setGeometry(size_t cols, size_t rows)
Sets number of columns (nbins) and number of rows (nrays). Does not change resolution.
Definition: ODIM.h:199
bool getStartTime(drain::Time &t) const
Retrieves the start time. Returns true if successful. // consider: throws error if fail.
Definition: ODIM.h:235
std::string object
Definition: ODIM.h:171
std::string date
Nominal time, in dateformat.
Definition: ODIM.h:175
static void updateH5AttributeGroups(const T &odim, Hi5Tree &dst)
Write ODIM metadata to WHAT, WHERE and HOW groups.
Definition: ODIM.h:286
bool distinguishNodata(const std::string &quantityPrefix="")
If nodata==undetect, set nodata=maxValue (hoping its not nodata...)
Definition: ODIM.cpp:97
static const std::string dateformat
Applied 8-digit date format, "%Y%m%d".
Definition: ODIM.h:162
std::string time
Nominal time, in timeformat.
Definition: ODIM.h:177
bool getEndTime(drain::Time &t) const
Retrieves the end time. Returns true if successful. // consider: throws error if fail.
Definition: ODIM.h:241
static const std::string timeformat
Applied 6-digit date format, "%H%M%S".
Definition: ODIM.h:166
bool setTime(const drain::Time &t)
Sets date and time . Returns true if successful, throws error if fail.
Definition: ODIM.cpp:231
std::string quantity
dataX/what (obligatory)
Definition: ODIM.h:181
Definition: DataSelector.cpp:44