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: Store water levels in a Pandas DataFrame and index time with datetime instead of Excel numerical date #276

Merged
merged 28 commits into from
Apr 10, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
28 commits
Select commit Hold shift + click to select a range
0846b04
Improve print statement to show the file name.
jnsebgosselin Apr 8, 2019
0bbbba2
Refactor read_water_level_datafile function
jnsebgosselin Apr 8, 2019
8baf81f
Correctly convert data to numeric with pandas
jnsebgosselin Apr 8, 2019
c2edc0d
Define some global var
jnsebgosselin Apr 8, 2019
ba4198f
Check if filename is valid
jnsebgosselin Apr 8, 2019
93f74db
Improve header parsing
jnsebgosselin Apr 8, 2019
35e3b6e
Improve data parsing
jnsebgosselin Apr 8, 2019
0da99cb
Add EmptyWLDataset class
jnsebgosselin Apr 8, 2019
35eb19b
Codestyle
jnsebgosselin Apr 8, 2019
cd0b4ab
Rename attrs Well Name to Well
jnsebgosselin Apr 9, 2019
ff5488d
Move EmptyWLDataset up
jnsebgosselin Apr 9, 2019
f59d568
Move some logics to a new WLDataset class
jnsebgosselin Apr 9, 2019
bd317cb
implement the new dataset in WLDataFrameBase
jnsebgosselin Apr 9, 2019
9d3bbee
Access WL data fom panda dataframe directly
jnsebgosselin Apr 9, 2019
b5a72b8
Refactor __getitem__ of WLDataFrame
jnsebgosselin Apr 9, 2019
e5657d0
WLDataFrame: Refactor __load_dataset__
jnsebgosselin Apr 9, 2019
b2bf494
WLCalc: refactor time property
jnsebgosselin Apr 9, 2019
d4f5bf1
Codestyle
jnsebgosselin Apr 9, 2019
eebaf03
Refactor ProjetReader and WLDataFrameHDF5
jnsebgosselin Apr 9, 2019
a1a47c3
Add pandas to the requirements
jnsebgosselin Apr 9, 2019
fb1b9af
Fix BRFManager to use the new xlsdate property
jnsebgosselin Apr 9, 2019
9bfcc29
Fix manager_data.py
jnsebgosselin Apr 9, 2019
5d02623
Fix HydroPrint
jnsebgosselin Apr 9, 2019
5664aa3
Use xldate attr instead of 'Time'
jnsebgosselin Apr 9, 2019
18b8cb1
Fix test_reading_waterlvl
jnsebgosselin Apr 9, 2019
bec51b2
Fi x test_reading_waterlvl (2)
jnsebgosselin Apr 9, 2019
543dd55
Clean test_reading_waterlvl
jnsebgosselin Apr 9, 2019
8b2b9a9
Improve how datetimes are converted to xldates
jnsebgosselin Apr 10, 2019
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
6 changes: 3 additions & 3 deletions gwhat/HydroCalc2.py
Original file line number Diff line number Diff line change
Expand Up @@ -519,7 +519,7 @@ def water_lvl(self):

@property
def time(self):
return np.array([]) if self.wldset is None else self.wldset.datetimes
return np.array([]) if self.wldset is None else self.wldset.xldates

@property
def wldset(self):
Expand Down Expand Up @@ -1228,8 +1228,8 @@ def _draw_obs_wl(self, draw=True):
self.clear_selected_wl(draw=False)
if self.wldset is not None:
self._obs_wl_plt.set_data(
self.wldset.datetimes + (self.dt4xls2mpl * self.dformat),
self.wldset.waterlevels)
self.time + (self.dt4xls2mpl * self.dformat),
self.water_lvl)
self._obs_wl_plt.set_visible(self.wldset is not None)
if draw:
self.draw()
Expand Down
2 changes: 1 addition & 1 deletion gwhat/HydroPrint2.py
Original file line number Diff line number Diff line change
Expand Up @@ -480,7 +480,7 @@ def best_fit_waterlvl(self):
def best_fit_time(self):
wldset = self.dmngr.get_current_wldset()
if wldset is not None:
date0, date1 = self.hydrograph.best_fit_time(wldset['Time'])
date0, date1 = self.hydrograph.best_fit_time(wldset.xldates)
self.date_start_widget.setDate(QDate(date0[0], date0[1], date0[2]))
self.date_end_widget.setDate(QDate(date1[0], date1[1], date1[2]))

