WSL/SLF GitLab Repository

GSNIO.cc 15.3 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
/***********************************************************************************/
/*  Copyright 2009 EPFL                                                            */
/***********************************************************************************/
/* 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 "GSNIO.h"

using namespace std;

namespace mio {
/**
 * @page gsn GSN
 * @section gsn_format Format
 * This plugin reads meteorological data from GSN (Global Sensor Network, see <a href="http://sourceforge.net/apps/trac/gsn/"> GSN home page</a>) as a web service. It therefore requires GSoap.
 * @subsection gsn_fields Field mapping
 * The following GSN fields are read from GSN and mapped to MeteoData attributes:
 * - TIMED mapped to date
 * - LIGHT mapped to iswr
 * - TEMPERATURE or AIR_TEMP mapped to ta
 * - WIND_SPEED mapped to wv
 * - SOLAR_RAD mapped to iswr
 * - currently, nothing feeds ilwr //HACK: THIS IS A BUG!!
 * - AIR_HUMID mapped to rh
 * - SOIL_TEMP_ECTM mapped to tss
 * - GROUND_TEMP_TNX mapped to tsg
 * - RAIN_METER mapped to hnw
 *
 * @section gsn_units Units
 * The units are assumed to be the following:
 * - temperatures in celsius
 * - relative humidity in %
 * - wind speed in m/s
 * - precipitations in mm/h
 * - radiation in W/m²
47
 * - time is provided as a Unix timestamp, which is always in UTC
48
49
50
51
52
53
54
 *
 * @section gsn_keywords Keywords
 * This plugin uses the following keywords:
 * - COORDSYS: input coordinate system (see Coords) specified in the [Input] section
 * - COORDPARAM: extra input coordinates parameters (see Coords) specified in the [Input] section
 * - COORDSYS: output coordinate system (see Coords) specified in the [Output] section
 * - COORDPARAM: extra output coordinates parameters (see Coords) specified in the [Output] section
55
 * - ENDPOINT: The URL of the web service e.g. http://192.33.210.10:22201/services/A3DWebService/
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
 * - PROXY: an IP address or a resolveable hostname
 * - PROXYPORT: the port the proxy is listening on
 * - PROXYUSER: (if necessary) a proxy username
 * - PROXYPASS: (if necessary) a proxy password
 *
 * @section license Licensing
 * This software 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.
 *
 * Part of the software embedded in this product is gSOAP software.
 * Portions created by gSOAP are Copyright (C) 2001-2009 Robert A. van Engelen, Genivia inc. All Rights Reserved.
 * THE SOFTWARE IN THIS PRODUCT WAS IN PART PROVIDED BY GENIVIA INC AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT  NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
 */

const double GSNIO::plugin_nodata = -999.0; //plugin specific nodata value

71
GSNIO::GSNIO(void (*delObj)(void*), const Config& i_cfg) : IOInterface(delObj), cfg(i_cfg)
72
{
73
74
75
76
77
78
79
80
81
82
	IOUtils::getProjectionParameters(cfg, coordin, coordinparam, coordout, coordoutparam);
	initGSNConnection();
}

GSNIO::GSNIO(const std::string& configfile) : IOInterface(NULL), cfg(configfile)
{
	IOUtils::getProjectionParameters(cfg, coordin, coordinparam, coordout, coordoutparam);
	initGSNConnection();
}

83
GSNIO::GSNIO(const Config& cfgreader) : IOInterface(NULL), cfg(cfgreader)
84
85
86
87
88
89
90
91
{
	IOUtils::getProjectionParameters(cfg, coordin, coordinparam, coordout, coordoutparam);
	initGSNConnection();
}

GSNIO::~GSNIO() throw(){}

