wolfhece 2.1.99__py3-none-any.whl → 2.1.101__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.
Files changed (35) hide show
  1. wolfhece/PyDraw.py +220 -29
  2. wolfhece/PyGui.py +1039 -53
  3. wolfhece/PyVertexvectors.py +2 -2
  4. wolfhece/Results2DGPU.py +37 -13
  5. wolfhece/acceptability/Parallels.py +2 -2
  6. wolfhece/acceptability/_add_path.py +23 -0
  7. wolfhece/acceptability/acceptability.py +594 -563
  8. wolfhece/acceptability/acceptability_gui.py +564 -331
  9. wolfhece/acceptability/cli.py +307 -120
  10. wolfhece/acceptability/func.py +1754 -1597
  11. wolfhece/apps/version.py +1 -1
  12. wolfhece/bernoulli/losses.py +76 -23
  13. wolfhece/bernoulli/losses_jax.py +143 -0
  14. wolfhece/bernoulli/pipe.py +7 -2
  15. wolfhece/gpuview.py +4 -1
  16. wolfhece/libs/__init__.py +11 -10
  17. wolfhece/libs/wolfogl.cp310-win_amd64.pyd +0 -0
  18. wolfhece/math_parser/__init__.py +4 -4
  19. wolfhece/math_parser/calculator.py +51 -9
  20. wolfhece/mesh2d/bc_manager.py +25 -2
  21. wolfhece/mesh2d/gpu_2d.py +644 -0
  22. wolfhece/mesh2d/simple_2d.py +2817 -0
  23. wolfhece/mesh2d/wolf2dprev.py +5 -2
  24. wolfhece/pidcontroller.py +131 -0
  25. wolfhece/pywalous.py +7 -7
  26. wolfhece/scenario/config_manager.py +98 -21
  27. wolfhece/wolf_array.py +391 -176
  28. wolfhece/wolf_vrt.py +108 -7
  29. wolfhece/wolfresults_2D.py +113 -6
  30. wolfhece/xyz_file.py +91 -51
  31. {wolfhece-2.1.99.dist-info → wolfhece-2.1.101.dist-info}/METADATA +3 -1
  32. {wolfhece-2.1.99.dist-info → wolfhece-2.1.101.dist-info}/RECORD +35 -30
  33. {wolfhece-2.1.99.dist-info → wolfhece-2.1.101.dist-info}/WHEEL +1 -1
  34. {wolfhece-2.1.99.dist-info → wolfhece-2.1.101.dist-info}/entry_points.txt +0 -0
  35. {wolfhece-2.1.99.dist-info → wolfhece-2.1.101.dist-info}/top_level.txt +0 -0
