MeteoIODoc  MeteoIODoc-2.6.0
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
IOUtils.h
Go to the documentation of this file.
1 /***********************************************************************************/
2 /* Copyright 2009 WSL Institute for Snow and Avalanche Research SLF-DAVOS */
3 /***********************************************************************************/
4 /* This file is part of MeteoIO.
5  MeteoIO is free software: you can redistribute it and/or modify
6  it under the terms of the GNU Lesser General Public License as published by
7  the Free Software Foundation, either version 3 of the License, or
8  (at your option) any later version.
9 
10  MeteoIO is distributed in the hope that it will be useful,
11  but WITHOUT ANY WARRANTY; without even the implied warranty of
12  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13  GNU Lesser General Public License for more details.
14 
15  You should have received a copy of the GNU Lesser General Public License
16  along with MeteoIO. If not, see <http://www.gnu.org/licenses/>.
17 */
18 #ifndef IOUTILS_H
19 #define IOUTILS_H
20 
21 #include <iostream>
22 #include <string>
23 #include <map>
24 #include <vector>
25 #include <cstdlib>
26 #include <limits>
27 #include <cmath>
28 
31 #include <meteoio/IOExceptions.h>
33 
34 namespace mio {
35 
36 #ifdef _MSC_VER
37 double round(const double& x);
38 #endif
39 
40 class MeteoData;
41 class Coords;
42 class Config;
43 
48 std::string getLibVersion();
49 
50 namespace IOUtils {
52  raw = 1,
53  filtered = 1 << 1,
54  resampled = 1 << 2,
55  generated = 1 << 3,
56  num_of_levels = 1 << 4
57  };
58 
60  const double nodata = -999.0;
61  //const double not_set = std::numeric_limits<double>::max()-2.;
62  const unsigned int unodata = static_cast<unsigned int>(-1);
63  const int inodata = -999;
64  const short int snodata = -999;
65  const size_t npos = static_cast<size_t>(-1);
66 
67  const double grid_epsilon = 5.;
69  const double lat_epsilon = lon_epsilon;
70 
71  inline double C_TO_K(const double& T) {return ((T==nodata)? T : T + Cst::t_water_freezing_pt);}
72  inline double K_TO_C(const double& T) {return ((T==nodata)? T : T - Cst::t_water_freezing_pt);}
73 
81  inline bool checkEpsilonEquality(const double& val1, const double& val2, const double& epsilon) {return (fabs(val1-val2) < epsilon);}
82 
92  size_t seek(const Date& soughtdate, const std::vector<MeteoData>& vecM, const bool& exactmatch=true);
93 
99  double bearing_to_angle(const double& bearing);
105  double angle_to_bearing(const double& angle);
111  double bearing(std::string bearing_str);
117  std::string bearing(double bearing);
118 
124  std::string getLogName();
125 
130  std::string getHostName();
131 
136  void trim(std::string &s);
137 
138  void stripComments(std::string& str);
139 
149  bool readKeyValuePair(const std::string& in_line, const std::string& delimiter,
150  std::string &key, std::string &value, const bool& setToUpperCase=false);
151 
152  void toUpper(std::string& str);
153  std::string strToUpper(std::string str);
154  void toLower(std::string& str);
155  std::string strToLower(std::string str);
156  bool isNumeric(std::string input, const unsigned int& nBase=10);
157  size_t readLineToVec(const std::string& line_in, std::vector<double>& vec_data);
158  size_t readLineToVec(const std::string& line_in, std::vector<std::string>& vecString);
159  size_t readLineToVec(const std::string& line_in, std::vector<std::string>& vecString, const char& delim);
160 
170  template <class T> bool convertString(T& t, const std::string& str, std::ios_base& (*f)(std::ios_base&) = std::dec) {
171  std::string s(str);
172  trim(s); //delete trailing and leading whitespaces and tabs
173  if (s.empty()) {
174  t = static_cast<T> (nodata);
175  return true;
176  } else {
177  std::istringstream iss(s);
178  iss.setf(std::ios::fixed);
179  iss.precision(std::numeric_limits<double>::digits10); //try to read values with maximum precision
180  iss >> f >> t; //Convert first part of stream with the formatter (e.g. std::dec, std::oct)
181  if (iss.fail()) {
182  //Conversion failed
183  return false;
184  }
185  std::string tmp;
186  getline(iss, tmp); //get rest of line, if any
187  trim(tmp);
188  if (!tmp.empty() && tmp[0] != '#' && tmp[0] != ';') {
189  //if line holds more than one value it's invalid
190  return false;
191  }
192  return true;
193  }
194  }
195  // fully specialized template functions (implementation must not be in header)
196  template<> bool convertString<double>(double& t, const std::string& str, std::ios_base& (*f)(std::ios_base&));
197  template<> bool convertString<std::string>(std::string& t, const std::string& str, std::ios_base& (*f)(std::ios_base&));
198  template<> bool convertString<bool>(bool& t, const std::string& str, std::ios_base& (*f)(std::ios_base&));
199  template<> bool convertString<unsigned int>(unsigned int& t, const std::string& str, std::ios_base& (*f)(std::ios_base&));
200  template<> bool convertString<Coords>(Coords& t, const std::string& str, std::ios_base& (*f)(std::ios_base&));
201 
202  bool convertString(Date& t, const std::string& str, const double& time_zone, std::ios_base& (*f)(std::ios_base&) = std::dec);
203 
212  template <class T> void getValueForKey(const std::map<std::string,std::string>& properties,
213  const std::string& key, T& t, const ThrowOptions& options=IOUtils::dothrow){
214  if (key.empty() && options!=IOUtils::nothrow)
215  throw InvalidArgumentException("Empty key", AT);
216 
217  const std::map<std::string, std::string>::const_iterator it = properties.find(key);
218 
219  if (it == properties.end()){
220  if (options == IOUtils::nothrow)
221  return;
222  else
223  throw UnknownValueException("No value for key " + key, AT);
224  }
225  const std::string& value = it->second;
226 
227  if (!convertString<T>(t, value, std::dec) && options!=IOUtils::nothrow) {
228  std::cerr << "[E] When reading \"" << key << "\" = \"" << t << "\"\n";
229  throw ConversionFailedException(value, AT);
230  }
231  }
232 
241  template <class T> void getValueForKey(const std::map<std::string,std::string>& properties,
242  const std::string& key, std::vector<T>& vecT, const ThrowOptions& options=IOUtils::dothrow)
243  {
244  if (key.empty() && options!=IOUtils::nothrow)
245  throw InvalidArgumentException("Empty key", AT);
246 
247  const std::map<std::string, std::string>::const_iterator it = properties.find(key);
248  if (it == properties.end()) {
249  if (options == IOUtils::nothrow) {
250  return;
251  } else {
252  throw UnknownValueException("No value for key " + key, AT);
253  }
254  }
255  const std::string& value = it->second;
256 
257  //split value string
258  std::vector<std::string> vecUnconvertedValues;
259  const size_t counter = readLineToVec(value, vecUnconvertedValues);
260  for (size_t ii=0; ii<counter; ii++){
261  T myvar;
262  if (!convertString<T>(myvar, vecUnconvertedValues.at(ii), std::dec) && options!=IOUtils::nothrow){
263  std::cerr << "[E] When reading \"" << key << "\" = \"" << myvar << "\"\n";
264  throw ConversionFailedException(vecUnconvertedValues.at(ii), AT);
265  }
266  vecT.push_back(myvar);
267  }
268  }
269 
277  template <class T> T standardizeNodata(const T& value, const double& plugin_nodata) {
278  if (value==plugin_nodata) return static_cast<T> (nodata);
279  else return value;
280  }
281 
291  void getProjectionParameters(const Config& cfg, std::string& coordin, std::string& coordinparam,
292  std::string& coordout, std::string& coordoutparam);
293 
300  void getTimeZoneParameters(const Config& cfg, double& tz_in, double& tz_out);
301 
312  void getArraySliceParams(const size_t& dimx, const size_t& nbworkers, const size_t &wk, size_t& startx, size_t& nx);
313 
319  double unitsPrefix(const char& prefix);
320 
329  double unitsConversion(const double& val, std::string unitIn, std::string unitOut);
330 } //end namespace IOUtils
331 
332 } //end namespace mio
333 
334 #endif
std::string getLibVersion()
Return the library version.
Definition: IOUtils.cc:54
double angle_to_bearing(const double &angle)
Converts a trigonometric angle to a compass bearing.
Definition: IOUtils.cc:66
size_t readLineToVec(const std::string &line_in, std::vector< double > &vec_data)
Definition: IOUtils.cc:246
bool isNumeric(std::string str, const unsigned int &nBase)
Definition: IOUtils.cc:166
const int inodata
Definition: IOUtils.h:63
ThrowOptions
Definition: IOUtils.h:59
Definition: IOUtils.h:59
double K_TO_C(const double &T)
Definition: IOUtils.h:72
const double to_deg
Definition: Meteoconst.h:80
void toLower(std::string &str)
Definition: IOUtils.cc:150
bool convertString< bool >(bool &t, const std::string &str, std::ios_base &(*f)(std::ios_base &))
Definition: IOUtils.cc:312
bool convertString(Date &t, const std::string &str, const double &time_zone, std::ios_base &(*f)(std::ios_base &))
Definition: IOUtils.cc:419
double unitsPrefix(const char &prefix)
Convert a textual representation of a unit prefix (like 'm' or 'G') to multiplying factor...
Definition: IOUtils.cc:664
std::string strToLower(std::string str)
Definition: IOUtils.cc:161
thrown when encountered an unexpected value (e.g. unknown name or key)
Definition: IOExceptions.h:144
void getTimeZoneParameters(const Config &cfg, double &tz_in, double &tz_out)
A function that parses a Config object for the time_zone keyword and returns the timezone.
Definition: IOUtils.cc:581
double unitsConversion(const double &val, std::string unitIn, std::string unitOut)
Performs simple unit conversion (supports temperature, prefixes and exponents) NOTE "composite" uni...
Definition: IOUtils.cc:689
bool readKeyValuePair(const std::string &in_line, const std::string &delimiter, std::string &key, std::string &value, const bool &setToUpperCase)
read a string line, parse it and save it into a map object, that is passed by reference ...
Definition: IOUtils.cc:186
Definition: IOUtils.h:56
Definition: IOUtils.h:54
Definition: IOUtils.h:55
T standardizeNodata(const T &value, const double &plugin_nodata)
Standardize a given value to use MeteoIO's internal nodata value (if applicable)
Definition: IOUtils.h:277
A class to handle geographic coordinate systems. This class offers an easy way to transparently conve...
Definition: Coords.h:81
void getProjectionParameters(const Config &cfg, std::string &coordin, std::string &coordinparam, std::string &coordout, std::string &coordoutparam)
A function that parses a Config object for COORSYS, COORDPARAM keywords in [Input] and [Output] secti...
Definition: IOUtils.cc:573
void trim(std::string &str)
Removes trailing and leading whitespaces, tabs and newlines from a string.
Definition: IOUtils.cc:131
bool convertString< Coords >(Coords &t, const std::string &str, std::ios_base &(*f)(std::ios_base &))
Definition: IOUtils.cc:555
bool checkEpsilonEquality(const double &val1, const double &val2, const double &epsilon)
Check whether two values are equal regarding a certain epsilon environment (within certain radius of ...
Definition: IOUtils.h:81
const size_t npos
npos is the out-of-range value
Definition: IOUtils.h:65
double bearing_to_angle(const double &bearing)
Converts a compass bearing to a trigonometric angle.
Definition: IOUtils.cc:62
Definition: IOUtils.h:52
void stripComments(std::string &str)
Definition: IOUtils.cc:123
const double grid_epsilon
What is an acceptable small distance on a grid, in meters.
Definition: IOUtils.h:67
ProcessingLevel
Definition: IOUtils.h:51
bool convertString< double >(double &t, const std::string &str, std::ios_base &(*f)(std::ios_base &))
Definition: IOUtils.cc:342
std::string getLogName()
Retrieve the user name This checks various environment variables (USERNAME, USER, LOGNAME)...
Definition: IOUtils.cc:212
const double earth_R0
Definition: Meteoconst.h:61
void toUpper(std::string &str)
Definition: IOUtils.cc:146
Definition: IOUtils.h:59
Definition: IOUtils.h:53
void getArraySliceParams(const size_t &dimx, const size_t &nbworkers, const size_t &wk, size_t &startx, size_t &nx)
Returns the parameters for splitting an array in several, balanced sub-arrays. This is mostly usefull...
Definition: IOUtils.cc:644
long int round(const double &x)
Optimized version of c++ round() This version works with positive and negative numbers but does not c...
Definition: MathOptim.h:44
A class that reads a key/value file. These files (typically named *.ini) follow the INI file format s...
Definition: Config.h:58
const double t_water_freezing_pt
Definition: Meteoconst.h:49
thrown when an unsuccessful to convert data types/classes is made (e.g. attempt to convert a literal ...
Definition: IOExceptions.h:120
std::string getHostName()
Retrieve the name of the computer running the binary.
Definition: IOUtils.cc:225
#define AT
Definition: IOExceptions.h:29
const double lon_epsilon
in degrees. Small angle for longitudes, so sin(x)=x
Definition: IOUtils.h:68
thrown when encountered an unexpected function's argument (e.g. bad index, bad or missing parameter n...
Definition: IOExceptions.h:132
const double nodata
This is the internal nodata value.
Definition: IOUtils.h:60
void getValueForKey(const std::map< std::string, std::string > &properties, const std::string &key, T &t, const ThrowOptions &options=IOUtils::dothrow)
Returns, with the requested type, the value associated to a key (template function).
Definition: IOUtils.h:212
A class to handle timestamps. This class handles conversion between different time display formats (I...
Definition: Date.h:79
double C_TO_K(const double &T)
Definition: IOUtils.h:71
std::string strToUpper(std::string str)
Definition: IOUtils.cc:154
double bearing(std::string bearing_str)
Converts a string bearing to a compass bearing.
Definition: IOUtils.cc:70
const short int snodata
Definition: IOUtils.h:64
size_t seek(const Date &soughtdate, const std::vector< MeteoData > &vecM, const bool &exactmatch)
Search for an element at a given date in a vector of MeteoData. The position of the matching date is ...
Definition: IOUtils.cc:586
bool convertString< unsigned int >(unsigned int &t, const std::string &str, std::ios_base &(*f)(std::ios_base &))
Definition: IOUtils.cc:394
const double lat_epsilon
in degrees. Small angle for latitudes.
Definition: IOUtils.h:69
const unsigned int unodata
Definition: IOUtils.h:62