OGS
SensorData Class Referencefinal

Detailed Description

A container for sensor data at an observation site. The class stores a number of time series and has been created for use in Station-objects.

See also
Station

Definition at line 51 of file SensorData.h.

#include <SensorData.h>

Public Member Functions

 SensorData (const std::string &file_name)
 SensorData (std::vector< std::size_t > time_steps)
 ~SensorData ()
void addTimeSeries (const std::string &data_name, std::vector< float > *data, const std::string &data_unit_string="")
void addTimeSeries (SensorDataType data_name, std::vector< float > *data, const std::string &data_unit_string="")
const std::vector< float > * getTimeSeries (SensorDataType time_series_name) const
 Returns the time series with the given name.
const std::vector< SensorDataType > & getTimeSeriesNames () const
 Returns all time series names contained in this container.
const std::vector< std::size_t > & getTimeSteps () const
 Returns the time step vector (if it exists)
std::size_t getStartTime () const
 Returns the first time step.
std::size_t getEndTime () const
 Returns the last time step.
std::size_t getStepSize () const
void setTimeUnit (TimeStepType t)
 Allows to set a unit for the time steps.
TimeStepType getTimeUnit () const
 Returns the unit the time steps.

Static Public Member Functions

static std::string convertSensorDataType2String (SensorDataType t)
 Converts Sensor Data Types to Strings.
static SensorDataType convertString2SensorDataType (const std::string &s)
 Converts Strings to Sensor Data Types.

Private Member Functions

int readDataFromFile (const std::string &file_name)
 Reads a CSV-file with time series data and fills the container.

Private Attributes

std::size_t _start
std::size_t _end
std::size_t _step_size
TimeStepType _time_unit
std::vector< std::string > _data_unit_string
std::vector< std::size_t > _time_steps
std::vector< SensorDataType_vec_names
std::vector< std::vector< float > * > _data_vecs

Constructor & Destructor Documentation

◆ SensorData() [1/2]

SensorData::SensorData ( const std::string & file_name)
explicit

Constructor using file name (automatically reads the file and fills all data structures)

Definition at line 14 of file SensorData.cpp.

16{
17 readDataFromFile(file_name);
18}
std::size_t _end
Definition SensorData.h:116
TimeStepType _time_unit
Definition SensorData.h:118
std::size_t _start
Definition SensorData.h:115
std::size_t _step_size
Definition SensorData.h:117
int readDataFromFile(const std::string &file_name)
Reads a CSV-file with time series data and fills the container.

References _end, _start, _step_size, _time_unit, NONE, and readDataFromFile().

◆ SensorData() [2/2]

SensorData::SensorData ( std::vector< std::size_t > time_steps)
explicit

Constructor using a time step vector valid for all time series that will be added later

Definition at line 20 of file SensorData.cpp.

21 : _start(time_steps.front()),
22 _end(time_steps.back()),
23 _step_size(0),
25 _time_steps(time_steps)
26{
27 if (!std::is_sorted(
28 time_steps.begin(), time_steps.end(), std::less_equal{}))
29 {
30 ERR("Error in SensorData() - Time series has no order!");
31 }
32}
void ERR(fmt::format_string< Args... > fmt, Args &&... args)
Definition Logging.h:40
std::vector< std::size_t > _time_steps
Definition SensorData.h:120

References _end, _start, _step_size, _time_steps, _time_unit, and NONE.

◆ ~SensorData()

SensorData::~SensorData ( )

Definition at line 34 of file SensorData.cpp.

35{
36 for (std::vector<float>* vec : _data_vecs)
37 {
38 delete vec;
39 }
40}
std::vector< std::vector< float > * > _data_vecs
Definition SensorData.h:122

References _data_vecs.

Member Function Documentation

◆ addTimeSeries() [1/2]

void SensorData::addTimeSeries ( const std::string & data_name,
std::vector< float > * data,
const std::string & data_unit_string = "" )

Adds a time series that needs to conform to the time step vector specified in the constructor. Optionally a unit for the time series can be given. The name is converted to SensorDataType enum.

Definition at line 42 of file SensorData.cpp.

45{
47 data,
48 data_unit_string);
49}
void addTimeSeries(const std::string &data_name, std::vector< float > *data, const std::string &data_unit_string="")
static SensorDataType convertString2SensorDataType(const std::string &s)
Converts Strings to Sensor Data Types.

References addTimeSeries(), and convertString2SensorDataType().

Referenced by addTimeSeries().

◆ addTimeSeries() [2/2]

void SensorData::addTimeSeries ( SensorDataType data_name,
std::vector< float > * data,
const std::string & data_unit_string = "" )

Adds a time series that needs to conform to the time step vector specified in the constructor. Optionally a unit for the time series can be given.

Definition at line 51 of file SensorData.cpp.

54{
55 if (_step_size > 0)
56 {
57 if (((_end - _start) / _step_size) != data->size())
58 {
59 WARN(
60 "Warning in SensorData::addTimeSeries() - Lengths of time "
61 "series does not match number of time steps.");
62 return;
63 }
64 }
65 else
66 {
67 if (data->size() != _time_steps.size())
68 {
69 WARN(
70 "Warning in SensorData::addTimeSeries() - Lengths of time "
71 "series does not match number of time steps.");
72 return;
73 }
74 }
75
76 _vec_names.push_back(data_name);
77 _data_vecs.push_back(data);
78 _data_unit_string.push_back(data_unit_string);
79}
void WARN(fmt::format_string< Args... > fmt, Args &&... args)
Definition Logging.h:34
std::vector< SensorDataType > _vec_names
Definition SensorData.h:121
std::vector< std::string > _data_unit_string
Definition SensorData.h:119

References _data_unit_string, _data_vecs, _end, _start, _step_size, _time_steps, _vec_names, and WARN().

◆ convertSensorDataType2String()

std::string SensorData::convertSensorDataType2String ( SensorDataType t)
static

Converts Sensor Data Types to Strings.

Definition at line 161 of file SensorData.cpp.

162{
164 {
165 return "Evaporation";
166 }
168 {
169 return "Precipitation";
170 }
172 {
173 return "Temperature";
174 }
175 // pls leave this as last choice
176 return "Unknown";
177}

References EVAPORATION, PRECIPITATION, and TEMPERATURE.

Referenced by getTimeSeries(), and DiagramList::readList().

◆ convertString2SensorDataType()

SensorDataType SensorData::convertString2SensorDataType ( const std::string & s)
static

Converts Strings to Sensor Data Types.

Definition at line 179 of file SensorData.cpp.

180{
181 if (s == "Evaporation" || s == "EVAPORATION")
182 {
184 }
185 if (s == "Precipitation" || s == "PRECIPITATION")
186 {
188 }
189 if (s == "Temperature" || s == "TEMPERATURE")
190 {
192 }
194}

References EVAPORATION, OTHER, PRECIPITATION, and TEMPERATURE.

Referenced by addTimeSeries(), and readDataFromFile().

◆ getEndTime()

std::size_t SensorData::getEndTime ( ) const
inline

Returns the last time step.

Definition at line 93 of file SensorData.h.

93{ return _end; }

References _end.

Referenced by DiagramPrefsDialog::DiagramPrefsDialog(), and DiagramList::readList().

◆ getStartTime()

std::size_t SensorData::getStartTime ( ) const
inline

Returns the first time step.

Definition at line 90 of file SensorData.h.

90{ return _start; }

References _start.

Referenced by DiagramPrefsDialog::DiagramPrefsDialog(), and DiagramList::readList().

◆ getStepSize()

std::size_t SensorData::getStepSize ( ) const
inline

Returns the interval between time steps (Returns "0" if a vector is given!)

Definition at line 97 of file SensorData.h.

97{ return _step_size; }

References _step_size.

Referenced by DiagramList::readList().

◆ getTimeSeries()

const std::vector< float > * SensorData::getTimeSeries ( SensorDataType time_series_name) const

Returns the time series with the given name.

Definition at line 81 of file SensorData.cpp.

83{
84 for (std::size_t i = 0; i < _vec_names.size(); i++)
85 {
86 if (time_series_name == _vec_names[i])
87 {
88 return _data_vecs[i];
89 }
90 }
91 ERR("Error in SensorData::getTimeSeries() - Time series '{:s}' not found.",
92 convertSensorDataType2String(time_series_name));
93 return nullptr;
94}
static std::string convertSensorDataType2String(SensorDataType t)
Converts Sensor Data Types to Strings.

References _data_vecs, _vec_names, convertSensorDataType2String(), and ERR().

Referenced by DiagramList::readList().

◆ getTimeSeriesNames()

const std::vector< SensorDataType > & SensorData::getTimeSeriesNames ( ) const
inline

Returns all time series names contained in this container.

Definition at line 81 of file SensorData.h.

82 {
83 return _vec_names;
84 }

References _vec_names.

Referenced by DiagramList::readList().

◆ getTimeSteps()

const std::vector< std::size_t > & SensorData::getTimeSteps ( ) const
inline

Returns the time step vector (if it exists)

Definition at line 87 of file SensorData.h.

87{ return _time_steps; }

References _time_steps.

