WSL/SLF GitLab Repository

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

Now, when libsmet attemps to write a station that does not contain any data,...

Now, when libsmet attemps to write a station that does not contain any data, it will complain but will not throw an exception (otherwise when working with large numbers of stations, the odds of getting an exception are too high). An implementation for fast cubic root is now part of Optim, but still has to be tested. Some code cleanup (using .empty(), using copy constructor instead of construct+copy) occured in IOUtils.
parent debf5b77
......@@ -450,10 +450,10 @@ size_t readLineToVec(const std::string& line_in, std::vector<std::string>& vecSt
while (!iss.eof()) {
iss >> std::skipws >> tmp_string;
if (tmp_string != "") {
if (!tmp_string.empty()) {
vecString.push_back(tmp_string);
tmp_string.clear();
}
tmp_string.clear();
}
return vecString.size();
......@@ -480,7 +480,7 @@ const char NUM[] = "0123456789";
template<> bool convertString<std::string>(std::string& t, const std::string& str, std::ios_base& (*f)(std::ios_base&))
{
(void)f;
std::string s = str;
std::string s(str);
trim(s); //delete trailing and leading whitespaces and tabs
t = s;
......@@ -489,7 +489,7 @@ template<> bool convertString<std::string>(std::string& t, const std::string& st
template<> bool convertString<bool>(bool& t, const std::string& str, std::ios_base& (*f)(std::ios_base&))
{
std::string s = str;
std::string s(str);
trim(s); //delete trailing and leading whitespaces and tabs
if (toupper(s[0])=='T' || toupper(s[0])=='Y' ) {
......@@ -510,7 +510,7 @@ template<> bool convertString<bool>(bool& t, const std::string& str, std::ios_ba
if (pos != std::string::npos) {
std::string tmp = s.substr(pos);
trim(tmp);
if ((tmp.length() > 0) && tmp[0] != '#' && tmp[0] != ';') {//if line holds more than one value it's invalid
if (!tmp.empty() && tmp[0] != '#' && tmp[0] != ';') {//if line holds more than one value it's invalid
return false;
}
}
......@@ -520,9 +520,9 @@ template<> bool convertString<bool>(bool& t, const std::string& str, std::ios_ba
template<> bool convertString<unsigned int>(unsigned int& t, const std::string& str, std::ios_base& (*f)(std::ios_base&))
{
std::string s = str;
std::string s(str);
trim(s); //delete trailing and leading whitespaces and tabs
if (s.size() == 0) {
if (s.empty()) {
t = unodata;
return true;
} else {
......@@ -537,7 +537,7 @@ template<> bool convertString<unsigned int>(unsigned int& t, const std::string&
std::string tmp;
getline(iss, tmp); //get rest of line, if any
trim(tmp);
if ((tmp.length() > 0) && tmp[0] != '#' && tmp[0] != ';') {
if (!tmp.empty() && tmp[0] != '#' && tmp[0] != ';') {
//if line holds more than one value it's invalid
return false;
}
......@@ -547,7 +547,7 @@ template<> bool convertString<unsigned int>(unsigned int& t, const std::string&
bool convertString(Date& t, const std::string& str, const double& time_zone, std::ios_base& (*f)(std::ios_base&))
{
std::string s = str;
std::string s(str);
trim(s); //delete trailing and leading whitespaces and tabs
(void)f;
......@@ -605,7 +605,7 @@ bool convertString(Date& t, const std::string& str, const double& time_zone, std
std::string tmp(rest);
trim(tmp);
if ((tmp.length() > 0) && tmp[0] != '#' && tmp[0] != ';') {//if line holds more than one value it's invalid
if (!tmp.empty() && tmp[0] != '#' && tmp[0] != ';') {//if line holds more than one value it's invalid
return false;
}
......@@ -614,7 +614,7 @@ bool convertString(Date& t, const std::string& str, const double& time_zone, std
template<> bool convertString<Coords>(Coords& t, const std::string& str, std::ios_base& (*f)(std::ios_base&))
{
std::string s = str;
std::string s(str);
trim(s); //delete trailing and leading whitespaces and tabs
(void)f;
......@@ -677,9 +677,6 @@ size_t seek(const Date& soughtdate, const std::vector<MeteoData>& vecM, const bo
//if we reach this point: the date is spanned by the buffer and there are at least two elements
if (exactmatch){
//first = 1; last = vecM.size()-1;
//size_t first = 1, last = vecM.size()-1;
//perform binary search
while (first <= last) {
const size_t mid = (first + last) / 2; // compute mid point
......@@ -691,7 +688,6 @@ size_t seek(const Date& soughtdate, const std::vector<MeteoData>& vecM, const bo
return mid; // found it. return position
}
} else {
//first = 0; last = vecM.size()-1;
//perform binary search
while (first <= last) {
const size_t mid = (first + last) / 2; // compute mid point
......@@ -706,7 +702,6 @@ size_t seek(const Date& soughtdate, const std::vector<MeteoData>& vecM, const bo
last = mid - 1; // repeat search in bottom half
else
return mid; // found it. return position
}
}
......@@ -753,7 +748,7 @@ void FileIndexer::setIndex(const Date& i_date, const std::streampos& i_pos)
const file_index elem(i_date, i_pos);
//check if we can simply append the new index
if(vec_size==0 || elem>vecIndex[vec_size-1]) {
if(vecIndex.empty() || elem>vecIndex[vec_size-1]) {
vecIndex.push_back(elem);
return;
}
......@@ -802,7 +797,7 @@ std::streampos FileIndexer::getIndex(const double& i_date) const
size_t FileIndexer::binarySearch(const Date& soughtdate) const
{//perform binary search, return the first element that is GREATER than the provided value
const size_t vec_size = vecIndex.size();
if(vec_size==0) return static_cast<size_t>(-1);
if(vecIndex.empty()) return static_cast<size_t>(-1);
if(soughtdate<vecIndex[0].date) return static_cast<size_t>(-1);
if(soughtdate>=vecIndex[vec_size-1].date) return vec_size-1;
......
......@@ -162,6 +162,49 @@ namespace Optim {
}
}
//see http://metamerist.com/cbrt/cbrt.htm
template <int n>
inline float nth_rootf(float x) {
const int ebits = 8;
const int fbits = 23;
int& i = (int&) x;
const int bias = (1 << (ebits-1))-1;
i = (i - (bias << fbits)) / n + (bias << fbits);
return x;
}
template <int n>
inline double nth_rootd(double x) {
const int ebits = 11;
const int fbits = 52;
long long& i = (long long&) x;
const long long bias = (1 << (ebits-1))-1;
i = (i - (bias << fbits)) / n + (bias << fbits);
return x;
}
/**
* @brief Optimized version of cubic root
* This version is based on a single iteration Halley's method (see https://en.wikipedia.org/wiki/Halley%27s_method)
* with a seed provided by a bit hack approximation. It should offer 15-16 bits precision and be three times
* faster than pow(x, 1/3).
* Source: http://metamerist.com/cbrt/cbrt.htm
*
* Please benchmark your code before deciding to use this!!
* @param x argument
* @return x^(1/3)
*/
inline double cbrt(double x) {
const double a = nth_rootd<3>(x);
const double a3 = a*a*a;
const double b = a * ( a3 + x + x) / ( a3 + a3 + x );
return b; //single iteration, otherwise set a=b and do it again
}
}
} //end namespace
......
......@@ -342,7 +342,7 @@ bool SMETWriter::check_fields(const std::string& key, const std::string& value)
nr_of_fields = counter;
}
if (key == "fields"){
if (key == "fields"){
size_t count_wgs84 = 0, count_epsg = 0;
//check if location is in data and if timestamp is present
......@@ -434,12 +434,13 @@ void SMETWriter::write(const std::vector<std::string>& vec_timestamp, const std:
const size_t nr_of_data_fields = nr_of_fields - 1;
if(nr_of_fields<=1) { //avoid division by zero in the next block
throw SMETException("Attempting to write a dataset that contains no fields to file \"" + filename + "\"!", SMET_AT);
cerr << "[W] Attempting to write a dataset that contains no fields to file \"" + filename + "\"!\n";
return;
}
const size_t nr_of_lines = data.size() / (nr_of_fields-1);
if ((nr_of_lines != vec_timestamp.size()) || ((data.size() % (nr_of_fields-1)) != 0))
throw SMETException("Inconsistency between vec_timestamp and data detected for file \""+filename+"\", recheck your data", SMET_AT);
throw SMETException("Inconsistency between the number of timestamp and number of data points detected for file \""+filename+"\", recheck your data", SMET_AT);
std::vector<double> current_data(nr_of_fields-1);
check_formatting();
......
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