wolfhece.acceptability.func
===========================

.. py:module:: wolfhece.acceptability.func

.. autoapi-nested-parse::

   Author: University of Liege, HECE, LEMA
   Date: 2024

   Copyright (c) 2024 University of Liege. All rights reserved.

   This script and its content are protected by copyright law. Unauthorized
   copying or distribution of this file, via any medium, is strictly prohibited.









Module Contents
---------------

.. py:data:: ENGINE
   :value: 'pyogrio'


.. py:data:: EXTENT
   :value: '.gpkg'


.. py:class:: Modif_Type(*args, **kwds)

   Bases: :py:obj:`enum.Enum`

   .. autoapi-inheritance-diagram:: wolfhece.acceptability.func.Modif_Type
      :parts: 1
      :private-bases:


   Enum class for the type of modification


   .. py:attribute:: WALOUS
      :value: 'Walous layers changed to PICC buidings'



   .. py:attribute:: POINT2POLY_EPURATION
      :value: 'Change BDREF based on AJOUT_PDET sent by Perrine (SPI)'



   .. py:attribute:: POINT2POLY_PICC
      :value: 'Convert the points to polygons based on PICC'



   .. py:attribute:: POINT2POLY_CAPAPICC
      :value: 'Convert the points to polygons based on PICC and CaPa'



   .. py:attribute:: INHABITED
      :value: 'Select only inhabited buildings'



   .. py:attribute:: ROAD
      :value: 'Create a buffer around the roads'



   .. py:attribute:: COPY
      :value: 'Copy the data'



.. py:class:: GPU_2D_file_extensions(*args, **kwds)

   Bases: :py:obj:`enum.Enum`

   .. autoapi-inheritance-diagram:: wolfhece.acceptability.func.GPU_2D_file_extensions
      :parts: 1
      :private-bases:


   Create a collection of name/value pairs.

   Example enumeration:

   >>> class Color(Enum):
   ...     RED = 1
   ...     BLUE = 2
   ...     GREEN = 3

   Access them by:

   - attribute access::

   >>> Color.RED
   <Color.RED: 1>

   - value lookup:

   >>> Color(1)
   <Color.RED: 1>

   - name lookup:

   >>> Color['RED']
   <Color.RED: 1>

   Enumerations can be iterated over, and know how many members they have:

   >>> len(Color)
   3

   >>> list(Color)
   [<Color.RED: 1>, <Color.BLUE: 2>, <Color.GREEN: 3>]

   Methods can be added to enumerations, and members can have their own
   attributes -- see the documentation for details.


   .. py:attribute:: TIF
      :value: '.tif'



   .. py:attribute:: TIFF
      :value: '.tiff'



   .. py:attribute:: PY
      :value: '.py'



   .. py:attribute:: NPY
      :value: '.npy'



   .. py:attribute:: BIN
      :value: '.bin'



   .. py:attribute:: JSON
      :value: '.json'



   .. py:attribute:: TXT
      :value: '.txt'



.. py:class:: Vulnerability_csv(file: pathlib.Path)

   .. py:attribute:: file


   .. py:attribute:: data
      :value: None



   .. py:method:: get_layers() -> list


   .. py:method:: get_vulnerability_level(layer: str) -> str


   .. py:method:: get_vulnerability_code(layer: str) -> str


.. py:function:: get_data_type(fname: pathlib.Path)

.. py:function:: cleaning_directory(dir: pathlib.Path)

   Cleaning the directory


.. py:function:: empty_folder(folder)

   Empties the content of a directory if it exists.


