WSL/SLF GitLab Repository

Buffer.cc 12 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
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
/***********************************************************************************/
/*  Copyright 2014 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/dataClasses/Buffer.h>

#include <algorithm>

using namespace std;

namespace mio {

bool MeteoBuffer::get(const Date& date, METEO_SET &vecMeteo) const
{
	vecMeteo.clear();

	if (empty() || (date < ts_start) || (date > ts_end)) //data is NOT fully in cache
		return false;

	for (size_t ii=0; ii<ts_buffer.size(); ii++) { //loop over stations
		if (ts_buffer[ii].empty()) continue; //no data in buffer for this station

		const size_t pos = IOUtils::seek(date, ts_buffer[ii], true);
		if (pos!=IOUtils::npos)
			vecMeteo.push_back( ts_buffer[ii][pos] );
	}

	return true;
}

bool MeteoBuffer::get(const Date& date_start, const Date& date_end, std::vector< METEO_SET > &vecMeteo) const
{
	vecMeteo.clear();
48
	vecMeteo.reserve(ts_buffer.size());
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
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
134
135
136
137
138
139
140
141
142
143
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
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197

	if (empty() || (date_start < ts_start) || (date_end > ts_end)) //data is NOT fully in cache
		return false;

	for (size_t ii=0; ii<ts_buffer.size(); ii++) { //loop over stations
		vecMeteo.push_back( vector<MeteoData>() );  //insert one empty vector of MeteoData

		if (ts_buffer[ii].empty()) continue; //no data in buffer for this station
		if (ts_buffer[ii].front().date>date_end || ts_buffer[ii].back().date<date_start) continue; //no data in buffer for this station

		size_t pos_start = IOUtils::seek(date_start, ts_buffer[ii], false);
		if (pos_start==IOUtils::npos) pos_start = 0;
		size_t pos_end = IOUtils::seek(date_end, ts_buffer[ii], false);
		if (pos_end==IOUtils::npos) pos_end = ts_buffer[ii].size() - 1;

		vecMeteo[ii].reserve(pos_end-pos_start+1); //weird that the "insert" does not handle it internally...
		vecMeteo[ii].insert(vecMeteo[ii].begin(), ts_buffer[ii].begin()+pos_start, ts_buffer[ii].begin()+pos_end);
	}

	return true;
}

bool MeteoBuffer::empty() const
{ //the ts_buffer could be empty if there was no data between the provided dates
  //so the empty criteria is if the ts_start and ts_end are valid or undef
	return (ts_start.isUndef() || ts_end.isUndef());
}

void MeteoBuffer::clear()
{
	ts_buffer.clear();
	ts_start.setUndef(true);
	ts_end.setUndef(true);
}

void MeteoBuffer::push(const Date& date_start, const Date& date_end, const std::vector< METEO_SET >& vecMeteo)
{
	if (empty()) {
		ts_start = date_start;
		ts_end = date_end;
		ts_buffer = vecMeteo;
		return;
	}

	const size_t nrStationsBuffer = ts_buffer.size();
	const size_t nrStationsPush = vecMeteo.size();

	//check that we are dealing with the same stations
	if (nrStationsBuffer!=nrStationsPush) {
		ostringstream ss;
		ss << "The number of stations changed over time from " << nrStationsBuffer << " to " << nrStationsPush << ", ";
		ss << "this is not handled yet!";
		throw IOException(ss.str(), AT);
	}

	for (size_t ii=0; ii<nrStationsBuffer; ii++) { //for all stations
		if (ts_buffer[ii].empty() || vecMeteo[ii].empty())
			continue;
		if (ts_buffer[ii].front().meta.getHash()!=vecMeteo[ii].front().meta.getHash()) {
			ostringstream ss;
			ss << "The stations changed over time from " << ts_buffer[ii].front().meta.getHash() << " to " << vecMeteo[ii].front().meta.getHash() << ", ";
			ss << "this is not handled yet!";
			throw IOException(ss.str(), AT);
		}
	}

	//now, do the append/merge
	for (size_t ii=0; ii<nrStationsBuffer; ii++) { //for all stations
		if (vecMeteo[ii].empty()) continue;

		if (ts_buffer[ii].empty()) {
			ts_buffer[ii] = vecMeteo[ii];
			continue;
		}

		const Date buffer_start = ts_buffer[ii].front().date;
		const Date buffer_end = ts_buffer[ii].back().date;
		const Date data_start = vecMeteo[ii].front().date;
		const Date data_end = vecMeteo[ii].back().date;

		if (data_start>buffer_end) { //the data simply fits to the end
			ts_buffer[ii].insert(ts_buffer[ii].end(), vecMeteo[ii].begin(), vecMeteo[ii].begin()+vecMeteo[ii].size());
			continue;
		}
		if (data_end<buffer_start) { //the data simply fits at the start
			ts_buffer[ii].insert(ts_buffer[ii].begin(), vecMeteo[ii].begin(), vecMeteo[ii].begin()+vecMeteo[ii].size());
			continue;
		}

		//there is some overlap, only copy data that does NOT overlap
		if (data_start<buffer_start) {
			const size_t pos = IOUtils::seek(buffer_start, vecMeteo[ii], false); //returns the first date >=
			ts_buffer[ii].insert(ts_buffer[ii].begin(), vecMeteo[ii].begin(), vecMeteo[ii].begin()+pos-1);
		}
		if (data_end>buffer_end) {
			size_t pos = IOUtils::seek(buffer_end, vecMeteo[ii], false); //returns the first date >=
			if (vecMeteo[ii][pos].date == buffer_end) pos++; //to make sure we have an element that is not already in buffer
			ts_buffer[ii].insert(ts_buffer[ii].end(), vecMeteo[ii].begin()+pos, vecMeteo[ii].begin()+vecMeteo[ii].size());
		}
	}

	ts_start = min(ts_start, date_start);
	ts_end = max(ts_end, date_end);
}

/*void MeteoBuffer::push(const Date& date, const METEO_SET& vecMeteo)
{

}*/

