ParamInfo.h
Go to the documentation of this file.
1/*!
2 * \file ParamInfo.h
3 * \brief Class to store parameter item information
4 *
5 * Changelog:
6 * - 1. 2018-05-18 - lj - Code review and reformat.
7 * - 2. 2022-08-18 - lj - Change float to FLTPT, use template to support int and float.
8 *
9 * \author Junzhi Liu, Liangjun Zhu, Shen Shen
10 * \version 2.0
11 */
12#ifndef SEIMS_PARAMETER_INFO_H
13#define SEIMS_PARAMETER_INFO_H
14#include <vector>
15#include <algorithm>
16
17#include "utils_math.h"
18#include "utils_string.h"
19
20#include "MetadataInfoConst.h"
21#include "basic.h"
22#include "text.h"
23
24using namespace ccgl;
25using namespace utils_math;
26using namespace utils_string;
27using std::vector;
28using std::map;
29
30/*!
31* \ingroup data
32 * \class ParamInfo
33 *
34 * \brief Class to store and manage parameter information from the parameter database
35 */
36template <typename T>
37class ParamInfo {
38public:
39 //! Construct an empty instance.
40 ParamInfo();
41
42 //! Construct for initial parameters from DB
43 ParamInfo(string& name, string& desc, string& unit, string& mid, T value, string& change, T impact,
44 T maximum, T minimum, bool isint);
45
46 //! Construct for module Parameter
47 ParamInfo(string& name, string& basicname, string& desc, string& unit, string& source, string& mid,
48 dimensionTypes dim, string& climtype, T value = 0);
49
50 //! Construct for module Input, Output, and InOutput
51 ParamInfo(string& name, string& basicname, string& desc, string& unit, string& source, string& mid,
52 dimensionTypes dim, transferTypes tftype, string& climtype,
53 bool isconst, bool isoutput);
54
55 //! Copy constructor
56 ParamInfo(const ParamInfo& another);
57
58 //! Destructor
59 ~ParamInfo();
60
61 /*!
62 * \brief Return the adjusted value for this parameter
63 * \param[in] pre_value Default is NODATA_VALUE which will be treated
64 * as ParamInfo.Value, otherwise adjust the given value.
65 * \return adjusted float value
66 */
67 T GetAdjustedValue(T pre_value = NODATA_VALUE);
68
69 //! Adjust value with indexed impact
70 T GetAdjustedValueWithImpactIndexes(T pre_value, int curImpactIndex);
71
72 //! Adjust 1D array
73 void Adjust1DArray(int n, T* data);
74
75 //! Adjust 1D Raster, \sa Adjust1DArray()
76 void Adjust1DRaster(int n, T* data);
77
78 //! Adjust 1D Raster on selected area
79 int Adjust1DRaster(int n, T* data, const int* units, const vector<int>& selunits,
80 const int* lu, const vector<int>& sellu);
81
82 //! Adjust 1D Raster on selected area, using impact index version
83 int Adjust1DRasterWithImpactIndexes(int n, T* data, const int* units,
84 const vector<int>& selunits, const map<int, int>& impactIndexes,
85 const int* lu, const vector<int>& sellu);
86
87 //! Adjust 2D array
88 void Adjust2DArray(int n, T** data);
89
90 //! Adjust 2D Raster
91 void Adjust2DRaster(int n, int lyrs, T** data);
92
93 //! Adjust 1D Raster on selected area
94 int Adjust2DRaster(int n, int lyrs, T** data, const int* units, const vector<int>& selunits,
95 const int* lu, const vector<int>& sellu);
96
97 //! Adjust 2D Raster on selected area, using impact index version
98 int Adjust2DRasterWithImpactIndexes(int n, int lyrs, T** data, const int* units,
99 const vector<int>& selunits, const map<int, int>& impactIndexes,
100 const int* lu, const vector<int>& sellu);
101
102 //! Name
103 string Name;
104 //! Basic name
105 string BasicName;
106 //! Description
108 //! Units
109 string Units;
110 //! Source, to identify which the parameters can be derived
111 string Source;
112 //! Used by Module Ids
113 string ModuleID;
114 //! Data dimension type
116 //! Data transfer type
118 //! Value
120 //! Change type
121 string Change;
122 //! Impact value
124 //! Absolute maximum value
126 //! Absolute minimum value
128 //! is integer?
130 //! Dependence parameters
132 //! Climate type
134 //! Is constant or not
136 //! Is output or not
138 //! Is output to other modules or not
140 //! whether is initialized
142 //! If the BMP effectiveness is variable, set the values of impacts
143 vector<T> ImpactSeries;
144};
145
146
147/*******************************************************/
148/************* Implementation Code Begin ***************/
149/*******************************************************/
150
151template <typename T>
153 Name(""), BasicName(""), Description(""), Units(""), Source(""), ModuleID(""),
154 Dimension(DT_Unknown), Transfer(TF_None),
155 Value(0), Change(""), Impact(0), Maximum(0), Minimum(0), IsInteger(false),
156 DependPara(nullptr), ClimateType(""),
157 IsConstant(false), IsOutput(false), OutputToOthers(false),
158 initialized(false), ImpactSeries() {
159}
160
161template <typename T>
162ParamInfo<T>::ParamInfo(string& name, string& desc, string& unit, string& mid, T value, string& change,
163 T impact, T maximum, T minimum, bool isint):
164 Name(name), BasicName(""), Description(desc), Units(unit), Source(""), ModuleID(mid),
165 Dimension(DT_Unknown), Transfer(TF_None),
166 Value(value), Change(change), Impact(impact), Maximum(maximum), Minimum(minimum), IsInteger(isint),
167 DependPara(nullptr), ClimateType(""),
168 IsConstant(false), IsOutput(false), OutputToOthers(false),
169 initialized(false), ImpactSeries() {
170
171}
172
173template <typename T>
174ParamInfo<T>::ParamInfo(string& name, string& basicname, string& desc, string& unit, string& source, string& mid,
175 dimensionTypes dim, string& climtype, T value /* = 0 */):
176 Name(name), BasicName(basicname), Description(desc), Units(unit), Source(source), ModuleID(mid),
177 Dimension(dim), Transfer(TF_None),
178 Value(value), Change(""), Impact(0), Maximum(0), Minimum(0), IsInteger(false),
179 DependPara(nullptr), ClimateType(climtype),
180 IsConstant(false), IsOutput(false), OutputToOthers(false),
181 initialized(false), ImpactSeries() {
182
183}
184
185template <typename T>
186ParamInfo<T>::ParamInfo(string& name, string& basicname, string& desc, string& unit, string& source, string& mid,
187 dimensionTypes dim, transferTypes tftype, string& climtype,
188 bool isconst, bool isoutput) :
189 Name(name), BasicName(basicname), Description(desc), Units(unit), Source(source), ModuleID(mid),
190 Dimension(dim), Transfer(tftype),
191 Value(0), Change(""), Impact(0), Maximum(0), Minimum(0), IsInteger(false),
192 DependPara(nullptr), ClimateType(climtype),
193 IsConstant(isconst), IsOutput(isoutput), OutputToOthers(false),
194 initialized(false), ImpactSeries() {
195
196}
197
198template <typename T>
200 Name = another.Name;
201 BasicName = another.BasicName;
202 Description = another.Description;
203 Units = another.Units;
204 Source = another.Source;
205 ModuleID = another.ModuleID;
206 Dimension = another.Dimension;
207 Transfer = another.Transfer;
208 Value = another.Value;
209 Change = another.Change;
210 Impact = another.Impact;
211 Maximum = another.Maximum;
212 Minimum = another.Minimum;
213 IsInteger = another.IsInteger;
214 DependPara = another.DependPara;
215 ClimateType = another.ClimateType;
216 IsConstant = another.IsConstant;
217 IsOutput = another.IsOutput;
218 OutputToOthers = another.OutputToOthers;
219 initialized = another.initialized;
220 ImpactSeries = another.ImpactSeries;
221}
222
223template <typename T>
225 if (DependPara != nullptr) {
226 DependPara = nullptr;
227 }
228}
229
230template <typename T>
231T ParamInfo<T>::GetAdjustedValue(const T pre_value /* = NODATA_VALUE */) {
232 T res = pre_value;
233 if (FloatEqual(pre_value, NODATA_VALUE)) {
234 res = Value;
235 }
236 if (FloatEqual(res, NODATA_VALUE)) {
237 /// Do not change NoData value
238 return res;
239 }
240
241 if (StringMatch(Change, PARAM_CHANGE_RC) && !FloatEqual(Impact, 1.)) {
242 res *= Impact;
243 }
244 else if (StringMatch(Change, PARAM_CHANGE_AC) && !FloatEqual(Impact, 0.)) {
245 res += Impact;
246 }
247 else if (StringMatch(Change, PARAM_CHANGE_VC) && !FloatEqual(Impact, NODATA_VALUE)) {
248 res = Impact;
249 }
250 else if (StringMatch(Change, PARAM_CHANGE_NC)) {
251 //don't change
252 return res;
253 }
254
255 if (!FloatEqual(Maximum, NODATA_VALUE) && res > Maximum) res = Maximum;
256 if (!FloatEqual(Minimum, NODATA_VALUE) && res < Minimum) res = Minimum;
257 return res;
258}
259
260template <typename T>
261T ParamInfo<T>::GetAdjustedValueWithImpactIndexes(const T pre_value, const int curImpactIndex) {
262 T res = pre_value;
263 if (FloatEqual(pre_value, NODATA_VALUE)) {
264 res = Value;
265 }
266 if (FloatEqual(res, NODATA_VALUE)) {
267 /// Do not change NoData value
268 return res;
269 }
270
271 T tmpImpact = ImpactSeries[curImpactIndex];
272 if (StringMatch(Change, PARAM_CHANGE_RC) && !FloatEqual(tmpImpact, 1)) {
273 res *= tmpImpact;
274 }
275 else if (StringMatch(Change, PARAM_CHANGE_AC) && !FloatEqual(tmpImpact, 0)) {
276 res += tmpImpact;
277 }
278 else if (StringMatch(Change, PARAM_CHANGE_VC) && !FloatEqual(tmpImpact, NODATA_VALUE)) {
279 res = tmpImpact;
280 }
281 else if (StringMatch(Change, PARAM_CHANGE_NC)) { //don't change
282 return res;
283 }
284
285 if (!FloatEqual(Maximum, NODATA_VALUE) && res > Maximum) res = Maximum;
286 if (!FloatEqual(Minimum, NODATA_VALUE) && res < Minimum) res = Minimum;
287 return res;
288}
289
290template <typename T>
291void ParamInfo<T>::Adjust1DArray(const int n, T* data) {
292#pragma omp parallel for
293 for (int i = 0; i < n; i++) {
294 if (!FloatEqual(data[i], NODATA_VALUE)) {
295 /// Do not change NoData value
296 data[i] = GetAdjustedValue(data[i]);
297 }
298 }
299}
300
301template <typename T>
302void ParamInfo<T>::Adjust1DRaster(const int n, T* data) {
303 Adjust1DArray(n, data);
304}
305
306template <typename T>
307int ParamInfo<T>::Adjust1DRaster(const int n, T* data, const int* units,
308 const vector<int>& selunits,
309 const int* lu, const vector<int>& sellu) {
310 int count = 0;
311 for (int i = 0; i < n; i++) {
312 if (FloatEqual(data[i], NODATA_VALUE)) {
313 /// Do not change NoData value
314 continue;
315 }
316 int curunit = units[i];
317 int curlu = lu[i];
318 if (find(selunits.begin(), selunits.end(), curunit) == selunits.end()) {
319 continue;
320 }
321 if (find(sellu.begin(), sellu.end(), curlu) == sellu.end()) {
322 continue;
323 }
324 data[i] = GetAdjustedValue(data[i]);
325 count += 1;
326 }
327 return count;
328}
329
330template <typename T>
331int ParamInfo<T>::Adjust1DRasterWithImpactIndexes(const int n, T* data, const int* units,
332 const vector<int>& selunits, const map<int, int>& impactIndexes,
333 const int* lu, const vector<int>& sellu) {
334 int count = 0;
335 for (int i = 0; i < n; i++) {
336 if (FloatEqual(data[i], NODATA_VALUE)) {
337 /// Do not change NoData value
338 continue;
339 }
340 int curunit = units[i];
341 int curlu = lu[i];
342 //cannot find, continue
343 if (find(selunits.begin(), selunits.end(), curunit) == selunits.end()) {
344 continue;
345 }
346 if (find(sellu.begin(), sellu.end(), curlu) == sellu.end()) {
347 continue;
348 }
349 map<int, int>::const_iterator it = impactIndexes.find(curunit);
350 if (it == impactIndexes.end()) {
351 continue;
352 }
353 data[i] = GetAdjustedValueWithImpactIndexes(data[i], it->second);
354 count += 1;
355 }
356 return count;
357}
358
359template <typename T>
360void ParamInfo<T>::Adjust2DArray(const int n, T** data) {
361#pragma omp parallel for
362 for (int i = 0; i < n; i++) {
363 Adjust1DArray(CVT_INT(data[i][0]), data[i] + 1);
364 }
365}
366
367template <typename T>
368void ParamInfo<T>::Adjust2DRaster(const int n, const int lyrs, T** data) {
369#pragma omp parallel for
370 for (int i = 0; i < n; i++) {
371 Adjust1DArray(lyrs, data[i]);
372 }
373}
374
375template <typename T>
376int ParamInfo<T>::Adjust2DRaster(const int n, const int lyrs, T** data,
377 const int* units, const vector<int>& selunits,
378 const int* lu, const vector<int>& sellu) {
379 int count = 0;
380 for (int i = 0; i < n; i++) {
381 int curunit = units[i];
382 int curlu = lu[i];
383 if (find(selunits.begin(), selunits.end(), curunit) == selunits.end()) {
384 continue;
385 }
386 if (find(sellu.begin(), sellu.end(), curlu) == sellu.end()) {
387 continue;
388 }
389 for (int j = 0; j < lyrs; j++) {
390 data[i][j] = GetAdjustedValue(data[i][j]);
391 }
392 count += 1;
393 }
394 return count;
395}
396
397template <typename T>
398int ParamInfo<T>::Adjust2DRasterWithImpactIndexes(const int n, const int lyrs, T** data,
399 const int* units, const vector<int>& selunits,
400 const map<int, int>& impactIndexes,
401 const int* lu, const vector<int>& sellu) {
402 int count = 0;
403 for (int i = 0; i < n; i++) {
404 int curunit = units[i];
405 int curlu = lu[i];
406 //cannot find, continue
407 if (find(selunits.begin(), selunits.end(), curunit) == selunits.end()) {
408 continue;
409 }
410 if (find(sellu.begin(), sellu.end(), curlu) == sellu.end()) {
411 continue;
412 }
413 map<int, int>::const_iterator it = impactIndexes.find(curunit);
414 if (it == impactIndexes.end()) {
415 continue;
416 }
417 for (int j = 0; j < lyrs; j++) {
418 data[i][j] = GetAdjustedValueWithImpactIndexes(data[i][j], it->second);
419 }
420 count += 1;
421 }
422 return count;
423}
424
425#endif /* SEIMS_PARAMETER_INFO_H */
Define some const variables used by MetadataInfo class.
@ TF_None
Default, which means no need to be transferred.
Definition: MetadataInfoConst.h:87
@ DT_Unknown
Unknown type.
Definition: MetadataInfoConst.h:64
Basic definitions.
#define CVT_INT(param)
A reference to the postfix of executable file for RELWITHDEBINFO mode.
Definition: basic.h:325
#define NODATA_VALUE
Global utility definitions.
Definition: basic.h:245
bool IsInteger
is integer?
Definition: ParamInfo.h:129
int Adjust2DRasterWithImpactIndexes(int n, int lyrs, T **data, const int *units, const vector< int > &selunits, const map< int, int > &impactIndexes, const int *lu, const vector< int > &sellu)
Adjust 2D Raster on selected area, using impact index version.
Definition: ParamInfo.h:398
vector< T > ImpactSeries
If the BMP effectiveness is variable, set the values of impacts.
Definition: ParamInfo.h:143
string Source
Source, to identify which the parameters can be derived.
Definition: ParamInfo.h:111
T Minimum
Absolute minimum value.
Definition: ParamInfo.h:127
bool initialized
whether is initialized
Definition: ParamInfo.h:141
string Units
Units.
Definition: ParamInfo.h:109
ParamInfo()
Construct an empty instance.
Definition: ParamInfo.h:152
~ParamInfo()
Destructor.
Definition: ParamInfo.h:224
string ClimateType
Climate type.
Definition: ParamInfo.h:133
void Adjust1DArray(int n, T *data)
Adjust 1D array.
Definition: ParamInfo.h:291
T Value
Value.
Definition: ParamInfo.h:119
T GetAdjustedValueWithImpactIndexes(T pre_value, int curImpactIndex)
Adjust value with indexed impact.
Definition: ParamInfo.h:261
string Description
Description.
Definition: ParamInfo.h:107
T Maximum
Absolute maximum value.
Definition: ParamInfo.h:125
string Name
Name.
Definition: ParamInfo.h:103
dimensionTypes Dimension
Data dimension type.
Definition: ParamInfo.h:115
T Impact
Impact value.
Definition: ParamInfo.h:123
string ModuleID
Used by Module Ids.
Definition: ParamInfo.h:113
T GetAdjustedValue(T pre_value=NODATA_VALUE)
Return the adjusted value for this parameter.
Definition: ParamInfo.h:231
bool IsConstant
Is constant or not.
Definition: ParamInfo.h:135
ParamInfo * DependPara
Dependence parameters.
Definition: ParamInfo.h:131
void Adjust1DRaster(int n, T *data)
Adjust 1D Raster,.
Definition: ParamInfo.h:302
bool OutputToOthers
Is output to other modules or not.
Definition: ParamInfo.h:139
void Adjust2DRaster(int n, int lyrs, T **data)
Adjust 2D Raster.
Definition: ParamInfo.h:368
int Adjust1DRasterWithImpactIndexes(int n, T *data, const int *units, const vector< int > &selunits, const map< int, int > &impactIndexes, const int *lu, const vector< int > &sellu)
Adjust 1D Raster on selected area, using impact index version.
Definition: ParamInfo.h:331
bool IsOutput
Is output or not.
Definition: ParamInfo.h:137
transferTypes Transfer
Data transfer type.
Definition: ParamInfo.h:117
string BasicName
Basic name.
Definition: ParamInfo.h:105
void Adjust2DArray(int n, T **data)
Adjust 2D array.
Definition: ParamInfo.h:360
string Change
Change type.
Definition: ParamInfo.h:121
Class to store and manage parameter information from the parameter database.
Definition: ParamInfo.h:37
transferTypes
Float values be transferred across subbasins for MPI version.
Definition: MetadataInfoConst.h:86
dimensionTypes
enum of dimension data types
Definition: MetadataInfoConst.h:63
bool FloatEqual(T1 v1, T2 v2)
Whether v1 is equal to v2.
Definition: utils_math.h:141
bool StringMatch(const char *a, const char *b)
Match char ignore cases.
Common Cross-platform Geographic Library (CCGL)
Predefined string constants used in the code BE CAUTION, constant value must be aligned by SPACE,...
CONST_CHARS PARAM_CHANGE_VC
replace by a value
Definition: text.h:65
CONST_CHARS PARAM_CHANGE_AC
add a value
Definition: text.h:67
CONST_CHARS PARAM_CHANGE_RC
multiply a ratio, which is diff from SWAT: * (1+ratio)
Definition: text.h:66
CONST_CHARS PARAM_CHANGE_NC
no change
Definition: text.h:68
Useful math equations in CCGL.
Handling string related issues in CCGL.