https://github.com/mwtoews/surface-water-network
A Python package to create and analyze surface water networks.
https://github.com/mwtoews/surface-water-network
hydrology modflow python surface-water
Last synced: 9 months ago
JSON representation
A Python package to create and analyze surface water networks.
- Host: GitHub
- URL: https://github.com/mwtoews/surface-water-network
- Owner: mwtoews
- License: bsd-3-clause
- Created: 2019-05-21T01:32:38.000Z (about 7 years ago)
- Default Branch: main
- Last Pushed: 2025-07-07T17:08:34.000Z (11 months ago)
- Last Synced: 2025-08-13T02:22:22.392Z (10 months ago)
- Topics: hydrology, modflow, python, surface-water
- Language: Python
- Homepage: https://mwtoews.github.io/surface-water-network/
- Size: 7.89 MB
- Stars: 29
- Watchers: 6
- Forks: 6
- Open Issues: 13
-
Metadata Files:
- Readme: README.md
- License: LICENSE
- Citation: CITATION.cff
Awesome Lists containing this project
- open-sustainable-technology - Surface water network - A Python package to create and analyze surface water networks. (Hydrosphere / Freshwater and Hydrology)
README
# Surface water network
[](https://zenodo.org/badge/latestdoi/187739645)
[](https://app.codacy.com/manual/mwtoews/surface-water-network?utm_source=github.com&utm_medium=referral&utm_content=mwtoews/surface-water-network&utm_campaign=Badge_Grade_Dashboard)
[](https://codecov.io/gh/mwtoews/surface-water-network)
[](https://github.com/mwtoews/surface-water-network/actions/workflows/tests.yml)
A Python package to create and analyze surface water networks.
## Python packages
Python 3.10+ is required.
### Required
- `geopandas` - process spatial data similar to pandas
- `packaging` - used to check package versions
- `pandas` - tabular data analysis
- `pyproj` - spatial projection support
- `rtree` - spatial index support
### Optional
- `flopy >=3.3.6` - read/write MODFLOW models
- `netCDF4` - used to read TopNet files
## Testing
Run `pytest -v` or `python3 -m pytest -v`
For faster multi-core `pytest -v -n 2` (with `pytest-xdist`)
To run doctests `pytest -v swn --doctest-modules`
## Examples
```python
import geopandas
import pandas as pd
import swn
```
Read from Shapefile:
```python
shp_srs = "tests/data/DN2_Coastal_strahler1z_stream_vf.shp"
lines = geopandas.read_file(shp_srs)
lines.set_index("nzsegment", inplace=True, verify_integrity=True) # optional
```
Or, read from PostGIS:
```python
from sqlalchemy import create_engine, engine
con_url = engine.url.URL(drivername="postgresql", database="scigen")
con = create_engine(con_url)
sql = "SELECT * FROM wrc.rec2_riverlines_coastal"
lines = geopandas.read_postgis(sql, con)
lines.set_index("nzsegment", inplace=True, verify_integrity=True) # optional
```
Initialise and create network:
```python
n = swn.SurfaceWaterNetwork.from_lines(lines.geometry)
print(n)
#
```
Plot the network, write a Shapefile, write and read a SurfaceWaterNetwork file:
```python
n.plot()
swn.file.gdf_to_shapefile(n.segments, "segments.shp")
n.to_pickle("network.pkl")
n = swn.SurfaceWaterNetwork.from_pickle("network.pkl")
```
Remove segments that meet a condition (stream order), or that are
upstream/downstream from certain locations:
```python
n.remove(
n.segments.stream_order == 1,
segnums=n.gather_segnums(upstream=3047927))
```
Read flow data from a TopNet netCDF file, convert from m3/s to m3/day:
```python
nc_path = "tests/data/streamq_20170115_20170128_topnet_03046727_strahler1.nc"
flow = swn.file.topnet2ts(nc_path, "mod_flow", 86400)
# remove time and truncate to closest day
flow.index = flow.index.floor("d")
# 7-day mean
flow7d = flow.resample("7D").mean()
# full mean
flow_m = pd.DataFrame(flow.mean(0)).T
```
Process a MODFLOW/flopy model:
```python
import flopy
m = flopy.modflow.Modflow.load("h.nam", model_ws="tests/data", check=False)
nm = swn.SwnModflow.from_swn_flopy(n, m)
nm.default_segment_data()
nm.set_segment_data_inflow(flow_m)
nm.plot()
nm.to_pickle("sfr_network.pkl")
nm = swn.SwnModflow.from_pickle("sfr_network.pkl", n, m)
nm.set_sfr_obj()
m.sfr.write_file("file.sfr")
nm.grid_cells.to_file("grid_cells.shp")
nm.reaches.to_file("reaches.shp")
```
## Citation
Toews, M. W.; Hemmings, B. 2019. A surface water network method for generalising streams and rapid groundwater model development. In: New Zealand Hydrological Society Conference, Rotorua, 3-6 December, 2019. p. 166-169.