WSL/SLF GitLab Repository

Commit 99c544dd authored by Mathias Bavay's avatar Mathias Bavay
Browse files

Removing some try/catch, adding some documentation and adding the missing...

Removing some try/catch, adding some documentation and adding the missing error message as reported by issue 357
parent 2d0fc33c
......@@ -256,6 +256,12 @@ class Config {
}
}
/**
* @brief Template function to retrieve a vector of values of class T for a certain key pattern
* @param[in] keystart std::string representing a pattern for the key in the key/value file
* @param[in] section std::string representing a section name; the key has to be part of this section
* @param[out] vecT a vector of class T into which the values for the corresponding keys are saved
*/
template <typename T> void getValues(const std::string& keystart, const std::string& section, std::vector<T>& vecT) const
{
vecT.clear();
......
......@@ -52,19 +52,13 @@ void FilterMAD::process(const unsigned int& param, const std::vector<MeteoData>&
void FilterMAD::MAD_filter_point(const std::vector<MeteoData>& ivec, const unsigned int& param, const size_t& start, const size_t& end, double &value)
{
const double K = 1. / 0.6745;
double mad = IOUtils::nodata;
double median = IOUtils::nodata;
std::vector<double> data( end-start+1 );
for(size_t ii=start; ii<=end; ii++) data[ii-start] = ivec[ii](param);
//Calculate MAD
try {
median = Interpol1D::getMedian(data);
mad = Interpol1D::getMedianAverageDeviation(data);
} catch(const exception&){
return;
}
const double median = Interpol1D::getMedian(data);
const double mad = Interpol1D::getMedianAverageDeviation(data);
if( median==IOUtils::nodata || mad==IOUtils::nodata ) return;
......
......@@ -39,20 +39,20 @@ void ProcUnshade::process(const unsigned int& param, const std::vector<MeteoData
if(param!=MeteoData::ISWR && param!=MeteoData::RSWR)
throw InvalidArgumentException("Trying to use "+getName()+" filter on " + MeteoData::getParameterName(param) + " but it can only be applied to iswr/rswr!!" + getName(), AT);
if( ivec.empty() ) return;
ovec = ivec;
const size_t nr_data = ovec.size();
vector<double> vecAlbedo, vecJulian;
//build the vector of albedos
for (size_t ii=0; ii<nr_data; ii++) {
const double iswr = ovec[ii](MeteoData::ISWR);
const double rswr = ovec[ii](MeteoData::RSWR);
if(iswr<=0. && rswr<=0.) continue;
const double curr_julian = ovec[ii].date.getJulian(true);
const double albedo = (iswr!=IOUtils::nodata && rswr!=IOUtils::nodata)? rswr / (iswr+1e-6) : IOUtils::nodata;
vecJulian.push_back( curr_julian );
if(albedo>0. && albedo<1.)
vecAlbedo.push_back( albedo );
......@@ -63,33 +63,33 @@ void ProcUnshade::process(const unsigned int& param, const std::vector<MeteoData
//filter and reinterpolate the albedo
if( !filterAlbedo(vecJulian, vecAlbedo) ) return;
if( !linInterpolate(vecJulian, vecAlbedo) ) return;
//apply the albedo correction
size_t alb_idx = 0;
for (size_t ii=0; ii<nr_data; ii++) {
double& iswr = ovec[ii](MeteoData::ISWR);
double& rswr = ovec[ii](MeteoData::RSWR);
if(iswr<=0. && rswr<=0.) continue; //skip nights
const double albedo = vecAlbedo[alb_idx];
if(param==MeteoData::ISWR) {
if(rswr!=IOUtils::nodata && albedo!=IOUtils::nodata) {
if(rswr>0. && albedo>0.)
iswr = rswr / albedo;
else
else
iswr = 0.;
}
} else { //on RSWR
if(iswr!=IOUtils::nodata && albedo!=IOUtils::nodata) {
if(iswr>0. && albedo>0.)
rswr = iswr * albedo;
else
else
rswr = 0.;
}
}
alb_idx++;
}
}
......@@ -97,14 +97,14 @@ void ProcUnshade::process(const unsigned int& param, const std::vector<MeteoData
bool ProcUnshade::filterAlbedo(const std::vector<double>& julian, std::vector<double> &data) const
{
if(julian.empty()) return false;
vector<double> vecWindow;
double start_julian = julian[0];
size_t start_idx = 0;
for(size_t ii=0; ii<data.size(); ii++) { //piecewise MAD filter on a data window
if(data[ii]!=IOUtils::nodata) vecWindow.push_back( data[ii] );
const double curr_julian = julian[ii];
if( (curr_julian-start_julian) >= max_gap) {
if( !MADFilter(start_idx, vecWindow, data) ) return false;
......@@ -113,39 +113,33 @@ bool ProcUnshade::filterAlbedo(const std::vector<double>& julian, std::vector<do
start_idx = ii;
}
}
if(!vecWindow.empty())
if( !MADFilter(start_idx, vecWindow, data) ) return false;
return true;
}
bool ProcUnshade::MADFilter(const size_t& start_idx, std::vector<double> &vecWindow, std::vector<double> &data)
{
const double K = 1. / 0.6745;
double mad = IOUtils::nodata;
double median = IOUtils::nodata;
try {
median = Interpol1D::getMedian(vecWindow, false); //we aleady handled nodata
mad = Interpol1D::getMedianAverageDeviation(vecWindow, false);
} catch(const exception&){
return false;
}
const double median = Interpol1D::getMedian(vecWindow, false); //we aleady handled nodata
const double mad = Interpol1D::getMedianAverageDeviation(vecWindow, false);
if( median==IOUtils::nodata || mad==IOUtils::nodata ) return false;
const double sigma = mad * K;
const double lower_lim = median - 3.*sigma;
const double upper_lim = median + 3.*sigma;
for(size_t ii=0; ii<vecWindow.size(); ii++) {
double& value = data[start_idx+ii];
if( value!=IOUtils::nodata && ((value>upper_lim) || (value<lower_lim)) ) {
value = IOUtils::nodata;
}
}
return true;
}
......@@ -155,14 +149,14 @@ void ProcUnshade::interpolFill(const size_t& start_idx, const size_t& end_idx, c
const double x2 = julian[end_idx];
const double y1 = data[start_idx];
const double y2 = data[end_idx];
if (x1 == x2)
throw IOException("Attempted division by zero", AT);
//Solving y = ax + b
const double a = (y2 - y1) / (x2 - x1);
const double b = y2 - a*x2;
for(size_t ii = start_idx+1; ii<end_idx; ii++)
data[ii] = a * julian[ii] + b;
}
......@@ -170,7 +164,7 @@ void ProcUnshade::interpolFill(const size_t& start_idx, const size_t& end_idx, c
bool ProcUnshade::linInterpolate(const std::vector<double>& julian, std::vector<double> &data) const
{
const size_t nr_data = data.size();
//get starting point
size_t start_idx = IOUtils::npos;
for(size_t ii=0; ii<nr_data; ii++) {
......@@ -179,9 +173,9 @@ bool ProcUnshade::linInterpolate(const std::vector<double>& julian, std::vector<
break;
}
}
if(start_idx==IOUtils::npos) return false;
for(size_t ii=start_idx+1; ii<nr_data; ii++) {
if(data[ii]!=IOUtils::nodata) {
if( ii!=start_idx+1 && (julian[ii]-julian[start_idx])<=max_gap )
......@@ -189,7 +183,7 @@ bool ProcUnshade::linInterpolate(const std::vector<double>& julian, std::vector<
start_idx = ii;
}
}
return true;
}
......@@ -198,9 +192,9 @@ void ProcUnshade::parse_args(std::vector<std::string> vec_args)
if (vec_args.size() > 1){
throw InvalidArgumentException("Invalid arguments for filter " + getName(), AT);
}
vector<double> filter_args;
if(vec_args.size()==1) {
IOUtils::convertString( max_gap, vec_args[0]);
max_gap /= 86400.; //convert back to days
......
......@@ -201,8 +201,6 @@ double Interpol1D::weightedMean(const double& d1, const double& d2, const double
double Interpol1D::weightedMean(const std::vector<double>& vecData, const std::vector<double>& weight)
{
const size_t nPts = vecData.size();
if (nPts == 0)
throw NoAvailableDataException("Trying to calculate an arithmetic mean with no data points", AT);
if(nPts != weight.size()) {
std::ostringstream ss;
ss << "Computing weighted mean of a vector of size " << nPts;
......@@ -229,8 +227,6 @@ double Interpol1D::weightedMean(const std::vector<double>& vecData, const std::v
double Interpol1D::arithmeticMean(const std::vector<double>& vecData)
{
const size_t nPts = vecData.size();
if (nPts == 0)
throw NoAvailableDataException("Trying to calculate an arithmetic mean with no data points", AT);
unsigned int count=0;
double sum = 0.0;
......@@ -276,8 +272,7 @@ double Interpol1D::getMedian(const std::vector<double>& vecData, const bool& kee
//This uses a sorting algorithm for getting middle element
//as much more efficient than full sorting (O(n) compared to O(n log(n))
if(keep_nodata) {
if (vecData.empty())
throw NoAvailableDataException("Trying to calculate a median with no data points", AT);
if (vecData.empty()) return IOUtils::nodata;
vector<double> vecTemp;
for(size_t i=0; i<vecData.size(); i++) {
......@@ -294,8 +289,7 @@ double Interpol1D::getMedian(const std::vector<double>& vecData, const bool& kee
double Interpol1D::getMedianAverageDeviation(std::vector<double> vecData, const bool& keep_nodata)
{
if (vecData.empty())
throw NoAvailableDataException("Trying to calculate MAD with no data points", AT);
if (vecData.empty()) return IOUtils::nodata;
const double median = Interpol1D::getMedian(vecData, keep_nodata);
if(median==IOUtils::nodata)
......
......@@ -108,9 +108,10 @@ void Interpol2D::getNeighbors(const double& x, const double& y,
//convert a vector of stations into two vectors of eastings and northings
void Interpol2D::buildPositionsVectors(const std::vector<StationData>& vecStations, std::vector<double>& vecEastings, std::vector<double>& vecNorthings)
{
vecEastings.resize( vecStations.size() );
vecNorthings.resize( vecStations.size() );
for (size_t i=0; i<vecStations.size(); i++) {
const size_t nr_stations = vecStations.size();
vecEastings.resize( nr_stations );
vecNorthings.resize( nr_stations );
for (size_t i=0; i<nr_stations; i++) {
const Coords& position = vecStations[i].position;
vecEastings[i] = position.getEasting();
vecNorthings[i] = position.getNorthing();
......
......@@ -865,18 +865,21 @@ void SMETReader::read_header(std::ifstream& fin)
SMETCommon::stripComments(line);
SMETCommon::readLineToVec(line, tmpvec);
checkSignature(tmpvec, isAscii);
std::cerr << "Line read:" << line << "\n";
//2. Read Header
while (!fin.eof() && (fin.peek() != '[')) //skip lines until '[' is found
while (!fin.eof() && (fin.peek() != '[')) {//skip lines until '[' is found
getline(fin, line, eoln);
std::cerr << "Seek:" << line << "\n";
}
getline(fin, line, eoln);
std::cerr << "Header line read:" << line << "\n";
SMETCommon::stripComments(line);
SMETCommon::trim(line);
SMETCommon::toUpper(line);
if (line != "[HEADER]")
throw;// InvalidFormatException("Section " + line + " in "+ filename + " invalid", AT);
throw SMETException("Read \"" + line + "\" in "+ filename + " when expecting \"[HEADER]\"", AT);
while (!fin.eof() && (fin.peek() != '[')){ //Read until next section
getline(fin, line, eoln);
......
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