Deltares / hydromt_wflow

Wflow plugin for HydroMT
https://deltares.github.io/hydromt_wflow/
GNU General Public License v3.0
20 stars 15 forks source link

Checking (sub)basin dimensions when delineating (sub)basin geometry/mask #236

Closed shartgring closed 6 months ago

shartgring commented 10 months ago

Kind of request

Adding new functionality

Enhancement Description

It is possible that a user provides region settings that result in a basin being generated consisting of only 1 cell. For example, when only -r "{'subbasin': [x, y] is provided without strord settings. Later in the workflow, this will likely cause errors resulting in failed model building.

Use case

Consider the following log:

2024-02-02 11:31:02,337 - build - log - DEBUG - Writing log messages to new file C:\Users\hartgrin\OneDrive - Stichting Deltares\Projecten\PROMISCES\wflow_danube_SPARTACUS\hydromt.log.
2024-02-02 11:31:02,338 - build - log - INFO - HydroMT version: 0.9.2
2024-02-02 11:31:02,338 - build - main - INFO - Building instance of wflow model at C:\Users\hartgrin\OneDrive - Stichting Deltares\Projecten\PROMISCES\wflow_danube_SPARTACUS.
2024-02-02 11:31:02,339 - build - main - INFO - User settings:
2024-02-02 11:31:02,699 - build - data_catalog - INFO - Reading data catalog deltares_data v2024.1.30
2024-02-02 11:31:02,699 - build - data_catalog - INFO - Parsing data catalog from https://raw.githubusercontent.com/Deltares/hydromt/main/data/catalogs/deltares_data.yml
2024-02-02 11:31:03,082 - build - model_api - INFO - Initializing wflow model from hydromt_wflow (v0.4.1).
2024-02-02 11:31:03,083 - build - data_catalog - INFO - Parsing data catalog from C:\Users\hartgrin\.conda\envs\promisces\Lib\site-packages\hydromt_wflow\data\parameters_data.yml
2024-02-02 11:31:03,093 - build - model_api - DEBUG - Setting model config options.
2024-02-02 11:31:03,096 - build - model_api - DEBUG - Default config read from C:\Users\hartgrin\.conda\envs\promisces\Lib\site-packages\hydromt_wflow\data\wflow\wflow_sbm.toml
2024-02-02 11:31:03,096 - build - model_api - INFO - setup_basemaps.region: {'subbasin': [18.8813, 47.19553], 'bounds': [3.251953, 43.213183, 24.56543, 50.162824]}
2024-02-02 11:31:03,097 - build - model_api - INFO - setup_basemaps.res: 0.00833
2024-02-02 11:31:03,097 - build - model_api - INFO - setup_basemaps.hydrography_fn: merit_hydro
2024-02-02 11:31:03,097 - build - model_api - INFO - setup_basemaps.basin_index_fn: merit_hydro_index
2024-02-02 11:31:03,097 - build - model_api - INFO - setup_basemaps.upscale_method: ihu
2024-02-02 11:31:03,098 - build - wflow - INFO - Preparing base hydrography basemaps.
2024-02-02 11:31:03,339 - build - rasterdataset - INFO - Reading merit_hydro raster data from p:\wflow_global\hydromt\topography\merit_hydro\*.vrt
2024-02-02 11:31:05,449 - build - basin_mask - DEBUG - Parsed region (kind=subbasin): {'bounds': [3.251953, 43.213183, 24.56543, 50.162824], 'xy': [18.8813, 47.19553]}
2024-02-02 11:31:33,751 - build - basin_mask - WARNING - Loading very large spatial domain to derive a subbasin. Provide initial 'bounds' if this takes too long.
2024-02-02 11:36:00,078 - build - basin_mask - INFO - subbasin bbox: [18.8808, 47.1950, 18.8817, 47.1958]
2024-02-02 11:36:02,733 - build - wflow - DEBUG - Adding basins vector to geoms.
2024-02-02 11:36:02,739 - build - basemaps - DEBUG - Mask in dataset assumed to represent subbasins.
2024-02-02 11:36:02,740 - build - basemaps - DEBUG - (Sub)basin at original resolution has 1 cells.
2024-02-02 11:36:02,842 - build - basemaps - DEBUG - Burn subbasin outlet in upstream area data.
2024-02-02 11:36:04,070 - build - basemaps - INFO - Upscale flow direction data: 10x, ihu method.
2024-02-02 11:36:27,822 - build - main - ERROR - module 'numpy' has no attribute 'bool'.
`np.bool` was a deprecated alias for the builtin `bool`. To avoid this error in existing code, use `bool` by itself. Doing this will not modify any behavior and is safe. If you specifically wanted the numpy scalar type, use `np.bool_` here.
The aliases was originally deprecated in NumPy 1.20; for more details and guidance see the original release note at:
    https://numpy.org/devdocs/release/1.20.0-notes.html#deprecations
Traceback (most recent call last):
  File "C:\Users\hartgrin\AppData\Roaming\Python\Python311\site-packages\hydromt\cli\main.py", line 221, in build
    mod.build(region, opt=opt)
  File "C:\Users\hartgrin\AppData\Roaming\Python\Python311\site-packages\hydromt\models\model_api.py", line 246, in build
    self._run_log_method(method, **kwargs)
  File "C:\Users\hartgrin\AppData\Roaming\Python\Python311\site-packages\hydromt\models\model_api.py", line 188, in _run_log_method
    return func(*args, **kwargs)
           ^^^^^^^^^^^^^^^^^^^^^
  File "C:\Users\hartgrin\.conda\envs\promisces\Lib\site-packages\hydromt_wflow\wflow.py", line 240, in setup_basemaps
    ds_base, _ = workflows.hydrography(
                 ^^^^^^^^^^^^^^^^^^^^^^
  File "C:\Users\hartgrin\.conda\envs\promisces\Lib\site-packages\hydromt_wflow\workflows\basemaps.py", line 157, in hydrography
    ).astype(np.bool)
             ^^^^^^^
  File "C:\Users\hartgrin\AppData\Roaming\Python\Python311\site-packages\numpy\__init__.py", line 338, in __getattr__
    raise AttributeError(__former_attrs__[attr])
AttributeError: module 'numpy' has no attribute 'bool'.
`np.bool` was a deprecated alias for the builtin `bool`. To avoid this error in existing code, use `bool` by itself. Doing this will not modify any behavior and is safe. If you specifically wanted the numpy scalar type, use `np.bool_` here.
The aliases was originally deprecated in NumPy 1.20; for more details and guidance see the original release note at:
    https://numpy.org/devdocs/release/1.20.0-notes.html#deprecations

It may be nice to 'catch' these errors and provide a warning when a model consisting of 1 by 1 cell is generated (or n_x by n_y where n_x,y < 3). For example, in the form of an if-statement that may produce the log:

 - build - basin_mask - WARNING  - Basin dimensions are {n_x} cells by {n_y} cells. Consider using 'strord' argument to correctly delineate subbasin geometry

Additional Context

I was not sure whether this would fit better under hydromt core or wflow, my guess was wflow ;)

hboisgon commented 9 months ago

That's a good suggestion @shartgring ! I think hydromt wflow is a good place for it