WSL/SLF GitLab Repository

CosmoXMLIO.cc 25.3 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
#include "CosmoXMLIO.h"
19
20
21
#include <meteoio/meteolaws/Atmosphere.h>
#include <sstream>

22
//To read a text file
23
24
25
26
27
28
29
30
31
32
33
34
35
36
#include <iostream>
#include <fstream>
#include <string>

using namespace std;

namespace mio {
/**
 * @page cosmoxml COSMOXML
 * @section cosmoxml_format Format
 * This plugin reads the XML files as generated by the Cosmo system.
 * The files are outputted in Grib format and preprocessed by FieldExtra (MeteoSwiss)
 * to get XML files.
 * It requires libxml to compile and run.
37
 * When writing files, it creates one file per station nammed as {stationID}_{numerical date}.xml
38
39
40
41
 *
 * @section cosmoxml_units Units
 * The units are assumed to be the following:
 * - temperatures in celsius
42
43
 * - dew point in celsius (input)
 * - relative humidity in % (output)
44
45
46
 * - wind speed in m/s
 * - precipitations in mm/h
 * - radiation in W/m²
47
 * - maximal wind speed in m/s (not implemented yet...)
48
49
50
 *
 * @section cosmoxml_keywords Keywords
 * This plugin uses the following keywords:
51
 * - COORDSYS:  input coordinate system (see Coords) specified in the [Input] section
52
53
 * - METEOPATH: string containing the path to the xml files to be read, specified in the [Input] section
 * - METEOPATH: string containing the path where the XML files have to be written, specified in the [Output] section
54
 * - METEO:     specify COSMOXML for [Input] and/or [Output] section(s)
55
56
57
58
59
 *
 * Example:
 * @code
 * [Input]
 * COORDSYS	= CH1903
60
 * METEO	= COSMOXML
61
62
 * METEOPATH	= ./input/meteoXMLdata
 * [Output]
63
 * METEO	= COSMOXML
64
65
 * METEOPATH	= ./output/meteoXMLdata
 * @endcode
66
67
 */

68
69
const double CosmoXMLIO::in_tz = 0.; //Plugin specific timezone
const double CosmoXMLIO::out_tz = 0.; //Plugin specific time zone
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

CosmoXMLIO::CosmoXMLIO(void (*delObj)(void*), const Config& i_cfg) : IOInterface(delObj), cfg(i_cfg)
{
	IOUtils::getProjectionParameters(cfg, coordin, coordinparam, coordout, coordoutparam);
}

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

CosmoXMLIO::CosmoXMLIO(const Config& cfgreader) : IOInterface(NULL), cfg(cfgreader)
{
	IOUtils::getProjectionParameters(cfg, coordin, coordinparam, coordout, coordoutparam);
}

CosmoXMLIO::~CosmoXMLIO() throw()
{
	
}

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

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

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

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

115
void CosmoXMLIO::readStationData(const Date& station_date, std::vector<StationData>& vecStation)
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
	//Get all files from directory
	string meteopath = "", station_path="";
 	cfg.getValue("METEOPATH", "Input", meteopath);
	if (meteopath == "")
		throw ConversionFailedException("Error while reading value for METEOPATH", AT);	
	const string pattern = "xml";
	list<string> dirlist;
	IOUtils::readDirectory(meteopath, dirlist, pattern);
	dirlist.sort();
	
	vecStation.clear();	//Initialize Station Vector

	list<string>::iterator itr;	//To loop in the stations list
	
	for( itr = dirlist.begin(); itr != dirlist.end(); itr++ ) {	//Loop over all stations in the meteopath directory
		station_path = meteopath + "/" + *itr;
		
		StationData sd;
		//Initialize variables
		double altitude=IOUtils::nodata, latitude=IOUtils::nodata, longitude=IOUtils::nodata;
		string station_name="", station_ID="";
		bool is_first=true;
		Date first_date=IOUtils::nodata, last_date=IOUtils::nodata, date_read=IOUtils::nodata;
		
		//Read station and meteo data
		xmlpp::TextReader reader(station_path);
		while(reader.read()) {
			if(reader.has_attributes()) {
				reader.move_to_first_attribute();
				const string key=reader.get_value();
				//StationData
				if(key=="identifier") station_name = getValue(reader);
				if(key=="station_abbreviation") station_ID = getValue(reader);
				if(key=="station.height") altitude = getDoubleValue(reader);
				if(key=="station.latitude") latitude = getDoubleValue(reader);
				if(key=="station.longitude") longitude = getDoubleValue(reader);
 				if(key=="missing_value_code") plugin_nodata = getDoubleValue(reader);
				//Test used to write station data only if the given date is in the station data interval
				if(key=="reference_ts") {
					if(!is_first) {
						//Check date
						date_read = getDateValue(reader);
						if (date_read < first_date) {
							first_date = date_read;
						}
						if (date_read > last_date) {
							last_date = date_read;
						}
					} else {
						//First date to be read -> is first and last date
						first_date = getDateValue(reader);
						last_date = first_date;
						is_first = false;
					}
				}
				//End of date test
			}
		}
		
		//Write station data if the given date is in the station data interval
		if ((station_date > first_date) && (station_date < last_date)) {
			sd.stationName = station_name;
			sd.stationID = station_ID;
			sd.position.setLatLon(latitude, longitude, altitude);
			vecStation.push_back(sd);	//Store results in vecStation
		}
		
		//Write station data for any date
// 		sd.stationName = station_name;
// 		sd.stationID = station_ID;
// 		sd.position.setLatLon(latitude, longitude, altitude);
// 		vecStation.push_back(sd);	//Store results in vecStation

	}
191
192
}

193
//Use parser to get value corresponding to a key
194
195
196
197
198
199
200
201
202
std::string CosmoXMLIO::getValue(xmlpp::TextReader& reader) {
	reader.move_to_element();
	reader.read();
	const string value = reader.get_value();
	reader.read();
	reader.read();
	return( value );
}

203
//Conversion from string to double of the value read by the parser
204
205
206
207
208
209
210
211
double CosmoXMLIO::getDoubleValue(xmlpp::TextReader& reader) {
	double value;
	const string Svalue = getValue(reader);
	if( IOUtils::convertString(value, Svalue) == false) {
		stringstream ss;
		ss << "Can not parse (double)" << Svalue;
		throw ConversionFailedException(ss.str(), AT);
	}
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
	return IOUtils::standardizeNodata(value, plugin_nodata);
}

//Conversion °C to °K
double CosmoXMLIO::c2k(const double& value) {
	if(value!=IOUtils::nodata)
		return C_TO_K(value);
	else
		return value;
}

//Conversion °K to °C
double CosmoXMLIO::k2c(const double& value) {
	if(value!=IOUtils::nodata)
		return K_TO_C(value);
	else
		return value;
229
230
}

231
//Conversion from string to Date of the value read by the parser
232
233
234
235
236
237
238
239
240
241
242
Date CosmoXMLIO::getDateValue(xmlpp::TextReader& reader) {
	Date date;
	const string Svalue = getValue(reader);
	if( IOUtils::convertString(date, Svalue, in_tz) == false) {
		stringstream ss;
		ss << "Can not parse (date)" << Svalue;
		throw ConversionFailedException(ss.str(), AT);
	}
	return date;
}

243
244
245
246
247
248
249
250
//Converts dew point to humidity and writes station position
void CosmoXMLIO::finishMeteo(const double& latitude, const double& longitude, const double& altitude, 
                             double& dew_point, MeteoData& meteo) {
	// Set position
	if(altitude!=IOUtils::nodata && latitude!=IOUtils::nodata && longitude!=IOUtils::nodata) {
		meteo.meta.position.setLatLon(latitude, longitude, altitude);
	} else {
		throw InvalidFormatException("Meteo data found, but no position information", AT);
251
252
	}
	
253
254
255
	//Set RH
	if(meteo.ta!=IOUtils::nodata && dew_point!=IOUtils::nodata) {
		meteo.rh=Atmosphere::DewPointtoRh(dew_point, meteo.ta,TRUE);
256
	}
257
258
259
	
	//Reset dew_point
	dew_point = IOUtils::nodata;
260
261
}

262
//----------> Read Station and Meteo data, uses all stations in the "meteopath" directory <----------
263
void CosmoXMLIO::readMeteoData(const Date& dateStart, const Date& dateEnd,
264
265
                               std::vector< std::vector<MeteoData> >& vecMeteo,
                               const unsigned int&)
266
{	
267
268
269
270
271
272
273
274
275
276
277
278
	//Get all files from directory
	string meteopath = "", station_path="";
 	cfg.getValue("METEOPATH", "Input", meteopath);
	if (meteopath == "")
		throw ConversionFailedException("Error while reading value for METEOPATH", AT);	
	const string pattern = "xml";
	list<string> dirlist;
	IOUtils::readDirectory(meteopath, dirlist, pattern);
	dirlist.sort();
	
	vecMeteo.clear();	//Initialize Meteo Vector

279
	vecMeteo.insert(vecMeteo.begin(), dirlist.size(), std::vector<MeteoData>());	//Allocation for the vectors
280
281
	list<string>::iterator itr;	//To loop in the stations list
	
282
	unsigned int ii=0;	//Declare and initialize counter (to know which station we are dealing with)
283
	for( itr = dirlist.begin(); itr != dirlist.end(); itr++ ) {	//Loop over all stations in the meteopath directory
284
285
286
287
288
		station_path = meteopath + "/" + *itr;
		
		MeteoData meteo;
		//Initialize variables
		double altitude=IOUtils::nodata, latitude=IOUtils::nodata, longitude=IOUtils::nodata;
289
290
		double dew_point=IOUtils::nodata;
		bool is_first=true;
291
		bool next_station = false;
292
293
294
295
296
297
298
299
		
		//Read station and meteo data
		xmlpp::TextReader reader(station_path);
		while(reader.read()) {
			if(reader.has_attributes()) {
				reader.move_to_first_attribute();
				const string key=reader.get_value();
				//StationData
300
301
				if(key=="identifier") meteo.meta.stationName = getValue(reader);
				if(key=="station_abbreviation") meteo.meta.stationID = getValue(reader);
302
303
304
305
306
307
				if(key=="station.height") altitude = getDoubleValue(reader);
				if(key=="station.latitude") latitude = getDoubleValue(reader);
				if(key=="station.longitude") longitude = getDoubleValue(reader);
	// 			if(key=="model_station_height") altitude = getDoubleValue(reader);
	// 			if(key=="model_station_latitude") latitude = getDoubleValue(reader);
	// 			if(key=="model_station_longitude") longitude = getDoubleValue(reader);
308
309
 				if(key=="missing_value_code") plugin_nodata = getDoubleValue(reader);
				
310
				//MeteoData
311
312
313
314
315
				if(key=="T_2M") meteo.ta = c2k( getDoubleValue(reader) );
				if(key=="TD_2M") dew_point = c2k( getDoubleValue(reader) );
				if(key=="GLOB") meteo.iswr = getDoubleValue(reader);
				if(key=="TOT_PREC") meteo.hnw = getDoubleValue(reader);
				if(key=="FF_10M") meteo.vw = getDoubleValue(reader);
316
				if(key=="VMAX_10M") meteo.vw_max = getDoubleValue(reader);
317
				if(key=="reference_ts") {
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
					if(!is_first) {
						//We can finish our object and push it.
						//Before we check if it is in the right time interval
						if (meteo.date < dateStart) {
							meteo.reset();
							meteo.date = getDateValue(reader);
							//Go to the next entry
							continue;
						}
						if (meteo.date > dateEnd) {
							meteo.reset();
							//Go to the next station
							next_station = true;
							break;
						}
333
334
335
						finishMeteo(latitude, longitude, altitude, dew_point, meteo);
						vecMeteo[ii].push_back( meteo );
					} else {
336
						//Nothing in memory to be written when we meet the first date entry
337
						is_first = false;
338
					}
339
					meteo.reset();
340
					meteo.date = getDateValue(reader);
341
342
343
				}
			}
		}
344
		if (next_station==true) continue;
345
346
		finishMeteo(latitude, longitude, altitude, dew_point, meteo);
		vecMeteo[ii].push_back( meteo );
347
348
349
350
		reader.close();
		ii++;
	}
}
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
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432

