Skip to content

Commit

Permalink
Merge pull request #318 from jmccreight/feat_exchange
Browse files Browse the repository at this point in the history
prms_segment_lateral_inflow_components_to_netcdf
  • Loading branch information
jmccreight authored Nov 26, 2024
2 parents d70b5f7 + a0c2b23 commit 50631f1
Show file tree
Hide file tree
Showing 8 changed files with 227 additions and 28 deletions.
3 changes: 2 additions & 1 deletion asv_benchmarks/asv.conf.json
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,8 @@

// List of branches to benchmark. If not provided, defaults to "master"
// (for git) or "default" (for mercurial).
"branches": ["main"], // for git
// "branches": ["main"], // for git
"branches": ["develop"], // for git
// "branches": ["default"], // for mercurial

// The DVCS being used. If not set, it will be automatically
Expand Down
4 changes: 2 additions & 2 deletions asv_benchmarks/benchmarks/prms.py
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ def time_prms_parameter_read(self, domain):
(domains),
)
def time_prms_control_read(self, domain):
control_file = test_data_dir / f"{domain}/control.test"
control_file = test_data_dir / f"{domain}/nhm.control"
if pws.__version__ == "0.2.0":
_ = pws.Control.load(control_file)
else:
Expand All @@ -78,7 +78,7 @@ def setup(self, *args):
self.tag = args[1]
self.processes = model_tests[self.tag]

self.control_file = test_data_dir / f"{self.domain}/control.test"
self.control_file = test_data_dir / f"{self.domain}/nhm.control"
self.parameter_file = test_data_dir / f"{self.domain}/myparam.param"

