wolfhece 2.1.25__py3-none-any.whl → 2.1.28__py3-none-any.whl

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -1,5 +1,7 @@
1
- from .Parallels import parallel_gpd_clip, parallel_v2r
2
- from .func import data_modification, Comp_Vulnerability, Comp_Vulnerability_Scen, match_vuln_modrec, VulMod, shp_to_raster, Accept_Manager, cleaning_directory
1
+ from .Parallels import parallel_gpd_clip, parallel_v2r, parallel_datamod
2
+ from .func import data_modification, compute_vulnerability, compute_vulnerability4scenario
3
+ from .func import match_vulnerability2sim, compute_acceptability, shp_to_raster
4
+ from .func import Accept_Manager, cleaning_directory, EXTENT, Vulnerability_csv, compute_code
3
5
 
4
6
  import pandas as pd
5
7
  import os
@@ -11,26 +13,30 @@ import geopandas as gpd
11
13
  from pathlib import Path
12
14
  import logging
13
15
  from tqdm import tqdm
16
+ from enum import Enum
17
+ from pyogrio import read_dataframe
14
18
 
15
-
16
-
17
-
18
- class Vulnerability_csv():
19
-
20
- def __init__(self, file:Path) -> None:
21
- self.file = file
22
- self.data = pd.read_csv(file, sep=",", encoding='latin-1')
23
-
24
- def get_layers(self) -> list:
25
- return [a[1] for a in self.data["Path"].str.split('/')]
26
-
27
- def get_vulnerability_level(self, layer:str) -> str:
28
- idx = self.get_layers().index(layer)
29
- return self.data.iloc[idx]["Vulne"]
30
-
31
- def get_vulnerability_code(self, layer:str) -> str:
32
- idx = self.get_layers().index(layer)
33
- return self.data.iloc[idx]["Code"]
19
+ class steps_base_data_creation(Enum):
20
+ """
21
+ Enum for the steps in the base data creation
22
+ """
23
+ CLIP_GDB = 1
24
+ CLIP_CADASTER = 2
25
+ CLIP_PICC = 3
26
+ POINTS2POLYS = 4
27
+ RASTERIZE_IGN = 5
28
+ PREPROCESS_VULNCODE = 6
29
+ DATABASE_TO_RASTER = 7
30
+
31
+ class steps_vulnerability(Enum):
32
+ """
33
+ Enum for the steps in the vulnerability computation
34
+ """
35
+ CREATE_RASTERS = 1
36
+ CREATE_RASTERS_VULN = 10
37
+ CREATE_RASTERS_CODE = 11
38
+ APPLY_MODIFS = 2
39
+ MATCH_SIMUL = 3
34
40
 
35
41
  def Base_data_creation(main_dir:str = 'Data',
36
42
  Original_gdb:str = 'GT_Resilence_dataRisques202010.gdb',
@@ -39,7 +45,9 @@ def Base_data_creation(main_dir:str = 'Data',
39
45
  PICC_Walloon:str = 'PICC_vDIFF.gdb',
40
46
  CE_IGN_top10v:str = 'CE_IGN_TOP10V/CE_IGN_TOP10V.shp',
41
47
  resolution:float = 1.,
42
- number_procs:int = 8):
48
+ number_procs:int = 8,
49
+ steps:list[int] | list[steps_base_data_creation] = [1,2,3,4,5,6,7],
50
+ Vulnerability_csv:str = 'Vulnerability.csv'):
43
51
  """
44
52
  Create the databse.
45
53
 
@@ -61,109 +69,158 @@ def Base_data_creation(main_dir:str = 'Data',
61
69
  :param number_procs: The number of processors to use for parallel processing
62
70
 
63
71
  """
64
- NUMBER_PROCS = number_procs
72
+ LAYER_CABU = "CaBu"
73
+ LAYER_CAPA = "CaPa"
74
+ LAYER_BATIEMPRISE = "CONSTR_BATIEMPRISE"
65
75
 
66
- dirsnames = Accept_Manager(main_dir,
76
+ manager = Accept_Manager(main_dir,
67
77
  Study_area,
68
78
  Original_gdb=Original_gdb,
69
79
  CaPa_Walloon=CaPa_Walloon,
70
80
  PICC_Walloon=PICC_Walloon,
71
- CE_IGN_top10v=CE_IGN_top10v)
81
+ CE_IGN_top10v=CE_IGN_top10v,
82
+ Vulnerability_csv=Vulnerability_csv)
72
83
 
73
- if not dirsnames.check_before_database_creation():
84
+ if not manager.check_before_database_creation():
74
85
  logging.error("The necessary files are missing - Verify logs for more information")
75
86
  return
76
87
 
77
- dirsnames.change_dir()
88
+ manager.change_dir()
89
+ done = []
90
+
91
+ if 1 in steps or 6 in steps or steps_base_data_creation.PREPROCESS_VULNCODE in steps or steps_base_data_creation.CLIP_GDB in steps:
92
+ # Load the vulnerability CSV to get the layers
93
+ vulnerability_csv = Vulnerability_csv(manager.VULNERABILITY_CSV)
78
94
 
79
- # Clean the directory to avoid any conflict
80
- # GPKG driver does not overwrite the existing file but adds new layers
81
- cleaning_directory(dirsnames.TMP_STUDYAREA)
95
+ if 1 in steps or steps_base_data_creation.CLIP_GDB in steps:
96
+ # Clean the directory to avoid any conflict
97
+ # GPKG driver does not overwrite the existing file but adds new layers
98
+ cleaning_directory(manager.TMP_CLIPGDB)
82
99
 
83
- # ********************************************************************************************************************
84
- # Step 1, Clip Original GDB
100
+ # ********************************************************************************************************************
101
+ # Step 1, Clip Original GDB
85
102
 
86
- # Load the vulnerability CSV to get the layers
87
- vulnerability_csv = Vulnerability_csv(dirsnames.VULNERABILITY_CSV)
88
- # Clip the GDB file and store it in dirsnames.SA_DATABASE
89
- parallel_gpd_clip(vulnerability_csv.get_layers(), dirsnames.ORIGINAL_GDB, dirsnames.SA, dirsnames.SA_DATABASE, NUMBER_PROCS)
103
+ # Clip the GDB file and store it in dirsnames.SA_DATABASE
104
+ parallel_gpd_clip(vulnerability_csv.get_layers(), manager.ORIGINAL_GDB, manager.SA, manager.TMP_CLIPGDB, number_procs)
105
+
106
+ done.append(steps_base_data_creation.CLIP_GDB)
90
107
 
91
- # ********************************************************************************************************************
92
- # Step 2, Clip Cadaster data
108
+ if 2 in steps or steps_base_data_creation.CLIP_CADASTER in steps:
109
+ # ********************************************************************************************************************
110
+ # Step 2, Clip Cadaster data
111
+ cleaning_directory(manager.TMP_CADASTER)
93
112
 
94
- # Only 2 layers are present in the Cadastre Walloon file
95
- LAYER_CABU = "CaBu"
96
- LAYER_CAPA = "CaPa"
97
- # Clip the Cadastre Walloon file and store it in dirsnames.SA_CAPA
98
- parallel_gpd_clip([LAYER_CABU, LAYER_CAPA], dirsnames.CAPA_WALLOON, dirsnames.SA, dirsnames.SA_CAPA, min(2, NUMBER_PROCS))
113
+ # Only 2 layers are present in the Cadastre Walloon file
114
+ # Clip the Cadastre Walloon file and store it in dirsnames.SA_CAPA
115
+ parallel_gpd_clip([LAYER_CABU, LAYER_CAPA], manager.CAPA_WALLOON, manager.SA, manager.TMP_CADASTER, min(2, number_procs))
99
116
 
