Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

PR: Added a tool to install KGS_BRF software automatically. #117

Merged
merged 7 commits into from
Dec 8, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,7 @@ Projects/
.spyproject/
@ new-prô'jèt!/

kgs_brf.exe
WHAT.pref
BRFInput.txt
BRFOutput.txt
Expand Down
14 changes: 14 additions & 0 deletions docs/compute_brf.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
Computing the Well Barometric Response Function
===============================================

.. _brf_demo:
.. figure:: img/demo/brf_demo.*
:align: center
:width: 100%
:alt: alternate text
:figclass: align-center

Presentation of the interface to the `KGS Barometric Response Function Software`_
to compute the barometric response function of a well.

.. _KGS Barometric Response Function Software: http://www.kgs.ku.edu/HighPlains/OHP/index_program/brf.html
Binary file added docs/img/demo/brf_demo.gif
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added docs/img/demo/brf_demo.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
1 change: 1 addition & 0 deletions docs/index.rst
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ please contact Jean-Sébastien Gosselin at jean-sebastien.gosselin@ete.inrs.ca.
gapfill_weather_data
plotting_hydrograph
master_recession_curve
compute_brf
groundwater_recharge
about_gwhat
glossary
2 changes: 2 additions & 0 deletions gwhat/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@
@author: jnsebgosselin
"""

import os
__version__ = 'GWHAT 0.1.1'
__date__ = '08/12/2017'
__project_url__ = 'https://github.com/jnsebgosselin/gwhat'
__rootdir__ = os.path.dirname(os.path.realpath(__file__))
Binary file removed gwhat/brf_mod/kgs_brf.exe
Binary file not shown.
4 changes: 2 additions & 2 deletions gwhat/brf_mod/kgs_brf.py
Original file line number Diff line number Diff line change
Expand Up @@ -111,8 +111,8 @@ def run_kgsbrf():
dirname = os.path.dirname(os.path.realpath(__file__))
exename = os.path.join(dirname, 'kgs_brf.exe')
parname = os.path.join(dirname, 'kgs_brf.par')

os.system('""%s" < "%s""' % (exename, parname))
if os.path.exists(exename) and os.path.exists(parname):
os.system('""%s" < "%s""' % (exename, parname))

# process = QtCore.QProcess()
# process.start(exename+" < "+ parname)
Expand Down
136 changes: 127 additions & 9 deletions gwhat/brf_mod/kgs_gui.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,31 +19,112 @@
along with this program. If not, see <http://www.gnu.org/licenses/>
"""

from PyQt5.QtCore import pyqtProperty
from PyQt5.QtCore import Qt, QDate

# ---- Imports: standard libraries

import os
import requests
import zipfile
import io

# ---- Imports: third parties

from PyQt5.QtCore import Qt, QDate, QPoint
from PyQt5.QtWidgets import (QLabel, QDateTimeEdit, QCheckBox, QPushButton,
QApplication, QDialog, QSpinBox, QAbstractSpinBox,
QGridLayout, QDoubleSpinBox, QFrame, QWidget)
QApplication, QSpinBox, QAbstractSpinBox,
QGridLayout, QDoubleSpinBox, QFrame, QWidget,
QDesktopWidget)

from xlrd import xldate_as_tuple
from xlrd.xldate import xldate_from_date_tuple
import numpy as np
import matplotlib as mpl
from matplotlib.backends.backend_qt5agg import FigureCanvasQTAgg


# ---- Imports: local

import gwhat.common.widgets as myqt
from PyQt5.QtCore import pyqtSignal as QSignal
from gwhat.brf_mod.kgs_plot import BRFFigure
from gwhat.common import IconDB, StyleDB, QToolButtonNormal, QToolButtonSmall
from gwhat import brf_mod as bm

mpl.rc('font', **{'family': 'sans-serif', 'sans-serif': ['Arial']})


class KGSBRFInstaller(myqt.QFrameLayout):
"""
A simple widget to download the kgs_brf program and install it in the
proper directory.
http://www.kgs.ku.edu/HighPlains/OHP/index_program/brf.html
"""

sig_kgs_brf_installed = QSignal(str)

def __init__(self, parent=None):
super(KGSBRFInstaller, self).__init__(parent)

self.setAutoFillBackground(True)
self.setAttribute(Qt.WA_DeleteOnClose)

self.install_btn = QPushButton("Install")
self.install_btn.clicked.connect(self.install_kgsbrf)

self.addWidget(self.install_btn, 1, 1)
self.setRowStretch(0, 100)
self.setRowStretch(self.rowCount(), 100)
self.setColumnStretch(0, 100)
self.setColumnStretch(self.columnCount(), 100)

@property
def install_dir(self):
"""Path to the installation folder."""
return os.path.dirname(os.path.realpath(__file__))

@property
def kgs_brf_name(self):
"""Name of the kgs_brf binary executable."""
return "kgs_brf.exe"

@property
def kgs_brf_path(self):
"""Path to the kgs_brf binary executable."""
return os.path.join(self.install_dir, "kgs_brf.exe")

def kgsbrf_is_installed(self):
"""Returns whether kgs_brf is installed or not."""
return os.path.exists(self.kgs_brf_path)

def install_kgsbrf(self):
"""Download and install the kgs_brf software."""
print("Installing KGS_BRF software...", end=" ")
QApplication.setOverrideCursor(Qt.WaitCursor)
url = "http://www.kgs.ku.edu/HighPlains/OHP/index_program/KGS_BRF.zip"
request = requests.get(url)
zfile = zipfile.ZipFile(io.BytesIO(request .content))

if not os.path.exists(self.install_dir):
os.mkdir(self.install_dir)

