Skip to content

Commit

Permalink
Merge branch 'main' into feature-eager-saving
Browse files Browse the repository at this point in the history
  • Loading branch information
pnuu authored Mar 10, 2022
2 parents d4eb0ca + 97882c0 commit dbd4e5d
Show file tree
Hide file tree
Showing 2 changed files with 66 additions and 14 deletions.
29 changes: 15 additions & 14 deletions trollflow2/plugins/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -424,9 +424,10 @@ def covers(job):
end_time = scn_mda['end_time']
sensor = scn_mda['sensor']
if isinstance(sensor, (list, tuple, set)):
if len(sensor) > 1:
LOG.warning("Multiple sensors given, taking the first one for "
"coverage calculations: %s", sensor)
sensor = list(sensor)[0]
LOG.warning("Possibly many sensors given, taking only one for "
"coverage calculations: %s", sensor)

areas = list(product_list['product_list']['areas'].keys())
for area in areas:
Expand Down Expand Up @@ -632,7 +633,7 @@ def check_sunlight_coverage(job):
continue
min_day = config.get('min')
max_day = config.get('max')
use_pass = config.get('check_pass', False)
check_pass = config.get('check_pass', False)

if min_day is None and max_day is None:
LOG.debug("Sunlight coverage not configured for %s / %s",
Expand All @@ -644,24 +645,24 @@ def check_sunlight_coverage(job):
if area_def is None:
continue

if use_pass and overpass is None:
if check_pass and overpass is None:
overpass = Pass(platform_name, start_time, end_time, instrument=sensor)

if coverage[use_pass] is None:
coverage[use_pass] = _get_sunlight_coverage(area_def,
start_time,
overpass)
if coverage[check_pass] is None:
coverage[check_pass] = _get_sunlight_coverage(area_def,
start_time,
overpass)
area_conf = product_list['product_list']['areas'][area]
area_conf['area_sunlight_coverage_percent'] = coverage[use_pass] * 100
if min_day is not None and coverage[use_pass] < (min_day / 100.0):
area_conf['area_sunlight_coverage_percent'] = coverage[check_pass] * 100
if min_day is not None and coverage[check_pass] < (min_day / 100.0):
LOG.info("Not enough sunlight coverage for "
f"product '{product!s}', removed. Needs at least "
f"{min_day:.1f}%, got {coverage[use_pass]:.1%}.")
f"{min_day:.1f}%, got {coverage[check_pass]:.1%}.")
dpath.util.delete(product_list, prod_path)
if max_day is not None and coverage[use_pass] > (max_day / 100.0):
if max_day is not None and coverage[check_pass] > (max_day / 100.0):
LOG.info("Too much sunlight coverage for "
f"product '{product!s}', removed. Needs at most "
f"{max_day:.1f}%, got {coverage[use_pass]:.1%}.")
f"{max_day:.1f}%, got {coverage[check_pass]:.1%}.")
dpath.util.delete(product_list, prod_path)


Expand Down Expand Up @@ -695,7 +696,7 @@ def _get_sunlight_coverage(area_def, start_time, overpass=None):
return 0.0
else:
daylight_area = daylight.area()
total_area = adp.area()
total_area = cut_area_poly.area()
return daylight_area / total_area


Expand Down
51 changes: 51 additions & 0 deletions trollflow2/tests/test_trollflow2.py
Original file line number Diff line number Diff line change
Expand Up @@ -1029,6 +1029,22 @@ def test_metadata_is_read_from_scene(self):
ts_pass.assert_called_with(job["input_mda"]["platform_name"], scene.start_time, scene.end_time,
instrument=list(scene.sensor_names)[0])

def test_fully_sunlit_scene_returns_full_coverage(self):
"""Test that a fully sunlit scene returns 100% coverage."""
from trollflow2.plugins import check_sunlight_coverage
from pyresample.spherical import SphPolygon
import numpy as np
with mock.patch('trollflow2.plugins.Pass') as tst_pass,\
mock.patch('trollflow2.plugins.get_twilight_poly') as twilight:
tst_pass.return_value.boundary.contour_poly = SphPolygon(np.array([(0, 0), (0, 90), (45, 0)]))
twilight.return_value = SphPolygon(np.array([(0, 0), (0, 90), (90, 0)]))
scene = _get_mocked_scene_with_properties()
job = {"scene": scene, "product_list": self.product_list.copy(),
"input_mda": {"platform_name": "platform"}}
job['product_list']['product_list']['sunlight_coverage'] = {'min': 10, 'max': 40, 'check_pass': True}
check_sunlight_coverage(job)
assert job['product_list']['product_list']['areas']['euron1']['area_sunlight_coverage_percent'] == 100

def test_product_not_loaded(self):
"""Test that product isn't loaded when sunlight coverage is too low."""
from trollflow2.plugins import check_sunlight_coverage
Expand Down Expand Up @@ -1136,6 +1152,41 @@ def test_covers_no_trollsched(self):
covers(job)
self.assertEqual(job, job_orig)

def test_covers_complains_when_multiple_sensors_are_provided(self):
"""Test that the plugin complains when multiple sensors are provided."""
from trollflow2.plugins import covers

with mock.patch('trollflow2.plugins.get_scene_coverage') as get_scene_coverage, \
mock.patch('trollflow2.plugins.Pass'):
get_scene_coverage.return_value = 10.0
scn = _get_mocked_scene_with_properties()
job = {"product_list": self.product_list,
"input_mda": {"platform_name": "platform",
"sensor": ["avhrr-3", "mhs"]},
"scene": scn}
with self.assertLogs("trollflow2.plugins", logging.WARNING) as log:
covers(job)
assert len(log.output) == 1
assert ("Multiple sensors given, taking the first one for coverage calculations" in log.output[0])

def test_covers_does_not_complain_when_one_sensor_is_provided_as_a_sequence(self):
"""Test that the plugin complains when multiple sensors are provided."""
from trollflow2.plugins import covers

with mock.patch('trollflow2.plugins.get_scene_coverage') as get_scene_coverage, \
mock.patch('trollflow2.plugins.Pass'):
get_scene_coverage.return_value = 10.0
scn = _get_mocked_scene_with_properties()
job = {"product_list": self.product_list,
"input_mda": {"platform_name": "platform",
"sensor": ["avhrr-3"]},
"scene": scn}
with self.assertLogs("trollflow2.plugins", logging.WARNING) as log:
covers(job)
logger = logging.getLogger("trollflow2.plugins")
logger.warning("Dummy warning")
assert len(log.output) == 1

def test_metadata_is_read_from_scene(self):
"""Test that the scene and message metadata are merged correctly."""
from trollflow2.plugins import covers
Expand Down

0 comments on commit dbd4e5d

Please sign in to comment.