StartGUIs

Bases: KPFFunction

Start KPF GUIs

Source code in kpf/utils/StartGUIs.py
 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
class StartGUIs(KPFFunction):
    '''Start KPF GUIs

    '''
    @classmethod
    def pre_condition(cls, args):
        pass

    @classmethod
    def perform(cls, args):
        # Get DISPLAY varibales
        env = os.environ
        uidisp = {}
        kvncstatus_proc = subprocess.run(['kvncstatus'], env=env,
                                         stdout=subprocess.PIPE)
        kvncstatus = Table.read(kvncstatus_proc.stdout.decode(), format='ascii')
        username = os.getlogin()
        display = {'control0':  kvncstatus[kvncstatus['Desktop'] == f'kpf-{username}-control0']['Display'][0],
                   'control1':  kvncstatus[kvncstatus['Desktop'] == f'kpf-{username}-control1']['Display'][0],
                   'control2':  kvncstatus[kvncstatus['Desktop'] == f'kpf-{username}-control2']['Display'][0],
                   'telstatus':  kvncstatus[kvncstatus['Desktop'] == f'kpf-{username}-telstatus']['Display'][0],
                   }

        # Start GUIs if needed
        for GUI in GUI_list:
            # Find out of GUIs are Running
            log.debug(f"Setting DISPLAY to kpf{display[GUI['display']]}")
            env['DISPLAY'] = f"kpf{display[GUI['display']]}"
            window_names = get_window_list(env=env)
            GUIname = GUI['name']
            if GUIname not in window_names and args.get('position_only', False) is False:
                instrume = ktl.cache('dcs1', 'INSTRUME')
                if GUIname == 'MAGIQ - Observer UI: KPF on Keck1' and instrume.read() != 'KPF':
                    log.info(f'Selected instrument is not KPF, not starting magiq')
                    success = False
                else:
                    log.info(f"Starting '{GUIname}' GUI")
                    gui_proc = subprocess.Popen(GUI['cmd'], env=env,
                                                stdout=subprocess.PIPE,
                                                stderr=subprocess.PIPE)
                    success = waitfor_window_to_appear(GUIname, env=env)
                    if success is False:
                        log.error(f'{GUIname} did not come up')
                        stdout, stderr = gui_proc.communicate()
                        log.error(f"STDERR: {stderr.decode()}")
                        log.error(f"STDOUT: {stdout.decode()}")
            else:
                log.info(f"Existing '{GUIname}' window found")
                success = True
            time.sleep(2)
            if GUI.get('position', None) is not None and success is True:
                log.info(f"Positioning '{GUIname}' GUI")
                wmctrl_cmd = ['wmctrl', '-r', f'"{GUIname}"', '-e', GUI['position']]
                log.debug(f"  Running: {' '.join(wmctrl_cmd)}")
                wmctrl_proc = subprocess.run(' '.join(wmctrl_cmd), env=env, shell=True)
                if GUI['cmd'][0] == 'xterm':
                    xterm_title = GUI['cmd'][2]
                    success = waitfor_window_to_appear(xterm_title, env=env)
                    log.info(f"Minimizing '{xterm_title}'")
                    wmctrl_cmd = ['wmctrl', '-r', xterm_title, '-b', 'add,hidden']
                    wmctrl_proc = subprocess.run(' '.join(wmctrl_cmd), env=env, shell=True)
            if GUIname == 'SAOImage kpfds9':
                # Configure ds9 initial color maps and scaling
                cmaps = {'1': 'cool', '2': 'green', '3': 'heat'}
                for frameno in cmaps.keys():
                    xpaset_cmds = [['xpaset', '-p', 'kpfds9', 'frame', 'frameno', f'{frameno}'],
                                   ['xpaset', '-p', 'kpfds9', 'cmap', f'{cmaps[frameno]}'],
                                   ['xpaset', '-p', 'kpfds9', 'scale', '99.5']]
                    for xpaset_cmd in xpaset_cmds:
                        xpa_proc = subprocess.Popen(xpaset_cmd,
                                                    stdout=subprocess.PIPE,
                                                    stderr=subprocess.PIPE)
                        time.sleep(1)

    @classmethod
    def post_condition(cls, args):
        pass

    @classmethod
    def add_cmdline_args(cls, parser):
        parser.add_argument("--position", "-p",
                            dest="position_only",
                            default=False, action="store_true",
                            help="Only position the GUIs, do not start")
        return super().add_cmdline_args(parser)