MeteoIODoc  MeteoIODoc-2.6.0
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
libsmet.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 LIBSMET_H
19 #define LIBSMET_H
20 
21 #include <meteoio/FileUtils.h>
22 
23 #include <string>
24 #include <iostream>
25 #include <vector>
26 #include <set>
27 #include <map>
28 
29 #define SMET_STRINGIFY(x) #x
30 #define SMET_TOSTRING(x) SMET_STRINGIFY(x)
31 #define SMET_AT __FILE__ ":" SMET_TOSTRING(__LINE__)
32 
33 namespace smet {
34 
37 
43 class SMETException : public std::exception {
44  public:
45  SMETException(const std::string& message="SMETException occured", const std::string& position="");
46  ~SMETException() throw();
47  const char* what() const throw();
48 
49  protected:
50  std::string msg;
51 };
52 
58 class SMETCommon {
59  public:
60  static bool validFileAndPath(const std::string& filename);
61  static void copy_file(const std::string& src, const std::string& dest);
62  static bool fileExists(const std::string& filename);
63  static double convert_to_double(const std::string& in_string);
64  static int convert_to_int(const std::string& in_string);
65  static void stripComments(std::string& str);
66  static char getEoln(std::istream& fin);
67  static void trim(std::string& str);
68  static void toUpper(std::string& str);
69  static bool readKeyValuePair(const std::string& in_line, const std::string& delimiter,
70  std::map<std::string,std::string>& out_map);
71  static size_t readLineToVec(const std::string& line_in, std::vector<std::string>& vec_string);
72  static bool is_decimal(const std::string& value);
73 
74  public:
75  static std::set<std::string> all_optional_header_keys;
76  static std::set<std::string> all_decimal_header_values;
77  static std::set<std::string> all_mandatory_header_keys;
78  static const char* smet_version;
79 
80  private:
81  static const bool __init;
82  static bool initStaticData();
83 };
84 
94 class SMETWriter {
95  public:
101  SMETWriter(const std::string& in_filename, const SMETType& in_type=ASCII);
102 
110  SMETWriter(const std::string& in_filename, const std::string& in_fields, const double& in_nodata);
111 
117  void set_header_value(const std::string& key, const std::string& value);
118 
124  void set_header_value(const std::string& key, const double& value);
125 
134  void write(const std::vector<std::string>& vec_timestamp, const std::vector<double>& data);
135 
141  void write(const std::vector<double>& data);
142 
149  void set_precision(const std::vector<int>& vec_precision);
150 
157  void set_width(const std::vector<int>& vec_width);
158 
159  const std::string toString() const;
160 
161  private:
162  void print_if_exists(const std::string& header_field, std::ofstream& fout) const;
163  void write_header(std::ofstream& fout); //only writes when all necessary header values are set
164  void write_data_line_ascii(const std::string& timestamp, const std::vector<double>& data, std::ofstream& fout);
165  void write_data_line_binary(const std::vector<double>& data, std::ofstream& fout);
166  bool check_fields(const std::string& key, const std::string& value);
167  void check_formatting();
168  bool valid_header_pair(const std::string& key, const std::string& value);
169  bool valid_header();
170 
171  std::vector<std::string> other_header_keys; //this vector is used to preserve the sequence of header keys
172  std::vector<int> ascii_precision, ascii_width;
173  std::map< std::string, std::string > header;
174  std::set<std::string> mandatory_header_keys;
175 
176  std::string filename;
177  std::string nodata_string;
178  SMETType smet_type;
179  double nodata_value;
180  size_t nr_of_fields, julian_field, timestamp_field;
181  char location_wgs84, location_epsg;
182  bool location_in_header, location_in_data_wgs84, location_in_data_epsg;
183  bool timestamp_present, julian_present;
184  bool file_is_binary, append_mode, append_possible;
185 };
186 
195 class SMETReader {
196  public:
197  friend class SMETWriter; //so the writer can call the reader for handling append mode
198 
203  SMETReader(const std::string& in_fname);
205 
214  void read(const std::string& timestamp_start, const std::string& timestamp_end,
215  std::vector<std::string>& vec_timestamp, std::vector<double>& vec_data);
216 
224  void read(const double& julian_start, const double& julian_end, std::vector<double>& vec_data);
225 
231  void read(std::vector<std::string>& vec_timestamp, std::vector<double>& vec_data);
232 
237  void read(std::vector<double>& vec_data);
238 
244  std::string get_header_value(const std::string& key) const;
245 
251  double get_header_doublevalue(const std::string& key) const;
252 
258  int get_header_intvalue(const std::string& key) const;
259 
264  bool contains_timestamp() const;
265 
271  std::string get_field_name(const size_t& nr_of_field);
272 
278  bool location_in_header(const LocationType& type) const;
279 
285  bool location_in_data(const LocationType& type) const;
286 
291  size_t get_nr_of_fields() const;
292 
301  void get_units_conversion(std::vector<double>& offset, std::vector<double>& multiplier) const;
302 
308  void convert_to_MKSA(const bool& in_mksa);
309 
314  std::string get_filename() const;
315 
316  private:
317  void truncate_file(const std::string& date_stop) const;
318  void copy_file_header(std::ifstream& fin, std::ofstream& fout) const;
319  void copy_file_data(const std::string& date_stop, std::ifstream& fin, std::ofstream& fout) const;
320  std::string getLastTimestamp() const;
321  void read_data_ascii(std::ifstream& fin, std::vector<std::string>& vec_timestamp, std::vector<double>& vec_data);
322  void read_data_binary(std::ifstream& fin, std::vector<double>& vec_data);
323  void cleanup(std::ifstream& fin) throw();
324  void checkSignature(const std::vector<std::string>& vecSignature, bool& o_isAscii);
325  void read_header(std::ifstream& fin);
326  void process_header();
327 
328  std::streampos data_start_fpointer;
329 
330  std::vector<double> vec_offset; //an offset for every column, except timestamp
331  std::vector<double> vec_multiplier; //a multiplier for every column, except timestamp
332  std::vector<std::string> vec_fieldnames; //holds the column names, except for timestamp column
333  std::map< std::string, std::string > header; //holds the header
334  mio::FileUtils::FileIndexer indexer; //in order to save file pointers
335 
336  std::string filename;
337  std::string timestamp_start, timestamp_end; //the beginning and end date of the current timestamp_interval
338  double nodata_value; //The nodata value as seen in the header section of the SMET file
339  double julian_start, julian_end; //the beginning and end date of the current julian_interval
340  static const size_t streampos_every_n_lines; //save current stream pos every n lines of data
341  size_t nr_of_fields; //is always the number of fields minus the timestamp field, if present
342  size_t timestamp_field, julian_field; //index of the timestamp and julian column, if present
343  char location_wgs84, location_epsg, location_data_wgs84, location_data_epsg;
344  char eoln; //end of line character for this file
345  bool timestamp_present, julian_present;
346  bool isAscii; //true if the file is in SMET ASCII format, false if it is in binary format
347  bool mksa; //true if MKSA converted values have to be returned
348  bool timestamp_interval, julian_interval; //true if data shall only be read for a time interval
349 };
350 
351 }
352 
353 #endif
static double convert_to_double(const std::string &in_string)
Definition: libsmet.cc:153
bool location_in_header(const LocationType &type) const
Check whether location information is written in the header.
Definition: libsmet.cc:878
const std::string toString() const
Definition: libsmet.cc:372
bool location_in_data(const LocationType &type) const
Check whether location information is written in the data section.
Definition: libsmet.cc:889
void get_units_conversion(std::vector< double > &offset, std::vector< double > &multiplier) const
Get the unit conversion (offset and multiplier) that are used for this SMET object If the fields unit...
Definition: libsmet.cc:872
~SMETException()
Definition: libsmet.cc:48
void convert_to_MKSA(const bool &in_mksa)
Set whether the values returned should be converted according to unit_offset and multiplier or whethe...
Definition: libsmet.cc:850
Definition: libsmet.h:36
bool contains_timestamp() const
Check whether timestamp is a part of the fields.
Definition: libsmet.cc:1516
void set_precision(const std::vector< int > &vec_precision)
Set precision for each field (except timestamp), otherwise a default precision of 3 is used for each ...
Definition: libsmet.cc:800
SMETWriter(const std::string &in_filename, const SMETType &in_type=ASCII)
The constructor allows to set the filename, the type and whether the file should be gzipped...
Definition: libsmet.cc:297
std::string msg
Definition: libsmet.h:50
A basic exception class adjusted for the needs of the SMET library.
Definition: libsmet.h:43
SMETReader(const std::string &in_fname)
A constructor that will immediately parse the header of the SMET file.
Definition: libsmet.cc:810
static std::set< std::string > all_decimal_header_values
Definition: libsmet.h:76
The SMETWriter class that enables to write a SMET formatted file. The user constructs a SMETWriter cl...
Definition: libsmet.h:94
Definition: libsmet.h:36
static const char * smet_version
Definition: libsmet.h:78
static size_t readLineToVec(const std::string &line_in, std::vector< std::string > &vec_string)
Definition: libsmet.cc:276
The SMETReader class enables to read a SMET formatted file. Data and header info can be extracted thr...
Definition: libsmet.h:195
Definition: FileUtils.h:129
double get_header_doublevalue(const std::string &key) const
Get a double value for a header key in a SMET file.
Definition: libsmet.cc:1489
LocationType
Definition: libsmet.h:36
void write(const std::vector< std::string > &vec_timestamp, const std::vector< double > &data)
Write a SMET file, providing ASCII ISO formatted timestamps and data.
Definition: libsmet.cc:546
static void copy_file(const std::string &src, const std::string &dest)
Definition: libsmet.cc:132
static std::set< std::string > all_optional_header_keys
Definition: libsmet.h:75
~SMETReader()
Definition: libsmet.h:204
std::string get_filename() const
Retrieve the filename that this reader operates upon.
Definition: libsmet.cc:1521
size_t get_nr_of_fields() const
Get number of fields (=columns) in SMET file (timestamp excluded)
Definition: libsmet.cc:867
static void trim(std::string &str)
Definition: libsmet.cc:174
std::string get_header_value(const std::string &key) const
Get a string value for a header key in a SMET file.
Definition: libsmet.cc:1507
static bool fileExists(const std::string &filename)
Definition: libsmet.cc:101
SMETType
Definition: libsmet.h:35
Definition: libsmet.h:35
void set_width(const std::vector< int > &vec_width)
Set width for each field (except timestamp), otherwise a default width of 8 is used for each column...
Definition: libsmet.cc:795
A static class to provide basic operations and variables for the libsmet library. ...
Definition: libsmet.h:58
void set_header_value(const std::string &key, const std::string &value)
Set a key, value pair in the SMET header (both strings)
Definition: libsmet.cc:409
Definition: libsmet.h:35
static void toUpper(std::string &str)
Definition: libsmet.cc:229
static bool validFileAndPath(const std::string &filename)
Definition: libsmet.cc:116
std::string get_field_name(const size_t &nr_of_field)
Get a name for a certain column in the SMET file.
Definition: libsmet.cc:855
const char * what() const
Definition: libsmet.cc:50
static std::set< std::string > all_mandatory_header_keys
Definition: libsmet.h:77
static int convert_to_int(const std::string &in_string)
Definition: libsmet.cc:165
SMETException(const std::string &message="SMETException occured", const std::string &position="")
Definition: libsmet.cc:45
void read(const std::string &timestamp_start, const std::string &timestamp_end, std::vector< std::string > &vec_timestamp, std::vector< double > &vec_data)
Read the data in a SMET file for a given interval of time if no timestamp is present in the file...
Definition: libsmet.cc:1153
static char getEoln(std::istream &fin)
Definition: libsmet.cc:234
static bool is_decimal(const std::string &value)
Definition: libsmet.cc:267
int get_header_intvalue(const std::string &key) const
Get an int value for a header key in a SMET file.
Definition: libsmet.cc:1498
static bool readKeyValuePair(const std::string &in_line, const std::string &delimiter, std::map< std::string, std::string > &out_map)
Definition: libsmet.cc:189
static void stripComments(std::string &str)
Definition: libsmet.cc:221