Loading...
Searching...
No Matches
EncodingODIM.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#ifndef ENC_ODIM_STRUCT
32#define ENC_ODIM_STRUCT
33
34#include <ostream>
35#include <cmath>
36#include <string>
37#include <set>
38#include <algorithm>
39//#include <drain/util/Options.h>
40#include <drain/util/ReferenceMap.h>
41#include <drain/util/Rectangle.h>
42#include <drain/util/Time.h>
43#include <drain/util/ValueScaling.h>
44
45#include "hi5/Hi5.h"
46#include "radar/Constants.h"
47#include "ODIMPath.h"
48
49namespace rack {
50
52
75class EncodingODIM : public drain::ReferenceMap { // public ODIMPathElem,
76
77public:
78
80 typedef ODIMPathElem::group_t group_t;
81
82 drain::ValueScaling & scaling;
83 const drain::ValueScaling & scalingConst;
84
85 drain::ValueScaling ownScaling;
86
87 typedef enum {NONE=0, SCALING=1, RANGE=2} ExplicitSetting;
88
89 static
90 const drain::FlagResolver::dict_t settingDict;
91
92 //typedef drain::EnumFlagger<drain::SingleFlagger<TiffCompliance> > tiffComplianceFlagger;
93 //typedef drain::EnumFlagger<drain::MultiFlagger<Adaption> > AdaptionFlagger;
94
95 int explicitSettings = NONE;
96
98 inline
99 EncodingODIM(group_t initialize = ODIMPathElem::ALL_LEVELS) : scaling(ownScaling), scalingConst(ownScaling), explicitSettings(NONE){
100 init(initialize);
101 };
102
104 EncodingODIM(const EncodingODIM & odim);
105
107
110 EncodingODIM(char type, double scale=1.0, double offset=NAN, double nodata = NAN, double undetect = NAN, const drain::Range<double> & range = {0,0});
111
113
116 EncodingODIM(char type, const drain::Range<double> & range, double scale=0.0, double offset=NAN, double nodata = NAN, double undetect = NAN);
117
118
119 inline
120 EncodingODIM(const drain::image::Image & image) : scaling(ownScaling), scalingConst(image.getScaling()), explicitSettings(NONE) {
121 initFromImage(image);
122 };
123
124 inline
125 EncodingODIM(drain::image::Image & image) : scaling(image.getScaling()), scalingConst(image.getScaling()), explicitSettings(NONE) {
126 initFromImage(image);
127 };
128
129 inline
130 void copyEncoding(const EncodingODIM & odim){
131 // updateFromMap(odim); // risk: copies attribs also beyon ODIM?
132 type = odim.type;
133 scaling.set(odim.scaling);
134 nodata = odim.nodata;
135 undetect = odim.undetect;
136 explicitSettings = odim.explicitSettings;
137 }
138
139 inline
140 EncodingODIM & operator=(const EncodingODIM & odim) {
141 // std::cerr << "EncodingODIM & operator=" << std::endl;
142 // updateFromMap(odim);
143 // explicitSettings = odim.explicitSettings;
144 copyEncoding(odim);
145 return *this;
146 }
147
148 // In case of const image this is not "true" ? But for const EncodingODIM, never called?
149 operator drain::ValueScaling & (){
150 return scaling;
151 }
152
153 operator const drain::ValueScaling & () const {
154 return scalingConst;
155 }
156
157
159
162 std::string type;
163
164 double nodata;
165 double undetect;
166
167 EncodingODIM & setScaling(double gain, double offset = NAN);
168
169 EncodingODIM & setScaling(double gain, double offset, double undetect, double nodata);
170
171 inline
172 bool isSet() const {
173 return ((scaling.scale != 0.0) && (!type.empty()) && (type.at(0) != '*'));
174 }
175
177 // todo: bool operator==(const EncodingODIM & odim);
178 static
179 inline
180 bool haveSimilarEncoding(const EncodingODIM & odim1, const EncodingODIM & odim2){
181 return (odim1.type == odim2.type) &&
182 (odim1.scaling.scale == odim2.scaling.scale) &&
183 (odim1.scaling.offset == odim2.scaling.offset) &&
184 (odim1.undetect == odim2.undetect) &&
185 (odim1.nodata == odim2.nodata)
186 ;
187 };
188
189 void setRange(double min, double max);
190
192 //template <class T>
193 inline
194 void setType(const std::type_info & type){
195 this->type = drain::Type::getTypeChar(type);
196 scaling.set(1.0, 0.0);
197 undetect = drain::Type::call<drain::typeMin, double>(type);
198 nodata = drain::Type::call<drain::typeMax, double>(type);
199 }
200
202 template <class T>
203 inline
204 void setTypeDefaults(const T & type, const std::string & values = ""){
205
206 drain::Logger mout(__FILE__, __FUNCTION__);
207
208 drain::Type t(type);
209 const char typechar = drain::Type::getTypeChar(t);
210 if (this->type.empty())
211 this->type = typechar;
212 else {
213 if (this->type.at(0) != typechar)
214 mout.warn("different types: " , this->type , '/' , typechar );
215 }
216
217 scaling.set(1.0, 0.0);
218
219 //if (!type.empty()){ // ?
220 if (typechar != '*'){
221 undetect = drain::Type::call<drain::typeMin, double>(t); //drain::Type::getMin<double>(t);
222 nodata = drain::Type::call<drain::typeMax, double>(t); // drain::Type::call<drain::typeMax,double>(t);
223 }
224 else {
225 undetect = drain::Type::call<drain::typeMin, double>(this->type); //drain::Type::getMin<double>(this->type);
226 nodata = drain::Type::call<drain::typeMax, double>(this->type); //drain::Type::call<drain::typeMax,double>(this->type);
227 }
228
229 setValues(values);
230
231 }
232
233 inline
234 void setTypeDefaults(){
235 setTypeDefaults(this->type);
236 }
237
239 inline
240 bool isValue(double x) const {
241 return (x != undetect) && (x != nodata);
242 }
243
245 inline
246 double scaleForward(double x) const {
247 return scaling.offset + scaling.scale*x; // TODO: direct
248 }
249
251 inline
252 double scaleInverse(double y) const {
253 return (y-scaling.offset)/scaling.scale; // TODO: direct
254 }
255
257 double getMin() const;
258
260 double getMax() const;
261
263 inline
264 double operator()(double y) const {
265 return (y-scaling.offset)/scaling.scale;
266 }
267
268
270 // ?virtual?
272 void clear();
273
274
276 /*
277 inline
278 void getShortKeys(std::map<std::string,std::string> & m){
279 for (ReferenceMap::iterator it = begin(); it != end(); ++it){
280 const std::string & longKey = it->first;
281 const std::string shortKey = longKey.substr(0,longKey.find(':'));
282 m[shortKey] = longKey;
283 std::cerr << shortKey << '#' << longKey << '\n';
284 }
285 }
286 */
287
288
290 virtual
291 void updateLenient(const EncodingODIM & odim);
292
293
295 /*
296 * Does not change the values of the map.
297 *
298 * The object itself can be given as an argument, \see addShortKeys().
299 */
301
303 /*
304 * Does not change the values of the map.
305 */
306 inline
308 grantShortKeys(*this);
309 }
310
312
315 void copyFrom(const drain::image::Image & data);
316
318
321 static
322 inline
323 void checkType(Hi5Tree & dst){
324 EncodingODIM odim;
325 checkType(dst, odim);
326 } // Temp for thread-safety.
327
328
330
341 static
342 const ODIMPathElemSeq & attributeGroups;
343
344 //bool RANGE_DRIVEN;
345
346protected:
347
348
349
350 static
351 void checkType(Hi5Tree & dst, EncodingODIM & odim); // SEE ABOVE?
352
353
354 static
355 const ODIMPathElemSeq & getAttributeGroups();
356
357
358private:
359
360 virtual // must
361 void init(group_t initialize = ODIMPathElem::ALL_LEVELS);
362
363 void initFromImage(const drain::image::Image & img);
364
365};
366
367
368
369} // namespace rack
370
371
372#endif
373
374// Rack
LogSourc e is the means for a function or any program segment to "connect" to a Log.
Definition Log.h:313
Logger & warn(const TT &... args)
Possible error, but execution can continue.
Definition Log.h:431
Definition Range.h:52
Definition ReferenceMap.h:207
void setValues(const std::string &entries, char assignmentSymbol='=', char separatorSymbol=0)
Sets values. If strictness==STRICTLY_CLOSED, throws exception if tries to assign a non-existing entry...
Definition SmartMap.h:311
Utilities related to std::type_info.
Definition Type.h:50
Linear scaling and physical range for image intensities.
Definition ValueScaling.h:64
double & scale
Multiplicative coefficient \i a in: y = ax + b.
Definition ValueScaling.h:68
double & offset
Additive coefficient \i b in: y = ax + b.
Definition ValueScaling.h:71
Class for multi-channel digital images. Supports dynamic typing with base types (char,...
Definition Image.h:193
Structure for data storage type, scaling and marker codes. Does not contain quantity.
Definition EncodingODIM.h:75
double operator()(double y) const
Functor (why inverse?)
Definition EncodingODIM.h:264
EncodingODIM(group_t initialize=ODIMPathElem::ALL_LEVELS)
Default constructor.
Definition EncodingODIM.h:99
static bool haveSimilarEncoding(const EncodingODIM &odim1, const EncodingODIM &odim2)
Checks if data encoding is similar (storage type, gain, offset, undetect and nodata are the same).
Definition EncodingODIM.h:180
double scaleInverse(double y) const
Converts a quantity to storage scale: x = (y-offset)/gain .
Definition EncodingODIM.h:252
void addShortKeys()
Creates a short alias (attrib) for each (group):(attrib). Example: "gain" => "what:gain".
Definition EncodingODIM.h:307
void setType(const std::type_info &type)
Complete with setTypeDefaults()
Definition EncodingODIM.h:194
static const ODIMPathElemSeq & attributeGroups
Copies contents of this to a h5 group.
Definition EncodingODIM.h:342
std::string type
This is non-standard (not in ODIM), but a practical means of handling storage type of datasets.
Definition EncodingODIM.h:162
double scaleForward(double x) const
Converts a quantity from storage scale: y = offset + gain*y .
Definition EncodingODIM.h:246
static void checkType(Hi5Tree &dst)
Traverses recursively subtrees and checks the types of PolarODIM variables.
Definition EncodingODIM.h:323
virtual void updateLenient(const EncodingODIM &odim)
Todo: keep the function, but move implementation to (future single-exec) register ?
Definition EncodingODIM.cpp:229
double getMin() const
Returns the minimum physical value that can be returned using current storage type,...
Definition EncodingODIM.cpp:394
void copyFrom(const drain::image::Image &data)
Copies image attributes and type . Experimental.
Definition EncodingODIM.cpp:269
bool isValue(double x) const
Returns true for a valid measurement value, false for undetect and nodata marker values.
Definition EncodingODIM.h:240
void clear()
Resets the values.
Definition EncodingODIM.cpp:183
void setTypeDefaults(const T &type, const std::string &values="")
Sets gain=1, offset=0, undetect=type_min, nodata=type_max. Note: sets type only if unset.
Definition EncodingODIM.h:204
double getMax() const
Returns the minimum physical value that can be returned using current storage type,...
Definition EncodingODIM.cpp:422
void grantShortKeys(drain::ReferenceMap &ref)
Creates a short alias (attrib) for each (group):(attrib). Example: "gain" => "what:gain".
Definition EncodingODIM.cpp:252
static const group_t ALL_LEVELS
Abbreviation for linking (referencing) attributes at different levels (tree depths).
Definition ODIMPath.h:121
unsigned int group_t
In H5, "groups" correspond to directories or folders in file system.
Definition ODIMPath.h:92
Definition DataSelector.cpp:44