Expand Down
16 changes: 7 additions & 9 deletions gwhat/brf_mod/kgs_gui.py
Original file line number Diff line number Diff line change
Expand Up @@ -306,15 +306,13 @@ def set_wldset(self, wldset):
self.btn_seldata.setAutoRaise(True)
self.setEnabled(wldset is not None)
if wldset is not None:
self.set_daterange((self.wldset['Time'][0],
self.wldset['Time'][-1]))
xldates = self.wldset.xldates
self.set_daterange((xldates[0], xldates[-1]))

# Set the period over which the BRF would be evaluated.
saved_brfperiod = wldset.get_brfperiod()
self.set_brfperiod(
(saved_brfperiod[0] or np.floor(self.wldset['Time'][0]),
saved_brfperiod[1] or np.floor(self.wldset['Time'][-1])
))
self.set_brfperiod((saved_brfperiod[0] or np.floor(xldates[0]),
saved_brfperiod[1] or np.floor(xldates[-1])))

def set_daterange(self, daterange):
"""
Expand All @@ -335,11 +333,11 @@ def calc_brf(self):

brfperiod = self.get_brfperiod()
t1 = min(brfperiod)
i1 = np.where(self.wldset['Time'] >= t1)[0][0]
i1 = np.where(self.wldset.xldates >= t1)[0][0]
t2 = max(brfperiod)
i2 = np.where(self.wldset['Time'] <= t2)[0][-1]
i2 = np.where(self.wldset.xldates <= t2)[0][-1]

time = np.copy(self.wldset['Time'][i1:i2+1])
time = np.copy(self.wldset.xldates[i1:i2+1])
wl = np.copy(self.wldset['WL'][i1:i2+1])
bp = np.copy(self.wldset['BP'][i1:i2+1])
if len(bp) == 0:
Expand Down
4 changes: 2 additions & 2 deletions gwhat/gwrecharge/gwrecharge_calc2.py
Original file line number Diff line number Diff line change
Expand Up @@ -98,8 +98,8 @@ def load_data(self, wxdset, wldset):

self.wldset = wldset
self.A, self.B = wldset['mrc/params']
self.twlvl, self.wlobs = self.make_data_daily(wldset['Time'],
wldset['WL'])
self.twlvl, self.wlobs = self.make_data_daily(
wldset.xldate, wldset['WL'])

if not self.A and not self.B:
error = ("Groundwater recharge cannot be computed because a"
Expand Down
4 changes: 2 additions & 2 deletions gwhat/hydrograph4.py
Original file line number Diff line number Diff line change
Expand Up @@ -855,7 +855,7 @@ def draw_waterlvl(self):

# ---- Logger Measures

time = self.wldset['Time']
time = self.wldset.xldates
if self.WLdatum == 1: # masl
water_lvl = self.wldset['Elevation']-self.wldset['WL']
else: # mbgs -> yaxis is inverted
Expand Down Expand Up @@ -1333,7 +1333,7 @@ def LatLong2Dist(LAT1, LON1, LAT2, LON2):
# 0: daily | 1: weekly | 2: monthly | 3: yearly
hydrograph.RAINscale = 100

hydrograph.best_fit_time(wldset['Time'])
hydrograph.best_fit_time(wldset.xldate)
hydrograph.best_fit_waterlvl()
hydrograph.generate_hydrograph()
#
Expand Down
8 changes: 4 additions & 4 deletions gwhat/projet/manager_data.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,10 +24,10 @@
from gwhat.utils import icons
import gwhat.common.widgets as myqt
from gwhat.hydrograph4 import LatLong2Dist
import gwhat.projet.reader_waterlvl as wlrd
from gwhat.projet.reader_waterlvl import WLDataFrame
from gwhat.projet.reader_projet import (INVALID_CHARS, is_dsetname_valid,
make_dsetname_valid)
import gwhat.meteo.weather_reader as wxrd
from gwhat.meteo.weather_reader import WXDataFrame
from gwhat.widgets.buttons import ToolBarWidget
from gwhat.widgets.spinboxes import StrSpinBox

Expand Down Expand Up @@ -698,9 +698,9 @@ def load_dataset(self, filename):

try:
if self._datatype == 'water level':
self._dataset = wlrd.read_water_level_datafile(filename)
self._dataset = WLDataFrame(filename)
elif self._datatype == 'daily weather':
self._dataset = wxrd.WXDataFrame(filename)
self._dataset = WXDataFrame(filename)
except Exception:
self._dataset = None

Expand Down
32 changes: 21 additions & 11 deletions gwhat/projet/reader_projet.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@

# ---- Standard library imports
import os
import csv
import os.path as osp
from shutil import copyfile

Expand All @@ -20,7 +19,7 @@

# ---- Local library imports
from gwhat.meteo.weather_reader import WXDataFrameBase
from gwhat.projet.reader_waterlvl import WLDataFrameBase
from gwhat.projet.reader_waterlvl import WLDataFrameBase, WLDataset
from gwhat.gwrecharge.glue import GLUEDataFrameBase
from gwhat.common.utils import save_content_to_file
from gwhat.utils.math import nan_as_text_tolist, calcul_rmse
Expand Down Expand Up @@ -124,7 +123,6 @@ def backup_project_file(self):
print('done')
return True


# ---- Project Properties
@property
def name(self):
Expand Down Expand Up @@ -183,7 +181,6 @@ def lon(self, x):
self.db.attrs['longitude'] = x

# ---- Water Levels Dataset Handlers

@property
def wldsets(self):
"""
Expand Down Expand Up @@ -224,10 +221,16 @@ def add_wldset(self, name, df):
grp = self.db['wldsets'].create_group(name)