100
- # ********************************************************************************************************************
101
- # Step 3, Clip PICC data
117
+ done.append(steps_base_data_creation.CLIP_CADASTER)
102
118
 
103
- # ONly 1 layer is needed from the PICC Walloon file
104
- LAYER_BATIEMPRISE = "CONSTR_BATIEMPRISE"
105
- # Clip the PICC Walloon file and store it in dirsnames.SA_PICC
106
- parallel_gpd_clip([LAYER_BATIEMPRISE], dirsnames.PICC_WALLOON, dirsnames.SA, dirsnames.SA_PICC, min(1, NUMBER_PROCS))
119
+ if 3 in steps or steps_base_data_creation.CLIP_PICC in steps:
120
+ # ********************************************************************************************************************
121
+ # Step 3, Clip PICC data
122
+ cleaning_directory(manager.TMP_PICC)
123
+
124
+ # ONly 1 layer is needed from the PICC Walloon file
125
+ # Clip the PICC Walloon file and store it in dirsnames.SA_PICC
126
+ parallel_gpd_clip([LAYER_BATIEMPRISE], manager.PICC_WALLOON, manager.SA, manager.TMP_PICC, min(1, number_procs))
127
+
128
+ done.append(steps_base_data_creation.CLIP_PICC)
107
129
 
108
- # ********************************************************************************************************************
109
- # Step 4, create database based on changes in report
130
+ if 4 in steps or steps_base_data_creation.POINTS2POLYS in steps:
131
+ # ********************************************************************************************************************
132
+ # Step 4, create database based on changes in report
110
133
 
111
- layers = fiona.listlayers(dirsnames.SA_DATABASE)
112
- # PreLoad Picc and CaPa from clipped files
113
- Picc:gpd.GeoDataFrame = gpd.read_file(dirsnames.SA_PICC, layer = LAYER_BATIEMPRISE)
114
- CaPa:gpd.GeoDataFrame = gpd.read_file(dirsnames.SA_CAPA, layer = LAYER_CAPA)
134
+ cleaning_directory(manager.TMP_WMODIF)
115
135
 
116
- assert Picc.crs == CaPa.crs, "The crs of the two shapefiles are different"
136
+ # PreLoad Picc and CaPa from clipped files
137
+ Picc:gpd.GeoDataFrame = read_dataframe(str(manager.TMP_PICC / (LAYER_BATIEMPRISE+EXTENT)), layer=LAYER_BATIEMPRISE)
138
+ CaPa:gpd.GeoDataFrame = read_dataframe(str(manager.TMP_CADASTER / (LAYER_CAPA+EXTENT)), layer=LAYER_CAPA)
117
139
 
118
- for curlayer in tqdm(layers, desc="Vulnerability : Processing layers"):
119
- data_modification(dirsnames.SA_DATABASE, curlayer, dirsnames.SA_FINAL, Picc, CaPa)
140
+ assert Picc.crs == CaPa.crs, "The crs of the two shapefiles are different"
120
141
 
121
- # ********************************************************************************************************************
122
- # Step 5 : Rasaterize the IGN data "Course d'eau" to get the riverbed mask
123
- shp_to_raster(dirsnames.CE_IGN_TOP10V, dirsnames.SA_MASKED_RIVER, resolution)
124
-
125
- # ********************************************************************************************************************
126
- # Step 6 : Pre-processing for Vulnerability
127
- # Save the database with vulnerability levels and codes
128
- # This database will be rasterized in 'Database_to_raster'
142
+ parallel_datamod(manager=manager, picc=Picc, capa=CaPa, number_procs=number_procs)
129
143
 
130
- layers_sa = fiona.listlayers(dirsnames.SA_FINAL)
131
- layers_csv = vulnerability_csv.get_layers()
132
-
133
- # Search difference between the two lists of layers
134
- list_shp = list(set(layers_csv).difference(layers_sa))
144
+ done.append(steps_base_data_creation.POINTS2POLYS)
135
145
 