//Writes header and description of station data
void CosmoXMLIO::writeHeader(std::stringstream& XMLdata)
{
	XMLdata << "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n";
	XMLdata << "<!DOCTYPE tree-table-xml SYSTEM \"tree-table-xml.dtd\">\n<tree-table-xml>\n";
	XMLdata << "<ttable id=\"MeteoIO->GribXML_save\">\n<row>\n<col id=\"datainformation-tables\">\n<ttable id=\"metadata\">\n";
	XMLdata << "<row>\n<col id=\"id\">identifier</col>\n<col id=\"element\">unique composed identifier</col>\n<col id=\"unit\">character</col>\n<col id=\"class\">java.lang.String</col>\n<col id=\"decimal-precision\" class=\"java.lang.Integer\">0</col>\n<col id=\"scale\" class=\"java.lang.Integer\">1</col>\n<col id=\"mandatory\" class=\"java.lang.Boolean\">1</col>\n</row>\n";
	XMLdata << "<row>\n<col id=\"id\">station_abbreviation</col>\n<col id=\"element\">station abbrevation</col>\n<col id=\"unit\">character</col>\n<col id=\"class\">java.lang.String</col>\n<col id=\"decimal-precision\" class=\"java.lang.Integer\">0</col>\n<col id=\"scale\" class=\"java.lang.Integer\">1</col>\n<col id=\"mandatory\" class=\"java.lang.Boolean\">1</col>\n</row>\n";
	XMLdata << "<row>\n<col id=\"id\">station_height</col>\n<col id=\"element\">station height</col>\n<col id=\"unit\">m</col>\n<col id=\"class\">java.lang.Double</col>\n<col id=\"decimal-precision\" class=\"java.lang.Integer\">0</col>\n<col id=\"scale\" class=\"java.lang.Integer\">1</col>\n<col id=\"mandatory\" class=\"java.lang.Boolean\">1</col>\n</row>\n";
	XMLdata << "<row>\n<col id=\"id\">station_latitude</col>\n<col id=\"element\">station latitude</col>\n<col id=\"unit\">degree</col>\n<col id=\"class\">java.lang.Double</col>\n<col id=\"decimal-precision\" class=\"java.lang.Integer\">4</col>\n<col id=\"scale\" class=\"java.lang.Integer\">1</col>\n<col id=\"mandatory\" class=\"java.lang.Boolean\">1</col>\n</row>\n";
	XMLdata << "<row>\n<col id=\"id\">station_longitude</col>\n<col id=\"element\">station longitude</col>\n<col id=\"unit\">degree</col>\n<col id=\"class\">java.lang.Double</col>\n<col id=\"decimal-precision\" class=\"java.lang.Integer\">4</col>\n<col id=\"scale\" class=\"java.lang.Integer\">1</col>\n<col id=\"mandatory\" class=\"java.lang.Boolean\">1</col>\n</row>\n";
	XMLdata << "<row>\n<col id=\"id\">model_station_height</col>\n<col id=\"element\">height of model grid point associated to station</col>\n<col id=\"unit\">m</col>\n<col id=\"class\">java.lang.Double</col>\n<col id=\"decimal-precision\" class=\"java.lang.Integer\">0</col>\n<col id=\"scale\" class=\"java.lang.Integer\">1</col>\n<col id=\"mandatory\" class=\"java.lang.Boolean\">1</col>\n</row>\n";
	XMLdata << "<row>\n<col id=\"id\">model_station_latitude</col>\n<col id=\"element\">latitude of model grid point associated to station</col>\n<col id=\"unit\">degree</col>\n<col id=\"class\">java.lang.Double</col>\n<col id=\"decimal-precision\" class=\"java.lang.Integer\">4</col>\n<col id=\"scale\" class=\"java.lang.Integer\">1</col>\n<col id=\"mandatory\" class=\"java.lang.Boolean\">1</col>\n</row>\n";
	XMLdata << "<row>\n<col id=\"id\">model_station_longitude</col>\n<col id=\"element\">longitude of model grid point associated to station</col>\n<col id=\"unit\">degree</col>\n<col id=\"class\">java.lang.Double</col>\n<col id=\"decimal-precision\" class=\"java.lang.Integer\">4</col>\n<col id=\"scale\" class=\"java.lang.Integer\">1</col>\n<col id=\"mandatory\" class=\"java.lang.Boolean\">1</col>\n</row>\n";
	XMLdata << "<row>\n<col id=\"id\">data_cat_number</col>\n<col id=\"element\">data cat number</col>\n<col id=\"unit\">decimal code</col>\n<col id=\"class\">java.lang.Double</col>\n<col id=\"decimal-precision\" class=\"java.lang.Integer\">0</col>\n<col id=\"scale\" class=\"java.lang.Integer\">1</col>\n<col id=\"mandatory\" class=\"java.lang.Boolean\">1</col>\n</row>\n";
	XMLdata << "<row>\n<col id=\"id\">data_cat_description</col>\n<col id=\"element\">data cat description</col>\n<col id=\"unit\">character</col>\n<col id=\"class\">java.lang.String</col>\n<col id=\"decimal-precision\" class=\"java.lang.Integer\">0</col>\n<col id=\"scale\" class=\"java.lang.Integer\">1</col>\n<col id=\"mandatory\" class=\"java.lang.Boolean\">0</col>\n</row>\n";
	XMLdata << "<row>\n<col id=\"id\">model_configuration</col>\n<col id=\"element\">model configuration</col>\n<col id=\"unit\">decimal code</col>\n<col id=\"class\">java.lang.Double</col>\n<col id=\"decimal-precision\" class=\"java.lang.Integer\">0</col>\n<col id=\"scale\" class=\"java.lang.Integer\">1</col>\n<col id=\"mandatory\" class=\"java.lang.Boolean\">1</col>\n</row>\n";
	XMLdata << "<row>\n<col id=\"id\">missing_value_code</col>\n<col id=\"element\">missing value code</col>\n<col id=\"unit\">code</col>\n<col id=\"class\">java.lang.Double</col>\n<col id=\"decimal-precision\" class=\"java.lang.Integer\">1</col>\n<col id=\"scale\" class=\"java.lang.Integer\">1</col>\n<col id=\"mandatory\" class=\"java.lang.Boolean\">1</col>\n</row>\n";
	XMLdata << "<row>\n<col id=\"id\">model_run_dt</col>\n<col id=\"element\">model run date (UTC)</col>\n<col id=\"unit\">YYYYMMDDHHmm</col>\n<col id=\"class\">java.lang.String</col>\n<col id=\"decimal-precision\" class=\"java.lang.Integer\">1</col>\n<col id=\"scale\" class=\"java.lang.Integer\">1</col>\n<col id=\"mandatory\" class=\"java.lang.Boolean\">1</col>\n</row>\n";
	XMLdata << "</ttable>\n<!-- metadata datainformation section -->\n</col>\n";
}

