Skip to content

Commit

Permalink
Merge pull request #128 from pnuu/fix-scene-attribute-usage
Browse files Browse the repository at this point in the history
Fix scene attribute usage in sza_check
  • Loading branch information
mraspaud authored Dec 17, 2021
2 parents d121f1d + 3053efc commit 611896c
Show file tree
Hide file tree
Showing 2 changed files with 75 additions and 31 deletions.
5 changes: 3 additions & 2 deletions trollflow2/plugins/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -528,8 +528,9 @@ def metadata_alias(job):

def sza_check(job):
"""Remove products which are not valid for the current Sun zenith angle."""
scn = job['scene']
start_time = scn.attrs['start_time']
scn_mda = job['scene'].attrs.copy()
scn_mda.update(job['input_mda'])
start_time = scn_mda['start_time']
product_list = job['product_list']
areas = list(product_list['product_list']['areas'].keys())
for area in areas:
Expand Down
101 changes: 72 additions & 29 deletions trollflow2/tests/test_trollflow2.py
Original file line number Diff line number Diff line change
Expand Up @@ -1262,48 +1262,91 @@ def test_get_plugin_conf(self):
class TestSZACheck(TestCase):
"""Test case for SZA check."""

def test_sza_check(self):
"""Test the SZA check."""
def setUp(self):
"""Create common items."""
product_list_no_sza, job_no_sza = _get_product_list_and_job()
self.product_list_no_sza = product_list_no_sza
self.job_no_sza = job_no_sza
product_list_with_sza, job_with_sza = _get_product_list_and_job(add_sza_limits=True)
self.product_list_with_sza = product_list_with_sza
self.job_with_sza = job_with_sza

def test_sza_check_no_settings(self):
"""Test the SZA check without any settings."""
from trollflow2.plugins import sza_check
with mock.patch("trollflow2.plugins.sun_zenith_angle") as sun_zenith_angle:
job = {}
scene = mock.MagicMock()
scene.attrs = {'start_time': 42}
job['scene'] = scene
from trollflow2.launcher import yaml, UnsafeLoader
product_list = yaml.load(yaml_test1, Loader=UnsafeLoader)
job['product_list'] = product_list.copy()
# Run without any settings
sza_check(job)
sza_check(self.job_no_sza)
sun_zenith_angle.assert_not_called()

# Add SZA limits to couple of products
# Day product
product_list['product_list']['areas']['omerc_bb']['products']['ct']['sunzen_maximum_angle'] = 95.
product_list['product_list']['areas']['omerc_bb']['products']['ct']['sunzen_check_lon'] = 25.
product_list['product_list']['areas']['omerc_bb']['products']['ct']['sunzen_check_lat'] = 60.
# Night product
product_list['product_list']['areas']['germ']['products']['cloudtype']['sunzen_minimum_angle'] = 85.
product_list['product_list']['areas']['germ']['products']['cloudtype']['sunzen_check_lon'] = 25.
product_list['product_list']['areas']['germ']['products']['cloudtype']['sunzen_check_lat'] = 60.

# Zenith angle that removes nothing
def test_sza_check_with_ok_sza(self):
"""Test the SZA check with SZA that is ok for all the products."""
from trollflow2.plugins import sza_check
with mock.patch("trollflow2.plugins.sun_zenith_angle") as sun_zenith_angle:
# Zenith angle that is ok for all the products
sun_zenith_angle.return_value = 90.
sza_check(job)

sza_check(self.job_with_sza)

sun_zenith_angle.assert_called_with(42, 25., 60.)
self.assertDictEqual(job['product_list'], product_list)
self.assertDictEqual(self.job_with_sza['product_list'], self.product_list_with_sza)

def test_sza_check_removes_day_products(self):
"""Test the SZA check with SZA that removes day products."""
from trollflow2.plugins import sza_check
with mock.patch("trollflow2.plugins.sun_zenith_angle") as sun_zenith_angle:
# Zenith angle that removes day products
sun_zenith_angle.return_value = 100.
sza_check(job)
self.assertTrue('cloud_top_height' in product_list['product_list']['areas']['omerc_bb']['products'])
self.assertFalse('ct' in product_list['product_list']['areas']['omerc_bb']['products'])

sza_check(self.job_with_sza)

self.assertTrue('cloud_top_height' in
self.product_list_with_sza['product_list']['areas']['omerc_bb']['products'])
self.assertFalse('ct' in self.product_list_with_sza['product_list']['areas']['omerc_bb']['products'])

def test_sza_check_removes_night_products(self):
"""Test the SZA check with SZA that removes night products."""
from trollflow2.plugins import sza_check
with mock.patch("trollflow2.plugins.sun_zenith_angle") as sun_zenith_angle:
# Zenith angle that removes night products
sun_zenith_angle.return_value = 45.
sza_check(job)

sza_check(self.job_with_sza)

# There was only one product, so the whole area is deleted
self.assertFalse('germ' in job['product_list']['product_list']['areas'])
self.assertFalse('germ' in self.job_with_sza['product_list']['product_list']['areas'])


def _get_product_list_and_job(add_sza_limits=False):
from trollflow2.launcher import yaml, UnsafeLoader
product_list = yaml.load(yaml_test1, Loader=UnsafeLoader)
if add_sza_limits:
_add_sunzen_limits(product_list)
job = _create_job(product_list)

return product_list, job


def _create_job(product_list):
job = {}
scene = mock.MagicMock()
scene.attrs = {}
job['input_mda'] = {'start_time': 42, 'another_message_item': 'coconut'}
job['scene'] = scene
job['product_list'] = product_list.copy()

return job


def _add_sunzen_limits(product_list):
# Add SZA limits to couple of products
# Day product
product_list['product_list']['areas']['omerc_bb']['products']['ct']['sunzen_maximum_angle'] = 95.
product_list['product_list']['areas']['omerc_bb']['products']['ct']['sunzen_check_lon'] = 25.
product_list['product_list']['areas']['omerc_bb']['products']['ct']['sunzen_check_lat'] = 60.
# Night product
product_list['product_list']['areas']['germ']['products']['cloudtype']['sunzen_minimum_angle'] = 85.
product_list['product_list']['areas']['germ']['products']['cloudtype']['sunzen_check_lon'] = 25.
product_list['product_list']['areas']['germ']['products']['cloudtype']['sunzen_check_lat'] = 60.


class TestOverviews(TestCase):
Expand Down

0 comments on commit 611896c

Please sign in to comment.