ImageBackIlluminatedFibers

Bases: KPFTranslatorFunction

Take images of the back illuminated fibers using the FVCs

Source code in kpf/engineering/ImageBackIlluminatedFibers.py
 20
 21
 22
 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
118
119
120
121
122
123
class ImageBackIlluminatedFibers(KPFTranslatorFunction):
    '''Take images of the back illuminated fibers using the FVCs
    '''
    @classmethod
    def pre_condition(cls, args, logger, cfg):
        pass

    @classmethod
    def perform(cls, args, logger, cfg):
        this_file_name = Path(__file__).name.replace('.py', '')
        log_path = Path(f'/s/sdata1701/KPFTranslator_logs/')
        images_file = log_path / Path(f'{this_file_name}_images.txt')
        hstnow = datetime.now()
        now_str = hstnow.strftime('%Y-%m-%d %H:%M:%S')

        LEDoutlets = {'Science': 'E7',
                      'Sky': 'E8',
                      'CaHK': 'J7',
                      'ExpMeter': 'H1'}
        LEDnames = {'Science': 'Science Back-Illumination LED',
                    'Sky': 'Sky Back-Illumination LED',
                    'CaHK': 'HK Back-Illumination LED',
                    'ExpMeter': 'Exp Meter Back Illum LED'}
        exptimes = {'SCI': {'Science': 0.1,
                            'Sky': 1,
                            'ExpMeter': 2},
                    'CAHK': {'CaHK': 5}
                    }
        kpffvc = ktl.cache('kpffvc')
        kpfpower = ktl.cache('kpfpower')
        kpffiu = ktl.cache('kpffiu')

        def take_back_illuminated_image(camera, LEDname):
            log.info(f"Taking back illuminated image of {LEDname} fiber with {camera} FVC")

            camnum = {'SCI': 1, 'CAHK': 2}[camera]
            powerkw = ktl.cache('kpfpower', f'KPFFVC{camnum}')
            if powerkw.read() == 'Off':
                FVCPower.execute({'camera': camera, 'power': 'on'})
                # Time shim to let FVC service connect to camera after power up
                time.sleep(10)

            # Set ADC to Null
            if camera == 'SCI':
                kpffiu['ADC1NAM'].write('Null')
                kpffiu['ADC2NAM'].write('Null')
                success1 = kpffiu['ADC1NAM'].waitFor("=='Null'", timeout=30)
                success2 = kpffiu['ADC2NAM'].waitFor("=='Null'", timeout=30)
                if success1 is False or success2 is False:
                    raise KPFException('Failed to reach Null position on science ADC')
            elif camera == 'CAHK':
                kpffiu['HKXNAM'].write('Null')
                kpffiu['HKYNAM'].write('Null')
                success1 = kpffiu['HKXNAM'].waitFor("=='Null'", timeout=30)
                success2 = kpffiu['HKYNAM'].waitFor("=='Null'", timeout=30)
                if success1 is False or success2 is False:
                    raise KPFException('Failed to reach Null position on HK ADC')

            # Turn LED on
            outlet = LEDoutlets[LEDname]
            if LEDnames[LEDname] != kpfpower[f"OUTLET_{outlet}_NAME"].read():
                raise KPFException(f"Expected outlet {outlet} to have name {LEDnames[LEDname]}")
            log.debug('Turning LED on')
            kpfpower[f"OUTLET_{outlet}"].write('On')
            # Take FVC Image
            SetFVCExpTime.execute({'camera': camera,
                                   'exptime': exptimes[camera][LEDname]})
            lastfile = TakeFVCExposure.execute({'camera': camera})
            log.info(f'  LASTFILE: {lastfile}')
            log.debug('Turning LED off')
            kpfpower[f"OUTLET_{outlet}"].write('Off')

            # Append to images file
            if images_file.exists() is False:
                # Write header line
                header = f"# HST date, camera, LED, file\n"
                with open(images_file, 'w') as f:
                    f.write(header)
            row = f"{now_str}, {camera:4s}, {LEDname:8s}, {lastfile}\n"
            with open(images_file, 'a') as f:
                f.write(row)

        if args.get('SCI', False) is True:
            take_back_illuminated_image('SCI', 'Science')
            take_back_illuminated_image('SCI', 'Sky')
            take_back_illuminated_image('SCI', 'ExpMeter')
        if args.get('CAHK', False) is True:
            take_back_illuminated_image('CAHK', 'CaHK')

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

    @classmethod
    def add_cmdline_args(cls, parser, cfg=None):
        parser.add_argument("--Science", "--Sci", "--science", "--sci", "--SCI",
                            dest="SCI",
                            default=False, action="store_true",
                            help="Image science and sky fibers with science FVC?")
        parser.add_argument("--CaHK", "--HK", "--cahk", "--hk", "--CAHK",
                            dest="CAHK",
                            default=False, action="store_true",
                            help="Image CaHK fiber with CaHK FVC?")
        return super().add_cmdline_args(parser, cfg)