From bd59ed82cf69acfd1118e674bf59bab52edb5b33 Mon Sep 17 00:00:00 2001 From: akrherz Date: Sat, 9 Dec 2023 07:38:05 -0600 Subject: [PATCH] feat: add ARTCC filter to PIREP download https://mesonet.agron.iastate.edu/request/gis/pireps.php --- cgi-bin/request/gis/pireps.py | 18 +++++++++++++----- htdocs/request/gis/pireps.php | 30 ++++++++++++++++++++++++++++++ 2 files changed, 43 insertions(+), 5 deletions(-) diff --git a/cgi-bin/request/gis/pireps.py b/cgi-bin/request/gis/pireps.py index ad1ccbcd2a..eaf8f4c873 100644 --- a/cgi-bin/request/gis/pireps.py +++ b/cgi-bin/request/gis/pireps.py @@ -6,7 +6,7 @@ import shapefile from pyiem.exceptions import IncompleteWebRequest from pyiem.util import get_dbconn -from pyiem.webutil import iemapp +from pyiem.webutil import ensure_list, iemapp def get_context(environ): @@ -15,12 +15,15 @@ def get_context(environ): form["fmt"] = environ.get("fmt", "shp") form["sts"] = environ["sts"] form["ets"] = environ["ets"] - + form["artcc"] = ensure_list(environ, "artcc") + if "_ALL" in form["artcc"]: + form["artcc"] = [] return form def run(ctx, start_response): """Go run!""" + artcc_sql = "" if not ctx["artcc"] else " artcc = ANY(%s) and " pgconn = get_dbconn("postgis") cursor = pgconn.cursor() @@ -46,17 +49,20 @@ def run(ctx, start_response): substr(trim(substring(replace(report, ',', ' '), '/TB([^/]*)/')), 0, 255) as turb, artcc, ST_y(geom::geometry) as lat, ST_x(geom::geometry) as lon - from pireps WHERE {spatialsql} + from pireps WHERE {spatialsql} {artcc_sql} valid >= %s and valid < %s ORDER by valid ASC """ - args = ( + args = [ ctx["sts"], ctx["ets"], - ) + ] + if ctx["artcc"]: + args.insert(0, ctx["artcc"]) cursor.execute(sql, args) if cursor.rowcount == 0: start_response("200 OK", [("Content-type", "text/plain")]) + pgconn.close() return b"ERROR: no results found for your query" fn = f"pireps_{ctx['sts']:%Y%m%d%H%M}_{ctx['ets']:%Y%m%d%H%M}" @@ -73,6 +79,7 @@ def run(ctx, start_response): ) for row in cursor: sio.write(",".join([str(s) for s in row]) + "\n") + pgconn.close() return sio.getvalue().encode("ascii", "ignore") shpio = BytesIO() @@ -108,6 +115,7 @@ def run(ctx, start_response): ("Content-type", "application/octet-stream"), ("Content-Disposition", f"attachment; filename={fn}.zip"), ] + pgconn.close() start_response("200 OK", headers) return zio.getvalue() diff --git a/htdocs/request/gis/pireps.php b/htdocs/request/gis/pireps.php index 7b9d69dcec..ed4df858fd 100644 --- a/htdocs/request/gis/pireps.php +++ b/htdocs/request/gis/pireps.php @@ -7,6 +7,32 @@ $t = new MyView(); $t->title = "Download PIREPs"; +$artcc = Array( + "ZAB"=> "[ZAB] ALBUQUERQUE", + "ZAN"=> "[ZAN] ANCHORAGE", + "ZAU"=> "[ZAU] CHICAGO", + "ZBW"=> "[ZBW] BOSTON ", + "ZDC"=> "[ZDC] WASHINGTON", + "ZDV"=> "[ZDV] DENVER", + "ZFW"=> "[ZFW] FORT WORTH", + "ZHU"=> "[ZHU] HOUSTON", + "ZID"=> "[ZID] INDIANAPOLIS", + "ZJX"=> "[ZJX] JACKSONVILLE", + "ZKC"=> "[ZKC] KANSAS CITY", + "ZLA"=> "[ZLA] LOS ANGELES", + "ZLC"=> "[ZLC] SALT LAKE CITY", + "ZMA"=> "[ZMA] MIAMI", + "ZME"=> "[ZME] MEMPHIS", + "ZMP"=> "[ZMP] MINNEAPOLIS", + "ZNY"=> "[ZNY] NEW YORK", + "ZOA"=> "[ZOA] OAKLAND", + "ZOB"=> "[ZOB] CLEVELAND ", + "ZSE"=> "[ZSE] SEATTLE", + "ZTL"=> "[ZTL] ATLANTA", + "_ALL" => "No ARTCC Filter", +); +$artccSelect = make_select("artcc", "_ALL", $artcc); + $content = <<