double MeteoBuffer::getAvgSamplingRate() const
{
	if (ts_buffer.empty())
		return IOUtils::nodata;

	const size_t nr_stations = ts_buffer.size();
	double sum = 0;
	for (size_t ii=0; ii<nr_stations; ii++){ //loop over all stations
		if(!ts_buffer[ii].empty()) {
			const std::vector<MeteoData>& curr_station = ts_buffer[ii];
			const double days = curr_station.back().date.getJulian() - curr_station.front().date.getJulian();

			//add the average sampling rate for this station
			const size_t nr_data_pts = ts_buffer[ii].size();
			if(days>0.) sum += (double)(nr_data_pts-1) / days; //the interval story: 2 points define 1 interval!
		}
	}
	if (sum > 0.){
		return ((double)sum / (double)(nr_stations*24*3600)); //in points per seconds, ie Hz
	}

	return IOUtils::nodata;
}

Date MeteoBuffer::getBufferStart() const
{
	return ts_start;
}

Date MeteoBuffer::getBufferEnd() const
{
	return ts_end;
}

std::vector< METEO_SET >& MeteoBuffer::getBuffer()
{
	return ts_buffer;
}

198
199
200
201
202
203
204
205
void MeteoBuffer::setBufferStart(const Date& date) {
	ts_start = date;
}

void MeteoBuffer::setBufferEnd(const Date& date) {
	ts_end = date;
}

206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
const std::string MeteoBuffer::toString() const
{
	ostringstream os;
	os << "<MeteoBuffer>\n";

	os << "Buffer content (" << ts_buffer.size() << " stations)\n";
	for(size_t ii=0; ii<ts_buffer.size(); ii++) {
		if (!ts_buffer[ii].empty()){
			os << std::setw(10) << ts_buffer[ii].front().meta.stationID << " = "
			   << ts_buffer[ii].front().date.toString(Date::ISO) << " - "
			   << ts_buffer[ii].back().date.toString(Date::ISO) << ", "
			   << ts_buffer[ii].size() << " timesteps" << endl;
		}
	}

	os << "</MeteoBuffer>\n";
	return os.str();
}

225
/********************************************************************************************/
226
/****************************** GridBuffer class ***********************************************/
227
228
229
/********************************************************************************************/

GridBuffer::GridBuffer(const size_t& in_max_grids) 
230
           : mapBufferedGrids(), mapBufferedDEMs(), mapBufferedInfos(), IndexBufferedGrids(), IndexBufferedDEMs(), max_grids(in_max_grids) 
231
232
{}

233
234
235
236
237
238
239
240
241
242
243
244
245
246
247

bool GridBuffer::get(Grid2DObject& grid, const std::string& grid_hash) const
{
	if (IndexBufferedGrids.empty())
		return false;

	const std::map<std::string, Grid2DObject>::const_iterator it = mapBufferedGrids.find( grid_hash );
	if (it != mapBufferedGrids.end()) { //already in map
		grid = (*it).second;
		return true;
	}

	return false;
}

248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
bool GridBuffer::get(Grid2DObject& grid, const std::string& grid_hash, std::string& grid_info) const
{
	grid_info.clear();
	if (max_grids==0) return false;
	
	if (get(grid, grid_hash)==true) {
		const std::map<std::string, std::string>::const_iterator it = mapBufferedInfos.find( grid_hash );
		if (it != mapBufferedInfos.end()) { //found in map
			grid_info = (*it).second;
		}
		return true;
	}
	
	return false;
}

264
265
266
267
268
269
bool GridBuffer::get(Grid2DObject& grid, const MeteoGrids::Parameters& parameter, const Date& date) const
{
	const string grid_hash = date.toString(Date::ISO)+"::"+MeteoGrids::getParameterName(parameter);
	return get(grid, grid_hash);
}

