Skip to content
This repository has been archived by the owner on Feb 26, 2020. It is now read-only.

Commit

Permalink
Added "aliases" for replacing values in messages
Browse files Browse the repository at this point in the history
  • Loading branch information
pnuu committed Aug 25, 2014
1 parent f697ee8 commit b7f4070
Show file tree
Hide file tree
Showing 3 changed files with 65 additions and 13 deletions.
48 changes: 43 additions & 5 deletions bin/trollstalker.py
Expand Up @@ -47,9 +47,10 @@ class EventHandler(ProcessEvent):
*posttroll_port* - port number to publish the messages on
*filepattern* - filepattern for finding information from the filename
"""
def __init__(self, service, instrument, posttroll_port=0, filepattern=None):
def __init__(self, service, instrument, posttroll_port=0, filepattern=None,
aliases=None):
super(EventHandler, self).__init__()

self._pub = NoisyPublisher("trollstalker", posttroll_port, service)
self.pub = self._pub.start()
self.service = service
Expand All @@ -58,6 +59,7 @@ def __init__(self, service, instrument, posttroll_port=0, filepattern=None):
filepattern = '{filename}'
self.file_parser = Parser(filepattern)
self.instrument = instrument
self.aliases = aliases

def stop(self):
'''Stop publisher.
Expand Down Expand Up @@ -126,6 +128,12 @@ def parse_file_info(self, event):
self.info = self.file_parser.parse(event.pathname)
self.info['uri'] = event.pathname
self.info['instrument'] = self.instrument

# replace values with corresponding aliases, if any are given
if self.aliases:
for key in self.info:
if key in self.aliases:
self.info[key] = self.aliases[key][str(self.info[key])]
except ValueError:
# Filename didn't match pattern, so empty the info dict
self.info = {}
Expand All @@ -139,7 +147,7 @@ def stop(self, *args, **kwargs):


def create_notifier(service, instrument, posttroll_port, filepattern,
event_names, monitored_dirs):
event_names, monitored_dirs, aliases=None):
'''Create new notifier'''

# Event handler observes the operations in defined folder
Expand All @@ -157,7 +165,8 @@ def create_notifier(service, instrument, posttroll_port, filepattern,

event_handler = EventHandler(service, instrument,
posttroll_port=posttroll_port,
filepattern=filepattern)
filepattern=filepattern,
aliases=aliases)
notifier = NewThreadedNotifier(manager, event_handler)

# Add directories and event masks to watch manager
Expand All @@ -166,6 +175,34 @@ def create_notifier(service, instrument, posttroll_port, filepattern,

return notifier

def parse_aliases(config):
'''Parse aliases from the config.
Aliases are given in the config as:
{'alias_<name>': 'value:alias'}, or
{'alias_<name>': 'value1:alias1|value2:alias2'},
where <name> is the name of the key which value will be
replaced. The later form is there to support several possible
substitutions (eg. '2' -> '9' and '3' -> '10' in the case of MSG).
'''
aliases = {}

for key in config:
if 'alias' in key:
alias = config[key]
new_key = key.replace('alias_', '')
if '|' in alias or ':' in alias:
parts = alias.split('|')
aliases2 = {}
for part in parts:
key2, val2 = part.split(':')
aliases2[key2] = val2
alias = aliases2
aliases[new_key] = alias
return aliases

def main():
'''Main(). Commandline parsing and stalker startup.'''
Expand Down Expand Up @@ -259,6 +296,7 @@ def main():
instrument = instrument or config['instrument']
except KeyError:
pass
aliases = parse_aliases(config)
try:
log_config = config["stalker_log_config"]
except KeyError:
Expand Down Expand Up @@ -290,7 +328,7 @@ def main():

# Start watching for new files
notifier = create_notifier(service, instrument, posttroll_port, filepattern,
event_names, monitored_dirs)
event_names, monitored_dirs, aliases=aliases)
notifier.start()

try:
Expand Down
7 changes: 7 additions & 0 deletions doc/source/configuration.rst
Expand Up @@ -27,6 +27,13 @@ The required keys are:

Optional configuration keys:

* *alias_<name>* --- gives a replacement for keyword *<name>*. Useful (and actually required!) with HRIT/MSG files. Has two different formats:
* **alias_name=original:replacement**
* alias_platform_name=MSG:meteosat
* replace value *MSG* of the keyword *platform_name* to *meteosat*
* **alias_name=old1:new1|old2:new2|old3:new3** ...
* alias_satnumber=1:8|2:9|3:10
* depending on the current value, replace the old value of *satnumber* with a new value: '1' -> '8', '2' -> '9' or '3' -> 10
* *stalker_log_config* --- full path to logging configuration file for *trollstalker*
* the output filename and path for *trollstalker* log files are set here
* *td_log_config* --- full path to logging configuration file for *trollduction*
Expand Down
23 changes: 15 additions & 8 deletions trollduction/trollduction.py
Expand Up @@ -152,10 +152,10 @@ def run(self, product_config, msg):

time_slot = msg.data['time']

# orbit is empty string for meteosat, change it to None
if 'orbit' in msg.data:
if msg.data['orbit'] == '':
msg.data['orbit'] = None
# orbit is not given for GEO satellites, use None

if 'orbit' not in msg.data:
msg.data['orbit'] = None

t1a = time.time()

Expand Down Expand Up @@ -256,11 +256,13 @@ def load_unload_channels(self, products, area_def_names=None):
loaded_channels = []
required_channels = []
wavelengths = []
chan_names = []

# Get information which channels are loaded
for chan in self.global_data.channels:
required_channels.append(False)
wavelengths.append(chan.wavelength_range)
chan_names.append(chan.name)
if chan.is_loaded():
loaded_channels.append(True)
else:
Expand All @@ -272,10 +274,15 @@ def load_unload_channels(self, products, area_def_names=None):
product['composite']+'.prerequisites')
for req in reqs:
for i in range(len(self.global_data.channels)):
if req >= np.min(wavelengths[i]) and \
req <= np.max(wavelengths[i]):
required_channels[i] = True
break
if isinstance(req, str):
if req is chan_names[i]:
required_channels[i] = True
break
else:
if req >= np.min(wavelengths[i]) and \
req <= np.max(wavelengths[i]):
required_channels[i] = True
break

to_load = []
to_unload = []
Expand Down

0 comments on commit b7f4070

Please sign in to comment.