Skip to content

Commit

Permalink
PR: Show an error when importing a water level dataset and the data a…
Browse files Browse the repository at this point in the history
…re not monotically increasing in time. (#208)

* Code style

* Move the code to open file in a function

* Return None if not mono increasing

* Docstrings

* Make var name more explicit

* Remove print and uneeded code

* Optimize the layout
  • Loading branch information
jnsebgosselin committed May 11, 2018
1 parent 394eb5f commit c1d098b
Show file tree
Hide file tree
Showing 2 changed files with 40 additions and 31 deletions.
26 changes: 12 additions & 14 deletions gwhat/projet/manager_data.py
Original file line number Diff line number Diff line change
Expand Up @@ -198,6 +198,7 @@ def wldataset_count(self):
return len(self.projet.wldsets)

def import_wldataset(self):
"""Open a dialog window to import a water level dataset from a file."""
if self.projet is None:
msg = ('Please first select a valid WHAT project or '
'create a new one.')
Expand Down Expand Up @@ -281,6 +282,7 @@ def wxdataset_count(self):
return len(self.projet.wxdsets)

def import_wxdataset(self):
"""Open a dialog window to import a weather dataset from a file."""
if self.projet is None:
msg = ("Please first select a valid project or create a new one.")
btn = QMessageBox.Ok
Expand Down Expand Up @@ -443,9 +445,9 @@ def __initUI__(self):
"on how to format your input data files correctly."
"</i></font>"
) % (self._datatype.capitalize(), url_i)
self._msg = QLabel(msg)
self._msg.setVisible(False)
self._msg.setOpenExternalLinks(True)
self._error_lbl = QLabel(msg)
self._error_lbl.setVisible(False)
self._error_lbl.setOpenExternalLinks(True)

# Select Dataset Layout

Expand All @@ -458,10 +460,10 @@ def __initUI__(self):
grp_dset.addWidget(self.directory, row, 1)
grp_dset.addWidget(self.btn_browse, row, 3)
row += 1
grp_dset.addWidget(self._msg, row, 1, 1, 3)
grp_dset.addWidget(self._error_lbl, row, 1, 1, 3)

grp_dset.setContentsMargins(0, 0, 0, 15)
grp_dset.setColumnStretch(2, 100)
grp_dset.setColumnStretch(1, 100)
grp_dset.setVerticalSpacing(15)

# ----- Station Info Groupbox
Expand Down Expand Up @@ -539,6 +541,8 @@ def __initUI__(self):
layout.addLayout(toolbar, 2, 0)

layout.setRowMinimumHeight(3, 15)
layout.setRowStretch(10, 100)
layout.setColumnStretch(0, 100)

def _add_info_field(self, label, widget):
"""Add a new field to the Station Info group box."""
Expand Down Expand Up @@ -624,9 +628,8 @@ def load_dataset(self, filename):
# Load the Data :

QApplication.setOverrideCursor(Qt.WaitCursor)
msg = 'Loading %s data...' % self._datatype
print(msg)
self.ConsoleSignal.emit('<font color=black>%s</font>' % msg)
self.ConsoleSignal.emit(
"<font color=black>Loading %s data...</font>" % self._datatype)
for i in range(5):
QCoreApplication.processEvents()

Expand Down Expand Up @@ -678,7 +681,7 @@ def update_gui(self, filename=None):
dsetname = dsetname.replace(char, '_')
self._dset_name.setText(dsetname)

self._msg.setVisible(
self._error_lbl.setVisible(
self._dataset is None and self.directory.text() != '')
self.btn_ok.setEnabled(self._dataset is not None)
self.grp_info.setEnabled(self._dataset is not None)
Expand Down Expand Up @@ -742,11 +745,6 @@ def close(self):
self.directory.clear()
self.update_gui()

def show(self):
"""Qt method override."""
super(NewDatasetDialog, self).show()
self.setFixedSize(self.size())


class ExportWeatherButton(QToolButtonBase):
"""
Expand Down
45 changes: 28 additions & 17 deletions gwhat/projet/reader_waterlvl.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,15 +21,32 @@

# ---- Read and Load Water Level Datafiles

def read_water_level_datafile(filename):
"""Load a water level dataset from a csv or Excel file."""
def open_water_level_datafile(filename):
"""Open a water level data file and return the data."""
root, ext = os.path.splitext(filename)
if ext not in FILE_EXTS:
print("ERROR: supported file format are: ", FILE_EXTS)
return None
raise ValueError("Supported file format are: ", FILE_EXTS)
else:
print('Loading waterlvl time-series from %s file...' % ext[1:])

if ext == '.csv':
with open(filename, 'r', encoding='utf8') as f:
data = list(csv.reader(f, delimiter=','))
elif ext in ['.xls', '.xlsx']:
with xlrd.open_workbook(filename, on_demand=True) as wb:
sheet = wb.sheet_by_index(0)
data = [sheet.row_values(rowx, start_colx=0, end_colx=None) for
rowx in range(sheet.nrows)]

return data


def read_water_level_datafile(filename):
"""Load a water level dataset from a csv or Excel file."""
data = open_water_level_datafile(filename)
if data is None:
return None

df = {'filename': filename,
'Well': '',
'Well ID': '',
Expand All @@ -43,20 +60,12 @@ def read_water_level_datafile(filename):
'BP': np.array([]),
'ET': np.array([])}

if ext == '.csv':
with open(filename, 'r', encoding='utf8') as f:
data = list(csv.reader(f, delimiter=','))
elif ext in ['.xls', '.xlsx']:
with xlrd.open_workbook(filename, on_demand=True) as wb:
sheet = wb.sheet_by_index(0)
data = [sheet.row_values(rowx, start_colx=0, end_colx=None) for
rowx in range(sheet.nrows)]

# ---- Read the Header

for row, line in enumerate(data):
if len(line) == 0:
continue

try:
label = line[0].lower().replace(":", "").replace("=", "").strip()
except AttributeError:
Expand Down Expand Up @@ -110,12 +119,17 @@ def read_water_level_datafile(filename):
df['Time'] = data[:, 0].astype(float)
df['WL'] = data[:, 1].astype(float)
except ValueError:
print('ERROR: The water level datafile is not formatted correctly')
print('The water level datafile is not formatted correctly')
return None
else:
print('Waterlvl time-series for well %s loaded successfully.' %
df['Well'])

# The data are not monotically increasing in time.
if np.min(np.diff(df['Time'])) <= 0:
print("The data are not monotically increasing in time.")
return None

# Read the barometric data

try:
Expand All @@ -139,9 +153,6 @@ def read_water_level_datafile(filename):
return df


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


def make_waterlvl_continuous(t, wl):
# This method produce a continuous daily water level time series.
# Missing data are filled with nan values.
Expand Down

0 comments on commit c1d098b

Please sign in to comment.