diff --git a/dev-requirements.txt b/dev-requirements.txt index 516f4344..8d4aad8e 100644 --- a/dev-requirements.txt +++ b/dev-requirements.txt @@ -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 diff --git a/requirements.txt b/requirements.txt index b46234e2..a69d2809 100644 --- a/requirements.txt +++ b/requirements.txt @@ -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 diff --git a/swmmanywhere/geospatial_utilities.py b/swmmanywhere/geospatial_utilities.py index 86d1c458..29d918f7 100644 --- a/swmmanywhere/geospatial_utilities.py +++ b/swmmanywhere/geospatial_utilities.py @@ -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, @@ -661,17 +659,12 @@ 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, @@ -679,15 +672,6 @@ def derive_subbasins_streamorder(fid: Path, 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 diff --git a/tests/test_graph_utilities.py b/tests/test_graph_utilities.py index 9b2484f4..45ef1c48 100644 --- a/tests/test_graph_utilities.py +++ b/tests/test_graph_utilities.py @@ -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 @@ -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() - \ No newline at end of file + 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) diff --git a/tests/test_swmmanywhere.py b/tests/test_swmmanywhere.py index c043f172..3e6d829c 100644 --- a/tests/test_swmmanywhere.py +++ b/tests/test_swmmanywhere.py @@ -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: