WSL/SLF GitLab Repository

IOHandler.cc 11 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

#include <meteoio/IOHandler.h>
20
21
22
23
24
25
26
27
28
29
30
31

using namespace std;

namespace mio {
 /**
 * @page plugins Plugins overview
 * The data access is handled by a system of plugins. They all offer the same interface, meaning that a plugin can transparently be replaced by another one. Since they might rely on third party libraries for accessing the data, they have been created as plugins, that is they are loaded on demand (and also compiled only if requested at compile time). A plugin can therefore fail to load (for example if it does not exist) at run time.
 *
 * @section available_plugins Available plugins
 * So far the following children have been implemented (by keyword for the io.ini key/value config file). Please read the documentation for each plugin in order to know the plugin-specific keywords:
 * - \subpage a3d "A3D" for reading original Alpine3D meteo files (no extra requirements)
 * - \subpage borma "BORMA" for reading Borma xml meteo files (requires libxml)
32
 * - \subpage cosmoxml "COSMO" for treading XML files produced by MeteoSwiss COSMO's postprocessing (requires libxml)
33
34
35
36
37
38
39
40
 * - \subpage imis "IMIS" for reading meteo data out of the IMIS database (requires Oracle's OCCI library)
 * - \subpage geotop "GEOTOP" for reading original GeoTop meteo files (no extra requirements)
 * - \subpage snowpack "SNOWPACK" for reading original SNOWPACK meteo files (no extra requirements)
 * - \subpage gsn "GSN" for reading meteo data out of the Global Sensor Network web service interface (requires GSoap)
 * - \subpage arc "ARC" for reading ESRI/ARC DEM files (no extra requirements)
 * - \subpage grass "GRASS" for reading Grass DEM files (no extra requirements)
 * - \subpage arps "ARPSIO" for reading ARPS formatted DEM (no extra requirements)
 * - \subpage pgmio "PGMIO" for reading PGM grid files (no extra requirements)
41
 * - \subpage smetio "SMETIO" for reading SMET meteo data files (no extra requirements)
42
43
44
45
46
 *
 */

void IOHandler::registerPlugins()
{
47
#if defined(_WIN32)
48
49
50
51
52
53
54
55
56
57
58
	const std::string libsuffix = ".dll";
#elif defined(APPLE)
	const std::string libsuffix = ".dylib";
#else
	const std::string libsuffix = ".so";
#endif
#ifdef _POPC_
	const std::string popc_extra = "popc";
#else
	const std::string popc_extra = "";
#endif
59
	//mapPlugins[io.ini KEY]= IOPlugin(library file name, class name, NULL, NULL);
60
61
62
63
64
	mapPlugins["A3D"]       = IOPlugin("", "A3DIO", &fileio, NULL);
	mapPlugins["BORMA"]     = IOPlugin("libbormaio"+popc_extra+libsuffix, "BormaIO", NULL, NULL);
	mapPlugins["IMIS"]      = IOPlugin("libimisio"+popc_extra+libsuffix, "ImisIO", NULL, NULL);
	mapPlugins["GEOTOP"]    = IOPlugin("libgeotopio"+popc_extra+libsuffix, "GeotopIO", NULL, NULL);
	mapPlugins["SNOWPACK"]  = IOPlugin("libsnio"+popc_extra+libsuffix, "SNIO", NULL, NULL);
65
66
67
68
69
70
	mapPlugins["GSN"]       = IOPlugin("libgsnio"+popc_extra+libsuffix, "GSNIO", NULL, NULL);
	mapPlugins["ARC"]       = IOPlugin("libarcio"+popc_extra+libsuffix, "ARCIO", NULL, NULL);
	mapPlugins["GRASS"]     = IOPlugin("libgrassio"+popc_extra+libsuffix, "GrassIO", NULL, NULL);
	mapPlugins["ARPS"]      = IOPlugin("libarpsio"+popc_extra+libsuffix, "ARPSIO", NULL, NULL);
	mapPlugins["PGM"]       = IOPlugin("libpgmio"+popc_extra+libsuffix, "PGMIO", NULL, NULL);
	mapPlugins["SMET"]      = IOPlugin("libsmetio"+popc_extra+libsuffix, "SMETIO", NULL, NULL);
71
	mapPlugins["COSMOXML"]      = IOPlugin("libcosmoxmlio"+popc_extra+libsuffix, "CosmoXMLIO", NULL, NULL);
72
73
74
}

#ifdef _POPC_
75
IOHandler::IOHandler(const std::string& configfile) :  cfg(configfile), fileio(configfile){
76
#else
77
IOHandler::IOHandler(const std::string& configfile) : IOInterface(NULL), cfg(configfile), fileio(configfile){
78
#endif
79
80
81
82
83
84
	registerPlugins();
}

//Copy constructor
#ifdef _POPC_
//IOHandler::IOHandler(const IOHandler& aio) : cfg(aio.cfg), fileio(aio.cfg), bormaio(aio.cfg), imisio(aio.cfg){
85
	//Nothing else so far //HACK for POPC
86
87
88
89
90
91
92
93
94
95
//}
#else
IOHandler::IOHandler(const IOHandler& aio) : IOInterface(NULL), cfg(aio.cfg), fileio(aio.cfg)
{
	//Nothing else so far
	//TODO: Deal with the IOInterface* pointers, e.g. bormaio
}
#endif

#ifdef _POPC_
96
IOHandler::IOHandler(const Config& cfgreader) : cfg(cfgreader), fileio(cfgreader)
97
#else
98
IOHandler::IOHandler(const Config& cfgreader) : IOInterface(NULL), cfg(cfgreader), fileio(cfgreader)
99
100
101
102
103
104
#endif
{
	registerPlugins();
}

#ifdef _POPC_
105
IOHandler::~IOHandler(){
106
#else
107
IOHandler::~IOHandler() throw(){
108
109
#endif
	// Get rid of the objects
110
	std::map<std::string, IOPlugin>::iterator mapit;
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
	for (mapit = mapPlugins.begin(); mapit!=mapPlugins.end(); mapit++){
		deletePlugin((mapit->second).dynLibrary, (mapit->second).io);
	}
}
#ifdef _POPC_
void IOHandler::deletePlugin(DynamicLibrary*& dynLibrary, IOInterface*& io)
#else
void IOHandler::deletePlugin(DynamicLibrary*& dynLibrary, IOInterface*& io) throw()
#endif
{
	if (dynLibrary != NULL) {
		if (io != NULL) {
			io->deleteSelf();
			io = NULL;
		}

		// Close the dynamic library
#ifndef _POPC_ //HACK: this line causes a segfault in the parallel version for unknown reasons, lwk
		delete dynLibrary;
#endif
	}
}

void IOHandler::loadPlugin(const std::string& libname, const std::string& classname, DynamicLibrary*& dynLibrary, IOInterface*& io)
{
	std::string pluginpath = "";

	try {
139
		cfg.getValue("PLUGINPATH", pluginpath, Config::nothrow);
140
141
142
143
144
145
146
147
		if (pluginpath != "")
			pluginpath += "/";

		//Which dynamic library needs to be loaded
		std::string filename = pluginpath + libname;
		dynLibrary = DynamicLoader::loadObjectFile(filename);

		if(dynLibrary == NULL) {
148
149
150
			std::cout << AT << ": [E] Failed loading dynamic plugin " << classname << " from " << filename << std::endl;
			std::cout << "\t" << DynamicLoader::getErrorMessage() << std::endl;
			std::cout << "Please check your PLUGINPATH in your configuration file!" << std::endl;
151
		} else {
152
			io = dynamic_cast<IOInterface*>((dynLibrary)->newObject(classname, cfg));
153
154

			if(io == NULL) {
155
				std::cout << AT << ": [E] Failed loading dynamic plugin " << classname << " from " << filename << "(NULL pointer to plugin's class)" << std::endl;
156
157
				//delete dynLibrary; This causes a segfault !!
			} else {
158
				std::cout << "[i] Success loading dynamic plugin " << classname << " from " << filename << std::endl;
159
160
			}
		}
161
	} catch (const std::exception& e) {
162
	#ifndef _POPC_ //HACK: this line causes a segfault in the parallel version for unknown reasons, lwk
163
164
		if (dynLibrary != NULL)
			delete dynLibrary;
165
	#endif
166
		std::cerr << AT << ": [E] failed while loading plugin - " << e.what() << std::endl;
167
168
169
170
171
172
173
174
	}
}

IOInterface* IOHandler::getPlugin(const std::string& cfgkey, const std::string& cfgsection)
{
	std::string op_src="";
	cfg.getValue(cfgkey, cfgsection, op_src);

175
	std::map<std::string, IOPlugin>::iterator mapit = mapPlugins.find(op_src);
176
	if (mapit == mapPlugins.end())
177
		throw IOException("Can not find plugin " + op_src + " as requested in file " + cfg.getSourceName() + ". Has its developer declared it in IOHandler::registerPlugins?", AT);
178
179
180
181
182
183

	if ((mapit->second).io == NULL){
		loadPlugin((mapit->second).libname, (mapit->second).classname, (mapit->second).dynLibrary, (mapit->second).io);
	}

	if ((mapit->second).io == NULL) {
184
		throw IOException("Requesting to read/write data with plugin '" + op_src + "', but plugin is not loaded", AT);
185
186
187
188
189
	}

	return (mapit->second).io;
}

190
void IOHandler::read2DGrid(Grid2DObject& out_grid, const std::string& i_filename)
191
192
{
	IOInterface *plugin = getPlugin("GRID2D", "Input");
193
	plugin->read2DGrid(out_grid, i_filename);
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
}

void IOHandler::readDEM(DEMObject& dem_out)
{
	IOInterface *plugin = getPlugin("DEM", "Input");
	plugin->readDEM(dem_out);
	dem_out.update();
}

void IOHandler::readLanduse(Grid2DObject& landuse_out)
{
	IOInterface *plugin = getPlugin("LANDUSE", "Input");
	plugin->readLanduse(landuse_out);
}

209
void IOHandler::readStationData(const Date& date, STATION_TIMESERIE& vecStation)
210
211
212
213
214
{
	IOInterface *plugin = getPlugin("METEO", "Input");
	plugin->readStationData(date, vecStation);
}

215
void IOHandler::readMeteoData(const Date& date, METEO_TIMESERIE& vecMeteo)
216
217
218
219
{
	vecMeteo.clear();

	std::vector< std::vector<MeteoData> > meteoTmpBuffer;
220
	readMeteoData(date, date, meteoTmpBuffer);
221
222
223

	unsigned int emptycounter = 0;
	for (unsigned int ii=0; ii<meteoTmpBuffer.size(); ii++){//stations
224
		if (meteoTmpBuffer[ii].size() > 0){
225
226
227
228
229
230
231
232
233
234
235
236
			vecMeteo.push_back(meteoTmpBuffer[ii][0]);
		} else {
			emptycounter++;
		}
	}

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

void IOHandler::readMeteoData(const Date& dateStart, const Date& dateEnd,
237
                              std::vector<METEO_TIMESERIE>& vecMeteo,
238
                              const size_t& stationindex)
239
240
{
	IOInterface *plugin = getPlugin("METEO", "Input");
241
	plugin->readMeteoData(dateStart, dateEnd, vecMeteo, stationindex);
242
}
243
244
245
246
#ifdef _POPC_
void IOHandler::writeMeteoData(std::vector<METEO_TIMESERIE>& vecMeteo,
                               const std::string& name)
#else
247
void IOHandler::writeMeteoData(const std::vector<METEO_TIMESERIE>& vecMeteo,
248
                               const std::string& name)
249
#endif
250
251
{
	IOInterface *plugin = getPlugin("METEO", "Output");
252
	plugin->writeMeteoData(vecMeteo, name);
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
}

void IOHandler::readAssimilationData(const Date& date_in, Grid2DObject& da_out)
{
	IOInterface *plugin = getPlugin("DA", "Input");
	plugin->readAssimilationData(date_in, da_out);
}

void IOHandler::readSpecialPoints(std::vector<Coords>& pts) {
	IOInterface *plugin = getPlugin("SPECIALPTS", "Input");
	plugin->readSpecialPoints(pts);
}

void IOHandler::write2DGrid(const Grid2DObject& grid_in, const std::string& name)
{
	IOInterface *plugin = getPlugin("GRID2D", "Output");
	plugin->write2DGrid(grid_in, name);
}

272
#ifndef _POPC_
273
274
std::ostream& operator<<(std::ostream& os, const IOHandler& data)
{
275
276
277
278
279
280
281
282
283
284
285
286
	os << data.toString();
	return os;
}
#endif

#ifdef _POPC_
std::string IOHandler::toString()
#else
std::string IOHandler::toString() const
#endif
{
	std::stringstream os;
287
	os << "<IOHandler>\n";
288
	os << "Config cfg = " << hex << &cfg << dec << "\n";
289
290
291

	os << "<mapPlugins>\n";
	os << setw(10) << "Keyword" << " = " << IOPlugin::header << "\n";
292
	std::map<std::string, IOPlugin>::const_iterator it1;
293
	for (it1=mapPlugins.begin(); it1 != mapPlugins.end(); it1++){
294
295
296
297
		os << setw(10) << it1->first << " = " <<  it1->second;
	}
	os << "</mapPlugins>\n";
	os << "</IOHandler>\n";
298
	return os.str();
299
300
}

301
} //end namespace