Merge branch 'scenarios' of gitlab.com:pamhyr/pamhyr2 into scenarios

scenarios
Theophile Terraz 2025-10-14 09:01:16 +02:00
commit 315b913fd0
4 changed files with 85 additions and 11 deletions

View File

@ -20,7 +20,7 @@ import os
import logging
import numpy as np
from tools import timer, trace, old_pamhyr_date_to_timestamp
from tools import timer, trace, old_pamhyr_date_to_timestamp, logger_exception
from Solver.CommandLine import CommandLineSolver
@ -543,8 +543,67 @@ class Rubar3(CommandLineSolver):
for d0, d1 in bc.data:
f.write(f"{d1} {d0}\n")
def read_hydlim(self, study, fname, results, qlog=None, name="0"):
return
def read_profil(self, study, fname, results, qlog=None, name="0"):
logger.info(f"read: profil.{name}")
with open(fname, "r") as f:
reachs = []
for i, edge in enumerate(study.river.enable_edges()):
reach = edge.reach
# Add results reach to reach list
res_reach = results.river.add(i)
reachs.append((res_reach, len(reach) + 1))
def read_data_line(f):
line = f.readline().split()
return tuple(map(float, line))
def set_and_compute_limites(reach, ind, z, q, s):
reach.set(ind, timestamp, "Z", z)
reach.set(ind, timestamp, "Q", q)
reach.set(ind, timestamp, "V", s)
profile = reach.profile(ind)
limits = profile.geometry.get_water_limits(z)
reach.set(
ind, timestamp, "water_limits", limits
)
ts = set()
end = False
while True:
line = f.readline()
if line == "":
results.set("timestamps", ts)
return
timestamp = float(line)
ts.add(timestamp)
logger.info(f"read: profil.{name}: timestamp = {timestamp}")
for reach, lm in reachs:
# First profile
e, s, q, z = read_data_line(f)
set_and_compute_limites(reach, 0, z, q, s)
# For each profile
ind = 1
pe, ps, pq, pz = read_data_line(f)
while ind < lm - 2:
e, s, q, z = read_data_line(f)
set_and_compute_limites(
reach, ind,
(pz + z) / 2,
(pq + q) / 2,
(ps + s) / 2
)
pe, ps, pq, pz = z, s, q, e
ind += 1
# Last profile
e, s, q, z = read_data_line(f)
set_and_compute_limites(reach, ind, z, q, s)
@timer
def results(self, study, repertory, qlog=None, name="0"):
@ -554,14 +613,14 @@ class Rubar3(CommandLineSolver):
repertory=repertory,
name=name,
)
results_file = f"hydlim.{name}"
results_file = f"profil.{name}"
fname = os.path.join(repertory, results_file)
if not os.path.isfile(fname):
logger.info(f"Result file {results_file} does not exist")
logger.warning(f"Result file {results_file} does not exist")
return None
try:
self.read_hydlim(study, fname, results, qlog, name=name)
self.read_profil(study, fname, results, qlog, name=name)
except Exception as e:
logger.error(f"Failed to read results")
logger_exception(e)

View File

@ -31,6 +31,7 @@ from platformdirs import user_cache_dir
from Solver.AdisTS import AdisTS
from Solver.Mage import Mage8
from Solver.RubarBE import Rubar3
from tools import logger_exception, pamhyr_db_need_update
from PyQt5 import QtGui
@ -1506,6 +1507,8 @@ class ApplicationWindow(QMainWindow, ListedSubWindow, WindowToolKit):
return False
def open_solver_results(self, solver, results=None):
logger.info(f"{solver} {results}")
def reading_fn():
self._tmp_results = results
@ -1525,7 +1528,10 @@ class ApplicationWindow(QMainWindow, ListedSubWindow, WindowToolKit):
if type(results) is str:
logger.info(f"Open results from {os.path.dirname(results)}")
name = os.path.basename(results).replace(".BIN", "")
if ".BIN" in results:
name = os.path.basename(results).replace(".BIN", "")
elif "profil." in results:
name = os.path.basename(results).replace("profil.", "")
def reading_fn():
self._tmp_results = solver.results(
@ -1664,7 +1670,7 @@ class ApplicationWindow(QMainWindow, ListedSubWindow, WindowToolKit):
dialog.setFileMode(QFileDialog.FileMode.ExistingFile)
dialog.setDefaultSuffix(".BIN")
# dialog.setFilter(dialog.filter() | QtCore.QDir.Hidden)
dialog.setNameFilters(['Mage (*.BIN)'])
dialog.setNameFilters(['Mage (*.BIN)', 'Rubar3 (profil.*)'])
if self._study.filename is not None:
if self._last_solver is None:
@ -1679,8 +1685,15 @@ class ApplicationWindow(QMainWindow, ListedSubWindow, WindowToolKit):
if dialog.exec_():
file_name = dialog.selectedFiles()
logger.info(f"Select results: {file_name}")
solver = None
if ".BIN" in file_name:
solver = Mage8("Mage8")
else:
solver = Rubar3("Rubar3")
self.open_solver_results(
Mage8("Mage"),
solver,
results=file_name[0]
)

View File

@ -63,7 +63,8 @@ class TableModel(PamhyrTableModel):
self._lst = self._parent._solvers
def __init__(self, **kwargs):
self._timestamp = 0.0
self._timestamp = max(kwargs["parent"]._timestamps)
super(TableModel, self).__init__(**kwargs)
def data(self, index, role=Qt.DisplayRole):

View File

@ -500,7 +500,8 @@ class SolverLogWindow(PamhyrWindow):
if self._results is None:
def reading_fn():
self._results = self._solver.results(
self._study, self._workdir, qlog=self._output
self._study, self._workdir,
qlog=self._output, name=self._study.name
)
dlg = WaitingDialog(