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

Fix 'ahi_hsd' reader crashing when 'observation_timeline' was invalid #2107

Merged
merged 7 commits into from
May 11, 2022
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
12 changes: 12 additions & 0 deletions satpy/readers/ahi_hsd.py
Original file line number Diff line number Diff line change
Expand Up @@ -436,6 +436,13 @@ def nominal_end_time(self):
"""Get the nominal end time."""
return self._modify_observation_time_for_nominal(self.observation_end_time)

@staticmethod
def _is_valid_timeline(timeline):
"""Check that the `observation_timeline` value is not a fill value."""
if int(timeline[:2]) > 23:
return False
return True

def _modify_observation_time_for_nominal(self, observation_time):
"""Round observation time to a nominal time based on known observation frequency.

Expand All @@ -450,11 +457,16 @@ def _modify_observation_time_for_nominal(self, observation_time):

"""
timeline = "{:04d}".format(self.basic_info['observation_timeline'][0])
if not self._is_valid_timeline(timeline):
warnings.warn("Observation timeline is fill value, not rounding observation time.")
return observation_time

if self.observation_area == 'FLDK':
dt = 0
else:
observation_frequency_seconds = {'JP': 150, 'R3': 150, 'R4': 30, 'R5': 30}[self.observation_area[:2]]
dt = observation_frequency_seconds * (int(self.observation_area[2:]) - 1)

return observation_time.replace(
hour=int(timeline[:2]), minute=int(timeline[2:4]) + dt//60,
second=dt % 60, microsecond=0)
Expand Down
27 changes: 22 additions & 5 deletions satpy/tests/reader_tests/test_ahi_hsd.py
Original file line number Diff line number Diff line change
Expand Up @@ -37,11 +37,11 @@

FAKE_BASIC_INFO: InfoDict = {
'blocklength': 0,
'satellite': np.array(['Himawari-8']),
'observation_area': np.array(['FLDK']),
'observation_start_time': np.array([58413.12523839]),
'observation_end_time': np.array([58413.12562439]),
'observation_timeline': np.array([300]),
'satellite': 'Himawari-8',
'observation_area': 'FLDK',
'observation_start_time': 58413.12523839,
'observation_end_time': 58413.12562439,
'observation_timeline': '0300',
Comment on lines +40 to +44
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Does removing the np.array reflect what the real code is getting? At least in the NetCDF/HDF5 readers these libraries have cases where they return a scalar array of a single string and we have to do extra work to make sure they are converted to plain strings correctly. If you kept the original np.array usage and converted 300 to a string would that imitate the files well enough?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, it does: While debugging I had some print statements inside the ahi_hsd.py file and they showed that there's no numpy arrays for these values, they're all simple strings or floats.

}
FAKE_DATA_INFO: InfoDict = {
'blocklength': 50,
Expand Down Expand Up @@ -359,6 +359,23 @@ def test_blocklen_error(self, *mocks):
fh._check_fpos(fp_, fpos, 0, 'header 1')
assert len(w) > 0

def test_is_valid_time(self):
"""Test that valid times are correctly identified."""
assert AHIHSDFileHandler._is_valid_timeline(FAKE_BASIC_INFO['observation_timeline'])
assert not AHIHSDFileHandler._is_valid_timeline('65526')

def test_time_rounding(self):
"""Test rounding of the nominal time."""
mocker = mock.MagicMock()
in_date = datetime(2020, 1, 1, 12, 0, 0)

with mock.patch('satpy.readers.ahi_hsd.AHIHSDFileHandler._is_valid_timeline', mocker):
with _fake_hsd_handler() as fh:
mocker.return_value = True
assert fh._modify_observation_time_for_nominal(in_date) == datetime(2020, 1, 1, 3, 0, 0)
mocker.return_value = False
assert fh._modify_observation_time_for_nominal(in_date) == datetime(2020, 1, 1, 12, 0, 0)


class TestAHICalibration(unittest.TestCase):
"""Test case for various AHI calibration types."""
Expand Down