GCBM
timeseriesutils.h
Go to the documentation of this file.
1 #ifndef MOJA_MODULES_CBM_TIMESERIESUTILS_H_
2 #define MOJA_MODULES_CBM_TIMESERIESUTILS_H_
3 
4 #include <moja/_core_exports.h>
5 #include <moja/dynamic.h>
6 #include <moja/exception.h>
7 
9 
10 namespace moja {
11 namespace modules {
12 namespace cbm {
13 
36 inline TimeSeries convertDynamicObjectToTimeSeries(const DynamicObject& s) {
37  if (!(s.size() == 1 && s.begin()->first == "$time_series")) {
38  throw InvalidArgumentException();
39  }
40  auto varStruct = s.begin()->second.extract<const DynamicObject>();
41 
42  int yr0 = varStruct["year_0"];
43  int dataPerYr = varStruct["data_per_year"];
44  int nYrs = varStruct["n_years"];
45  bool subSame = varStruct["sub_same"];
46 
47  auto arr = varStruct["values"].extract<DynamicVector>();
48  std::vector<boost::optional<double>> values;
49  for (auto item : arr) {
50  if (item.isEmpty()) {
51  values.push_back(boost::optional<double>{});
52  } else {
53  values.push_back(item.convert<double>());
54  }
55  }
56 
57  TimeSeries timeSeries(yr0, dataPerYr, nYrs, subSame, values);
58  return timeSeries;
59 }
60 
61 inline TimeSeries convertDynamicToTimeSeries(const DynamicVar& value) {
62  if (!value.isStruct()) {
63  throw InvalidArgumentException();
64  }
65 
66  auto& s = value.extract<const DynamicObject>();
68 }
69 
70 inline DynamicVar convertTimeSeriesToDynamic(const TimeSeries& timeseries) {
71 
72  DynamicVector ts_values = DynamicVector();
73 
74  for (auto val : timeseries.raw()) {
75  if (val)
76  ts_values.push_back(val.get());
77  else
78  ts_values.push_back(DynamicVar()); // json NULL to allow for nodata value
79  }
80 
81  DynamicObject ts_body = DynamicObject({
82  { "year_0", timeseries.yr0() },
83  { "data_per_year", timeseries.dataPerYr() },
84  { "n_years", timeseries.nYrs() },
85  { "sub_same", timeseries.subSame() },
86  { "values", ts_values }
87  });
88 
89  DynamicObject ts_container = DynamicObject({
90  { "$time_series", ts_body}
91  });
92  return ts_container;
93 }
94 
95 } // namespace cbm
96 } // namespace modules
97 } // namespace moja
98 
99 #endif // MOJA_MODULES_CBM_TIMESERIESUTILS_H_
moja::modules::cbm::convertDynamicObjectToTimeSeries
TimeSeries convertDynamicObjectToTimeSeries(const DynamicObject &s)
Converts a DynamicObject containing TimeSeries configuration to an actual TimeSeries object....
Definition: timeseriesutils.h:36
moja::modules::cbm::TimeSeries::dataPerYr
int dataPerYr() const
The number of data points per year in the TimeSeries.
Definition: timeseries.cpp:108
moja::modules::cbm
Definition: ageclasshelper.cpp:12
timeseries.h
moja::modules::cbm::TimeSeries::subSame
bool subSame() const
True means that sub-timesteps get the full year value; false means the yearly value will be divided e...
Definition: timeseries.cpp:109
moja::modules::cbm::TimeSeries
Encapsulates a timeseries of data. Normal usage allows modules to retrieve the most relevant data for...
Definition: timeseries.h:55
moja::modules::cbm::TimeSeries::yr0
int yr0() const
The first year or timestep of data for the TimeSeries.
Definition: timeseries.cpp:106
moja::modules::cbm::convertDynamicToTimeSeries
TimeSeries convertDynamicToTimeSeries(const DynamicVar &value)
Definition: timeseriesutils.h:61
moja::modules::cbm::convertTimeSeriesToDynamic
DynamicVar convertTimeSeriesToDynamic(const TimeSeries &timeseries)
Definition: timeseriesutils.h:70
moja::modules::cbm::TimeSeries::raw
const std::vector< boost::optional< double > > raw() const
The raw data for the TimeSeries.
Definition: timeseries.cpp:110
moja::modules::cbm::TimeSeries::nYrs
int nYrs() const
The number of years in the TimeSeries.
Definition: timeseries.cpp:107
moja
Definition: ageclasshelper.cpp:10