PlotKPC.py 4.4 KB
Newer Older
# PlotKPC.py -- Pamhyr
# Copyright (C) 2023  INRAE
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program.  If not, see <https://www.gnu.org/licenses/>.

# -*- coding: utf-8 -*-
from View.Tools.PamhyrPlot import PamhyrPlot

from PyQt5.QtCore import (
    QCoreApplication
)

_translate = QCoreApplication.translate

class PlotKPC(PamhyrPlot):
    def __init__(self, canvas=None, trad=None, toolbar=None,
                 results=None, reach_id=0, profile_id=0,
                 parent=None):
        super(PlotKPC, self).__init__(
            canvas=canvas,
            trad=trad,
            data=results,
            toolbar=toolbar,
            parent=parent
        )

        self._current_timestamp = max(results.get("timestamps"))
        self._current_reach_id = reach_id
        self._current_profile_id = profile_id

    @property
    def results(self):
        return self.data

    @results.setter
    def results(self, results):
        self.data = results
        self._current_timestamp = max(results.get("timestamps"))

    @timer
    def draw(self, highlight=None):
        self.canvas.axes.cla()
        self.canvas.axes.grid(color='grey', linestyle='--', linewidth=0.5)

        if self.results is None:
            return

        reach = self.results.river.reach(self._current_reach_id)

        self.canvas.axes.set_ylabel(
            _translate("MainWindow_reach", "Elevation (m)"),
        )
        self.canvas.axes.set_xlabel(
            _translate("MainWindow_reach", "KP (m)"),
        )

        kp = reach.geometry.get_kp()
        z_min = reach.geometry.get_z_min()
Theophile Terraz's avatar
Theophile Terraz committed
        z_max = reach.geometry.get_z_max()
        )

        self.line_kp_zmin = self.canvas.axes.plot(
            kp, z_min,
            color='grey', lw=1.
        )

        if len(reach.geometry.profiles) != 0:
            kp = reach.geometry.get_kp()

            # Water elevation
            water_z = list(
                map(
                    lambda p: p.get_ts_key(self._current_timestamp, "Z"),
                    reach.profiles
                )
            )

            self.canvas.axes.plot(
                kp, water_z, lw=1.,
                color='b',
            )

            self.canvas.axes.fill_between(
                kp, z_min, water_z,
Theophile Terraz's avatar
Theophile Terraz committed
                color='skyblue', alpha=0.7, interpolate=True
Theophile Terraz's avatar
Theophile Terraz committed
        self.profile, = self.canvas.axes.plot(
            [
                kp[self._current_profile_id],
                kp[self._current_profile_id]
            ],
            [
                z_max[self._current_profile_id],
                z_min[self._current_profile_id]
            ],
Theophile Terraz's avatar
Theophile Terraz committed
            color='red', lw=1.
        )

        self.canvas.figure.tight_layout()
        self.canvas.figure.canvas.draw_idle()
        if self.toolbar is not None:
            self.toolbar.update()

    def set_reach(self, reach_id):
        self._current_reach_id = reach_id
        self._current_profile_id = 0
Theophile Terraz's avatar
Theophile Terraz committed
        self.update()

    def set_profile(self, profile_id):
        self._current_profile_id = profile_id
Theophile Terraz's avatar
Theophile Terraz committed
        self.update_profil()

    def set_timestamp(self, timestamp):
        self._current_timestamp = timestamp
Theophile Terraz's avatar
Theophile Terraz committed
        self.update()
Theophile Terraz's avatar
Theophile Terraz committed

    def update_profil(self):
        reach = self.results.river.reach(self._current_reach_id)
        kp = reach.geometry.get_kp()
        z_min = reach.geometry.get_z_min()
        z_max = reach.geometry.get_z_max()
        self.profile.set_data(
            [
                kp[self._current_profile_id],
                kp[self._current_profile_id]
            ],
            [
                z_max[self._current_profile_id],
                z_min[self._current_profile_id]
            ]
Theophile Terraz's avatar
Theophile Terraz committed
        )
        self.canvas.figure.canvas.draw_idle()