Skip to content

Commit

Permalink
Merge pull request #264 from jnsebgosselin/imp_brf_selection
Browse files Browse the repository at this point in the history
PR: Improvements to the tool to select a period for the calculation of the BRF
  • Loading branch information
jnsebgosselin committed Feb 11, 2019
2 parents f130e66 + 4b98edd commit d96bb54
Showing 1 changed file with 54 additions and 48 deletions.
102 changes: 54 additions & 48 deletions gwhat/HydroCalc2.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@
# Licensed under the terms of the GNU General Public License.

# ---- Standard library imports

from time import clock
import csv
import os
Expand All @@ -16,7 +15,6 @@


# ---- Third party imports

import numpy as np
from matplotlib.patches import Rectangle
from PyQt5.QtCore import Qt
Expand All @@ -35,7 +33,6 @@


# ---- Local imports

from gwhat.gwrecharge.gwrecharge_gui import RechgEvalWidget
import gwhat.common.widgets as myqt
from gwhat.common.widgets import DialogWindow
Expand Down Expand Up @@ -75,7 +72,7 @@ def __init__(self, datamanager, parent=None):

# Setup BRF calculation tool.
self.brf_eval_widget = BRFManager(parent=self)
self.brf_eval_widget.sig_brfperiod_changed.connect(self.set_brfperiod)
self.brf_eval_widget.sig_brfperiod_changed.connect(self.plot_brfperiod)
self.brf_eval_widget.btn_seldata.sig_value_changed.connect(
lambda: self.toggle_brfperiod_selection(
self.brf_eval_widget.btn_seldata.value())
Expand Down Expand Up @@ -108,10 +105,6 @@ def __init__(self, datamanager, parent=None):
# Selected water level data.
self.wl_selected_i = []

# Barometric Response Function :
self.selected_brfperiod = [None, None]
self._select_brfperiod_flag = False

# Soil Profiles :
self.soilFilename = []
self.SOILPROFIL = SoilProfil()
Expand Down Expand Up @@ -223,10 +216,15 @@ def _setup_mpl_canvas(self):
self.h_etp, = ax1.plot([], [], color='#FF6666', lw=1.5, zorder=500,
ls='-')

# BRF :
# Barometric response function (BRF).
self.h_brf1 = ax0.axvline(0, color='orange')
self.h_brf2 = ax0.axvline(0, color='orange')

self._selected_brfperiod = [None, None]
self._brf_selector = ax0.axvspan(
0, 0, edgecolor='black', facecolor='orange', linestyle=':',
fill=True, alpha=0.15, visible=False)

# Predicted GLUE water levels
self.glue_plt, = ax0.plot([], [])

Expand All @@ -237,15 +235,15 @@ def _setup_mpl_canvas(self):
fill=True, alpha=0.15, visible=False)
ax0.add_patch(self._rect_selector)

# Vertical guide line under cursor :
self.vguide = ax0.axvline(-1, color='red', zorder=40)
self.vguide.set_visible(False)
# Vertical guide line under cursor.
self.vguide = ax0.axvline(
-1, color='black', zorder=40, linestyle='--', lw=1, visible=False)

offset = mpl.transforms.ScaledTranslation(-5/72, 5/72,
self.fig.dpi_scale_trans)

# x and y coorrdinate labels displayed at the right-bottom corner
# of the graph
offset = mpl.transforms.ScaledTranslation(
-5/72, 5/72, self.fig.dpi_scale_trans)
self.xycoord = ax0.text(
1, 0, '', ha='right', transform=ax0.transAxes + offset)
self.xycoord.set_visible(False)
Expand Down Expand Up @@ -511,7 +509,7 @@ def set_wldset(self, wldset):

# Setup BRF widget.
self.brf_eval_widget.set_wldset(wldset)
self.set_brfperiod(self.brf_eval_widget.get_brfperiod())
self.plot_brfperiod()

self.setup_hydrograph()
self.toolbar.update()
Expand Down Expand Up @@ -611,19 +609,17 @@ def btn_mrc2rechg_isClicked(self):
self.SOILPROFIL.zlayer, self.SOILPROFIL.Sy)

# ---- BRF handlers
def set_brfperiod(self, period):
"""Set and plot the value of the BRF calculation period."""
self.selected_brfperiod = period
self.plot_brfperiod()

