Skip to content

Commit

Permalink
Add support for gzipped HRIT files
Browse files Browse the repository at this point in the history
  • Loading branch information
sfinkens committed Oct 10, 2022
1 parent 7a30eb8 commit c993c6c
Show file tree
Hide file tree
Showing 3 changed files with 39 additions and 6 deletions.
15 changes: 9 additions & 6 deletions satpy/readers/hrit_base.py
Expand Up @@ -356,7 +356,8 @@ def __init__(self, filename, mda):
self.bpp = mda['number_of_bits_per_pixel']
self.compressed = mda['compression_flag_for_data'] == 1
self.offset = mda['total_header_length']
self.zipped = os.fspath(filename).endswith('.bz2')
self.bzipped = os.fspath(filename).endswith('.bz2')
self.gzipped = os.fspath(filename).endswith('.gz')

def read_data(self):
"""Read the data."""
Expand All @@ -367,12 +368,14 @@ def read_data(self):
return data

def _read_data_from_file(self):
# check, if 'filename' is a file on disk,
# or a file like obj, possibly residing already in memory
try:
if self._should_read_from_disk():
return self._read_data_from_disk()
except (FileNotFoundError, AttributeError):
return self._read_file_like()
return self._read_file_like()

def _should_read_from_disk(self):
is_bz2_or_wavelet = self.bzipped or self.compressed
is_uncompressed = not (is_bz2_or_wavelet or self.gzipped)
return is_bz2_or_wavelet or is_uncompressed

def _read_data_from_disk(self):
# For reading the image data, unzip_context is faster than generic_open
Expand Down
3 changes: 3 additions & 0 deletions satpy/readers/utils.py
Expand Up @@ -18,6 +18,7 @@
"""Helper functions for satpy readers."""

import bz2
import gzip
import logging
import os
import shutil
Expand Down Expand Up @@ -288,6 +289,8 @@ def generic_open(filename, *args, **kwargs):
"""
if os.fspath(filename).endswith('.bz2'):
fp = bz2.open(filename, *args, **kwargs)
elif os.fspath(filename).endswith(".gz"):
fp = gzip.open(filename)
else:
try:
fp = filename.open(*args, **kwargs)
Expand Down
27 changes: 27 additions & 0 deletions satpy/tests/reader_tests/test_hrit_base.py
Expand Up @@ -18,6 +18,7 @@
"""The HRIT base reader tests package."""

import bz2
import gzip
import os
import unittest
from datetime import datetime
Expand Down Expand Up @@ -156,6 +157,14 @@ def stub_bzipped_hrit_file(tmp_path):
return filename


@pytest.fixture
def stub_gzipped_hrit_file(tmp_path):
"""Create a stub gzipped hrit file."""
filename = tmp_path / "some_hrit_file.gz"
create_stub_hrit(filename, open_fun=gzip.open)
return filename


@pytest.fixture
def stub_compressed_hrit_file(tmp_path):
"""Create a stub compressed hrit file."""
Expand Down Expand Up @@ -244,6 +253,24 @@ def test_read_band_bzipped2_filepath(self, stub_bzipped_hrit_file):
res = self.reader.read_band('VIS006', None)
assert res.compute().shape == (464, 3712)

def test_read_band_gzipped_filepath(self, stub_gzipped_hrit_file):
"""Test reading a single band from a gzipped file."""
self.reader.filename = stub_gzipped_hrit_file

res = self.reader.read_band('VIS006', None)
assert res.compute().shape == (464, 3712)

def test_read_band_gzip_stream(self, stub_gzipped_hrit_file):
"""Test reading a single band from a gzip stream."""
import fsspec
filename = stub_gzipped_hrit_file

fs_file = fsspec.open(filename)
self.reader.filename = FSFile(fs_file)

res = self.reader.read_band('VIS006', None)
assert res.compute().shape == (464, 3712)


def fake_decompress(infile, outdir='.'):
"""Fake decompression."""
Expand Down

0 comments on commit c993c6c

Please sign in to comment.