# backwards compatability pre pywatershed
Expand Down
31 changes: 27 additions & 4 deletions autotest/test_prms_channel_flow_graph.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
PRMSRunoff,
PRMSSoilzone,
prms_channel_flow_graph_to_model_dict,
prms_segment_lateral_inflow_components_to_netcdf,
)
from pywatershed.base.adapter import AdapterNetcdf, adapter_factory
from pywatershed.base.control import Control
Expand All @@ -18,8 +19,8 @@
from pywatershed.base.parameters import Parameters
from pywatershed.constants import nan, zero
from pywatershed.hydrology.prms_channel_flow_graph import (
HruNodeFlowExchange,
HruSegmentFlowAdapter,
HruSegmentFlowExchange,
PRMSChannelFlowNodeMaker,
)
from pywatershed.parameters import PrmsParameters
Expand Down Expand Up @@ -167,6 +168,7 @@ def test_prms_channel_flow_graph_compare_prms(

# check exchange
lateral_inflow_answers.advance()

np.testing.assert_allclose(
inflow_prms.current,
lateral_inflow_answers.current,
Expand Down Expand Up @@ -203,7 +205,7 @@ def test_prms_channel_flow_graph_compare_prms(
flow_graph.finalize()


exchange_types = ("hrusegmentflowexchange", "inflowexchangefactory")
exchange_types = ("hrunodeflowexchange", "inflowexchangefactory")


@pytest.mark.parametrize("exchange_type", exchange_types)
Expand All @@ -215,8 +217,8 @@ def test_hru_segment_flow_exchange(
tmp_path,
exchange_type,
):
if exchange_type == "hrusegmentflowexchange":
Exchange = HruSegmentFlowExchange
if exchange_type == "hrunodeflowexchange":
Exchange = HruNodeFlowExchange
else:
# else we implement the exchange by-hand here.
# this is also implemented in channel_flow_graph_to_model_dict
Expand Down Expand Up @@ -354,6 +356,7 @@ def calculation(self) -> None:
)

model = Model(model_dict)

for istep in range(control.n_times):
model.advance()
model.calculate()
Expand Down Expand Up @@ -507,3 +510,23 @@ def test_prms_channel_flow_graph_to_model_dict(
assert da.node_maker_index[-3:].values.tolist() == check_indices
assert da.node_maker_name[-3:].values.tolist() == check_names
assert da.node_maker_id[-3:].values.tolist() == check_ids


def test_prms_segment_lateral_inflow_components_to_netcdf(
simulation, control, parameters, tmp_path
):
nc_out_file_path = tmp_path / "segment_lateral_inflows.nc"
prms_segment_lateral_inflow_components_to_netcdf(
control,
parameters,
input_dir=simulation["output_dir"],
nc_out_file_path=nc_out_file_path,
output_sum=True,
)

results = xr.load_dataset(nc_out_file_path).lateral_inflow_vol
answers = xr.load_dataarray(
simulation["output_dir"] / "seg_lateral_inflow.nc"
)

xr.testing.assert_allclose(answers, results)
1 change: 1 addition & 0 deletions doc/api/flow_graph.rst
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ FlowGraph base classes and subclasses. See :class:`FlowGraph` for an overview of
PRMSChannelFlowNodeMaker
prms_channel_flow_graph_to_model_dict
prms_channel_flow_graph_postprocess
prms_segment_lateral_inflow_components_to_netcdf
HruSegmentFlowAdapter
HruSegmentFlowExchange

6 changes: 4 additions & 2 deletions pywatershed/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,12 +19,13 @@
from .hydrology.prms_canopy import PRMSCanopy
from .hydrology.prms_channel import PRMSChannel
from .hydrology.prms_channel_flow_graph import (
HruNodeFlowExchange,
HruSegmentFlowAdapter,
HruSegmentFlowExchange,
PRMSChannelFlowNode,
PRMSChannelFlowNodeMaker,
prms_channel_flow_graph_postprocess,
prms_channel_flow_graph_to_model_dict,
prms_segment_lateral_inflow_components_to_netcdf,
)
from .hydrology.prms_et import PRMSEt
from .hydrology.prms_groundwater import PRMSGroundwater
Expand All @@ -51,10 +52,11 @@
__all__ = (
"prms_channel_flow_graph_postprocess",
"prms_channel_flow_graph_to_model_dict",
"prms_segment_lateral_inflow_components_to_netcdf",
"PRMSChannelFlowNode",
"PRMSChannelFlowNodeMaker",
"HruSegmentFlowAdapter",
"HruSegmentFlowExchange",
"HruNodeFlowExchange",
"ModelGraph",
"ColorBrewer",
"PRMSAtmosphere",
Expand Down
4 changes: 3 additions & 1 deletion pywatershed/base/adapter.py
Original file line number Diff line number Diff line change
Expand Up @@ -127,9 +127,11 @@ def __init__(
self,
data: np.ndarray,
variable: str,
control: Control = None,
) -> None:
super().__init__(variable)
self.name = "AdapterOnedarray"
self.control = control
self._current_value = data
return

Expand Down Expand Up @@ -174,7 +176,7 @@ def adapter_factory(

elif isinstance(var, np.ndarray) and len(var.shape) == 1:
# Adapt 1-D np.ndarrays
return AdapterOnedarray(var, variable=variable_name)
return AdapterOnedarray(var, variable=variable_name, control=control)

elif isinstance(var, TimeseriesArray):
# Adapt TimeseriesArrays as is.
Expand Down
6 changes: 4 additions & 2 deletions pywatershed/hydrology/__init__.py
Original file line number Diff line number Diff line change
@@ -1,12 +1,13 @@
from .prms_canopy import PRMSCanopy
from .prms_channel import PRMSChannel
from .prms_channel_flow_graph import (
HruNodeFlowExchange,
HruSegmentFlowAdapter,
HruSegmentFlowExchange,
PRMSChannelFlowNode,
PRMSChannelFlowNodeMaker,
prms_channel_flow_graph_postprocess,
prms_channel_flow_graph_to_model_dict,
prms_segment_lateral_inflow_components_to_netcdf,
)
from .prms_groundwater import PRMSGroundwater
from .prms_groundwater_no_dprst import PRMSGroundwaterNoDprst
Expand All @@ -19,10 +20,11 @@
__all__ = (
"prms_channel_flow_graph_postprocess",
"prms_channel_flow_graph_to_model_dict",
"prms_segment_lateral_inflow_components_to_netcdf",
"PRMSChannelFlowNode",
"PRMSChannelFlowNodeMaker",
"HruSegmentFlowAdapter",
"HruSegmentFlowExchange",
"HruNodeFlowExchange",
"PRMSCanopy",
"PRMSChannel",
"PRMSGroundwater",
Expand Down
Loading

0 comments on commit 50631f1

Please sign in to comment.