Skip to content
Snippets Groups Projects
Verified Commit 86f7d9c2 authored by Owen Graham's avatar Owen Graham
Browse files

Refactor to allow use of station IDs

parent f65c1d7f
Branches
No related tags found
No related merge requests found
...@@ -115,20 +115,18 @@ def get_records(): ...@@ -115,20 +115,18 @@ def get_records():
@app.route('/record/years') @app.route('/record/years')
def get_record_years(): def get_record_years():
"""Return a JSON list of years for the selected station.""" """Return a JSON list of years for the selected station."""
name = get_query('name') station = get_station_record(get_query('name'))
for station in get_records(): years = [i['year'] for i in station['records']]
if station['name'] == name: return jsonify(years)
years = [i['year'] for i in station['records']]
return jsonify(years)
abort(404)
@app.route('/plot/boxplot') @app.route('/plot/boxplot')
def plot_boxplot(): def plot_boxplot():
name = get_query('name') station_id = get_query('name')
year1 = get_query('year1', to=year_type) year1 = get_query('year1', to=year_type)
year2 = get_query('year2', to=year_type) year2 = get_query('year2', to=year_type)
meas = get_query('measurement', to=meas_type) meas = get_query('measurement', to=meas_type)
station = get_station_record(station_id)
plot_data = [] plot_data = []
...@@ -142,7 +140,7 @@ def plot_boxplot(): ...@@ -142,7 +140,7 @@ def plot_boxplot():
return row[meas.field] return row[meas.field]
for year in years: for year in years:
data = read_data(name, year) data = read_data(station, year)
selected_data = data[:, meas.field] selected_data = data[:, meas.field]
plot_data.append(selected_data) plot_data.append(selected_data)
...@@ -165,6 +163,7 @@ def plot_boxplot(): ...@@ -165,6 +163,7 @@ def plot_boxplot():
f'Min {meas.title}: {minimum[meas.field]}, Date: ({minimum[0]}).'), f'Min {meas.title}: {minimum[meas.field]}, Date: ({minimum[0]}).'),
fontsize='small', fontsize='small',
) )
name = station['name']
plt.suptitle(f'{meas.title} measurements, {name} Station, ' plt.suptitle(f'{meas.title} measurements, {name} Station, '
f'{start_year} - {end_year}.') f'{start_year} - {end_year}.')
return savefig_response(fig) return savefig_response(fig)
...@@ -172,10 +171,11 @@ def plot_boxplot(): ...@@ -172,10 +171,11 @@ def plot_boxplot():
@app.route('/plot') @app.route('/plot')
def plot(): def plot():
name = get_query('name') station_id = get_query('name')
year = get_query('year', to=year_type) year = get_query('year', to=year_type)
meas = get_query('measurement', to=meas_type) meas = get_query('measurement', to=meas_type)
data = read_data(name, year) station = get_station_record(station_id)
data = read_data(station, year)
fig, axes = plt.subplots() fig, axes = plt.subplots()
fig.set_figheight(6) fig.set_figheight(6)
fig.set_figwidth(12) fig.set_figwidth(12)
...@@ -195,6 +195,7 @@ def plot(): ...@@ -195,6 +195,7 @@ def plot():
f'Min {meas.title}: {minimum[meas.field]}, Date: ({minimum[0]}).'), f'Min {meas.title}: {minimum[meas.field]}, Date: ({minimum[0]}).'),
fontsize='small', fontsize='small',
) )
name = station['name']
plt.suptitle(f'{meas.title} measurements, {name} Station, ' plt.suptitle(f'{meas.title} measurements, {name} Station, '
f'{data[0][0].year}') f'{data[0][0].year}')
return savefig_response(fig) return savefig_response(fig)
...@@ -204,16 +205,19 @@ def plot(): ...@@ -204,16 +205,19 @@ def plot():
def plot_overlay(): def plot_overlay():
num_datasets = 2 num_datasets = 2
datasets = tuple(SimpleNamespace() for _ in range(num_datasets)) datasets = tuple(SimpleNamespace() for _ in range(num_datasets))
stations = get_records()
for n, dset in enumerate(datasets, start=1): for n, dset in enumerate(datasets, start=1):
dset.name = get_query(f'name{n}') dset.station_id = get_query(f'name{n}')
dset.year = get_query(f'year{n}', to=year_type) dset.year = get_query(f'year{n}', to=year_type)
dset.station = get_station_record(dset.station_id, stations=stations)
dset.name = dset.station['name']
meas = get_query('measurement', to=meas_type) meas = get_query('measurement', to=meas_type)
def ignore_feb_29(rows): def ignore_feb_29(rows):
return [row for row in rows if (row[0].month, row[0].day) != (2, 29)] return [row for row in rows if (row[0].month, row[0].day) != (2, 29)]
for dset in datasets: for dset in datasets:
raw_data = read_data(dset.name, dset.year) raw_data = read_data(dset.station, dset.year)
dset.data = np.array(ignore_feb_29(raw_data)) dset.data = np.array(ignore_feb_29(raw_data))
fig, axes = plt.subplots() fig, axes = plt.subplots()
...@@ -292,25 +296,34 @@ def year_type(s): ...@@ -292,25 +296,34 @@ def year_type(s):
raise ValueError(f'bad year arg: {s!r}') raise ValueError(f'bad year arg: {s!r}')
def get_link(name, year): def get_link(station, year):
"""Get the link to a dataset. """Get the link to a dataset.
Calls `abort(404)` if there is no link/record for the given station Calls `abort(404)` if there is no link/record for the given year.
and year.
""" """
for station in get_records(): for record in station['records']:
if station['name'] == name: if record['year'] == year:
for record in station['records']: return record['url']
if record['year'] == year:
return record['url']
abort(404) abort(404)
@app.route('/record/link') @app.route('/record/link')
def get_record_link(): def get_record_link():
name = get_query('name') station = get_station_record(get_query('name'))
year = get_query('year', to=year_type) year = get_query('year', to=year_type)
return jsonify(get_link(name, year)) return jsonify(get_link(station, year))
def get_station_record(station_id, stations=None):
"""Get the station record corresponding to the station ID.
Calls `abort(404)` if there is none."""
if stations is None:
stations = get_records()
for station in stations:
if station['name'] == station_id:
return station
abort(404)
def get_resources(link): def get_resources(link):
...@@ -331,13 +344,13 @@ def get_resources(link): ...@@ -331,13 +344,13 @@ def get_resources(link):
yield url yield url
def read_data(name, year): def read_data(station, year):
daily_data = [] daily_data = []
resource_list = get_resources(get_link(name, year)) resource_list = get_resources(get_link(station, year))
for url in resource_list: for url in resource_list:
with urlopen(url) as data: with urlopen(url) as data:
lines = map(bytes.decode, data) lines = map(bytes.decode, data)
spec = (SOUTH_POLE_DATA if name == 'South Pole Station' spec = (SOUTH_POLE_DATA if station['name'] == 'South Pole Station'
else ONE_HOUR_DATA) else ONE_HOUR_DATA)
for row in asccol.parse_data(lines, spec): for row in asccol.parse_data(lines, spec):
date = datetime.datetime(row.year, row.month, row.day, date = datetime.datetime(row.year, row.month, row.day,
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment