Skip to content
Snippets Groups Projects

Compare revisions

Changes are shown as if the source revision was being merged into the target revision. Learn more about comparing revisions.

Source

Select target project
No results found

Target

Select target project
  • afid.nur-kholis/finam-mhm
  • FINAM/finam-mhm
2 results
Show changes
Commits on Source (3)
"""
Simple coupling setup using live view modules.
"""
import logging
from datetime import datetime, timedelta
import numpy as np
......@@ -26,11 +27,13 @@ plot = time_series.TimeSeriesView(
mhm = Mhm(cwd="../../MHM/mhm")
composition = Composition([mhm, plot])
composition = Composition(
[mhm, plot], log_level=logging.DEBUG, log_file=True, print_log=False
)
composition.initialize()
grid_value = mhm.outputs["L1_TOTAL_RUNOFF"] >> base.GridToValue(func=grid_select)
grid_value >> time.LinearInterpolation() >> plot.inputs["Runoff"]
composition.run(datetime(1992, 1, 1))
composition.run(datetime(1990, 1, 10))
plt.show()
from .mhm import Mhm
from .lai_adapter import YearlyToMonthly
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
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.notify_targets(time)
def get_data(self, time):
if not isinstance(time, datetime):
raise ValueError("Time must be of type datetime")
lai = [self.data * self.lai_curve[m] for m in range(0, len(self.lai_curve))]
return lai
......@@ -9,6 +9,7 @@ from finam.core.interfaces import ComponentStatus
from finam.core.sdk import ATimeComponent, Input, Output
from finam.data.grid import Grid, GridSpec
from finam.tools.cwd_helper import execute_in_cwd
from finam.tools.log_helper import LogCStdOutStdErr
class Mhm(ATimeComponent):
......@@ -60,15 +61,18 @@ class Mhm(ATimeComponent):
@execute_in_cwd
def initialize(self):
super().initialize()
mp.mhm.init(
namelist_mhm=self.namelist_mhm,
namelist_mhm_param=self.namelist_mhm_param,
namelist_mhm_output=self.namelist_mhm_output,
namelist_mrm_output=self.namelist_mrm_output,
cwd=".",
)
mp.run.prepare()
mp.run.prepare_domain()
with LogCStdOutStdErr(self.logger):
mp.mhm.init(
namelist_mhm=self.namelist_mhm,
namelist_mhm_param=self.namelist_mhm_param,
namelist_mhm_output=self.namelist_mhm_output,
namelist_mrm_output=self.namelist_mrm_output,
cwd=".",
)
with LogCStdOutStdErr(self.logger):
mp.run.prepare()
with LogCStdOutStdErr(self.logger):
mp.run.prepare_domain()
# set time
year, month, day, hour = mp.run.current_time()
hour = max(hour, 0) # fix for first time step
......@@ -132,8 +136,10 @@ class Mhm(ATimeComponent):
# Don't run further than mHM can
if mp.run.finished():
return
mp.run.do_time_step()
mp.run.write_output() # do we want this here?
with LogCStdOutStdErr(self.logger):
mp.run.do_time_step()
with LogCStdOutStdErr(self.logger):
mp.run.write_output() # do we want this here?
# update time
year, month, day, hour = mp.run.current_time()
self._time = datetime(year=year, month=month, day=day, hour=hour)
......@@ -157,7 +163,10 @@ class Mhm(ATimeComponent):
@execute_in_cwd
def finalize(self):
super().finalize()
mp.run.finalize_domain()
mp.run.finalize()
mp.mhm.finalize()
with LogCStdOutStdErr(self.logger):
mp.run.finalize_domain()
with LogCStdOutStdErr(self.logger):
mp.run.finalize()
with LogCStdOutStdErr(self.logger):
mp.mhm.finalize()
self._status = ComponentStatus.FINALIZED
import unittest
from datetime import datetime, timedelta
from finam.modules.generators import CallbackGenerator
from finam.data.grid import Grid, GridSpec
from finam_mhm_module import YearlyToMonthly
class TestLaiAdapter(unittest.TestCase):
def test_adapter(self):
grid = Grid(GridSpec(3, 2))
grid.fill(1.0)
for c in range(3):
grid.set(c, 0, 2.0)
source = CallbackGenerator(
callbacks={"LAI": lambda t: grid},
start=datetime(2000, 1, 1),
step=timedelta(365.0),
)
lai_curve = [0.0, 0.0, 0.2, 0.5, 0.8, 1.0, 1.0, 1.0, 0.8, 0.5, 0.2, 0.0]
adapter = YearlyToMonthly(lai_curve)
source.initialize()
source.outputs["LAI"] >> adapter
source.connect()
source.validate()
lai_months = adapter.get_data(datetime(2000, 1, 1, 0))
self.assertEqual(len(lai_months), 12)
for m in range(len(lai_months)):
self.assertTrue(isinstance(lai_months[m], Grid))
self.assertEqual(lai_months[m], grid * lai_curve[m])
grid.fill(2.0)
source.update()
lai_months_2 = adapter.get_data(datetime(2001, 1, 1, 0))
self.assertEqual(len(lai_months_2), 12)
for m in range(len(lai_months_2)):
self.assertTrue(isinstance(lai_months_2[m], Grid))
self.assertEqual(lai_months_2[m], grid * lai_curve[m])