136
- logging.info("Excluded layers due to no features in shapefiles:")
137
- logging.info(list_shp)
146
+ if 5 in steps or steps_base_data_creation.RASTERIZE_IGN in steps:
147
+ # ********************************************************************************************************************
148
+ # Step 5 : Rasaterize the IGN data "Course d'eau" to get the riverbed mask
149
+ shp_to_raster(manager.CE_IGN_TOP10V, manager.SA_MASKED_RIVER, resolution)
138
150
 
139
- logging.info("STEP1: Saving the database for Vulnerability with attributes Vulne and Code")
140
-
141
- for curlayer in layers_sa:
142
- logging.info(curlayer)
151
+ done.append(steps_base_data_creation.RASTERIZE_IGN)
152
+
153
+ if 6 in steps or steps_base_data_creation.PREPROCESS_VULNCODE in steps:
154
+ # ********************************************************************************************************************
155
+ # Step 6 : Pre-processing for Vulnerability
156
+ # Save the database with vulnerability levels and codes
157
+ # This database will be rasterized in 'Database_to_raster'
158
+
159
+ layers_sa = manager.get_layers_in_wmodif()
160
+ layers_csv = vulnerability_csv.get_layers()
161
+
162
+ # Search difference between the two lists of layers
163
+ list_shp = list(set(layers_csv).difference(layers_sa))
164
+
165
+ logging.info("Excluded layers due to no features in shapefiles:")
166
+ logging.info(list_shp)
167
+
168
+ not_in_csv = [curlayer for curlayer in layers_sa if curlayer not in layers_csv]
169
+ if len(not_in_csv) > 0:
170
+ logging.error("Not treated layers due to no vulnerability level or code:")
171
+ logging.error(not_in_csv)
143
172
 
144
- shp:gpd.GeoDataFrame = gpd.read_file(dirsnames.SA_FINAL, layer=curlayer)
173
+ logging.info("STEP1: Saving the database for Vulnerability with attributes Vulne and Code")
145
174
 
146
- x, y = shp.shape
147
- if x > 0:
148
- shp["Path"] = curlayer
149
- shp["Vulne"] = vulnerability_csv.get_vulnerability_level(curlayer)
150
- shp["Code"] = vulnerability_csv.get_vulnerability_code(curlayer)
151
- shp = shp[["geometry", "Path", "Vulne","Code"]]
152
- shp.to_file(dirsnames.SA_FINAL_V, layer=curlayer)
153
-
154
- # Rasterize the database
155
- Database_to_raster(main_dir, Study_area, resolution)
175
+ for curlayer in layers_sa:
176
+ logging.info(curlayer)
177
+
178
+ in_file = str(manager.TMP_WMODIF / (curlayer+EXTENT))
179
+ out_file = str(manager.TMP_CODEVULNE / (curlayer+EXTENT))
180
+
181
+ shp:gpd.GeoDataFrame = gpd.read_file(in_file)
182
+
183
+ nb_lines, _ = shp.shape
184
+ if nb_lines > 0:
185
+ shp["Path"] = curlayer
186
+ shp["Vulne"] = vulnerability_csv.get_vulnerability_level(curlayer)
187
+ shp["Code"] = vulnerability_csv.get_vulnerability_code(curlayer)
188
+ shp = shp[["geometry", "Path", "Vulne","Code"]]
189
+ shp.to_file(out_file)
190
+ else:
191
+ # Normally, Phase 1 should have removed the empty shapefiles
192
+ # But, we never know... ;-)
193
+ logging.warning(f"Empty shapefile {curlayer} in {in_file}")
194
+
195
+ done.append(steps_base_data_creation.PREPROCESS_VULNCODE)
196
+
197
+ if 7 in steps or steps_base_data_creation.DATABASE_TO_RASTER in steps:
198
+ # Rasterize the database
199
+ cleaning_directory(manager.TMP_RASTERS)
200
+ cleaning_directory(manager.TMP_RASTERS_CODE)
201
+ cleaning_directory(manager.TMP_RASTERS_VULNE)
202
+
203
+ Database_to_raster(main_dir,
204
+ Study_area,
205
+ resolution,
206
+ number_procs=number_procs,
207
+ Vulnerability_csv=Vulnerability_csv)
156
208
 
