Skip to content

Commit

Permalink
Move to Remote BMI & containerized model (#12)
Browse files Browse the repository at this point in the history
* Use remotebmi containerized model

* Add Dockerfile

* Add new demo notebook

* Update dockerfile

* Update README

* Correct entrypoint, set ewatercycle minimum version

* Update CMIP forcing notebook

* Update formatting

* Add link to parameter set download

* Modify Dockerfile to allow any user to run the container

---------

Co-authored-by: sverhoeven <[email protected]>
  • Loading branch information
BSchilperoort and sverhoeven authored Jan 8, 2025
1 parent d88623e commit 07adbef
Show file tree
Hide file tree
Showing 12 changed files with 387 additions and 731 deletions.
18 changes: 18 additions & 0 deletions Dockerfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
FROM ghcr.io/ewatercycle/remotebmi-julia:0.1.0

LABEL org.opencontainers.image.source="https://github.com/eWaterCycle/ewatercycle-wflowjl"

# Install Wflow
RUN julia -e 'using Pkg; Pkg.add(PackageSpec(name="Wflow", version="0.8.1"))'

RUN echo "using Wflow" > run.jl
RUN echo "import RemoteBMI.Server: run_bmi_server" >> run.jl
RUN echo "port = parse(Int, get(ENV, \"BMI_PORT\", \"50051\"))" >> run.jl
RUN echo "run_bmi_server(Wflow.Model, \"0.0.0.0\", port)" >> run.jl

# chmod central depot path so all users can access it
RUN chmod -R 777 ${JULIA_DEPOT_PATH}

# Expose port and start server
EXPOSE 50051
CMD ["julia", "run.jl"]
23 changes: 7 additions & 16 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,38 +2,29 @@

Wflow.jl plugin for [eWatercycle](https://ewatercycle.readthedocs.io/).

The Wflow.jl documentation is available at https://deltares.github.io/Wflow.jl/dev/ .
The Wflow.jl documentation is available at https://deltares.github.io/Wflow.jl/ .

## Installation

eWaterCycle must be installed in a [mamba](https://conda-forge.org/miniforge/) environment. The environment can be created with
Please first install ewatercycle, for more info see the [general ewatercycle documentation](https://ewatercycle.readthedocs.io/).

```console
wget https://raw.githubusercontent.com/eWaterCycle/ewatercycle/main/environment.yml
mamba env create --name ewatercycle-wflowjl --file environment.yml
conda activate ewatercycle-wflowjl
```

Install this package alongside your eWaterCycle installation
To install this package alongside your eWaterCycle installation, do:

```console
pip install ewatercycle-wflowjl
```

Then Wflow becomes available as one of the eWaterCycle models
Then Wflow becomes available as one of the eWaterCycle models:

```python
from ewatercycle.models import WflowJl
import ewatercycle.models
ewatercycle.models.sources["WflowJl"]
```

Note that unlike other plugins, the WflowJl eWaterCycle model does not run in a container.

This is due to limitations of the Julia language.

## Usage

Usage of Wflow.jl forcing generation and model execution is shown in
[docs/generate_era5_forcing.ipynb](https://github.com/eWaterCycle/ewatercycle-wflowjl/tree/main/docs/generate_era5_forcing.ipynb) and [docs/wflowjl_local.ipynb](https://github.com/eWaterCycle/ewatercycle-wflow/tree/main/docs/wflowjl_local.ipynb) respectively.
[docs/generate_era5_forcing.ipynb](https://github.com/eWaterCycle/ewatercycle-wflowjl/tree/main/docs/generate_era5_forcing.ipynb) and [docs/demo.ipynb](https://github.com/eWaterCycle/ewatercycle-wflow/tree/main/docs/demo.ipynb) respectively.

## License

Expand Down
264 changes: 264 additions & 0 deletions docs/demo.ipynb

Large diffs are not rendered by default.

278 changes: 75 additions & 203 deletions docs/generate_cmip_forcing.ipynb

Large diffs are not rendered by default.

451 changes: 0 additions & 451 deletions docs/wflowjl_local.ipynb

This file was deleted.

15 changes: 9 additions & 6 deletions pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -26,8 +26,8 @@ dynamic = ["version"]

# Include here only the dependencies for the eWaterCycle wrapped model
dependencies = [
"ewatercycle>=2.0.0b2",
"grpc4bmi[julia]",
"ewatercycle>=2.4.0",
"remotebmi",
"toml",
]

Expand All @@ -42,9 +42,13 @@ dev = [
WflowJl = "ewatercycle_wflowjl.model:WflowJl"

[project.entry-points."ewatercycle.forcings"]
WflowJlForcing = "ewatercycle_wflow.forcing.forcing:WflowJlForcing"
WflowJlForcing = "ewatercycle_wflowjl.forcing.forcing:WflowJlForcing"

[tool.ruff]
target-version = "py310"
extend-exclude = ["*.ipynb"]

[tool.ruff.lint]
select = ["E", "F", "B", "D", "C90", "I", "N", "UP", "PLE", "PLR", "PLW"]
extend-select = ["D401", "D400", "D404", "TID252"]
ignore = [
Expand All @@ -53,12 +57,11 @@ ignore = [
"N813",
]
dummy-variable-rgx = "^(_+|(_+[a-zA-Z0-9_]*[a-zA-Z0-9]+?))$"
target-version = "py310"

[tool.ruff.pydocstyle]
[tool.ruff.lint.pydocstyle]
convention = "google"

[tool.ruff.isort]
[tool.ruff.lint.isort]
known-first-party = ["ewatercycle_wflow"]
force-single-line = true
lines-after-imports = 2
3 changes: 2 additions & 1 deletion src/ewatercycle_wflowjl/__init__.py
Original file line number Diff line number Diff line change
@@ -1,2 +1,3 @@
"""eWaterCycle plugin for Wflow.jl."""
__version__ = "0.0.1"

__version__ = "0.2.0"
1 change: 1 addition & 0 deletions src/ewatercycle_wflowjl/forcing/diagnostic_script.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
"""wflow diagnostic."""

import logging
from pathlib import Path

Expand Down
1 change: 1 addition & 0 deletions src/ewatercycle_wflowjl/forcing/forcing.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
"""Forcing related functionality for wflow."""

from datetime import datetime
from pathlib import Path

Expand Down
1 change: 1 addition & 0 deletions src/ewatercycle_wflowjl/forcing/makkink.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
"""Makkink formula for potential evaporation, implemented for Iris."""

import logging
from pathlib import Path

Expand Down
45 changes: 9 additions & 36 deletions src/ewatercycle_wflowjl/model.py
Original file line number Diff line number Diff line change
@@ -1,21 +1,20 @@
"""Wflow.jl eWaterCycle Model."""

import datetime
from collections.abc import Iterable
from pathlib import Path
from typing import Literal

import numpy as np
import pandas as pd
import toml
import xarray as xr
from bmipy import Bmi
from ewatercycle.base.model import LocalModel
from ewatercycle.base.model import ContainerizedModel
from ewatercycle.base.model import eWaterCycleModel
from ewatercycle.base.parameter_set import ParameterSet
from ewatercycle.container import ContainerImage
from ewatercycle.util import geographical_distances
from ewatercycle.util import get_time
from grpc4bmi.bmi_julia_model import BmiJulia
from juliacall import JuliaError
from juliacall import Main as jl
from pydantic import PrivateAttr
from pydantic import model_validator

Expand Down Expand Up @@ -201,39 +200,13 @@ def _make_cfg_file(self, **kwargs) -> Path:
return config_file


def install_wflow():
"""Install Wflow.jl with the BMI branch."""
jl.seval("import Pkg")
jl.Pkg.add(name="Wflow", rev="BMI")
jl.Pkg.add("BasicModelInterface")


def check_wflow_install():
"""Check if Wflow is installed in the Juliacall environment."""
try:
jl.seval("using Wflow")
except JuliaError as e:
if "not found in current path" in str(e):
install_wflow()
else:
raise


class WflowBmi(BmiJulia):
"""Wflow.jl Basic Model Interface."""

def __init__(self):
"""Wflow.jl Basic Model Interface."""
check_wflow_install()

m = self.from_name("Wflow.Model", implementation_name="Wflow.BMI")
super().__init__(m.model, m.implementation)


class WflowJl(WflowJlMixins, LocalModel):
class WflowJl(WflowJlMixins, ContainerizedModel):
"""Wflow.jl eWaterCycle LocalModel."""

bmi_class: type[Bmi] = WflowBmi
bmi_image: ContainerImage = ContainerImage(
"ghcr.io/ewatercycle/wflowjl-remotebmi:0.2.0"
)
protocol: Literal["grpc", "openapi"] = "openapi"

def get_latlon_grid(self, name: str) -> tuple[np.ndarray, np.ndarray, np.ndarray]:
"""Grid latitude, longitude and shape for variable.
Expand Down
18 changes: 0 additions & 18 deletions src/ewatercycle_wflowjl/utils.py

This file was deleted.

0 comments on commit 07adbef

Please sign in to comment.