WSL/SLF GitLab Repository

Commit 61362d11 authored by Mathias Bavay's avatar Mathias Bavay
Browse files

The usage of SMET for giving special points was not documented. When giving...

The usage of SMET for giving special points was not documented. When giving OLWR in SMET, TSS was computed but could lead to an arithmetic exception. PNGIO could throw an exception without freeing all the png_* memory, this has been improved. A few HACKS have been fixed/removed.
parent ec604de4
......@@ -458,7 +458,6 @@ void Coords::setXY(const double in_easting, const double in_northing, const doub
* @param[in] in_invalidate should the geographic coordinates be invalidated? (default=true, this flag should be false ONLY when calling from Grid2/3DObject)
*/
void Coords::setGridIndex(const int in_grid_i, const int in_grid_j, const int in_grid_k, const bool in_invalidate) {
//HACK TODO make grid_i,j,k friends of Grid2/3DObject -> remove _invalidate and ALWAYS invalidate
grid_i = in_grid_i;
grid_j = in_grid_j;
grid_k = in_grid_k;
......
......@@ -59,23 +59,22 @@ namespace mio {
const double A3DIO::plugin_nodata = -9999.0; //plugin specific nodata value
const unsigned int A3DIO::buffer_reserve = 23*24*2; //kind of average size of a buffer for optimizing vectors
//Main constructor
A3DIO::A3DIO(const std::string& configfile) : IOInterface(NULL), cfg(configfile) //HACK is this constructor still needed?
A3DIO::A3DIO(void (*delObj)(void*), const Config& i_cfg) : IOInterface(delObj), cfg(i_cfg)
{
IOUtils::getProjectionParameters(cfg, coordin, coordinparam, coordout, coordoutparam);
in_tz = out_tz = 0.;
IOUtils::getTimeZoneParameters(cfg, in_tz, out_tz);
}
//Copy constructor
A3DIO::A3DIO(const A3DIO& aio) : IOInterface(NULL), cfg(aio.cfg)
A3DIO::A3DIO(const std::string& configfile) : IOInterface(NULL), cfg(configfile)
{
IOUtils::getProjectionParameters(cfg, coordin, coordinparam, coordout, coordoutparam);
in_tz = out_tz = 0.;
IOUtils::getTimeZoneParameters(cfg, in_tz, out_tz);
}
A3DIO::A3DIO(void (*delObj)(void*), const Config& i_cfg) : IOInterface(delObj), cfg(i_cfg)
//Copy constructor
A3DIO::A3DIO(const A3DIO& aio) : IOInterface(NULL), cfg(aio.cfg)
{
IOUtils::getProjectionParameters(cfg, coordin, coordinparam, coordout, coordoutparam);
in_tz = out_tz = 0.;
......
......@@ -391,7 +391,7 @@ void CosmoXMLIO::writeLocationHeader(const StationData& station, std::stringstre
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_number\">" << "TO DO!" << "</col>\n";
// 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";
......
......@@ -733,7 +733,7 @@ void GRIBIO::scanMeteoPath()
const std::string& filename = *it;
std::string::size_type spos = filename.find_first_of("0123456789");
Date date;
IOUtils::convertString(date, filename.substr(spos,10), tz_in); //HACK MeteoSwiss-only file naming?
IOUtils::convertString(date, filename.substr(spos,10), tz_in);
std::pair<Date,std::string> tmp(date, filename);
cache_meteo_files.push_back(tmp);
......
......@@ -100,16 +100,19 @@ const unsigned char PNGIO::transparent_grey = channel_max_color;
PNGIO::PNGIO(void (*delObj)(void*), const Config& i_cfg) : IOInterface(delObj), cfg(i_cfg)
{
fp=NULL;
setOptions();
}
PNGIO::PNGIO(const std::string& configfile) : IOInterface(NULL), cfg(configfile)
{
fp=NULL;
setOptions();
}
PNGIO::PNGIO(const Config& cfgreader) : IOInterface(NULL), cfg(cfgreader)
{
fp=NULL;
setOptions();
}
......@@ -207,7 +210,7 @@ double PNGIO::getScaleFactor(const double& grid_w, const double& grid_h)
}
PNGIO::~PNGIO() throw() {
//HACK: implement a cleanup (close fp, free png pointers, etc
if(fp!=NULL) fclose(fp); fp=NULL;
}
void PNGIO::read2DGrid(Grid2DObject&, const std::string&)
......@@ -298,14 +301,14 @@ void PNGIO::setFile(const std::string& filename, png_structp& png_ptr, png_infop
// Initialize write structure
png_ptr = png_create_write_struct(PNG_LIBPNG_VER_STRING, NULL, NULL, NULL);
if (png_ptr == NULL) {
fclose(fp);
fclose(fp); fp=NULL;
throw IOException("Could not allocate write structure", AT);
}
// Initialize info structure
info_ptr = png_create_info_struct(png_ptr);
if (info_ptr == NULL) {
fclose(fp);
fclose(fp); fp=NULL;
png_destroy_write_struct(&png_ptr, (png_infopp)NULL);
free(png_ptr);
throw IOException("Could not allocate info structure", AT);
......@@ -363,7 +366,7 @@ unsigned int PNGIO::setLegend(const unsigned int &ncols, const unsigned int &nro
}
}
void PNGIO::writeDataSection(const Grid2DObject &grid, const Array2D<double> &legend_array, const Gradient &gradient, const unsigned int &full_width, const png_structp &png_ptr)
void PNGIO::writeDataSection(const Grid2DObject &grid, const Array2D<double> &legend_array, const Gradient &gradient, const unsigned int &full_width, const png_structp &png_ptr, png_infop& info_ptr)
{
const unsigned int ncols = grid.ncols;
const unsigned int nrows = grid.nrows;
......@@ -377,6 +380,9 @@ void PNGIO::writeDataSection(const Grid2DObject &grid, const Array2D<double> &le
png_bytep row = (png_bytep)calloc(channels*sizeof(png_byte), full_width);
if(row==NULL) {
fclose(fp); fp=NULL;
png_free_data(png_ptr, info_ptr, PNG_FREE_ALL, -1);
free(png_ptr);
throw IOException("Can not allocate row memory in PNGIO!", AT);
}
......@@ -451,7 +457,7 @@ void PNGIO::closePNG(png_structp& png_ptr, png_infop& info_ptr, png_color *palet
png_free_data(png_ptr, info_ptr, PNG_FREE_ALL, -1);
if(indexed_png && palette!=NULL) free(palette);
png_destroy_write_struct(&png_ptr, &info_ptr);
fclose(fp);
fclose(fp); fp=NULL;
free(info_ptr);
free(png_ptr);
}
......@@ -487,7 +493,7 @@ void PNGIO::write2DGrid(const Grid2DObject& grid_in, const std::string& filename
metadata_text.push_back("Unknown Gridded data");
writeMetadata(png_ptr, info_ptr);
writeDataSection(grid, legend_array, gradient, full_width, png_ptr);
writeDataSection(grid, legend_array, gradient, full_width, png_ptr, info_ptr);
png_write_end(png_ptr, NULL);
closePNG(png_ptr, info_ptr, palette);
......@@ -619,7 +625,7 @@ void PNGIO::write2DGrid(const Grid2DObject& grid_in, const MeteoGrids::Parameter
metadata_text.push_back( MeteoGrids::getParameterName(parameter) );
writeMetadata(png_ptr, info_ptr);
writeDataSection(grid, legend_array, gradient, full_width, png_ptr);
writeDataSection(grid, legend_array, gradient, full_width, png_ptr, info_ptr);
png_write_end(png_ptr, NULL);
closePNG(png_ptr, info_ptr, palette);
......
......@@ -72,7 +72,7 @@ class PNGIO : public IOInterface {
void setFile(const std::string& filename, png_structp& png_ptr, png_infop& info_ptr, const unsigned int &width, const unsigned int &height);
void writeWorldFile(const Grid2DObject& grid_in, const std::string& filename);
unsigned int setLegend(const unsigned int &ncols, const unsigned int &nrows, const double &min, const double &max, Array2D<double> &legend_array);
void writeDataSection(const Grid2DObject &grid, const Array2D<double> &legend_array, const Gradient &gradient, const unsigned int &full_width, const png_structp &png_ptr);
void writeDataSection(const Grid2DObject &grid, const Array2D<double> &legend_array, const Gradient &gradient, const unsigned int &full_width, const png_structp &png_ptr, png_infop& info_ptr);
void setPalette(const Gradient &gradient, png_structp& png_ptr, png_infop& info_ptr, png_color *palette);
void closePNG(png_structp& png_ptr, png_infop& info_ptr, png_color *palette);
std::string decimal_to_dms(const double& decimal);
......
......@@ -25,6 +25,7 @@ namespace mio {
* @page smetio SMET
* @section template_format Format
* The Station meteo data files is a station centered, ascii file format that has been designed with flexibility and ease of use in mind. Please refer to its <a href="../SMET_specifications.pdf">official format specification</a> for more information.
* This plugin can also provide special points, given as a SMET file containing either latitude/longitude/altitude or easting/northing/altitude. For the latter, the header must contain the epsg code (see example below).
*
* @section template_units Units
* All units are MKSA, the only exception being the precipitations that are in mm/h. It is however possible to use multipliers and offsets (but they must be specified in the file header).
......@@ -34,10 +35,12 @@ namespace mio {
* - STATION#: input filename (in METEOPATH). As many meteofiles as needed may be specified
* - METEOPATH: meteo files directory where to read/write the meteofiles; [Input] and [Output] sections
* - METEOPARAM: output file format options (ASCII or BINARY that might be followed by GZIP)
* - SPECIALPTSFILE: a path+file name to the a file containing grid coordinates of special points of interest (for special outputs)
*
* Example:
* @code
* [Input]
* METEO = SMET
* METEOPATH = ./input
* STATION1 = uppper_station.smet
* STATION2 = lower_station.smet
......@@ -46,6 +49,20 @@ namespace mio {
* METEOPATH = ./output
* METEOPARAM = ASCII GZIP
* @endcode
*
* Below is an example of special points input:
* @code
* SMET 1.1 ASCII
* [HEADER]
* station_id = my_pts
* epsg = 21781
* nodata = -999
* fields = easting northing altitude
* [DATA]
* 832781 187588 2115
* 635954 80358 2428
* @endcode
*
*/
const std::string SMETIO::dflt_extension = ".smet";
......@@ -393,6 +410,8 @@ void SMETIO::copy_data(const smet::SMETReader& myreader,
double SMETIO::olwr_to_tss(const double& olwr) {
const double ea = 1.;
if(olwr==IOUtils::nodata) return IOUtils::nodata;
if(olwr<0.) return IOUtils::nodata; //since olwr is NOT filtered, making sure no arithmetic exception would happen
if(olwr>1e4) return IOUtils::nodata; //since olwr is NOT filtered, making sure no arithmetic exception would happen
return pow( olwr / ( ea * Cst::stefan_boltzmann ), 0.25);
}
......@@ -691,7 +710,7 @@ bool SMETIO::checkConsistency(const std::vector<MeteoData>& vecMeteo, StationDat
* true is returned, otherwise false
*/
if (vecMeteo.size() > 0) //HACK to get the station data even when encoutering bug 87
if (vecMeteo.size() > 0) //to get the station data even when encoutering bug 87
sd = vecMeteo[0].meta;
for (size_t ii=1; ii<vecMeteo.size(); ii++){
......
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment