WSL/SLF GitLab Repository

MeteoData.cc 12 KB
Newer Older
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
/***********************************************************************************/
/*  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>
19
#include <meteoio/StationData.h>
20
21
22
23
24
25
26
27

using namespace std;
namespace mio {

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

bool MeteoData::initStaticData()
{
	//Associate unsigned int value and a string representation of a meteo parameter
34
35
36
37
38
39
40
41
42
43
44
45
	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";
46
47
48
49
50
51
52
53
54

	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);

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

58

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

63
64
65
66
67
68
69
70
71
72
73
74
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;
}

75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
void MeteoData::associateMemberVariables()
{
	//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;
}

92
void MeteoData::initParameterMap()
93
{//NOTE: any performace improvement here would make a big difference...
94
95
96
97
98
99
100
101
102
103
104
105
106
107
	//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";

108
	//The following mapping needs to be done for every instance of MeteoData
109
	associateMemberVariables();
110
111
	nrOfAllParameters = meteoparam.size();

112
113
114
115
	//Go through all parameters in <int,string> map and store them into <string,double*> map
	map<unsigned int, string>::const_iterator tmpit;
	for (tmpit = meteoparamname.begin(); tmpit != meteoparamname.end(); tmpit++)
		mapParameterByName[tmpit->second] = meteoparam[tmpit->first];
116

117
	//Check for inconsistency between enum Parameters and the two maps meteoparam and meteoparamname
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
	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++;	
}

140
unsigned int MeteoData::getNrOfParameters() const
141
142
{
	return nrOfAllParameters;
143
144
}

145
MeteoData::MeteoData() : date(0.0, 0.), resampled(false)
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
{
	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
{
163
	*this = md; //reverts to the operator= implementation
164
165
166
167
}

MeteoData& MeteoData::operator=(const MeteoData& rhs)
{
168
//NOTE: any performace improvement here would make a big difference...
169
170
171
172
	if (this == &rhs) //Test self assignment
		return *this;

	date = rhs.date;
173
	meta = rhs.meta;
174
	resampled = rhs.resampled;
175
	extraparameters = rhs.extraparameters;
176
	meteoparamname = rhs.meteoparamname;
177

178
179
	associateMemberVariables(); //sets the pointers for the standard parameters in the meteoparam map

180
181
	std::map<unsigned int, double*>::const_iterator tmpit;
	for (unsigned int ii=0; ii<=MeteoData::lastparam; ii++) {
182
		//go through meteoparameters and copy them by value
183
184
		double* val = meteoparam[ii];
		tmpit = rhs.meteoparam.find(ii);
185
186
		*val  = *(tmpit->second);                     //copy by value
		mapParameterByName[meteoparamname[ii]] = val; //copy pointer into mapParameterByName
187
188
189
190
	}

	for (unsigned int ii=MeteoData::lastparam+1; ii<rhs.getNrOfParameters(); ii++) {
		//for the extraparameters other measures are necessary
191
192
193
194
		const string& name = meteoparamname[ii];  //get the name of that extra parameter
		double* val = &extraparameters[name];     //copy the pointer
		meteoparam[ii] = val;                     //copy pointer into meteoparam
		mapParameterByName[name] = val;           //copy pointer into mapParameterByName
195
196
197
198
199
	}

	nrOfAllParameters = meteoparam.size();

	//Check for inconsistency between enum Parameters and the two maps meteoparam and meteoparamname
200
201
	//if ((meteoparam.size() != meteoparamname.size()) || (meteoparam.size() != getNrOfParameters()))
	//	throw IOException("Inconsistency within class MeteoData: Check function initParameterMap()", AT);
202

203
204
205
	return *this;
}

206
void MeteoData::setDate(const Date& in_date)
207
{
208
	date = in_date;
209
210
}

211
212
213
214
215
216
217
218
void MeteoData::reset()
{
	std::map<unsigned int, double*>::iterator it;
	for (it=meteoparam.begin(); it!=meteoparam.end(); it++){
		*it->second = IOUtils::nodata;
	}	
}

219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
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;
	}
}

236
bool MeteoData::isResampled() const
237
238
239
240
{
	return resampled;
}

241
void MeteoData::setResampled(const bool& in_resampled)
242
{
243
	resampled = in_resampled;
244
245
246
247
248
249
250
}

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);

251
	for (unsigned int ii=0; ii<in.getNrOfParameters(); ii++){
252
253
254
255
256
257
258
259
260
261
262
263
264
265
		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
266
	if (parindex >= getNrOfParameters())
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
		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);
}

284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
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);
}

308
309
310
311
312
313
314
315
316
317
unsigned int MeteoData::getParameterIndex(const std::string& parname) const
{
	for (map<unsigned int, string>::const_iterator it=meteoparamname.begin(); it!=meteoparamname.end(); it++){
		if (it->second == parname)
			return it->first;
	}

	return IOUtils::npos; //parameter not a part of MeteoData
}

318
319
320
321
322
323
324
325
326
327
328
bool MeteoData::param_exists(const std::string& parname) const
{
	std::map<std::string, double*>::const_iterator it;
	it = mapParameterByName.find(parname);
	if (it != mapParameterByName.end()) 
		return true;

	return false;
}


329
330
331
std::ostream& operator<<(std::ostream& os, const MeteoData& data) {

	os << "<meteo>\n";
332
	os << data.meta;
333
	os << data.date.toString(Date::FULL) << "\n";
334
335
336
337

	std::map<unsigned int, double*>::const_iterator it1;
	for (it1=data.meteoparam.begin(); it1 != data.meteoparam.end(); it1++){
		if( (*it1->second) != IOUtils::nodata ) {
338
			os << setw(7) << data.getNameForParameter(it1->first) << ":" << setw(15) << *it1->second << "\n";
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
		}
	}
	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;
	}
}

354
355
} //namespace

356
#ifdef _POPC_
357
358
#include "marshal_meteoio.h"
using namespace mio; //HACK for POPC
359
360
361
362
363
void MeteoData::Serialize(POPBuffer &buf, bool pack)
{
	if (pack){
		buf.Pack(&resampled,1);
		date.Serialize(buf,true);
364
		meta.Serialize(buf,true);
365
		buf.Pack(&nrOfAllParameters,1);
366
367
368
369
370
		buf.Pack(&ta,1);
		buf.Pack(&vw,1);
		buf.Pack(&dw,1);
		buf.Pack(&rh,1);
		buf.Pack(&hnw,1);
371
372
373
		buf.Pack(&iswr,1);
		buf.Pack(&rswr,1);
		buf.Pack(&ilwr,1);
374
375
376
		buf.Pack(&tsg,1);
		buf.Pack(&tss,1);
		buf.Pack(&hs,1);
377
378
		buf.Pack(&p,1);
		marshal_map_str_dbl(buf, extraparameters, 0, FLAG_MARSHAL, NULL);
379
	} else {
380
381
		buf.UnPack(&resampled,1);
		date.Serialize(buf,false);
382
		meta.Serialize(buf,false);
383
		buf.UnPack(&nrOfAllParameters,1);
384
385
386
387
388
		buf.UnPack(&ta,1);
		buf.UnPack(&vw,1);
		buf.UnPack(&dw,1);
		buf.UnPack(&rh,1);
		buf.UnPack(&hnw,1);
389
390
391
		buf.UnPack(&iswr,1);
		buf.UnPack(&rswr,1);
		buf.UnPack(&ilwr,1);
392
393
394
		buf.UnPack(&tsg,1);
		buf.UnPack(&tss,1);
		buf.UnPack(&hs,1);
395
396
		buf.UnPack(&p,1);
		marshal_map_str_dbl(buf, extraparameters, 0, !FLAG_MARSHAL, NULL);
397
398
399
400
401
		initParameterMap();
	}
}
#endif