OGS
SensorData.cpp
Go to the documentation of this file.
1
15#include "SensorData.h"
16
17#include <algorithm>
18#include <cstdlib>
19#include <fstream>
20
21#include "BaseLib/DateTools.h"
22#include "BaseLib/Logging.h"
23#include "BaseLib/StringTools.h"
24
25SensorData::SensorData(const std::string& file_name)
26 : _start(0), _end(0), _step_size(0), _time_unit(TimeStepType::NONE)
27{
28 readDataFromFile(file_name);
29}
30
31SensorData::SensorData(std::vector<std::size_t> time_steps)
32 : _start(time_steps.front()),
33 _end(time_steps.back()),
34 _step_size(0),
35 _time_unit(TimeStepType::NONE),
36 _time_steps(time_steps)
37{
38 if (!std::is_sorted(
39 time_steps.begin(), time_steps.end(), std::less_equal{}))
40 {
41 ERR("Error in SensorData() - Time series has no order!");
42 }
43}
44
46{
47 for (std::vector<float>* vec : _data_vecs)
48 {
49 delete vec;
50 }
51}
52
53void SensorData::addTimeSeries(const std::string& data_name,
54 std::vector<float>* data,
55 const std::string& data_unit_string)
56{
58 data,
59 data_unit_string);
60}
61
63 std::vector<float>* data,
64 const std::string& data_unit_string)
65{
66 if (_step_size > 0)
67 {
68 if (((_end - _start) / _step_size) != data->size())
69 {
70 WARN(
71 "Warning in SensorData::addTimeSeries() - Lengths of time "
72 "series does not match number of time steps.");
73 return;
74 }
75 }
76 else
77 {
78 if (data->size() != _time_steps.size())
79 {
80 WARN(
81 "Warning in SensorData::addTimeSeries() - Lengths of time "
82 "series does not match number of time steps.");
83 return;
84 }
85 }
86
87 _vec_names.push_back(data_name);
88 _data_vecs.push_back(data);
89 _data_unit_string.push_back(data_unit_string);
90}
91
92const std::vector<float>* SensorData::getTimeSeries(
93 SensorDataType time_series_name) const
94{
95 for (std::size_t i = 0; i < _vec_names.size(); i++)
96 {
97 if (time_series_name == _vec_names[i])
98 {
99 return _data_vecs[i];
100 }
101 }
102 ERR("Error in SensorData::getTimeSeries() - Time series '{:s}' not found.",
103 convertSensorDataType2String(time_series_name));
104 return nullptr;
105}
106
107int SensorData::readDataFromFile(const std::string& file_name)
108{
109 std::ifstream in(file_name.c_str());
110
111 if (!in.is_open())
112 {
113 INFO("SensorData::readDataFromFile() - Could not open file {:s}.",
114 file_name);
115 return 0;
116 }
117
118 std::string line;
119
120 /* first line contains field names */
121 std::getline(in, line);
122 std::list<std::string> fields = BaseLib::splitString(line, '\t');
123 std::list<std::string>::const_iterator it(fields.begin());
124 std::size_t const nFields = fields.size();
125
126 if (nFields < 2)
127 {
128 return 0;
129 }
130
131 std::size_t const nDataArrays(nFields - 1);
132
133 // create vectors necessary to hold the data
134 for (std::size_t i = 0; i < nDataArrays; i++)
135 {
137 _data_unit_string.emplace_back("");
138 _data_vecs.push_back(new std::vector<float>);
139 }
140
141 while (std::getline(in, line))
142 {
143 fields = BaseLib::splitString(line, '\t');
144
145 if (nFields != fields.size())
146 {
147 return 0;
148 }
149
150 it = fields.begin();
151 std::size_t const pos(it->rfind("."));
152 std::size_t const current_time_step = (pos == std::string::npos)
153 ? atoi((it++)->c_str())
154 : BaseLib::strDate2int(*it++);
155 _time_steps.push_back(current_time_step);
156
157 for (std::size_t i = 0; i < nDataArrays; i++)
158 {
159 _data_vecs[i]->push_back(
160 static_cast<float>(strtod((it++)->c_str(), nullptr)));
161 }
162 }
163
164 in.close();
165
166 _start = _time_steps[0];
167 _end = _time_steps[_time_steps.size() - 1];
168
169 return 1;
170}
171
173{
175 {
176 return "Evaporation";
177 }
179 {
180 return "Precipitation";
181 }
183 {
184 return "Temperature";
185 }
186 // pls leave this as last choice
187 return "Unknown";
188}
189
191{
192 if (s == "Evaporation" || s == "EVAPORATION")
193 {
195 }
196 if (s == "Precipitation" || s == "PRECIPITATION")
197 {
199 }
200 if (s == "Temperature" || s == "TEMPERATURE")
201 {
203 }
205}
Definition of date helper functions.
void INFO(fmt::format_string< Args... > fmt, Args &&... args)
Definition Logging.h:35
void ERR(fmt::format_string< Args... > fmt, Args &&... args)
Definition Logging.h:45
void WARN(fmt::format_string< Args... > fmt, Args &&... args)
Definition Logging.h:40
Definition of the SensorData class.
TimeStepType
Definition SensorData.h:43
SensorDataType
Definition SensorData.h:28
Definition of string helper functions.
const std::vector< float > * getTimeSeries(SensorDataType time_series_name) const
Returns the time series with the given name.
std::size_t _end
Definition SensorData.h:127
std::vector< SensorDataType > _vec_names
Definition SensorData.h:132
std::vector< std::vector< float > * > _data_vecs
Definition SensorData.h:133
SensorData(const std::string &file_name)
std::size_t _start
Definition SensorData.h:126
std::vector< std::string > _data_unit_string
Definition SensorData.h:130
std::vector< std::size_t > _time_steps
Definition SensorData.h:131
void addTimeSeries(const std::string &data_name, std::vector< float > *data, const std::string &data_unit_string="")
std::size_t _step_size
Definition SensorData.h:128
int readDataFromFile(const std::string &file_name)
Reads a CSV-file with time series data and fills the container.
static SensorDataType convertString2SensorDataType(const std::string &s)
Converts Strings to Sensor Data Types.
static std::string convertSensorDataType2String(SensorDataType t)
Converts Sensor Data Types to Strings.
int strDate2int(const std::string &s)
std::vector< std::string > splitString(std::string const &str)