WSL/SLF GitLab Repository

Commit c7cc84d0 authored by Thomas Egger's avatar Thomas Egger
Browse files

Fixed a small bug in IOUtils::seek, the binary search on vectors of MeteoData.

Added two functions to MeteoData: bool param_exists(string) and void reset()
reset() sets all the values of the MeteoData object to IOUtils::nodata (note: not the date), and param_exists(string) returns true if a certain parameter exists and false otherwise in a MeteoData object.

The Meteo1DInterpolator now correctly creates MeteoData objects that have more parameters than the default ones (like ta, hnw, ...)
parent 2d55399c
......@@ -385,8 +385,6 @@ unsigned int IOUtils::seek(const Date& soughtdate, const std::vector<MeteoData>&
//returns index of element, if element does not exist it returns closest index after soughtdate
//the element needs to be an exact hit or embedded between two measurments
unsigned int ii = 1;
if (vecM.size() <= 0) {//no elements in buffer
return IOUtils::npos;
}
......@@ -406,7 +404,7 @@ unsigned int IOUtils::seek(const Date& soughtdate, const std::vector<MeteoData>&
//if we reach this point: the date is spanned by the buffer and there are at least two elements
if (exactmatch){
unsigned int first = ii, last = vecM.size()-1;
unsigned int first = 1, last = vecM.size()-1;
//perform binary search
while (first <= last) {
......@@ -419,7 +417,7 @@ unsigned int IOUtils::seek(const Date& soughtdate, const std::vector<MeteoData>&
return mid; // found it. return position
}
} else {
unsigned int first = ii, last = vecM.size()-1;
unsigned int first = 0, last = vecM.size()-1;
//perform binary search
while (first <= last) {
......
......@@ -53,27 +53,31 @@ unsigned int Meteo1DInterpolator::resampleData(const Date& date, std::vector<Met
if (vecM.size() != vecS.size())
throw IOException("Inconsistency between vecM and vecS detected", AT);
if (vecM.size() == 0){ //Deal with case of the empty vector
vecM.push_back(MeteoData(date));
vecS.push_back(StationData());
return 0; //nothing left to do
}
//Find element in the vector, or insert it at the appropriate position
unsigned int position = IOUtils::seek(date, vecM, false);
if (position == IOUtils::npos){ //nothing found append new element at the left or right
if (vecM.size() == 0){
vecM.push_back(MeteoData(date));
vecS.push_back(StationData());
return 0; //nothing left to do
}
MeteoData tmpmd(vecM.at(0)); //create a clone of one of the elements
tmpmd.reset(); //set all values to IOUtils::nodata
tmpmd.setDate(date);
if (position == IOUtils::npos){ //nothing found append new element at the left or right
if (vecM.at(0).date > date){
vecM.insert(vecM.begin(), MeteoData(date));
vecM.insert(vecM.begin(), tmpmd);
vecS.insert(vecS.begin(), vecS.at(0)); //copy element
position = 0;
} else if (vecM.at(vecM.size()-1).date < date){
vecM.push_back(MeteoData(date));
vecM.push_back(tmpmd);
vecS.push_back(vecS.at(vecS.size()-1)); //copy element
position = vecM.size() - 1;
}
} else if ((position != IOUtils::npos) && (vecM[position].date != date)){//insert before position
vecM.insert(vecM.begin()+position, MeteoData(date));
vecM.insert(vecM.begin()+position, tmpmd);
vecS.insert(vecS.begin()+position, vecS[position]); //copy element
}
......
......@@ -188,6 +188,14 @@ void MeteoData::setDate(const Date& _date)
date = _date;
}
void MeteoData::reset()
{
std::map<unsigned int, double*>::iterator it;
for (it=meteoparam.begin(); it!=meteoparam.end(); it++){
*it->second = IOUtils::nodata;
}
}
void MeteoData::setData(const MeteoData::Parameters& param, const double& value)
{
*meteoparam[param] = value;
......@@ -277,6 +285,17 @@ const double& MeteoData::param(const std::string& parname) const
return *(it->second);
}
bool MeteoData::param_exists(const std::string& parname) const
{
std::map<std::string, double*>::const_iterator it;
it = mapParameterByName.find(parname);
if (it != mapParameterByName.end())
return true;
return false;
}
std::ostream& operator<<(std::ostream& os, const MeteoData& data) {
os << "<meteo>\n";
......
......@@ -102,6 +102,9 @@ class MeteoData {
*/
void addParameter(const std::string& i_paramname);
bool param_exists(const std::string& parname) const;
void reset();
bool isResampled();
void setResampled(const bool&);
......
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