From 0d22d0e13119e576e0155fea9400efbebcadd073 Mon Sep 17 00:00:00 2001
From: Owen Graham <ohgraham1@madisoncollege.edu>
Date: Mon, 27 Nov 2023 13:10:23 -0600
Subject: [PATCH] Leave all years-by-station listing to the API

---
 make_station_years_json.py |   51 --
 station-years.json         | 1097 ------------------------------------
 visualizer/__init__.py     |   10 +-
 visualizer/data.py         |    5 +
 4 files changed, 7 insertions(+), 1156 deletions(-)
 delete mode 100644 make_station_years_json.py
 delete mode 100644 station-years.json

diff --git a/make_station_years_json.py b/make_station_years_json.py
deleted file mode 100644
index fe7f295..0000000
--- a/make_station_years_json.py
+++ /dev/null
@@ -1,51 +0,0 @@
-"""Fetch the stations/years with AWS data and write them to a file."""
-
-import json
-import sys
-from urllib.parse import quote
-from urllib.request import urlopen
-
-API_ROOT = 'https://amrdcdata.ssec.wisc.edu/data-api'
-OUTFILE = 'station-years.json'
-
-
-def main():
-    print('Fetching station list', file=sys.stderr)
-    with urlopen(f'{API_ROOT}/aws/list') as f:
-        aws_list = json.load(f)
-    stations = [x[0] for x in aws_list['stations']]
-
-    def records_items():
-        total = len(stations)
-        s_width = max(len(repr(s)) for s in stations)
-        n_width = len(str(total))
-        for n, station in enumerate(stations, start=1):
-            print((f'Fetching years for {station!r:{s_width}} '
-                   f'({n:{n_width}}/{total})'),
-                  file=sys.stderr)
-            yield station, years_for_station(station)
-
-    records = dict(records_items())
-    print(f'Writing to {OUTFILE}', file=sys.stderr)
-    with open(OUTFILE, 'w') as f:
-        json.dump(records, f, indent=2)
-    print('Done', file=sys.stderr)
-
-
-def years_for_station(station):
-    """Fetch the years with AWS data for the given station."""
-    with urlopen(f'{API_ROOT}/aws/list/stations={urlesc(station)}') as f:
-        years_list = json.load(f)
-    return [year for x in years_list for year in x]
-
-
-def urlesc(s):
-    """Quote a string as a URL path component.
-
-    Spaces are encoded as '%20'.
-    """
-    return quote(s, safe='')
-
-
-if __name__ == '__main__':
-    main()
diff --git a/station-years.json b/station-years.json
deleted file mode 100644
index 454e438..0000000
--- a/station-years.json
+++ /dev/null
@@ -1,1097 +0,0 @@
-{
-  "AGO-4": [
-    2012,
-    2013,
-    2014,
-    2015,
-    2016,
-    2017,
-    2018,
-    2019,
-    2020,
-    2021,
-    2022,
-    2023
-  ],
-  "AGO-5": [
-    2020
-  ],
-  "Austin": [
-    2015,
-    2016,
-    2017,
-    2019,
-    2020,
-    2021
-  ],
-  "Baldrick": [
-    2009,
-    2010,
-    2011,
-    2012,
-    2013,
-    2014,
-    2015,
-    2016,
-    2017,
-    2018,
-    2019,
-    2020,
-    2021,
-    2022,
-    2023
-  ],
-  "Bear Peninsula": [
-    2011,
-    2012,
-    2013,
-    2014,
-    2015,
-    2016,
-    2017,
-    2018,
-    2019,
-    2020,
-    2021,
-    2022,
-    2023
-  ],
-  "Bonaparte Point": [
-    2003,
-    2004,
-    2009,
-    2010,
-    2011,
-    2012,
-    2013,
-    2014,
-    2015,
-    2016,
-    2017
-  ],
-  "Brianna": [
-    2003,
-    2009,
-    2010,
-    2011,
-    2012,
-    2013,
-    2014,
-    2015
-  ],
-  "Butler Island": [
-    2003,
-    2004,
-    2009,
-    2010,
-    2011,
-    2012
-  ],
-  "Byrd": [
-    2003,
-    2004,
-    2009,
-    2010,
-    2011,
-    2012,
-    2013,
-    2014,
-    2015,
-    2016,
-    2017,
-    2018,
-    2019,
-    2020,
-    2021,
-    2022,
-    2023
-  ],
-  "Cape Bird": [
-    2003,
-    2004,
-    2009,
-    2010,
-    2011,
-    2012,
-    2013,
-    2014,
-    2015,
-    2016,
-    2017,
-    2018,
-    2019,
-    2020,
-    2021,
-    2022,
-    2023
-  ],
-  "Cape Denison": [
-    2003,
-    2004,
-    2009,
-    2010,
-    2011,
-    2021,
-    2022,
-    2023
-  ],
-  "Cape Hallett": [
-    2011,
-    2012,
-    2013,
-    2014,
-    2015,
-    2016,
-    2017,
-    2018,
-    2019,
-    2020
-  ],
-  "Cape Spencer": [
-    2003,
-    2004
-  ],
-  "Carolyn": [
-    2009,
-    2010,
-    2011,
-    2012,
-    2013,
-    2014
-  ],
-  "Clean Air": [
-    2003,
-    2004
-  ],
-  "D-10": [
-    2003,
-    2004,
-    2009,
-    2010,
-    2011,
-    2012,
-    2013,
-    2014,
-    2015,
-    2016,
-    2017,
-    2018,
-    2019,
-    2020,
-    2021,
-    2022,
-    2023
-  ],
-  "D-47": [
-    2003,
-    2004,
-    2009,
-    2010,
-    2011,
-    2012,
-    2013,
-    2014,
-    2015,
-    2016,
-    2017,
-    2018,
-    2019,
-    2020,
-    2021,
-    2022,
-    2023
-  ],
-  "D-85": [
-    2009,
-    2010,
-    2011,
-    2012,
-    2013,
-    2014,
-    2015,
-    2016,
-    2017,
-    2018,
-    2019,
-    2020,
-    2021,
-    2022,
-    2023
-  ],
-  "Dismal Island": [
-    2003,
-    2004,
-    2009,
-    2010,
-    2011,
-    2012,
-    2013,
-    2014,
-    2017,
-    2018,
-    2019,
-    2020,
-    2021,
-    2022
-  ],
-  "Dome C II": [
-    1996,
-    2003,
-    2004,
-    2009,
-    2010,
-    2011,
-    2012,
-    2013,
-    2014,
-    2015,
-    2016,
-    2017,
-    2018,
-    2019,
-    2020,
-    2021,
-    2022,
-    2023
-  ],
-  "Dome Fuji": [
-    2003,
-    2004,
-    2010,
-    2011,
-    2012,
-    2013,
-    2014,
-    2015,
-    2016,
-    2017,
-    2018,
-    2019,
-    2020,
-    2021,
-    2022,
-    2023
-  ],
-  "E-66": [
-    2009,
-    2010,
-    2011
-  ],
-  "Elaine": [
-    2003,
-    2009,
-    2010,
-    2011,
-    2012,
-    2013,
-    2014,
-    2015,
-    2016,
-    2017,
-    2018,
-    2019,
-    2020,
-    2022,
-    2023
-  ],
-  "Elizabeth": [
-    2003,
-    2009,
-    2010,
-    2011,
-    2012,
-    2013,
-    2015,
-    2016,
-    2017,
-    2018,
-    2019,
-    2020,
-    2021,
-    2022,
-    2023
-  ],
-  "Emilia": [
-    2004,
-    2009,
-    2010,
-    2011,
-    2012,
-    2013,
-    2014,
-    2015,
-    2016,
-    2017,
-    2018,
-    2019,
-    2020,
-    2021,
-    2022,
-    2023
-  ],
-  "Emma": [
-    2014,
-    2015,
-    2016,
-    2017,
-    2018,
-    2019,
-    2020,
-    2021,
-    2022,
-    2023
-  ],
-  "Eric": [
-    2009,
-    2010,
-    2011,
-    2012,
-    2013,
-    2014
-  ],
-  "Erin": [
-    2004,
-    2009,
-    2010,
-    2011,
-    2012,
-    2013,
-    2014,
-    2015,
-    2016,
-    2017,
-    2018,
-    2019,
-    2020,
-    2021,
-    2022,
-    2023
-  ],
-  "Evans Knoll": [
-    2011,
-    2012,
-    2013,
-    2014,
-    2015,
-    2016,
-    2017,
-    2018,
-    2019
-  ],
-  "Ferrell": [
-    2003,
-    2004,
-    2009,
-    2010,
-    2011,
-    2012,
-    2013,
-    2014,
-    2015,
-    2016,
-    2017,
-    2018,
-    2019,
-    2020,
-    2021,
-    2022,
-    2023
-  ],
-  "Ferrell II": [
-    2011,
-    2012,
-    2014
-  ],
-  "Fossil Bluff": [
-    2009,
-    2010,
-    2011,
-    2012
-  ],
-  "Gill": [
-    2001,
-    2003,
-    2004,
-    2009,
-    2010,
-    2011,
-    2012,
-    2013,
-    2014,
-    2015,
-    2016,
-    2017,
-    2018,
-    2019,
-    2020,
-    2021,
-    2022,
-    2023
-  ],
-  "Harry": [
-    2003,
-    2004,
-    2009,
-    2010,
-    2011,
-    2012,
-    2013,
-    2014,
-    2016,
-    2017,
-    2018,
-    2019,
-    2020,
-    2021,
-    2022,
-    2023
-  ],
-  "Henry": [
-    2003,
-    2004,
-    2009,
-    2010,
-    2011,
-    2012,
-    2013,
-    2014,
-    2015,
-    2016,
-    2017,
-    2018,
-    2019,
-    2020,
-    2021,
-    2022,
-    2023
-  ],
-  "Herbie Alley": [
-    2003,
-    2004
-  ],
-  "Hugo Island": [
-    2009,
-    2010,
-    2011,
-    2012,
-    2013,
-    2014,
-    2015,
-    2016
-  ],
-  "Janet": [
-    2011,
-    2012,
-    2013,
-    2014,
-    2015,
-    2016,
-    2017,
-    2018,
-    2019,
-    2020,
-    2021,
-    2022,
-    2023
-  ],
-  "JASE2007": [
-    2009,
-    2010,
-    2011,
-    2012,
-    2013,
-    2014,
-    2015,
-    2016,
-    2017,
-    2018,
-    2019,
-    2020,
-    2021,
-    2022,
-    2023
-  ],
-  "Kathie": [
-    2016,
-    2017,
-    2018,
-    2019,
-    2020,
-    2021,
-    2022
-  ],
-  "Kirkwood Island": [
-    2003,
-    2004
-  ],
-  "Kominko-Slade": [
-    2009,
-    2010,
-    2011,
-    2012,
-    2013,
-    2014,
-    2015,
-    2016,
-    2017,
-    2018,
-    2019
-  ],
-  "Larsen Ice Shelf": [
-    2003,
-    2004,
-    2009,
-    2010,
-    2011,
-    2012
-  ],
-  "Laurie II": [
-    2003,
-    2004,
-    2009,
-    2010,
-    2011,
-    2012,
-    2013,
-    2014,
-    2015,
-    2016,
-    2017,
-    2018,
-    2019,
-    2020,
-    2021,
-    2022,
-    2023
-  ],
-  "Lettau": [
-    2003,
-    2004,
-    2009,
-    2010,
-    2011,
-    2014,
-    2015,
-    2016,
-    2017,
-    2018,
-    2019,
-    2020,
-    2021,
-    2022,
-    2023
-  ],
-  "Limbert": [
-    2004,
-    2009,
-    2010,
-    2011,
-    2012
-  ],
-  "Linda": [
-    2003,
-    2004,
-    2009,
-    2010,
-    2011,
-    2012,
-    2015,
-    2016,
-    2017,
-    2018,
-    2019,
-    2020,
-    2021,
-    2022,
-    2023
-  ],
-  "Lorne": [
-    2009,
-    2010,
-    2011,
-    2012,
-    2013,
-    2014,
-    2015,
-    2016,
-    2017,
-    2019,
-    2020,
-    2021,
-    2022,
-    2023
-  ],
-  "Manuela": [
-    2003,
-    2004,
-    2009,
-    2010,
-    2011,
-    2012,
-    2013,
-    2014,
-    2015,
-    2016,
-    2017,
-    2018,
-    2019,
-    2020,
-    2021,
-    2022,
-    2023
-  ],
-  "Marble Point": [
-    1991,
-    1992,
-    1993,
-    1994,
-    1995,
-    1996,
-    1997,
-    1998,
-    2003,
-    2004,
-    2009,
-    2010,
-    2011,
-    2012,
-    2013,
-    2014,
-    2015,
-    2016,
-    2017,
-    2018,
-    2019,
-    2020,
-    2021,
-    2022,
-    2023
-  ],
-  "Marble Point II": [
-    2011,
-    2012,
-    2013,
-    2014,
-    2015,
-    2016,
-    2017,
-    2018,
-    2019,
-    2020,
-    2021,
-    2022,
-    2023
-  ],
-  "Margaret": [
-    2009,
-    2010,
-    2011,
-    2012,
-    2013,
-    2014,
-    2015,
-    2016,
-    2017,
-    2018,
-    2019,
-    2020,
-    2021,
-    2022,
-    2023
-  ],
-  "Marilyn": [
-    2003,
-    2004,
-    2009,
-    2010,
-    2011,
-    2012,
-    2013,
-    2014,
-    2015,
-    2016,
-    2017,
-    2018,
-    2019,
-    2020,
-    2021,
-    2022,
-    2023
-  ],
-  "Marlene": [
-    2011,
-    2012,
-    2013,
-    2014
-  ],
-  "Mary": [
-    2009,
-    2010,
-    2011,
-    2012
-  ],
-  "Minna Bluff": [
-    2003,
-    2004,
-    2009,
-    2010,
-    2011,
-    2012,
-    2013,
-    2014,
-    2015,
-    2016,
-    2017,
-    2018,
-    2019,
-    2020,
-    2022,
-    2023
-  ],
-  "Mizuho": [
-    2003,
-    2004,
-    2009,
-    2010,
-    2011,
-    2012,
-    2013,
-    2014,
-    2015,
-    2016,
-    2017,
-    2018,
-    2019,
-    2020,
-    2021,
-    2022,
-    2023
-  ],
-  "Mount Fleming": [
-    2009,
-    2010,
-    2011
-  ],
-  "Mount Siple": [
-    2003,
-    2004,
-    2009,
-    2010,
-    2011,
-    2012,
-    2013,
-    2015,
-    2016,
-    2017,
-    2018,
-    2019
-  ],
-  "Mulock": [
-    2009,
-    2010,
-    2011
-  ],
-  "Nico": [
-    2004,
-    2009,
-    2010,
-    2011,
-    2012,
-    2013,
-    2014,
-    2015,
-    2016,
-    2017,
-    2018,
-    2019,
-    2020,
-    2021,
-    2022,
-    2023
-  ],
-  "PANDA-South": [
-    2009,
-    2010,
-    2011,
-    2012,
-    2013,
-    2014,
-    2015,
-    2016,
-    2017,
-    2018,
-    2019,
-    2020,
-    2021,
-    2022,
-    2023
-  ],
-  "Pegasus North": [
-    2003,
-    2004,
-    2009,
-    2010,
-    2011,
-    2012,
-    2013,
-    2014,
-    2015,
-    2016,
-    2017
-  ],
-  "Pegasus South": [
-    2003,
-    2004,
-    2009
-  ],
-  "Penguin Point": [
-    2004
-  ],
-  "Phoenix": [
-    2017,
-    2018,
-    2019,
-    2020,
-    2021,
-    2023
-  ],
-  "Port Martin": [
-    2003,
-    2016,
-    2017
-  ],
-  "Possession Island": [
-    2003,
-    2004,
-    2009,
-    2010,
-    2011,
-    2012,
-    2013,
-    2014,
-    2015,
-    2016,
-    2017,
-    2018,
-    2019,
-    2020,
-    2021,
-    2022,
-    2023
-  ],
-  "Racer Rock": [
-    2003,
-    2004
-  ],
-  "Relay Station": [
-    2003,
-    2004,
-    2010,
-    2011,
-    2012,
-    2013,
-    2014,
-    2015,
-    2016,
-    2017,
-    2018,
-    2019,
-    2020,
-    2021,
-    2022,
-    2023
-  ],
-  "Sabrina": [
-    2009,
-    2010,
-    2011,
-    2012,
-    2013,
-    2014,
-    2015,
-    2016,
-    2017,
-    2018,
-    2019,
-    2020,
-    2021,
-    2022,
-    2023
-  ],
-  "Schwerdtfeger": [
-    2003,
-    2004,
-    2009,
-    2010,
-    2011,
-    2012,
-    2013,
-    2014,
-    2015,
-    2016,
-    2017,
-    2018,
-    2019,
-    2020,
-    2021,
-    2022,
-    2023
-  ],
-  "Siple Dome": [
-    2003,
-    2004,
-    2009,
-    2010,
-    2011,
-    2012,
-    2013,
-    2014,
-    2015,
-    2016,
-    2017,
-    2018,
-    2019,
-    2020,
-    2021,
-    2022,
-    2023
-  ],
-  "Sky Blu": [
-    2003,
-    2004,
-    2009,
-    2010,
-    2011,
-    2012
-  ],
-  "Swithinbank": [
-    2009,
-    2010,
-    2011
-  ],
-  "Theresa": [
-    2003,
-    2004,
-    2009,
-    2010,
-    2011,
-    2012,
-    2013,
-    2014,
-    2015,
-    2016,
-    2017,
-    2018,
-    2019,
-    2020,
-    2021,
-    2022
-  ],
-  "Thurston Island": [
-    2011,
-    2012,
-    2013,
-    2014,
-    2015,
-    2016,
-    2017,
-    2018,
-    2019,
-    2020,
-    2021,
-    2022,
-    2023
-  ],
-  "Tom": [
-    2011,
-    2012,
-    2013,
-    2014
-  ],
-  "Uranus Glacier": [
-    2003,
-    2004
-  ],
-  "Vito": [
-    2004,
-    2009,
-    2010,
-    2011,
-    2012,
-    2013,
-    2014,
-    2015,
-    2016,
-    2017,
-    2018,
-    2019,
-    2020,
-    2021,
-    2022,
-    2023
-  ],
-  "White Island": [
-    2012,
-    2013,
-    2014,
-    2015,
-    2016,
-    2017,
-    2018,
-    2019,
-    2020,
-    2021,
-    2022,
-    2023
-  ],
-  "Whitlock": [
-    2003,
-    2004,
-    2011,
-    2012,
-    2013,
-    2014,
-    2015,
-    2016,
-    2017,
-    2018,
-    2019,
-    2020,
-    2021,
-    2022,
-    2023
-  ],
-  "Willie Field": [
-    2003,
-    2004,
-    2009,
-    2010,
-    2012,
-    2015,
-    2016,
-    2017,
-    2018,
-    2019,
-    2020,
-    2021,
-    2022,
-    2023
-  ],
-  "Windless Bight": [
-    2003,
-    2004,
-    2009,
-    2010,
-    2011,
-    2012,
-    2015,
-    2016,
-    2017,
-    2018,
-    2019,
-    2020,
-    2021,
-    2022,
-    2023
-  ]
-}
\ No newline at end of file
diff --git a/visualizer/__init__.py b/visualizer/__init__.py
index 36a9e08..d69b914 100644
--- a/visualizer/__init__.py
+++ b/visualizer/__init__.py
@@ -1,14 +1,8 @@
-import json
-from urllib.request import urlopen
-
 from flask import Flask, g, render_template, Response
 
-from .data import res_queried_data
+from .data import get_station_years, res_queried_data
 from .parameters import get_param
 
-with open('station-years.json') as f:
-    station_years = json.load(f)
-
 app = Flask(__name__)
 
 app.jinja_env.trim_blocks = True
@@ -30,7 +24,7 @@ def plot_embed():
 
 def render_plotter(embedded):
     g.embedded = embedded
-    g.station_years = station_years
+    g.station_years = get_station_years()
     return render_template('plotter.html')
 
 
diff --git a/visualizer/data.py b/visualizer/data.py
index 9c398bc..3671900 100644
--- a/visualizer/data.py
+++ b/visualizer/data.py
@@ -19,6 +19,11 @@ api_cols_fields = ('date', 'time', 'temperature', 'pressure', 'wind_speed',
 APICols = namedtuple('APICols', api_cols_fields)
 
 
+def get_station_years():
+    with urlopen(f'{API_ROOT}/aws/list/station_years') as f:
+        return json.load(f)
+
+
 def res_queried_data():
     station_name = get_param('station', to=station_name_type)
     year = get_param('year', to=year_type)
-- 
GitLab