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

Reorder Python functions

parent 2130f4fb
Branches
No related tags found
No related merge requests found
......@@ -106,12 +106,6 @@ def boxplot():
return render_template('boxplot.html')
def read_stations():
"""Read amrdcrecords.json."""
with open('amrdcrecords.json') as f:
return json.load(f)
@app.route('/record/years')
def record_years():
"""Return a list of years for the selected station."""
......@@ -120,54 +114,12 @@ def record_years():
return jsonify(years)
@app.route('/plot/boxplot')
def plot_boxplot():
"""Boxplot one station/measurement @ multiple years."""
station_id = get_param('station')
year1 = get_param('year1', to=year_type)
year2 = get_param('year2', to=year_type)
meas = get_param('measurement', to=meas_type)
station = get_station(station_id)
plot_data = []
start_year, end_year = sorted([year1, year2])
years = range(start_year, end_year + 1)
maximum = minimum = None
def at_index(row):
"""Get the selected field of `row`."""
return row[meas.field]
for year in years:
data = read_data(station, year)
selected_data = data[:, meas.field]
plot_data.append(selected_data)
year_max = max(data, key=at_index)
year_min = min(data, key=at_index)
if maximum is None:
maximum, minimum = year_max, year_min
else:
maximum = max(maximum, year_max, key=at_index)
minimum = min(minimum, year_min, key=at_index)
fig, axes = plt.subplots()
fig.set_figheight(6)
fig.set_figwidth(12)
axes.boxplot(plot_data, positions=years)
axes.set_ylabel(meas.title)
axes.grid(True)
axes.set_title(
(f'Max {meas.title}: {maximum[meas.field]}, Date: ({maximum[0]}). '
f'Min {meas.title}: {minimum[meas.field]}, Date: ({minimum[0]}).'),
fontsize='small',
)
name = station['name']
plt.suptitle(f'{meas.title} measurements, {name} Station, '
f'{start_year} - {end_year}.')
return savefig_response(fig)
@app.route('/record/link')
def record_link():
"""Return the source link for the selected dataset."""
station = get_station(get_param('station'))
year = get_param('year', to=year_type)
return jsonify(get_link(station, year))
@app.route('/plot')
......@@ -261,11 +213,54 @@ def plot_overlay():
return savefig_response(fig)
def savefig_response(fig):
"""Make an image response with `fig.savefig()`."""
buf = BytesIO()
fig.savefig(buf, format='png')
return Response(buf.getvalue(), mimetype='image/png')
@app.route('/plot/boxplot')
def plot_boxplot():
"""Boxplot one station/measurement @ multiple years."""
station_id = get_param('station')
year1 = get_param('year1', to=year_type)
year2 = get_param('year2', to=year_type)
meas = get_param('measurement', to=meas_type)
station = get_station(station_id)
plot_data = []
start_year, end_year = sorted([year1, year2])
years = range(start_year, end_year + 1)
maximum = minimum = None
def at_index(row):
"""Get the selected field of `row`."""
return row[meas.field]
for year in years:
data = read_data(station, year)
selected_data = data[:, meas.field]
plot_data.append(selected_data)
year_max = max(data, key=at_index)
year_min = min(data, key=at_index)
if maximum is None:
maximum, minimum = year_max, year_min
else:
maximum = max(maximum, year_max, key=at_index)
minimum = min(minimum, year_min, key=at_index)
fig, axes = plt.subplots()
fig.set_figheight(6)
fig.set_figwidth(12)
axes.boxplot(plot_data, positions=years)
axes.set_ylabel(meas.title)
axes.grid(True)
axes.set_title(
(f'Max {meas.title}: {maximum[meas.field]}, Date: ({maximum[0]}). '
f'Min {meas.title}: {minimum[meas.field]}, Date: ({minimum[0]}).'),
fontsize='small',
)
name = station['name']
plt.suptitle(f'{meas.title} measurements, {name} Station, '
f'{start_year} - {end_year}.')
return savefig_response(fig)
def get_param(key, to=str):
......@@ -299,23 +294,17 @@ def year_type(s):
raise ValueError(f'bad year arg: {s!r}')
def get_link(station, year):
"""Get the link to a dataset.
Calls `abort(404)` if none is found.
"""
for record in station['records']:
if record['year'] == year:
return record['url']
abort(404)
def savefig_response(fig):
"""Make an image response with `fig.savefig()`."""
buf = BytesIO()
fig.savefig(buf, format='png')
return Response(buf.getvalue(), mimetype='image/png')
@app.route('/record/link')
def record_link():
"""Return the source link for the selected dataset."""
station = get_station(get_param('station'))
year = get_param('year', to=year_type)
return jsonify(get_link(station, year))
def read_stations():
"""Read amrdcrecords.json."""
with open('amrdcrecords.json') as f:
return json.load(f)
def get_station(station_id, stations=None):
......@@ -330,6 +319,17 @@ def get_station(station_id, stations=None):
abort(404)
def get_link(station, year):
"""Get the link to a dataset.
Calls `abort(404)` if none is found.
"""
for record in station['records']:
if record['year'] == year:
return record['url']
abort(404)
def get_resources(link):
"""Fetch the download links for a dataset."""
doc = jsonld.flatten(link + '.jsonld')
......
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please to comment