Skip to content

Commit

Permalink
Merge pull request #944 from akrherz/watch
Browse files Browse the repository at this point in the history
fix: parsing of watch replacement number
  • Loading branch information
akrherz authored Aug 5, 2024
2 parents 1d832e7 + 5c014a6 commit 2f67ee9
Show file tree
Hide file tree
Showing 4 changed files with 46 additions and 11 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ memcache is short circuited.

### Bug Fixes

- Fix and refactor SPC `saw` parsing of watch replacement number.
- Pop kwargs `fig` on `MapPlot`.
- Use less confusing landing page at WPC for ERO.
- Use `round` instead of `int` to compute zonal stats grid navigation.
Expand Down
18 changes: 18 additions & 0 deletions data/product_examples/SAW/SAW6.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
392
WWUS30 KWNS 050955
SAW6
SPC AWW 050955
WW 596 TORNADO FL GA CW 050955Z - 052000Z
AXIS..50 STATUTE MILES EAST AND WEST OF LINE..
50ESE PIE/SAINT PETERSBURG FL/ - 50N SSI/BRUNSWICK GA/
..AVIATION COORDS.. 45NM E/W /36ENE SRQ - 24SW SAV/
WIND GUSTS..60 KNOTS.
MAX TOPS TO 450.MEAN STORM MOTION VECTOR 18035.

REPLACES WW 595..FL GA CW

LAT...LON 27648274 31878230 31878060 27648111

THIS IS AN APPROXIMATION TO THE WATCH AREA. FOR A
COMPLETE DEPICTION OF THE WATCH SEE WOUS64 KWNS
FOR WOU6.
23 changes: 14 additions & 9 deletions src/pyiem/nws/products/saw.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@

import datetime
import re
from typing import Optional

from shapely.geometry import MultiPolygon
from shapely.geometry import Polygon as ShapelyPolygon
Expand Down Expand Up @@ -42,8 +43,16 @@ def __init__(self, text, utcnow=None):
self.ww_num = self.find_ww_num()
(self.sts, self.ets) = self.find_time()
self.ww_type = self.find_ww_type()
self.replaces_num = self.find_replaces()
self.affected_wfos = []

def find_replaces(self) -> Optional[int]:
"""Figure out what this watch replaces."""
tokens = REPLACES_RE.findall(self.unixtext)
if not tokens:
return None
return int(tokens[0])

def find_action(self):
"""Figure out if this is an issuance or cancells statement
Expand Down Expand Up @@ -114,12 +123,11 @@ def sql(self, txn):
)
txn.execute(sql, args)
# Is this a replacement?
if REPLACES_RE.findall(self.unixtext):
rnum = REPLACES_RE.findall(self.unixtext)[0][0]
if self.replaces_num is not None:
txn.execute(
"UPDATE watches SET expired = %s "
"WHERE num = %s and extract(year from expired) = %s",
(self.valid, rnum, self.sts.year),
(self.valid, self.replaces_num, self.sts.year),
)
elif self.action == self.CANCELS:
for table in ("watches", "watches_current"):
Expand Down Expand Up @@ -288,12 +296,9 @@ def get_jabbers(self, uri, _uri2=None, **kwargs):
f"Watch {self.ww_num}</a> {pds_extra}"
f"till {self.ets:%-H:%M} UTC"
)
if REPLACES_RE.findall(self.unixtext):
rtext = (
f"WW {REPLACES_RE.findall(self.unixtext)[0][0].strip()} "
)
plain += ", new watch replaces " + rtext
html += ", new watch replaces " + rtext
if self.replaces_num is not None:
plain += f", new watch replaces WW {self.replaces_num}"
html += f", new watch replaces WW {self.replaces_num}"

plain2 = f"{plain} {url}"
plain2 = " ".join(plain2.split())
Expand Down
15 changes: 13 additions & 2 deletions tests/nws/products/test_saw.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,16 @@
from pyiem.util import get_test_file, utc


def test_240805_bad_time():
"""Test what we parse this as."""
utcnow = utc(2024, 8, 5, 11)
prod = parser(get_test_file("SAW/SAW6.txt"), utcnow=utcnow)
assert prod.ww_num == 596
assert prod.sts == utc(2024, 8, 5, 9, 55)
assert prod.ets == utc(2024, 8, 5, 20, 0)
assert prod.replaces_num == 595


def test_220502_jabber():
"""Test that we can generate fancy messages."""
utcnow = utc(2021, 7, 29, 0)
Expand Down Expand Up @@ -80,14 +90,15 @@ def test_181231_linkisok():
@pytest.mark.parametrize("database", ["postgis"])
def test_replacement(dbcursor):
"""Can we do replacements?"""
utcnow = utc(2017, 8, 21, 9, 17)
utcnow = utc(2017, 4, 21, 9, 17)
prod = sawparser(get_test_file("SAW/SAW-replaces.txt"), utcnow=utcnow)
assert prod.replaces_num == 152
prod.sql(dbcursor)
jmsgs = prod.get_jabbers("")
assert len(jmsgs) == 1
ans = (
"SPC issues Severe Thunderstorm Watch"
" 153 till 17:00Z, new watch replaces WW 1 "
" 153 till 17:00Z, new watch replaces WW 152 "
"https://www.spc.noaa.gov/products/watch/2017/ww0153.html"
)
assert jmsgs[0][0] == ans
Expand Down

0 comments on commit 2f67ee9

Please sign in to comment.