From cdf25d61251ad6f61268b8a6c86bd2d1e38d33a8 Mon Sep 17 00:00:00 2001 From: David Hoese Date: Thu, 6 Oct 2022 15:25:11 -0500 Subject: [PATCH 1/2] Refactor rayleigh test --- pyspectral/tests/test_rayleigh.py | 27 +++++++++++---------------- 1 file changed, 11 insertions(+), 16 deletions(-) diff --git a/pyspectral/tests/test_rayleigh.py b/pyspectral/tests/test_rayleigh.py index 9dd5bd22..0e44f266 100644 --- a/pyspectral/tests/test_rayleigh.py +++ b/pyspectral/tests/test_rayleigh.py @@ -315,28 +315,23 @@ def test_get_reflectance_redband_outside_clip(self, fake_lut_hdf5): np.testing.assert_allclose(refl_corr2, refl_corr3) @patch('pyspectral.rayleigh.da', None) - def test_get_reflectance(self, fake_lut_hdf5): + @pytest.mark.parametrize( + ("sun_zenith", "sat_zenith", "azidiff", "redband_refl", "exp_result"), + [ + (np.array([67., 32.]), np.array([45., 18.]), np.array([150., 110.]), np.array([14., 5.]), + TEST_RAYLEIGH_RESULT1), + (np.array([60., 20.]), np.array([49., 26.]), np.array([140., 130.]), np.array([12., 8.]), + TEST_RAYLEIGH_RESULT2), + ] + ) + def test_get_reflectance(self, fake_lut_hdf5, sun_zenith, sat_zenith, azidiff, redband_refl, exp_result): """Test getting the reflectance correction.""" - sun_zenith = np.array([67., 32.]) - sat_zenith = np.array([45., 18.]) - azidiff = np.array([150., 110.]) - redband_refl = np.array([14., 5.]) rayl = _create_rayleigh() with mocked_rsr(): refl_corr = rayl.get_reflectance( sun_zenith, sat_zenith, azidiff, 'ch3', redband_refl) - np.testing.assert_allclose(refl_corr, TEST_RAYLEIGH_RESULT1) - - sun_zenith = np.array([60., 20.]) - sat_zenith = np.array([49., 26.]) - azidiff = np.array([140., 130.]) - redband_refl = np.array([12., 8.]) - with mocked_rsr(): - refl_corr = rayl.get_reflectance( - sun_zenith, sat_zenith, azidiff, 'ch3', redband_refl) - assert isinstance(refl_corr, np.ndarray) - np.testing.assert_allclose(refl_corr, TEST_RAYLEIGH_RESULT2) + np.testing.assert_allclose(refl_corr, exp_result) @patch('pyspectral.rayleigh.da', None) def test_get_reflectance_no_rsr(self, fake_lut_hdf5): From 538df91789a2ce91e291ddba87b61a16782413a4 Mon Sep 17 00:00:00 2001 From: David Hoese Date: Thu, 6 Oct 2022 15:26:56 -0500 Subject: [PATCH 2/2] Revert np.nan_to_num usage on rayleigh correction array Use a correction of NaN instead of a correction of 0 --- pyspectral/rayleigh.py | 2 +- pyspectral/tests/test_rayleigh.py | 3 +++ 2 files changed, 4 insertions(+), 1 deletion(-) diff --git a/pyspectral/rayleigh.py b/pyspectral/rayleigh.py index 826f45e9..3fa67603 100644 --- a/pyspectral/rayleigh.py +++ b/pyspectral/rayleigh.py @@ -267,7 +267,7 @@ def get_reflectance(self, sun_zenith, sat_zenith, azidiff, dtype=res.dtype, chunks=getattr(res, "chunks", None)) - res = np.clip(np.nan_to_num(res), 0, 100) + res = np.clip(res, 0, 100) if compute: res = res.compute() return res diff --git a/pyspectral/tests/test_rayleigh.py b/pyspectral/tests/test_rayleigh.py index 0e44f266..8630b365 100644 --- a/pyspectral/tests/test_rayleigh.py +++ b/pyspectral/tests/test_rayleigh.py @@ -44,6 +44,7 @@ TEST_RAYLEIGH_RESULT2 = np.array([9.653559, 8.464997], dtype='float32') TEST_RAYLEIGH_RESULT3 = np.array([5.488735, 8.533125], dtype='float32') TEST_RAYLEIGH_RESULT4 = np.array([0.0, 8.64748], dtype='float32') +TEST_RAYLEIGH_RESULT5 = np.array([9.653559, np.nan], dtype='float32') TEST_RAYLEIGH_RESULT_R1 = np.array([16.66666667, 20.83333333, 25.], dtype='float32') TEST_RAYLEIGH_RESULT_R2 = np.array([0., 6.25, 12.5], dtype='float32') @@ -322,6 +323,8 @@ def test_get_reflectance_redband_outside_clip(self, fake_lut_hdf5): TEST_RAYLEIGH_RESULT1), (np.array([60., 20.]), np.array([49., 26.]), np.array([140., 130.]), np.array([12., 8.]), TEST_RAYLEIGH_RESULT2), + (np.array([60., 20.]), np.array([49., 26.]), np.array([140., 130.]), np.array([12., np.nan]), + TEST_RAYLEIGH_RESULT5), ] ) def test_get_reflectance(self, fake_lut_hdf5, sun_zenith, sat_zenith, azidiff, redband_refl, exp_result):