with open(self.kgs_brf_path, 'wb') as f:
f.write(zfile.read(self.kgs_brf_name))

if self.kgsbrf_is_installed():
self.sig_kgs_brf_installed.emit(self.install_dir)
self.close()
print("done")
else:
print("failed")
QApplication.restoreOverrideCursor()


class BRFManager(myqt.QFrameLayout):
def __init__(self, wldset=None, parent=None):
super(BRFManager, self).__init__(parent)

self.viewer = BRFViewer(wldset, parent)
self.kgs_brf_installer = None
self.__initGUI__()

def __initGUI__(self):
Expand Down Expand Up @@ -133,7 +214,12 @@ def __initGUI__(self):

self.setColumnStretch(self.columnCount(), 100)

# =========================================================================
# ---- Install Panel

if not KGSBRFInstaller().kgsbrf_is_installed():
self.__install_kgs_brf_installer()

# ---- Properties

@property
def lagBP(self):
Expand Down Expand Up @@ -164,6 +250,27 @@ def brfperiod(self):

return (dstart, dend)

# ---- KGS BRF installer

def __install_kgs_brf_installer(self):
"""
Installs a KGSBRFInstaller that overlays the whole brf tool
layout until the KGS_BRF program is installed correctly.
"""
self.kgs_brf_installer = KGSBRFInstaller()
self.kgs_brf_installer.sig_kgs_brf_installed.connect(
self.__uninstall_kgs_brf_installer)
self.addWidget(self.kgs_brf_installer, 0, 0,
self.rowCount(), self.columnCount())

def __uninstall_kgs_brf_installer(self):
"""
Uninstall the KGSBRFInstaller after the KGS_BRF program has been
installed properly.
"""
self.kgs_brf_installer.sig_kgs_brf_installed.disconnect()
self.kgs_brf_installer = None

# =========================================================================

def set_wldset(self, wldset):
Expand Down Expand Up @@ -270,17 +377,16 @@ def calc_brf(self):
return


# :::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::


class BRFViewer(QWidget):

def __init__(self, wldset=None, parent=None):
super(BRFViewer, self).__init__(parent)

self.setWindowTitle('BRF Viewer')
self.setWindowTitle('BRF Results Viewer')
self.setWindowIcon(IconDB().master)
self.setWindowFlags(Qt.Window |
Qt.CustomizeWindowHint |
Qt.WindowMinimizeButtonHint |
Qt.WindowCloseButtonHint)

self.__initGUI__()
Expand Down Expand Up @@ -580,6 +686,18 @@ def plot_brf(self):

def show(self):
super(BRFViewer, self).show()
qr = self.frameGeometry()
if self.parentWidget():
parent = self.parentWidget()

wp = parent.frameGeometry().width()
hp = parent.frameGeometry().height()
cp = parent.mapToGlobal(QPoint(wp/2, hp/2))
else:
cp = QDesktopWidget().availableGeometry().center()

qr.moveCenter(cp)
self.move(qr.topLeft())
self.fig_frame.setFixedSize(self.fig_frame.size())
self.setFixedSize(self.size())

Expand Down
19 changes: 10 additions & 9 deletions gwhat/tests/pytest_test_order.txt
Original file line number Diff line number Diff line change
@@ -1,12 +1,13 @@
1 - test_projet.py
2 - test_search_weather_data.py
3 - test_dwnld_weather_data.py
4 - test_merge_weather_data.py
5 - test_gapfill_weather_gui.py
6 - test_gapfill_weather_algorithm.py
7 - test_manager_data.py
8 - test_hydroprint.py
9 - test_hydrocalc.py
1 - test_projet.py
2 - test_search_weather_data.py
3 - test_dwnld_weather_data.py
4 - test_merge_weather_data.py
5 - test_gapfill_weather_gui.py
6 - test_gapfill_weather_algorithm.py
7 - test_manager_data.py
8 - test_hydroprint.py
9 - test_hydrocalc.py
10 - test_brf_mod.py

test_tabwidget.py
test_read_waterlvl.py
53 changes: 53 additions & 0 deletions gwhat/tests/test_brf_mod.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
# -*- coding: utf-8 -*-

# Copyright © 2014-2017 GWHAT Project Contributors
# https://github.com/jnsebgosselin/gwhat
#
# This file is part of GWHAT (GroundWater Hydrograph Analysis Toolbox).
# Licensed under the terms of the GNU General Public License.

# Standard library imports
import sys
import os

# Third party imports
import pytest
from PyQt5.QtCore import Qt

# Local imports
sys.path.append(os.path.dirname(os.path.dirname(os.path.realpath(__file__))))
from gwhat.brf_mod.kgs_gui import BRFManager, KGSBRFInstaller


# Qt Test Fixtures
# --------------------------------

@pytest.fixture
def brf_manager_bot(qtbot):
brf_manager = BRFManager(None)
qtbot.addWidget(brf_manager)

return brf_manager, qtbot


# Test WLCalc
# -------------------------------

@pytest.mark.run(order=9)
def test_install_kgs_brf(brf_manager_bot):
brf_manager, qtbot = brf_manager_bot
brf_manager.show()
assert brf_manager
assert brf_manager.kgs_brf_installer

# Install the KGS_BRF software and assert that it was correctly
# installed and that the kgs_brf installer was uninstalled correctly.
qtbot.mouseClick(brf_manager.kgs_brf_installer.install_btn, Qt.LeftButton)

qtbot.waitUntil(lambda: brf_manager.kgs_brf_installer is None)
assert KGSBRFInstaller().kgsbrf_is_installed()


if __name__ == "__main__":
pytest.main(['-x', os.path.basename(__file__), '-v', '-rw'])
# pytest.main()