WSL/SLF GitLab Repository

BufferedIOHandler.cc 10.6 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
/***********************************************************************************/
/*  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/BufferedIOHandler.h>

using namespace std;

namespace mio {

#ifdef _POPC_
25
BufferedIOHandler::BufferedIOHandler(IOHandler& _iohandler, const Config& _cfg) 
26
	: iohandler(_iohandler), cfg(_cfg), meteoprocessor(_cfg), meteoBuffer(), stationBuffer(), startDateBuffer(), endDateBuffer(), mapBufferedGrids()
27
#else
28
BufferedIOHandler::BufferedIOHandler(IOHandler& _iohandler, const Config& _cfg) 
29
	  : IOInterface(NULL), iohandler(_iohandler), cfg(_cfg), meteoprocessor(_cfg), meteoBuffer(), stationBuffer(), startDateBuffer(), endDateBuffer(), mapBufferedGrids()
30
31
#endif
{
32
	setDfltBufferProperties();
33
34
35
36
37
38
39
40
}

#ifdef _POPC_
BufferedIOHandler::~BufferedIOHandler()
#else
BufferedIOHandler::~BufferedIOHandler() throw()
#endif
{
41
	setDfltBufferProperties();
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
}

void BufferedIOHandler::read2DGrid(Grid2DObject& _grid2Dobj, const std::string& _filename)
{
	std::map<std::string, Grid2DObject>::iterator it = mapBufferedGrids.find(_filename);
	if (it != mapBufferedGrids.end()) { //already in map
		_grid2Dobj = (*it).second; 
		return;
	}
	
	Grid2DObject tmpgrid2D;
	iohandler.read2DGrid(tmpgrid2D, _filename);
	mapBufferedGrids[_filename] = tmpgrid2D;
	_grid2Dobj = tmpgrid2D;
}

void BufferedIOHandler::readDEM(DEMObject& _grid2Dobj)
{
	std::map<std::string, Grid2DObject>::iterator it = mapBufferedGrids.find("/:DEM");
61
62
63
64
65
66
67
68
69
70
	if (it != mapBufferedGrids.end()) {
		//already in map. If the update properties have changed,
		//we copy the ones given in input and force the update of the object
		const DEMObject::update_type in_ppt = (DEMObject::update_type)_grid2Dobj.getUpdatePpt();
		_grid2Dobj = (*it).second;
		const DEMObject::update_type buff_ppt = (DEMObject::update_type)_grid2Dobj.getUpdatePpt();
		if(in_ppt!=buff_ppt) {
			_grid2Dobj.setUpdatePpt(in_ppt);
			_grid2Dobj.update();
		}
71
72
73
74
		return;
	}
	
	DEMObject tmpgrid2D;
75
76
	 //copy the updating policy of the destination
	tmpgrid2D.setUpdatePpt((DEMObject::update_type)_grid2Dobj.getUpdatePpt());
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
	iohandler.readDEM(tmpgrid2D);
	mapBufferedGrids["/:DEM"] = tmpgrid2D;
	_grid2Dobj = tmpgrid2D;
}

void BufferedIOHandler::readLanduse(Grid2DObject& _grid2Dobj)
{
	std::map<std::string, Grid2DObject>::iterator it = mapBufferedGrids.find("/:LANDUSE");
	if (it != mapBufferedGrids.end()) { //already in map
		_grid2Dobj = (*it).second; 
		return;
	}
	
	Grid2DObject tmpgrid2D;
	iohandler.readLanduse(tmpgrid2D);
	mapBufferedGrids["/:LANDUSE"] = tmpgrid2D;
	_grid2Dobj = tmpgrid2D;
}

void BufferedIOHandler::readAssimilationData(const Date& _date, Grid2DObject& _grid2Dobj)
{
	std::map<std::string, Grid2DObject>::iterator it = mapBufferedGrids.find("/:ASSIMILATIONDATA" + _date.toString(Date::FULL));
	if (it != mapBufferedGrids.end()) { //already in map
		_grid2Dobj = (*it).second; 
		return;
	}
	
	Grid2DObject tmpgrid2D;
	iohandler.readAssimilationData(_date, tmpgrid2D);
	mapBufferedGrids["/:ASSIMILATIONDATA" + _date.toString(Date::FULL)] = tmpgrid2D;
	_grid2Dobj = tmpgrid2D;
}

void BufferedIOHandler::readStationData(const Date& date, std::vector<StationData>& vecStation)
{
	iohandler.readStationData(date, vecStation);
}

#ifdef _POPC_
void BufferedIOHandler::writeMeteoData(std::vector< std::vector<MeteoData> >& vecMeteo,
							    std::vector< std::vector<StationData> >& vecStation,
							    const std::string& name)
#else 
void BufferedIOHandler::writeMeteoData(const std::vector< std::vector<MeteoData> >& vecMeteo,
							    const std::vector< std::vector<StationData> >& vecStation,
							    const std::string& name)
#endif
{
	iohandler.writeMeteoData(vecMeteo, vecStation, name);
}

void BufferedIOHandler::readMeteoData(const Date& dateStart, const Date& dateEnd, std::vector< std::vector<MeteoData> >& vecMeteo)
{
	std::vector< std::vector<StationData> > vecStation;
	readMeteoData(dateStart, dateEnd, vecMeteo, vecStation);
}

134
void BufferedIOHandler::setDfltBufferProperties()
135
136
137
138
139
140
{
	always_rebuffer = true;
	bufferbefore = Date(2.0);  //minus 2 days
	bufferafter = Date(20.0);  //plus 20 days
}

141
void BufferedIOHandler::readMeteoData(const Date& i_date, std::vector<MeteoData>& vecMeteo, std::vector<StationData>& vecStation){
142
143
144
145
146
147
148
149
150
151
	/* For every station: 
	 * 1) See whether data is already buffered
	 * 2) Filter - includes resampling
	 * 3) Return the values
	 */

	vecMeteo.clear();
	vecStation.clear();

	if (meteoBuffer.size() == 0){ //init
152
		bufferAllData(i_date);
153
154
155
156
	}

	//loop through all meteo buffers, there is one for each station
	for (unsigned int ii=0; ii<meteoBuffer.size(); ii++) {
157
		unsigned int index = IOUtils::npos;
158
159

		StationData tmpsd;
160
		std::string stationID = "";
161
		if (stationBuffer[ii].size() > 0){
162
			stationID = stationBuffer[ii][0].getStationID();
163
164
165
166
			tmpsd = stationBuffer[ii][0];
		}

		if (meteoBuffer[ii].size() > 0) {//check whether meteo data for the date exists in buffer
167
			index = IOUtils::seek(i_date, meteoBuffer[ii], false);
168
169
		}

170
		if (index == IOUtils::npos) { //not in buffer
171
172
			//Check buffering strategy
			bool rebuffer = false;
173
			if ((startDateBuffer.at(ii) > i_date) || (endDateBuffer.at(ii) < i_date)){
174
175
176
177
178
179
				rebuffer = true;
			} else { 
				if (always_rebuffer) rebuffer = true;
			}

			if (rebuffer){
180
				//cout << "[I] Station " << ii << "(" << stationID 
181
				//	<< ") data for date " << i_date.toString(Date::FULL) << " not in buffer ..." << endl;
182
				
183
184
185
				bool dataexists = bufferData(i_date, ii);
				if (dataexists) {//i_date is contained in buffer
					index = IOUtils::seek(i_date, meteoBuffer[ii], false);
186
187
188
189
190
191
				}
			}
		}

		//APPLY FILTERS
		MeteoData md; StationData sd;
192
		if (index != IOUtils::npos) {
193
194
			vector<MeteoData> mBuffer;
			std::vector<StationData> sBuffer;
195
196
197
198
199
200
201
202
			meteoprocessor.processData(i_date, meteoBuffer[ii], stationBuffer[ii], md, sd);
		}
		
		//Check whether StationData is meaningful, try to get meaningful meta data
		if (sd == StationData()){
			try {
				vector<StationData> vecStations;
				iohandler.readStationData(i_date, vecStations);
203
				if (vecStations.size() > ii) sd = vecStations[ii];
204
			} catch(exception& ex) {/*Ignore any exception*/}
205
206
		}

207
		if (index != IOUtils::npos) {
208
209
210
			vecMeteo.push_back(md);
			vecStation.push_back(sd);
		} else {
211
			cout << "[I] No data found for station " << stationID << " at date " << i_date.toString(Date::FULL) 
212
213
				<< endl;
			vecMeteo.push_back(MeteoData());
214
			vecMeteo[ii].date = i_date; //set correct date
215
216
217
218
219

			vecStation.push_back(sd);
		}
	}

