From 4023ae73bae92e76d9263b3b7acc1ae3f3304f08 Mon Sep 17 00:00:00 2001 From: Pierre-Antoine Rouby Date: Wed, 21 Aug 2024 12:03:55 +0200 Subject: [PATCH] IC: Use section ID instead of RK. --- .../HydraulicStructures.py | 34 ++++---- .../InitialConditions/InitialConditions.py | 78 ++++++++++++++----- src/View/InitialConditions/Table.py | 24 ++++-- src/View/InitialConditions/UndoCommand.py | 3 + 4 files changed, 93 insertions(+), 46 deletions(-) diff --git a/src/Model/HydraulicStructures/HydraulicStructures.py b/src/Model/HydraulicStructures/HydraulicStructures.py index dadc1507..0b9be72b 100644 --- a/src/Model/HydraulicStructures/HydraulicStructures.py +++ b/src/Model/HydraulicStructures/HydraulicStructures.py @@ -108,23 +108,6 @@ class HydraulicStructure(SQLSubModel): return cls._update_submodel(execute, version, data) - @classmethod - def _db_update_to_0_1_1(cls, execute, data, - origin_version="0.1.0"): - for v in ["input", "output"]: - execute( - "ALTER TABLE hydraulic_structures " + - f"ADD COLUMN {v}_section INTEGER" - ) - - cls._db_update_to_0_1_1_assoc_section_from_rk( - execute, "hydraulic_structures", - reach_column=f"{v}_reach", - rk_column=f"{v}_rk", - section_column=f"{v}_section", - origin_version=origin_version - ) - @classmethod def _db_update_to_0_1_0(cls, execute, data): table = "hydraulic_structures" @@ -178,6 +161,23 @@ class HydraulicStructure(SQLSubModel): f"WHERE pamhyr_id = {pid}" ) + @classmethod + def _db_update_to_0_1_1(cls, execute, data, + origin_version="0.1.0"): + for v in ["input", "output"]: + execute( + "ALTER TABLE hydraulic_structures " + + f"ADD COLUMN {v}_section INTEGER" + ) + + cls._db_update_to_0_1_1_assoc_section_from_rk( + execute, "hydraulic_structures", + reach_column=f"{v}_reach", + rk_column=f"{v}_rk", + section_column=f"{v}_section", + origin_version=origin_version + ) + @classmethod def _db_load(cls, execute, data=None): new = [] diff --git a/src/Model/InitialConditions/InitialConditions.py b/src/Model/InitialConditions/InitialConditions.py index b094a136..8dd644a4 100644 --- a/src/Model/InitialConditions/InitialConditions.py +++ b/src/Model/InitialConditions/InitialConditions.py @@ -29,9 +29,10 @@ logger = logging.getLogger() class Data(SQLSubModel): - def __init__(self, id: int = -1, name: str = "", - comment: str = "", reach=None, - rk: float = 0.0, discharge: float = 0.0, + def __init__(self, id: int = -1, + name: str = "", comment: str = "", + reach=None, section=None, + discharge: float = 0.0, height: float = 0.0, status=None): super(Data, self).__init__(id) @@ -43,13 +44,13 @@ class Data(SQLSubModel): self._name = name self._comment = comment - self._rk = rk + self._section = section self._discharge = discharge self._speed = 0.0 self._elevation = 0.0 self._height = height - if self._rk != 0.0: + if self._section is not None: self._update_from_rk() if self._height != 0.0: self._update_from_height() @@ -65,7 +66,7 @@ class Data(SQLSubModel): name TEXT NOT NULL, comment TEXT NOT NULL, reach INTEGER, - rk REAL NOT NULL, + section INTEGER, discharge REAL NOT NULL, height REAL NOT NULL, {Scenario.create_db_add_scenario()}, @@ -88,6 +89,10 @@ class Data(SQLSubModel): cls._db_update_to_0_1_0(execute, data) + if major == "0" and minor == "1": + if int(release) < 1: + cls._db_update_to_0_1_1(execute, data) + return cls._update_submodel(execute, version, data) @classmethod @@ -95,6 +100,10 @@ class Data(SQLSubModel): table = "initial_conditions" reachs = data['id2pid']['river_reach'] + cls._db_update_to_0_1_1( + execute, data, + origin_version="0.0.*" + ) cls.update_db_add_pamhyr_id(execute, table, data) Scenario.update_db_add_scenario(execute, table) @@ -102,9 +111,9 @@ class Data(SQLSubModel): execute( f"INSERT INTO {table}_tmp " + - "(pamhyr_id, ind, name, comment, reach, rk, " + + "(pamhyr_id, ind, name, comment, reach, section, " + "discharge, height, scenario) " + - "SELECT pamhyr_id, ind, name, comment, reach, rk, " + + "SELECT pamhyr_id, ind, name, comment, reach, section, " + "discharge, height, scenario " + f"FROM {table}" ) @@ -114,11 +123,25 @@ class Data(SQLSubModel): cls._db_update_to_0_1_0_set_reach_pid(execute, table, reachs) + @classmethod + def _db_update_to_0_1_1(cls, execute, data, + origin_version="0.1.0"): + execute( + "ALTER TABLE initial_conditions " + + f"ADD COLUMN section INTEGER" + ) + + cls._db_update_to_0_1_1_assoc_section_from_rk( + execute, "initial_conditions", + origin_version=origin_version + ) + @classmethod def _db_load(cls, execute, data=None): - id = data["reach"].pamhyr_id + reach = data["reach"] + id = reach.pamhyr_id table = execute( - "SELECT pamhyr_id, ind, name, comment, rk, discharge, height " + + "SELECT pamhyr_id, ind, name, comment, section, discharge, height " + "FROM initial_conditions " + f"WHERE reach = {id} " + "ORDER BY ind ASC" @@ -133,17 +156,25 @@ class Data(SQLSubModel): ind = next(it) name = next(it) comment = next(it) - rk = next(it) + section_id = next(it) discharge = next(it) height = next(it) + section = reduce( + lambda acc, s: ( + s if s.pamhyr_id == section_id else acc + ), + reach.reach.profiles, + None + ) + d = cls( id=pid, reach=data["reach"], status=data["status"], name=name, comment=comment, - rk=rk, + section=section, discharge=discharge, height=height, ) @@ -157,13 +188,13 @@ class Data(SQLSubModel): execute( "INSERT INTO " + - "initial_conditions(pamhyr_id, ind, name, comment, rk, " + + "initial_conditions(pamhyr_id, ind, name, comment, section, " + "discharge, height, reach) " + "VALUES (" + f"{self.pamhyr_id}, " + f"{ind}, '{self._db_format(self.name)}', " + f"'{self._db_format(self._comment)}', " + - f"{self._rk}, {self._discharge}, {self._height}, " + + f"{self._section.id}, {self._discharge}, {self._height}, " + f"{self._reach.id}" + ")" ) @@ -174,7 +205,7 @@ class Data(SQLSubModel): new = Data( name=self.name, comment=self._comment, - rk=self._rk, + section=self._section, discharge=self._discharge, height=self._height, reach=self._reach, @@ -194,7 +225,7 @@ class Data(SQLSubModel): elif key == "comment": val = self._comment elif key == "rk": - val = self._rk + val = self._section elif key == "speed": val = self._speed elif key == "discharge": @@ -207,9 +238,9 @@ class Data(SQLSubModel): return val def _update_get_min(self): - profile = self._reach.reach.get_profiles_from_rk(self._rk) - if len(profile) > 0: - min = profile[0].z_min() + profile = self._section + if profile is not None: + min = profile.z_min() else: min = 0.0 @@ -237,7 +268,7 @@ class Data(SQLSubModel): elif key == "comment": self._comment = str(value) elif key == "rk": - self._rk = float(value) + self._section = value self._update_from_rk() elif key == "speed": # Not supposed to be modified @@ -384,7 +415,12 @@ class InitialConditions(SQLSubModel): ) def get_rk(self): - return self._data_get("rk") + return list( + map( + lambda d: d["rk"].rk, + self._data + ) + ) def get_elevation(self): return self._data_get("elevation") diff --git a/src/View/InitialConditions/Table.py b/src/View/InitialConditions/Table.py index 1f6e2e8a..c7b8ad53 100644 --- a/src/View/InitialConditions/Table.py +++ b/src/View/InitialConditions/Table.py @@ -19,6 +19,7 @@ import logging import traceback from tools import trace, timer +from functools import reduce from PyQt5.QtCore import ( Qt, QVariant, QAbstractTableModel, @@ -58,8 +59,8 @@ class ComboBoxDelegate(QItemDelegate): self.editor.addItems( list( map( - str, - self._reach.get_rk() + lambda p: p.display_name(), + self._reach.profiles ) ) ) @@ -73,7 +74,13 @@ class ComboBoxDelegate(QItemDelegate): def setModelData(self, editor, model, index): text = str(editor.currentText()) - model.setData(index, text) + val = reduce( + lambda acc, p: p if text == p.display_name() else acc, + self._reach.profiles, + None + ) + + model.setData(index, val) editor.close() editor.deleteLater() @@ -104,14 +111,15 @@ class InitialConditionTableModel(PamhyrTableModel): row = index.row() column = index.column() - if self._headers[column] is "speed": + if self._headers[column] is "rk": + v = self._lst.get(row)[self._headers[column]] + return v.display_name() + elif self._headers[column] is "speed": z = self._lst.get(row)["elevation"] q = self._lst.get(row)["discharge"] - profile = self._reach.reach.get_profiles_from_rk( - self._lst.get(row)["rk"] - ) + profile = self._lst.get(row)["rk"] if len(profile) >= 1: - speed = profile[0].speed(q, z) + speed = profile.speed(q, z) return f"{speed:.4f}" return "" diff --git a/src/View/InitialConditions/UndoCommand.py b/src/View/InitialConditions/UndoCommand.py index 2331d9bc..298902ed 100644 --- a/src/View/InitialConditions/UndoCommand.py +++ b/src/View/InitialConditions/UndoCommand.py @@ -40,6 +40,9 @@ class SetCommand(QUndoCommand): if column == "name" or column == "comment": _type = str + if column == "rk": + _type = lambda x: x + self._new = _type(new_value) def undo(self):