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;
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
130 inline
131 EncodingODIM & operator=(const EncodingODIM & odim) {
132 // std::cerr << "EncodingODIM & operator=" << std::endl;
133 updateFromMap(odim);
134 explicitSettings = odim.explicitSettings;
135 return *this;
136 }
137
138 // In case of const image this is not "true" ? But for const EncodingODIM, never called?
139 operator drain::ValueScaling & (){
140 return scaling;
141 }
142
143 operator const drain::ValueScaling & () const {
144 return scalingConst;
145 }
146
147
149
152 std::string type;
153
155 //double scale;
156 //double offset;
157 double nodata;
158 double undetect;
159
160 EncodingODIM & setScaling(double gain, double offset = NAN);
161
162 EncodingODIM & setScaling(double gain, double offset, double undetect, double nodata);
163
164 inline
165 bool isSet() const {
166 return ((scaling.scale != 0.0) && (!type.empty()) && (type.at(0) != '*'));
167 }
168
170 // todo: bool operator==(const EncodingODIM & odim);
171 static
172 inline
173 bool haveSimilarEncoding(const EncodingODIM & odim1, const EncodingODIM & odim2){
174 return (odim1.type == odim2.type) &&
175 (odim1.scaling.scale == odim2.scaling.scale) &&
176 (odim1.scaling.offset == odim2.scaling.offset) &&
177 (odim1.undetect == odim2.undetect) &&
178 (odim1.nodata == odim2.nodata)
179 ;
180 };
181
182 void setRange(double min, double max);
183
185 //template <class T>
186 inline
187 void setType(const std::type_info & type){
188 this->type = drain::Type::getTypeChar(type);
189 scaling.set(1.0, 0.0);
190 undetect = drain::Type::call<drain::typeMin, double>(type);
191 nodata = drain::Type::call<drain::typeMax, double>(type);
192 }
193
195 template <class T>
196 inline
197 void setTypeDefaults(const T & type, const std::string & values = ""){
198
199 drain::Logger mout(__FILE__, __FUNCTION__);
200
201 drain::Type t(type);
202 const char typechar = drain::Type::getTypeChar(t);
203 if (this->type.empty())
204 this->type = typechar;
205 else {
206 if (this->type.at(0) != typechar)
207 mout.warn("different types: " , this->type , '/' , typechar );
208 }
209
210 scaling.set(1.0, 0.0);
211
212 //if (!type.empty()){ // ?
213 if (typechar != '*'){
214 undetect = drain::Type::call<drain::typeMin, double>(t); //drain::Type::getMin<double>(t);
215 nodata = drain::Type::call<drain::typeMax, double>(t); // drain::Type::call<drain::typeMax,double>(t);
216 }
217 else {
218 undetect = drain::Type::call<drain::typeMin, double>(this->type); //drain::Type::getMin<double>(this->type);
219 nodata = drain::Type::call<drain::typeMax, double>(this->type); //drain::Type::call<drain::typeMax,double>(this->type);
220 }
221
222 setValues(values);
223
224 }
225
226 inline
227 void setTypeDefaults(){
228 setTypeDefaults(this->type);
229 }
230
232 inline
233 bool isValue(double x) const {
234 return (x != undetect) && (x != nodata);
235 }
236
238 inline
239 double scaleForward(double x) const {
240 return scaling.offset + scaling.scale*x; // TODO: direct
241 }
242
244 inline
245 double scaleInverse(double y) const {
246 return (y-scaling.offset)/scaling.scale; // TODO: direct
247 }
248
250 double getMin() const;
251
253 double getMax() const;
254
256 inline
257 double operator()(double y) const {
258 return (y-scaling.offset)/scaling.scale;
259 }
260
261
263 // ?virtual?
265 void clear();
266
267
269 /*
270 inline
271 void getShortKeys(std::map<std::string,std::string> & m){
272 for (ReferenceMap::iterator it = begin(); it != end(); ++it){
273 const std::string & longKey = it->first;
274 const std::string shortKey = longKey.substr(0,longKey.find(':'));
275 m[shortKey] = longKey;
276 std::cerr << shortKey << '#' << longKey << '\n';
277 }
278 }
279 */
280
281
283 virtual
284 void updateLenient(const EncodingODIM & odim);
285
286
288 /*
289 * Does not change the values of the map.
290 *
291 * The object itself can be given as an argument, \see addShortKeys().
292 */
294
296 /*
297 * Does not change the values of the map.
298 */
299 inline
301 grantShortKeys(*this);
302 }
303
305
308 void copyFrom(const drain::image::Image & data);
309
311
314 static
315 inline
316 void checkType(Hi5Tree & dst){
317 EncodingODIM odim;
318 checkType(dst, odim);
319 } // Temp for thread-safety.
320
321
323
334 static
335 const ODIMPathElemSeq & attributeGroups;
336
337 //bool RANGE_DRIVEN;
338
339protected:
340
341
342
343 static
344 void checkType(Hi5Tree & dst, EncodingODIM & odim); // SEE ABOVE?
345
346
347 static
348 const ODIMPathElemSeq & getAttributeGroups();
349
350
351private:
352
353 virtual // must
354 void init(group_t initialize = ODIMPathElem::ALL_LEVELS);
355
356 void initFromImage(const drain::image::Image & img);
357
358};
359
360
361
362} // namespace rack
363
364
365#endif
366
367// Rack
LogSourc e is the means for a function or any program segment to "connect" to a Log.
Definition Log.h:312
Logger & warn(const TT &... args)
Possible error, but execution can continue.
Definition Log.h:430
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
void updateFromMap(const std::map< std::string, T2 > &m)
Assign values from a map. Updates existing entries only.
Definition SmartMap.h:294
Utilities related to std::type_info.
Definition Type.h:51
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:257
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:173
double scaleInverse(double y) const
Converts a quantity to storage scale: x = (y-offset)/gain .
Definition EncodingODIM.h:245
void addShortKeys()
Creates a short alias (attrib) for each (group):(attrib). Example: "gain" => "what:gain".
Definition EncodingODIM.h:300
void setType(const std::type_info &type)
Complete with setTypeDefaults()
Definition EncodingODIM.h:187
static const ODIMPathElemSeq & attributeGroups
Copies contents of this to a h5 group.
Definition EncodingODIM.h:335
std::string type
This is non-standard (not in ODIM), but a practical means of handling storage type of datasets.
Definition EncodingODIM.h:152
double scaleForward(double x) const
Converts a quantity from storage scale: y = offset + gain*y .
Definition EncodingODIM.h:239
static void checkType(Hi5Tree &dst)
Traverses recursively subtrees and checks the types of PolarODIM variables.
Definition EncodingODIM.h:316
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:233
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:197
double nodata
data[n]/what (obligatory)
Definition EncodingODIM.h:157
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