From fe4ae467840aaf230a33c4fea5b1769010650a75 Mon Sep 17 00:00:00 2001
From: Theophile Terraz <theophile.terraz@inrae.fr>
Date: Thu, 19 Sep 2024 10:03:44 +0200
Subject: [PATCH] work on initial conditions

---
 .../InitialConditions/InitialConditions.py    | 35 +++++---
 src/View/InitialConditions/DialogHeight.py    | 23 +++++-
 src/View/InitialConditions/UndoCommand.py     |  5 +-
 src/View/InitialConditions/Window.py          |  3 +-
 ...itialConditions_Dialog_Generator_Height.ui | 82 ++++++++++++++++---
 src/View/ui/Results.ui                        |  2 +-
 6 files changed, 121 insertions(+), 29 deletions(-)

diff --git a/src/Model/InitialConditions/InitialConditions.py b/src/Model/InitialConditions/InitialConditions.py
index c16c87ac..82756070 100644
--- a/src/Model/InitialConditions/InitialConditions.py
+++ b/src/Model/InitialConditions/InitialConditions.py
@@ -21,6 +21,7 @@ import logging
 from copy import copy, deepcopy
 from tools import trace, timer
 from functools import reduce
+from numpy import interp
 
 from Model.Tools.PamhyrDB import SQLSubModel
 
@@ -492,25 +493,39 @@ class InitialConditions(SQLSubModel):
 
         self._generate_resort_data(profiles)
 
-    def generate_height(self, elevation: float):
+    def generate_height(self,
+                        elevation1: float,
+                        elevation2: float,
+                        compute_discharge: bool,
+                        discharge: float):
         profiles = self._reach.reach.profiles.copy()
+        upstream_rk = profiles[0].rk
+        downstream_rk = profiles[-1].rk
         data_discharge = {}
-        if len(self._data) == 0:
-            for profile in profiles:
-                data_discharge[profile.rk] = 0.0
-        else:
-            for data in self._data:
-                data_discharge[data["rk"]] = data["discharge"]
+        if not compute_discharge:
+            if len(self._data) == 0:
+                for profile in profiles:
+                    data_discharge[profile.rk] = 0.0
+            else:
+                for data in self._data:
+                    data_discharge[data["rk"]] = data["discharge"]
+
         self._data = []
         for profile in profiles:
+
+            if not compute_discharge:
+                d = data_discharge[profile.rk]
+            else:
+                d = discharge
+            elevation = interp(profile.rk,
+                               [upstream_rk, downstream_rk],
+                               [elevation1, elevation2])
             new = Data(reach=self._reach, status=self._status)
             new["rk"] = profile.rk
-            new["discharge"] = data_discharge[profile.rk]
+            new["discharge"] = d
             new["elevation"] = elevation
             self._data.append(new)
 
-        self._generate_resort_data(profiles)
-
     def _generate_resort_data(self, profiles):
         is_reverse = False
         if profiles[0].rk > profiles[-1].rk:
diff --git a/src/View/InitialConditions/DialogHeight.py b/src/View/InitialConditions/DialogHeight.py
index e835716d..f65bf24f 100644
--- a/src/View/InitialConditions/DialogHeight.py
+++ b/src/View/InitialConditions/DialogHeight.py
@@ -28,7 +28,7 @@ from PyQt5.QtCore import (
 
 from PyQt5.QtWidgets import (
     QDialogButtonBox, QComboBox, QUndoStack, QShortcut,
-    QDoubleSpinBox
+    QDoubleSpinBox, QCheckBox, QLabel
 )
 
 
@@ -44,10 +44,27 @@ class HeightDialog(PamhyrDialog):
             parent=parent
         )
 
-        self.value = None
+        self.value = [None, None, None]
+        self.option = None
+        self.find(QCheckBox, "checkBox").clicked.connect(
+            self.enable_discharge
+        )
+
+    def enable_discharge(self):
+        cb = self.find(QCheckBox, "checkBox")
+        dsb = self.find(QDoubleSpinBox, "doubleSpinBox_3")
+        l = self.find(QLabel, "label_3")
+        dsb.setEnabled(cb.isChecked())
+        l.setEnabled(cb.isChecked())
 
     def accept(self):
-        self.value = self.find(QDoubleSpinBox, "doubleSpinBox").value()
+        self.value[0] = self.find(QDoubleSpinBox, "doubleSpinBox_1").value()
+        self.value[1] = self.find(QDoubleSpinBox, "doubleSpinBox_2").value()
+        self.option = self.find(QCheckBox, "checkBox").isChecked()
+        if self.option:
+            self.value[2] = self.find(QDoubleSpinBox, "doubleSpinBox_3").value()
+        else:
+            self.value[2] = None
         super().accept()
 
     def reject(self):
diff --git a/src/View/InitialConditions/UndoCommand.py b/src/View/InitialConditions/UndoCommand.py
index b461e643..64fb1a37 100644
--- a/src/View/InitialConditions/UndoCommand.py
+++ b/src/View/InitialConditions/UndoCommand.py
@@ -194,4 +194,7 @@ class GenerateCommand(QUndoCommand):
             self._ics.generate_discharge(self._param,
                                          self._option)
         elif self._generator == "height":