220
	if (vecMeteo.size() == 0) {//No data found - return one object set to i_date and nodata in all other fields
221
		vecMeteo.push_back(MeteoData());
222
		vecMeteo[0].date = i_date; //set correct date
223
224

		vecStation.push_back(StationData());
225
		//throw IOException("[E] No data for any station for date " + i_date.toString(Date::FULL) + " found", AT);
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
	}	
}

void BufferedIOHandler::getNextMeteoData(const Date& _date, std::vector<MeteoData>& vecMeteo, std::vector<StationData>& vecStation)
{
	//TODO: check whether there is something in the buffer!
	//Try to rebuffer!

	vecMeteo.clear();
	vecStation.clear();
	
	std::vector< std::vector<MeteoData> > meteoTmpBuffer;
	std::vector< std::vector<StationData> > stationTmpBuffer;
	readMeteoData(_date, (_date-Date(1900,1,2,0,0)), meteoTmpBuffer, stationTmpBuffer);	

	unsigned int emptycounter = 0;
	for (unsigned int ii=0; ii<meteoTmpBuffer.size(); ii++){//stations
		if ((meteoTmpBuffer[ii].size() > 0) && (stationTmpBuffer[ii].size() > 0)){
			vecMeteo.push_back(meteoTmpBuffer[ii][0]);
			vecStation.push_back(stationTmpBuffer[ii][0]);
		} else {
			emptycounter++;
		}
	}

	if (emptycounter == meteoTmpBuffer.size()){
		vecMeteo.clear();
		vecStation.clear();
	}
}

