src.flood_model.bg_flood_model

This script handles the processing of input files for the BG-Flood Model, executes the flood model, stores the resulting model output metadata in the database, and incorporates the model output into GeoServer for visualization.

Attributes

log

Base

sample_polygon

Functions

get_valid_bg_flood_dir(→ pathlib.Path)

Get the valid BG-Flood Model directory.

get_new_model_output_path(→ pathlib.Path)

Get a new file path for saving the BG Flood model output with the current timestamp included in the filename.

get_model_output_metadata(→ Tuple[str, str, str])

Get metadata related to the BG Flood model output.

store_model_output_metadata_to_db(→ int)

Store metadata related to the BG Flood model output in the database.

model_output_from_db_by_id(→ pathlib.Path)

Retrieves the path to the model output file from the database by model_id

model_extents_from_db_by_id(→ geopandas.GeoDataFrame)

Finds the extents of a model output in gpd.GeoDataFrame format

add_crs_to_model_output(→ None)

Add Coordinate Reference System (CRS) to the BG-Flood model output.

process_rain_input_files(→ None)

Process rain input files and write their parameter values to the BG-Flood parameter file.

process_boundary_input_files(→ None)

Process uniform boundary input files and write their parameter values to the BG-Flood parameter file.

process_river_input_files(→ None)

Process river input files, rename them, and write their parameter values to the BG-Flood parameter file.

prepare_bg_flood_model_inputs(→ None)

Prepare inputs for the BG-Flood Model.

run_bg_flood_model(→ None)

Run the BG-Flood Model for the specified catchment area.

main(→ int)

Generate BG-Flood model output for the requested catchment area, and incorporate the model output to GeoServer

Module Contents

src.flood_model.bg_flood_model.log
src.flood_model.bg_flood_model.Base
src.flood_model.bg_flood_model.get_valid_bg_flood_dir() pathlib.Path

Get the valid BG-Flood Model directory.

Returns:

The valid BG-Flood Model directory.

Return type:

pathlib.Path

Raises:

FileNotFoundError – If the BG-Flood Model directory is not found or is not a valid directory.

src.flood_model.bg_flood_model.get_new_model_output_path() pathlib.Path

Get a new file path for saving the BG Flood model output with the current timestamp included in the filename.

Returns:

The path to the BG Flood model output file.

Return type:

pathlib.Path

src.flood_model.bg_flood_model.get_model_output_metadata(model_output_path: pathlib.Path, catchment_area: geopandas.GeoDataFrame) Tuple[str, str, str]

Get metadata related to the BG Flood model output.

Parameters:
  • model_output_path (pathlib.Path) – The path to the BG Flood model output file.

  • catchment_area (gpd.GeoDataFrame) – A GeoDataFrame representing the catchment area.

Returns:

A tuple containing three elements: the name of the BG Flood model output file, its absolute path as a string, and the Well-Known Text (WKT) representation of the catchment area’s geometry.

Return type:

Tuple[str, str, str]

src.flood_model.bg_flood_model.store_model_output_metadata_to_db(engine: sqlalchemy.engine.Engine, model_output_path: pathlib.Path, catchment_area: geopandas.GeoDataFrame) int

Store metadata related to the BG Flood model output in the database.

Parameters:
  • engine (Engine) – The engine used to connect to the database.

  • model_output_path (pathlib.Path) – The path to the BG Flood model output file.

  • catchment_area (gpd.GeoDataFrame) – A GeoDataFrame representing the catchment area.

Returns:

Returns the model id of the new flood_model produced

Return type:

int

src.flood_model.bg_flood_model.model_output_from_db_by_id(engine: sqlalchemy.engine.Engine, model_id: int) pathlib.Path

Retrieves the path to the model output file from the database by model_id

Parameters:
  • engine (Engine) – The sqlalchemy database connection engine

  • model_id (int) – The ID of the flood model output being queried for

Returns:

The path to the model output file

Return type:

pathlib.Path

src.flood_model.bg_flood_model.model_extents_from_db_by_id(engine: sqlalchemy.engine.Engine, model_id: int) geopandas.GeoDataFrame

Finds the extents of a model output in gpd.GeoDataFrame format

Parameters:
  • engine (Engine) – The sqlalchemy database connection engine

  • model_id (int) – The ID of the flood model output being queried for

Returns:

Returns the geometry (extents) of the flood model output.

Return type:

gpd.GeoDataFrame

src.flood_model.bg_flood_model.add_crs_to_model_output(engine: sqlalchemy.engine.Engine, flood_model_output_id: int) None

Add Coordinate Reference System (CRS) to the BG-Flood model output.

Parameters:
  • engine (Engine) – The sqlalchemy database connection engine

  • flood_model_output_id (int) – The ID of the flood model output being queried for

Returns:

This function does not return any value.

Return type:

None

src.flood_model.bg_flood_model.process_rain_input_files(bg_flood_dir: pathlib.Path, param_file: TextIO) None

Process rain input files and write their parameter values to the BG-Flood parameter file.

Parameters:
  • bg_flood_dir (pathlib.Path) – The BG-Flood model directory containing the rain input files.

  • param_file (TextIO) – The file object representing the parameter file where the parameter values will be written.

Returns:

This function does not return any value.

Return type:

None

src.flood_model.bg_flood_model.process_boundary_input_files(bg_flood_dir: pathlib.Path, param_file: TextIO) None

Process uniform boundary input files and write their parameter values to the BG-Flood parameter file.

Parameters:
  • bg_flood_dir (pathlib.Path) – The BG-Flood model directory containing the uniform boundary input files.

  • param_file (TextIO) – The file object representing the parameter file where the parameter values will be written.

Returns:

This function does not return any value.

Return type:

None

src.flood_model.bg_flood_model.process_river_input_files(bg_flood_dir: pathlib.Path, param_file: TextIO) None

Process river input files, rename them, and write their parameter values to the BG-Flood parameter file.

Parameters:
  • bg_flood_dir (pathlib.Path) – The BG-Flood model directory containing the river input files.

  • param_file (TextIO) – The file object representing the parameter file where the parameter values will be written.

Returns:

This function does not return any value.

Return type:

None

src.flood_model.bg_flood_model.prepare_bg_flood_model_inputs(bg_flood_dir: pathlib.Path, model_output_path: pathlib.Path, hydro_dem_path: pathlib.Path, resolution: int | float, output_timestep: int | float, end_time: int | float, mask: int | float = 9999, gpu_device: int = 0, small_nc: int = 0) None

Prepare inputs for the BG-Flood Model.

Parameters:
  • bg_flood_dir (pathlib.Path) – The BG-Flood Model directory.

  • model_output_path (pathlib.Path) – The new file path for saving the BG Flood model output with the current timestamp included in the filename.

  • hydro_dem_path (pathlib.Path,) – The file path of the Hydrologically conditioned DEM (Hydro DEM) for the specified catchment area.

  • resolution (Union[int, float]) – The grid resolution in meters for metric grids, representing the size of each grid cell.

  • output_timestep (Union[int, float]) – Time step between model outputs in seconds. If the value is set to 0 then no output is generated.

  • end_time (Union[int, float]) – Time in seconds when the model stops. If the value is set to 0 then the model initializes but does not run.

  • mask (Union[int, float] = 9999) – The mask value is used to remove blocks from computation where the topography elevation (zb) is greater than the specified value. Default value is 9999.0 (no areas are masked).

  • gpu_device (int = 0) – Specify the GPU device to be used. Default value is 0 (the first available GPU). Set the value to -1 to use the CPU. For other GPUs, use values 2 and above.

  • small_nc (int = 0) – Specify whether the output should be saved as short integers to reduce the size of the output file. Set the value to 1 to enable short integer conversion, or set it to 0 to save all variables as floats. Default value is 0.

Returns:

This function does not return any value.

