CleanupAfterCalibrations

Bases: KPFScript

Script which cleans up after OBs with calibrations.

Parameters:
  • leave_lamps_on (bool) –

    Leave calibration lamps on when done?

  • OB (ObservingBlock) –

    A valid observing block (OB).

KTL Keywords Used:

  • kpfconfig.USEAGITATOR
  • kpf_expmeter.USETHRESHOLD

Functions Called: - kpf.calbench.CalLampPower - kpf.calbench.IsCalSourceEnabled - kpf.calbench.SetLFCtoStandbyHigh - kpf.fiu.ConfigureFIU - kpf.spectrograph.SetObject - kpf.spectrograph.StopAgitator - kpf.spectrograph.WaitForL0File - kpf.spectrograph.WaitForReady - kpf.scripts.SetTargetInfo

Source code in kpf/scripts/CleanupAfterCalibrations.py
 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
class CleanupAfterCalibrations(KPFScript):
    '''Script which cleans up after OBs with calibrations.

    Args:
        leave_lamps_on (bool): Leave calibration lamps on when done?
        OB (ObservingBlock): A valid observing block (OB).

    KTL Keywords Used:

    - `kpfconfig.USEAGITATOR`
    - `kpf_expmeter.USETHRESHOLD`

    Functions Called:
    - `kpf.calbench.CalLampPower`
    - `kpf.calbench.IsCalSourceEnabled`
    - `kpf.calbench.SetLFCtoStandbyHigh`
    - `kpf.fiu.ConfigureFIU`
    - `kpf.spectrograph.SetObject`
    - `kpf.spectrograph.StopAgitator`
    - `kpf.spectrograph.WaitForL0File`
    - `kpf.spectrograph.WaitForReady`
    - `kpf.scripts.SetTargetInfo`
    '''
    @classmethod
    def pre_condition(cls, args, OB=None):
        pass

    @classmethod
    def perform(cls, args, OB=None):
        if isinstance(OB, dict):
            OB = ObservingBlock(OB)
        calibrations = OB.Calibrations
        log.info('-------------------------')
        log.info(f"Running {cls.__name__}")
        for i,calibration in enumerate(calibrations):
            log.debug(f"Calibration {i+1}/{len(calibrations)}")
            for key in calibration.to_dict():
                log.debug(f"  {key}: {calibration.get(key)}")
        log.info('-------------------------')

        SCRIPTMSG = ktl.cache('kpfconfig', 'SCRIPTMSG')
        SCRIPTMSG.write('')

        # Power off lamps
        if args.get('leave_lamps_on', False) == True:
            log.info('Not turning lamps off because leave_lamps_on option was invoked')
        else:
            lamps = set([c.get('CalSource') for c in calibrations])
            for lamp in lamps:
                if IsCalSourceEnabled.execute({'CalSource': lamp}) == True:
                    if lamp in ['Th_daily', 'Th_gold', 'U_daily', 'U_gold',
                                'BrdbandFiber', 'WideFlat']:
                        CalLampPower.execute({'lamp': lamp, 'power': 'off'})
                    if lamp == 'LFCFiber':
                        try:
                            SetLFCtoStandbyHigh.execute({})
                        except Exception as e:
                            log.error('SetLFCtoStandbyHigh failed')
                            log.error(e)
                            try:
                                SendEmail.execute({'Subject': 'ExecuteCals Failed',
                                                   'Message': f'{e}'})
                            except Exception as email_err:
                                log.error(f'Sending email failed')
                                log.error(email_err)

        runagitator = ktl.cache('kpfconfig', 'USEAGITATOR').read(binary=True)
        if runagitator is True:
            StopAgitator.execute({})

        FIUdest = args.get('FIUdest', 'Stowed')
        log.info(f"Sending FIU to {FIUdest}")
        ConfigureFIU.execute({'mode': FIUdest})

        # Turn off exposure meter controlled exposure
        log.debug('Clearing kpf_expmeter.USETHRESHOLD')
        USETHRESHOLD = ktl.cache('kpf_expmeter', 'USETHRESHOLD')
        USETHRESHOLD.write('No')

        # Set OBJECT back to empty string
        log.info('Waiting for readout to finish')
        WaitForReady.execute({})
        SetObject.execute({'Object': ''})

        # Clear target info
        SetTargetInfo.execute({})

        # Write L0 file name to log if can
        WaitForL0File.execute({})

    @classmethod
    def post_condition(cls, args, OB=None):
        pass

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