Quantity.h
1 /*
2 
3 MIT License
4 
5 Copyright (c) 2017 FMI Open Development / Markus Peura, first.last@fmi.fi
6 
7 Permission is hereby granted, free of charge, to any person obtaining a copy
8 of this software and associated documentation files (the "Software"), to deal
9 in the Software without restriction, including without limitation the rights
10 to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
11 copies of the Software, and to permit persons to whom the Software is
12 furnished to do so, subject to the following conditions:
13 
14 The above copyright notice and this permission notice shall be included in all
15 copies or substantial portions of the Software.
16 
17 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
18 IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
19 FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
20 AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
21 LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
22 OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
23 SOFTWARE.
24 
25 */
26 /*
27 Part of Rack development has been done in the BALTRAD projects part-financed
28 by the European Union (European Regional Development Fund and European
29 Neighbourhood Partnership Instrument, Baltic Sea Region Programme 2007-2013)
30 */
31 #ifndef ODIM_QUANTITY
32 #define ODIM_QUANTITY
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 <drain/util/Range.h>
44 #include "EncodingODIM.h"
45 //#include "Data.h"
46 
47 namespace rack {
48 
50 
53 class Quantity : public std::map<char,EncodingODIM> {
54 
55 public:
56 
57  typedef std::map<char,EncodingODIM> map_t;
58  typedef std::list<EncodingODIM> list_t;
59 
60  std::string name;
61 
63  char defaultType = '\0';
64 
65  drain::Range<double> physicalRange;
66 
68  /*
69  * A physical value that represents all the measurement results that fall below a detection limit.
70  */
71  double undetectValue;
72 
73 
75  inline
76  Quantity(): defaultType('\0'), undetectValue(std::numeric_limits<double>::signaling_NaN()) {
77  }
78 
80  inline
81  Quantity(const Quantity & quantity):
82  map_t(quantity),
83  name(quantity.name),
84  defaultType('\0'),
85  physicalRange(quantity.physicalRange),
86  undetectValue(quantity.undetectValue) {
87 
88  }
89 
91  Quantity(const std::string & name,
92  const drain::Range<double> & range = {},
93  char defaultType='\0',
94  const list_t & l = {},
95  double undetectValue = std::numeric_limits<double>::signaling_NaN());
96 
97 
99  /*
100  *
101  * Set defaultType, if unset.
102  */
103  EncodingODIM & set(char typecode);
104 
105 
107  inline
108  const EncodingODIM & get(char typecode = '\0') const {
109 
110  if (!typecode)
111  typecode = defaultType;
112 
113  const const_iterator it = find(typecode);
114 
115  if (it != end()){ // null ok
116  return it->second;
117  }
118  else {
119  //drain::Logger mout("Quantity", __FUNCTION__);
120  //mout.warn("undefined code for this quantity, code=" , typecode );
121  // TODO return default
122  static EncodingODIM empty;
123  return empty;
124  }
125 
126  }
127 
129  inline
130  const EncodingODIM & get(const std::string & t) const {
131  if (t.length() != 1)
132  //hrow (std::runtime_error(t + "<= illegal std::string in EncodingODIM::"+__FUNCTION__+" line "+__LINE__));
133  throw (std::runtime_error(t+" <= illegal std::string, "+ __FUNCTION__));
134  else
135  return get(t.at(0));
136  }
137 
139  inline
140  bool hasUndetectValue() const {
141  return !std::isnan(undetectValue);
142  }
143 
145 
150  inline
151  void setZero(double value){
152  undetectValue = value;
153  }
154 
156 
159  void setZero(const std::string & value);
160 
162  inline
163  void unsetZero(){
164  undetectValue = std::numeric_limits<double>::signaling_NaN();
165  }
166 
168 
172  inline
173  void setPhysicalRange(double min, double max = std::numeric_limits<double>::max() ){
174  // hasUndetectValue = true;
175  // undetectValue = min;
176  setZero(min);
177  physicalRange.set(min, max);
178  }
179 
181  std::ostream & toStream(std::ostream & ostr) const;
182 
183 
184 };
185 
186 inline
187 std::ostream & operator<<(std::ostream & ostr, const Quantity & q){
188  return q.toStream(ostr);
189 }
190 
191 
192 
193 } // namespace rack
194 
195 
196 #endif
197 
198 // Rack
Structure for data storage type, scaling and marker codes. Does not contain quantity.
Definition: EncodingODIM.h:75
Structure for defining.
Definition: Quantity.h:53
const EncodingODIM & get(char typecode='\0') const
Retrieve the scaling for a given storage type.
Definition: Quantity.h:108
void setZero(double value)
Set a value to be used like a real measurement, for example in interpolation.
Definition: Quantity.h:151
Quantity()
Default constructor.
Definition: Quantity.h:76
void setPhysicalRange(double min, double max=std::numeric_limits< double >::max())
Sets absolute or typical range of this quantity.
Definition: Quantity.h:173
Quantity(const Quantity &quantity)
Copy constructor.
Definition: Quantity.h:81
double undetectValue
A physical value corresponding a very small (unmeasurable) value has been defined.
Definition: Quantity.h:71
const EncodingODIM & get(const std::string &t) const
Retrieve the scaling for a given storage type.
Definition: Quantity.h:130
bool hasUndetectValue() const
True, if a value corresponding a very small (unmeasurable) value has been defined.
Definition: Quantity.h:140
std::ostream & toStream(std::ostream &ostr) const
Print declared encodings (storage types and scalings)
Definition: Quantity.cpp:103
void unsetZero()
Confirm that no value should be used as a substitute of undetected value.
Definition: Quantity.h:163
char defaultType
Default storage type.
Definition: Quantity.h:63
EncodingODIM & set(char typecode)
Declare encoding (a storage type and scaling) for this quantity.
Definition: Quantity.cpp:87
Definition: DataSelector.cpp:44