From d70412a77c65b04ac5805908526f40490f913a65 Mon Sep 17 00:00:00 2001 From: Frode Helgetun Krogh <70878501+frodehk@users.noreply.github.com> Date: Tue, 24 Oct 2023 11:46:07 +0200 Subject: [PATCH] chore: make sure no mismatch between timestemps and time series values --- src/libecalc/common/utils/rates.py | 17 +++++++++++++++++ 1 file changed, 17 insertions(+) diff --git a/src/libecalc/common/utils/rates.py b/src/libecalc/common/utils/rates.py index 9150ffd3ea..9bbcb01d04 100644 --- a/src/libecalc/common/utils/rates.py +++ b/src/libecalc/common/utils/rates.py @@ -156,6 +156,23 @@ def convert_none_to_nan(cls, v: float, field: ModelField) -> TimeSeriesValue: return math.nan return v + @validator("values", pre=True) + def timesteps_values_one_to_one(cls, v: list, values, field: ModelField): + nr_timesteps = len(values["timesteps"]) + nr_values = len(v) + + if not cls.__name__ == TimeSeriesVolumes.__name__: + if nr_timesteps != nr_values: + if all(math.isnan(i) for i in v): + # TODO: This should probably be solved another place. Temporary solution to make things run + return [math.nan] * len(values["timesteps"]) + else: + raise ProgrammingError( + "Time series: number of timesteps do not match number " + "of values. Most likely a bug, report to eCalc Dev Team." + ) + return v + def __len__(self) -> int: return len(self.values)