diff --git a/satpy/readers/ahi_hsd.py b/satpy/readers/ahi_hsd.py index 116f5e7d5a..92b568d87a 100644 --- a/satpy/readers/ahi_hsd.py +++ b/satpy/readers/ahi_hsd.py @@ -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. @@ -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) diff --git a/satpy/tests/reader_tests/test_ahi_hsd.py b/satpy/tests/reader_tests/test_ahi_hsd.py index 3a2495d5d0..1112ebe5a5 100644 --- a/satpy/tests/reader_tests/test_ahi_hsd.py +++ b/satpy/tests/reader_tests/test_ahi_hsd.py @@ -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', } FAKE_DATA_INFO: InfoDict = { 'blocklength': 50, @@ -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."""