Skip to content

Commit

Permalink
Merge main into branch
Browse files Browse the repository at this point in the history
  • Loading branch information
Dobson committed May 9, 2024
2 parents 6a99775 + acf82fb commit dcda2ec
Show file tree
Hide file tree
Showing 5 changed files with 19 additions and 42 deletions.
2 changes: 1 addition & 1 deletion dev-requirements.txt
Original file line number Diff line number Diff line change
Expand Up @@ -318,7 +318,7 @@ tqdm==4.66.2
# via
# cdsapi
# swmmanywhere (pyproject.toml)
typing-extensions==4.9.0
typing-extensions==4.11.0
# via
# mypy
# pydantic
Expand Down
2 changes: 1 addition & 1 deletion requirements.txt
Original file line number Diff line number Diff line change
Expand Up @@ -260,7 +260,7 @@ tqdm==4.66.2
# via
# cdsapi
# swmmanywhere (pyproject.toml)
typing-extensions==4.9.0
typing-extensions==4.11.0
# via
# pydantic
# pydantic-core
Expand Down
28 changes: 6 additions & 22 deletions swmmanywhere/geospatial_utilities.py
Original file line number Diff line number Diff line change
Expand Up @@ -36,8 +36,6 @@
import pysheds # noqa: E402
from pysheds import grid as pgrid # noqa: E402

from swmmanywhere.custom_logging import logger # noqa: E402

TransformerFromCRS = lru_cache(pyproj.transformer.Transformer.from_crs)

def get_utm_epsg(x: float,
Expand Down Expand Up @@ -661,33 +659,19 @@ def derive_subbasins_streamorder(fid: Path,
transform = grid.affine,
)

# Iterate over stream orders to find the first one with subbasins
streamorder_ = streamorder
subbasins = np.zeros_like(flow_dir)
while np.unique(subbasins.reshape(-1)).shape[0] == 1:
if streamorder == 0:
raise ValueError("""No subbasins found in derive_subbasins_streamorder.
Fix your DEM""")


subbasins, _ = flw.subbasins_streamorder(min_sto=streamorder)
streamorder -= 1
# Identify stream order
subbasins, _ = flw.subbasins_streamorder(min_sto=streamorder)
if np.unique(subbasins.reshape(-1)).shape[0] == 1:
raise ValueError("""No subbasins found in derive_subbasins_streamorder.
Use a lower `subcatchment_derivation.subbasin_streamorder` and
probably check your DEM.""")

gdf_bas = vectorize(subbasins.astype(np.int32),
0,
flw.transform,
grid.crs,
name="basin")

if (
(streamorder != streamorder_ - 1) &
(os.getenv("SWMMANYWHERE_VERBOSE", "false").lower() == "true")
):
logger.warning(f"""Stream order {streamorder_} resulted in no subbasins.
Using {streamorder + 1} instead. You can manually inspect
these at {fid.parent / 'subbasins.geojson'}.""")
gdf_bas.to_file(fid.parent / 'subbasins.geojson', driver='GeoJSON')

return gdf_bas


Expand Down
26 changes: 9 additions & 17 deletions tests/test_graph_utilities.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,10 +12,10 @@

import geopandas as gpd
import networkx as nx
import pytest
from shapely import geometry as sgeom

from swmmanywhere import parameters
from swmmanywhere.custom_logging import logger
from swmmanywhere.graph_utilities import graphfcns as gu
from swmmanywhere.graph_utilities import iterate_graphfcns, load_graph, save_graph

Expand Down Expand Up @@ -563,19 +563,11 @@ def test_clip_to_catchments():
assert len(G_.edges) == 28

# Check streamorder adjustment
os.environ['SWMMANYWHERE_VERBOSE'] = 'true'
logger.add(temp_path / 'test.log')
subcatchment_derivation = parameters.SubcatchmentDerivation(
subbasin_streamorder = 5,
subbasin_membership = 0.9,
subbasin_clip_method = 'community'
)
G_ = gu.clip_to_catchments(G,
addresses=addresses,
subcatchment_derivation=subcatchment_derivation)
with open(temp_path / 'test.log', 'r') as f:
log = f.read()
assert 'Stream order 5 resulted in no subbasins' in log
assert 'Using 4 instead' in log
logger.remove()

with pytest.raises(ValueError):
subcatchment_derivation = parameters.SubcatchmentDerivation(
subbasin_streamorder = 5,
subbasin_membership = 0.9
)
G_ = gu.clip_to_catchments(G,
addresses=addresses,
subcatchment_derivation=subcatchment_derivation)
3 changes: 2 additions & 1 deletion tests/test_swmmanywhere.py
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,8 @@ def test_swmmanywhere():
'building': str(test_data_dir / 'building.geoparquet'),
'precipitation': str(defs_dir / 'storm.dat')
}

config['parameter_overrides'] = {'subcatchment_derivation' :
{'subbasin_streamorder' : 5}}
config['run_settings']['duration'] = 1000
api_keys = {'nasadem_key' : 'b206e65629ac0e53d599e43438560d28'}
with open(base_dir / 'api_keys.yml', 'w') as f:
Expand Down

0 comments on commit dcda2ec

Please sign in to comment.