157
- dirsnames.restore_dir()
209
+ done.append(steps_base_data_creation.DATABASE_TO_RASTER)
210
+
211
+ manager.restore_dir()
212
+
213
+ return done
158
214
 
159
215
  def Database_to_raster(main_dir:str = 'Data',
160
216
  Study_area:str = 'Bassin_Vesdre.shp',
161
217
  resolution:float = 1.,
162
- number_procs:int = 16):
218
+ number_procs:int = 16,
219
+ Vulnerability_csv:str = 'Vulnerability.csv'):
163
220
  """
164
221
  Convert the vector database to raster database based on their vulnerability values
165
222
 
166
- Ecah leyer is converted to a raster file with the vulnerability values
223
+ Each leyer is converted to a raster file with the vulnerability values
167
224
  and the code values.
168
225
 
169
226
  They are stored in the TEMP/DATABASES/*StudyArea*/VULNERABILITY/RASTERS in:
@@ -175,33 +232,34 @@ def Database_to_raster(main_dir:str = 'Data',
175
232
  :param resolution: The resolution of the output raster files - default is 1 meter
176
233
  :param number_procs: The number of processors to use for parallel processing
177
234
 
178
- The parellel processing is safe as each layer is processed independently.
235
+ The parallel processing is safe as each layer is processed independently.
179
236
  """
180
237
 
181
- dirsnames = Accept_Manager(main_dir, Study_area)
238
+ manager = Accept_Manager(main_dir, Study_area, Vulnerability_csv=Vulnerability_csv)
182
239
 
183
240
  resolution = float(resolution)
184
241
 
185
- if not dirsnames.check_before_rasterize():
242
+ if not manager.check_before_rasterize():
186
243
  logging.error("The necessary files are missing - Verify logs for more information")
187
244
  return
188
245
 
189
- dirsnames.change_dir()
246
+ manager.change_dir()
190
247
 
191
248
  logging.info("Convert vectors to raster based on their vulnerability values")
192
- layers = fiona.listlayers(dirsnames.SA_FINAL_V)
193
249
 
194
250
  attributes = ["Vulne", "Code"]
195
251
  for cur_attrib in attributes:
196
- parallel_v2r(layers, dirsnames.SA_FINAL_V, dirsnames.SA, cur_attrib, resolution, number_procs)
252
+ parallel_v2r(manager, cur_attrib, resolution, number_procs, convert_to_sparse=True)
197
253
 
198
- dirsnames.restore_dir()
254
+ manager.restore_dir()
199
255
 
200
256
  def Vulnerability(main_dir:str = 'Data',
201
257
  scenario:str = 'Scenario1',
202
258
  Study_area:str = 'Bassin_Vesdre.shp',
203
259
  resolution:float = 1.,
204
- steps:list[int] = [1,2,3]):
260
+ steps:list[int] | list[steps_vulnerability] = [1,10,11,2,3],
261
+ Vulnerability_csv:str = 'Vulnerability.csv',
262
+ Intermediate_csv:str = 'Intermediate.csv'):
205
263
  """
206
264
  Compute the vulnerability for the study area and the scenario, if needed.
207
265
 
@@ -228,81 +286,105 @@ def Vulnerability(main_dir:str = 'Data',
228
286
 
229
287
  """
230
288
 
231
- dirsnames = Accept_Manager(main_dir, Study_area, scenario=scenario)
289
+ manager = Accept_Manager(main_dir,
290
+ Study_area,
291
+ scenario=scenario,
292
+ Vulnerability_csv=Vulnerability_csv,
293
+ Intermediate_csv=Intermediate_csv)
232
294
 