-            self._ics.generate_height(self._param)
+            self._ics.generate_height(self._param[0],
+                                      self._param[1],
+                                      self._option,
+                                      self._param[2])
diff --git a/src/View/InitialConditions/Window.py b/src/View/InitialConditions/Window.py
index 739f4974..5c6589e6 100644
--- a/src/View/InitialConditions/Window.py
+++ b/src/View/InitialConditions/Window.py
@@ -370,5 +370,6 @@ class InitialConditionsWindow(PamhyrWindow):
         dlg = HeightDialog(trad=self._trad, parent=self)
         if dlg.exec():
             value = dlg.value
-            self._table.generate("height", value, None)
+            compute_discharge = dlg.option
+            self._table.generate("height", value, compute_discharge)
             self._update()
diff --git a/src/View/ui/InitialConditions_Dialog_Generator_Height.ui b/src/View/ui/InitialConditions_Dialog_Generator_Height.ui
index 40e9cd9b..ee015bd1 100644
--- a/src/View/ui/InitialConditions_Dialog_Generator_Height.ui
+++ b/src/View/ui/InitialConditions_Dialog_Generator_Height.ui
@@ -6,8 +6,8 @@
    <rect>
     <x>0</x>
     <y>0</y>
-    <width>284</width>
-    <height>80</height>
+    <width>396</width>
+    <height>182</height>
    </rect>
   </property>
   <property name="windowTitle">
@@ -17,7 +17,70 @@
    <locale language="English" country="Europe"/>
   </property>
   <layout class="QGridLayout" name="gridLayout">
+   <item row="0" column="0">
+    <layout class="QHBoxLayout" name="horizontalLayout">
+     <item>
+      <widget class="QLabel" name="label">
+       <property name="text">
+        <string>Upstream Height (m)</string>
+       </property>
+      </widget>
+     </item>
+     <item>
+      <widget class="QDoubleSpinBox" name="doubleSpinBox_1">
+       <property name="minimum">
+        <double>-1000000.000000000000000</double>
+       </property>
+       <property name="maximum">
+        <double>1000000.000000000000000</double>
+       </property>
+      </widget>
+     </item>
+    </layout>
+   </item>
+   <item row="3" column="0">
+    <layout class="QHBoxLayout" name="horizontalLayout_3">
+     <item>
+      <widget class="QLabel" name="label_3">
+       <property name="enabled">
+        <bool>false</bool>
+       </property>
+       <property name="text">
+        <string>Discharge</string>
+       </property>
+      </widget>
+     </item>
+     <item>
+      <widget class="QDoubleSpinBox" name="doubleSpinBox_3">
+       <property name="enabled">
+        <bool>false</bool>
+       </property>
+      </widget>
+     </item>
+    </layout>
+   </item>
    <item row="1" column="0">
+    <layout class="QHBoxLayout" name="horizontalLayout_2">
+     <item>
+      <widget class="QLabel" name="label_2">
+       <property name="text">
+        <string>Downstream Height (m)</string>
+       </property>
+      </widget>
+     </item>
+     <item>
+      <widget class="QDoubleSpinBox" name="doubleSpinBox_2">
+       <property name="minimum">
+        <double>-1000000.000000000000000</double>
+       </property>
+       <property name="value">
+        <double>0.000000000000000</double>
+       </property>
+      </widget>
+     </item>
+    </layout>
+   </item>
+   <item row="4" column="0">
     <widget class="QDialogButtonBox" name="buttonBox">
      <property name="orientation">
       <enum>Qt::Horizontal</enum>
@@ -27,19 +90,12 @@
      </property>
     </widget>
    </item>
-   <item row="0" column="0">
-    <layout class="QHBoxLayout" name="horizontalLayout">
+   <item row="2" column="0">
+    <layout class="QHBoxLayout" name="horizontalLayout_4">
      <item>
-      <widget class="QLabel" name="label">
+      <widget class="QCheckBox" name="checkBox">
        <property name="text">
-        <string>Height (m)</string>
-       </property>
-      </widget>
-     </item>
-     <item>
-      <widget class="QDoubleSpinBox" name="doubleSpinBox">
-       <property name="maximum">
-        <double>1000000.000000000000000</double>
+        <string>Generate Constant Discharge</string>
        </property>
       </widget>
      </item>
diff --git a/src/View/ui/Results.ui b/src/View/ui/Results.ui
index 256ab368..1b4a93ee 100644
--- a/src/View/ui/Results.ui
+++ b/src/View/ui/Results.ui
@@ -132,7 +132,7 @@
         <item row="0" column="0">
          <widget class="QTabWidget" name="tabWidget">
           <property name="currentIndex">
-           <number>0</number>
+           <number>1</number>
           </property>
           <property name="tabsClosable">
            <bool>true</bool>
-- 
GitLab