WSL/SLF GitLab Repository

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

Slightly improved toString() output as well as first version of a proper...

Slightly improved toString() output as well as first version of a proper Buffer class (not used yet)
parent 6ddfd8fc
......@@ -524,16 +524,18 @@ const std::string IOHandler::toString() const
}
os << "</mapPlugins>\n";
os << "<excluded_params>\n";
std::map< std::string, std::set<std::string> >::const_iterator it_exc;
for (it_exc=excluded_params.begin(); it_exc != excluded_params.end(); ++it_exc) {
os << setw(10) << it_exc->first << " = ";
std::set<std::string>::const_iterator it_set;
for (it_set=(it_exc->second).begin(); it_set != (it_exc->second).end(); ++it_set)
os << *it_set << " ";
os << "\n";
if (!excluded_params.empty()) {
os << "<excluded_params>\n";
std::map< std::string, std::set<std::string> >::const_iterator it_exc;
for (it_exc=excluded_params.begin(); it_exc != excluded_params.end(); ++it_exc) {
os << setw(10) << it_exc->first << " = ";
std::set<std::string>::const_iterator it_set;
for (it_set=(it_exc->second).begin(); it_set != (it_exc->second).end(); ++it_set)
os << *it_set << " ";
os << "\n";
}
os << "</excluded_params>\n";
}
os << "</excluded_params>\n";
os << "</IOHandler>\n";
return os.str();
......
......@@ -41,6 +41,7 @@
#include <meteoio/dataClasses/Matrix.h>
#include <meteoio/dataClasses/MeteoData.h>
#include <meteoio/dataClasses/StationData.h>
#include <meteoio/dataClasses/Buffer.h>
#include <meteoio/DataGenerator.h>
#include <meteoio/exports.h>
......
/***********************************************************************************/
/* Copyright 2014 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/>.
*/
#include <meteoio/dataClasses/Buffer.h>
#include <algorithm>
using namespace std;
namespace mio {
bool MeteoBuffer::get(const Date& date, METEO_SET &vecMeteo) const
{
vecMeteo.clear();
if (empty() || (date < ts_start) || (date > ts_end)) //data is NOT fully in cache
return false;
for (size_t ii=0; ii<ts_buffer.size(); ii++) { //loop over stations
if (ts_buffer[ii].empty()) continue; //no data in buffer for this station
const size_t pos = IOUtils::seek(date, ts_buffer[ii], true);
if (pos!=IOUtils::npos)
vecMeteo.push_back( ts_buffer[ii][pos] );
}
return true;
}
bool MeteoBuffer::get(const Date& date_start, const Date& date_end, std::vector< METEO_SET > &vecMeteo) const
{
vecMeteo.clear();
if (empty() || (date_start < ts_start) || (date_end > ts_end)) //data is NOT fully in cache
return false;
for (size_t ii=0; ii<ts_buffer.size(); ii++) { //loop over stations
vecMeteo.push_back( vector<MeteoData>() ); //insert one empty vector of MeteoData
if (ts_buffer[ii].empty()) continue; //no data in buffer for this station
if (ts_buffer[ii].front().date>date_end || ts_buffer[ii].back().date<date_start) continue; //no data in buffer for this station
size_t pos_start = IOUtils::seek(date_start, ts_buffer[ii], false);
if (pos_start==IOUtils::npos) pos_start = 0;
size_t pos_end = IOUtils::seek(date_end, ts_buffer[ii], false);
if (pos_end==IOUtils::npos) pos_end = ts_buffer[ii].size() - 1;
vecMeteo[ii].reserve(pos_end-pos_start+1); //weird that the "insert" does not handle it internally...
vecMeteo[ii].insert(vecMeteo[ii].begin(), ts_buffer[ii].begin()+pos_start, ts_buffer[ii].begin()+pos_end);
}
return true;
}
bool MeteoBuffer::empty() const
{ //the ts_buffer could be empty if there was no data between the provided dates
//so the empty criteria is if the ts_start and ts_end are valid or undef
return (ts_start.isUndef() || ts_end.isUndef());
}
void MeteoBuffer::clear()
{
ts_buffer.clear();
ts_start.setUndef(true);
ts_end.setUndef(true);
}
void MeteoBuffer::push(const Date& date_start, const Date& date_end, const std::vector< METEO_SET >& vecMeteo)
{
if (empty()) {
ts_start = date_start;
ts_end = date_end;
ts_buffer = vecMeteo;
return;
}
const size_t nrStationsBuffer = ts_buffer.size();
const size_t nrStationsPush = vecMeteo.size();
//check that we are dealing with the same stations
if (nrStationsBuffer!=nrStationsPush) {
ostringstream ss;
ss << "The number of stations changed over time from " << nrStationsBuffer << " to " << nrStationsPush << ", ";
ss << "this is not handled yet!";
throw IOException(ss.str(), AT);
}
for (size_t ii=0; ii<nrStationsBuffer; ii++) { //for all stations
if (ts_buffer[ii].empty() || vecMeteo[ii].empty())
continue;
if (ts_buffer[ii].front().meta.getHash()!=vecMeteo[ii].front().meta.getHash()) {
ostringstream ss;
ss << "The stations changed over time from " << ts_buffer[ii].front().meta.getHash() << " to " << vecMeteo[ii].front().meta.getHash() << ", ";
ss << "this is not handled yet!";
throw IOException(ss.str(), AT);
}
}
//now, do the append/merge
for (size_t ii=0; ii<nrStationsBuffer; ii++) { //for all stations
if (vecMeteo[ii].empty()) continue;
if (ts_buffer[ii].empty()) {
ts_buffer[ii] = vecMeteo[ii];
continue;
}
const Date buffer_start = ts_buffer[ii].front().date;
const Date buffer_end = ts_buffer[ii].back().date;
const Date data_start = vecMeteo[ii].front().date;
const Date data_end = vecMeteo[ii].back().date;
if (data_start>buffer_end) { //the data simply fits to the end
ts_buffer[ii].insert(ts_buffer[ii].end(), vecMeteo[ii].begin(), vecMeteo[ii].begin()+vecMeteo[ii].size());
continue;
}
if (data_end<buffer_start) { //the data simply fits at the start
ts_buffer[ii].insert(ts_buffer[ii].begin(), vecMeteo[ii].begin(), vecMeteo[ii].begin()+vecMeteo[ii].size());
continue;
}
//there is some overlap, only copy data that does NOT overlap
if (data_start<buffer_start) {
const size_t pos = IOUtils::seek(buffer_start, vecMeteo[ii], false); //returns the first date >=
ts_buffer[ii].insert(ts_buffer[ii].begin(), vecMeteo[ii].begin(), vecMeteo[ii].begin()+pos-1);
}
if (data_end>buffer_end) {
size_t pos = IOUtils::seek(buffer_end, vecMeteo[ii], false); //returns the first date >=
if (vecMeteo[ii][pos].date == buffer_end) pos++; //to make sure we have an element that is not already in buffer
ts_buffer[ii].insert(ts_buffer[ii].end(), vecMeteo[ii].begin()+pos, vecMeteo[ii].begin()+vecMeteo[ii].size());
}
}
ts_start = min(ts_start, date_start);
ts_end = max(ts_end, date_end);
}
/*void MeteoBuffer::push(const Date& date, const METEO_SET& vecMeteo)
{
}*/
double MeteoBuffer::getAvgSamplingRate() const
{
if (ts_buffer.empty())
return IOUtils::nodata;
const size_t nr_stations = ts_buffer.size();
double sum = 0;
for (size_t ii=0; ii<nr_stations; ii++){ //loop over all stations
if(!ts_buffer[ii].empty()) {
const std::vector<MeteoData>& curr_station = ts_buffer[ii];
const double days = curr_station.back().date.getJulian() - curr_station.front().date.getJulian();
//add the average sampling rate for this station
const size_t nr_data_pts = ts_buffer[ii].size();
if(days>0.) sum += (double)(nr_data_pts-1) / days; //the interval story: 2 points define 1 interval!
}
}
if (sum > 0.){
return ((double)sum / (double)(nr_stations*24*3600)); //in points per seconds, ie Hz
}
return IOUtils::nodata;
}
Date MeteoBuffer::getBufferStart() const
{
return ts_start;
}
Date MeteoBuffer::getBufferEnd() const
{
return ts_end;
}
std::vector< METEO_SET >& MeteoBuffer::getBuffer()
{
return ts_buffer;
}
const std::string MeteoBuffer::toString() const
{
ostringstream os;
os << "<MeteoBuffer>\n";
os << "Buffer content (" << ts_buffer.size() << " stations)\n";
for(size_t ii=0; ii<ts_buffer.size(); ii++) {
if (!ts_buffer[ii].empty()){
os << std::setw(10) << ts_buffer[ii].front().meta.stationID << " = "
<< ts_buffer[ii].front().date.toString(Date::ISO) << " - "
<< ts_buffer[ii].back().date.toString(Date::ISO) << ", "
<< ts_buffer[ii].size() << " timesteps" << endl;
}
}
os << "</MeteoBuffer>\n";
return os.str();
}
} //end namespace
\ No newline at end of file
/***********************************************************************************/
/* Copyright 2014 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/>.
*/
#ifndef __BUFFER_H__
#define __BUFFER_H__
#include <meteoio/dataClasses/Date.h>
#include <meteoio/dataClasses/MeteoData.h>
namespace mio {
//we start easy: this is NOT a true ring buffer, just the same as what we previously had (for now)
class MeteoBuffer {
public:
MeteoBuffer() : ts_buffer(), ts_start(), ts_end() {};
bool get(const Date& date, METEO_SET &vecMeteo) const;
bool get(const Date& date_start, const Date& date_end, std::vector< METEO_SET > &vecMeteo) const;
double getAvgSamplingRate() const;
Date getBufferStart() const;
Date getBufferEnd() const;
bool empty() const;
void clear();
void push(const Date& date_start, const Date& date_end, const std::vector< METEO_SET >& vecMeteo);
//void push(const Date& date, const METEO_SET& vecMeteo);
const std::string toString() const;
std::vector< METEO_SET >& getBuffer();
private:
std::vector< METEO_SET > ts_buffer; ///< stores raw data
Date ts_start, ts_end; ///< store the beginning and the end date of the ts_buffer
};
}
#endif
\ No newline at end of file
......@@ -9,4 +9,5 @@ SET(dataClasses_sources
dataClasses/DEMObject.cc
dataClasses/StationData.cc
dataClasses/MeteoData.cc
dataClasses/Buffer.cc
)
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