Skip to content

Commit

Permalink
Implement more robust PRT gap search
Browse files Browse the repository at this point in the history
  • Loading branch information
mraspaud committed Aug 29, 2024
1 parent 6289b1a commit f48480f
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 9 deletions.
11 changes: 6 additions & 5 deletions pygac/calibration/noaa.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
23 changes: 19 additions & 4 deletions pygac/tests/test_reader.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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."""

Expand Down Expand Up @@ -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),
Expand Down Expand Up @@ -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)
Expand Down

0 comments on commit f48480f

Please sign in to comment.