Skip to content

Commit

Permalink
Change product name depending on coordinate system (sweref99 or wgs84)
Browse files Browse the repository at this point in the history
Signed-off-by: Adam.Dybbroe <a000680@c21856.ad.smhi.se>
  • Loading branch information
Adam.Dybbroe committed Jul 20, 2023
1 parent efe15ba commit e6ef5be
Show file tree
Hide file tree
Showing 2 changed files with 66 additions and 17 deletions.
13 changes: 8 additions & 5 deletions activefires_pp/post_processing.py
Original file line number Diff line number Diff line change
Expand Up @@ -409,7 +409,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 @@ -552,20 +552,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'] = ('ssh://%s/%s' % (self.host, 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
70 changes: 58 additions & 12 deletions activefires_pp/tests/test_messaging.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,10 @@

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

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 @@ -151,40 +153,84 @@ def test_check_incoming_message_txt_file_does_not_exist(setup_comm,
assert result is None


@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):
"""Test setup the posttroll message."""
get_config.return_value = CONFIG_EXAMPLE
gethostname.return_value = "my.host.name"

@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"

afpp = ActiveFiresPostprocessing(myconfigfile, myboarders_file, mymask_file)
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)
res_msg = afpp._generate_output_message(test_filepath, input_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'] == 'afimg'
assert res_msg.data['product'] == expected
assert res_msg.subject == '/VIIRS/L2/Fires/PP/National'
assert res_msg.data['uri'] == 'ssh://my.host.name//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'}}
res_msg = afpp._generate_output_message(test_filepath, input_msg, region=fake_region_mask)
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_no_fires(setup_comm, get_config, gethostname):
"""Test setup the posttroll message."""
get_config.return_value = CONFIG_EXAMPLE
gethostname.return_value = "my.host.name"

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

afpp = ActiveFiresPostprocessing(myconfigfile, myboarders_file, mymask_file)

input_msg = Message.decode(rawstr=TEST_MSG)
msg_str = 'No fire detections for this granule'

result_messages = afpp._generate_no_fires_messages(input_msg, msg_str)
Expand Down

0 comments on commit e6ef5be

Please sign in to comment.