From d030fd11a553162ffed796955c4a4911f392999c Mon Sep 17 00:00:00 2001 From: Martin Raspaud Date: Fri, 11 Sep 2020 11:28:22 +0200 Subject: [PATCH] Add tests to keep sun-earth distance-corrected reflectances as dask arrays --- satpy/readers/utils.py | 20 +++++++++++--------- satpy/tests/reader_tests/test_utils.py | 21 +++++++++++---------- 2 files changed, 22 insertions(+), 19 deletions(-) diff --git a/satpy/readers/utils.py b/satpy/readers/utils.py index 1b942e2895..98001db647 100644 --- a/satpy/readers/utils.py +++ b/satpy/readers/utils.py @@ -17,20 +17,20 @@ # satpy. If not, see . """Helper functions for satpy readers.""" -import logging - -from contextlib import closing -import tempfile import bz2 +import logging import os import shutil -import numpy as np -import pyproj +import tempfile import warnings +from contextlib import closing from io import BytesIO from subprocess import Popen, PIPE -from pyresample.geometry import AreaDefinition +import numpy as np +import pyproj +import xarray as xr +from pyresample.geometry import AreaDefinition from satpy import CHUNK_SIZE try: @@ -334,9 +334,10 @@ def apply_earthsun_distance_correction(reflectance, utc_date=None): utc_date = get_array_date(reflectance, utc_date) sun_earth_dist = sun_earth_distance_correction(utc_date) - reflectance = reflectance * sun_earth_dist * sun_earth_dist reflectance.attrs['sun_earth_distance_correction_applied'] = True reflectance.attrs['sun_earth_distance_correction_factor'] = sun_earth_dist + with xr.set_options(keep_attrs=True): + reflectance = reflectance * sun_earth_dist * sun_earth_dist return reflectance @@ -348,5 +349,6 @@ def remove_earthsun_distance_correction(reflectance, utc_date=None): reflectance.attrs['sun_earth_distance_correction_applied'] = False reflectance.attrs['sun_earth_distance_correction_factor'] = sun_earth_dist - reflectance = reflectance / (sun_earth_dist * sun_earth_dist) + with xr.set_options(keep_attrs=True): + reflectance = reflectance / (sun_earth_dist * sun_earth_dist) return reflectance diff --git a/satpy/tests/reader_tests/test_utils.py b/satpy/tests/reader_tests/test_utils.py index 2aabc1ba5d..7a91d0a197 100644 --- a/satpy/tests/reader_tests/test_utils.py +++ b/satpy/tests/reader_tests/test_utils.py @@ -17,15 +17,16 @@ # satpy. If not, see . """Testing of helper functions.""" +import os import unittest from datetime import datetime from unittest import mock -import os -import xarray as xr + +import dask.array as da import numpy as np import numpy.testing import pyresample.geometry - +import xarray as xr from satpy.readers import utils as hf @@ -335,16 +336,16 @@ class TestSunEarthDistanceCorrection(unittest.TestCase): """Tests for applying Sun-Earth distance correction to reflectance.""" def setUp(self): - """"Create input / output arrays for the tests.""" + """Create input / output arrays for the tests.""" self.test_date = datetime(2020, 8, 15, 13, 0, 40) - raw_refl = xr.DataArray(np.array([10., 20., 40., 1., 98., 50.]), + raw_refl = xr.DataArray(da.from_array([10., 20., 40., 1., 98., 50.]), attrs={'start_time': self.test_date, 'scheduled_time': self.test_date}) - corr_refl = xr.DataArray(np.array([10.50514689, 21.01029379, - 42.02058758, 1.05051469, - 102.95043957, 52.52573447]), + corr_refl = xr.DataArray(da.from_array([10.50514689, 21.01029379, + 42.02058758, 1.05051469, + 102.95043957, 52.52573447]), attrs={'start_time': self.test_date, 'scheduled_time': self.test_date}) self.raw_refl = raw_refl @@ -379,14 +380,14 @@ def test_get_utc_time(self): def test_apply_sunearth_corr(self): """Test the correction of reflectances with sun-earth distance.""" - out_refl = hf.apply_earthsun_distance_correction(self.raw_refl) np.testing.assert_allclose(out_refl, self.corr_refl) self.assertTrue(out_refl.attrs['sun_earth_distance_correction_applied']) + assert isinstance(out_refl.data, da.Array) def test_remove_sunearth_corr(self): """Test the removal of the sun-earth distance correction.""" - out_refl = hf.remove_earthsun_distance_correction(self.corr_refl) np.testing.assert_allclose(out_refl, self.raw_refl) self.assertFalse(out_refl.attrs['sun_earth_distance_correction_applied']) + assert isinstance(out_refl.data, da.Array)