Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

alter calculation of offset #287

Merged
merged 4 commits into from
Jul 7, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 2 additions & 9 deletions iris_grib/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -145,11 +145,7 @@ def __init__(self, grib_message, grib_fh=None):
# Store the file pointer and message length from the current
# grib message before it's changed by calls to the grib-api.
if deferred:
# Note that, the grib-api has already read this message and
# advanced the file pointer to the end of the message.
offset = grib_fh.tell()
message_length = eccodes.codes_get_long(
grib_message, 'totalLength')
offset = eccodes.codes_get_message_offset(grib_message)

# Initialise the key-extension dictionary.
# NOTE: this attribute *must* exist, or the the __getattr__ overload
Expand All @@ -170,11 +166,8 @@ def __init__(self, grib_message, grib_fh=None):
if deferred:
# Wrap the reference to the data payload within the data proxy
# in order to support deferred data loading.
# The byte offset requires to be reset back to the first byte
# of this message. The file pointer offset is always at the end
# of the current message due to the grib-api reading the message.
proxy = GribDataProxy(shape, np.array([0.]).dtype, grib_fh.name,
offset - message_length)
offset)
self._data = as_lazy_data(proxy)
else:
self.data = _message_values(grib_message, shape)
Expand Down
7 changes: 3 additions & 4 deletions iris_grib/message.py
Original file line number Diff line number Diff line change
Expand Up @@ -59,12 +59,11 @@ def messages_from_filename(filename):
file_ref = _OpenFileRef(grib_fh)

while True:
offset = grib_fh.tell()
grib_id = eccodes.codes_new_from_file(
grib_fh, eccodes.CODES_PRODUCT_GRIB
)
grib_id = eccodes.codes_new_from_file(grib_fh,
eccodes.CODES_PRODUCT_GRIB)
if grib_id is None:
break
offset = eccodes.codes_get_message_offset(grib_id)
raw_message = _RawGribMessage(grib_id)
recreate_raw = _MessageLocation(filename, offset)
yield GribMessage(raw_message, recreate_raw, file_ref=file_ref)
Expand Down
17 changes: 12 additions & 5 deletions iris_grib/tests/unit/test_GribWrapper.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
from iris_grib import GribWrapper, _load_generate


_offset = 0
_message_length = 1000


Expand Down Expand Up @@ -50,6 +51,10 @@ def _mock_codes_get_native_type(grib_message, key):
return result


def _mock_codes_get_message_offset(grib_message):
return _offset


class Test_edition(tests.IrisGribTest):
def setUp(self):
self.patch('iris_grib.GribWrapper._confirm_in_scope')
Expand All @@ -58,7 +63,8 @@ def setUp(self):
self.patch('eccodes.codes_get_string', _mock_codes_get_string)
self.patch('eccodes.codes_get_native_type',
_mock_codes_get_native_type)
self.tell = mock.Mock(side_effect=[_message_length])
self.patch('eccodes.codes_get_message_offset',
_mock_codes_get_message_offset)

def test_not_edition_1(self):
def func(grib_message, key):
Expand All @@ -71,7 +77,7 @@ def func(grib_message, key):

def test_edition_1(self):
grib_message = 'regular_ll'
grib_fh = mock.Mock(tell=self.tell)
grib_fh = mock.Mock()
wrapper = GribWrapper(grib_message, grib_fh)
self.assertEqual(wrapper.grib_message, grib_message)

Expand Down Expand Up @@ -99,9 +105,10 @@ def setUp(self):
self.patch('eccodes.codes_get_string', _mock_codes_get_string)
self.patch('eccodes.codes_get_native_type',
_mock_codes_get_native_type)
tell_tale = np.arange(1, 5) * _message_length
self.expected = tell_tale - _message_length
self.grib_fh = mock.Mock(tell=mock.Mock(side_effect=tell_tale))
self.patch('eccodes.codes_get_message_offset',
_mock_codes_get_message_offset)
self.expected = np.atleast_1d(_offset)
self.grib_fh = mock.Mock()
self.dtype = np.float64
self.path = self.grib_fh.name
self.lookup = _mock_codes_get_long
Expand Down