//Writes station data
void CosmoXMLIO::writeLocationHeader(const StationData& station, std::stringstream& XMLdata) 
{
		XMLdata << "<col>\n<ttable id=\"data\">\n<row>" << endl;
		XMLdata << "<col id=\"identifier\">" << station.getStationName() << "</col>\n";
		XMLdata << "<col id=\"station_abbreviation\">" << station.getStationID() << "</col>\n";
		
		//Same altitude, latitude and longitude data for station and model.
		XMLdata << "<col id=\"station.height\">" << station.position.getAltitude() << "</col>\n";
		XMLdata << "<col id=\"station.latitude\">" << station.position.getLat() << "</col>\n";
		XMLdata << "<col id=\"station.longitude\">" << station.position.getLon() << "</col>\n";
 		XMLdata << "<col id=\"model_station_height\">" << station.position.getAltitude() << "</col>\n";
		XMLdata << "<col id=\"model_station_latitude\">" << station.position.getLat() << "</col>\n";
		XMLdata << "<col id=\"model_station_longitude\">" << station.position.getLon() << "</col>\n";
// 		XMLdata << "<col id=\"data_cat_number\">" << "TO DO!" << "</col>\n";	//HACK do we need these values?
// 		XMLdata << "<col id=\"data_cat_description\">" << "TO DO!" << "</col>\n";
// 		XMLdata << "<col id=\"model_configuration\">" << "TO DO!" << "</col>\n";
 		XMLdata << "<col id=\"missing_value_code\">" << IOUtils::nodata << "</col>\n";
// 		XMLdata << "<col id=\"model_run_dt\">" << "TO DO!" << "</col>\n";
}

//Writes description of meteo data
void CosmoXMLIO::writeMeteoDataDescription(std::stringstream& XMLdata)
{
	XMLdata << "</row>\n</ttable>\n<!-- data datainformation section -->\n</col></row>\n";
	XMLdata << "<row>\n<col id=\"values-tables\">\n<ttable id=\"metadata\">\n";
	XMLdata << "<row>\n<col id=\"id\">identifier</col>\n<col id=\"element\">unique composed identifier</col>\n<col id=\"unit\">character</col>\n<col id=\"class\">java.lang.String</col>\n<col id=\"decimal-precision\" class=\"java.lang.Integer\">0</col>\n<col id=\"scale\" class=\"java.lang.Integer\">1</col>\n<col id=\"mandatory\" class=\"java.lang.Boolean\">1</col>\n</row>\n";
	XMLdata << "<row>\n<col id=\"id\">reference_ts</col>\n<col id=\"element\">timestamp (UTC)</col>\n<col id=\"unit\">YYYYMMDDHHmm</col>\n<col id=\"class\">java.lang.String</col>\n<col id=\"decimal-precision\" class=\"java.lang.Integer\">0</col>\n<col id=\"scale\" class=\"java.lang.Integer\">1</col>\n<col id=\"mandatory\" class=\"java.lang.Boolean\">1</col>\n</row>\n";
	XMLdata << "<row>\n<col id=\"id\">T_2M</col>\n<col id=\"element\">Temperature 2m above ground, height corrected</col>\n<col id=\"unit\">degree Celsius</col>\n<col id=\"class\">java.lang.Double</col>\n<col id=\"decimal-precision\" class=\"java.lang.Integer\">1</col>\n<col id=\"scale\" class=\"java.lang.Integer\">1</col>\n<col id=\"mandatory\" class=\"java.lang.Boolean\">1</col>\n</row>\n";
	XMLdata << "<row>\n<col id=\"id\">TD_2M</col>\n<col id=\"element\">Dew point temperature 2 m above ground, height corrected</col>\n<col id=\"unit\">degree Celsius</col>\n<col id=\"class\">java.lang.Double</col>\n<col id=\"decimal-precision\" class=\"java.lang.Integer\">1</col>\n<col id=\"scale\" class=\"java.lang.Integer\">1</col>\n<col id=\"mandatory\" class=\"java.lang.Boolean\">1</col>\n</row>\n";
	XMLdata << "<row>\n<col id=\"id\">GLOB</col>\n<col id=\"element\">Global radiation</col>\n<col id=\"unit\">W/m^2</col>\n<col id=\"class\">java.lang.Double</col>\n<col id=\"decimal-precision\" class=\"java.lang.Integer\">1</col>\n<col id=\"scale\" class=\"java.lang.Integer\">1</col>\n<col id=\"mandatory\" class=\"java.lang.Boolean\">1</col>\n</row>\n";
	XMLdata << "<row>\n<col id=\"id\">TOT_PREC</col>\n<col id=\"element\">Total precipitation, hourly sum</col>\n<col id=\"unit\">mm</col>\n<col id=\"class\">java.lang.Double</col>\n<col id=\"decimal-precision\" class=\"java.lang.Integer\">1</col>\n<col id=\"scale\" class=\"java.lang.Integer\">1</col>\n<col id=\"mandatory\" class=\"java.lang.Boolean\">1</col>\n</row>\n";
	XMLdata << "<row>\n<col id=\"id\">FF_10M</col>\n<col id=\"element\">Wind speed 10m above ground</col>\n<col id=\"unit\">m/s</col>\n<col id=\"class\">java.lang.Double</col>\n<col id=\"decimal-precision\" class=\"java.lang.Integer\">1</col>\n<col id=\"scale\" class=\"java.lang.Integer\">1</col>\n<col id=\"mandatory\" class=\"java.lang.Boolean\">1</col>\n</row>\n";
	XMLdata << "<row>\n<col id=\"id\">VMAX_10M</col>\n<col id=\"element\">Wind gusts 10m above ground</col>\n<col id=\"unit\">m/s</col>\n<col id=\"class\">java.lang.Double</col>\n<col id=\"decimal-precision\" class=\"java.lang.Integer\">1</col>\n<col id=\"scale\" class=\"java.lang.Integer\">1</col>\n<col id=\"mandatory\" class=\"java.lang.Boolean\">1</col>\n</row>\n";
	XMLdata << "</ttable>\n<!-- metadata values section -->\n</col>\n";
}