void BufferedIOHandler::bufferAllData(const Date& _date){
	Date fromDate = _date - bufferbefore;
	Date toDate   = _date + bufferafter;

	readMeteoData(fromDate, toDate, meteoBuffer, stationBuffer);

	for (unsigned int ii=0; ii<meteoBuffer.size(); ii++){
		//set the start and the end date of the interval requested for each station
		startDateBuffer.push_back(fromDate);
		endDateBuffer.push_back(toDate);
	}
}

bool BufferedIOHandler::bufferData(const Date& _date, const unsigned int& stationindex)
{
	Date fromDate = _date - bufferbefore;
	Date toDate   = _date + bufferafter;

	readMeteoData(fromDate, toDate, meteoBuffer, stationBuffer, stationindex);
	startDateBuffer.at(stationindex) = fromDate;
	endDateBuffer.at(stationindex) = toDate;

	if (meteoBuffer.size() == 0) {
		return false;
	}
	
	if (meteoBuffer[stationindex].size() == 0) {
		return false;
	}

	if ((!((_date >= meteoBuffer[stationindex][0].date) 
		  && (meteoBuffer[stationindex][meteoBuffer[stationindex].size()-1].date >= _date)))) {
		meteoBuffer[stationindex].clear();
		return false;
	}

	//If we reach this point: Date is definitely covered
	return true;
}

297
void BufferedIOHandler::setBufferPolicy(const buffer_policy& policy)
298
{
299
300
301
302
	if(policy==RECHECK_NODATA)
		always_rebuffer=true;
	else
		always_rebuffer=false;
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
}

void BufferedIOHandler::setBufferDuration(const Date& _beforeDate, const Date& _afterDate)
{
	bufferbefore = _beforeDate; 
	bufferafter  = _afterDate;
}

void BufferedIOHandler::readMeteoData(const Date& dateStart, const Date& dateEnd, 
							   std::vector< std::vector<MeteoData> >& vecMeteo,
							   std::vector< std::vector<StationData> >& vecStation,
							   const unsigned int& stationindex)
	
{
	iohandler.readMeteoData(dateStart, dateEnd, vecMeteo, vecStation, stationindex);
	if ((&meteoBuffer != &vecMeteo) && (&stationBuffer != &vecStation)){
		meteoBuffer = vecMeteo;      //copy by value
		stationBuffer = vecStation;  //copy by value
	}
}

void BufferedIOHandler::readSpecialPoints(std::vector<Coords>& _cpa)
{
	iohandler.readSpecialPoints(_cpa);
}

void BufferedIOHandler::write2DGrid(const Grid2DObject& _grid2Dobj, const std::string& _name)
{
	iohandler.write2DGrid(_grid2Dobj, _name);
}

void BufferedIOHandler::clearBuffer(){
	meteoBuffer.clear();
	stationBuffer.clear();
	startDateBuffer.clear();
	endDateBuffer.clear();
	mapBufferedGrids.clear();
}

} //namespace