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

Read `amrdcrecords.json` only once per invocation

parent 30479fa1
Branches
No related tags found
No related merge requests found
......@@ -10,7 +10,7 @@ import numpy as np
from .data import read_data
from .parameters import get_param, meas_type, year_type
from .records import get_station, read_stations
from .records import get_station
plt.style.use('ggplot')
plt.rcParams['axes.xmargin'] = 0
......@@ -129,11 +129,10 @@ class Overlay(Plotter):
def plot(cls):
num_datasets = 2
datasets = tuple(SimpleNamespace() for _ in range(num_datasets))
stations = read_stations()
for n, dset in enumerate(datasets, start=1):
dset.station_id = get_param(f'station{n}')
dset.year = get_param(f'year{n}', to=year_type)
dset.station = get_station(dset.station_id, stations=stations)
dset.station = get_station(dset.station_id)
dset.name = dset.station['name']
meas = get_param('measurement', to=meas_type)
......
"""Read and parse records of station data."""
from functools import lru_cache
import json
from flask import abort
@lru_cache(maxsize=None)
def read_stations():
"""Read amrdcrecords.json."""
with open('amrdcrecords.json') as f:
return json.load(f)
def get_station(station_id, stations=None):
def get_station(station_id):
"""Get a station record by ID.
Calls `abort(404)` if none is found.
"""
if stations is None:
stations = read_stations()
for station in stations:
for station in read_stations():
if station['id'] == station_id:
return station
abort(404)
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment