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 62 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 25 of file SensorData.cpp.

27{
28 readDataFromFile(file_name);
29}
std::size_t _end
Definition SensorData.h:127
TimeStepType _time_unit
Definition SensorData.h:129
std::size_t _start
Definition SensorData.h:126
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.

References 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 31 of file SensorData.cpp.

32 : _start(time_steps.front()),
33 _end(time_steps.back()),
34 _step_size(0),
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}
void ERR(fmt::format_string< Args... > fmt, Args &&... args)
Definition Logging.h:45
std::vector< std::size_t > _time_steps
Definition SensorData.h:131

References ERR().

◆ ~SensorData()

SensorData::~SensorData ( )

Definition at line 45 of file SensorData.cpp.

46{
47 for (std::vector<float>* vec : _data_vecs)
48 {
49 delete vec;
50 }
51}
std::vector< std::vector< float > * > _data_vecs
Definition SensorData.h:133

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 53 of file SensorData.cpp.

56{
58 data,
59 data_unit_string);
60}
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 62 of file SensorData.cpp.

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}
void WARN(fmt::format_string< Args... > fmt, Args &&... args)
Definition Logging.h:40
std::vector< SensorDataType > _vec_names
Definition SensorData.h:132
std::vector< std::string > _data_unit_string
Definition SensorData.h:130

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 172 of file SensorData.cpp.

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}

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 190 of file SensorData.cpp.

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}

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 104 of file SensorData.h.

104{ 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 101 of file SensorData.h.

101{ 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 108 of file SensorData.h.

108{ 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 92 of file SensorData.cpp.

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}
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 92 of file SensorData.h.

93 {
94 return _vec_names;
95 }

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 98 of file SensorData.h.

98{ 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 114 of file SensorData.h.

114{ 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 107 of file SensorData.cpp.

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}
void INFO(fmt::format_string< Args... > fmt, Args &&... args)
Definition Logging.h:35
int strDate2int(const std::string &s)
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 111 of file SensorData.h.

111{ _time_unit = t; }

References _time_unit.

Member Data Documentation

◆ _data_unit_string

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

Definition at line 130 of file SensorData.h.

Referenced by addTimeSeries(), and readDataFromFile().

◆ _data_vecs

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

Definition at line 133 of file SensorData.h.

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

◆ _end

std::size_t SensorData::_end
private

Definition at line 127 of file SensorData.h.

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

◆ _start

std::size_t SensorData::_start
private

Definition at line 126 of file SensorData.h.

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

◆ _step_size

std::size_t SensorData::_step_size
private

Definition at line 128 of file SensorData.h.

Referenced by addTimeSeries(), and getStepSize().

◆ _time_steps

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

Definition at line 131 of file SensorData.h.

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

◆ _time_unit

TimeStepType SensorData::_time_unit
private

Definition at line 129 of file SensorData.h.

Referenced by getTimeUnit(), and setTimeUnit().

◆ _vec_names

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

Definition at line 132 of file SensorData.h.

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


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