# 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 . # -*- 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() self.set_ticks_time_formater() self.enable_legend() self.idle() self.update_current() 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): self._current, = self.canvas.axes.plot( [self._current_timestamp, self._current_timestamp], self.canvas.axes.get_ylim(), # label=self.label_timestamp, color="grey", linestyle="dashed", lw=1., ) 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.draw() 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_current() self.update_idle() 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, self.canvas.axes.get_ylim() ) def update_current(self): self._current.set_data( self._current_timestamp, self.canvas.axes.get_ylim() )