Skip to content

Commit

Permalink
Merge branch 'sweref99_projection_output' into smhi-msb-enhancements2023
Browse files Browse the repository at this point in the history
# Conflicts:
#	activefires_pp/tests/test_messaging.py
  • Loading branch information
Adam.Dybbroe committed Jul 20, 2023
2 parents 4b157b0 + e6ef5be commit d41a6ee
Show file tree
Hide file tree
Showing 2 changed files with 75 additions and 26 deletions.
17 changes: 8 additions & 9 deletions activefires_pp/post_processing.py
Original file line number Diff line number Diff line change
Expand Up @@ -321,10 +321,6 @@ def __init__(self, configfile, shp_borders, shp_mask, regional_filtermask=None):
self.outfile_pattern_national_sweref99 = self.options.get('geojson_file_pattern_national_sweref99')
self.outfile_pattern_regional = self.options.get('geojson_file_pattern_regional')

# self.regional_outputs = self.options.get('geojson-regional')
# self.national_outputs = self.options.get('geojson-national')
# self.set_output_filename_parsers()

self.output_dir = self.options.get('output_dir', '/tmp')
self.filepath_detection_id_cache = self.options.get('filepath_detection_id_cache')

Expand Down Expand Up @@ -421,7 +417,7 @@ def _national_save_and_publish(self, feature_collection, ndata, af_shapeff, msg,
logger.debug("Output file path = %s", out_filepath)

store_geojson(out_filepath, feature_collection)
output_messages = self.get_output_messages(out_filepath, msg, ndata)
output_messages = self.get_output_messages(out_filepath, msg, ndata, sweref99=sweref99)

for output_msg in output_messages:
if output_msg:
Expand Down Expand Up @@ -566,20 +562,23 @@ def fires_filtering(self, msg, af_shapeff):

return afdata_ff

def get_output_messages(self, filepath, msg, number_of_data):
def get_output_messages(self, filepath, msg, number_of_data, sweref99=False):
"""Generate the adequate output message(s) depending on if an output file was created or not."""
logger.info("Geojson file created! Number of fires = %d", number_of_data)
return [self._generate_output_message(filepath, msg)]
return [self._generate_output_message(filepath, msg, sweref99=sweref99)]

def _generate_output_message(self, filepath, input_msg, region=None):
def _generate_output_message(self, filepath, input_msg, region=None, sweref99=False):
"""Create the output message to publish."""
output_topic = generate_posttroll_topic(self.output_topic, region)
to_send = prepare_posttroll_message(input_msg, region)
to_send['uri'] = str(filepath)
to_send['uid'] = os.path.basename(filepath)
to_send['type'] = 'GEOJSON-filtered'
to_send['format'] = 'geojson'
to_send['product'] = 'afimg'
if sweref99:
to_send['product'] = 'afimg_sweref99'
else:
to_send['product'] = 'afimg'
pubmsg = Message(output_topic, 'file', to_send)
return pubmsg

Expand Down
84 changes: 67 additions & 17 deletions activefires_pp/tests/test_messaging.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,9 +22,10 @@

"""Unit testing the message handling part of the post-processing."""

# import pytest
import pytest
from unittest.mock import patch
from datetime import datetime
import logging

from posttroll.message import Message
from posttroll.testing import patched_publisher
Expand Down Expand Up @@ -196,33 +197,82 @@ def test_check_incoming_message_txt_file_does_not_exist(setup_comm,
assert result is None


@pytest.mark.parametrize("sweref99, expected",
[(True, 'afimg_sweref99'),
(False, 'afimg')]
)
def test_prepare_posttroll_message_national(caplog, sweref99, expected):
"""Test prepare the posttroll message for detections on a National level."""
myconfigfile = "/my/config/file/path"
myboarders_file = "/my/shape/file/with/country/boarders"
mymask_file = "/my/shape/file/with/polygons/to/filter/out"

with patch('socket.gethostname') as gethostname:
with patch('activefires_pp.post_processing.read_config') as get_config:
with patch('activefires_pp.post_processing.ActiveFiresPostprocessing._setup_and_start_communication'):
get_config.return_value = CONFIG_EXAMPLE
gethostname.return_value = "my.host.name"

afpp = ActiveFiresPostprocessing(myconfigfile, myboarders_file, mymask_file)

test_filepath = "/my/geojson/file/path"

input_msg = Message.decode(rawstr=TEST_MSG)
with caplog.at_level(logging.INFO):
result_messages = afpp.get_output_messages(test_filepath, input_msg, 1, sweref99=sweref99)

log_expected = "Geojson file created! Number of fires = 1"
assert log_expected in caplog.text

res_msg = result_messages[0]

assert res_msg.data['platform_name'] == 'NOAA-20'
assert res_msg.data['type'] == 'GEOJSON-filtered'
assert res_msg.data['format'] == 'geojson'
assert res_msg.data['product'] == expected
assert res_msg.subject == '/VIIRS/L2/Fires/PP/National'
assert res_msg.data['uri'] == '/my/geojson/file/path'


def test_prepare_posttroll_message_regional(caplog):
"""Test setup the posttroll message."""
myconfigfile = "/my/config/file/path"
myboarders_file = "/my/shape/file/with/country/boarders"
mymask_file = "/my/shape/file/with/polygons/to/filter/out"

with patch('socket.gethostname') as gethostname:
with patch('activefires_pp.post_processing.read_config') as get_config:
with patch('activefires_pp.post_processing.ActiveFiresPostprocessing._setup_and_start_communication'):
get_config.return_value = CONFIG_EXAMPLE
gethostname.return_value = "my.host.name"
afpp = ActiveFiresPostprocessing(myconfigfile, myboarders_file, mymask_file)

test_filepath = "/my/geojson/file/path"

input_msg = Message.decode(rawstr=TEST_MSG)

fake_region_mask = {'attributes': {'Kod_omr': '9999',
'Testomr': 'Some area description'}}
with caplog.at_level(logging.INFO):
res_msg = afpp._generate_output_message(test_filepath, input_msg, region=fake_region_mask)

assert caplog.text == ''
assert res_msg.subject == '/VIIRS/L2/Fires/PP/Regional/9999'


@patch('socket.gethostname')
@patch('activefires_pp.post_processing.read_config')
@patch('activefires_pp.post_processing.ActiveFiresPostprocessing._setup_and_start_communication')
def test_prepare_posttroll_message(setup_comm, get_config,
gethostname, fake_yamlconfig_file_post_processing):
def test_prepare_posttroll_message_no_fires(setup_comm, gethostname, fake_yamlconfig_file_post_processing):
"""Test setup the posttroll message."""
get_config.return_value = CONFIG_EXAMPLE
gethostname.return_value = "my.host.name"

myboarders_file = "/my/shape/file/with/country/boarders"
mymask_file = "/my/shape/file/with/polygons/to/filter/out"
test_filepath = "/my/geojson/file/path"

afpp = ActiveFiresPostprocessing(fake_yamlconfig_file_post_processing,
myboarders_file, mymask_file)

test_filepath = "/my/geojson/file/path"

input_msg = Message.decode(rawstr=TEST_MSG)
res_msg = afpp.get_output_messages(test_filepath, input_msg, 1)

assert res_msg[0].data['platform_name'] == 'NOAA-20'
assert res_msg[0].data['type'] == 'GEOJSON-filtered'
assert res_msg[0].data['format'] == 'geojson'
assert res_msg[0].data['product'] == 'afimg'
assert res_msg[0].subject == '/VIIRS/L2/Fires/PP/National'
assert res_msg[0].data['uri'] == '/my/geojson/file/path'

input_msg = Message.decode(rawstr=TEST_MSG)

fake_region_mask = {'attributes': {'Kod_omr': '9999',
Expand Down

0 comments on commit d41a6ee

Please sign in to comment.