Go to the documentation of this file. 1 #ifndef MOJA_MODULES_CBM_TIMESERIESUTILS_H_
2 #define MOJA_MODULES_CBM_TIMESERIESUTILS_H_
4 #include <moja/_core_exports.h>
5 #include <moja/dynamic.h>
6 #include <moja/exception.h>
37 if (!(s.size() == 1 && s.begin()->first ==
"$time_series")) {
38 throw InvalidArgumentException();
40 auto varStruct = s.begin()->second.extract<
const DynamicObject>();
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"];
47 auto arr = varStruct[
"values"].extract<DynamicVector>();
48 std::vector<boost::optional<double>> values;
49 for (
auto item : arr) {
51 values.push_back(boost::optional<double>{});
53 values.push_back(item.convert<
double>());
57 TimeSeries timeSeries(yr0, dataPerYr, nYrs, subSame, values);
62 if (!value.isStruct()) {
63 throw InvalidArgumentException();
66 auto& s = value.extract<
const DynamicObject>();
72 DynamicVector ts_values = DynamicVector();
74 for (
auto val : timeseries.
raw()) {
76 ts_values.push_back(val.get());
78 ts_values.push_back(DynamicVar());
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 }
89 DynamicObject ts_container = DynamicObject({
90 {
"$time_series", ts_body}
99 #endif // MOJA_MODULES_CBM_TIMESERIESUTILS_H_
TimeSeries convertDynamicObjectToTimeSeries(const DynamicObject &s)
Converts a DynamicObject containing TimeSeries configuration to an actual TimeSeries object....
Definition: timeseriesutils.h:36
int dataPerYr() const
The number of data points per year in the TimeSeries.
Definition: timeseries.cpp:108
Definition: ageclasshelper.cpp:12
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
Encapsulates a timeseries of data. Normal usage allows modules to retrieve the most relevant data for...
Definition: timeseries.h:55
int yr0() const
The first year or timestep of data for the TimeSeries.
Definition: timeseries.cpp:106
TimeSeries convertDynamicToTimeSeries(const DynamicVar &value)
Definition: timeseriesutils.h:61
DynamicVar convertTimeSeriesToDynamic(const TimeSeries ×eries)
Definition: timeseriesutils.h:70
const std::vector< boost::optional< double > > raw() const
The raw data for the TimeSeries.
Definition: timeseries.cpp:110
int nYrs() const
The number of years in the TimeSeries.
Definition: timeseries.cpp:107
Definition: ageclasshelper.cpp:10