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
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
/***********************************************************************************/
/*  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/>.
*/
#ifdef _POPC_
	#include <meteoio/IOHandler.ph>
	#ifdef DEBUG_ARITHM
		#ifndef _GNU_SOURCE
			#define _GNU_SOURCE
		#endif
		#include <fenv.h>
	#endif
#else
	#include <meteoio/IOHandler.h>
#endif

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)
41
 * - \subpage cosmoxml "COSMO" for treading XML files produced by MeteoSwiss COSMO's postprocessing (requires libxml)
42
43
44
45
46
47
48
49
 * - \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)
50
 * - \subpage smetio "SMETIO" for reading SMET meteo data files (no extra requirements)
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
 *
 */

void IOHandler::registerPlugins()
{
#if defined(WIN32)
	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
68
	//mapPlugins[io.ini KEY]= IOPlugin(library file name, class name, NULL, NULL);
69
70
71
72
73
	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);
74
75
76
77
78
79
	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);
80
	mapPlugins["COSMOXML"]      = IOPlugin("libcosmoxmlio"+popc_extra+libsuffix, "CosmoXMLIO", NULL, NULL);
81
82
83
84
85
86
87
88
89
90
91
92
93
94
}

#ifdef _POPC_
IOHandler::IOHandler(const std::string& configfile) :  cfg(configfile), fileio(configfile){
#else
IOHandler::IOHandler(const std::string& configfile) : IOInterface(NULL), cfg(configfile), fileio(configfile)
{
#endif
	registerPlugins();
}

//Copy constructor
#ifdef _POPC_
//IOHandler::IOHandler(const IOHandler& aio) : cfg(aio.cfg), fileio(aio.cfg), bormaio(aio.cfg), imisio(aio.cfg){
95
	//Nothing else so far //HACK for POPC
96
97
98
99
100
101
102
103
104
105
//}
#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_
106
IOHandler::IOHandler(const Config& cfgreader) : cfg(cfgreader), fileio(cfgreader)
107
#else
108
IOHandler::IOHandler(const Config& cfgreader) : IOInterface(NULL), cfg(cfgreader), fileio(cfgreader)
109
110
111
112
113
114
115
116
117
118
119
120
121
122
#endif
{
#if defined(_POPC_) && defined(DEBUG_ARITHM)
	feenableexcept(FE_DIVBYZERO | FE_INVALID | FE_OVERFLOW );
#endif
	registerPlugins();
}

#ifdef _POPC_
IOHandler::~IOHandler(){
#else
IOHandler::~IOHandler() throw(){
#endif
	// Get rid of the objects
123
	std::map<std::string, IOPlugin>::iterator mapit;
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
	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::cout << "[i] " << AT << ": Loading dynamic plugin: " << libname << std::endl;
	std::string pluginpath = "";

	try {
153
		cfg.getValue("PLUGINPATH", pluginpath, Config::nothrow);
154
155
156
157
158
159
160
161
162
163
164
		if (pluginpath != "")
			pluginpath += "/";

		//Which dynamic library needs to be loaded
		std::cout << "\t" << "Trying to load " << libname << " ... ";
		std::string filename = pluginpath + libname;
		dynLibrary = DynamicLoader::loadObjectFile(filename);

		if(dynLibrary == NULL) {
			std::cout << "failed\n\tCouldn't load the dynamic library " << filename << "\n\t" << DynamicLoader::getErrorMessage() << std::endl;
		} else {
165
			io = dynamic_cast<IOInterface*>((dynLibrary)->newObject(classname, cfg));
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185

			if(io == NULL) {
				std::cout << "failed" << std::endl;
				//delete dynLibrary; This causes a segfault !!
			} else {
				std::cout << "success" << std::endl;
			}
		}
	} catch (std::exception& e) {
		if (dynLibrary != NULL)
			delete dynLibrary;
		std::cerr << "\t" << e.what() << std::endl;
	}
}

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

186
	std::map<std::string, IOPlugin>::iterator mapit = mapPlugins.find(op_src);
187
	if (mapit == mapPlugins.end())
188
		throw IOException("Can not find plugin " + op_src + " as requested in file " + cfg.getSourceName() + ". Has its developer declared it in IOHandler::registerPlugins?", AT);
189
190
191
192
193
194
195
196
197
198
199
200

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

	if ((mapit->second).io == NULL) {
		throw IOException("Requesting to read/write data with plugin for " + cfgkey + ", but plugin is not loaded", AT);
	}

	return (mapit->second).io;
}

201
void IOHandler::read2DGrid(Grid2DObject& out_grid, const std::string& _filename)
202
203
{
	IOInterface *plugin = getPlugin("GRID2D", "Input");
204
	plugin->read2DGrid(out_grid, _filename);
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
}

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

220
void IOHandler::readStationData(const Date& date, STATION_TIMESERIE& vecStation)
221
222
223
224
225
{
	IOInterface *plugin = getPlugin("METEO", "Input");
	plugin->readStationData(date, vecStation);
}

226
void IOHandler::readMeteoData(const Date& date, METEO_TIMESERIE& vecMeteo)
227
228
229
230
{
	vecMeteo.clear();

	std::vector< std::vector<MeteoData> > meteoTmpBuffer;
231
	readMeteoData(date, date, meteoTmpBuffer);
232
233
234

	unsigned int emptycounter = 0;
	for (unsigned int ii=0; ii<meteoTmpBuffer.size(); ii++){//stations
235
		if (meteoTmpBuffer[ii].size() > 0){
236
237
238
239
240
241
242
243
244
245
246
247
			vecMeteo.push_back(meteoTmpBuffer[ii][0]);
		} else {
			emptycounter++;
		}
	}

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

void IOHandler::readMeteoData(const Date& dateStart, const Date& dateEnd,
248
                              std::vector<METEO_TIMESERIE>& vecMeteo,
249
                              const unsigned& stationindex)
250
251
{
	IOInterface *plugin = getPlugin("METEO", "Input");
252
	plugin->readMeteoData(dateStart, dateEnd, vecMeteo, stationindex);
253
254
}
#ifdef _POPC_
255
void IOHandler::writeMeteoData(std::vector<METEO_TIMESERIE>& vecMeteo,
256
257
                               const std::string& name)
#else
258
void IOHandler::writeMeteoData(const std::vector<METEO_TIMESERIE>& vecMeteo,
259
260
261
262
                               const std::string& name)
#endif
{
	IOInterface *plugin = getPlugin("METEO", "Output");
263
	plugin->writeMeteoData(vecMeteo, name);
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
}

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

283
#ifndef _POPC_
284
285
std::ostream& operator<<(std::ostream& os, const IOHandler& data)
{
286
287
288
289
290
291
292
293
294
295
296
297
	os << data.toString();
	return os;
}
#endif

#ifdef _POPC_
std::string IOHandler::toString()
#else
std::string IOHandler::toString() const
#endif
{
	std::stringstream os;
298
	os << "<IOHandler>\n";
299
	os << "Config cfg = " << hex << &cfg << dec << "\n";
300
301
302

	os << "<mapPlugins>\n";
	os << setw(10) << "Keyword" << " = " << IOPlugin::header << "\n";
303
	std::map<std::string, IOPlugin>::const_iterator it1;
304
	for (it1=mapPlugins.begin(); it1 != mapPlugins.end(); it1++){
305
306
307
308
		os << setw(10) << it1->first << " = " <<  it1->second;
	}
	os << "</mapPlugins>\n";
	os << "</IOHandler>\n";
309
	return os.str();
310
311
}

312
} //end namespace