Skip to content

Commit

Permalink
Merge remote-tracking branch 'ribasim-python/main' into merge
Browse files Browse the repository at this point in the history
  • Loading branch information
visr committed Mar 6, 2023
2 parents 43ae9ed + 3462f9b commit feca045
Show file tree
Hide file tree
Showing 37 changed files with 3,150 additions and 4 deletions.
7 changes: 3 additions & 4 deletions .github/workflows/docs.yml
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ jobs:
arch:
- x64
steps:
- uses: actions/checkout@v2
- uses: actions/checkout@v3
- uses: julia-actions/setup-julia@v1
with:
version: ${{ matrix.version }}
Expand All @@ -37,9 +37,8 @@ jobs:
# setup Python / Jupyter
- uses: actions/setup-python@v4
with:
python-version: '3.x'
# avoid jupyter-client 8: https://github.com/quarto-dev/quarto-cli/issues/4122
- run: pip install jupyter jupyter-client==7.4.9
python-version: '3.11'
- run: pip install jupyter

- name: Install IJulia
run: julia -e 'using Pkg; Pkg.add("IJulia")'
Expand Down
139 changes: 139 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -23,3 +23,142 @@ compile/create_binaries/libribasim/
JuliaSysimage.dll
LocalPreferences.toml
.ipynb_checkpoints
# Byte-compiled / optimized / DLL files
__pycache__/
*.py[cod]
*$py.class

# C extensions
*.so

# Distribution / packaging
.Python
build/
develop-eggs/
dist/
downloads/
eggs/
.eggs/
lib/
lib64/
parts/
sdist/
var/
wheels/
pip-wheel-metadata/
share/python-wheels/
*.egg-info/
.installed.cfg
*.egg
MANIFEST

# PyInstaller
# Usually these files are written by a python script from a template
# before PyInstaller builds the exe, so as to inject date/other infos into it.
*.manifest
*.spec

# Installer logs
pip-log.txt
pip-delete-this-directory.txt

# Unit test / coverage reports
htmlcov/
.tox/
.nox/
.coverage
.coverage.*
.cache
nosetests.xml
coverage.xml
*.cover
*.py,cover
.hypothesis/
.pytest_cache/

# Translations
*.mo
*.pot

# Django stuff:
*.log
local_settings.py
db.sqlite3
db.sqlite3-journal

# Flask stuff:
instance/
.webassets-cache

# Scrapy stuff:
.scrapy

# Sphinx documentation
docs/_build/

# PyBuilder
target/

# Jupyter Notebook
.ipynb_checkpoints

# IPython
profile_default/
ipython_config.py

# pyenv
.python-version

# pipenv
# According to pypa/pipenv#598, it is recommended to include Pipfile.lock in version control.
# However, in case of collaboration, if having platform-specific dependencies or dependencies
# having no cross-platform support, pipenv may install dependencies that don't work, or not
# install all needed dependencies.
#Pipfile.lock

# PEP 582; used by e.g. github.com/David-OConnor/pyflow
__pypackages__/

# Celery stuff
celerybeat-schedule
celerybeat.pid

# SageMath parsed files
*.sage.py

# Environments
.env
.venv
env/
venv/
ENV/
env.bak/
venv.bak/

# Spyder project settings
.spyderproject
.spyproject

# Rope project settings
.ropeproject

# mkdocs documentation
/site

# mypy
.mypy_cache/
.dmypy.json
dmypy.json

# Pyre type checker
.pyre/

# Generated docs
/docs

# VS Code settings
*.vscode

# Example models
/examples/basic
/examples/basic-transient
11 changes: 11 additions & 0 deletions README.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
ribasim-python
==============

A Python package for working with `Ribasim.jl <https://github.com/Deltares/Ribasim.jl>`_.


Documentation
-------------

API documentation can be found `here <https://deltares.github.io/ribasim-python/ribasim.html>`_.
See also the examples directory.
Empty file added docs/.gitkeep
Empty file.
18 changes: 18 additions & 0 deletions environment.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
name: ribasim

channels:
- conda-forge

dependencies:
- python >= 3.9
- matplotlib
- pandas
- geopandas
- pandera
- pyarrow
- pydantic
- pyogrio
- shapely >=2.0
- tomli
- tomli-w
- xarray
78 changes: 78 additions & 0 deletions examples/basic-transient.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,78 @@
# %%
import os

os.environ["USE_PYGEOS"] = "0"

import numpy as np
import pandas as pd
import xarray as xr

import ribasim

# %%

model = ribasim.Model.from_toml("basic/basic.toml")

# %%

time = pd.date_range(model.starttime, model.endtime)
day_of_year = time.day_of_year.values
seconds_per_day = 24 * 60 * 60
evaporation = (
(-1.0 * np.cos(day_of_year / 365.0 * 2 * np.pi) + 1.0) * 0.0025 / seconds_per_day
)
rng = np.random.default_rng()
precipitation = (
rng.lognormal(mean=-1.0, sigma=1.7, size=time.size) * 0.001 / seconds_per_day
)

# %%
# We'll use xarray to easily broadcast the values.

timeseries = (
pd.DataFrame(
data={
"node_id": 1,
"time": time,
"drainage": 0.0,
"potential_evaporation": evaporation,
"infiltration": 0.0,
"precipitation": precipitation,
"urban_runoff": 0.0,
}
)
.set_index("time")
.to_xarray()
)

basin_ids = model.basin.static["node_id"].unique()
basin_nodes = xr.DataArray(
np.ones(len(basin_ids)), coords={"node_id": basin_ids}, dims=["node_id"]
)
forcing = (timeseries * basin_nodes).to_dataframe().reset_index()

# %%

state = pd.DataFrame(
data={
"node_id": basin_ids,
"storage": 1000.0,
"concentration": 0.0,
}
)

# %%

model.basin.forcing = forcing
model.basin.state = state

# %%

model.write("basic-transient")
# %%
# After running the model, read back the input:

df = pd.read_feather(r"c:\src\ribasim.jl\examples\basic-transient\basin.arrow")
output = df.set_index(["time", "node_id"]).to_xarray()
output["level"].plot(hue="node_id")
# %%
Loading

0 comments on commit feca045

Please sign in to comment.