From 69f3b9e9fc5364a301d42047d987e95157c1ea35 Mon Sep 17 00:00:00 2001 From: hboisgon Date: Wed, 3 Apr 2024 17:57:37 +0800 Subject: [PATCH] earlier error message if subbasin is too small #236 --- hydromt_wflow/wflow.py | 7 +++++++ hydromt_wflow/workflows/basemaps.py | 24 +++++++++++++++++++++--- 2 files changed, 28 insertions(+), 3 deletions(-) diff --git a/hydromt_wflow/wflow.py b/hydromt_wflow/wflow.py index 8c2c56aa..a90f5496 100644 --- a/hydromt_wflow/wflow.py +++ b/hydromt_wflow/wflow.py @@ -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}') diff --git a/hydromt_wflow/workflows/basemaps.py b/hydromt_wflow/workflows/basemaps.py index 1440d0e2..3e517d4b 100644 --- a/hydromt_wflow/workflows/basemaps.py +++ b/hydromt_wflow/workflows/basemaps.py @@ -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 @@ -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." )