Skip to content

Commit

Permalink
catch metar visibility with zero denominator
Browse files Browse the repository at this point in the history
  • Loading branch information
akrherz committed Sep 8, 2022
1 parent 72a07b9 commit d5c7df9
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 2 deletions.
2 changes: 2 additions & 0 deletions src/metpy/io/metar.py
Original file line number Diff line number Diff line change
Expand Up @@ -238,6 +238,8 @@ def parse_metar(metar_text, year, month, station_metadata=station_info):
# Handle fraction regardless
if '/' in vis_str:
num, denom = vis_str.split('/', maxsplit=1)
if int(denom) == 0:
raise ValueError('Visibility denominator is 0.')
visibility += int(num) / int(denom)
else: # Should be getting all cases of whole number without fraction
visibility += int(vis_str)
Expand Down
11 changes: 9 additions & 2 deletions tests/io/test_metar.py
Original file line number Diff line number Diff line change
Expand Up @@ -183,12 +183,19 @@
Metar('ORER', 36.22, 43.97, 409, datetime(2017, 5, 17, 20, 0), 300, 6.0, np.nan,
400, 'FG', np.nan, np.nan, 'VV', np.nan, np.nan, np.nan, np.nan, np.nan,
np.nan, np.nan, 8, 12, 12, units.Quantity(1013, 'hPa').m_as('inHg'), 45, 0, 0,
'NOSIG'))],
'NOSIG')),
# Invalid Visibility Unidata/Metpy#2652
('KGYR 072147Z 12006KT 1/0SM FEW100 SCT250 41/14 A2992',
Metar('KGYR', 33.42, -112.37, 295, datetime(2017, 5, 7, 21, 47), 120, 6.0, np.nan,
np.nan, np.nan, np.nan, np.nan, 'FEW', 10000, 'SCT', 25000, np.nan, np.nan,
np.nan, np.nan, 4, 41, 14, 29.92, 0, 0, 0,
''))],
ids=['missing station', 'BKN', 'FEW', 'current weather', 'smoke', 'CAVOK', 'vis fraction',
'missing temps', 'missing data', 'vertical vis', 'missing vertical vis', 'BCFG',
'-DZ', 'sky cover CB', '5 sky levels', '-FZUP', 'VV group', 'COR placement',
'M1/4SM vis', 'variable vis', 'runway vis', 'odd COR', 'IC', 'NSW',
'variable vis no dir', 'swapped wind and vis', 'space in wx code', 'truncated VV'])
'variable vis no dir', 'swapped wind and vis', 'space in wx code', 'truncated VV',
'vis div zero'])
def test_metar_parser(metar, truth):
"""Test parsing individual METARs."""
assert parse_metar(metar, 2017, 5) == truth
Expand Down

0 comments on commit d5c7df9

Please sign in to comment.