Forked from HYCAR-Hydro / airGR
Source project has a limited visibility.
Plot.py 3.34 KiB
# Plot.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 -*-

import logging

from functools import reduce

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

logger = logging.getLogger()

unit = {
    "elevation": "meter",
    "water_elevation": "meter",
    "discharge": "m3s",
}


class CustomPlot(PamhyrPlot):
    def __init__(self, x, y, reach, profile, timestamp,
                 data=None, canvas=None, trad=None,
                 toolbar=None, parent=None):
        super(CustomPlot, self).__init__(
            canvas=canvas,
            trad=trad,
            data=data,
            toolbar=toolbar,
            parent=parent
        )

        self._x = x
        self._y = y
        self._reach = reach
        self._profile = profile
        self._timestamp = timestamp

        logger.debug(
            "Create custom plot for: " +
            f"{x} -> {','.join(y)}: " +
            f"reach={reach}, profile={profile}, " +
            f"timestamp={timestamp}"
        )

        self._y_axis = list(
            set(
                map(
                    lambda y: self._trad[y],
                    self._y
                )
            )
        )

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

        if self.data is None:
            return

        self.canvas.axes.set_xlabel(
            self._trad[self._x],
            color='green', fontsize=12
        )
        self.canvas.axes.set_ylabel(
            self._trad[self._y_axis[0]],
            color='green', fontsize=12
        )

        for axes in self._y_axis[1:]:
            logger.info(axes)
            ax_new = ax.twinx()
            ax_new.set_ylabel(
                self._trad[axes],
                color='green', fontsize=12
            )

        if self._x is "kp":
            if "elevation" in self._y:
                logging.info("TODO: kp/elevation")
            if "water_elevation" in self._y:
                logging.info("TODO: kp/water_elevation")
            if "discharge" in self._y:
                logging.info("TODO: kp/discharge")
        elif self._x is "time":
            if "elevation" in self._y:
                logging.info("TODO: time/elevation")
            if "water_elevation" in self._y:
                logging.info("TODO: time/water_elevation")
            if "discharge" in self._y:
                logging.info("TODO: time/discharge")

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

    @timer
    def update(self, reach, profile, timestamp):
        if not self._init:
            self.draw()
            return