RunSciOB

Bases: KPFTranslatorFunction

Script to run a full Science OB from the command line.

This must have arguments as input, typically from a file using the -f command line tool.

This script is abortable. When .abort_execution() is invoked, the kpconfig.SCRIPTSTOP is set to Yes. This script checked for this value at various locations in the script. As a result, the script will not stop immediately, but will stop when it reaches a breakpoint.

ARGS:

  • OB - dict A fully specified observing block (OB).
Source code in kpf/scripts/RunSciOB.py
 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
class RunSciOB(KPFTranslatorFunction):
    '''Script to run a full Science OB from the command line.

    This must have arguments as input, typically from a file using the `-f`
    command line tool.

    This script is abortable.  When `.abort_execution()` is invoked, the
    `kpconfig.SCRIPTSTOP` is set to Yes.  This script checked for this value at
    various locations in the script.  As a result, the script will not stop
    immediately, but will stop when it reaches a breakpoint.

    ARGS:
    =====
    * __OB__ - `dict` A fully specified observing block (OB).
    '''
    abortable = True

    @classmethod
    def pre_condition(cls, OB, logger, cfg):
        check_input(OB, 'Template_Name', allowed_values=['kpf_sci'])
        check_input(OB, 'Template_Version', version_check=True, value_min='0.5')

    @classmethod
    @add_script_log(Path(__file__).name.replace(".py", ""))
    def perform(cls, OB, logger, cfg):
        log.info('-------------------------')
        log.info(f"Running {cls.__name__}")
        log.info('-------------------------')

        check_scriptstop()

        # Configure: 
        log.info(f"Configuring for Acquisition")
        ConfigureForAcquisition.execute(OB)
        WaitForConfigureAcquisition.execute(OB)

        check_scriptstop()

        log.debug('Asking for user input')
        print()
        print("###############################################################")
        print("    Before continuing, please ensure that the OA has placed")
        print("    the star on the KPF PO and they have initiated tip tilt")
        print("    corrections (if desired).")
        print()
        print("    Press 'Enter' to begin exposure(s) or 'a' to abort script")
        print("###############################################################")
        print()
        user_input = input()
        log.debug(f'response: "{user_input}"')
        if user_input.lower() in ['a', 'abort', 'q', 'quit']:
            raise KPFException("User chose to halt execution")

        check_scriptstop()

        log.info(f"Configuring for Science")
        ConfigureForScience.execute(OB)
        WaitForConfigureScience.execute(OB)

        check_script_running()
        set_script_keywords(Path(__file__).name, os.getpid())

        # Execute the Sci Sequence
        #   Wrap in try/except so that cleanup happens
        observations = OB.get('SEQ_Observations', [])
        for observation in observations:
            observation['Template_Name'] = 'kpf_sci'
            observation['Template_Version'] = OB['Template_Version']
            log.debug(f"Automatically setting TimedShutter_CaHK: {OB['TriggerCaHK']}")
            observation['TimedShutter_CaHK'] = OB['TriggerCaHK']
            observation['TriggerCaHK'] = OB['TriggerCaHK']
            observation['TriggerGreen'] = OB['TriggerGreen']
            observation['TriggerRed'] = OB['TriggerRed']
            # Note: pyyaml resolver currently converts off string to False boolean
            observation['TriggerGuide'] = True
            observation['Gmag'] = OB['Gmag']
            try:
                ExecuteSci.execute(observation)
            except Exception as e:
                log.error("ExecuteSci failed:")
                log.error(e)
                traceback_text = traceback.format_exc()
                log.error(traceback_text)
                # Cleanup
                clear_script_keywords()
                log.error('Running CleanupAfterScience and exiting')
                CleanupAfterScience.execute(OB)
                sys.exit(1)

        # Cleanup
        CleanupAfterScience.execute(OB)

    @classmethod
    def post_condition(cls, OB, logger, cfg):
        pass