WSL/SLF GitLab Repository

Commit 0921ea6c authored by Mathias Bavay's avatar Mathias Bavay
Browse files

The IOManager was only keeping a reference to the Config object given in its...

The IOManager was only keeping a reference to the Config object given in its constructor. Unfortunately, this means that if the original Config object would go out of scope, the reference would become invalid. Moreover, all internal objects (BufferedIOHandler, Meteo1DInterpolator, etc) were keeping the same reference to this external Config object. However, IOHandler was keeping a copy of the object. This has now been changed: IOManager keeps a copy of the Config object given to its constructor, and all other objects keep a reference to this copy. Therefore, the original Config object can be destroyed without any problem and the internal copy is kept at only one place (within IOManager).

A division by zero (for min=max) has been found and fixed in PGMIO. Moreover, the min and max are written in the metadata (so that the file can be read again and properly rescaled).
parent 19109ff6
......@@ -154,7 +154,6 @@ void IOHandler::loadPlugin(const std::string& libname, const std::string& classn
cout << "Please check your PLUGINPATH in your configuration file!" << std::endl;
} else {
io = dynamic_cast<IOInterface*>((dynLibrary)->newObject(classname, cfg));
if(io == NULL) {
cout << AT << ": [E] Failed loading dynamic plugin " << classname << " from " << filename << "(NULL pointer to plugin's class)" << std::endl;
//delete dynLibrary; This causes a segfault !!
......@@ -179,11 +178,9 @@ IOInterface* IOHandler::getPlugin(const std::string& cfgkey, const std::string&
std::map<std::string, IOPlugin>::iterator mapit = mapPlugins.find(op_src);
if (mapit == mapPlugins.end())
throw IOException("Can not find plugin " + op_src + " as requested in file " + cfg.getSourceName() + ". Has its developer declared it in IOHandler::registerPlugins?", AT);
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 '" + op_src + "', but plugin is not loaded", AT);
}
......@@ -378,7 +375,7 @@ std::string IOHandler::toString() const
{
std::stringstream os;
os << "<IOHandler>\n";
os << "Config cfg = " << hex << &cfg << dec << "\n";
os << "Config& cfg = " << hex << &cfg << dec << "\n";
os << "<mapPlugins>\n";
os << setw(10) << "Keyword" << " = " << IOPlugin::header << "\n";
......
......@@ -22,7 +22,7 @@ using namespace std;
namespace mio {
IOManager::IOManager(const Config& i_cfg) : cfg(i_cfg), rawio(i_cfg), bufferedio(rawio, i_cfg), meteoprocessor(i_cfg)
IOManager::IOManager(const Config& i_cfg) : cfg(i_cfg), rawio(cfg), bufferedio(rawio, cfg), meteoprocessor(cfg)
{
setProcessingLevel(IOManager::filtered | IOManager::resampled);
fcache_start = fcache_end = Date(0.0, 0.); //this should not matter, since 0 is still way back before any real data...
......@@ -354,7 +354,7 @@ std::string IOManager::toString() const {
stringstream os;
os << "<IOManager>\n";
os << "Config& cfg = " << hex << &cfg << dec << "\n";
os << "Config cfg = " << hex << &cfg << dec << "\n";
#ifndef _POPC_ //HACK popc
os << rawio;
#endif
......
......@@ -181,7 +181,7 @@ class IOManager {
bool read_filtered_cache(const Date& start_date, const Date& end_date,
std::vector< METEO_TIMESERIE >& vec_meteo);
const Config& cfg;
const Config cfg; ///< we keep this Config object as full copy, so the original one can get out of scope/be destroyed
IOHandler rawio;
BufferedIOHandler bufferedio;
MeteoProcessor meteoprocessor;
......
......@@ -68,11 +68,11 @@ void PGMIO::getGridPaths() {
grid2dpath_in="", grid2dpath_out="";
string tmp="";
cfg.getValue("GRID2D", "Input", tmp, Config::nothrow);
if (tmp == "ARC") //keep it synchronized with IOHandler.cc for plugin mapping!!
if (tmp == "PGM") //keep it synchronized with IOHandler.cc for plugin mapping!!
cfg.getValue("GRID2DPATH", "Input", grid2dpath_in);
tmp="";
cfg.getValue("GRID2D", "Output", tmp, Config::nothrow);
if (tmp == "ARC") //keep it synchronized with IOHandler.cc for plugin mapping!!
if (tmp == "PGM") //keep it synchronized with IOHandler.cc for plugin mapping!!
cfg.getValue("GRID2DPATH", "Output", grid2dpath_out);
}
......@@ -258,20 +258,24 @@ void PGMIO::write2DGrid(const Grid2DObject& grid_in, const std::string& name)
fout << fixed << showpoint << setprecision(6);
try {
const double min_value = grid_in.grid2D.getMin();
const double max_value = grid_in.grid2D.getMax();
double scaling;
if(min_value!=max_value) scaling = 1./(max_value - min_value) * (double)(nr_colors-1); //so we keep color 0 for nodata
else scaling = 1.;
//writing the header
fout << "P2\n";
fout << "#Generated by MeteoIO - http://slfsmm.indefero.net/p/meteoio\n";
fout << "#llcorner latitude = " << setprecision(6) << llcorner.getLat() << "\n";
fout << "#llcorner longitude = " << setprecision(6) << llcorner.getLon() << "\n";
fout << "#cellsize = " << setprecision(2) << grid_in.cellsize << " m\n";
fout << "#minimum = " << setprecision(6) << min_value << "\n";
fout << "#maximum = " << setprecision(6) << max_value << "\n";
fout << grid_in.ncols << " " << grid_in.nrows << "\n";
fout << nr_colors << "\n";
//writing the data
const double max_value = grid_in.grid2D.getMax();
const double min_value = grid_in.grid2D.getMin();
const double scaling = 1./(max_value - min_value) * (double)(nr_colors-1); //so we keep color 0 for nodata
for (unsigned int kk=grid_in.nrows-1; kk < grid_in.nrows; kk--) {
for (unsigned int ll=0; ll < grid_in.ncols; ll++) {
const double value = grid_in.grid2D(ll, kk);
......
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