Loading...
Searching...
No Matches
QuantityMap.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 ODIM_QUANTITY_MAP
32#define ODIM_QUANTITY_MAP
33
34#include <drain/Log.h>
35#include <drain/Type.h>
36#include <ostream>
37//#include <set>
38
39#include <stdexcept>
40
41#include <drain/util/ReferenceMap.h>
42
43#include "Data.h"
44#include "Quantity.h"
45
46
47namespace rack {
48
49
50class QuantityMap : public std::map<std::string, Quantity> {
51
52public:
53
54 typedef std::map<std::string, Quantity> map_t;
55
58
60 QuantityMap(const QuantityMap & m);
61
63 // QuantityMap(const map_t & m);
64
66 QuantityMap(const std::initializer_list<std::pair<std::string, Quantity> > & inits);
67
68 // New 2025: a set of frequently needed conf tables
69 // const Quantity & TH;
70 const Quantity & DBZ;
71 const Quantity & VRAD;
72 const Quantity & ZDR;
73 const Quantity & RHOHV;
74 const Quantity & KDP;
75 // const Quantity & PHIDP;
76 const Quantity & QIND;
77 const Quantity & PROB; // Rack
78 // const Quantity & FUZZY; // Rack
79
80 inline
81 QuantityMap & operator=(const std::initializer_list<std::pair<std::string, Quantity> > & inits){
82 assign(inits);
83 return *this;
84 }
85
86 void assign(const std::initializer_list<std::pair<std::string, Quantity> > & inits);
87
89
92 inline
93 bool hasKey(const std::string & key) const {
94 return find(key) != end(); // revised 2025
95 }
96
98
101 inline
102 bool hasQuantity(const std::string & key) const {
103 // return find(key) != end(); // revised 2025
104 return retrieve(key) != end(); // revised 2025
105 }
106
108
115 iterator retrieve(const std::string & key);
116
118
125 const_iterator retrieve(const std::string & key) const;
126
127 const Quantity & get(const std::string & key) const;
128
129 Quantity & get(const std::string & key);
130
132
138 bool setQuantityDefaults(EncodingODIM & dst, const std::string & quantity, const std::string & values = "") const;
139
140 inline
141 bool setQuantityDefaults(ODIM & dst) const {
142 return setQuantityDefaults(dst, dst.quantity);
143 }
144
146
155 template <class D>
156 bool setQuantityDefaults(PlainData<D> & dstData, const std::string & quantity = "", const std::string & values = "") const {
157
158 drain::Logger mout("QuantityMap", __FUNCTION__);
159
160 const std::string & q = !quantity.empty() ? quantity : dstData.odim.quantity;
161
162 const bool typeSet = setQuantityDefaults(dstData.odim, q, values);
163
164 if (dstData.odim.quantity.empty()){
165 if (!q.empty()){
166 dstData.odim.quantity = q;
167 }
168 else {
169 mout.warn("quantity neither given nor set already" );
170 }
171 }
172
173 if (!typeSet){
174 mout.warn("conf for " , quantity , "[" , dstData.odim.type , "] not found" );
175 }
176 // Redesign all this...
177 dstData.data.setType(dstData.odim.type);
178 //dstData.data.scaling.set(dstData.odim.scaling);
179 //dstData.data.setScaling(dstData.odim.scaling.scale, dstData.odim.scaling.offset);
180 dstData.data.setScaling(dstData.odim.scaling); // needed?
181
182 //if ((dstData.odim.quantity == "QIND") || (dstData.odim.quantity == "PROB")){
183 if ((q == "QIND") || (q == "PROB")){
184 //dstData.data.setOptimalScale(0.0, 1.0);
185 //dstData.data.setPhysicalRange(0.0, 1.0, true);
186 dstData.data.getScaling().setPhysicalRange(0.0, 1.0); // note: does not change scaling
187 }
188 else if (q == "CLASS"){
189 //dstData.data.setOptimalScale(0.0, 1.0);
190 drain::image::Image & img = dstData.data;
191 img.setPhysicalRange(0.0, img.getConf().getTypeMax<double>());
192 //dstData.data.getScaling().setPhysicalRange(0.0, 1.0); // note: does not change scaling
193 }
194
195
196 if (dstData.data.getName().empty())
197 dstData.data.setName(dstData.odim.quantity);
198
199 mout.debug2("final scaling for " , dstData.odim.quantity , '[' , quantity , ']' , dstData.data.getScaling() );
200
201 return typeSet;
202 }
203
205 template <class M>
206 inline
207 bool isNormalized(const M odim) const {
208 const Quantity & q = get(odim.quantity);
209 if (!q.defaultType){
210 drain::Logger mout("QuantityMap", __FUNCTION__);
211 mout.warn("no default type for quantity:" , odim.quantity );
212 return false;
213 }
215 }
216
218 std::ostream & toStream(std::ostream & ostr) const;
219
220
221};
222
223inline
224std::ostream & operator<<(std::ostream & ostr, const QuantityMap & map){
225 return map.toStream(ostr);
226}
227
228QuantityMap & getQuantityMap();
229
230
231
232} // namespace rack
233
234
235#endif
236
237// 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
Logger & debug2(const TT &... args)
Debug information.
Definition Log.h:676
T getTypeMax() const
Returns the maximum value supported by the current storage type.
Definition ImageConf.h:281
void setPhysicalRange(const Range< double > &range, bool rescale=false)
Sets the supported range for physical values and optionally adjusts the scaling for maximal resolutio...
Definition ImageFrame.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
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
ODIM metadata (quantity, gain, offset, undetect, nodata, date, time)
Definition ODIM.h:79
std::string quantity
dataX/what (obligatory)
Definition ODIM.h:181
Essential class for storing radar data.
Definition Data.h:300
Definition QuantityMap.h:50
bool setQuantityDefaults(PlainData< D > &dstData, const std::string &quantity="", const std::string &values="") const
Definition QuantityMap.h:156
bool setQuantityDefaults(EncodingODIM &dst, const std::string &quantity, const std::string &values="") const
Sets default values of given quantity without assigning the quantity. Optionally overrides with user ...
Definition QuantityMap.cpp:207
bool isNormalized(const M odim) const
Checks if data.
Definition QuantityMap.h:207
QuantityMap()
Default constructor.
Definition QuantityMap.cpp:39
bool hasQuantity(const std::string &key) const
Checks if an exact match or a variant, is found.
Definition QuantityMap.h:102
bool hasKey(const std::string &key) const
Checks if an exact match, without checking variants, is found.
Definition QuantityMap.h:93
std::ostream & toStream(std::ostream &ostr) const
Output.
Definition QuantityMap.cpp:99
iterator retrieve(const std::string &key)
Tries to find a quantity, first by exact match, or then among variants.
Definition QuantityMap.cpp:145
Structure for defining quantity.
Definition Quantity.h:55
const EncodingODIM & get(char typecode='\0') const
Retrieve the scaling for a given storage type.
Definition Quantity.cpp:168
char defaultType
Default storage type.
Definition Quantity.h:76
Definition DataSelector.cpp:44
QuantityMap & getQuantityMap()
Definition QuantityMap.cpp:279