Skip to content

Commit

Permalink
fix: NWS Product Signature logic
Browse files Browse the repository at this point in the history
closes #865
  • Loading branch information
akrherz committed Mar 8, 2024
1 parent 08fbe29 commit 3e25e56
Show file tree
Hide file tree
Showing 4 changed files with 32 additions and 9 deletions.
3 changes: 3 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,9 @@ web services to get the raw text. (#857)

### Bug Fixes

- Improve NWS Text Product signature logic so to more generally match what
looks like a signature (#865).

## **1.19.0** (4 Mar 2024)

### API Changes
Expand Down
16 changes: 12 additions & 4 deletions src/pyiem/nws/product.py
Original file line number Diff line number Diff line change
Expand Up @@ -799,10 +799,18 @@ def get_jabbers(self, uri, _uri2=None):
return [(plain, html, xtra)]

def get_signature(self):
"""Find the signature at the bottom of the page"""
return " ".join(
self.segments[-1].unixtext.replace("\n", " ").strip().split()
)
"""Return a signature that matches basic constraints (1 word)."""
lines = [x.strip() for x in self.unixtext.split("\n") if x.strip()]
res = None
for line in lines[::-1][:3]:
if line in ["$$", "&&", "$", "&"]:
continue
if line.find(" ") > -1 or line.find(".") > -1:
break
if 0 < len(line) < 25:
res = line
break
return res

def parse_segments(self):
"""Split the product by its $$"""
Expand Down
6 changes: 1 addition & 5 deletions src/pyiem/nws/products/_vtec_util.py
Original file line number Diff line number Diff line change
Expand Up @@ -370,10 +370,6 @@ def _do_sql_vtec_new(prod, txn, warning_table, segment, vtec):
if vtec.endts is None:
ets = bts + DEFAULT_EXPIRE_DELTA

fcster = prod.get_signature()
if fcster is not None:
fcster = fcster[:24]

# For each UGC code in this segment, we create a database entry
for ugc in segment.ugcs:
# Check to see if we have entries already for this UGC
Expand Down Expand Up @@ -438,7 +434,7 @@ def _do_sql_vtec_new(prod, txn, warning_table, segment, vtec):
vtec.office,
vtec.etn,
vtec.action,
fcster,
prod.get_signature(),
str(ugc),
vtec.phenomena,
vtec.significance,
Expand Down
16 changes: 16 additions & 0 deletions tests/nws/test_product.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,22 @@
from pyiem.wmo import WMO_RE


def test_gh865_fcster_none():
"""Test that we don't use the generic signature on this product."""
data = get_test_file("NPW/NPWDMX.txt")
prod = productparser(data)
assert prod.get_signature() is None


def test_gh865_fcster_scenarios():
"""Test that we handle all kinds of things."""
data = get_test_file("TORILX.txt")
prod = productparser(data)
assert prod.get_signature() == "MILLER"
prod = productparser(data.replace("MILLER", ""))
assert prod.get_signature() is None


def test_ahdnwc():
"""Test the jabber result we get from this product."""
data = get_test_file("AHD/AHDNWC.txt")
Expand Down

0 comments on commit 3e25e56

Please sign in to comment.