# Observing condition decision tool: monitor conditions and plan HETDEX
# observations
# Copyright (C) 2017 "The HETDEX collaboration"
#
# 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 <https://www.gnu.org/licenses/>.
'''Implementation of the ``ocd allow_hetdex`` command'''
from __future__ import (absolute_import, division, print_function,
unicode_literals)
from . import config as ocd_config
from . import states
from . import utils
[docs]def allow_hetdex_parser(subparsers):
'''Add a subcommand "execute_hetdex" to toggle the
:class:`HetdexAllowedState`.
Parameters
----------
subparsers : argparse subparsers object
subparser to use to generate new parsers
Returns
-------
parser : :class:`argparse.ArgumentParser`
modified parser
'''
import argparse as ap
parser = subparsers.add_parser('allow_hetdex',
help='Enable/disable HETDEX execution',
description='''Subcommand to enable or
disable the execution of HETDEX shots, even
if the conditions are good.''',
formatter_class=ap.ArgumentDefaultsHelpFormatter)
parser.set_defaults(func=allow_hetdex_cmd)
parser = utils.override_epilog_msg(parser)
parser.add_argument('action', choices=['start', 'stop'], help='''"start": allow OCD to
execute HETDEX shots. "stop": don't start the execution
of new HETDEX shot; if a shot is running, it will
finish.''')
parser = ocd_config.config_file_argument(parser)
parser.add_argument('-n', '--max-attempts', type=int, help='''Maximum
number of times "%(prog)s" attempts to
enable/disable HETDEX shot execution''', default=5)
parser.add_argument('-I', '--interval', type=float, help='''Wait "%(dest)s"
seconds before a new attempt.''', default=1)
title = 'Override options in the [urls] section'
overrides_urls = parser.add_argument_group(title=title)
overrides_urls.add_argument('-o', '--ocd-allow-hetdex',
dest='setting__urls__ocd_allow_hetdex',
metavar='OCD_ALLOW_HETDEX',
help='''Urls/paths used to send the signal to
enable/disable the HETDEX execution.''',
nargs='+')
overrides_urls.add_argument('-i', '--ocd-main-loop',
dest='setting__urls__ocd_main_loop',
metavar='OCD_MAIN_LOOP',
help='''Url/path from where the confirmation of
the enabling/disabling arrive.''', nargs='+')
title = 'Override options in the [allow_hetdex] section'
overrides_he = parser.add_argument_group(title=title)
overrides_he.add_argument('-N', '--n-ocd-allow-hetdex',
dest='setting__allow_hetdex__n_ocd_allow_hetdex',
metavar='N_OCD_ALLOW_HETDEX',
help='''Which of the urls for the
``ocd_allow_hetdex`` option of the ``[urls]``
section use to setup a ZMQ server''')
return subparsers
[docs]def allow_hetdex_cmd(args):
'''Entry point for enabling/disabling HETDEX shot execution.
Parameters
----------
args : :class:`~argparse.Namespace`
parsed command line arguments
'''
# load the configuration file
config = ocd_config.load_config(config_file=args.config, args=args)
# Setup the servers
utils.init_zmq_servers(args.subcommand, config)
send_listen = utils.SendAndListen.from_names('ocd_allow_hetdex',
'ocd_main_loop',
topics=states.HETDEX_CHANGED_TOPIC,
n_attempts=args.max_attempts,
interval=args.interval)
# create the outgoing event and the expected response
if args.action == 'start':
tcs_event_dict = {'action': states.HETDEX_TOGGLE.ENABLE}
response = {'new_state': states.HETDEX_CHANGED.ENABLED}
else:
tcs_event_dict = {'action': states.HETDEX_TOGGLE.DISABLE}
response = {'new_state': states.HETDEX_CHANGED.DISABLED}
print('Starting to make contact with OCD')
reply = send_listen.communicate(states.HETDEX_ALLOWED_TOPIC,
tcs_event_dict,
expected_topic=states.HETDEX_CHANGED_TOPIC,
expected_event=response)
returned_topic, returned_event, timeout_hit = reply
if timeout_hit:
print('WARNING: OCD has not replied back confirming that it acted'
' on my request. Are you sure that OCD is up and running?')
else:
msg = ('OCD confirms that now HETDEX is {}')
acted = 'allowed' if args.action == 'start' else 'not allowed'
print(msg.format(acted))