Skip to content

Commit

Permalink
Refactor code into smaller functions.
Browse files Browse the repository at this point in the history
  • Loading branch information
sbrodehl committed Apr 13, 2022
1 parent dd77305 commit c1d469b
Showing 1 changed file with 28 additions and 19 deletions.
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

0 comments on commit c1d469b

Please sign in to comment.