270
bool GridBuffer::get(DEMObject& grid, const std::string& grid_hash) const
271
{
272
273
274
275
276
277
	if (IndexBufferedDEMs.empty())
		return false;

	const std::map<std::string, DEMObject>::const_iterator it = mapBufferedDEMs.find( grid_hash );
	if (it != mapBufferedDEMs.end()) { //already in map
		//properties of the passed dem
278
279
280
		const DEMObject::update_type in_ppt = (DEMObject::update_type)grid.getUpdatePpt();
		const DEMObject::slope_type in_slope_alg = (DEMObject::slope_type)grid.getDefaultAlgorithm();

281
282
		//get cached dem and its properties
		grid = (*it).second;
283
284
285
286
		const DEMObject::update_type buff_ppt = (DEMObject::update_type)grid.getUpdatePpt();
		const DEMObject::slope_type buff_slope_alg = (DEMObject::slope_type)grid.getDefaultAlgorithm();

		if (in_ppt!=buff_ppt || in_slope_alg!=buff_slope_alg) {
287
			//if passed properties != cached properties -> set the returned one to the passed properties
288
289
290
291
292
293
294
			grid.setDefaultAlgorithm(in_slope_alg);
			grid.setUpdatePpt(in_ppt);
			grid.update();
		}

		return true;
	}
295

296
297
298
	return false;
}

299
void GridBuffer::push(const DEMObject& grid, const std::string& grid_hash)
300
301
{
	if (max_grids==0) return;
302
303
304
305
306
307
308
309
310
311

	if(IndexBufferedDEMs.size() >= max_grids) { //we need to remove the oldest grid
		const string tmp_hash = IndexBufferedDEMs.front();
		mapBufferedDEMs.erase( mapBufferedDEMs.find( tmp_hash ) );
		//swap followed by pop_back is faster than erase()
		swap( IndexBufferedDEMs.front(), IndexBufferedDEMs.back() );
		IndexBufferedDEMs.pop_back();
	}
	mapBufferedDEMs[ grid_hash ] = grid;
	IndexBufferedDEMs.push_back( grid_hash );
312
313
}

314
void GridBuffer::push(const Grid2DObject& grid, const std::string& grid_hash, const std::string& grid_info)
315
316
317
318
{
	if (max_grids==0) return;

	if(IndexBufferedGrids.size() >= max_grids) { //we need to remove the oldest grid
319
320
321
		const string tmp_hash = IndexBufferedGrids.front();
		mapBufferedGrids.erase( mapBufferedGrids.find( tmp_hash ) );
		mapBufferedInfos.erase( mapBufferedInfos.find( tmp_hash ) );
322
		//swap followed by pop_back is faster than erase()
323
324
		swap( IndexBufferedGrids.front(), IndexBufferedGrids.back() );
		IndexBufferedGrids.pop_back();
325
	}
326
327
328
329
330
331
332
333
334
335
	mapBufferedGrids[ grid_hash ] = grid;
	mapBufferedInfos[ grid_hash ] = grid_info;
	IndexBufferedGrids.push_back( grid_hash );
}

void GridBuffer::push(const Grid2DObject& grid, const std::string& grid_hash)
{
	if (max_grids==0) return;
	
	push(grid, grid_hash, "");
336
337
}

338
void GridBuffer::push(const Grid2DObject& grid, const MeteoGrids::Parameters& parameter, const Date& date)
339
{
340
341
	if (max_grids==0) return;
	
342
	const string grid_hash = date.toString(Date::ISO)+"::"+MeteoGrids::getParameterName(parameter);
343
	push(grid, grid_hash, "");
344
345
346
347
348
349
350
351
352
}

const std::string GridBuffer::toString() const
{
	ostringstream os;
	os << "<GridBuffer>\n";
	os << "Max buffered grids = " << max_grids << "\n";

	//cache content
353
354
355
356
	os << "Cached grids: " << mapBufferedGrids.size() << "\n";
	std::map<std::string, Grid2DObject>::const_iterator it_grid;
	for (it_grid=mapBufferedGrids.begin(); it_grid != mapBufferedGrids.end(); ++it_grid){
		os << setw(10) << "Grid " << it_grid->first << "\n";
357
358
359
	}
	
	//dem buffer
360
361
362
363
	os << "Cached dems: " << mapBufferedDEMs.size() << "\n";
	std::map<std::string, DEMObject>::const_iterator it_dem;
	for (it_dem=mapBufferedDEMs.begin(); it_dem != mapBufferedDEMs.end(); ++it_dem){
		os << setw(10) << "Dem " << it_dem->first << "\n";
364
365
366
367
368
369
370
	}

	os << "</GridBuffer>\n";
	return os.str();
}


371
} //end namespace