.. py:class:: Accept_Manager(main_dir: str = 'Data', Study_area=None, scenario=None, Original_gdb: str = 'GT_Resilence_dataRisques202010.gdb', CaPa_Walloon: str = 'Cadastre_Walloon.gpkg', PICC_Walloon: str = 'PICC_vDIFF.gdb', CE_IGN_top10v: str = 'CE_IGN_TOP10V/CE_IGN_TOP10V.shp', EPU_Stations: str = 'AJOUT_PDET_EPU_DG03_STATIONS.shp', Ponderation_csv: str = 'Ponderation.csv', Vuln_csv: str = 'Vulnerability.csv', Intermediate_csv: str = 'Intermediate.csv')

   Structure to store the directories and names of the files.

   In the main directory, the following directories are mandatory/created:
       - INPUT : filled by the user - contains the input data
       - TEMP  : created by the script - contains the temporary data for the study area
       - OUTPUT: created by the script - contains the output data for each scenario of the study area

   The INPUT directory contains the following subdirectories:
       - DATABASE: contains the data for the **entire Walloon region**
           - Cadastre_Walloon.gpkg: the Cadastre Walloon file
           - GT_Resilence_dataRisques202010.gdb: the original gdb file from SPW - GT Resilience
           - PICC-vDIFF.gdb: the PICC Walloon file
           - CE_IGN_TOP10V: the IGN top10v shapefile
       - EPU_STATIONS_NEW:
           - AJOUT_PDET_EPU_DG03_STATIONS.shp: the EPU stations shapefile
       - STUDY_AREA: contains the study area shapefiles - one for each study area - e.g. Bassin_Vesdre.shp
       - CSVs: contains the CSV files
           - Intermediate.csv: contains the matrices data for the acceptability computation
           # - Ponderation.csv: contains the ponderation data for the acceptability computation
           - Vulnerability.csv: contains the mapping between layers and vulnerability levels - a code value is also provided
       - WATER_DEPTH: contains the water depth data for each scenario
           - Study_area1:
               - Scenario1
               - Scenario2
               -...
               - ScenarioN
           - Study_area2:
               - Scenario1
               - Scenario2
               -...
               - ScenarioN
           -...
           - Study_areaN:
               - Scenario1
               - Scenario2
               -...
               - ScenarioN

   The TEMP directory contains the following subdirectories:
       - DATABASES: contains the temporary data each study area
           - Study_area1:
               - database.gpkg: the clipped database
               - CaPa.gpkg: the clipped Cadastre Walloon file
               - PICC.gpkg: the clipped PICC Walloon file
               - CE_IGN_TOP10V.tiff: the IGN top10v raster file
               - Maske_River_extent.tiff: the river extent raster file from IGN
               - VULNERABILITY: the vulnerability data
                   - RASTERS:
                       - Code  : one file for each layer
                       - Vulne : one file for each layer
                   - Scenario1:



   .. py:attribute:: old_dir
      :type:  pathlib.Path


   .. py:attribute:: main_dir
      :type:  pathlib.Path


   .. py:attribute:: _study_area
      :value: None



   .. py:attribute:: _scenario
      :value: None



   .. py:attribute:: _original_gdb
      :value: 'GT_Resilence_dataRisques202010.gdb'



   .. py:attribute:: _capa_walloon
      :value: 'Cadastre_Walloon.gpkg'



   .. py:attribute:: _picc_walloon
      :value: 'PICC_vDIFF.gdb'



   .. py:attribute:: _ce_ign_top10v
      :value: 'CE_IGN_TOP10V/CE_IGN_TOP10V.shp'



   .. py:attribute:: IN_DIR


   .. py:attribute:: IN_CH_VULN


   .. py:attribute:: IN_DATABASE


   .. py:attribute:: IN_STUDY_AREA


   .. py:attribute:: IN_CSV


   .. py:attribute:: IN_WATER_DEPTH


   .. py:attribute:: IN_EPU_STATIONS


   .. py:attribute:: IN_SA_INTERP
      :value: None



   .. py:attribute:: ORIGINAL_GDB


   .. py:attribute:: CAPA_WALLOON


   .. py:attribute:: PICC_WALLOON


   .. py:attribute:: CE_IGN_TOP10V


   .. py:attribute:: EPU_STATIONS


   .. py:attribute:: VULNERABILITY_CSV


   .. py:attribute:: POINTS_CSV


   .. py:attribute:: PONDERATION_CSV


   .. py:attribute:: _CSVs


   .. py:attribute:: _GPKGs


   .. py:attribute:: _GDBs


   .. py:attribute:: _SHPs


   .. py:attribute:: _ALLS


   .. py:attribute:: TMP_DIR


   .. py:attribute:: OUT_DIR


   .. py:attribute:: points2polys
      :value: []



   .. py:attribute:: lines2polys
      :value: []



   .. py:method:: create_paths()

      Create the paths for the directories and files



   .. py:method:: create_paths_scenario()


   .. py:property:: is_valid_inputs
      :type: bool



   .. py:property:: is_valid_study_area
      :type: bool



   .. py:property:: is_valid_vulnerability_csv
      :type: bool



   .. py:property:: is_valid_points_csv
      :type: bool



   .. py:property:: is_valid_ponderation_csv
      :type: bool



   .. py:method:: check_files() -> str

      Check the files in the directories



   .. py:method:: change_studyarea(Study_area: str = None) -> None


   .. py:method:: change_scenario(scenario: str) -> None


   .. py:method:: get_files_in_rm_buildings() -> list[pathlib.Path]


   .. py:method:: get_files_in_CHANGE_VULNE() -> list[pathlib.Path]


   .. py:method:: get_files_in_rasters_vulne() -> list[pathlib.Path]


   .. py:method:: get_layers_in_gdb() -> list[str]


   .. py:method:: get_layer_types_in_gdb() -> list[str]


   .. py:method:: get_layers_in_clipgdb() -> list[str]


   .. py:method:: get_layers_in_wmodif() -> list[str]


   .. py:method:: get_layers_in_codevulne() -> list[str]


   .. py:method:: get_files_in_rasters_code() -> list[pathlib.Path]


   .. py:method:: get_q_files() -> list[pathlib.Path]


   .. py:method:: get_list_scenarios() -> list[str]


   .. py:method:: get_list_studyareas(with_suffix: bool = False) -> list[str]


   .. py:method:: get_sims_files_for_scenario() -> list[pathlib.Path]


   .. py:method:: get_sims_files_for_baseline() -> list[pathlib.Path]


   .. py:method:: get_sim_file_for_return_period(return_period: int) -> pathlib.Path


   .. py:method:: get_types_in_file(file: str) -> list[str]

      Get the types of the geometries in the Shape file



   .. py:method:: is_type_unique(file: str) -> bool

      Check if the file contains only one type of geometry



   .. py:method:: is_polygons(set2test: set) -> bool

      Check if the set contains only polygons



   .. py:method:: is_same_types(file: str) -> tuple[bool, str]

      Check if the file contains only the same type of geometry



   .. py:method:: get_return_periods() -> list[int]

      Get the return periods from the simulations

      :return list[int]: the **sorted list** of return periods



   .. py:method:: get_modifiedrasters()


   .. py:method:: get_ponderations() -> pandas.DataFrame

      Get the ponderation data from available simulations



   .. py:method:: get_filepath_for_return_period(return_period: int) -> pathlib.Path


   .. py:method:: change_dir() -> None


   .. py:method:: restore_dir() -> None


   .. py:method:: check_inputs() -> bool

      Check if the input directories exist.

      Inputs can not be created automatically. The user must provide them.



   .. py:method:: check_temporary() -> bool

      Check if the temporary directories exist.

      If not, create them.



   .. py:method:: check_outputs() -> bool

      Check if the output directories exist.

      If not, create them.



   .. py:method:: check_before_database_creation() -> bool

      Check if the necessary files are present before the database creation



   .. py:method:: check_before_rasterize() -> bool


   .. py:method:: check_before_vulnerability() -> bool


   .. py:method:: check_vuln_code_sa() -> bool


   .. py:method:: check_vuln_code_scenario() -> bool


   .. py:method:: compare_original_clipped_layers() -> str

      Compare the original layers with the clipped ones



   .. py:method:: compare_clipped_raster_layers() -> str

      Compare the clipped layers with the rasterized ones



   .. py:method:: get_operand(file: str) -> Modif_Type

      Get the operand based on the layer name



   .. py:method:: check_origin_shape() -> list[str]


   .. py:method:: tree_name_tif(name)

      Find all .tiff files starting with 'vuln' in the directory and return paths



   .. py:method:: select_name_tif(path_baseline: pathlib.Path, folder_path: pathlib.Path, name) -> list[pathlib.Path]

      Collects and appends all .tiff files starting with 'name' from folder_path into a list.



   .. py:method:: check_nodata(name, path_baseline)

      Check nodata in a path



   .. py:method:: create_vrtIfExists(fn_baseline, fn_scenario, fn_vrt, name)

      Create a vrt file from a path



   .. py:method:: translate_vrt2tif(fn_VRT, fn_vuln_s)

      Translate vrt from OUTPUT > ... > Scenario to tif saved in the same folder, and delete the vrt file



   .. py:method:: copy_tif_files(files: list[pathlib.Path], destination_dir: pathlib.Path) -> None


   .. py:method:: wich_river_trace()

      Searches for existing riverbed traces: if none ending with '_scenarios' are found, it selects the baseline trace.



   .. py:method:: batch_creation_and_interpolation_fotran_holes(checked_paths: list[pathlib.Path], iftest: bool) -> tuple[bool, list[str]]

      Creates a batch file to launch holes.exe from the selected simulations and launches it.

      - Every files in EXTRACTED_LAST_STEP are interpolated for tests (iftest==True).
      - Only the check simulations of the windows are interpolated for the GUI (iftest!=True).

      :param checked_paths: list of paths to the checked simulations
      :param iftest: boolean to indicate if the function is called from the tests or not



   .. py:method:: batch_creation_and_interpolation_python_eikonal(checked_paths: list[pathlib.Path], iftest: bool) -> tuple[bool, list[str]]

      Creates a batch file to launch holes.exe from the selected simulations and launches it.

      - Every files in EXTRACTED_LAST_STEP are interpolated for tests (iftest==True).
      - Only the check simulations of the windows are interpolated for the GUI (iftest!=True).

      :param checked_paths: list of paths to the checked simulations
      :param iftest: boolean to indicate if the function is called from the tests or not



.. py:function:: clip_layer(layer: str, file_path: str, Study_Area: str, output_dir: str)

   Clip the input data based on the selected bassin and saves it
   in separate shape files.

   As shape file doen not support DateTime, the columns with DateTime
   are converted to string.

   :param layer: the layer name in the GDB file
   :param file_path: the path to the GDB file
   :param Study_Area: the path to the study area shapefile
   :param output_dir: the path to the output directory


.. py:function:: data_modification(layer: str, manager: Accept_Manager, picc: geopandas.GeoDataFrame, capa: geopandas.GeoDataFrame)

   Apply the data modifications as described in the LEMA report

   FIXME : Add more doc in this docstring

   :param input_database: the path to the input database
   :param layer: the layer name in the database
   :param output_database: the path to the output database
   :param picc: the PICC Walloon file -- Preloaded
   :param capa: the Cadastre Walloon file -- Preloaded


.. py:function:: compute_vulnerability(manager: Accept_Manager)

   Compute the vulnerability for the Study Area

   This function **will not modify** the data by the removed buildings/scenarios.

   :param dirsnames: the Dirs_Names object from the calling function


.. py:function:: compute_code(manager: Accept_Manager)

   Compute the code for the Study Area

   This function **will not modify** the data by the removed buildings/scenarios.

   :param dirsnames: the Dirs_Names object from the calling function


