-
Notifications
You must be signed in to change notification settings - Fork 8
/
seviri_level1c_runner.py
279 lines (218 loc) · 9.96 KB
/
seviri_level1c_runner.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
#!/usr/bin/env python
# -*- coding: utf-8 -*-
# Copyright (c) 2019 - 2021 Pytroll
# Author(s):
# Erik Johansson <Firstname.Lastname at smhi.se>
# Adam Dybbroe <Firstname.Lastname at smhi.se>
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
# You should have received a copy of the GNU General Public License
# along with this program. If not, see <http://www.gnu.org/licenses/>.
import argparse
import socket
import logging
import sys
import os
from multiprocessing import cpu_count
from urllib.parse import urlunsplit
from level1c4pps.seviri2pps_lib import process_one_scan # @UnresolvedImport
from posttroll.subscriber import Subscribe # @UnresolvedImport
from posttroll.publisher import Publish # @UnresolvedImport
from posttroll.message import Message # @UnresolvedImport
from nwcsafpps_runner.config import get_config_from_yamlfile as get_config
from nwcsafpps_runner.utils import (deliver_output_file, cleanup_workdir)
from nwcsafpps_runner.logger import setup_logging
SUPPORTED_METEOSAT_SATELLITES = ['meteosat-8', 'meteosat-9', 'meteosat-10', 'meteosat-11']
#LOG = logging.getLogger(__name__)
LOG = logging.getLogger('seviri-l1c-runner')
class L1cProcessor(object):
"""Container for the SEVIRI HRIT processing."""
def __init__(self, ncpus):
from multiprocessing.pool import ThreadPool
self.pool = ThreadPool(ncpus)
self.ncpus = ncpus
self.sensor = "unknown"
self.orbit_number = 99999 # Initialised orbit number
self.platform_name = 'unknown'
self.l1c_result = None
self.pass_start_time = None
self.l1cfile = None
self.level1_files = []
self.result_home = OPTIONS.get('output_dir', '/tmp')
self.working_home = OPTIONS.get('working_dir', '/tmp')
self.publish_topic = OPTIONS.get('publish_topic')
self.site = OPTIONS.get('site', 'unknown')
self.environment = OPTIONS.get('environment')
self.message_data = None
self.service = None
def initialise(self, service):
"""Initialise the processor."""
self.l1c_result = None
self.pass_start_time = None
self.l1cfile = None
self.level1_files = []
self.service = service
def deliver_output_file(self, subd=None):
"""Deliver the generated level-1c file to the configured destination."""
LOG.debug("Result file: %s", str(self.l1cfile))
LOG.debug("Result home dir: %s", str(self.result_home))
LOG.debug("Sub directory: %s", subd)
return deliver_output_file(self.l1cfile, self.result_home, subd)
def run(self, msg):
"""Start the L1c processing using process_one_scan."""
if not msg:
return False
if msg.type != 'dataset':
LOG.info("Not a dataset, don't do anything...")
return False
if ('platform_name' not in msg.data or
'start_time' not in msg.data):
LOG.warning("Message is lacking crucial fields...")
return False
if msg.data['platform_name'].lower() not in SUPPORTED_METEOSAT_SATELLITES:
LOG.info(str(msg.data['platform_name']) + ": " +
"Not a valid Meteosat scene. Continue...")
return False
self.platform_name = str(msg.data['platform_name'])
self.sensor = str(msg.data['sensor'])
self.message_data = msg.data
level1_dataset = msg.data['dataset']
if len(level1_dataset) < 1:
return False
pro_files = False
epi_files = False
level1_files = []
for level1 in level1_dataset:
level1_filename = level1['uri']
level1_files.append(level1_filename)
if '-PRO' in level1_filename:
pro_files = True
if '-EPI' in level1_filename:
epi_files = True
if not pro_files:
LOG.warning("PRO file is missing...")
return False
if not epi_files:
LOG.warning("EPI file is missing...")
return False
self.level1_files = level1_files
self.l1c_result = self.pool.apply_async(process_one_scan, (self.level1_files,
self.working_home))
return True
def publish_l1c(publisher, result_file, mda, **kwargs):
"""Publish the messages that l1c files are ready."""
if not result_file:
return
# Now publish:
to_send = mda.copy()
# Delete the input level-1 dataset from the message:
try:
del(to_send['dataset'])
except KeyError:
LOG.warning("Couldn't remove dataset from message")
if ('orbit' in kwargs) and ('orbit_number' in to_send.keys()):
to_send["orig_orbit_number"] = to_send["orbit_number"]
to_send["orbit_number"] = kwargs['orbit']
to_send["uri"] = urlunsplit(('ssh', socket.gethostname(), result_file, '', ''))
filename = os.path.basename(result_file)
to_send["uid"] = filename
publish_topic = kwargs.get('publish_topic', 'Unknown')
site = kwargs.get('site', 'unknown')
to_send['format'] = 'PPS-L1C'
to_send['type'] = 'NETCDF'
to_send['data_processing_level'] = '1c'
to_send['site'] = site
LOG.debug('Site = %s', site)
LOG.debug('Publish topic = %s', publish_topic)
for topic in publish_topic:
msg = Message('/'.join(('', topic)),
"file", to_send).encode()
LOG.debug("sending: %s", str(msg))
publisher.send(msg)
def seviri_l1c_runner(options, service_name="unknown"):
"""The live runner for the SEVIRI l1c product generation."""
LOG.info("Start the SEVIRI l1C runner...")
LOG.debug("Listens for messages of type: %s", str(options['message_types']))
ncpus_available = cpu_count()
LOG.info("Number of CPUs available = %s", str(ncpus_available))
ncpus = int(options.get('num_of_cpus', 1))
LOG.info("Will use %d CPUs when running the CSPP SEVIRI instances", ncpus)
l1c_proc = L1cProcessor(ncpus)
with Subscribe('', options['message_types'], True) as sub:
with Publish('seviri_l1c_runner', 0) as publisher:
while True:
# count = 0
for msg in sub.recv():
l1c_proc.initialise(service_name)
status = l1c_proc.run(msg)
if not status:
break # end the loop and reinitialize !
LOG.debug(
"Received message data = %s", str(l1c_proc.message_data))
LOG.info("Get the results from the multiptocessing pool-run")
l1c_proc.l1cfile = l1c_proc.l1c_result.get()
l1c_filepaths = l1c_proc.deliver_output_file()
if l1c_proc.result_home == l1c_proc.working_home:
LOG.info("home_dir = working_dir no cleaning necessary")
else:
LOG.info("Cleaning up directory %s", l1c_proc.working_home)
cleanup_workdir(l1c_proc.working_home + '/')
publish_l1c(publisher, l1c_filepaths,
l1c_proc.message_data,
orbit=l1c_proc.orbit_number,
publish_topic=l1c_proc.publish_topic,
environment=l1c_proc.environment,
site=l1c_proc.site)
LOG.info("L1C processing has completed.")
def get_arguments():
"""
Get command line arguments.
Return
name of the service and the config filepath
"""
parser = argparse.ArgumentParser()
parser.add_argument("-l", "--log-config",
help="Log config file to use instead of the standard logging.")
parser.add_argument('-c', '--config_file',
type=str,
dest='config_file',
default='seviri_l1c_config.yaml',
help="The file containing " +
"configuration parameters e.g. product_filter_config.yaml, \n" +
"default = ./seviri_l1c_config.yaml",
required=True)
parser.add_argument("-s", "--service",
type=str,
dest="service",
default='seviri-l1c',
help="Name of the service (e.g. iasi-lvl2), \n" +
"default = seviri-l1c")
parser.add_argument("-m", "--mode",
type=str,
dest="mode",
default='utv',
help="Environment. replaces SMHI_MODE \n" +
"default = utv")
parser.add_argument("-v", "--verbose", dest="verbosity", action="count", default=0,
help="Verbosity (between 1 and 2 occurrences with more leading to more "
"verbose logging). WARN=0, INFO=1, "
"DEBUG=2. This is overridden by the log config file if specified.")
args = parser.parse_args()
setup_logging(args)
service = args.service.lower()
if 'template' in args.config_file:
LOG.error("Template file given as master config, aborting!")
sys.exit()
return service, args.config_file, args.mode
if __name__ == '__main__':
(SERVICE_NAME, CONFIG_FILENAME, ENVIRON) = get_arguments()
OPTIONS = get_config(CONFIG_FILENAME, SERVICE_NAME, ENVIRON)
OPTIONS['environment'] = ENVIRON
OPTIONS['nagios_config_file'] = None
seviri_l1c_runner(OPTIONS, SERVICE_NAME)