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 <cmath>
35#include <string>
36//#include <drain/util/Options.h>
37#include <drain/util/ReferenceMap.h>
38#include <drain/util/Time.h>
39#include <drain/util/ValueScaling.h>
40
41#include "hi5/Hi5.h"
42#include "ODIMPath.h"
43
44namespace rack {
45
47
70class EncodingODIM : public drain::ReferenceMap { // public ODIMPathElem,
71
72public:
73
75 typedef ODIMPathElem::group_t group_t;
76
77 drain::ValueScaling & scaling;
78 const drain::ValueScaling & scalingConst;
79
80 drain::ValueScaling ownScaling;
81
82 typedef enum {NONE=0, SCALING=1, RANGE=2} ExplicitSetting;
83
84 static
85 const drain::FlagResolver::dict_t settingDict;
86
87 //typedef drain::EnumFlagger<drain::SingleFlagger<TiffCompliance> > tiffComplianceFlagger;
88 //typedef drain::EnumFlagger<drain::MultiFlagger<Adaption> > AdaptionFlagger;
89
90 int explicitSettings = NONE;
91
93 inline
94 EncodingODIM(group_t initialize = ODIMPathElem::ALL_LEVELS) : scaling(ownScaling), scalingConst(ownScaling), explicitSettings(NONE){
95 init(initialize);
96 };
97
99 EncodingODIM(const EncodingODIM & odim);
100
102
105 EncodingODIM(char type, double scale=1.0, double offset=NAN, double nodata = NAN, double undetect = NAN, const drain::Range<double> & range = {0,0});
106
108
111 EncodingODIM(char type, const drain::Range<double> & range, double scale=0.0, double offset=NAN, double nodata = NAN, double undetect = NAN);
112
113
114 inline
115 EncodingODIM(const drain::image::Image & image) : scaling(ownScaling), scalingConst(image.getScaling()), explicitSettings(NONE) {
116 initFromImage(image);
117 };
118
119 inline
120 EncodingODIM(drain::image::Image & image) : scaling(image.getScaling()), scalingConst(image.getScaling()), explicitSettings(NONE) {
121 initFromImage(image);
122 };
123
124 inline
125 void copyEncoding(const EncodingODIM & odim){
126 // updateFromMap(odim); // risk: copies attribs also beyon ODIM?
127 type = odim.type;
128 scaling.set(odim.scaling);
129 nodata = odim.nodata;
130 undetect = odim.undetect;
131 explicitSettings = odim.explicitSettings;
132 }
133
134 inline
135 EncodingODIM & operator=(const EncodingODIM & odim) {
136 // std::cerr << "EncodingODIM & operator=" << std::endl;
137 // updateFromMap(odim);
138 // explicitSettings = odim.explicitSettings;
139 copyEncoding(odim);
140 return *this;
141 }
142
143 // In case of const image this is not "true" ? But for const EncodingODIM, never called?
144 operator drain::ValueScaling & (){
145 return scaling;
146 }
147
148 operator const drain::ValueScaling & () const {
149 return scalingConst;
150 }
151
152
154
157 std::string type;
158
159 double nodata;
160 double undetect;
161
162 EncodingODIM & setScaling(double gain, double offset = NAN);
163
164 EncodingODIM & setScaling(double gain, double offset, double undetect, double nodata);
165
166 inline
167 bool isSet() const {
168 return ((scaling.scale != 0.0) && (!type.empty()) && (type.at(0) != '*'));
169 }
170
172 // todo: bool operator==(const EncodingODIM & odim);
173 static
174 inline
175 bool haveSimilarEncoding(const EncodingODIM & odim1, const EncodingODIM & odim2){
176 return (odim1.type == odim2.type) &&
177 (odim1.scaling.scale == odim2.scaling.scale) &&
178 (odim1.scaling.offset == odim2.scaling.offset) &&
179 (odim1.undetect == odim2.undetect) &&
180 (odim1.nodata == odim2.nodata)
181 ;
182 };
183
184 void setRange(double min, double max);
185
187 //template <class T>
188 inline
189 void setType(const std::type_info & type){
190 this->type = drain::Type::getTypeChar(type);
191 scaling.set(1.0, 0.0);
192 undetect = drain::Type::call<drain::typeMin, double>(type);
193 nodata = drain::Type::call<drain::typeMax, double>(type);
194 }
195
197 template <class T>
198 inline
199 void setTypeDefaults(const T & type, const std::string & values = ""){
200
201 drain::Logger mout(__FILE__, __FUNCTION__);
202
203 drain::Type t(type);
204 const char typechar = drain::Type::getTypeChar(t);
205 if (this->type.empty())
206 this->type = typechar;
207 else {
208 if (this->type.at(0) != typechar)
209 mout.warn("different types: " , this->type , '/' , typechar );
210 }
211
212 scaling.set(1.0, 0.0);
213
214 //if (!type.empty()){ // ?
215 if (typechar != '*'){
216 undetect = drain::Type::call<drain::typeMin, double>(t); //drain::Type::getMin<double>(t);
217 nodata = drain::Type::call<drain::typeMax, double>(t); // drain::Type::call<drain::typeMax,double>(t);
218 }
219 else {
220 undetect = drain::Type::call<drain::typeMin, double>(this->type); //drain::Type::getMin<double>(this->type);
221 nodata = drain::Type::call<drain::typeMax, double>(this->type); //drain::Type::call<drain::typeMax,double>(this->type);
222 }
223
224 setValues(values);
225
226 }
227
228 inline
229 void setTypeDefaults(){
230 setTypeDefaults(this->type);
231 }
232
234 inline
235 bool isValue(double x) const {
236 return (x != undetect) && (x != nodata);
237 }
238
240 inline
241 double scaleForward(double x) const {
242 return scaling.offset + scaling.scale*x; // TODO: direct
243 }
244
246 inline
247 double scaleInverse(double y) const {
248 return (y-scaling.offset)/scaling.scale; // TODO: direct
249 }
250
252 double getMin() const;
253
255 double getMax() const;
256
258 inline
259 double operator()(double y) const {
260 return (y-scaling.offset)/scaling.scale;
261 }
262
263
265 // ?virtual?
267 void clear();
268
269
271 /*
272 inline
273 void getShortKeys(std::map<std::string,std::string> & m){
274 for (ReferenceMap::iterator it = begin(); it != end(); ++it){
275 const std::string & longKey = it->first;
276 const std::string shortKey = longKey.substr(0,longKey.find(':'));
277 m[shortKey] = longKey;
278 std::cerr << shortKey << '#' << longKey << '\n';
279 }
280 }
281 */
282
283
285 virtual
286 void updateLenient(const EncodingODIM & odim);
287
288
290 /*
291 * Does not change the values of the map.
292 *
293 * The object itself can be given as an argument, \see addShortKeys().
294 */
296
298 /*
299 * Does not change the values of the map.
300 */
301 inline
303 grantShortKeys(*this);
304 }
305
307
310 void copyFrom(const drain::image::Image & data);
311
313
316 static
317 inline
318 void checkType(Hi5Tree & dst){
319 EncodingODIM odim;
320 checkType(dst, odim);
321 } // Temp for thread-safety.
322
323
325
336 static
337 const ODIMPathElemSeq & attributeGroups;
338
339 //bool RANGE_DRIVEN;
340
341protected:
342
343
344
345 static
346 void checkType(Hi5Tree & dst, EncodingODIM & odim); // SEE ABOVE?
347
348
349 static
350 const ODIMPathElemSeq & getAttributeGroups();
351
352
353private:
354
355 virtual // must
356 void init(group_t initialize = ODIMPathElem::ALL_LEVELS);
357
358 void initFromImage(const drain::image::Image & img);
359
360};
361
362
363
364} // namespace rack
365
366
367#endif
368
369// 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:215
void setValues(const std::string &entries, char assignmentChar='=', char separatorChar=0)
Sets values. If strictness==STRICTLY_CLOSED, throws exception if tries to assign a non-existing entry...
Definition SmartMap.h:299
Utilities related to std::type_info.
Definition Type.h:48
Linear scaling and physical range for image intensities.
Definition ValueScaling.h:63
double & scale
Multiplicative coefficient \i a in: y = ax + b.
Definition ValueScaling.h:67
double & offset
Additive coefficient \i b in: y = ax + b.
Definition ValueScaling.h:70
Class for multi-channel digital images. Supports dynamic typing with base types (char,...
Definition Image.h:183
Structure for data storage type, scaling and marker codes. Does not contain quantity.
Definition EncodingODIM.h:70
double operator()(double y) const
Functor (why inverse?)
Definition EncodingODIM.h:259
EncodingODIM(group_t initialize=ODIMPathElem::ALL_LEVELS)
Default constructor.
Definition EncodingODIM.h:94
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:175
double scaleInverse(double y) const
Converts a quantity to storage scale: x = (y-offset)/gain .
Definition EncodingODIM.h:247
void addShortKeys()
Creates a short alias (attrib) for each (group):(attrib). Example: "gain" => "what:gain".
Definition EncodingODIM.h:302
void setType(const std::type_info &type)
Complete with setTypeDefaults()
Definition EncodingODIM.h:189
static const ODIMPathElemSeq & attributeGroups
Copies contents of this to a h5 group.
Definition EncodingODIM.h:337
std::string type
This is non-standard (not in ODIM), but a practical means of handling storage type of datasets.
Definition EncodingODIM.h:157
double scaleForward(double x) const
Converts a quantity from storage scale: y = offset + gain*y .
Definition EncodingODIM.h:241
static void checkType(Hi5Tree &dst)
Traverses recursively subtrees and checks the types of PolarODIM variables.
Definition EncodingODIM.h:318
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:235
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:199
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:120
unsigned int group_t
In H5, "groups" correspond to directories or folders in file system.
Definition ODIMPath.h:91
Definition DataSelector.cpp:44