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 setTypeDefaults(const T & type, const std::string & values = ""){
188
189 drain::Logger mout(__FILE__, __FUNCTION__);
190
191 drain::Type t(type);
192 const char typechar = drain::Type::getTypeChar(t);
193 if (this->type.empty())
194 this->type = typechar;
195 else {
196 if (this->type.at(0) != typechar)
197 mout.warn("different types: " , this->type , '/' , typechar );
198 }
199
200 scaling.set(1.0, 0.0);
201
202 //if (!type.empty()){ // ?
203 if (typechar != '*'){
204 undetect = drain::Type::call<drain::typeMin, double>(t); //drain::Type::getMin<double>(t);
205 nodata = drain::Type::call<drain::typeMax, double>(t); // drain::Type::call<drain::typeMax,double>(t);
206 }
207 else {
208 undetect = drain::Type::call<drain::typeMin, double>(this->type); //drain::Type::getMin<double>(this->type);
209 nodata = drain::Type::call<drain::typeMax, double>(this->type); //drain::Type::call<drain::typeMax,double>(this->type);
210 }
211
212 setValues(values);
213
214 }
215
216 inline
217 void setTypeDefaults(){
218 setTypeDefaults(this->type);
219 }
220
222 inline
223 bool isValue(double x) const {
224 return (x != undetect) && (x != nodata);
225 }
226
228 inline
229 double scaleForward(double x) const {
230 return scaling.offset + scaling.scale*x; // TODO: direct
231 }
232
234 inline
235 double scaleInverse(double y) const {
236 return (y-scaling.offset)/scaling.scale; // TODO: direct
237 }
238
240 double getMin() const;
241
243 double getMax() const;
244
246 inline
247 double operator()(double y) const {
248 return (y-scaling.offset)/scaling.scale;
249 }
250
251
253 // ?virtual?
255 void clear();
256
257
259 /*
260 inline
261 void getShortKeys(std::map<std::string,std::string> & m){
262 for (ReferenceMap::iterator it = begin(); it != end(); ++it){
263 const std::string & longKey = it->first;
264 const std::string shortKey = longKey.substr(0,longKey.find(':'));
265 m[shortKey] = longKey;
266 std::cerr << shortKey << '#' << longKey << '\n';
267 }
268 }
269 */
270
271
273 virtual
274 void updateLenient(const EncodingODIM & odim);
275
276
278 /*
279 * Does not change the values of the map.
280 *
281 * The object itself can be given as an argument, \see addShortKeys().
282 */
284
286 /*
287 * Does not change the values of the map.
288 */
289 inline
291 grantShortKeys(*this);
292 }
293
295
298 void copyFrom(const drain::image::Image & data);
299
301
304 static
305 inline
306 void checkType(Hi5Tree & dst){
307 EncodingODIM odim;
308 checkType(dst, odim);
309 } // Temp for thread-safety.
310
311
313
324 static
325 const ODIMPathElemSeq & attributeGroups;
326
327 //bool RANGE_DRIVEN;
328
329protected:
330
331
332
333 static
334 void checkType(Hi5Tree & dst, EncodingODIM & odim); // SEE ABOVE?
335
336
337 static
338 const ODIMPathElemSeq & getAttributeGroups();
339
340
341private:
342
343 virtual // must
344 void init(group_t initialize = ODIMPathElem::ALL_LEVELS);
345
346 void initFromImage(const drain::image::Image & img);
347
348};
349
350
351
352} // namespace rack
353
354
355#endif
356
357// 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:184
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:247
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:235
void addShortKeys()
Creates a short alias (attrib) for each (group):(attrib). Example: "gain" => "what:gain".
Definition EncodingODIM.h:290
static const ODIMPathElemSeq & attributeGroups
Copies contents of this to a h5 group.
Definition EncodingODIM.h:325
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:229
static void checkType(Hi5Tree &dst)
Traverses recursively subtrees and checks the types of PolarODIM variables.
Definition EncodingODIM.h:306
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:223
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:187
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:418
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