• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

pytroll / pytroll-pps-runner / 9282584827

29 May 2024 08:14AM UTC coverage: 75.907% (+1.0%) from 74.884%
9282584827

Pull #62

github

web-flow
Merge e7a399b31 into 311997dcf
Pull Request #62: PPS product and level1c collector

389 of 557 branches covered (69.84%)

Branch coverage included in aggregate %.

100 of 135 new or added lines in 6 files covered. (74.07%)

19 existing lines in 2 files now uncovered.

1473 of 1896 relevant lines covered (77.69%)

3.11 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

81.73
/nwcsafpps_runner/message_utils.py
1
#!/usr/bin/env python
2
# -*- coding: utf-8 -*-
3

4
# Copyright (c) 2021 Adam.Dybbroe
5

6
# Author(s):
7

8
#   Adam.Dybbroe <a000680@c21856.ad.smhi.se>
9

10
# This program is free software: you can redistribute it and/or modify
11
# it under the terms of the GNU General Public License as published by
12
# the Free Software Foundation, either version 3 of the License, or
13
# (at your option) any later version.
14

15
# This program is distributed in the hope that it will be useful,
16
# but WITHOUT ANY WARRANTY; without even the implied warranty of
17
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
18
# GNU General Public License for more details.
19

20
# You should have received a copy of the GNU General Public License
21
# along with this program.  If not, see <http://www.gnu.org/licenses/>.
22

23
"""Message utilities."""
4✔
24

25
import logging
4✔
26
import os
4✔
27

28
from posttroll.message import Message
4✔
29
from nwcsafpps_runner.utils import create_pps_file_from_lvl1c
4✔
30

31

32
LOG = logging.getLogger(__name__)
4✔
33

34

35
def remove_non_pps_products(msg_data):
4✔
36
    """ Remove non-PPS files from datasetlis.t"""
37
    msg_data["dataset"] = [item for item in msg_data["dataset"] if "S_NWC" in item["uid"]]
4✔
38

39

40
def get_pps_sensor_from_msg(sensor_msg):
4✔
41
    """ Get pps sensor from msg sensor."""
42
    sensor = None
4✔
43
    if type(sensor_msg) is list and len(sensor_msg) == 1:
4!
44
        sensor = sensor_msg[0]
4✔
45
    if sensor is None:
4!
NEW
46
        for pps_sensor in ['viirs', 'avhrr', 'modis', 'mersi2', 'metimage', 'slstr']:
×
NEW
47
            if pps_sensor in sensor_msg:
×
NEW
48
                sensor = pps_sensor
×
49
    if "avhrr/3" in sensor_msg:
4!
NEW
50
        sensor = "avhrr"
×
51
    return sensor
4✔
52

53

54
def add_lvl1c_to_msg(msg_data, options):
4✔
55
    """Add PPS lvl1c file to a collection of PPS products."""
56
    level1c_path = os.environ.get('SM_IMAGER_DIR', options.get('pps_lvl1c_dir', './'))
4✔
57
    sensor = options.get('sensor', get_pps_sensor_from_msg(msg_data["sensor"]))
4✔
58
    num_files = len(msg_data['dataset'])
4✔
59
    to_add = {}
4✔
60
    for item in msg_data['dataset']:
4✔
61
        lvl1c_file = create_pps_file_from_lvl1c(item["uri"], level1c_path,
4✔
62
                                                name_tag=sensor, file_type='.nc')
63
        to_add[lvl1c_file] = {
4✔
64
            "uri": lvl1c_file,
65
            "uid": os.path.basename(lvl1c_file)}
66
    msg_data['dataset'].extend(to_add.values())
4✔
67

68

69
def flatten_collection(msg_data):
4✔
70
    """Flatten collection msg to dataset msg."""
71
    if "collection" in msg_data:
4✔
72
        collection = msg_data.pop("collection")
4✔
73
        msg_data["dataset"] = []
4✔
74
        for ind in range(0, len(collection)):
4✔
75
            for item in collection[ind]["dataset"]:
4✔
76
                if type(item) == dict:
4!
77
                    msg_data["dataset"].append(item)
4✔
78

79

80
def prepare_pps_collector_message(msg, options):
4✔
81
    to_send = msg.data.copy()
4✔
82
    flatten_collection(to_send)
4✔
83
    remove_non_pps_products(to_send)
4✔
84
    add_lvl1c_to_msg(to_send, options)
4✔
85
    return to_send
4✔
86

87

88
def prepare_nwp_message(result_file, publish_topic):
4✔
89
    """Prepare message for NWP files."""
90
    to_send = {}
4✔
91
    to_send["uri"] = result_file
4✔
92
    filename = os.path.basename(result_file)
4✔
93
    to_send["uid"] = filename
4✔
94
    to_send['format'] = 'NWP grib'
4✔
95
    to_send['type'] = 'grib'
4✔
96
    return to_send
4✔
97

98

99
def prepare_l1c_message(result_file, mda, **kwargs):
4✔
100
    """Prepare the output message for the level-1c file creation."""
101
    if not result_file:
4!
102
        return
×
103

104
    # Now publish:
105
    to_send = mda.copy()
4✔
106
    # Delete the input level-1 dataset from the message:
107
    try:
4✔
108
        del to_send['dataset']
4✔
109
    except KeyError:
×
110
        LOG.warning("Couldn't remove dataset from message")
×
111
    try:
4✔
112
        del to_send['filename']
4✔
113
    except KeyError:
4✔
114
        LOG.warning("Couldn't remove filename from message")
4✔
115

116
    if ('orbit' in kwargs) and ('orbit_number' in to_send.keys()):
4!
117
        to_send["orig_orbit_number"] = to_send["orbit_number"]
×
118
        to_send["orbit_number"] = kwargs['orbit']
×
119

120
    to_send["uri"] = result_file
4✔
121
    filename = os.path.basename(result_file)
4✔
122
    to_send["uid"] = filename
4✔
123

124
    to_send['format'] = 'PPS-L1C'
4✔
125
    to_send['type'] = 'NETCDF'
4✔
126
    to_send['data_processing_level'] = '1c'
4✔
127

128
    return to_send
4✔
129

130

131
def publish_l1c(publisher, publish_msg, publish_topic, msg_type="file"):
4✔
132
    """Publish the messages that l1c files are ready."""
133
    LOG.debug('Publish topic = %s', publish_topic)
4✔
134
    for topic in publish_topic:
4✔
135
        msg = Message(topic, msg_type, publish_msg).encode()
4✔
136
        LOG.debug("sending: %s", str(msg))
4✔
137
        publisher.send(msg)
4✔
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2025 Coveralls, Inc