void GSNIO::initGSNConnection(){
92
	endpoint = hostname = port = userid = passwd = "";
93
94
95
96
	proxyport = -1;

	//soap_init(&gsn);
	//soap_init2(&gsn, SOAP_IO_KEEPALIVE, SOAP_IO_KEEPALIVE);
97
	
98
	cfg.getValue("ENDPOINT", "INPUT", endpoint, Config::nothrow);
99
100
101
102
	if (endpoint != ""){
		gsn.soap_endpoint = endpoint.c_str();
		cout << "\tUsing GSN Endpoint: " << endpoint << endl;
	}
103
104
105
106
107
108
109
	/*
	 * Trying to read proxy settings:
	 * - Firstly the hostname and port (both have to be provided).
	 * - If this succeeds then the username and password will be read
	 * - parameters not set will be set to ""
	 */
	try {
110
		cfg.getValue("PROXY", "INPUT", hostname, Config::nothrow);
111
		if (hostname == "") return;
112
		cfg.getValue("PROXYPORT", "INPUT", port, Config::nothrow);
113
114
115
116
117
118
119
120
121
122
		if (port == "") return;

		if (!IOUtils::convertString(proxyport, port, std::dec))
			throw ConversionFailedException("", AT);
		if (proxyport < 1)
			throw IOException("",AT);

		gsn.proxy_host = hostname.c_str();
		gsn.proxy_port = proxyport;

123
		cfg.getValue("PROXYUSER", "INPUT", userid);
124
		gsn.proxy_userid = userid.c_str();
125
		cfg.getValue("PROXYPASS", "INPUT", passwd);
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
		gsn.proxy_passwd = passwd.c_str();
	} catch(...){}
}

void GSNIO::read2DGrid(Grid2DObject&, const std::string& filename)
{
	//Nothing so far
	(void)filename;
	throw IOException("Nothing implemented here", AT);
}

void GSNIO::readDEM(DEMObject&)
{
	//Nothing so far
	throw IOException("Nothing implemented here", AT);
}

void GSNIO::readLanduse(Grid2DObject&)
{
	//Nothing so far
	throw IOException("Nothing implemented here", AT);
}

void GSNIO::writeMeteoData(const std::vector< std::vector<MeteoData> >&,
					  const std::string&)
{
	//Nothing so far
	throw IOException("Nothing implemented here", AT);
}

void GSNIO::readStationData(const Date&, std::vector<StationData>& vecStation)
{
	vecStation.clear();

	if (vecStationName.size() == 0)
		readStationNames(); //reads station names into vector<string> vecStationName

	for (unsigned int ii=0; ii<vecStationName.size(); ii++){ //loop through stations
		StationData sd;
		readStationMetaData(sd, ii);

		vecStation.push_back(sd);
	}
}

void GSNIO::readMeteoData(const Date& dateStart, const Date& dateEnd,
172
173
                          std::vector< std::vector<MeteoData> >& vecMeteo,
                          const unsigned int& stationindex)
174
175
176
177
178
179
180
181
182
183
184
185
186
187
{
	if (vecStationName.size() == 0)
		readStationNames(); //reads station names into vector<string> vecStationName

	if (vecStationName.size() == 0) //if there are no stations -> return
		return;

	unsigned int indexStart=0, indexEnd=vecStationName.size();

	//The following part decides whether all the stations are rebuffered or just one station
	if (stationindex == IOUtils::npos){
		vecMeteo.clear();
		vecMeteo.insert(vecMeteo.begin(), vecStationName.size(), vector<MeteoData>());
	} else {
188
		if (stationindex < vecMeteo.size()){
189
190
191
192
193
194
195
196
197
198
			indexStart = stationindex;
			indexEnd   = stationindex+1;
		} else {
			throw IndexOutOfBoundsException("You tried to access a stationindex in readMeteoData that is out of bounds", AT);
		}
	}

	for (unsigned int ii=indexStart; ii<indexEnd; ii++){ //loop through stations
		StationData sd;
		readStationMetaData(sd, ii);
199
		readData(dateStart, dateEnd, vecMeteo, sd, ii);
200
201
202
203
204
205
206
207
208
209
210
	}
}