Return type:

None

src.flood_model.bg_flood_model.run_bg_flood_model(engine: sqlalchemy.engine.Engine, catchment_area: geopandas.GeoDataFrame, model_output_path: pathlib.Path, output_timestep: int | float, end_time: int | float, resolution: int | float | None = None, mask: int | float = 9999, gpu_device: int = 0, small_nc: int = 0) None

Run the BG-Flood Model for the specified catchment area.

Parameters:
  • engine (Engine) – The engine used to connect to the database.

  • catchment_area (gpd.GeoDataFrame) – A GeoDataFrame representing the catchment area.

  • model_output_path (pathlib.Path) – The new file path for saving the BG Flood model output with the current timestamp included in the filename.

  • output_timestep (Union[int, float]) – Time step between model outputs in seconds. If the value is set to 0 then no output is generated.

  • end_time (Union[int, float]) – Time in seconds when the model stops. If the value is set to 0 then the model initializes but does not run.

  • resolution (Optional[Union[int, float]] = None) – The grid resolution in meters for metric grids, representing the size of each grid cell. If not provided (default is None), the resolution of the Hydrologically conditioned DEM will be used as the grid resolution.

  • mask (Union[int, float] = 9999) – The mask value is used to remove blocks from computation where the topography elevation (zb) is greater than the specified value. Default value is 9999.0 (no areas are masked).

  • gpu_device (int = 0) – Specify the GPU device to be used. Default value is 0 (the first available GPU). Set the value to -1 to use the CPU. For other GPUs, use values 2 and above.

  • small_nc (int = 0) – Specify whether the output should be saved as short integers to reduce the size of the output file. Set the value to 1 to enable short integer conversion, or set it to 0 to save all variables as floats. Default value is 0.

Returns:

This function does not return any value.

Return type:

None

src.flood_model.bg_flood_model.main(selected_polygon_gdf: geopandas.GeoDataFrame, output_timestep: int | float, end_time: int | float, resolution: int | float | None = None, mask: int | float = 9999, gpu_device: int = 0, small_nc: int = 0, log_level: src.digitaltwin.utils.LogLevel = LogLevel.DEBUG) int

Generate BG-Flood model output for the requested catchment area, and incorporate the model output to GeoServer for visualization.

Parameters:
  • selected_polygon_gdf (gpd.GeoDataFrame) – A GeoDataFrame representing the selected polygon, i.e., the catchment area.

  • output_timestep (Union[int, float]) – Time step between model outputs in seconds. If the value is set to 0 then no output is generated.

  • end_time (Union[int, float]) – Time in seconds when the model stops. If the value is set to 0 then the model initializes but does not run.

  • resolution (Optional[Union[int, float]] = None) – The grid resolution in meters for metric grids, representing the size of each grid cell. If not provided (default is None), the resolution of the Hydrologically conditioned DEM will be used as the grid resolution.

  • mask (Union[int, float] = 9999) – The mask value is used to remove blocks from computation where the topography elevation (zb) is greater than the specified value. Default value is 9999.0 (no areas are masked).

  • gpu_device (int = 0) – Specify the GPU device to be used. Default value is 0 (the first available GPU). Set the value to -1 to use the CPU. For other GPUs, use values 2 and above.

  • small_nc (int = 0) – Specify whether the output should be saved as short integers to reduce the size of the output file. Set the value to 1 to enable short integer conversion, or set it to 0 to save all variables as floats. Default value is 0.

  • log_level (LogLevel = LogLevel.DEBUG) – The log level to set for the root logger. Defaults to LogLevel.DEBUG. The available logging levels and their corresponding numeric values are: - LogLevel.CRITICAL (50) - LogLevel.ERROR (40) - LogLevel.WARNING (30) - LogLevel.INFO (20) - LogLevel.DEBUG (10) - LogLevel.NOTSET (0)

Returns:

Returns the model id of the new flood_model produced

Return type:

int

src.flood_model.bg_flood_model.sample_polygon