WSL/SLF GitLab Repository

Commit b53b81aa authored by Mathias Bavay's avatar Mathias Bavay
Browse files

The GridBuffer class has been expanded and is now used by Meteo2Dinterpolator...

The GridBuffer class has been expanded and is now used by Meteo2Dinterpolator for interpolated grids
parent ee7edab4
......@@ -24,11 +24,14 @@ namespace mio {
Meteo2DInterpolator::Meteo2DInterpolator(const Config& i_cfg, TimeSeriesManager& i_tsmanager, GridsManager& i_gridsmanager)
: cfg(i_cfg), tsmanager(i_tsmanager), gridsmanager(i_gridsmanager),
mapBufferedGrids(), mapBufferedInfos(), IndexBufferedGrids(), mapAlgorithms(),
grid_buffer(0), mapAlgorithms(),
v_params(), v_coords(), v_stations(), virtual_point_cache(),
max_grids(10), algorithms_ready(false), use_full_dem(false), downscaling(false), virtual_stations(false)
algorithms_ready(false), use_full_dem(false), downscaling(false), virtual_stations(false)
{
setDfltBufferProperties();
size_t max_grids = 10; //default number of grids to keep in buffer
cfg.getValue("BUFF_GRIDS", "Interpolations2D", max_grids, IOUtils::nothrow);
grid_buffer.setMaxGrids(max_grids);
setAlgorithms();
cfg.getValue("Virtual_stations", "Input", virtual_stations, IOUtils::nothrow);
if (virtual_stations) {
......@@ -46,51 +49,6 @@ Meteo2DInterpolator::~Meteo2DInterpolator()
}
}
void Meteo2DInterpolator::setDfltBufferProperties()
{
max_grids = 10; //default number of grids to keep in buffer
cfg.getValue("BUFF_GRIDS", "Interpolations2D", max_grids, IOUtils::nothrow);
}
void Meteo2DInterpolator::addToBuffer(const Date& date, const DEMObject& dem, const MeteoData::Parameters& meteoparam, const Grid2DObject& grid, const std::string& info)
{
if (max_grids==0) return;
if (IndexBufferedGrids.size() >= max_grids) { //we need to remove the oldest grid
mapBufferedGrids.erase( mapBufferedGrids.find( IndexBufferedGrids.front() ) );
mapBufferedInfos.erase( mapBufferedInfos.find( IndexBufferedGrids.front() ) );
IndexBufferedGrids.erase( IndexBufferedGrids.begin() ); //begin = iterator to front()
}
std::ostringstream ss;
ss << dem.llcorner.printLatLon() << " " << dem.getNx() << "x" << dem.getNy() << " @" << dem.cellsize << "::" << date.toString(Date::ISO) << "::" << MeteoData::getParameterName(meteoparam);
mapBufferedGrids[ ss.str() ] = grid;
mapBufferedInfos[ ss.str() ] = info;
IndexBufferedGrids.push_back( ss.str() );
}
bool Meteo2DInterpolator::getFromBuffer(const Date& date, const DEMObject& dem, const MeteoData::Parameters& meteoparam, Grid2DObject& grid, std::string& info) const
{
if (IndexBufferedGrids.empty())
return false;
std::ostringstream ss;
ss << dem.llcorner.printLatLon() << " " << dem.getNx() << "x" << dem.getNy() << " @" << dem.cellsize << "::" << date.toString(Date::ISO) << "::" << MeteoData::getParameterName(meteoparam);
const std::map<std::string, std::string>::const_iterator it_info = mapBufferedInfos.find( ss.str() );
if (it_info != mapBufferedInfos.end()) {
info = (*it_info).second;
}
const std::map<std::string, Grid2DObject>::const_iterator it = mapBufferedGrids.find( ss.str() );
if (it != mapBufferedGrids.end()) { //already in map
grid = (*it).second;
return true;
}
return false;
}
/* By reading the Config object build up a list of user configured algorithms
* for each MeteoData::Parameters parameter (i.e. each member variable of MeteoData like ta, p, hnw, ...)
* Concept of this constructor: loop over all MeteoData::Parameters and then look
......@@ -152,12 +110,15 @@ void Meteo2DInterpolator::interpolate(const Date& date, const DEMObject& dem, co
if (!algorithms_ready)
setAlgorithms();
const string param_name = MeteoData::getParameterName(meteoparam);
//Get grid from buffer if it exists
if (getFromBuffer(date, dem, meteoparam, result, InfoString))
std::ostringstream grid_hash;
grid_hash << dem.llcorner.printLatLon() << " " << dem.getNx() << "x" << dem.getNy() << " @" << dem.cellsize << " " << date.toString(Date::ISO) << " " << param_name;
if (grid_buffer.get(result, grid_hash.str(), InfoString))
return;
//Show algorithms to be used for this parameter
const string param_name = MeteoData::getParameterName(meteoparam);
const map<string, vector<InterpolationAlgorithm*> >::iterator it = mapAlgorithms.find(param_name);
if (it==mapAlgorithms.end()) {
throw IOException("No interpolation algorithms configured for parameter "+param_name, AT);
......@@ -193,7 +154,8 @@ void Meteo2DInterpolator::interpolate(const Date& date, const DEMObject& dem, co
Meteo2DInterpolator::checkMinMax(0.0, 10000.0, result);
}
addToBuffer(date, dem, meteoparam, result, InfoString);
//save grid in buffer
grid_buffer.push(result, grid_hash.str(), InfoString);
}
//HACK make sure that skip_virtual_stations = true before calling this method when using virtual stations!
......@@ -441,12 +403,7 @@ const std::string Meteo2DInterpolator::toString() const {
}
//cache content
os << "Current buffer content (" << mapBufferedGrids.size() << " grids):\n";
std::map<std::string, Grid2DObject>::const_iterator it1;
for (it1=mapBufferedGrids.begin(); it1 != mapBufferedGrids.end(); ++it1){
os << setw(10) << "Grid " << it1->first << "\n";
}
os << grid_buffer.toString();
os << "</Meteo2DInterpolator>\n";
return os.str();
}
......
......@@ -21,6 +21,7 @@
#include <meteoio/TimeSeriesManager.h>
#include <meteoio/Config.h>
#include <meteoio/dataClasses/Buffer.h>
#include <meteoio/dataClasses/Date.h>
#include <meteoio/dataClasses/MeteoData.h>
#include <meteoio/dataClasses/DEMObject.h>
......@@ -168,20 +169,14 @@ class Meteo2DInterpolator {
static size_t get_parameters(const Config& cfg, std::set<std::string>& set_parameters);
static size_t getAlgorithmsForParameter(const Config& cfg, const std::string& parname, std::vector<std::string>& vecAlgorithms);
void addToBuffer(const Date& date, const DEMObject& dem, const MeteoData::Parameters& meteoparam, const Grid2DObject& grid, const std::string& info);
bool getFromBuffer(const Date& date, const DEMObject& dem, const MeteoData::Parameters& meteoparam, Grid2DObject& grid, std::string& info) const;
size_t getVirtualStationsData(const Date& i_date, METEO_SET& vecMeteo);
void setDfltBufferProperties();
void setAlgorithms();
void initVirtualStations();
const Config& cfg; ///< Reference to Config object, initialized during construction
TimeSeriesManager& tsmanager; ///< Reference to TimeSeriesManager object, used for callbacks, initialized during construction
GridsManager& gridsmanager; ///< Reference to GridsManager object, used for callbacks, initialized during construction
std::map<std::string, Grid2DObject> mapBufferedGrids; ///< Buffer interpolated grids
std::map<std::string, std::string> mapBufferedInfos; ///< Buffer interpolations info messages
std::vector<std::string> IndexBufferedGrids; ///< Keep position information for easy erase fo specific grids
GridBuffer grid_buffer;
std::map< std::string, std::vector<InterpolationAlgorithm*> > mapAlgorithms; //per parameter interpolation algorithms
......@@ -190,7 +185,6 @@ class Meteo2DInterpolator {
std::vector<StationData> v_stations; ///< metadata for virtual stations
std::map<Date, METEO_SET > virtual_point_cache; ///< stores already resampled virtual data points
size_t max_grids; ///< How many grids to buffer
bool algorithms_ready; ///< Have the algorithms objects been constructed?
bool use_full_dem; ///< use full dem for point-wise spatial interpolations
bool downscaling; ///< Are we downscaling meteo grids instead of interpolating stations' data?
......
......@@ -221,8 +221,14 @@ const std::string MeteoBuffer::toString() const
return os.str();
}
/********************************************************************************************/
/****************************** GridBuffer class ***********************************************/
/********************************************************************************************/
GridBuffer::GridBuffer(const size_t& in_max_grids)
: dem_buffer(), mapBufferedGrids(), mapBufferedInfos(), IndexBufferedGrids(), max_grids(in_max_grids)
{}
bool GridBuffer::get(Grid2DObject& grid, const std::string& grid_hash) const
{
......@@ -238,6 +244,22 @@ bool GridBuffer::get(Grid2DObject& grid, const std::string& grid_hash) const
return false;
}
bool GridBuffer::get(Grid2DObject& grid, const std::string& grid_hash, std::string& grid_info) const
{
grid_info.clear();
if (max_grids==0) return false;
if (get(grid, grid_hash)==true) {
const std::map<std::string, std::string>::const_iterator it = mapBufferedInfos.find( grid_hash );
if (it != mapBufferedInfos.end()) { //found in map
grid_info = (*it).second;
}
return true;
}
return false;
}
bool GridBuffer::get(Grid2DObject& grid, const MeteoGrids::Parameters& parameter, const Date& date) const
{
const string grid_hash = date.toString(Date::ISO)+"::"+MeteoGrids::getParameterName(parameter);
......@@ -271,7 +293,7 @@ bool GridBuffer::get(DEMObject& grid) const
return false;
}
void GridBuffer::push(const DEMObject& in_grid)
void GridBuffer::push(const DEMObject& grid)
{
//HACK: properly manage buffering multiple dems!
//std::ostringstream grid_hash;
......@@ -281,25 +303,39 @@ void GridBuffer::push(const DEMObject& in_grid)
if (!dem_buffer.empty())
dem_buffer.clear();
dem_buffer.push_back( in_grid );
dem_buffer.push_back( grid );
}
void GridBuffer::push(const Grid2DObject& in_grid, const std::string& grid_hash)
void GridBuffer::push(const Grid2DObject& grid, const std::string& grid_hash, const std::string& grid_info)
{
if (max_grids==0) return;
if(IndexBufferedGrids.size() >= max_grids) { //we need to remove the oldest grid
mapBufferedGrids.erase( mapBufferedGrids.find( IndexBufferedGrids.front() ) );
IndexBufferedGrids.erase( IndexBufferedGrids.begin() );
const string tmp_hash = IndexBufferedGrids.front();
mapBufferedGrids.erase( mapBufferedGrids.find( tmp_hash ) );
mapBufferedInfos.erase( mapBufferedInfos.find( tmp_hash ) );
//IndexBufferedGrids.erase( IndexBufferedGrids.begin() );
swap( IndexBufferedGrids.front(), IndexBufferedGrids.back() );
IndexBufferedGrids.pop_back();
}
mapBufferedGrids[ grid_hash ] = in_grid;
IndexBufferedGrids.push_back( grid_hash );
mapBufferedGrids[ grid_hash ] = grid;
mapBufferedInfos[ grid_hash ] = grid_info;
IndexBufferedGrids.push_back( grid_hash );
}
void GridBuffer::push(const Grid2DObject& grid, const std::string& grid_hash)
{
if (max_grids==0) return;
push(grid, grid_hash, "");
}
void GridBuffer::push(const Grid2DObject& in_grid, const MeteoGrids::Parameters& parameter, const Date& date)
void GridBuffer::push(const Grid2DObject& grid, const MeteoGrids::Parameters& parameter, const Date& date)
{
if (max_grids==0) return;
const string grid_hash = date.toString(Date::ISO)+"::"+MeteoGrids::getParameterName(parameter);
push(in_grid, grid_hash);
push(grid, grid_hash, "");
}
const std::string GridBuffer::toString() const
......
......@@ -126,26 +126,29 @@ class MeteoBuffer {
*/
class GridBuffer {
public:
GridBuffer(const size_t& in_max_grids) : dem_buffer(), mapBufferedGrids(), IndexBufferedGrids(), max_grids(in_max_grids) {};
GridBuffer(const size_t& in_max_grids);
bool empty() const {return IndexBufferedGrids.empty();};
void clear() {mapBufferedGrids.clear(); IndexBufferedGrids.clear();};
void clear() {mapBufferedGrids.clear(); mapBufferedInfos.clear(); IndexBufferedGrids.clear();};
size_t size() const {return IndexBufferedGrids.size();};
void setMaxGrids(const size_t& in_max_grids) {max_grids=in_max_grids;};
bool get(DEMObject& grid) const;
bool get(Grid2DObject& grid, const std::string& grid_hash) const;
bool get(Grid2DObject& grid, const std::string& grid_hash, std::string& grid_info) const;
bool get(Grid2DObject& grid, const MeteoGrids::Parameters& parameter, const Date& date) const;
bool get(DEMObject& grid) const;
void push(const Grid2DObject& in_grid, const std::string& grid_hash);
void push(const Grid2DObject& in_grid, const MeteoGrids::Parameters& parameter, const Date& date);
void push(const DEMObject& in_grid);
void push(const DEMObject& grid);
void push(const Grid2DObject& grid, const std::string& grid_hash);
void push(const Grid2DObject& grid, const std::string& grid_hash, const std::string& grid_info);
void push(const Grid2DObject& grid, const MeteoGrids::Parameters& parameter, const Date& date);
const std::string toString() const;
private:
private:
std::vector<DEMObject> dem_buffer;
std::map<std::string, Grid2DObject> mapBufferedGrids;
std::map<std::string, Grid2DObject> mapBufferedGrids; ///< Buffer interpolated grids
std::map<std::string, std::string> mapBufferedInfos; ///< Buffer interpolations info messages
std::vector<std::string> IndexBufferedGrids; // this is required in order to know which grid is the oldest one
size_t max_grids; ///< How many grids to buffer (grids, dem, landuse and assimilation grids together)
};
......
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