From 30aff090ea027313de49a8f3a86a4d02bda57bc3 Mon Sep 17 00:00:00 2001 From: Stephan Finkensieper Date: Thu, 13 Jun 2024 07:27:37 +0000 Subject: [PATCH] Use channel name instead of data ids --- satpy/readers/utils.py | 36 +++++++++++--------------- satpy/tests/reader_tests/test_utils.py | 26 +++++-------------- 2 files changed, 22 insertions(+), 40 deletions(-) diff --git a/satpy/readers/utils.py b/satpy/readers/utils.py index f4743137b7..d66e6537b0 100644 --- a/satpy/readers/utils.py +++ b/satpy/readers/utils.py @@ -487,7 +487,6 @@ class CalibrationCoefficientSelector: .. code-block:: python from satpy.readers.utils import CalibrationCoefficientSelector - from satpy.tests.utils import make_dataid coefs = { "nominal": { @@ -508,20 +507,16 @@ class CalibrationCoefficientSelector: "gsics": ["ch2", "ch3"] } - ch1 = make_dataid(name="ch1") - ch2 = make_dataid(name="ch2") - ch3 = make_dataid(name="ch3") - 2. Query: .. code-block:: python >>> s = CalibrationCoefficientSelector(coefs, modes) - >>> s.get_coefs(ch1) + >>> s.get_coefs("ch1") "meirink_ch1" - >>> s.get_coefs(ch2) + >>> s.get_coefs("ch2") "gsics_ch2" - >>> s.get_coefs(ch3) + >>> s.get_coefs("ch3") KeyError: 'No gsics calibration coefficients for ch3' 3. Fallback to nominal for ch3: @@ -529,7 +524,7 @@ class CalibrationCoefficientSelector: .. code-block:: python >>> s = CalibrationCoefficientSelector(coefs, modes, fallback="nominal") - >>> s.get_coefs(ch3) + >>> s.get_coefs("ch3") "nominal_ch3" """ @@ -555,26 +550,25 @@ def __init__(self, coefs, modes=None, default="nominal", fallback=None): if self.fallback and self.fallback not in self.coefs: raise KeyError("No fallback coefficients") - def get_coefs(self, dataset_id): - """Get calibration coefficients for the given dataset. + def get_coefs(self, channel): + """Get calibration coefficients for the given channel. Args: - dataset_id (DataID): Desired dataset + channel (str): Channel name """ - mode = self._get_mode(dataset_id) - return self._get_coefs(dataset_id, mode) + mode = self._get_mode(channel) + return self._get_coefs(channel, mode) - def _get_coefs(self, dataset_id, mode): - ds_name = dataset_id["name"] + def _get_coefs(self, channel, mode): try: - return self.coefs[mode][ds_name] + return self.coefs[mode][channel] except KeyError: if self.fallback: - return self.coefs[self.fallback][ds_name] - raise KeyError(f"No {mode} calibration coefficients for {ds_name}") + return self.coefs[self.fallback][channel] + raise KeyError(f"No {mode} calibration coefficients for {channel}") - def _get_mode(self, dataset_id): + def _get_mode(self, channel): for mode, channels in self.modes.items(): - if dataset_id["name"] in channels: + if channel in channels: return mode return self.default diff --git a/satpy/tests/reader_tests/test_utils.py b/satpy/tests/reader_tests/test_utils.py index 18ca3d0ae0..fd400bde08 100644 --- a/satpy/tests/reader_tests/test_utils.py +++ b/satpy/tests/reader_tests/test_utils.py @@ -35,7 +35,6 @@ from satpy.readers import FSFile from satpy.readers import utils as hf from satpy.readers.utils import CalibrationCoefficientSelector -from satpy.tests.utils import make_dataid class TestHelpers(unittest.TestCase): @@ -535,17 +534,6 @@ def fixture_coefs(self): } } - @pytest.fixture(name="ch1") - def fixture_ch1(self): - """Make fake data ID.""" - return make_dataid(name="ch1") - - @pytest.fixture(name="dataset_ids") - def fixture_dataset_ids(self, ch1): - """Make fake data IDs.""" - ch2 = make_dataid(name="ch2") - return [ch1, ch2] - @pytest.mark.parametrize( ("calib_modes", "expected"), [ @@ -567,27 +555,27 @@ def fixture_dataset_ids(self, ch1): ), ] ) - def test_get_coefs(self, dataset_ids, coefs, calib_modes, expected): + def test_get_coefs(self, coefs, calib_modes, expected): """Test getting calibration coefficients.""" s = CalibrationCoefficientSelector(coefs, calib_modes) coefs = { - dataset_id["name"]: s.get_coefs(dataset_id) - for dataset_id in dataset_ids + channel: s.get_coefs(channel) + for channel in ["ch1", "ch2"] } assert coefs == expected - def test_missing_coefs(self, coefs, ch1): + def test_missing_coefs(self, coefs): """Test handling of missing coefficients.""" calib_modes = {"mode2": ["ch1"]} s = CalibrationCoefficientSelector(coefs, calib_modes) with pytest.raises(KeyError, match="No mode2 calibration *"): - s.get_coefs(ch1) + s.get_coefs("ch1") - def test_fallback_to_nominal(self, coefs, ch1): + def test_fallback_to_nominal(self, coefs): """Test falling back to nominal coefficients.""" calib_modes = {"mode2": ["ch1"]} s = CalibrationCoefficientSelector(coefs, calib_modes, fallback="nominal") - assert s.get_coefs(ch1) == "nominal_ch1" + assert s.get_coefs("ch1") == "nominal_ch1" def test_no_default_coefs(self): """Test initialization without default coefficients."""