From f48480ff6a014533de60fa7d13e2cce523e19a30 Mon Sep 17 00:00:00 2001 From: Martin Raspaud Date: Thu, 29 Aug 2024 15:32:05 +0200 Subject: [PATCH] Implement more robust PRT gap search --- pygac/calibration/noaa.py | 11 ++++++----- pygac/tests/test_reader.py | 23 +++++++++++++++++++---- 2 files changed, 25 insertions(+), 9 deletions(-) diff --git a/pygac/calibration/noaa.py b/pygac/calibration/noaa.py index 4f5c273..ceae9fe 100644 --- a/pygac/calibration/noaa.py +++ b/pygac/calibration/noaa.py @@ -457,14 +457,15 @@ def calibrate_thermal(counts, prt, ict, space, line_numbers, channel, cal): # Note that the prt values are the average value of the three readings from one of the four # PRTs. See reader.get_telemetry implementations. prt_threshold = 50 # empirically found and set by Abhay Devasthale - offset = 0 - for i, prt_val in enumerate(prt): + for offset in range(5): # According to the KLM Guide the fill value between PRT measurments is 0, but we search - # for the first measurment gap using the threshold. Is this on purpose? - if prt_val < prt_threshold: - offset = i + # for the first measurement gap using the threshold, because the fill value is in practice + # not always exactly 0. + if np.median(prt[(line_numbers - line_numbers[0]) % 5 == offset]) < prt_threshold: break + else: + raise IndexError("No PRT 0-index found!") # get the PRT index, iprt equals to 0 corresponds to the measurement gaps iprt = (line_numbers - line_numbers[0] + 5 - offset) % 5 diff --git a/pygac/tests/test_reader.py b/pygac/tests/test_reader.py index e11b865..0c0ea92 100644 --- a/pygac/tests/test_reader.py +++ b/pygac/tests/test_reader.py @@ -87,6 +87,7 @@ def get_header_timestamp(self): def get_telemetry(self): """Get the telemetry.""" prt = 51 * np.ones(self.along_track) # prt threshold is 50 + prt[::5] = 0 ict = 101 * np.ones((self.along_track, 3)) # ict threshold is 100 space = 101 * np.ones((self.along_track, 3)) # space threshold is 100 return prt, ict, space @@ -119,6 +120,15 @@ def get_tsm_pixels(self, channels): pass +class FakeGACReaderWithWrongPRTs(FakeGACReader): + + along_track = 10 + + def __init__(self, *args, **kwargs): + super().__init__(*args, **kwargs) + self.scans["scan_line_number"] = np.array([1, 3, 4, 5, 6, 7, 8, 9, 10, 11]) + + class TestGacReader(unittest.TestCase): """Test the common GAC Reader.""" @@ -253,6 +263,15 @@ def test_get_calibrated_channels(self): np.testing.assert_allclose(res[:, 2, 1], 10.23511303) assert reader.meta_data["calib_coeffs_version"] == "PATMOS-x, v2023" + def test_get_calibrated_channels_with_wrong_prts(self): + """Test getting calibrated channels.""" + reader = FakeGACReaderWithWrongPRTs() + res = reader.get_calibrated_channels() + + np.testing.assert_allclose(res[:, 1, 0], 8.84714652) + np.testing.assert_allclose(res[:, 2, 1], 10.23511303) + assert reader.meta_data["calib_coeffs_version"] == "PATMOS-x, v2023" + def test_to_datetime64(self): """Test conversion from (year, jday, msec) to datetime64.""" t0 = GACReader.to_datetime64(year=np.array(1970), jday=np.array(1), @@ -847,10 +866,6 @@ def test_passing_calibration_coeffs_to_reader_init_is_deprecated(): def test_passing_calibration_to_reader(): """Test passing calibration info to `get_calibrated_channels`.""" - #reader = LACPODReader(interpolate_coords=False) - #ds = reader.read_as_dataset(pod_file_with_tbm_header) - #from pygac.calibration.noaa import calibrate - method = "InvalidMethod" with pytest.raises(ValueError, match=method): reader = FakeGACReader(calibration_method=method)