233
- if not dirsnames.check_before_vulnerability():
295
+ if not manager.check_before_vulnerability():
234
296
  logging.error("The necessary files are missing - Verify logs for more information")
235
297
  return
236
298
 
237
299
  logging.info("Starting VULNERABILITY computations at {} m resolution".format(resolution))
238
300
 
239
- dirsnames.change_dir()
301
+ manager.change_dir()
302
+ done = []
240
303
 
241
- if 1 in steps:
304
+ if 1 in steps or steps_vulnerability.CREATE_RASTERS in steps:
242
305
  # Step 1 : Compute the vulnerability rasters for the study area
243
306
  # The data **will not** be impacted by the scenario modifications
244
307
 
245
308
  logging.info("Generate Vulnerability rasters {}m".format(resolution))
246
309
 
247
- cleaning_directory(dirsnames.TMP_SCEN_DIR)
310
+ cleaning_directory(manager.TMP_SCEN_DIR)
248
311
 
249
- Comp_Vulnerability(dirsnames)
312
+ if 10 in steps or steps_vulnerability.CREATE_RASTERS_VULN in steps:
313
+ compute_vulnerability(manager)
314
+ done.append(steps_vulnerability.CREATE_RASTERS_VULN)
315
+
316
+ if 11 in steps or steps_vulnerability.CREATE_RASTERS_CODE in steps:
317
+ compute_code(manager)
318
+ done.append(steps_vulnerability.CREATE_RASTERS_CODE)
250
319
 
251
- if 2 in steps:
320
+ done.append(steps_vulnerability.CREATE_RASTERS)
321
+
322
+ if 2 in steps or steps_vulnerability.APPLY_MODIFS in steps:
252
323
  # Step 2 : Compute the vulnerability rasters for the scenario
253
324
  # The data **will be** impacted by the scenario modifications
254
325
 
255
- if not dirsnames.check_vuln_code_sa():
326
+ if not manager.check_vuln_code_sa():
256
327
  logging.error("The vulnerability and code files for the study area are missing")
257
328
  logging.warning("Force the computation even if not prescribed in the steps")
258
329
 
259
330
  Vulnerability(main_dir, scenario, Study_area, resolution, [1])
260
331
 
261
- bu:list[Path] = dirsnames.get_files_in_rm_buildings()
332
+ bu:list[Path] = manager.get_files_in_rm_buildings()
262
333
 
263
334
  if len(bu)>0:
264
335
  for curfile in bu:
265
- tiff_file = dirsnames.TMP_RM_BUILD_DIR / (curfile.stem + ".tiff")
336
+ tiff_file = manager.TMP_RM_BUILD_DIR / (curfile.stem + ".tiff")
266
337
  shp_to_raster(curfile, tiff_file)
267
338
 
268
- Comp_Vulnerability_Scen(dirsnames)
339
+ compute_vulnerability4scenario(manager)
269
340
  else:
270
- logging.warning(f"No buildings were removed in water depth analysis OR No shapefiles in {dirsnames.IN_RM_BUILD_DIR}")
341
+ logging.warning(f"No buildings were removed in water depth analysis OR No shapefiles in {manager.IN_RM_BUILD_DIR}")
342
+
343
+ done.append(steps_vulnerability.APPLY_MODIFS)
271
344
 
272
- if 3 in steps:
345
+ if 3 in steps or steps_vulnerability.MATCH_SIMUL in steps:
273
346
  # Step 3 : Clip the vulnerability/code rasters to the **simulation area**
274
347
 
275
348
  logging.info("Save Vulnerability files for the area of interest")
276
349
 
277
- return_periods = dirsnames.get_return_periods()
278
- TMAX = dirsnames.get_filepath_for_return_period(return_periods[-1])
350
+ return_periods = manager.get_return_periods()
351
+ TMAX = manager.get_filepath_for_return_period(return_periods[-1])
279
352
 
280
353
  if TMAX is None:
281
354
  logging.error("The file for the maximum return period is missing")
282
355
  return
283
356
 
