From 3458a5df9f38bf2b56eca9f6a9a0ff44fcb95d20 Mon Sep 17 00:00:00 2001 From: JeaDylan <101608960+JeaDylan@users.noreply.github.com> Date: Thu, 27 Aug 2026 17:19:31 +0200 Subject: [PATCH] Import INI files: handle INI RK matching and refresh table after import --- .../InitialConditions/InitialConditions.py | 15 ++++++++- src/View/InitialConditions/Table.py | 33 ++++++++----------- 2 files changed, 27 insertions(+), 21 deletions(-) diff --git a/src/Model/InitialConditions/InitialConditions.py b/src/Model/InitialConditions/InitialConditions.py index d0ca7160..0d2a9abb 100644 --- a/src/Model/InitialConditions/InitialConditions.py +++ b/src/Model/InitialConditions/InitialConditions.py @@ -19,6 +19,7 @@ import logging from copy import copy, deepcopy +from math import isclose from tools import trace, timer from functools import reduce from numpy import interp @@ -440,14 +441,26 @@ class InitialConditions(SQLSubModel): def new_from_data(self, rk, discharge, elevation): n = Data(reach=self._reach, status=self._status) + # Values read from Mage INI files are strings, whereas profile RK + # values are stored as floats. Normalize the imported value before + # looking up its associated profile. + rk = float(rk) + section = reduce( lambda acc, s: ( - s if s.rk == rk else acc + s if isclose(s.rk, rk, rel_tol=1e-9, abs_tol=1e-9) + else acc ), self._reach.reach.profiles, None ) + if section is None: + raise ValueError( + f"No profile with RK {rk} exists in reach " + f"{self._reach.name}" + ) + n['section'] = section n['discharge'] = discharge n['elevation'] = elevation diff --git a/src/View/InitialConditions/Table.py b/src/View/InitialConditions/Table.py index 0290f817..c8da8e45 100644 --- a/src/View/InitialConditions/Table.py +++ b/src/View/InitialConditions/Table.py @@ -290,8 +290,6 @@ class InitialConditionTableModel(PamhyrTableModel): logger.error("No results data") return - self.layoutAboutToBeChanged.emit() - ts = max(results.get("timestamps")) res_reach = results.river.get_reach_by_geometry( self._reach.reach @@ -307,19 +305,15 @@ class InitialConditionTableModel(PamhyrTableModel): ) ) - self._undo.push( - ReplaceDataCommand( - self._lst, - list( - map( - lambda d: self._lst.new_from_data(*d), - data - ) - ) + new_data = list( + map( + lambda d: self._lst.new_from_data(*d), + data ) ) self.layoutAboutToBeChanged.emit() + self._undo.push(ReplaceDataCommand(self._lst, new_data)) self.layoutChanged.emit() def read_from_ini(self, file_name): @@ -355,18 +349,17 @@ class InitialConditionTableModel(PamhyrTableModel): line_split[2], line_split[3]]) - self._undo.push( - ReplaceDataCommand( - self._lst, - list( - map( - lambda d: self._lst.new_from_data(*d), - data - ) - ) + new_data = list( + map( + lambda d: self._lst.new_from_data(*d), + data ) ) + self.layoutAboutToBeChanged.emit() + self._undo.push(ReplaceDataCommand(self._lst, new_data)) + self.layoutChanged.emit() + def undo(self): self._undo.undo() self.layoutChanged.emit()