Skip to content

Commit

Permalink
BUG: Handle message 18 missing VCP info (Fixes #3413)
Browse files Browse the repository at this point in the history
At some point the fixed VCP info in message type 18 was converted to a
spare field. This still parses fine when filled with 0's, but apparently
some sites are now sending out some random bytes in this location, which
can break our parser.
  • Loading branch information
dopplershift committed Feb 27, 2024
1 parent 93881c6 commit 7735476
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 6 deletions.
17 changes: 11 additions & 6 deletions src/metpy/io/nexrad.py
Original file line number Diff line number Diff line change
Expand Up @@ -545,12 +545,17 @@ def _decode_msg18(self, msg_hdr):
attr = f'VCPAT{num}'
dat = self.rda[attr]
vcp_hdr = self.vcp_fmt.unpack_from(dat, 0)
off = self.vcp_fmt.size
els = []
for _ in range(vcp_hdr.num_el_cuts):
els.append(self.vcp_el_fmt.unpack_from(dat, off))
off += self.vcp_el_fmt.size
self.rda[attr] = vcp_hdr._replace(els=els)
# At some point these got changed to spares, so only try to parse the rest if
# it looks like the right data.
if vcp_hdr.num == num and 0 < 2 * vcp_hdr.size_hw <= len(dat):
off = self.vcp_fmt.size
els = []
for _ in range(vcp_hdr.num_el_cuts):
els.append(self.vcp_el_fmt.unpack_from(dat, off))
off += self.vcp_el_fmt.size
self.rda[attr] = vcp_hdr._replace(els=els)
else: # Otherwise this is just spare and we should dump
self.rda.pop(attr)

msg31_data_hdr_fmt = NamedStruct([('stid', '4s'), ('time_ms', 'L'),
('date', 'H'), ('az_num', 'H'),
Expand Down
1 change: 1 addition & 0 deletions src/metpy/static-data-manifest.txt
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ CAM_test.nc ab62e6dca907c34eb44b842c67eb3eb817127306feb3524fa6d83670b628727c
GFS_global.nc feae73f72340ee9e8b04fca92f182638a99316ad7262152809b7ccb9b6691e10
GFS_test.nc b69ae13179428667f6bc14dada1d5f9af4d3737b2b76a79a6001664e1525df3c
HI-REGIONAL_4km_3.9_20160616_1715.gini 30896dda51c9f933027d8086f0a86543efce12ea90a52981eccbce2e0ce1529e
KJKL_20240227_102059 9344358cb53f2f9449b5a984d40a7dee8048fe6ecbb7fe219ab2420f8ee18776
KICX_20170712_1458 94bd4f795832f056f7489a5562acf76de9a9cab1694549562cc3154abb22527c
KLTX20050329_100015.gz cad6ad8df707ad63c9ddb7306de869186241dd821517daf21ae0a80f4ce0a58d
KTLX19990503_235621.gz 7a097251bb7a15dbcdec75812812e41a86c5eb9850f55c3d91d120c2c61e046e
Expand Down
6 changes: 6 additions & 0 deletions tests/io/test_nexrad.py
Original file line number Diff line number Diff line change
Expand Up @@ -109,6 +109,12 @@ def test_msg15():
assert f.clutter_filter_map['datetime'] == datetime(2013, 5, 19, 5, 15, 0, 0)


def test_msg18_novcps():
"""Check handling of message type 18 with VCP info now spares does not crash."""
f = Level2File(get_test_data('KJKL_20240227_102059', as_file_obj=False))

Check warning

Code scanning / CodeQL

File is not always closed Warning test

File is opened but is not closed.
assert 'VCPAT11' not in f.rda


def test_single_chunk(caplog):
"""Check that Level2File copes with reading a file containing a single chunk."""
# Need to override the test level set above
Expand Down

0 comments on commit 7735476

Please sign in to comment.