WSL/SLF GitLab Repository

MeteoData.cc 9.55 KB
Newer Older
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
/***********************************************************************************/
/*  Copyright 2009 WSL Institute for Snow and Avalanche Research    SLF-DAVOS      */
/***********************************************************************************/
/* This file is part of MeteoIO.
    MeteoIO is free software: you can redistribute it and/or modify
    it under the terms of the GNU Lesser General Public License as published by
    the Free Software Foundation, either version 3 of the License, or
    (at your option) any later version.

    MeteoIO is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU Lesser General Public License for more details.

    You should have received a copy of the GNU Lesser General Public License
    along with MeteoIO.  If not, see <http://www.gnu.org/licenses/>.
*/
#include <meteoio/MeteoData.h>

using namespace std;
namespace mio {

/************************************************************
 * static section                                           *
 ************************************************************/
const unsigned int MeteoData::nrOfParameters =  MeteoData::lastparam - MeteoData::firstparam + 1;
27
map<unsigned int, string> MeteoData::static_meteoparamname;
28
29
30
31
32
const bool MeteoData::__init = MeteoData::initStaticData();

bool MeteoData::initStaticData()
{
	//Associate unsigned int value and a string representation of a meteo parameter
33
34
35
36
37
38
39
40
41
42
43
44
	static_meteoparamname[TA]   = "TA";
	static_meteoparamname[ISWR] = "ISWR";
	static_meteoparamname[VW]   = "VW";
	static_meteoparamname[DW]   = "DW";
	static_meteoparamname[RH]   = "RH";
	static_meteoparamname[ILWR] = "ILWR";
	static_meteoparamname[HNW]  = "HNW";
	static_meteoparamname[TSG]  = "TSG";
	static_meteoparamname[TSS]  = "TSS";
	static_meteoparamname[HS]   = "HS";
	static_meteoparamname[RSWR] = "RSWR";
	static_meteoparamname[P]    = "P";
45
46
47
48
49
50
51
52
53

	return true;
}

const std::string& MeteoData::getParameterName(const unsigned int& parindex)
{
	if (parindex >= MeteoData::nrOfParameters)
		throw IndexOutOfBoundsException("Trying to access meteo parameter that does not exist", AT);

54
	return MeteoData::static_meteoparamname[parindex];
55
56
}

57

58
59
60
61
/************************************************************
 * non-static section                                       *
 ************************************************************/

62
63
64
65
66
67
68
69
70
71
72
73
const std::string& MeteoData::getNameForParameter(const unsigned int& parindex) const
{
	std::map<unsigned int, std::string>::const_iterator it;
	it = meteoparamname.find(parindex);

#ifndef NOSAFECHECKS
	if (it == meteoparamname.end())
		throw IndexOutOfBoundsException("Trying to get name for parameter that does not exist", AT);
#endif
	return it->second;
}

74
75
void MeteoData::initParameterMap()
{
76
77
78
79
80
81
82
83
84
85
86
87
88
89
	//Associate unsigned int value and a string representation of a meteo parameter
	meteoparamname[TA]   = "TA";
	meteoparamname[ISWR] = "ISWR";
	meteoparamname[VW]   = "VW";
	meteoparamname[DW]   = "DW";
	meteoparamname[RH]   = "RH";
	meteoparamname[ILWR] = "ILWR";
	meteoparamname[HNW]  = "HNW";
	meteoparamname[TSG]  = "TSG";
	meteoparamname[TSS]  = "TSS";
	meteoparamname[HS]   = "HS";
	meteoparamname[RSWR] = "RSWR";
	meteoparamname[P]    = "P";

90
91
92
93
94
95
96
97
98
99
100
101
102
103
	//The following mapping needs to be done for every instance of MeteoData
	meteoparam[TA]       = &ta;
	meteoparam[ISWR]     = &iswr;
	meteoparam[VW]       = &vw;
	meteoparam[DW]       = &dw;
	meteoparam[RH]       = &rh;
	meteoparam[ILWR]     = &ilwr;
	meteoparam[HNW]      = &hnw;
	meteoparam[TSG]      = &tsg;
	meteoparam[TSS]      = &tss;
	meteoparam[HS]       = &hs;
	meteoparam[RSWR]     = &rswr;
	meteoparam[P]        = &p;

104
105
106
107
108
109
110
111
112
113
114
115
116
117
	std::map<std::string, double>::iterator it;
	unsigned int counter = lastparam + 1;
	for (it=extraparameters.begin(); it!=extraparameters.end(); it++){
		meteoparamname[counter] = it->first;
		meteoparam[counter]     = &(it->second); //points to the address of the element
		counter++;
	}

	nrOfAllParameters = meteoparam.size();

	for (unsigned int ii=0; ii<getNrOfParameters(); ii++){
		mapParameterByName[getNameForParameter(ii)] = meteoparam[ii];
	}

118
	//Check for inconsistency between enum Parameters and the two maps meteoparam and meteoparamname
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
	if ((meteoparam.size() != meteoparamname.size()) || (meteoparam.size() != getNrOfParameters()))
		throw IOException("Inconsistency within class MeteoData: Check function initParameterMap()", AT);
}

void MeteoData::addParameter(const std::string& i_paramname)
{
	//Check whether name is taken
	std::map<std::string, double*>::const_iterator it;
	it = mapParameterByName.find(i_paramname);
	if (it != mapParameterByName.end())
		throw IndexOutOfBoundsException("Trying to add a meteo parameter that already exists: " + i_paramname, AT);

	//Add parameter to extraparameters map
	extraparameters[i_paramname] = IOUtils::nodata;
	meteoparamname[getNrOfParameters()] = i_paramname;
	meteoparam[getNrOfParameters()] = &extraparameters[i_paramname];
	mapParameterByName[i_paramname] = &extraparameters[i_paramname];

	//Increase nrOfAllParameters
	nrOfAllParameters++;	
}

unsigned int MeteoData::getNrOfParameters()
{
	return nrOfAllParameters;
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
}

MeteoData::MeteoData() : date(0.0), resampled(false)
{
	initParameterMap(); //must be first statement
	initAllParameters();
}

MeteoData::MeteoData(const Date& date_in) : date(date_in), resampled(false)
{
	initParameterMap(); //must be first statement
	initAllParameters();
}

#ifdef _POPC_
MeteoData::MeteoData(const MeteoData& md) : paroc_base()
#else
MeteoData::MeteoData(const MeteoData& md)
#endif
{
	*this = md;
}

MeteoData& MeteoData::operator=(const MeteoData& rhs)
{
	if (this == &rhs) //Test self assignment
		return *this;

	date = rhs.date;
	resampled = rhs.resampled;

175
	extraparameters = rhs.extraparameters;
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
	initParameterMap();

	std::map<unsigned int, double*>::const_iterator it;
	for (it=rhs.meteoparam.begin(); it!=rhs.meteoparam.end(); it++){
		*meteoparam[it->first] = *(it->second);
	}

	return *this;
}

void MeteoData::setDate(const Date& _date)
{
	date = _date;
}

void MeteoData::setData(const MeteoData::Parameters& param, const double& value)
{
	*meteoparam[param] = value;
}

/**
* @brief Standardize nodata values
* The plugin-specific nodata values are replaced by MeteoIO's internal nodata value
*/
void MeteoData::standardizeNodata(const double& plugin_nodata) {
	for(unsigned int ii=0; ii<nrOfParameters; ii++){
		//loop through all meteo params and check whether they're nodata values
		if (param(ii)<=plugin_nodata)
			param(ii) = IOUtils::nodata;
	}
}

bool MeteoData::isResampled()
{
	return resampled;
}

void MeteoData::setResampled(const bool& _resampled)
{
	resampled = _resampled;
}

bool MeteoData::operator==(const MeteoData& in) const
{
	//An object is equal if the date is equal and all meteo parameters are equal
	bool eval = (date==in.date);

	for (unsigned int ii=0; ii<MeteoData::nrOfParameters; ii++){
		eval &= (param(ii) == in.param(ii));
	}

	return eval;
}

bool MeteoData::operator!=(const MeteoData& in) const
{
	return !(*this==in);
}

double& MeteoData::param(const unsigned int& parindex)
{
#ifndef NOSAFECHECKS
238
	if (parindex >= getNrOfParameters())
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
		throw IndexOutOfBoundsException("Trying to access meteo parameter that does not exist", AT);
#endif	
	return *(meteoparam[parindex]);
}

const double& MeteoData::param(const unsigned int& parindex) const
{
	std::map<unsigned int, double*>::const_iterator it;
	it = meteoparam.find(parindex);

#ifndef NOSAFECHECKS
	if (it == meteoparam.end())
		throw IndexOutOfBoundsException("Trying to access meteo parameter that does not exist", AT);
#endif
	return *(it->second);
}

256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
double& MeteoData::param(const std::string& parname)
{
	std::map<std::string, double*>::iterator it;
	it = mapParameterByName.find(parname);

#ifndef NOSAFECHECKS
	if (it == mapParameterByName.end())
		throw IndexOutOfBoundsException("Trying to access meteo parameter that does not exist", AT);
#endif	
	return *(it->second);
}

const double& MeteoData::param(const std::string& parname) const
{
	std::map<std::string, double*>::const_iterator it;
	it = mapParameterByName.find(parname);

#ifndef NOSAFECHECKS
	if (it == mapParameterByName.end())
		throw IndexOutOfBoundsException("Trying to access meteo parameter that does not exist", AT);
#endif
	return *(it->second);
}

280
281
282
283
284
285
286
287
std::ostream& operator<<(std::ostream& os, const MeteoData& data) {

	os << "<meteo>\n";
	os << data.date;

	std::map<unsigned int, double*>::const_iterator it1;
	for (it1=data.meteoparam.begin(); it1 != data.meteoparam.end(); it1++){
		if( (*it1->second) != IOUtils::nodata ) {
288
			os << setw(7) << data.getNameForParameter(it1->first) << ":" << setw(15) << *it1->second << "\n";
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
		}
	}
	os << "</meteo>\n";

	return os;
}

void MeteoData::initAllParameters()
{
	std::map<unsigned int, double*>::iterator it;
	for (it=meteoparam.begin(); it!=meteoparam.end(); it++){
		*meteoparam[it->first] = IOUtils::nodata;
	}
}

#ifdef _POPC_
void MeteoData::Serialize(POPBuffer &buf, bool pack)
{
	if (pack){
		buf.Pack(&resampled,1);
		date.Serialize(buf,true);
		buf.Pack(&ta,1);
		buf.Pack(&iswr,1);
		buf.Pack(&vw,1);
		buf.Pack(&dw,1);
		buf.Pack(&rh,1);
		buf.Pack(&ilwr,1);
		//buf.Pack(&ea,1);
		buf.Pack(&hnw,1);
		buf.Pack(&tsg,1);
		buf.Pack(&tss,1);
		buf.Pack(&hs,1);
		buf.Pack(&rswr,1);
	}else{
		buf.UnPack(&resampled,1);
		date.Serialize(buf,false);
		buf.UnPack(&ta,1);
		buf.UnPack(&iswr,1);
		buf.UnPack(&vw,1);
		buf.UnPack(&dw,1);
		buf.UnPack(&rh,1);
		buf.UnPack(&ilwr,1);
		//buf.UnPack(&ea,1);
		buf.UnPack(&hnw,1);
		buf.UnPack(&tsg,1);
		buf.UnPack(&tss,1);
		buf.UnPack(&hs,1);
		buf.UnPack(&rswr,1);
		initParameterMap();
	}
}
#endif

} //namespace