284
- match_vuln_modrec(dirsnames.SA_MASKED_RIVER,dirsnames.OUT_MASKED_RIVER, TMAX)
285
- match_vuln_modrec(dirsnames.SA_VULN, dirsnames.OUT_VULN, TMAX)
286
- match_vuln_modrec(dirsnames.SA_CODE, dirsnames.OUT_CODE, TMAX)
357
+ match_vulnerability2sim(manager.SA_MASKED_RIVER,manager.OUT_MASKED_RIVER, TMAX)
358
+ match_vulnerability2sim(manager.SA_VULN, manager.OUT_VULN, TMAX)
359
+ match_vulnerability2sim(manager.SA_CODE, manager.OUT_CODE, TMAX)
287
360
 
288
- dirsnames.restore_dir()
361
+ done.append(steps_vulnerability.MATCH_SIMUL)
362
+
363
+ manager.restore_dir()
364
+
365
+ return done
289
366
 
290
367
  def Acceptability(main_dir:str = 'Vesdre',
291
368
  scenario:str = 'Scenario1',
292
- Study_area:str = 'Bassin_Vesdre.shp'):
369
+ Study_area:str = 'Bassin_Vesdre.shp',
370
+ coeff_auto:bool = True,
371
+ Ponderation_csv:str = 'Ponderation.csv'):
293
372
  """ Compute acceptability for the scenario """
294
373
 
295
- dirsnames = Accept_Manager(main_dir, Study_area, scenario=scenario)
374
+ manager = Accept_Manager(main_dir,
375
+ Study_area,
376
+ scenario=scenario,
377
+ Ponderation_csv=Ponderation_csv)
296
378
 
297
- dirsnames.change_dir()
379
+ manager.change_dir()
298
380
 
299
381
  # Load the vulnerability raster **for the scenario**
300
- vulne = gdal.Open(str(dirsnames.OUT_VULN))
382
+ vulne = gdal.Open(str(manager.OUT_VULN))
301
383
  # Convert to numpy array
302
384
  vulne = vulne.GetRasterBand(1).ReadAsArray()
303
385
 
304
386
  # Load the river mask
305
- riv = gdal.Open(str(dirsnames.OUT_MASKED_RIVER))
387
+ riv = gdal.Open(str(manager.OUT_MASKED_RIVER))
306
388
 
307
389
  # Get the geotransform and projection for the output tiff
308
390
  geotrans = riv.GetGeoTransform()
