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
51
67class QuantityMap : public std::map<std::string, Quantity> {
68
69public:
70
71 typedef std::map<std::string, Quantity> map_t;
72
75
77 QuantityMap(const QuantityMap & m);
78
80 // QuantityMap(const map_t & m);
81
83 QuantityMap(const std::initializer_list<std::pair<std::string, Quantity> > & inits);
84
85 // New 2025: a set of frequently needed conf tables
86 // const Quantity & TH;
87 const Quantity & DBZ;
88 const Quantity & VRAD;
89 const Quantity & ZDR;
90 const Quantity & RHOHV;
91 const Quantity & KDP;
92 // const Quantity & PHIDP;
93 const Quantity & QIND;
94 const Quantity & PROB; // Rack
95 // const Quantity & FUZZY; // Rack
96
97 inline
98 QuantityMap & operator=(const std::initializer_list<std::pair<std::string, Quantity> > & inits){
99 assign(inits);
100 return *this;
101 }
102
103 void assign(const std::initializer_list<std::pair<std::string, Quantity> > & inits);
104
106
109 inline
110 bool hasKey(const std::string & key) const {
111 return find(key) != end(); // revised 2025
112 }
113
115
118 inline
119 bool hasQuantity(const std::string & key) const {
120 // return find(key) != end(); // revised 2025
121 return retrieve(key) != end(); // revised 2025
122 }
123
125
132 iterator retrieve(const std::string & key);
133
135
142 const_iterator retrieve(const std::string & key) const;
143
144 const Quantity & get(const std::string & key) const;
145
146 Quantity & get(const std::string & key);
147
149
155 bool setQuantityDefaults(EncodingODIM & dst, const std::string & quantity, const std::string & values = "") const;
156
157 inline
158 bool setQuantityDefaults(ODIM & dst) const {
159 return setQuantityDefaults(dst, dst.quantity);
160 }
161
163
172 template <class D>
173 bool setQuantityDefaults(PlainData<D> & dstData, const std::string & quantity = "", const std::string & values = "") const {
174
175 drain::Logger mout("QuantityMap", __FUNCTION__);
176
177 const std::string & q = !quantity.empty() ? quantity : dstData.odim.quantity;
178
179 const bool typeSet = setQuantityDefaults(dstData.odim, q, values);
180
181 if (dstData.odim.quantity.empty()){
182 if (!q.empty()){
183 dstData.odim.quantity = q;
184 }
185 else {
186 mout.warn("quantity neither given nor set already" );
187 }
188 }
189
190 if (!typeSet){
191 mout.warn("conf for " , quantity , "[" , dstData.odim.type , "] not found" );
192 }
193 // Redesign all this...
194 dstData.data.setType(dstData.odim.type);
195 //dstData.data.scaling.set(dstData.odim.scaling);
196 //dstData.data.setScaling(dstData.odim.scaling.scale, dstData.odim.scaling.offset);
197 dstData.data.setScaling(dstData.odim.scaling); // needed?
198
199 //if ((dstData.odim.quantity == "QIND") || (dstData.odim.quantity == "PROB")){
200 if ((q == "QIND") || (q == "PROB")){
201 //dstData.data.setOptimalScale(0.0, 1.0);
202 //dstData.data.setPhysicalRange(0.0, 1.0, true);
203 dstData.data.getScaling().setPhysicalRange(0.0, 1.0); // note: does not change scaling
204 }
205 else if (q == "CLASS"){
206 //dstData.data.setOptimalScale(0.0, 1.0);
207 drain::image::Image & img = dstData.data;
208 img.setPhysicalRange(0.0, img.getConf().getTypeMax<double>());
209 //dstData.data.getScaling().setPhysicalRange(0.0, 1.0); // note: does not change scaling
210 }
211
212
213 if (dstData.data.getName().empty())
214 dstData.data.setName(dstData.odim.quantity);
215
216 mout.debug2("final scaling for " , dstData.odim.quantity , '[' , quantity , ']' , dstData.data.getScaling() );
217
218 return typeSet;
219 }
220
222 template <class M>
223 inline
224 bool isNormalized(const M odim) const {
225 const Quantity & q = get(odim.quantity);
226 if (!q.defaultType){
227 drain::Logger mout("QuantityMap", __FUNCTION__);
228 mout.warn("no default type for quantity:" , odim.quantity );
229 return false;
230 }
232 }
233
235 std::ostream & toStream(std::ostream & ostr) const;
236
237
238};
239
240inline
241std::ostream & operator<<(std::ostream & ostr, const QuantityMap & map){
242 return map.toStream(ostr);
243}
244
245QuantityMap & getQuantityMap();
246
247
248
249} // namespace rack
250
251
252#endif
253
254// 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:103
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
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
Registry for regular quantities appearing in weather radar.
Definition QuantityMap.h:67
bool setQuantityDefaults(PlainData< D > &dstData, const std::string &quantity="", const std::string &values="") const
Definition QuantityMap.h:173
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:224
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:119
bool hasKey(const std::string &key) const
Checks if an exact match, without checking variants, is found.
Definition QuantityMap.h:110
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