Skip to content
Snippets Groups Projects
Commit 259e2133 authored by Sebastian Müller's avatar Sebastian Müller 🐈
Browse files

Add first draft of mhm module

parent eb5468c3
No related branches found
No related tags found
No related merge requests found
"""Simple live viewer for a mHM run."""
"""
Simple coupling setup using live view modules.
"""
from datetime import timedelta, datetime
import numpy as np
from finam.adapters import time, base
from finam.core.schedule import Composition
from finam.modules.visual import time_series
from finam_mhm_module import Mhm
def grid_select(grid):
return grid.get(col=3, row=5)
plot = time_series.TimeSeriesView(
start=datetime(1990, 1, 1),
step=timedelta(days=1),
inputs=["Linear (1)"],
intervals=[1],
)
mhm = Mhm(cwd="../../MHM/mhm")
composition = Composition([mhm, plot])
composition.initialize()
grid_value = mhm.outputs["runoff"] >> base.GridToValue(func=grid_select)
grid_value >> time.LinearInterpolation() >> plot.inputs["Linear (1)"]
composition.run(datetime(1991, 1, 1))
from .mhm import Mhm
"""
Finam mHM module.
FINAM mHM module.
"""
from datetime import datetime
import mhm_pybind as mp
from finam.core.sdk import ATimeComponent, Output
from finam.core.interfaces import ComponentStatus
from finam.data.grid import Grid, GridSpec
class Mhm(ATimeComponent):
def __init__(
self,
namelist_mhm="mhm.nml",
namelist_mhm_param="mhm_parameter.nml",
namelist_mhm_output="mhm_outputs.nml",
namelist_mrm_output="mrm_outputs.nml",
cwd=".",
):
super(Mhm, self).__init__()
mp.mhm.init(
namelist_mhm=namelist_mhm,
namelist_mhm_param=namelist_mhm_param,
namelist_mhm_output=namelist_mhm_output,
namelist_mrm_output=namelist_mrm_output,
cwd=cwd,
)
self._status = ComponentStatus.CREATED
def initialize(self):
super().initialize()
mp.run.prepare()
mp.run.prepare_domain()
# set time
year, month, day, hour = mp.run.current_time()
self._time = datetime(year=year, month=month, day=day, hour=hour)
# get grid info
ncols, nrows, ncells, xll, yll, cell_size, no_data = mp.get.L1_domain_info()
self.no_data = no_data
self.gridspec = GridSpec(
ncols=ncols, nrows=nrows, cell_size=cell_size, xll=xll, yll=yll
)
self.outputs["runoff"] = Output()
self._status = ComponentStatus.INITIALIZED
def connect(self):
super().connect()
runoff = mp.get_variable("L1_total_runoff")
self._outputs["runoff"].push_data(
data=Grid(
spec=self.gridspec,
no_data=self.no_data,
data=runoff.filled().reshape(-1),
),
time=self.time,
)
self._status = ComponentStatus.CONNECTED
def validate(self):
super().validate()
self._status = ComponentStatus.VALIDATED
def update(self):
super().update()
# 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?
# update time
year, month, day, hour = mp.run.current_time()
self._time = datetime(year=year, month=month, day=day, hour=hour)
# push outputs
runoff = mp.get_variable("L1_total_runoff")
self._outputs["runoff"].push_data(
data=Grid(
spec=self.gridspec,
no_data=self.no_data,
data=runoff.filled().reshape(-1),
),
time=self.time,
)
self._status = ComponentStatus.UPDATED
def finalize(self):
super().finalize()
mp.run.finalize_domain()
mp.run.finalize()
mp.mhm.finalize()
self._status = ComponentStatus.FINALIZED
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment