Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix usage when trollsched is not installed #223

Merged
merged 9 commits into from
Nov 18, 2024
15 changes: 10 additions & 5 deletions trollflow2/plugins/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -639,8 +639,8 @@
Helper for covers().
"""
area_path = "/product_list/areas/%s" % area
cov = get_scene_coverage(platform_name, start_time, end_time,
sensor, area)
cov = _get_scene_coverage(platform_name, start_time, end_time,
sensor, area)
product_list['product_list']['areas'][area]['area_coverage_percent'] = cov
if cov < min_coverage:
logger.info(
Expand All @@ -654,7 +654,7 @@
f"{min_coverage:.2f}% - Carry on with {area:s}")


def get_scene_coverage(platform_name, start_time, end_time, sensor, area_id):
def _get_scene_coverage(platform_name, start_time, end_time, sensor, area_id):
"""Get scene area coverage in percentages."""
overpass = Pass(platform_name, start_time, end_time, instrument=sensor)
area_def = get_area_def(area_id)
Expand Down Expand Up @@ -968,6 +968,11 @@
"""
logger.info("Checking valid data fraction.")

if get_twilight_poly is None:
logger.error("Trollsched import failed, calculation of valid data fraction not possible")
logger.info("Keeping all products")
return

Check warning on line 974 in trollflow2/plugins/__init__.py

View check run for this annotation

Codecov / codecov/patch

trollflow2/plugins/__init__.py#L972-L974

Added lines #L972 - L974 were not covered by tests

exp_cov = {}
# As stated, this will trigger a computation. To prevent computing
# multiple times, we should persist everything that needs to be persisted,
Expand Down Expand Up @@ -1028,8 +1033,8 @@
end_time = prod.attrs["end_time"]
sensor = prod.attrs["sensor"]
if area_name not in exp_cov:
# get_scene_coverage uses %, convert to fraction
exp_cov[area_name] = get_scene_coverage(
# _get_scene_coverage uses %, convert to fraction
exp_cov[area_name] = _get_scene_coverage(
platform_name, start_time, end_time, sensor, area_name)/100
exp_valid = exp_cov[area_name]
if exp_valid == 0:
Expand Down
40 changes: 20 additions & 20 deletions trollflow2/tests/test_trollflow2.py
Original file line number Diff line number Diff line change
Expand Up @@ -1347,9 +1347,9 @@ 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, \
with mock.patch('trollflow2.plugins._get_scene_coverage') as _get_scene_coverage, \
mock.patch('trollflow2.plugins.Pass'):
get_scene_coverage.return_value = 10.0
_get_scene_coverage.return_value = 10.0
scn = _get_mocked_scene_with_properties()
job = {"product_list": self.product_list,
"input_mda": {"platform_name": "platform",
Expand All @@ -1365,9 +1365,9 @@ 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, \
with mock.patch('trollflow2.plugins._get_scene_coverage') as _get_scene_coverage, \
mock.patch('trollflow2.plugins.Pass'):
get_scene_coverage.return_value = 10.0
_get_scene_coverage.return_value = 10.0
scn = _get_mocked_scene_with_properties()
job = {"product_list": self.product_list,
"input_mda": {"platform_name": "platform",
Expand All @@ -1383,19 +1383,19 @@ def test_metadata_is_read_from_scene(self):
"""Test that the scene and message metadata are merged correctly."""
from trollflow2.plugins import covers

with mock.patch('trollflow2.plugins.get_scene_coverage') as get_scene_coverage, \
with mock.patch('trollflow2.plugins._get_scene_coverage') as _get_scene_coverage, \
mock.patch('trollflow2.plugins.Pass'):
get_scene_coverage.return_value = 10.0
_get_scene_coverage.return_value = 10.0
scn = _get_mocked_scene_with_properties()
job = {"product_list": self.product_list,
"input_mda": {"platform_name": "platform"},
"scene": scn}
covers(job)
get_scene_coverage.assert_called_with(job["input_mda"]["platform_name"],
scn.start_time,
scn.end_time,
list(scn.sensor_names)[0],
"omerc_bb")
_get_scene_coverage.assert_called_with(job["input_mda"]["platform_name"],
scn.start_time,
scn.end_time,
list(scn.sensor_names)[0],
"omerc_bb")

def test_covers(self):
"""Test coverage."""
Expand Down Expand Up @@ -1429,14 +1429,14 @@ def test_covers_uses_only_one_sensor(self):
"scene": scn}
job2 = copy.deepcopy(job)

with mock.patch('trollflow2.plugins.get_scene_coverage') as get_scene_coverage, \
with mock.patch('trollflow2.plugins._get_scene_coverage') as _get_scene_coverage, \
mock.patch('trollflow2.plugins.Pass'):
get_scene_coverage.return_value = 10.0
_get_scene_coverage.return_value = 10.0
covers(job)
get_scene_coverage.assert_called_with(input_mda['platform_name'],
input_mda['start_time'],
input_mda['end_time'],
'avhrr-4', 'omerc_bb')
_get_scene_coverage.assert_called_with(input_mda['platform_name'],
input_mda['start_time'],
input_mda['end_time'],
'avhrr-4', 'omerc_bb')

del job2["product_list"]["product_list"]["areas"]["euron1"]["min_coverage"]
del job2["product_list"]["product_list"]["min_coverage"]
Expand All @@ -1446,7 +1446,7 @@ def test_covers_uses_only_one_sensor(self):

def test_scene_coverage(self):
"""Test scene coverage."""
from trollflow2.plugins import get_scene_coverage
from trollflow2.plugins import _get_scene_coverage
with mock.patch('trollflow2.plugins.get_area_def') as get_area_def, \
mock.patch('trollflow2.plugins.Pass') as ts_pass:
area_coverage = mock.MagicMock()
Expand All @@ -1455,7 +1455,7 @@ def test_scene_coverage(self):
overpass.area_coverage = area_coverage
ts_pass.return_value = overpass
get_area_def.return_value = 6
res = get_scene_coverage(1, 2, 3, 4, 5)
res = _get_scene_coverage(1, 2, 3, 4, 5)
self.assertEqual(res, 100 * 0.2)
ts_pass.assert_called_with(1, 2, 3, instrument=4)
get_area_def.assert_called_with(5)
Expand Down Expand Up @@ -2185,7 +2185,7 @@ def test_valid_filter(caplog, sc_3a_3b):
job2 = copy.deepcopy(job)
prods2 = job2['product_list']['product_list']['areas']['euron1']['products']

with mock.patch("trollflow2.plugins.get_scene_coverage") as tpg, \
with mock.patch("trollflow2.plugins._get_scene_coverage") as tpg, \
caplog.at_level(logging.DEBUG):
tpg.return_value = 100
check_valid_data_fraction(job)
Expand Down
Loading