Skip to content

Commit

Permalink
Merge pull request #3117 from mgrover1/add-xdr-lib-submodule
Browse files Browse the repository at this point in the history
ENH: Move xdrlib inside metpy
  • Loading branch information
dcamron authored Sep 13, 2023
2 parents 04a27fe + d2a3abb commit 72095f1
Showing 1 changed file with 32 additions and 9 deletions.
41 changes: 32 additions & 9 deletions src/metpy/io/nexrad.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@
import pathlib
import re
import struct
from xdrlib import Unpacker

import numpy as np
from scipy.constants import day, milli
Expand Down Expand Up @@ -2301,11 +2300,37 @@ def _read_trends(self):
0xba07: _unpack_packet_raster_data}


class Level3XDRParser(Unpacker):
class Level3XDRParser:
"""Handle XDR-formatted Level 3 NEXRAD products."""

def __init__(self, data):
"""Initialize the parser with a buffer for the bytes."""
self._buf = IOBuffer(data)

def unpack_uint(self):
"""Unpack a 4-byte unsigned integer."""
return self._buf.read_int(4, 'big', False)

def unpack_int(self):
"""Unpack a 4-byte signed integer."""
return self._buf.read_int(4, 'big', True)

def unpack_float(self):
"""Unpack a 4-byte floating-point value."""
return self._buf.read_binary(1, '>f')[0]

def unpack_string(self):
"""Unpack a string."""
n = self.unpack_uint()
return self._buf.read_ascii((n + 3) // 4 * 4)[:n]

def unpack_int_array(self):
"""Unpack an int array."""
n = self.unpack_uint()
return self._buf.read_binary(n, '>l')

def __call__(self, code):
"""Perform the actual unpacking."""
"""Perform the actual product unpacking."""
xdr = OrderedDict()

if code == 28:
Expand All @@ -2314,12 +2339,10 @@ def __call__(self, code):
log.warning('XDR: code %d not implemented', code)

# Check that we got it all
self.done()
return xdr
if not self._buf.at_end():
log.warning('Data remains in XDR buffer.')

def unpack_string(self):
"""Unpack the internal data as a string."""
return Unpacker.unpack_string(self).decode('ascii')
return xdr

def _unpack_prod_desc(self):
xdr = OrderedDict()
Expand Down Expand Up @@ -2415,7 +2438,7 @@ def _unpack_radial(self):
width=self.unpack_float(),
num_bins=self.unpack_int(),
attributes=self.unpack_string(),
data=self.unpack_array(self.unpack_int)))
data=self.unpack_int_array()))
return ret._replace(radials=rads)

text_fmt = namedtuple('TextComponent', ['parameters', 'text'])
Expand Down

0 comments on commit 72095f1

Please sign in to comment.