void GSNIO::readStationMetaData(StationData& sd, const unsigned int& stationindex)
{
	_ns1__getSensorLocation sensorloc_req;
	_ns1__getSensorLocationResponse sensorloc;

	sensorloc_req.sensor = &vecStationName[stationindex];

	if (gsn.getSensorLocation(&sensorloc_req, &sensorloc) == SOAP_OK){
211
212
213
214
215
216
217
218
219
		if (sensorloc.return_.size() < 4) {
			stringstream ss;
			const unsigned int nr = sensorloc.return_.size();
			ss << "Not enough SensorLocation data received: expected 4, received " << nr << "\n";
			for(unsigned int ii=0; ii<nr; ii++) {
				ss << "sensorloc[" << ii << "]=" << sensorloc.return_[ii] << "  ";
			}
			throw IOException(ss.str(), AT);
		}
220
221
222
223
224
225
226
227
228
229
230
231

		const std::string *s0 = &sensorloc.return_[0]; //easier to type
		const std::string *s1 = &sensorloc.return_[1]; //easier to type
		const std::string *s2 = &sensorloc.return_[2]; //easier to type
		const std::string *s3 = &sensorloc.return_[3]; //easier to type
		const size_t sep0 = s0->find_first_of("=");
		const size_t sep1 = s1->find_first_of("=");
		const size_t sep2 = s2->find_first_of("=");
		const size_t sep3 = s3->find_first_of("=");
		double latitude=IOUtils::nodata, longitude=IOUtils::nodata, altitude=IOUtils::nodata;
		const std::string name = s0->substr((sep0+1), (s0->size()-sep0-2));

232
233
234
		convertStringToDouble(latitude, s1->substr((sep1+1), (s1->size()-1)), "Latitude");
		convertStringToDouble(longitude, s2->substr((sep2+1), (s2->size()-1)), "Longitude");
		convertStringToDouble(altitude, s3->substr((sep3+1), (s3->size()-1)), "Altitude");
235
236
237
238
239
240
241
242

		//HACK!! would it be possible for getValueForKey() to do this transparently? (with a user flag)
		latitude = IOUtils::standardizeNodata(latitude, plugin_nodata);
		longitude = IOUtils::standardizeNodata(longitude, plugin_nodata);
		altitude = IOUtils::standardizeNodata(altitude, plugin_nodata);

		Coords coordinate(coordin, coordinparam);
		coordinate.setLatLon(latitude, longitude, altitude);
243
		sd.setStationData(coordinate, vecStationName[stationindex], name);
244
245
246
247
248
249
	} else {
		soap_print_fault(&gsn, stdout);
		throw IOException("Error in communication with GSN",AT);
	}
}

250
void GSNIO::parseString(const std::string& in_string, MeteoData& md){
251
	std::stringstream ss(in_string);
252
253
254
255
256
	std::string tmpstring;

	while (std::getline(ss, tmpstring, ';')){
		std::stringstream data(tmpstring);
		while (std::getline(data, tmpstring, '=')){
257
			const string key = tmpstring;
258
259
260
261
262
263
264
			if (!(std::getline(data, tmpstring, '=')))
				throw InvalidFormatException("",AT);

			if (key == "LIGHT") convertStringToDouble(md.iswr, tmpstring, "ISWR");
			else if (key == "AIR_TEMP") convertStringToDouble(md.ta, tmpstring, "Air Temperature");
			else if (key == "WIND_SPEED") convertStringToDouble(md.vw, tmpstring, "Wind Velocity");
			else if (key == "WIND_DIRECTION") convertStringToDouble(md.dw, tmpstring, "Wind Velocity");
265
266
			else if (key == "INCOMING_RADIATION") {
				convertStringToDouble(md.iswr, tmpstring, "incoming_radiation");
267
268
				//convertStringToDouble(md.ilwr, tmpstring, "solar_rad");
			}
269
270
			else if (key == "RELATIVE_HUMIDITY") convertStringToDouble(md.rh, tmpstring, "relative_humidity");
			else if (key == "SURFACE_TEMP") convertStringToDouble(md.tss, tmpstring, "soil_temp");
271
272
273
274
275
			else if (key == "GROUND_TEMP_TNX") convertStringToDouble(md.tsg, tmpstring, "ground_temp_tnx");
			else if (key == "RAIN_METER") convertStringToDouble(md.hnw, tmpstring, "rain_meter");
			else if (key == "TIMED") {
				tmpstring = tmpstring.substr(0,tmpstring.length()-3); //cut away the seconds
				time_t measurementTime;
276
277
278
279
280
281
282
				if (!IOUtils::convertString(measurementTime, tmpstring, std::dec)) {
					stringstream ss;
					ss << "Conversion failed for value TIMED=" << tmpstring;
					throw ConversionFailedException(ss.str(), AT);
				}
				md.date.setDate(measurementTime);
				md.date.rnd(60., Date::CLOSEST); //HACK: round to closest minute
283
284
285
286
287
			}
		}
	}
}

288
289
void GSNIO::convertStringToDouble(double& d, const std::string& in_string, const std::string& in_parname){
	if (!IOUtils::convertString(d, in_string, std::dec))
290
		throw ConversionFailedException("Conversion failed for value " + in_parname, AT);
291
292
293
}

