Forked from reversaal / OhmPi
Source project has a limited visibility.
PlotH.py 4.88 KiB
# PlotH.py -- Pamhyr
# Copyright (C) 2023-2024  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 -*-

import logging

from functools import reduce
from datetime import datetime

from tools import timer, trace
from View.Tools.PamhyrPlot import PamhyrPlot

from PyQt5.QtCore import (
    QCoreApplication
)

_translate = QCoreApplication.translate

logger = logging.getLogger()


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

        self._mode = "time"

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

        self.label_x = _translate("Results", "Time (s)")
        self.label_y = _translate("Results", "Discharge (m³/s)")

        self.label_discharge = _translate("Results", "Cross-section discharge")
        self.label_discharge_max = _translate("Results", "Max discharge")
        self.label_timestamp = _translate("Results", "Current timestamp")

        self._isometric_axis = False

        self._auto_relim_update = False
        self._autoscale_update = False

    @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.init_axes()

        if self.results is None:
            return

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

        if reach.geometry.number_profiles == 0:
            self._init = False
            return

        self.draw_max(reach)
        self.draw_data(reach, profile)
        self.draw_current(reach, profile)

        self.set_ticks_time_formater()

        self.enable_legend()

        self.idle()
        self._init = True

    def draw_data(self, reach, profile):
        self.ts = list(self.results.get("timestamps"))
        self.ts.sort()

        x = self.ts
        y = profile.get_key("Q")

        self._line, = self.canvas.axes.plot(
            x, y,
            label=self.label_discharge,
            color=self.color_plot,
            **self.plot_default_kargs
        )

    def draw_current(self, reach, profile):
        y = profile.get_key("Q")

        kargs = self.plot_default_kargs.copy()
        kargs["marker"] = "o"
        self._current, = self.canvas.axes.plot(
            self._current_timestamp,
            y[self.ts.index(self._current_timestamp)],
            # label=self.label_timestamp,
            color=self.color_plot_current,
            **kargs
        )

    def draw_max(self, reach):
        self.ts = list(self.results.get("timestamps"))
        self.ts.sort()

        x = self.ts
        y = []
        for ts in x:
            ts_y = -9999
            for profile in reach.profiles:
                q = profile.get_ts_key(ts, "Q")
                ts_y = max(ts_y, q)
            y.append(ts_y)

        self._line_max, = self.canvas.axes.plot(
            x, y,
            label=self.label_discharge_max,
            color=self.color_plot_highlight,
            linestyle='dotted',
            **self.plot_default_kargs
        )

    def set_reach(self, reach_id):
        self._current_reach_id = reach_id
        self._current_profile_id = 0
        self.update()

    def set_profile(self, profile_id):
        self._current_profile_id = profile_id
        self.update()

    def set_timestamp(self, timestamp):
        self._current_timestamp = timestamp
        self.update()

    def update(self):
        if not self._init:
            self.draw()

        self.update_data()

        self.update_idle()

    def update_data(self):
        reach = self.results.river.reach(self._current_reach_id)
        profile = reach.profile(self._current_profile_id)

        x = self.ts
        y = profile.get_key("Q")

        self._line.set_data(x, y)

        self._current.set_data(
            self._current_timestamp,
            y[self.ts.index(self._current_timestamp)]
        )