.. py:function:: compute_vulnerability4scenario(manager: Accept_Manager)

   Compute the vulnerability for the scenario

   This function **will modify** the data by the removed buildings/scenarios.

   FIXME: It could be interseting to permit the user to provide tiff files for the removed buildings and other scenarios.

   :param dirsnames: the Dirs_Names object from the calling function


.. py:function:: match_vulnerability2sim(inRas: pathlib.Path, outRas: pathlib.Path, MODREC: pathlib.Path)

   Clip the raster to the MODREC/simulation extent

   :param inRas: the input raster file
   :param outRas: the output raster file
   :param MODREC: the MODREC/simulation extent file



.. py:function:: update_accept(accept, model_h, ij, bounds, loc_accept)

.. py:function:: compute_acceptability(manager: Accept_Manager, model_h: numpy.ndarray, vulnerability: numpy.ndarray, interval: int, geo_projection: tuple, save_to_file: bool = True) -> numpy.ndarray

   Compute the local acceptability based on :
       - the vulnerability
       - the water depth
       - the matrices

   :param manager: the Accept_Manager object from the calling function
   :param model_h: the water depth matrix
   :param vulnerability: the vulnerability matrix
   :param interval: the return period
   :param geo_projection: the geotransform and the projection - tuple extracted from another raster file



.. py:function:: shp_to_raster(vector_fn: str | pathlib.Path, raster_fn: str | pathlib.Path, pixel_size: float = 1.0, manager: Accept_Manager = None)

   Convert a vector layer to a raster tiff file.

   The raster will contain only 2 values : 0 and 1

   - 1 : the inside of the vector layer
   - 0 : the rest == NoData/NullValue

   :param vector_fn: the path to the vector file
   :param raster_fn: the path to the raster file
   :param pixel_size: the pixel size of the raster


.. py:function:: vector_to_raster(layer: str, manager: Accept_Manager, attribute: str, pixel_size: float, convert_to_sparse: bool = True)

   Convert a vector layer to a raster tiff file

   FIXME: Test de vulerability value and return immedialty if it is 1 if attribute == "Vulne"

   :param layer: the layer name in the GDB file
   :param vector_input: the path to the vector file
   :param extent: the path to the extent file
   :param attribute: the attribute to rasterize
   :param pixel_size: the pixel size of the raster



.. py:function:: convert_to_csr(i_indices, j_indices, num_rows)