Skip to content

Commit

Permalink
Use channel name instead of data ids
Browse files Browse the repository at this point in the history
  • Loading branch information
sfinkens committed Jun 13, 2024
1 parent dca1a18 commit 30aff09
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 40 deletions.
36 changes: 15 additions & 21 deletions satpy/readers/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -487,7 +487,6 @@ class CalibrationCoefficientSelector:
.. code-block:: python
from satpy.readers.utils import CalibrationCoefficientSelector
from satpy.tests.utils import make_dataid
coefs = {
"nominal": {
Expand All @@ -508,28 +507,24 @@ 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:
.. code-block:: python
>>> s = CalibrationCoefficientSelector(coefs, modes, fallback="nominal")
>>> s.get_coefs(ch3)
>>> s.get_coefs("ch3")
"nominal_ch3"
"""

Expand All @@ -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
26 changes: 7 additions & 19 deletions satpy/tests/reader_tests/test_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -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):
Expand Down Expand Up @@ -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"),
[
Expand All @@ -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."""
Expand Down

0 comments on commit 30aff09

Please sign in to comment.