RunCalOB

Bases: KPFTranslatorFunction

Script to run a full Calibration 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/RunCalOB.py
 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
class RunCalOB(KPFTranslatorFunction):
    '''Script to run a full Calibration 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_cal'])
        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__}")
        for key in OB:
            if key not in ['SEQ_Darks', 'SEQ_Calibrations']:
                log.debug(f"  {key}: {OB[key]}")
            else:
                log.debug(f"  {key}:")
                for entry in OB[key]:
                    log.debug(f"    {entry}")
        log.info('-------------------------')

        # Configure: Turn on Lamps
        try:
            ConfigureForCalibrations.execute(OB)
        except ExistingScriptRunning as e:
            log.error('ExistingScriptRunning')
            sys.exit(1)
        except Exception as e:
            log.error('ConfigureForCalibrations Failed')
            log.error(e)
            traceback_text = traceback.format_exc()
            log.error(traceback_text)
            # Email error to kpf_info
            if not isinstance(e, ScriptStopTriggered):
                try:
                    msg = [f'{type(e)}',
                           f'{traceback_text}',
                           '',
                           f'{OB}']
                    SendEmail.execute({'Subject': 'ConfigureForCalibrations Failed',
                                       'Message': '\n'.join(msg)})
                except Exception as email_err:
                    log.error(f'Sending email failed')
                    log.error(email_err)
            log.error('Running CleanupAfterCalibrations and exiting')
            CleanupAfterCalibrations.execute(OB)
            raise e

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

        # Execute the Dark Sequence
        try:
            darks = OB.get('SEQ_Darks', [])
            if len(darks) > 0:
                log.info(f"Setting source select shutters")
                SetSourceSelectShutters.execute({}) # No args defaults all to false
                log.info(f"Setting timed shutters")
                SetTimedShutters.execute({}) # No args defaults all to false
                log.info(f"Setting OCTAGON to Home position")
                SetCalSource.execute({'CalSource': 'Home'})
                log.info(f"Setting FlatField Fiber position to 'Blank'")
                SetFlatFieldFiberPos.execute({'FF_FiberPos': 'Blank'})
            for dark in darks:
                # Wrap in try/except so that cleanup happens
                dark['Template_Name'] = 'kpf_dark'
                dark['Template_Version'] = OB['Template_Version']
                ExecuteDark.execute(dark)
        except Exception as e:
            log.error("ExecuteDarks failed:")
            log.error(e)
            traceback_text = traceback.format_exc()
            log.error(traceback_text)
            # Email error to kpf_info
            if not isinstance(e, ScriptStopTriggered):
                try:
                    msg = [f'{type(e)}',
                           f'{traceback_text}',
                           '',
                           f'{OB}']
                    SendEmail.execute({'Subject': 'ExecuteDarks Failed',
                                       'Message': '\n'.join(msg)})
                except Exception as email_err:
                    log.error(f'Sending email failed')
                    log.error(email_err)
            # Cleanup
            log.error('Running CleanupAfterCalibrations and exiting')
            CleanupAfterCalibrations.execute(OB)
            sys.exit(1)

        # Execute the Cal Sequence
        try:
            cals = OB.get('SEQ_Calibrations', [])
            for cal in cals:
                cal['TriggerCaHK'] = OB['TriggerCaHK']
                cal['TriggerGreen'] = OB['TriggerGreen']
                cal['TriggerRed'] = OB['TriggerRed']
                # No need to specify TimedShutter_CaHK in OB/calibration
                cal['TimedShutter_CaHK'] = OB.get('TriggerCaHK', False)
                log.debug(f"Automatically setting TimedShutter_CaHK: {cal['TimedShutter_CaHK']}")
                cal['Template_Name'] = 'kpf_lamp'
                cal['Template_Version'] = OB['Template_Version']
                cal['nointensemon'] = OB.get('nointensemon', False)
                cal['leave_lamps_on'] = OB.get('leave_lamps_on', False) # Only needed for LFC
                ExecuteCal.execute(cal)
        except Exception as e:
            log.error("ExecuteCal failed:")
            log.error(e)
            traceback_text = traceback.format_exc()
            log.error(traceback_text)
            # Email error to kpf_info
            if not isinstance(e, ScriptStopTriggered):
                try:
                    msg = [f'{type(e)}',
                           f'{traceback.format_exc()}',
                           '',
                           f'{OB}']
                    SendEmail.execute({'Subject': 'ExecuteCals Failed',
                                       'Message': '\n'.join(msg)})
                except Exception as email_err:
                    log.error(f'Sending email failed')
                    log.error(email_err)
            # Cleanup
            log.error('Running CleanupAfterCalibrations and exiting')
            CleanupAfterCalibrations.execute(OB)
            sys.exit(1)

        # Cleanup: Turn off lamps
        try:
            CleanupAfterCalibrations.execute(OB)
        except Exception as e:
            log.error("CleanupAfterCalibrations failed:")
            log.error(e)
            traceback_text = traceback.format_exc()
            log.error(traceback_text)
            clear_script_keywords()
            # Email error to kpf_info
            try:
                msg = [f'{type(e)}',
                       f'{traceback_text}',
                       '',
                       f'{OB}']
                SendEmail.execute({'Subject': 'CleanupAfterCalibrations Failed',
                                   'Message': '\n'.join(msg)})
            except Exception as email_err:
                log.error(f'Sending email failed')
                log.error(email_err)

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

    @classmethod
    def add_cmdline_args(cls, parser, cfg=None):
        parser.add_argument('--leave_lamps_on', dest="leave_lamps_on",
                            default=False, action="store_true",
                            help='Leave the lamps on after cleanup phase?')
        parser.add_argument('--nointensemon', dest="nointensemon",
                            default=False, action="store_true",
                            help='Skip the intensity monitor measurement?')
        return super().add_cmdline_args(parser, cfg)