@@ -312,7 +394,7 @@ def Acceptability(main_dir:str = 'Vesdre',
312
394
  riv = riv.GetRasterBand(1).ReadAsArray()
313
395
 
314
396
  # Get the return periods available
315
- return_periods = dirsnames.get_return_periods()
397
+ return_periods = manager.get_return_periods()
316
398
 
317
399
  # Prepare the river bed filter
318
400
  # Useful as we iterate over the return periods
@@ -323,24 +405,25 @@ def Acceptability(main_dir:str = 'Vesdre',
323
405
  for curT in tqdm(return_periods):
324
406
 
325
407
  # Load the **FILLED** modelled water depth for the return period
326
- model_h = gdal.Open(str(dirsnames.get_sim_file_for_return_period(curT)))
408
+ model_h = gdal.Open(str(manager.get_sim_file_for_return_period(curT)))
327
409
  # Convert to numpy array
328
410
  model_h = model_h.GetRasterBand(1).ReadAsArray()
329
411
 
330
- # Set nan if the water depth is 0
412
+ # Set nan if the water depth is 0.
413
+ # 'nan' is a good choice as it will not be considered in the computation.
331
414
  model_h[model_h == 0] = np.nan
332
415
  # Set nan in the river bed
333
416
  model_h[ij_riv] = np.nan
334
417
 
335
418
  logging.info("Return period {}".format(curT))
336
419
  # Compute the local acceptability for the return period
337
- VulMod(dirsnames, model_h, vulne, curT, (geotrans, proj))
420
+ compute_acceptability(manager, model_h, vulne, curT, (geotrans, proj))
338
421
 
339
422
  # At this point, the local acceptability for each return period is computed
340
423
  # and stored in tiff files in the TEMP/SutyArea/scenario/Q_FILES directory
341
424
 
342
425
  # Get the list of Q files
343
- qs = dirsnames.get_q_files()
426
+ qs = manager.get_q_files()
344
427
  # Initialize the dictionary to store the acceptability values
345
428
  q_dict = {}
346
429
 
@@ -349,7 +432,7 @@ def Acceptability(main_dir:str = 'Vesdre',
349
432
  logging.info("vm"+str(curT))
350
433
 
351
434
  # We set the filename from the return period, not the "qs" list
352
- q_filename = dirsnames.TMP_QFILES / "Q{}.tif".format(curT)
435
+ q_filename = manager.TMP_QFILES / "Q{}.tif".format(curT)
353
436
 
354
437
  # Check if the file exists
355
438
  assert q_filename.exists(), "The file {} does not exist".format(q_filename)
@@ -391,10 +474,19 @@ def Acceptability(main_dir:str = 'Vesdre',
391
474
  # pond.set_index("Interval", inplace=True)
392
475
 
393
476
  # Get ponderations for the return periods
394
- pond = dirsnames.get_ponderations()
477
+ if coeff_auto:
478
+ pond = manager.get_ponderations()
479
+ assert pond["Ponderation"].sum() > 0.999999 and pond["Ponderation"].sum()<1.0000001, "The sum of the ponderations is not equal to 1"
480
+
481
+ elif manager.is_valid_ponderation_csv:
482
+ pond = pd.read_csv(manager.PONDERATION_CSV)
483
+ pond.set_index("Interval", inplace=True)
484
+ else:
485
+ logging.error("The ponderation file is missing")
486
+ logging.info("Please provide the ponderation file or set 'coeff_auto' to True")
487
+ return
395
488
 
396
489
  assert len(pond) == len(return_periods), "The number of ponderations is not equal to the number of return periods"
397
- assert pond["Ponderation"].sum() > 0.999999 and pond["Ponderation"].sum()<1.0000001, "The sum of the ponderations is not equal to 1"
398
490
 
399
491
  # Initialize the combined acceptability matrix -- Ponderate mean of the local acceptability
400
492
  comb = np.zeros(q_dict["vm{}".format(return_periods[-1])].shape)
@@ -406,7 +498,7 @@ def Acceptability(main_dir:str = 'Vesdre',
406
498
 
407
499
  # Set up output GeoTIFF
408
500
  driver = gdal.GetDriverByName('GTiff')
409
- dataset = driver.Create(str(dirsnames.OUT_ACCEPT), x_pixels, y_pixels, 1, gdal.GDT_Float32, options=["COMPRESS=LZW"])
501
+ dataset = driver.Create(str(manager.OUT_ACCEPT), x_pixels, y_pixels, 1, gdal.GDT_Float32, options=["COMPRESS=LZW"])
410
502
  dataset.GetRasterBand(1).WriteArray(comb.astype(np.float32))
411
503
  dataset.SetGeoTransform(geotrans)
412
504
  dataset.SetProjection(proj)
@@ -414,7 +506,9 @@ def Acceptability(main_dir:str = 'Vesdre',
414
506
  del(dataset)
415
507
 
416
508
  # Resample to 100m
417
- Agg = gdal.Warp(str(dirsnames.OUT_ACCEPT_100M), str(dirsnames.OUT_ACCEPT), xRes=100, yRes=100, resampleAlg='Average')
509
+ Agg = gdal.Warp(str(manager.OUT_ACCEPT_100M), str(manager.OUT_ACCEPT), xRes=100, yRes=100, resampleAlg='Average')
418
510
  del(Agg)
419
511
 
420
- dirsnames.restore_dir()
512
+ manager.restore_dir()
513
+
514
+ return 0