# Water level data
grp.create_dataset('Time', data=df['Time'])
grp.create_dataset('WL', data=df['WL'])
grp.create_dataset('BP', data=df['BP'])
grp.create_dataset('ET', data=df['ET'])
grp.create_dataset(
'Time',
data=np.array(df['Time'], dtype=h5py.special_dtype(vlen=str)))
# See http://docs.h5py.org/en/latest/strings.html as to why this
# is necessary to do this in order to save a list of strings in
# a dataset with h5py.

grp.create_dataset('WL', data=np.copy(df['WL']))
grp.create_dataset('BP', data=np.copy(df['BP']))
grp.create_dataset('ET', data=np.copy(df['ET']))

# Piezometric well info
grp.attrs['filename'] = df['filename']
Expand Down Expand Up @@ -280,7 +283,6 @@ def del_wldset(self, name):
self.db.flush()

# ---- Weather Dataset Handlers

@property
def wxdsets(self):
"""
Expand Down Expand Up @@ -381,8 +383,16 @@ def __init__(self, hdf5group, *args, **kwargs):
def __load_dataset__(self, hdf5group):
self.dset = hdf5group
self._undo_stack = []
self._waterlevels = self.dset['WL'][...]
self._datetimes = self.dset['Time'][...]

columns = []
data = []
for colname in ['Time', 'WL', 'BP', 'ET']:
if len(self.dset[colname][...]):
data.append(self.dset[colname][...])
columns.append(colname)
data = np.vstack(tuple(data)).transpose()
columns = tuple(columns)
self._dataf = WLDataset(data, columns)

# Make older datasets compatible with newer format.
if 'Well ID' not in list(self.dset.attrs.keys()):
Expand Down
Loading