diff --git a/src/finam_mhm_module/__init__.py b/src/finam_mhm_module/__init__.py index 23a644a2e30ef39961df939476eee7ce3b5439e7..355a541af585818043b1dabe67a013fe93a891f5 100644 --- a/src/finam_mhm_module/__init__.py +++ b/src/finam_mhm_module/__init__.py @@ -1 +1,2 @@ from .mhm import Mhm +from .lai_adapter import YearlyToMonthly diff --git a/src/finam_mhm_module/lai_adapter.py b/src/finam_mhm_module/lai_adapter.py new file mode 100644 index 0000000000000000000000000000000000000000..d70e69b74a881645d9634e9c2039cff50b4a7d65 --- /dev/null +++ b/src/finam_mhm_module/lai_adapter.py @@ -0,0 +1,35 @@ +from datetime import datetime + +from finam.core.sdk import AAdapter + + +class YearlyToMonthly(AAdapter): + """Disaggregates yearly LAI to a stack of monthly LAIs.""" + + def __init__(self, lai_curve): + super().__init__() + self.data = None + self.month = 0 + if len(lai_curve) != 12: + raise ValueError("LAI curve must be an array of 12 values") + self.lai_curve = lai_curve + + def source_changed(self, time): + if not isinstance(time, datetime): + raise ValueError("Time must be of type datetime") + + self.data = self.pull_data(time) + self.month = time.month + + self.notify_targets(time) + + def get_data(self, time): + if not isinstance(time, datetime): + raise ValueError("Time must be of type datetime") + + new_month = time.month + lai = [] + for m in range(self.month - 1, new_month): + lai.append(self.data * self.lai_curve[m]) + + return lai diff --git a/tests/test_lai_adapter.py b/tests/test_lai_adapter.py new file mode 100644 index 0000000000000000000000000000000000000000..d5defe70bab56bc6d7c83d7a51b4bc96e50f9f58 --- /dev/null +++ b/tests/test_lai_adapter.py @@ -0,0 +1,9 @@ +import unittest +from datetime import datetime, timedelta + +from finam_mhm_module import YearlyToMonthly + + +class TestLaiAdapter(unittest.TestCase): + def test_adapter(self): + pass