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: Add support for input weather data files in xls and xlsx formats #298

Merged
merged 3 commits into from
Oct 20, 2019
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
Binary file added gwhat/meteo/tests/sample_weather_datafile.xls
Binary file not shown.
Binary file added gwhat/meteo/tests/sample_weather_datafile.xlsx
Binary file not shown.
5 changes: 3 additions & 2 deletions gwhat/meteo/tests/test_read_weather_data.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,9 @@
from gwhat.utils.dates import datetimeindex_to_xldates


def test_read_weather_data():
fmeteo = osp.join(osp.dirname(__file__), "sample_weather_datafile.csv")
@pytest.mark.parametrize("ext", ['.csv', '.xls', '.xlsx'])
def test_read_weather_data(ext):
fmeteo = osp.join(osp.dirname(__file__), "sample_weather_datafile" + ext)
wxdset = WXDataFrame(fmeteo)

# Assert that the dataset was loaded correctly and that the 3 lines that
Expand Down
37 changes: 29 additions & 8 deletions gwhat/meteo/weather_reader.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
# ---- Third party imports
import numpy as np
import pandas as pd
import xlrd
from xlrd.xldate import xldate_from_datetime_tuple, xldate_as_datetime

# ---- Local library imports
Expand All @@ -41,6 +42,7 @@
'Tavg': 'Tavg (\u00B0C)',
'Tmin': 'Tmin (\u00B0C)',
'PET': 'PET (mm)'}
FILE_EXTS = ['.out', '.csv', '.xls', '.xlsx']


# ---- API
Expand Down Expand Up @@ -279,15 +281,34 @@ def open_weather_datafile(filename):
Open the csv datafile and try to guess the delimiter.
Return None if this fails.
"""
for dlm in ['\t', ',', ';']:
with open(filename, 'r') as csvfile:
reader = list(csv.reader(csvfile, delimiter=dlm))
for line in reader:
if line and line[0] == 'Station Name':
return reader
root, ext = os.path.splitext(filename)
if ext not in FILE_EXTS:
raise ValueError("Supported file format are: ", FILE_EXTS)
else:
print("Failed to open %s." % os.path.basename(filename))
return None
print('Loading daily weather time series from "%s"...' %
osp.basename(filename))

if ext in ['.csv', '.out']:
for dlm in ['\t', ',', ';']:
with open(filename, 'r') as csvfile:
reader = list(csv.reader(csvfile, delimiter=dlm))
for row in reader:
if re.search(r'(time|datetime|year)',
''.join(row).replace(" ", "").replace("_", ""),
re.IGNORECASE):
if len(row) >= 2:
return reader
else:
break
else:
print("Failed to open %s." % os.path.basename(filename))
return None
elif ext in ['.xls', '.xlsx']:
with xlrd.open_workbook(filename, on_demand=True) as wb:
sheet = wb.sheet_by_index(0)
reader = [sheet.row_values(rowx, start_colx=0, end_colx=None) for
rowx in range(sheet.nrows)]
return reader


def read_weather_datafile(filename):
Expand Down