Referenced by DiagramList::readList().

◆ getTimeUnit()

TimeStepType SensorData::getTimeUnit ( ) const
inline

Returns the unit the time steps.

Definition at line 103 of file SensorData.h.

103{ return _time_unit; }

References _time_unit.

◆ readDataFromFile()

int SensorData::readDataFromFile ( const std::string & file_name)
private

Reads a CSV-file with time series data and fills the container.

Definition at line 96 of file SensorData.cpp.

97{
98 std::ifstream in(file_name.c_str());
99
100 if (!in.is_open())
101 {
102 INFO("SensorData::readDataFromFile() - Could not open file {:s}.",
103 file_name);
104 return 0;
105 }
106
107 std::string line;
108
109 /* first line contains field names */
110 std::getline(in, line);
111 std::list<std::string> fields = BaseLib::splitString(line, '\t');
112 std::list<std::string>::const_iterator it(fields.begin());
113 std::size_t const nFields = fields.size();
114
115 if (nFields < 2)
116 {
117 return 0;
118 }
119
120 std::size_t const nDataArrays(nFields - 1);
121
122 // create vectors necessary to hold the data
123 for (std::size_t i = 0; i < nDataArrays; i++)
124 {
126 _data_unit_string.emplace_back("");
127 _data_vecs.push_back(new std::vector<float>);
128 }
129
130 while (std::getline(in, line))
131 {
132 fields = BaseLib::splitString(line, '\t');
133
134 if (nFields != fields.size())
135 {
136 return 0;
137 }
138
139 it = fields.begin();
140 std::size_t const pos(it->rfind("."));
141 std::size_t const current_time_step = (pos == std::string::npos)
142 ? atoi((it++)->c_str())
143 : BaseLib::strDate2int(*it++);
144 _time_steps.push_back(current_time_step);
145
146 for (std::size_t i = 0; i < nDataArrays; i++)
147 {
148 _data_vecs[i]->push_back(
149 static_cast<float>(strtod((it++)->c_str(), nullptr)));
150 }
151 }
152
153 in.close();
154
155 _start = _time_steps[0];
156 _end = _time_steps[_time_steps.size() - 1];
157
158 return 1;
159}
void INFO(fmt::format_string< Args... > fmt, Args &&... args)
Definition Logging.h:28
int strDate2int(const std::string &s)
Definition DateTools.cpp:91
std::vector< std::string > splitString(std::string const &str)

References _data_unit_string, _data_vecs, _end, _start, _time_steps, _vec_names, convertString2SensorDataType(), INFO(), BaseLib::splitString(), and BaseLib::strDate2int().

Referenced by SensorData().

◆ setTimeUnit()

void SensorData::setTimeUnit ( TimeStepType t)
inline

Allows to set a unit for the time steps.

Definition at line 100 of file SensorData.h.

100{ _time_unit = t; }

References _time_unit.

Member Data Documentation

◆ _data_unit_string

std::vector<std::string> SensorData::_data_unit_string
private

Definition at line 119 of file SensorData.h.

Referenced by addTimeSeries(), and readDataFromFile().

◆ _data_vecs

std::vector<std::vector<float>*> SensorData::_data_vecs
private

Definition at line 122 of file SensorData.h.

Referenced by ~SensorData(), addTimeSeries(), getTimeSeries(), and readDataFromFile().

◆ _end

std::size_t SensorData::_end
private

Definition at line 116 of file SensorData.h.

Referenced by SensorData(), SensorData(), addTimeSeries(), getEndTime(), and readDataFromFile().

◆ _start

std::size_t SensorData::_start
private

Definition at line 115 of file SensorData.h.

Referenced by SensorData(), SensorData(), addTimeSeries(), getStartTime(), and readDataFromFile().

◆ _step_size

std::size_t SensorData::_step_size
private

Definition at line 117 of file SensorData.h.

Referenced by SensorData(), SensorData(), addTimeSeries(), and getStepSize().

◆ _time_steps

std::vector<std::size_t> SensorData::_time_steps
private

Definition at line 120 of file SensorData.h.

Referenced by SensorData(), addTimeSeries(), getTimeSteps(), and readDataFromFile().

◆ _time_unit

TimeStepType SensorData::_time_unit
private

Definition at line 118 of file SensorData.h.

Referenced by SensorData(), SensorData(), getTimeUnit(), and setTimeUnit().

◆ _vec_names

std::vector<SensorDataType> SensorData::_vec_names
private

Definition at line 121 of file SensorData.h.

Referenced by addTimeSeries(), getTimeSeries(), getTimeSeriesNames(), and readDataFromFile().


The documentation for this class was generated from the following files: