Skip to content

Commit

Permalink
Add tests to keep sun-earth distance-corrected reflectances as dask a…
Browse files Browse the repository at this point in the history
…rrays
  • Loading branch information
mraspaud committed Sep 11, 2020
1 parent 37b68c8 commit d030fd1
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 19 deletions.
20 changes: 11 additions & 9 deletions satpy/readers/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,20 +17,20 @@
# satpy. If not, see <http://www.gnu.org/licenses/>.
"""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:
Expand Down Expand Up @@ -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


Expand All @@ -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
21 changes: 11 additions & 10 deletions satpy/tests/reader_tests/test_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,15 +17,16 @@
# satpy. If not, see <http://www.gnu.org/licenses/>.
"""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


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

0 comments on commit d030fd1

Please sign in to comment.