Skip to content

Commit

Permalink
Allow a user to specify lid_desc for NWS forecasts (a special case, s…
Browse files Browse the repository at this point in the history
…ee changelog).
  • Loading branch information
chaunceygardiner committed Feb 10, 2021
1 parent 902c78f commit 41fd823
Show file tree
Hide file tree
Showing 4 changed files with 44 additions and 13 deletions.
32 changes: 22 additions & 10 deletions bin/user/forecast.py
Original file line number Diff line number Diff line change
Expand Up @@ -113,7 +113,11 @@
# to your location.
# National Weather Service location identifier
lid = MAZ014
lid = MAZ005
# lid_desc disambiguates when a lid is listed more than once in the document.
# If lid_desc is not specified, the first entry for the specified lid is used.
lid_desc = Lowell-Middlesex MA
# National Weather Service forecast office identifier
foid = BOX
Expand Down Expand Up @@ -555,7 +559,7 @@
from weewx.engine import StdService
from weewx.cheetahgenerator import SearchList

VERSION = "3.4.0b11"
VERSION = "3.4.0b12"

if weewx.__version__ < "4":
raise weewx.UnsupportedFeature(
Expand Down Expand Up @@ -1663,6 +1667,7 @@ def __init__(self, engine, config_dict):
self.url = d.get('url', NWS_DEFAULT_PFM_URL)
self.max_tries = int(d.get('max_tries', 3))
self.lid = d.get('lid', None)
self.lid_desc = d.get('lid_desc', None)
self.foid = d.get('foid', None)

errmsg = []
Expand All @@ -1676,8 +1681,8 @@ def __init__(self, engine, config_dict):
logerr('%s: forecast will not be run' % NWS_KEY)
return

loginf('%s: interval=%s max_age=%s lid=%s foid=%s' %
(NWS_KEY, self.interval, self.max_age, self.lid, self.foid))
loginf('%s: interval=%s max_age=%s lid=%s lid_desc=%s foid=%s' %
(NWS_KEY, self.interval, self.max_age, self.lid, self.lid_desc, self.foid))
self._bind()

def get_forecast(self, dummy_event):
Expand All @@ -1689,10 +1694,14 @@ def get_forecast(self, dummy_event):
return None
if self.save_raw:
self.save_raw_forecast(text, basename='nws-raw')
matrix = NWSParseForecast(text, self.lid)
matrix = NWSParseForecast(text, self.lid, self.lid_desc)
if matrix is None:
logerr('%s: no PFM found for %s in forecast from %s' %
(NWS_KEY, self.lid, self.foid))
if self.lid_desc is None:
logerr('%s: no PFM found for %s in forecast from %s' %
(NWS_KEY, self.lid, self.foid))
else:
logerr('%s: no PFM found for %s/%s in forecast from %s' %
(NWS_KEY, self.lid, self.lid_desc, self.foid))
return None
logdbg('%s: forecast matrix: %s' % (NWS_KEY, matrix))
records = NWSProcessForecast(self.foid, self.lid, matrix)
Expand Down Expand Up @@ -1762,7 +1771,7 @@ def NWSDownloadForecast(foid, url=NWS_DEFAULT_PFM_URL, max_tries=3):
logerr('%s: failed to download forecast' % NWS_KEY)
return None

def NWSExtractLocation(text, lid):
def NWSExtractLocation(text, lid, lid_desc=None):
"""Extract a single location from a US National Weather Service PFM."""

alllines = text.splitlines()
Expand All @@ -1774,17 +1783,20 @@ def NWSExtractLocation(text, lid):
elif lines is not None:
if line.startswith('$$'):
break
elif lines is not None and len(lines) == 1 and lid_desc is not None and line != lid_desc:
# It's not the reight lid_desc. Keep looking.
lines = None
else:
lines.append(line)
return lines

def NWSParseForecast(text, lid):
def NWSParseForecast(text, lid, lid_desc=None):
"""Parse a United States National Weather Service point forcast matrix.
Save it into a dictionary with per-hour elements for wind, temperature,
etc. extracted from the point forecast.
"""

lines = NWSExtractLocation(text, lid)
lines = NWSExtractLocation(text, lid, lid_desc)
if lines is None:
return None

Expand Down
13 changes: 13 additions & 0 deletions changelog
Original file line number Diff line number Diff line change
@@ -1,3 +1,16 @@
3.4.0b12
* NWS foid: BOX, lid: MAZ005 has multiple entries (actually, this is common)
the forecast plugin picks the first one found. To pick a different one,
specify the location text as the lid_desc parameter.
[Forecast]
[[NWS]]
lid = MAZ005
lid_desc = Lowell-Middlesex MA
foid = BOX
Note: This is not required and, to speicify it, you need to examine
the report returened from NWS. Most users should ignore this
change.

3.4.0b11
* Add missing freezing rain icon.
* Fix for WU tempMin/tempMax.
Expand Down
2 changes: 1 addition & 1 deletion install.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ def loader():
class ForecastInstaller(ExtensionInstaller):
def __init__(self):
super(ForecastInstaller, self).__init__(
version="3.4.0b11",
version="3.4.0b12",
name='forecast',
description='Generate and display weather and tide forecasts.',
author="Matthew Wall",
Expand Down
10 changes: 8 additions & 2 deletions readme.txt
Original file line number Diff line number Diff line change
Expand Up @@ -84,8 +84,14 @@ wee_extension --install weewx-forecast-3.4.0b11.zip

[Forecast]
[[NWS]]
lid = MAZ014 # specify a location identifier
foid = BOX # specify a forecast office identifier
lid = MAZ005 # specify a location identifier
foid = BOX # specify a forecast office identifier
lid_desc = Framingham-Middlesex MA # (optional) specify a lid description for cases
# where the lid is repeated in the forecast file
# and the user does not want the first entry for that lid.
# Just omit this entry entirely unless you have examined
# the forecast file and know the description of the repeated
# lid entry that you want to use.
[[WU]]
api_key = XXXXXXXXXXXXXXXX # specify a weather underground api_key
# A location may be specified. If it isn't, your stations lat/long
Expand Down

0 comments on commit 41fd823

Please sign in to comment.