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

Refactor HRIT/LRIT format reader. #2087

Merged
merged 1 commit into from
Apr 14, 2022
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
47 changes: 28 additions & 19 deletions satpy/readers/hrit_base.py
Original file line number Diff line number Diff line change
Expand Up @@ -318,6 +318,33 @@ def get_area_def(self, dsid):
self.area = area
return area

def _memmap_data(self, shape, dtype):
# For reading the image data, unzip_context is faster than generic_open
with utils.unzip_context(self.filename) as fn:
return np.memmap(fn, mode='r',
offset=self.mda['total_header_length'],
dtype=dtype,
shape=shape)

def _read_file_or_file_like(self, shape, dtype):
# filename is likely to be a file-like object, already in memory
with utils.generic_open(self.filename, mode="rb") as fp:
no_elements = np.prod(shape)
fp.seek(self.mda['total_header_length'])
return np.frombuffer(
fp.read(np.dtype(dtype).itemsize * no_elements),
dtype=np.dtype(dtype),
count=no_elements.item()
).reshape(shape)

def _read_or_memmap_data(self, shape, dtype):
# check, if 'filename' is a file on disk,
# or a file like obj, possibly residing already in memory
try:
return self._memmap_data(shape, dtype)
except (FileNotFoundError, AttributeError):
return self._read_file_or_file_like(shape, dtype)

def read_band(self, key, info):
"""Read the data."""
shape = int(np.ceil(self.mda['data_field_length'] / 8.))
Expand All @@ -327,25 +354,7 @@ def read_band(self, key, info):
elif self.mda['number_of_bits_per_pixel'] in [8, 10]:
dtype = np.uint8
shape = (shape, )
# check, if 'filename' is a file on disk,
# or a file like obj, possibly residing already in memory
try:
# For reading the image data, unzip_context is faster than generic_open
with utils.unzip_context(self.filename) as fn:
data = np.memmap(fn, mode='r',
offset=self.mda['total_header_length'],
dtype=dtype,
shape=shape)
except (FileNotFoundError, AttributeError):
# filename is likely to be a file-like object, already in memory
with utils.generic_open(self.filename, mode="rb") as fp:
no_elements = np.prod(shape)
fp.seek(self.mda['total_header_length'])
data = np.frombuffer(
fp.read(np.dtype(dtype).itemsize * no_elements),
dtype=np.dtype(dtype),
count=no_elements.item()
).reshape(shape)
data = self._read_or_memmap_data(shape, dtype)
data = da.from_array(data, chunks=shape[0])
if self.mda['number_of_bits_per_pixel'] == 10:
data = dec10216(data)
Expand Down