void GSNIO::readData(const Date& dateStart, const Date& dateEnd, std::vector< std::vector<MeteoData> >& vecMeteo,
294
                     const StationData& sd, const unsigned int& stationindex)
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
{
	_ns1__getMeteoData meteodata_req;
	_ns1__getMeteoDataResponse meteodata;
	std::vector<std::string> vecString;

	meteodata_req.sensor = &vecStationName[stationindex];

	LONG64 l1(dateStart.getUnixDate());
	LONG64 l2(dateEnd.getUnixDate());

	l1*=1000; //GSN is using ms, not seconds
	l2*=1000; //GSN is using ms, not seconds

	meteodata_req.from = l1;
	meteodata_req.to = l2;

	if (gsn.getMeteoData(&meteodata_req, &meteodata) == SOAP_OK){
		cout << "\t[D] GSN: nr of datasets received for station "<< stationindex << ": " << meteodata.return_.size() << endl;
		for (unsigned int jj=0; jj<meteodata.return_.size(); jj++){
			MeteoData md;
315
			md.meta = sd;
316
			parseString(meteodata.return_[jj], md);
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
			convertUnits(md);
			vecMeteo[stationindex].push_back(md);
		}
	} else {
		soap_print_fault(&gsn, stderr);
		throw IOException("Error in communication with GSN",AT);
	}
}

void GSNIO::readStationNames()
{
	vecStationName.clear();

	//Read in the StationNames
	string xmlpath="", str_stations="";
	int stations=0;

334
	cfg.getValue("NROFSTATIONS", "Input", str_stations, Config::nothrow);
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406

	if (str_stations != ""){
		if (!IOUtils::convertString(stations, str_stations, std::dec))
			throw ConversionFailedException("Error while reading value for NROFSTATIONS", AT);

		for (int ii=0; ii<stations; ii++) {
			stringstream tmp_stream;
			string stationname="", tmp_file="";

			tmp_stream << (ii+1); //needed to construct key name
			cfg.getValue(string("STATION"+tmp_stream.str()), "Input", stationname);
			std::cout << "\tRead io.ini stationname: '" << stationname << "'" << std::endl;
			vecStationName.push_back(stationname);
		}
	} else { //just take all GSN stations available
		_ns1__getSensorsResponse sensors;
		if (gsn.getSensors(&sensors) == SOAP_OK){
			for (unsigned int ii=0; ii<sensors.return_.size(); ii++){
				//cout << "[d] Sensor " << ii << " Name: " << sensors.return_[ii] << endl;
				std::cout << "\tRead GSN stationname: '" << sensors.return_[ii] << "'" << std::endl;
				vecStationName.push_back(sensors.return_[ii]);
			}
		} else {
			soap_print_fault(&gsn, stderr);
			throw IOException("Error in communication with GSN",AT);
		}
	}
}

void GSNIO::readAssimilationData(const Date&, Grid2DObject&)
{
	//Nothing so far
	throw IOException("Nothing implemented here", AT);
}

void GSNIO::readSpecialPoints(std::vector<Coords>&)
{
	//Nothing so far
	throw IOException("Nothing implemented here", AT);
}

void GSNIO::write2DGrid(const Grid2DObject&, const std::string& name)
{
	//Nothing so far
	(void)name;
	throw IOException("Nothing implemented here", AT);
}

void GSNIO::convertUnits(MeteoData& meteo)
{
	meteo.standardizeNodata(plugin_nodata);

	//converts C to Kelvin, converts ilwr to ea, converts RH to [0,1]
	if(meteo.ta!=IOUtils::nodata) {
		meteo.ta=C_TO_K(meteo.ta);
	}

	if(meteo.tsg!=IOUtils::nodata) {
		meteo.tsg=C_TO_K(meteo.tsg);
	}

	if(meteo.tss!=IOUtils::nodata) {
		meteo.tss=C_TO_K(meteo.tss);
	}

	if(meteo.rh!=IOUtils::nodata) {
		meteo.rh /= 100.;
	}
}

extern "C"
{
407
408
409
#define COMPILE_PLUGIN
#include "exports.h"

410
	METEOIO_EXPORT void deleteObject(void* obj) {
411
412
413
		delete reinterpret_cast<PluginObject*>(obj);
	}

414
	METEOIO_EXPORT void* loadObject(const string& classname, const Config& cfg) {
415
416
		if(classname == "GSNIO") {
			//cerr << "Creating dynamic handle for " << classname << endl;
417
			return new GSNIO(deleteObject, cfg);
418
419
420
421
422
423
424
		}
		//cerr << "Could not load " << classname << endl;
		return NULL;
	}
}

} //namespace