A basin's water balanced day by day across coupled models (SWAT+, NextGen, MODFLOW 6 and water users) through BMI
pip install basinledger Copy PIP instructions
A basin's water, quantity and quality, balanced day by day across coupled models.
The code was written with AI assistance (Claude, by Anthropic), designed and directed by Andrew Sommerlot. See AI use.
Basin Ledger couples a watershed model, a groundwater model and the models of large water users through BMI, and steps them together one day at a time with one Python driver:
Every volume and mass that moves between models is entered in a ledger twice, as sent and as received. A day where they differ stops the run. Basin Ledger is a sibling of Water Ledger. Water Ledger estimates one campus's water from public data. Basin Ledger follows that water through a basin's streams and aquifers.
flowchart LR U["water users (BMI)
hourly: withdrawal, consumption,
return, loads, power"] -- "day's volumes" --> D D["driver
one day at a time"] -- "update()" --> S["SWAT+
(swatplus-bmi)"] S -- "aquifer recharge, m/s" --> D D -- "recharge by cell, well rates
(set between prepare and solve)" --> M["MODFLOW 6
(libmf6, modflowapi)"] M -- "simulated flows, heads" --> D D --> L[("ledger
every day")] A NextGen run takes SWAT+'s place as a replay. It gives calibrated flows on NextGen geography, but without feedback (see docs/design.md).
Places are examples, not code. Each example (examples/ /) is one config file that wires real models for one basin and its users, plus a script that fetches its public data and a short write-up. The engine knows nothing about any place or any kind of water user.
Needs Python 3.11 or later, and gfortran for SWAT+ (on macOS, Homebrew's gcc).
pip install "basinledger[users,nextgen]" # or: uv add "basinledger[users,nextgen]" The extras:
Two model programs aren't Python packages, and come separately:
SWAT+ is built by swatplus-bmi, which is installed with Basin Ledger. It fetches SWAT+ 62.0.1, patches it and builds it, in a few minutes:
swatplus-bmi build MODFLOW 6's shared library (libmf6) comes from USGS's release through flopy's get-modflow, also installed with Basin Ledger. Basin Ledger looks for it where :flopy puts it, or at $LIBMF6:
get-modflow :flopy --repo modflow6 --subset mf6,libmf6.dylib # libmf6.so on Linux From a source checkout, for development and the tests, with uv:
git clone https://github.com/northlinehydrology/basin-ledger && cd basin-ledger uv sync uv run python examples/synthetic/fetch.py --get # builds SWAT+ (swatplus-bmi build) and fetches libmf6 (get-modflow) uv run pytest # about a minute on one core; -m "not slow" takes about 10 s uv run --group examples marimo edit examples/synthetic/notebook.py Continuous testing:
uv run python -m basinledger run examples/synthetic/surface.toml # writes ledger.csv, series.csv, summary.json uv run python -m basinledger run examples/synthetic/surface.toml --baseline # the same without its water users uv run python -m basinledger run examples/synthetic/two_way.toml --save-at 365 --save runs/warm uv run python -m basinledger run examples/synthetic/two_way.toml --restart runs/warm --run scenario uv run python -m basinledger status examples/synthetic/surface.toml # calibrated, or a screening run Or from Python:
from basinledger.driver import Basin b = Basin.from_config("examples/synthetic/basin.toml", run="pumping") b.run() # every day of the SWAT+ project; the ledger checks each day print(b.ledger.totals()) # m3 (and kg, MWh) per account for the whole run b.finalize() An example's config (abridged; the full schema is in src/basinledger/config.py):
[swatplus] project = "{swatplus_refdata}/Osu_1hru" [modflow6] builder = "model:build" # model.py writes the MODFLOW 6 simulation for the run's days [recharge] map = "all:aqu001" # SWAT+ aquifers to MODFLOW 6 cells by area, or a CSV map [[wells]] name = "pw1" cell = [0, 20, 28] [[utilities]] name = "city" sources = [{route = "well:pw1", share = 1.0}] losses = 0.10 outfall = "swat:point_source:wwtp" [[users]] name = "plant" component = "basinledger.testing:SyntheticUser" # or waterledger_models.datacenter.bmi:CoolingWaterBmi withdrawal = "utility:city" return = "utility:city" [[rules]] name = "plant well limit" kind = "drawdown_limit" user = "plant" cell = [0, 20, 28] head_min = 88.3 limit = 0.002
The synthetic example: SWAT+'s Osu_1hru recharge drives a small MODFLOW 6 aquifer, and a synthetic user pumps one well. Drawdown is the difference from the same run without the user.
Any BMI component that reports its withdrawal, consumption and return can be a water user. Its gain, storage, constituent loads, return temperature and electricity are optional. A component that uses its own variable names is mapped role by role in the config. The engine checks each user's balance every step: withdrawal + gain = consumption + return + change in storage. It then integrates the hourly rates into the daily volumes the hydrologic models take. See docs/design.md.
Earlier SWAT and SWAT+ couplings with MODFLOW either compile MODFLOW-NWT into one program (SWAT-MODFLOW, SWAT+MODFLOW) or use SWAT+'s own gwflow module. See docs/prior-art.md.
Cite the archived software (Zenodo; the DOI will be listed here once the first release is archived) and, where SWAT+ is used, swatplus-bmi (doi:10.5281/zenodo.23963201). Details in CITATION.cff.
Basin Ledger was designed and directed by Andrew Sommerlot and built with an AI coding assistant (Claude, by Anthropic), which wrote most of the code, tests and documentation under his direction and review. Whether it is right rests on the tests: each model's own budget closes, and every transfer is checked against what the other model received.
BSD 3-Clause (LICENSE). SWAT+ is under the GNU LGPL 2.1 and runs as its own process through swatplus-bmi. MODFLOW 6 is public domain (USGS).
All 2 files in this release have provenance metadata.
BMI MODFLOW 6 NextGen SWAT+ groundwater hydrology model coupling water use
For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.
Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.
PyPI verified that this artifact, at this checksum, originated from the publisher listed below.
Signed by GitHub Actions, verified by PyPI on Nov 5, 2026.
All 2 files in this release have been uploaded with provenance metadata.
PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.
To learn more about protecting yourself from malicious actors, visit our package security documentation.




