From 87c2a9bebd4111f71ba4f910585d5911b58739c2 Mon Sep 17 00:00:00 2001 From: Nicolas Mounet Date: Mon, 9 Nov 2020 16:08:58 +0100 Subject: [PATCH] Adding refine method (with linear interpolation) in PiecewiseConstant class --- cara/models.py | 12 ++++++++++++ 1 file changed, 12 insertions(+) diff --git a/cara/models.py b/cara/models.py index 471e9739..3b297063 100644 --- a/cara/models.py +++ b/cara/models.py @@ -103,6 +103,9 @@ class PeriodicInterval(Interval): @dataclass(frozen=True) class PiecewiseConstant: + # TODO: implement rather a periodic version (24-hour period), where + # transition_times and values have the same length. + #: transition times at which the function changes value (hours). transition_times: typing.Tuple[float, ...] @@ -135,6 +138,15 @@ class PiecewiseConstant: present_times.append((t1,t2)) return SpecificInterval(present_times=present_times) + def refine(self,refine_factor=10): + # build a new PiecewiseConstant object with a refined mesh, + # using a linear interpolation in-between the initial mesh points + refined_times = np.linspace(self.transition_times[0],self.transition_times[-1], + (len(self.transition_times)-1)*refine_factor+1) + return PiecewiseConstant(tuple(refined_times), + tuple(np.interp(refined_times[:-1],self.transition_times, + self.values+(self.values[-1],) ) ) ) + # Geneva hourly temperatures as piecewise constant function (in Kelvin) GenevaTemperatures_hourly = {