Skip to content

Commit

Permalink
migrate vtec_events_byugc json from php to python
Browse files Browse the repository at this point in the history
  • Loading branch information
akrherz committed Feb 15, 2020
1 parent 4859bc9 commit 4cceb37
Show file tree
Hide file tree
Showing 5 changed files with 71 additions and 53 deletions.
2 changes: 2 additions & 0 deletions config/mesonet.inc
Expand Up @@ -263,6 +263,8 @@
RewriteRule prism/([0-9\.\-]+)/([0-9\.\-]+)/([0-9\.\-]+)$ prism.py?lon=$1&lat=$2&valid=$3 [QSA]
RewriteRule stage4/([0-9\.\-]+)/([0-9\.\-]+)/([0-9\.\-]+)$ stage4.py?lon=$1&lat=$2&valid=$3 [QSA]
RewriteRule network/([0-9\.\-A-Z]+).geojson network.py?network=$1 [QSA]
# 15 Feb 2020 migration from PHP to python
RewriteRule vtec_events_byugc.php(.*) vtec_events_byugc.py$1 [QSA]
</Directory>

<Directory "/opt/iem/htdocs/kml">
Expand Down
2 changes: 1 addition & 1 deletion htdocs/json/index.php
Expand Up @@ -556,7 +556,7 @@

$services[] = Array(
"title" => "Search for Warnings by UGC Code and Date Interval",
"url" => "/json/vtec_events_byugc.php?ugc={ugc}&edate={edate}&sdate={sdate}",
"url" => "/json/vtec_events_byugc.py?ugc={ugc}&edate={edate}&sdate={sdate}",
"desc" => "Provides a json response of archived warnings valid for the
given UGC code and date interval (UTC time, end date exclusive). The date
of product issuance is used for the date filtering.",
Expand Down
50 changes: 0 additions & 50 deletions htdocs/json/vtec_events_byugc.php

This file was deleted.

66 changes: 66 additions & 0 deletions htdocs/json/vtec_events_byugc.py
@@ -0,0 +1,66 @@
""" Find VTEC events by a given UGC code. """
import json
import datetime

from paste.request import parse_formvars
from pyiem.util import get_dbconn, html_escape
from pandas.io.sql import read_sql


def run(ugc, sdate, edate):
""" Answer the request! """
pgconn = get_dbconn("postgis")

df = read_sql(
"""
SELECT
to_char(issue at time zone 'UTC',
'YYYY-MM-DDThh24:MI:SSZ') as iso_issued,
to_char(expire at time zone 'UTC',
'YYYY-MM-DDThh24:MI:SSZ') as iso_expired,
eventid, phenomena, significance, hvtec_nwsli, wfo
from warnings WHERE ugc = %s and issue > %s
and issue < %s ORDER by issue ASC
""",
pgconn,
params=(ugc, sdate, edate),
)

res = {"events": []}
for _, row in df.iterrows():
res["events"].append(
{
"issue": row["iso_issued"],
"expire": row["iso_expired"],
"eventid": row["eventid"],
"phenomena": row["phenomena"],
"hvtec_nwsli": row["hvtec_nwsli"],
"significance": row["significance"],
"wfo": row["wfo"],
}
)

return json.dumps(res)


def application(environ, start_response):
"""Answer request."""
fields = parse_formvars(environ)
ugc = fields.get("ugc", "IAC001")[:6]
sdate = datetime.datetime.strptime(
fields.get("sdate", "1986/1/1"), "%Y/%m/%d"
)
edate = datetime.datetime.strptime(
fields.get("edate", "2099/1/1"), "%Y/%m/%d"
)
cb = fields.get("callback", None)

res = run(ugc, sdate, edate)
if cb is None:
data = res
else:
data = "%s(%s)" % (html_escape(cb), res)

headers = [("Content-type", "application/json")]
start_response("200 OK", headers)
return [data.encode("ascii")]
4 changes: 2 additions & 2 deletions htdocs/vtec/search.js
Expand Up @@ -202,7 +202,7 @@ function setupUI() {
model : 'SBW',
proxy : {
type : 'ajax',
url : '/json/vtec_events_byugc.php',
url : '/json/vtec_events_byugc.py',
reader: {
type: 'json',
rootProperty: 'events'
Expand Down Expand Up @@ -348,7 +348,7 @@ function setupUI() {
text : 'Load Grid with Settings Above',
handler : function(){

eventStore.getProxy().setUrl('/json/vtec_events_byugc.php');
eventStore.getProxy().setUrl('/json/vtec_events_byugc.py');

eventStore.load({
add : false,
Expand Down

0 comments on commit 4cceb37

Please sign in to comment.