@@ -1,331 +1,564 @@
1
- """
2
- Author: University of Liege, HECE
3
- Date: 2024
4
-
5
- Copyright (c) 2024 University of Liege. All rights reserved.
6
-
7
- This script and its content are protected by copyright law. Unauthorized
8
- copying or distribution of this file, via any medium, is strictly prohibited.
9
- """
10
-
11
- from .acceptability import Base_data_creation, Database_to_raster, Vulnerability, Acceptability
12
- from .acceptability import steps_base_data_creation, steps_vulnerability, steps_acceptability
13
- from .func import Accept_Manager
14
-
15
- import wx
16
- import logging
17
- import matplotlib
18
- from matplotlib.figure import Figure
19
- from matplotlib.backends.backend_wxagg import FigureCanvasWxAgg as FigureCanvas
20
- from matplotlib.backends.backend_wxagg import NavigationToolbar2WxAgg as NavigationToolbar2Wx
21
- import os
22
- from gettext import gettext as _
23
-
24
- class AcceptabilityGui(wx.Frame):
25
- """ The main frame for the vulnerability/acceptability computation """
26
-
27
- def __init__(self, parent=None, width=1024, height=500):
28
-
29
- super(wx.Frame, self).__init__(parent, title='Acceptability', size=(width, height))
30
-
31
- self._manager = None
32
- self._mapviewer = None
33
-
34
- self.InitUI()
35
-
36
- @property
37
- def mapviewer(self):
38
- return self._mapviewer
39
-
40
- @mapviewer.setter
41
- def mapviewer(self, value):
42
- from ..PyDraw import WolfMapViewer
43
-
44
- if not isinstance(value, WolfMapViewer):
45
- raise TypeError("The mapviewer must be a WolfMapViewer")
46
-
47
- self._mapviewer = value
48
-
49
- def InitUI(self):
50
-
51
- sizer_hor_main = wx.BoxSizer(wx.HORIZONTAL)
52
-
53
- sizer_vert1 = wx.BoxSizer(wx.VERTICAL)
54
-
55
- sizer_hor_threads = wx.BoxSizer(wx.HORIZONTAL)
56
- sizer_hor1 = wx.BoxSizer(wx.HORIZONTAL)
57
- sizer_hor1_1 = wx.BoxSizer(wx.HORIZONTAL)
58
- sizer_hor2 = wx.BoxSizer(wx.HORIZONTAL)
59
- sizer_hor3 = wx.BoxSizer(wx.HORIZONTAL)
60
- sizer_hor4 = wx.BoxSizer(wx.HORIZONTAL)
61
-
62
- panel = wx.Panel(self)
63
-
64
- self._but_maindir = wx.Button(panel, label='Main Directory')
65
- self._but_maindir.Bind(wx.EVT_BUTTON, self.OnMainDir)
66
-
67
- self._listbox_studyarea = wx.ListBox(panel, choices=[], style=wx.LB_SINGLE)
68
- self._listbox_studyarea.Bind(wx.EVT_LISTBOX, self.OnStudyArea)
69
- self._listbox_studyarea.SetToolTip("Choose the study area")
70
-
71
- self._listbox_scenario = wx.ListBox(panel, choices=[], style=wx.LB_SINGLE)
72
- self._listbox_scenario.Bind(wx.EVT_LISTBOX, self.OnScenario)
73
- self._listbox_scenario.SetToolTip("Choose the scenario")
74
-
75
-
76
- self._text_process = wx.StaticText(panel, label='Number of threads:')
77
-
78
- self._nb_process = wx.SpinCtrl(panel, value=str(os.cpu_count()), min=1, max=os.cpu_count())
79
- self._nb_process.SetToolTip("Number of threads to use")
80
-
81
- sizer_hor_threads.Add(self._text_process, 1, wx.ALL | wx.EXPAND, 0)
82
- sizer_hor_threads.Add(self._nb_process, 1, wx.ALL | wx.EXPAND, 0)
83
-
84
- sizer_hor1.Add(self._but_maindir, 2, wx.ALL | wx.EXPAND, 0)
85
- sizer_hor1.Add(self._listbox_studyarea, 1, wx.ALL | wx.EXPAND, 0)
86
- sizer_hor1.Add(self._listbox_scenario, 1, wx.ALL | wx.EXPAND, 0)
87
-
88
- self._but_checkfiles = wx.Button(panel, label='Check Files')
89
- self._but_checkfiles.Bind(wx.EVT_BUTTON, self.OnCheckFiles)
90
-
91
- sizer_hor1_1.Add(self._but_checkfiles, 1, wx.ALL | wx.EXPAND, 0)
92
-
93
- self._but_creation = wx.Button(panel, label='DataBase Creation')
94
- self._but_creation.Bind(wx.EVT_BUTTON, self.OnCreation)
95
-
96
- self._steps_db = wx.CheckListBox(panel, choices=steps_base_data_creation.get_list_names(), style=wx.LB_MULTIPLE | wx.CHK_CHECKED)
97
-
98
- self._but_vulnerability = wx.Button(panel, label='Vulnerability')
99
- self._but_vulnerability.Bind(wx.EVT_BUTTON, self.OnVulnerability)
100
-
101
- self._steps_vulnerability = wx.CheckListBox(panel, choices=steps_vulnerability.get_list_names(), style=wx.LB_MULTIPLE | wx.CHK_CHECKED)
102
-
103
- self._but_acceptability = wx.Button(panel, label='Acceptability')
104
- self._but_acceptability.Bind(wx.EVT_BUTTON, self.OnAcceptability)
105
-
106
- self._steps_acceptability = wx.CheckListBox(panel, choices=steps_acceptability.get_list_names(), style=wx.LB_MULTIPLE | wx.CHK_CHECKED)
107
-
108
- sizer_hor2.Add(self._but_creation, 1, wx.ALL | wx.EXPAND, 0)
109
- sizer_hor2.Add(self._steps_db, 1, wx.ALL | wx.EXPAND, 0)
110
-
111
- sizer_hor3.Add(self._but_vulnerability, 1, wx.ALL | wx.EXPAND, 0)
112
- sizer_hor3.Add(self._steps_vulnerability, 1, wx.ALL | wx.EXPAND, 0)
113
-
114
- sizer_hor4.Add(self._but_acceptability, 1, wx.ALL | wx.EXPAND, 0)
115
- sizer_hor4.Add(self._steps_acceptability, 1, wx.ALL | wx.EXPAND, 0)
116
-
117
- sizer_vert1.Add(sizer_hor1, 2, wx.EXPAND, 0)
118
- sizer_vert1.Add(sizer_hor1_1, 1, wx.EXPAND, 0)
119
- sizer_vert1.Add(sizer_hor_threads, 0, wx.EXPAND, 0)
120
- sizer_vert1.Add(sizer_hor2, 1, wx.EXPAND, 0)
121
- sizer_vert1.Add(sizer_hor3, 1, wx.EXPAND, 0)
122
- sizer_vert1.Add(sizer_hor4, 1, wx.EXPAND, 0)
123
-
124
- # ------
125
-
126
- sizer_vert2 = wx.BoxSizer(wx.VERTICAL)
127
-
128
- self._listbox_returnperiods = wx.ListBox(panel, choices=[], style=wx.LB_SINGLE)
129
- self._listbox_returnperiods.SetToolTip("All available return periods in the database")
130
-
131
- self._listbox_sims = wx.ListBox(panel, choices=[], style=wx.LB_SINGLE)
132
- self._listbox_sims.SetToolTip("All available simulations in the database")
133
-
134
- self._listbox_sims.Bind(wx.EVT_LISTBOX, self.OnSims)
135
- self._listbox_sims.Bind(wx.EVT_LISTBOX_DCLICK, self.OnSimsDBLClick)
136
-
137
- sizer_vert2.Add(self._listbox_returnperiods, 1, wx.EXPAND, 0)
138
- sizer_vert2.Add(self._listbox_sims, 1, wx.EXPAND, 0)
139
-
140
- # ------
141
-
142
- sizer_vert3 = wx.BoxSizer(wx.VERTICAL)
143
-
144
- matplotlib.use('WXAgg')
145
-
146
- self._figure = Figure(figsize=(5, 4), dpi=100)
147
- self._axes = self._figure.add_subplot(111)
148
- self._canvas = FigureCanvas(panel, -1, self._figure)
149
- self._toolbar = NavigationToolbar2Wx(self._canvas)
150
- self._toolbar.Realize()
151
-
152
- sizer_vert3.Add(self._canvas, 1, wx.EXPAND, 0)
153
- sizer_vert3.Add(self._toolbar, 0, wx.LEFT | wx.EXPAND, 0)
154
-
155
- # ------
156
-
157
- sizer_hor_main.Add(sizer_vert1, 1, wx.EXPAND, 0)
158
- sizer_hor_main.Add(sizer_vert2, 1, wx.EXPAND, 0)
159
- sizer_hor_main.Add(sizer_vert3, 1, wx.EXPAND, 0)
160
-
161
- panel.SetSizer(sizer_hor_main)
162
- panel.Layout()
163
-
164
- self._but_acceptability.Enable(False)
165
- self._but_vulnerability.Enable(False)
166
- self._but_creation.Enable(False)
167
-
168
- def OnSims(self, e:wx.ListEvent):
169
- """ Load sim into the mapviewer """
170
- pass
171
-
172
- def OnSimsDBLClick(self, e:wx.ListEvent):
173
- """ Load sim into the mapviewer """
174
- if self.mapviewer is None:
175
- return
176
-
177
- from ..PyDraw import draw_type
178
-
179
- idx_sim = e.GetSelection()
180
- tmppath = self._manager.get_filepath_for_return_period(self._manager.get_return_periods()[idx_sim])
181
- if tmppath.stem not in self.mapviewer.get_list_keys(drawing_type=draw_type.ARRAYS):
182
- self.mapviewer.add_object('array', filename=str(tmppath), id=tmppath.stem)
183
- self.mapviewer.Refresh()
184
-
185
- def OnCheckFiles(self, e):
186
- """ Check the files """
187
-
188
- if self._manager is None:
189
- logging.error("No main directory selected -- Nothing to check")
190
- return
191
-
192
- ret = self._manager.check_files()
193
-
194
- if ret == "":
195
- logging.info("All files are present")
196
- with wx.MessageDialog(self, "All files are present in the INPUT directory", "Info", wx.OK | wx.ICON_INFORMATION) as dlg:
197
- dlg.ShowModal()
198
- else:
199
- logging.error(f"Missing files: {ret}")
200
- with wx.MessageDialog(self, f"Missing files: \n{ret}", "Error", wx.OK | wx.ICON_ERROR) as dlg:
201
- dlg.ShowModal()
202
-
203
- def OnMainDir(self, e):
204
-
205
- with wx.DirDialog(self, "Choose the main directory containing the data:",
206
- style=wx.DD_DEFAULT_STYLE
207
- ) as dlg:
208
-
209
- if dlg.ShowModal() == wx.ID_OK:
210
- self._manager = Accept_Manager(dlg.GetPath(), Study_area=None)
211
-
212
- self._listbox_studyarea.Clear()
213
- self._listbox_studyarea.InsertItems(self._manager.get_list_studyareas(), 0)
214
-
215
- self._listbox_scenario.Clear()
216
-
217
- ret = self._manager.check_files()
218
-
219
- if ret == "":
220
- logging.info("All files are present")
221
- self._but_acceptability.Enable(True)
222
- self._but_vulnerability.Enable(True)
223
- self._but_creation.Enable(True)
224
- else:
225
- logging.error(f"Missing files: {ret}")
226
- with wx.MessageDialog(self, f"Missing files: \n{ret}", "Error", wx.OK | wx.ICON_ERROR) as dlg:
227
- dlg.ShowModal()
228
-
229
- else:
230
- return
231
-
232
- def OnStudyArea(self, e):
233
- """ Change the study area """
234
-
235
- if self._manager is None:
236
- return
237
-
238
- study_area:str = self._manager.get_list_studyareas(with_suffix=True)[e.GetSelection()]
239
- self._manager.change_studyarea(study_area)
240
-
241
- self._listbox_scenario.Clear()
242
- self._listbox_scenario.InsertItems(self._manager.get_list_scenarios(), 0)
243
-
244
- if self.mapviewer is not None:
245
- tmp_path = self._manager.IN_STUDY_AREA / study_area
246
-
247
- from ..PyDraw import draw_type
248
- if not tmp_path.stem in self.mapviewer.get_list_keys(drawing_type=draw_type.VECTORS):
249
- self.mapviewer.add_object('vector', filename=str(tmp_path), id=tmp_path.stem)
250
- self.mapviewer.Refresh()
251
-
252
- def OnScenario(self, e):
253
- """ Change the scenario """
254
-
255
- if self._manager is None:
256
- return
257
-
258
- scenario = self._manager.get_list_scenarios()[e.GetSelection()]
259
- self._manager.change_scenario(scenario)
260
-
261
- self._listbox_returnperiods.Clear()
262
- rt = self._manager.get_return_periods()
263
- self._listbox_returnperiods.InsertItems([str(crt) for crt in rt],0)
264
-
265
- self._listbox_sims.Clear()
266
- sims = [str(self._manager.get_filepath_for_return_period(currt).name) for currt in rt]
267
- self._listbox_sims.InsertItems(sims, 0)
268
-
269
- ponds = self._manager.get_ponderations()
270
-
271
- self._axes.clear()
272
- ponds.plot(ax=self._axes, kind='bar')
273
- self._canvas.draw()
274
-
275
- def OnCreation(self, e):
276
- """ Create the database """
277
-
278
- if self._manager is None:
279
- return
280
-
281
- steps = list(self._steps_db.GetCheckedStrings())
282
- steps = [int(cur.split('-')[1]) for cur in steps]
283
-
284
- if len(steps) == 0:
285
- logging.error("No steps selected")
286
- return
287
-
288
- nb = int(self._nb_process.GetValue())
289
-
290
- if nb == 1:
291
- logging.warning("Running in single thread")
292
- logging.warning("This may take a long time")
293
-
294
- Base_data_creation(self._manager.main_dir, number_procs=nb, steps=steps)
295
-
296
-
297
- def OnVulnerability(self, e):
298
- """ Run the vulnerability """
299
-
300
- if self._manager is None:
301
- return
302
-
303
- steps = list(self._steps_vulnerability.GetCheckedStrings())
304
- steps = [int(cur.split('-')[1]) for cur in steps]
305
-
306
- if len(steps) == 0:
307
- logging.error("No steps selected")
308
- return
309
-
310
- Vulnerability(self._manager.main_dir,
311
- scenario=self._manager.scenario,
312
- Study_area=self._manager.Study_area,
313
- steps=steps)
314
-
315
- def OnAcceptability(self, e):
316
- """ Run the acceptability """
317
-
318
- if self._manager is None:
319
- return
320
-
321
- steps = list(self._steps_acceptability.GetCheckedStrings())
322
- steps = [int(cur.split('-')[1]) for cur in steps]
323
-
324
- if len(steps) == 0:
325
- logging.error("No steps selected")
326
- return
327
-
328
- Acceptability(self._manager.main_dir,
329
- scenario=self._manager.scenario,
330
- Study_area=self._manager.Study_area,
331
- steps=steps)
1
+ """
2
+ Author: University of Liege, HECE
3
+ Date: 2024
4
+
5
+ Copyright (c) 2024 University of Liege. All rights reserved.
6
+
7
+ This script and its content are protected by copyright law. Unauthorized
8
+ copying or distribution of this file, via any medium, is strictly prohibited.
9
+ """
10
+ from .acceptability import Base_data_creation, Database_to_raster, Vulnerability, Acceptability
11
+ from .acceptability import steps_base_data_creation, steps_vulnerability, steps_acceptability
12
+ from .func import Accept_Manager
13
+ from ..scenario.config_manager import Config_Manager_2D_GPU
14
+ import wx
15
+ import glob
16
+ import wx.lib.dialogs
17
+ import logging
18
+ import subprocess
19
+ import matplotlib
20
+ import shutil
21
+ from matplotlib.figure import Figure
22
+ from matplotlib.backends.backend_wxagg import FigureCanvasWxAgg as FigureCanvas
23
+ from matplotlib.backends.backend_wxagg import NavigationToolbar2WxAgg as NavigationToolbar2Wx
24
+ import os
25
+ from pathlib import Path
26
+ from gettext import gettext as _
27
+
28
+ from wolfhece.Results2DGPU import wolfres2DGPU
29
+ from pathlib import Path
30
+
31
+ def read_result(fn) :
32
+ wolfres2DGPU_X= wolfres2DGPU(fn)
33
+ return wolfres2DGPU_X
34
+
35
+ def nullvalue_for_hole(WA):
36
+ #WolfArray
37
+ WA.nullvalue = 0.
38
+ WA.set_nullvalue_in_mask()
39
+
40
+ def read_export_z_bin(fn_read, fn_write):
41
+ wolfres2DGPU_test = read_result(fn_read)
42
+ wolfres2DGPU_test.read_oneresult(-1)
43
+ wd = wolfres2DGPU_test.get_h_for_block(1)
44
+ top = wolfres2DGPU_test.get_top_for_block(1)
45
+ nullvalue_for_hole(wd)
46
+ nullvalue_for_hole(top)
47
+ wd.array = wd.array + top.array
48
+ wd.write_all(fn_write)
49
+
50
+ def empty_folder(folder):
51
+ if os.path.exists(folder):
52
+ for files in os.listdir(folder):
53
+ fn = os.path.join(folder, files)
54
+ try:
55
+ if os.path.isfile(fn) or os.path.islink(fn):
56
+ os.unlink(fn)
57
+ elif os.path.isdir(fn):
58
+ shutil.rmtree(fn)
59
+ except Exception as e:
60
+ print(f"Error when deleting file {fn}: {e}")
61
+ else:
62
+ print("The folder does not exist.")
63
+
64
+
65
+
66
+ class AcceptabilityGui(wx.Frame):
67
+ """ The main frame for the vulnerability/acceptability computation """
68
+
69
+ def __init__(self, parent=None, width=1024, height=500):
70
+
71
+ super(wx.Frame, self).__init__(parent, title='Acceptability', size=(width, height))
72
+
73
+ self._manager = None
74
+ self._mapviewer = None
75
+ self.InitUI()
76
+
77
+ @property
78
+ def mapviewer(self):
79
+ return self._mapviewer
80
+
81
+ @mapviewer.setter
82
+ def mapviewer(self, value):
83
+ from ..PyDraw import WolfMapViewer
84
+
85
+ if not isinstance(value, WolfMapViewer):
86
+ raise TypeError("The mapviewer must be a WolfMapViewer")
87
+
88
+ self._mapviewer = value
89
+
90
+ def InitUI(self):
91
+
92
+ sizer_hor_main = wx.BoxSizer(wx.HORIZONTAL)
93
+
94
+ sizer_vert1 = wx.BoxSizer(wx.VERTICAL)
95
+
96
+ sizer_hor_threads = wx.BoxSizer(wx.HORIZONTAL)
97
+ sizer_hor1 = wx.BoxSizer(wx.HORIZONTAL)
98
+ sizer_hor1_1 = wx.BoxSizer(wx.HORIZONTAL)
99
+ sizer_hor2 = wx.BoxSizer(wx.HORIZONTAL)
100
+ sizer_hor3 = wx.BoxSizer(wx.HORIZONTAL)
101
+ sizer_hor4 = wx.BoxSizer(wx.HORIZONTAL)
102
+
103
+ # 1st LINE
104
+ panel = wx.Panel(self)
105
+ self._but_maindir = wx.Button(panel, label='Main Directory')
106
+ self._but_maindir.Bind(wx.EVT_BUTTON, self.OnMainDir)
107
+
108
+ self._listbox_studyarea = wx.ListBox(panel, choices=[], style=wx.LB_SINGLE)
109
+ self._listbox_studyarea.Bind(wx.EVT_LISTBOX, self.OnStudyArea)
110
+ self._listbox_studyarea.SetToolTip("Choose the study area")
111
+
112
+ self._listbox_scenario = wx.ListBox(panel, choices=[], style=wx.LB_SINGLE)
113
+ self._listbox_scenario.Bind(wx.EVT_LISTBOX, self.OnScenario)
114
+ self._listbox_scenario.SetToolTip("Choose the scenario")
115
+
116
+
117
+ # 3nd LINE
118
+ self._text_process = wx.StaticText(panel, label='Number of threads:')
119
+
120
+ self._nb_process = wx.SpinCtrl(panel, value=str(os.cpu_count()), min=1, max=os.cpu_count())
121
+ self._nb_process.SetToolTip("Number of threads to use")
122
+
123
+ sizer_hor_threads.Add(self._text_process, 1, wx.ALL | wx.EXPAND, 0)
124
+ sizer_hor_threads.Add(self._nb_process, 1, wx.ALL | wx.EXPAND, 0)
125
+
126
+ sizer_hor1.Add(self._but_maindir, 2, wx.ALL | wx.EXPAND, 0)
127
+ sizer_hor1.Add(self._listbox_studyarea, 1, wx.ALL | wx.EXPAND, 0)
128
+ sizer_hor1.Add(self._listbox_scenario, 1, wx.ALL | wx.EXPAND, 0)
129
+
130
+ # 2nd LINE
131
+ self._but_checkfiles = wx.Button(panel, label='Check directories structure')
132
+ self._but_checkfiles.Bind(wx.EVT_BUTTON, self.OnCheckFiles)
133
+
134
+ sizer_hor1_1.Add(self._but_checkfiles, 1, wx.ALL | wx.EXPAND, 0)
135
+
136
+ # Hydrodynamic part
137
+ self._but_checkfiles = wx.Button(panel, label='Check input water depths')
138
+ self._but_checkfiles.Bind(wx.EVT_BUTTON, self.OnHydrodynInput)
139
+
140
+ sizer_hor1_1.Add(self._but_checkfiles, 1, wx.ALL | wx.EXPAND, 0)
141
+
142
+ self._but_checkfiles = wx.Button(panel, label='Change / Load water depths results')
143
+ self._but_checkfiles.Bind(wx.EVT_BUTTON, self.OnLastStepBin)
144
+
145
+ sizer_hor1_1.Add(self._but_checkfiles, 1, wx.ALL | wx.EXPAND, 0)
146
+
147
+ #Other lines with functions of algorithm
148
+ self._but_creation = wx.Button(panel, label='DataBase Creation')
149
+ self._but_creation.Bind(wx.EVT_BUTTON, self.OnCreation)
150
+
151
+ self._steps_db = wx.CheckListBox(panel, choices=steps_base_data_creation.get_list_names(), style=wx.LB_MULTIPLE | wx.CHK_CHECKED)
152
+
153
+ self._but_vulnerability = wx.Button(panel, label='Vulnerability')
154
+ self._but_vulnerability.Bind(wx.EVT_BUTTON, self.OnVulnerability)
155
+
156
+ self._steps_vulnerability = wx.CheckListBox(panel, choices=steps_vulnerability.get_list_names(), style=wx.LB_MULTIPLE | wx.CHK_CHECKED)
157
+
158
+ self._but_acceptability = wx.Button(panel, label='Acceptability')
159
+ self._but_acceptability.Bind(wx.EVT_BUTTON, self.OnAcceptability)
160
+
161
+ self._steps_acceptability = wx.CheckListBox(panel, choices=steps_acceptability.get_list_names(), style=wx.LB_MULTIPLE | wx.CHK_CHECKED)
162
+
163
+ sizer_hor2.Add(self._but_creation, 1, wx.ALL | wx.EXPAND, 0)
164
+ sizer_hor2.Add(self._steps_db, 1, wx.ALL | wx.EXPAND, 0)
165
+
166
+ sizer_hor3.Add(self._but_vulnerability, 1, wx.ALL | wx.EXPAND, 0)
167
+ sizer_hor3.Add(self._steps_vulnerability, 1, wx.ALL | wx.EXPAND, 0)
168
+
169
+ sizer_hor4.Add(self._but_acceptability, 1, wx.ALL | wx.EXPAND, 0)
170
+ sizer_hor4.Add(self._steps_acceptability, 1, wx.ALL | wx.EXPAND, 0)
171
+
172
+ sizer_vert1.Add(sizer_hor1, 2, wx.EXPAND, 0)
173
+ sizer_vert1.Add(sizer_hor1_1, 1, wx.EXPAND, 0)
174
+ sizer_vert1.Add(sizer_hor_threads, 0, wx.EXPAND, 0)
175
+ sizer_vert1.Add(sizer_hor2, 1, wx.EXPAND, 0)
176
+ sizer_vert1.Add(sizer_hor3, 1, wx.EXPAND, 0)
177
+ sizer_vert1.Add(sizer_hor4, 1, wx.EXPAND, 0)
178
+
179
+ # ------
180
+
181
+ sizer_vert2 = wx.BoxSizer(wx.VERTICAL)
182
+
183
+ self._listbox_returnperiods = wx.ListBox(panel, choices=[], style=wx.LB_SINGLE)
184
+ self._listbox_returnperiods.SetToolTip("All available return periods in the database")
185
+
186
+ self._listbox_sims = wx.ListBox(panel, choices=[], style=wx.LB_SINGLE)
187
+ self._listbox_sims.SetToolTip("All available simulations in the database")
188
+
189
+ self._listbox_sims.Bind(wx.EVT_LISTBOX, self.OnSims)
190
+ self._listbox_sims.Bind(wx.EVT_LISTBOX_DCLICK, self.OnSimsDBLClick)
191
+
192
+ sizer_vert2.Add(self._listbox_returnperiods, 1, wx.EXPAND, 0)
193
+ sizer_vert2.Add(self._listbox_sims, 1, wx.EXPAND, 0)
194
+
195
+ # ------
196
+
197
+ sizer_vert3 = wx.BoxSizer(wx.VERTICAL)
198
+
199
+ matplotlib.use('WXAgg')
200
+
201
+ self._figure = Figure(figsize=(5, 4), dpi=100)
202
+ self._axes = self._figure.add_subplot(111)
203
+ self._canvas = FigureCanvas(panel, -1, self._figure)
204
+ self._toolbar = NavigationToolbar2Wx(self._canvas)
205
+ self._toolbar.Realize()
206
+
207
+ sizer_vert3.Add(self._canvas, 1, wx.EXPAND, 0)
208
+ sizer_vert3.Add(self._toolbar, 0, wx.LEFT | wx.EXPAND, 0)
209
+
210
+ # ------
211
+
212
+ sizer_hor_main.Add(sizer_vert1, 1, wx.EXPAND, 0)
213
+ sizer_hor_main.Add(sizer_vert2, 1, wx.EXPAND, 0)
214
+ sizer_hor_main.Add(sizer_vert3, 1, wx.EXPAND, 0)
215
+
216
+ panel.SetSizer(sizer_hor_main)
217
+ panel.Layout()
218
+
219
+ self._but_acceptability.Enable(False)
220
+ self._but_vulnerability.Enable(False)
221
+ self._but_creation.Enable(False)
222
+
223
+ def OnSims(self, e:wx.ListEvent):
224
+ """ Load sim into the mapviewer """
225
+ pass
226
+
227
+ def OnSimsDBLClick(self, e:wx.ListEvent):
228
+ """ Load sim into the mapviewer """
229
+ if self.mapviewer is None:
230
+ return
231
+
232
+ from ..PyDraw import draw_type
233
+
234
+ idx_sim = e.GetSelection()
235
+ tmppath = self._manager.get_filepath_for_return_period(self._manager.get_return_periods()[idx_sim])
236
+ if tmppath.stem not in self.mapviewer.get_list_keys(drawing_type=draw_type.ARRAYS):
237
+ self.mapviewer.add_object('array', filename=str(tmppath), id=tmppath.stem)
238
+ self.mapviewer.Refresh()
239
+
240
+ def OnCheckFiles(self, e):
241
+ """ Check the files """
242
+
243
+ if self._manager is None:
244
+ logging.error("No main directory selected -- Nothing to check")
245
+ return
246
+
247
+ ret = self._manager.check_files()
248
+
249
+ if ret == "":
250
+ logging.info("The folder is well structured")
251
+ with wx.MessageDialog(self, "The folder is well structured", "Info", wx.OK | wx.ICON_INFORMATION) as dlg:
252
+ dlg.ShowModal()
253
+ else:
254
+ logging.error(f"Missing files: {ret}")
255
+ with wx.MessageDialog(self, f"Missing files: \n{ret}", "Error", wx.OK | wx.ICON_ERROR) as dlg:
256
+ dlg.ShowModal()
257
+
258
+
259
+ def OnHydrodynInput(self,e):
260
+ """ A test to check if the FILLED water depths files exist.
261
+ -If YES : the code can go on
262
+ -If NO : either need to be computed, either the code will use the baseline ones
263
+ """
264
+
265
+ if self._manager is None:
266
+ logging.error("No main directory selected -- Nothing to check")
267
+ return
268
+
269
+ paths_FilledWD = self._manager.get_sims_files_for_baseline()
270
+
271
+ if len(paths_FilledWD) == 0 :
272
+ logging.info("There are no interpolated free surface files. Need for them to go on the acceptability computations.")
273
+ dialog = wx.MessageDialog(None, "There are no interpolated free surface files. Need for them to go on the acceptability computations. Please choose an action.", "Choose an option",
274
+ wx.YES_NO | wx.CANCEL | wx.ICON_QUESTION)
275
+
276
+ dialog.SetYesNoLabels("Use _baseline simulations", "Load simulations")
277
+ response = dialog.ShowModal()
278
+
279
+ if response == wx.ID_YES:
280
+ logging.info("Decision of using baseline simulations.")
281
+ paths_FilledWD_base = self._manager.get_sims_files_for_baseline()
282
+ if len(paths_FilledWD_base) == 0 :
283
+ logging.info("Cannot select files in the _baseline folder.")
284
+ else:
285
+ self._manager.copy_tif_files(paths_FilledWD_base, self._manager.IN_SCEN_DIR)
286
+
287
+ elif response == wx.ID_NO:
288
+ logging.info("Decision of loading simulations.")
289
+ with wx.MessageDialog(self, f"Please use the 'Change / Load water depths results' button of the manager and follow the instructions.", "Redirecting",
290
+ wx.OK | wx.ICON_INFORMATION) as dlg:
291
+ dlg.ShowModal()
292
+ else:
293
+ print("Cancelled")
294
+
295
+ dialog.Destroy()
296
+
297
+ else:
298
+ for names in paths_FilledWD:
299
+ logging.info(f"Interpolated free surface file found: {names.name}.")
300
+ with wx.MessageDialog(self,
301
+ f"{len(paths_FilledWD)} files of interpolated free surface found in the folder (see logs window). If you want to change it, click the 'Change/ Load' water depths results?",
302
+ "Information",
303
+ style=wx.OK | wx.ICON_INFORMATION) as dlg:
304
+ dlg.ShowModal()
305
+
306
+ def OnLastStepBin(self,e):
307
+ """ Link between acceptability and simulations
308
+ -Either the last steps of the steady simulations for the scenarios already exist : only have to point towards them, then the free surfaces are filled
309
+ -Or they dont exist and need to be done outside this manager
310
+ """
311
+
312
+ dlg = wx.DirDialog(None, "Please select the simulation folder that contains the folders 'sim_' to use.", style=wx.DD_DEFAULT_STYLE)
313
+ if dlg.ShowModal() == wx.ID_OK:
314
+ datadir = Path(dlg.GetPath())
315
+ logging.info(f"Selected folder : {datadir}")
316
+ else:
317
+ logging.info('No folder found / selected. Please try again..')
318
+
319
+ sims = {}
320
+ path_LastSteps = Path(self._manager.IN_SA_EXTRACTED)
321
+ empty_folder(path_LastSteps)
322
+ for subdir in datadir.iterdir():
323
+ if subdir.is_dir() and subdir.name.startswith("sim_"):
324
+ sims[subdir.name] = subdir
325
+ fn_read = Path(subdir / "simul_gpu_results")
326
+ logging.info(f"Found simulation folder: {subdir}")
327
+
328
+ parts = subdir.name.split("sim_")
329
+ if len(parts) > 1:
330
+ name = parts[1]
331
+ fn_write = Path(path_LastSteps / (name + '.bin'))
332
+ read_export_z_bin(fn_read, fn_write)
333
+ else:
334
+ logging.info(f"Please, ensure your simulations are named with the return period, e.g sim_T4")
335
+
336
+ else:
337
+ logging.info('No folder found / selected. Please try again...')
338
+
339
+ End = False
340
+ path_Interp = Path(self._manager.IN_SA_INTERP)
341
+ bat_file_path = os.path.join(self._manager.IN_SCEN_DIR, "process_files.bat")
342
+ if os.path.exists(bat_file_path):
343
+ logging.info(f"The file {bat_file_path} already exists and will be replaced.")
344
+ os.remove(bat_file_path)
345
+ path_code = os.path.join(self._manager.IN_WATER_DEPTH, "holes.exe")
346
+
347
+ C = None
348
+ D = None
349
+
350
+ for file in os.listdir(Path(self._manager.IN_SA_DEM)):
351
+ file_path = Path(self._manager.IN_SA_DEM) / file
352
+
353
+ if file_path.is_file() and file.startswith("MNT_") and file_path.suffix == ".bin":
354
+ if "mask" not in file:
355
+ D = file_path
356
+ else:
357
+ C = file_path
358
+
359
+ if D == None:
360
+ logging.info("DEM (.bin) not found. The file must begins by 'MNT' and CANNOT include 'mask'")
361
+
362
+ if C == None:
363
+ logging.info("DEM mask (.bin) not found. The file must begins by 'MNT' and must include 'mask'")
364
+
365
+ A = [os.path.join(path_LastSteps, f) for f in os.listdir(path_LastSteps) if f.endswith(".bin")]
366
+ B = [os.path.join(path_Interp, os.path.splitext(os.path.basename(f))[0]) for f in A]
367
+
368
+
369
+ with open(bat_file_path, "w") as bat_file:
370
+ for a, b in zip(A, B):
371
+ line = f'"{path_code}" filling in="{a}" out="{b}" mask="{C}" dem="{D}"\n'
372
+ bat_file.write(line)
373
+ End = True
374
+
375
+ if End == True :
376
+ logging.info("Please wait for the filling computations. A message will appear to notify you when operations are completed.")
377
+ with wx.MessageDialog(self, f"The interpolation of the given free surface will begin when you press OK, please wait.",
378
+ "Redirecting", wx.OK | wx.ICON_INFORMATION) as dlg:
379
+ dlg.ShowModal()
380
+
381
+ empty_folder(self._manager.IN_SA_INTERP)
382
+ subprocess.run([bat_file_path], check=True)
383
+
384
+ renamed_files = []
385
+ path_fichier=self._manager.IN_SA_INTERP
386
+ for file in path_fichier.glob("*.tif"):
387
+ if "_h" in file.name:
388
+ new_name = file.stem.split("_h")[0].replace(".bin", "") + ".tif"
389
+ file.rename(file.with_name(new_name))
390
+ renamed_files.append(new_name)
391
+ #delete the other
392
+ for file in path_fichier.glob("*.tif"):
393
+ if "_combl" in file.name or file.name not in renamed_files:
394
+ file.unlink()
395
+ logging.info("Filling completed.")
396
+ with wx.MessageDialog(self, f"Filling completed. Created files : {renamed_files}",
397
+ "Redirecting", wx.OK | wx.ICON_INFORMATION) as dlg:
398
+ dlg.ShowModal()
399
+
400
+
401
+
402
+ def OnMainDir(self, e):
403
+
404
+ with wx.DirDialog(self, "Choose the main directory containing the data (folders INPUT, TEMP and OUTPUT):",
405
+ style=wx.DD_DEFAULT_STYLE
406
+ ) as dlg:
407
+
408
+ if dlg.ShowModal() == wx.ID_OK:
409
+ self._manager = Accept_Manager(dlg.GetPath(), Study_area=None)
410
+
411
+ self._listbox_studyarea.Clear()
412
+ self._listbox_studyarea.InsertItems(self._manager.get_list_studyareas(), 0)
413
+
414
+ self._listbox_scenario.Clear()
415
+
416
+ ret = self._manager.check_files()
417
+
418
+ if ret == "":
419
+ logging.info("All the files are present")
420
+ self._but_acceptability.Enable(True)
421
+ self._but_vulnerability.Enable(True)
422
+ self._but_creation.Enable(True)
423
+ else:
424
+ logging.error(f"Missing files: {ret}")
425
+ with wx.MessageDialog(self, f"Missing files: \n{ret}", "Error", wx.OK | wx.ICON_ERROR) as dlg:
426
+ dlg.ShowModal()
427
+
428
+ else:
429
+ return
430
+
431
+ def OnStudyArea(self, e):
432
+ """ Change the study area """
433
+
434
+ if self._manager is None:
435
+ return
436
+
437
+ study_area:str = self._manager.get_list_studyareas(with_suffix=True)[e.GetSelection()]
438
+ self._manager.change_studyarea(study_area)
439
+
440
+ self._listbox_scenario.Clear()
441
+ sc = self._manager.get_list_scenarios()
442
+ self._listbox_scenario.InsertItems(sc, 0)
443
+
444
+ if self.mapviewer is not None:
445
+ tmp_path = self._manager.IN_STUDY_AREA / study_area
446
+
447
+ from ..PyDraw import draw_type
448
+ if not tmp_path.stem in self.mapviewer.get_list_keys(drawing_type=draw_type.VECTORS):
449
+ self.mapviewer.add_object('vector', filename=str(tmp_path), id=tmp_path.stem)
450
+ self.mapviewer.Refresh()
451
+
452
+ def OnScenario(self, e):
453
+ """ Change the scenario """
454
+ if self._manager is None:
455
+ return
456
+
457
+ scenario = self._manager.get_list_scenarios()[e.GetSelection()]
458
+ self._manager.change_scenario(scenario)
459
+
460
+ self._listbox_returnperiods.Clear()
461
+ rt = self._manager.get_return_periods()
462
+ if len(rt) != 0 :
463
+ self._listbox_returnperiods.InsertItems([str(crt) for crt in rt],0)
464
+ self._listbox_sims.Clear()
465
+ sims = [str(self._manager.get_filepath_for_return_period(currt).name) for currt in rt]
466
+ self._listbox_sims.InsertItems(sims, 0)
467
+ ponds = self._manager.get_ponderations()
468
+ if isinstance(ponds, list):
469
+ self._axes.clear()
470
+ ponds.plot(ax=self._axes, kind='bar')
471
+ self._canvas.draw()
472
+
473
+ def OnCreation(self, e):
474
+ """ Create the database """
475
+
476
+ if self._manager is None:
477
+ return
478
+
479
+ wx.MessageBox(
480
+ "The database will now be created. This process may take some time, and the window may temporarily stop responding.",
481
+ "Information",
482
+ wx.OK | wx.ICON_INFORMATION
483
+ )
484
+
485
+ steps = list(self._steps_db.GetCheckedStrings())
486
+ steps = [int(cur.split('-')[1]) for cur in steps]
487
+
488
+ if len(steps) != 0:
489
+ Base_data_creation(self._manager.main_dir, number_procs=self._nb_process.GetValue(), steps=steps)
490
+
491
+ wx.MessageBox(
492
+ "The database is created with the selected steps.",
493
+ "Information",
494
+ wx.OK | wx.ICON_INFORMATION
495
+ )
496
+ else :
497
+ wx.MessageBox(
498
+ "No database created because no steps were selected.",
499
+ "Attention",
500
+ wx.OK | wx.ICON_INFORMATION
501
+ )
502
+
503
+ def OnVulnerability(self, e):
504
+ """ Run the vulnerability """
505
+
506
+ if self._manager is None:
507
+ return
508
+
509
+ steps = list(self._steps_vulnerability.GetCheckedStrings())
510
+ steps = [int(cur.split('-')[1]) for cur in steps]
511
+
512
+ if len(steps) == 0:
513
+ logging.error("No steps selected. By default every steps will be performed.")
514
+ Vulnerability(str(self._manager.main_dir),
515
+ scenario=str(self._manager.scenario),
516
+ Study_area=str(self._manager.Study_area),
517
+ steps=[1,10,11,2,3])
518
+ wx.MessageBox(
519
+ "Vulnerability computed with every steps.",
520
+ "Information",
521
+ wx.OK | wx.ICON_INFORMATION
522
+ )
523
+
524
+ else :
525
+ Vulnerability(self._manager.main_dir,
526
+ scenario=self._manager.scenario,
527
+ Study_area=self._manager.Study_area,
528
+ steps=steps)
529
+ wx.MessageBox(
530
+ "Vulnerability computed with the selected steps.",
531
+ "Information",
532
+ wx.OK | wx.ICON_INFORMATION
533
+ )
534
+
535
+ def OnAcceptability(self, e):
536
+ """ Run the acceptability """
537
+
538
+ if self._manager is None:
539
+ return
540
+
541
+ steps = list(self._steps_acceptability.GetCheckedStrings())
542
+ steps = [int(cur.split('-')[1]) for cur in steps]
543
+
544
+ if len(steps) == 0:
545
+ logging.error("No steps selected. By default every steps will be performed.")
546
+ Acceptability(self._manager.main_dir,
547
+ scenario=self._manager.scenario,
548
+ Study_area=self._manager.Study_area,
549
+ steps=[1,2,3,4,5])
550
+ wx.MessageBox(
551
+ "Acceptability computed with every steps.",
552
+ "Information",
553
+ wx.OK | wx.ICON_INFORMATION
554
+ )
555
+ else :
556
+ Acceptability(self._manager.main_dir,
557
+ scenario=self._manager.scenario,
558
+ Study_area=self._manager.Study_area,
559
+ steps=steps)
560
+ wx.MessageBox(
561
+ "Acceptability computed with the selected steps.",
562
+ "Information",
563
+ wx.OK | wx.ICON_INFORMATION
564
+ )