WSL/SLF GitLab Repository

IOManager.cc 8.83 KB
Newer Older
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
/***********************************************************************************/
/*  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/>.
*/
18

19
20
21
22
23
24
#include <meteoio/IOManager.h>

using namespace std;

namespace mio {

25
26
IOManager::IOManager(const std::string& filename_in) : cfg(filename_in), iohandler(cfg),
                                                       tsmanager(iohandler, cfg), gridsmanager(iohandler, cfg), interpolator(cfg, tsmanager, gridsmanager),
27
                                                       vstations_refresh_rate(IOUtils::unodata), downscaling(false), virtual_stations(false)
28
29
30
31
{
	initIOManager();
}

32
33
IOManager::IOManager(const Config& i_cfg) : cfg(i_cfg), iohandler(cfg),
                                            tsmanager(iohandler, cfg), gridsmanager(iohandler, cfg), interpolator(cfg, tsmanager, gridsmanager),
34
                                            vstations_refresh_rate(IOUtils::unodata), downscaling(false), virtual_stations(false)
35
36
37
38
39
{
	initIOManager();
}

void IOManager::initIOManager()
40
{
41
	cfg.getValue("Virtual_stations", "Input", virtual_stations, IOUtils::nothrow);
42
	cfg.getValue("Downscaling", "Input", downscaling, IOUtils::nothrow);
43
44
45
46
47
	if (virtual_stations || downscaling) { //in this case, we do not want to re-apply the filters
		tsmanager.setProcessingLevel(IOUtils::resampled | IOUtils::generated);
		gridsmanager.setProcessingLevel(IOUtils::resampled | IOUtils::generated);
		cfg.getValue("VSTATIONS_REFRESH_RATE", "Input", vstations_refresh_rate, IOUtils::nothrow);
	}
48
49
50
51
}

void IOManager::setProcessingLevel(const unsigned int& i_level)
{
52
53
54
55
	if (!virtual_stations && !downscaling) {
		tsmanager.setProcessingLevel(i_level);
		gridsmanager.setProcessingLevel(i_level);
	}
56
57
}

58
59
60
void IOManager::setMinBufferRequirements(const double& buffer_size, const double& buff_before)
{
	tsmanager.setMinBufferRequirements(buffer_size, buff_before);
61
62
}

63
double IOManager::getAvgSamplingRate() const
64
{
65
	return tsmanager.getAvgSamplingRate();
66
67
}

68
void IOManager::push_meteo_data(const IOUtils::ProcessingLevel& level, const Date& date_start, const Date& date_end,
69
                                const std::vector< METEO_SET >& vecMeteo)
70
{
71
	tsmanager.push_meteo_data(level, date_start, date_end, vecMeteo);
72
73
}

74
size_t IOManager::getStationData(const Date& date, STATIONS_SET& vecStation)
75
{
76
77
78
79
80
81
82
	vecStation.clear();

	if (virtual_stations || downscaling) {
		return interpolator.getVirtualStationsMeta(date, vecStation);
	} else { //usual case
		return tsmanager.getStationData(date, vecStation);
	}
83
84
85
}

//for an interval of data: decide whether data should be filtered or raw
86
size_t IOManager::getMeteoData(const Date& dateStart, const Date& dateEnd, std::vector< METEO_SET >& vecVecMeteo)
87
{
88
	return tsmanager.getMeteoData(dateStart, dateEnd, vecVecMeteo); //equivalent with the number of stations that have data
89
90
}

91
92
void IOManager::clear_cache()
{
93
94
	tsmanager.clear_cache();
	gridsmanager.clear_cache();
95
96
}

97
98
99
100
101
void IOManager::add_to_points_cache(const Date& i_date, const METEO_SET& vecMeteo)
{
	tsmanager.add_to_points_cache(i_date, vecMeteo);
}

102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
//This is small helper method to call the spatial interpolations when dealing with virtual stations or downsampling
void IOManager::load_virtual_meteo(const Date& i_date, METEO_SET& vecMeteo)
{
	const double half_range = (vstations_refresh_rate)/(3600.*24.*2.);
	const Date range_start = i_date - half_range;
	const Date range_end = i_date + half_range;

	if (virtual_stations)
		interpolator.getVirtualMeteoData(Meteo2DInterpolator::VSTATIONS, i_date, vecMeteo);
	if (downscaling)
		interpolator.getVirtualMeteoData(Meteo2DInterpolator::SMART_DOWNSCALING, i_date, vecMeteo);

	tsmanager.push_meteo_data(IOUtils::raw, range_start, range_end, vecMeteo);
}

117
118
119
120
121
//data can be raw or processed (filtered, resampled)
size_t IOManager::getMeteoData(const Date& i_date, METEO_SET& vecMeteo)
{
	vecMeteo.clear();

122
	if (!virtual_stations && !downscaling) { //this is the usual case
123
		tsmanager.getMeteoData(i_date, vecMeteo);
124
	} else {
125
126
127
128
129
130
131
132
133
134
135
		//find the nearest sampling points (vstations_refresh_rate apart) around the requested point
		const Date i_date_down( Date::rnd(i_date, vstations_refresh_rate, Date::DOWN) );
		const Date i_date_up( Date::rnd(i_date, vstations_refresh_rate, Date::UP) );
		const Date buff_start( tsmanager.getRawBufferStart() );
		const Date buff_end( tsmanager.getRawBufferEnd() );

		if (buff_start.isUndef() || i_date_down<buff_start || i_date_down>buff_end)
			load_virtual_meteo(i_date_down, vecMeteo);

		if (buff_start.isUndef() || i_date_up<buff_start || i_date_up>buff_end)
			load_virtual_meteo(i_date_up, vecMeteo);
136
137

		tsmanager.getMeteoData(i_date, vecMeteo);
138
	}
139

140
141
	return vecMeteo.size();
}
142

143
void IOManager::writeMeteoData(const std::vector< METEO_SET >& vecMeteo, const std::string& name)
144
{
145
	tsmanager.writeMeteoData(vecMeteo, name);
146
147
}

148
149
150
bool IOManager::getMeteoData(const Date& date, const DEMObject& dem, const MeteoData::Parameters& meteoparam,
                  Grid2DObject& result)
{
151
	std::string info_string;
152
	const bool status = getMeteoData(date, dem, meteoparam, result, info_string);
153
154
	cerr << "[i] Interpolating " << MeteoData::getParameterName(meteoparam);
	cerr << " (" << info_string << ") " << endl;
155
	return status;
156
157
158
159
160
161
}

bool IOManager::getMeteoData(const Date& date, const DEMObject& dem, const MeteoData::Parameters& meteoparam,
                  Grid2DObject& result, std::string& info_string)
{
	interpolator.interpolate(date, dem, meteoparam, result, info_string);
162
	return (!result.empty());
163
164
}

165
166
void IOManager::interpolate(const Date& date, const DEMObject& dem, const MeteoData::Parameters& meteoparam,
                            const std::vector<Coords>& in_coords, std::vector<double>& result)
167
168
169
{
	string info_string;
	interpolate(date, dem, meteoparam, in_coords, result, info_string);
170
171
	cerr << "[i] Interpolating " << MeteoData::getParameterName(meteoparam);
	cerr << " (" << info_string << ") " << endl;
172
173
174
175
}

void IOManager::interpolate(const Date& date, const DEMObject& dem, const MeteoData::Parameters& meteoparam,
                            const std::vector<Coords>& in_coords, std::vector<double>& result, std::string& info_string)
176
{
177
	interpolator.interpolate(date, dem, meteoparam, in_coords, result, info_string);
178
179
}

180
181
void IOManager::read2DGrid(Grid2DObject& grid2D, const std::string& filename)
{
182
	gridsmanager.read2DGrid(grid2D, filename);
183
184
}

185
186
void IOManager::read2DGrid(Grid2DObject& grid2D, const MeteoGrids::Parameters& parameter, const Date& date)
{
187
	gridsmanager.read2DGrid(grid2D, parameter, date);
188
189
}

190
191
192
193
194
195
196
197
198
199
void IOManager::read3DGrid(Grid3DObject& grid3D, const std::string& filename)
{
	gridsmanager.read3DGrid(grid3D, filename);
}

void IOManager::read3DGrid(Grid3DObject& grid3D, const MeteoGrids::Parameters& parameter, const Date& date)
{
	gridsmanager.read3DGrid(grid3D, parameter, date);
}

200
201
void IOManager::readDEM(DEMObject& grid2D)
{
202
	gridsmanager.readDEM(grid2D);
203
204
205
206
}

void IOManager::readLanduse(Grid2DObject& grid2D)
{
207
	gridsmanager.readLanduse(grid2D);
208
209
210
211
}

void IOManager::readAssimilationData(const Date& date, Grid2DObject& grid2D)
{
212
	gridsmanager.readAssimilationData(date, grid2D);
213
214
}

215
void IOManager::readPOI(std::vector<Coords>& cpa)
216
{
217
	iohandler.readPOI(cpa);
218
219
220
221
}

void IOManager::write2DGrid(const Grid2DObject& grid2D, const std::string& name)
{
222
	gridsmanager.write2DGrid(grid2D, name);
223
224
}

225
226
void IOManager::write2DGrid(const Grid2DObject& grid2D, const MeteoGrids::Parameters& parameter, const Date& date)
{
227
	gridsmanager.write2DGrid(grid2D, parameter, date);
228
229
}

230
231
232
233
234
235
236
237
238
239
void IOManager::write3DGrid(const Grid3DObject& grid3D, const std::string& name)
{
	gridsmanager.write3DGrid(grid3D, name);
}

void IOManager::write3DGrid(const Grid3DObject& grid3D, const MeteoGrids::Parameters& parameter, const Date& date)
{
	gridsmanager.write3DGrid(grid3D, parameter, date);
}

240
const std::string IOManager::toString() const {
241
	ostringstream os;
242
	os << "<IOManager>\n";
243
	os << "Config cfg = " << hex << &cfg << dec << "\n";
244
245
246
	os << iohandler.toString();
	os << tsmanager.toString();
	os << gridsmanager.toString();
247
	os << interpolator.toString();
248
249
	os << "Downscaling = " << downscaling << "\n";
	os << "Virtual stations = " << virtual_stations << "\n";
250
	os << "</IOManager>\n";
251
252
253
	return os.str();
}

254
} //namespace