Skip to content

Commit

Permalink
earlier error message if subbasin is too small #236
Browse files Browse the repository at this point in the history
  • Loading branch information
hboisgon committed Apr 3, 2024
1 parent 565c9f7 commit 69f3b9e
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 3 deletions.
7 changes: 7 additions & 0 deletions hydromt_wflow/wflow.py
Expand Up @@ -417,6 +417,13 @@ def setup_rivers(
"""
self.logger.info("Preparing river maps.")

# Check that river_upa threshold is bigger than the maximum uparea in the grid
if river_upa < self.grid[self._MAPS["uparea"]].max():
raise ValueError(
f"river_upa threshold {river_upa} should be larger than the maximum \
uparea in the grid {self.grid[self._MAPS['uparea']].max()}"
)

rivdph_methods = ["gvf", "manning", "powlaw"]
if rivdph_method not in rivdph_methods:
raise ValueError(f'"{rivdph_method}" unknown. Select from {rivdph_methods}')
Expand Down
24 changes: 21 additions & 3 deletions hydromt_wflow/workflows/basemaps.py
Expand Up @@ -99,9 +99,27 @@ def hydrography(
# NOTE: this mask is passed on from get_basin_geometry method
logger.debug("Mask in dataset assumed to represent subbasins.")
ncells = np.sum(ds["mask"].values)
logger.debug(f"(Sub)basin at original resolution has {ncells} cells.")

scale_ratio = int(np.round(res / ds.raster.res[0]))

if ncells < 4:
raise ValueError(
"(Sub)basin at original resolution should at least consist of two cells on "
f"each axis and the total number of cells is {ncells}. "
"Consider using a larger domain or higher spatial resolution. "
"For subbasin models, consider a (higher) threshold on for example "
"upstream area or stream order to snap the outlet."
)
elif ncells < 100 and scale_ratio > 1:
logger.warning(
f"(Sub)basin at original resolution is small and has {ncells} cells. "
"This may results in errors later when upscaling flow directions. "
"If so, consider using a larger domain or higher spatial resolution. "
"For subbasin models, consider a (higher) threshold on for example "
"upstream area or stream order to snap the outlet."
)
else:
logger.debug(f"(Sub)basin at original resolution has {ncells} cells.")

if scale_ratio > 1: # upscale flwdir
if flwdir is None:
# NOTE initialize with mask is FALSE
Expand Down Expand Up @@ -251,7 +269,7 @@ def hydrography(
logger.debug(f"Outlet coordinates ({len(xy_pit[0])}/{npits}): {xy_pit_str}.")
if np.any(np.asarray(ds_out.raster.shape) == 1):
raise ValueError(
"The output extent should at consist of two cells on each axis. "
"The output extent should at least consist of two cells on each axis. "
"Consider using a larger domain or higher spatial resolution. "
"For subbasin models, consider a (higher) threshold to snap the outlet."
)
Expand Down

0 comments on commit 69f3b9e

Please sign in to comment.