//Writes meteo data
void CosmoXMLIO::writeMeteo(const std::vector<MeteoData>& vecMeteo, std::stringstream& XMLdata)
{
	XMLdata << "<col>\n<ttable id=\"data\">\n";
	for(unsigned int jj=0; jj<vecMeteo.size(); jj++) {
		XMLdata << "<row>\n";
		XMLdata << "<col id=\"identifier\">" << vecMeteo[jj].meta.getStationName() << "</col>\n";
		Date tmp_date(vecMeteo[jj].date);
		tmp_date.setTimeZone(out_tz);
		XMLdata << "<col id=\"reference_ts\">" << tmp_date.toString(Date::NUM) << "</col>\n";
		XMLdata << "<col id=\"T_2M\">" << k2c(vecMeteo[jj].ta) << "</col>\n";
		
		if (vecMeteo[jj].rh==IOUtils::nodata || vecMeteo[jj].ta==IOUtils::nodata) {
			XMLdata << "<col id=\"TD_2M\">" << IOUtils::nodata << "</col>\n";
		} else {
			const double dew_point=Atmosphere::RhtoDewPoint(vecMeteo[jj].rh, vecMeteo[jj].ta, TRUE);
			XMLdata << "<col id=\"TD_2M\">" << k2c(dew_point) << "</col>\n";
		}
		
		XMLdata << "<col id=\"GLOB\">" << vecMeteo[jj].iswr << "</col>\n";
		XMLdata << "<col id=\"TOT_PREC\">" << vecMeteo[jj].hnw << "</col>\n";
		XMLdata << "<col id=\"FF_10M\">" << vecMeteo[jj].vw << "</col>\n";
433
		XMLdata << "<col id=\"VMAX_10M\">" << vecMeteo[jj].vw_max << "</col>\n";
434
435
436
		XMLdata << "</row>\n";
	}
}
437

438
439
440
441
442
443
444
//Writes last lines of the file
void CosmoXMLIO::writeFooter(std::stringstream& XMLdata)
{
	XMLdata << "</ttable>\n<!-- data values section -->\n</col>\n";
	XMLdata << "</row>\n</ttable>\n</tree-table-xml>\n";
}

445
446
447
448
void CosmoXMLIO::writeMeteoData(const std::vector< std::vector<MeteoData> >& vecMeteo,
						const std::string&)
{
	ofstream fout;
449
450
451
	string filename="", meteopath_out = "", line;
	stringstream XMLdata;
	cfg.getValue("METEOPATH", "Output", meteopath_out);
452
	for (unsigned int ii=0; ii < vecMeteo.size(); ii++) {
453
454
		//Test existence of element 0
		if(vecMeteo[ii].size()==0) {
455
			std::cout << "[E] Station " << ii+1 << " exists but contains no data! Skip writing it...\n";
456
457
			continue;
		}
458
		
459
460
461
462
463
		XMLdata.str("");	//Initialize XMLdata stringstream
		
		//Write the first part of the XML file (header and station data description)
		writeHeader(XMLdata);
	
464
		//Insert station data
465
466
467
468
		writeLocationHeader( vecMeteo[ii][0].meta, XMLdata );

		//Write the second part of the XML file (meteo data description)
		writeMeteoDataDescription(XMLdata);
469
470

		//Insert meteo data
471
		writeMeteo(vecMeteo[ii], XMLdata);
472

473
474
		//Write the last part of the XML file (after meteo data, only a few lines)
		writeFooter(XMLdata);
475
476

		//Save file
477
478
479
480
481
482
483
		std::string stat_id = vecMeteo[ii][0].meta.getStationID();
		if(stat_id=="") {
			stringstream ss;
			ss << "station" << ii;
			stat_id = ss.str();
		}
		filename = meteopath_out + "/" + stat_id + "_" + vecMeteo[ii][0].date.toString(Date::NUM) + ".xml";
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
		fout.open(filename.c_str());
		fout << XMLdata.str();
		fout.close();
	}
}

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

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

void CosmoXMLIO::cleanup() throw()
{
	
}

#ifndef _METEOIO_JNI
extern "C"
{
510
511
512
#define COMPILE_PLUGIN
#include "exports.h"

513
	METEOIO_EXPORT void deleteObject(void* obj) {
514
515
516
		delete reinterpret_cast<PluginObject*>(obj);
	}

517
	METEOIO_EXPORT void* loadObject(const string& classname, const Config& cfg) {
518
519
520
521
522
523
524
525
526
527
528
		if(classname == "CosmoXMLIO") {
			//cerr << "Creating dynamic handle for " << classname << endl;
			return new CosmoXMLIO(deleteObject, cfg);
		}
		//cerr << "Could not load " << classname << endl;
		return NULL;
	}
}
#endif

} //namespace