def plot_brfperiod(self):
"""
Plot on the graph the vertical lines that are used to define the period
over which the BRF is evaluated.
"""
period = (self.selected_brfperiod if
self.brf_eval_widget.isVisible() else [None, None])
for x, vline in zip(period, [self.h_brf1, self.h_brf2]):
if (self.brf_eval_widget.btn_seldata.value() is False and
self.brf_eval_widget.isVisible()):
brfperiod = self.brf_eval_widget.get_brfperiod()
else:
brfperiod = [None, None]
for x, vline in zip(brfperiod, [self.h_brf1, self.h_brf2]):
vline.set_visible(x is not None)
if x is not None:
x = x + self.dt4xls2mpl*self.dformat
Expand All @@ -636,24 +632,16 @@ def toggle_brfperiod_selection(self, value):
the graph.
"""
if self.wldset is None:
self._select_brfperiod_flag = False
self.brf_eval_widget.btn_seldata.setValue(False, silent=True)
if value is True:
self.emit_warning("Please import a valid water "
"level dataset first.")
return

self.brf_eval_widget.btn_seldata.setValue(value, silent=True)
self._select_brfperiod_flag = value
if value is True:
self.toggle_navig_and_select_tools(
self.brf_eval_widget.btn_seldata)
self.selected_brfperiod = [None, None]
elif value is False:
if not all(self.selected_brfperiod):
# The selection of the BRF calculation period was cancelled,
# we reset the period to its previous value.
self.selected_brfperiod = self.brf_eval_widget.get_brfperiod()
self.plot_brfperiod()

# ---- Peaks handlers
Expand Down Expand Up @@ -1221,6 +1209,20 @@ def _draw_rect_selection(self, x2, y2):

self.fig.axes[0].draw_artist(self._rect_selector)

def _draw_brf_selection(self, x2):
"""Draw the period of the BRF selection tool."""
x1 = self._selected_brfperiod[0]
if not all((x1, x2)):
self._brf_selector.set_visible(False)
else:
self._brf_selector.set_xy([[min(x1, x2), 0],
[min(x1, x2), 1],
[max(x1, x2), 1],
[max(x1, x2), 0],
[min(x1, x2), 0]])
self._brf_selector.set_visible(True)
self.fig.axes[0].draw_artist(self._brf_selector)

def _draw_mouse_cursor(self, x, y):
"""Draw a vertical and horizontal line at the specified xy position."""
if not all((x, y)):
Expand Down Expand Up @@ -1256,6 +1258,19 @@ def on_axes_leave(self, event):
"""Handle when the mouse cursor leaves an axe."""
self.toolbar.set_cursor(1)

def on_brf_select(self):
"""
Handle when a period has been selected for the BRF calculation.
"""
if all(self._selected_brfperiod):
brfperiod = [None, None]
for i in range(2):
x = self._selected_brfperiod[i] - (
self.dt4xls2mpl * self.dformat)
brfperiod[i] = self.time[np.argmin(np.abs(x - self.time))]
self.brf_eval_widget.set_brfperiod(brfperiod)
self.toggle_brfperiod_selection(False)

def on_rect_select(self):
"""
Handle when a rectangular area to select water level data has been
Expand Down Expand Up @@ -1315,6 +1330,9 @@ def on_mouse_move(self, event):

if self.rect_select_is_active and self.__mouse_btn_is_pressed:
self._draw_rect_selection(x, y)
if (self.brf_eval_widget.btn_seldata.value() and
self.__mouse_btn_is_pressed):
self._draw_brf_selection(x)

# ---- Remove Peak Cursor
if self.btn_delpeak.value() and len(self.peak_indx) > 0:
Expand Down Expand Up @@ -1365,6 +1383,10 @@ def onrelease(self, event):
self._rect_selection[1] = (event.xdata, event.ydata)
self._rect_selector.set_visible(False)
self.on_rect_select()
if self.brf_eval_widget.btn_seldata.value() is True:
self._selected_brfperiod[1] = event.xdata
self._brf_selector.set_visible(False)
self.on_brf_select()

if self.is_all_btn_raised():
self.draw()
Expand Down Expand Up @@ -1441,23 +1463,7 @@ def onclick(self, event):
self.__addPeakVisible = False
self.draw()
elif self.brf_eval_widget.btn_seldata.value() is True:
# Select the BRF calculation period.
xclic = event.xdata
if xclic is None:
return

xclic = xclic - (self.dt4xls2mpl * self.dformat)
argmin = np.argmin(np.abs(xclic - self.time))
i = 0 if self.selected_brfperiod[0] is None else 1
self.selected_brfperiod[i] = self.time[argmin]

if all(self.selected_brfperiod):
# Toggle off the selection of the BRF evaluation period and
# send the values to the BRF eval widget.
self.brf_eval_widget.set_brfperiod(self.selected_brfperiod)
self.toggle_brfperiod_selection(False)
else:
self.plot_brfperiod()
self._selected_brfperiod[0] = event.xdata
elif self.rect_select_is_active:
self._rect_selection[0] = (event.xdata, event.ydata)
else:
Expand Down

0 comments on commit d96bb54

Please sign in to comment.