Import INI files: handle INI RK matching and refresh table after import

new_design_pol
JeaDylan 2026-08-27 17:19:31 +02:00
parent e19eeedcc3
commit 3458a5df9f
2 changed files with 27 additions and 21 deletions

View File

@ -19,6 +19,7 @@
import logging import logging
from copy import copy, deepcopy from copy import copy, deepcopy
from math import isclose
from tools import trace, timer from tools import trace, timer
from functools import reduce from functools import reduce
from numpy import interp from numpy import interp
@ -440,14 +441,26 @@ class InitialConditions(SQLSubModel):
def new_from_data(self, rk, discharge, elevation): def new_from_data(self, rk, discharge, elevation):
n = Data(reach=self._reach, status=self._status) 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( section = reduce(
lambda acc, s: ( 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, self._reach.reach.profiles,
None None
) )
if section is None:
raise ValueError(
f"No profile with RK {rk} exists in reach "
f"{self._reach.name}"
)
n['section'] = section n['section'] = section
n['discharge'] = discharge n['discharge'] = discharge
n['elevation'] = elevation n['elevation'] = elevation

View File

@ -290,8 +290,6 @@ class InitialConditionTableModel(PamhyrTableModel):
logger.error("No results data") logger.error("No results data")
return return
self.layoutAboutToBeChanged.emit()
ts = max(results.get("timestamps")) ts = max(results.get("timestamps"))
res_reach = results.river.get_reach_by_geometry( res_reach = results.river.get_reach_by_geometry(
self._reach.reach self._reach.reach
@ -307,19 +305,15 @@ class InitialConditionTableModel(PamhyrTableModel):
) )
) )
self._undo.push( new_data = list(
ReplaceDataCommand( map(
self._lst, lambda d: self._lst.new_from_data(*d),
list( data
map(
lambda d: self._lst.new_from_data(*d),
data
)
)
) )
) )
self.layoutAboutToBeChanged.emit() self.layoutAboutToBeChanged.emit()
self._undo.push(ReplaceDataCommand(self._lst, new_data))
self.layoutChanged.emit() self.layoutChanged.emit()
def read_from_ini(self, file_name): def read_from_ini(self, file_name):
@ -355,18 +349,17 @@ class InitialConditionTableModel(PamhyrTableModel):
line_split[2], line_split[2],
line_split[3]]) line_split[3]])
self._undo.push( new_data = list(
ReplaceDataCommand( map(
self._lst, lambda d: self._lst.new_from_data(*d),
list( data
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): def undo(self):
self._undo.undo() self._undo.undo()
self.layoutChanged.emit() self.layoutChanged.emit()