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

#include <SensorData.h>

Public Member Functions

 SensorData (const std::string &file_name)
 Constructor using file name (automatically reads the file and fills all data structures) More...
 
 SensorData (std::vector< std::size_t > time_steps)
 Constructor using a time step vector valid for all time series that will be added later. More...
 
 ~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. More...
 
const std::vector< SensorDataType > & getTimeSeriesNames () const
 Returns all time series names contained in this container. More...
 
const std::vector< std::size_t > & getTimeSteps () const
 Returns the time step vector (if it exists) More...
 
std::size_t getStartTime () const
 Returns the first time step. More...
 
std::size_t getEndTime () const
 Returns the last time step. More...
 
std::size_t getStepSize () const
 Returns the interval between time steps (Returns "0" if a vector is given!) More...
 
void setTimeUnit (TimeStepType t)
 Allows to set a unit for the time steps. More...
 
TimeStepType getTimeUnit () const
 Returns the unit the time steps. More...
 

Static Public Member Functions

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

Private Member Functions

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

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

26 {
27  readDataFromFile(file_name);
28 }
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.
Definition: SensorData.cpp:106

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

31  : _start(time_steps.front()),
32  _end(time_steps.back()),
33  _step_size(0),
35  _time_steps(time_steps)
36 {
37  if (!std::is_sorted(
38  time_steps.begin(), time_steps.end(), std::less_equal{}))
39  {
40  ERR("Error in SensorData() - Time series has no order!");
41  }
42 }
void ERR(char const *fmt, Args const &... args)
Definition: Logging.h:42
std::vector< std::size_t > _time_steps
Definition: SensorData.h:120

References ERR().

◆ ~SensorData()

SensorData::~SensorData ( )

Definition at line 44 of file SensorData.cpp.

45 {
46  for (std::vector<float>* vec : _data_vecs)
47  {
48  delete vec;
49  }
50 }
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 52 of file SensorData.cpp.

55 {
57  data,
58  data_unit_string);
59 }
void addTimeSeries(const std::string &data_name, std::vector< float > *data, const std::string &data_unit_string="")
Definition: SensorData.cpp:52
static SensorDataType convertString2SensorDataType(const std::string &s)
Converts Strings to Sensor Data Types.
Definition: SensorData.cpp:189

References convertString2SensorDataType().

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

64 {
65  if (_step_size > 0)
66  {
67  if (((_end - _start) / _step_size) != data->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  else
76  {
77  if (data->size() != _time_steps.size())
78  {
79  WARN(
80  "Warning in SensorData::addTimeSeries() - Lengths of time "
81  "series does not match number of time steps.");
82  return;
83  }
84  }
85 
86  _vec_names.push_back(data_name);
87  _data_vecs.push_back(data);
88  _data_unit_string.push_back(data_unit_string);
89 }
void WARN(char const *fmt, Args const &... args)
Definition: Logging.h:37
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 171 of file SensorData.cpp.

172 {
174  {
175  return "Evaporation";
176  }
178  {
179  return "Precipitation";
180  }
182  {
183  return "Temperature";
184  }
185  // pls leave this as last choice
186  return "Unknown";
187 }

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

190 {
191  if (s == "Evaporation" || s == "EVAPORATION")
192  {
194  }
195  if (s == "Precipitation" || s == "PRECIPITATION")
196  {
198  }
199  if (s == "Temperature" || s == "TEMPERATURE")
200  {
202  }
203  return SensorDataType::OTHER;
204 }

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

94 { 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 91 of file SensorData.h.

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

93 {
94  for (std::size_t i = 0; i < _vec_names.size(); i++)
95  {
96  if (time_series_name == _vec_names[i])
97  {
98  return _data_vecs[i];
99  }
100  }
101  ERR("Error in SensorData::getTimeSeries() - Time series '{:s}' not found.",
102  convertSensorDataType2String(time_series_name));
103  return nullptr;
104 }
static std::string convertSensorDataType2String(SensorDataType t)
Converts Sensor Data Types to Strings.
Definition: SensorData.cpp:171

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

85 { return _vec_names; }

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

88 { 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 106 of file SensorData.cpp.

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

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 addTimeSeries(), getEndTime(), and readDataFromFile().

◆ _start

std::size_t SensorData::_start
private

Definition at line 115 of file SensorData.h.

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

◆ _step_size

std::size_t SensorData::_step_size
private

Definition at line 117 of file SensorData.h.

Referenced by addTimeSeries(), and getStepSize().

◆ _time_steps

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

Definition at line 120 of file SensorData.h.

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

◆ _time_unit

TimeStepType SensorData::_time_unit
private

Definition at line 118 of file SensorData.h.

Referenced by 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: