kfcDisplay

Automatic display for Focal Plane Camera

usage: kfcDisplay: [-h, --help] displayName

-h , --help

show this help message and exit

displayName

Name of DS9 instance

#! @KPYTHON@

import ktl
import sys
import signal
import argparse
import time
import signal

from KCWI.Helper import setupMonitoring, checkInitialValues, say
from KCWI.KcwiDS9 import ds9

description = "Automatic display for Focal Plane Camera"
parser = argparse.ArgumentParser(description=description)
parser.add_argument("displayName",nargs="?",default="AutoDisplayFPC",help='Name of DS9 instance')

def sig_handler(signum,frame):
    say(str(signum)+" "+str(frame))
    say("Stop detected. Killing ds9 and stopping ")
    if process_name is not None:
        process_name.send_signal(9)
    sys.exit(0)

def autodisplayfpc(displayName):

    # start keyword monitoring

    server = 'kfcs'
    try:
        trigtime = ktl.cache(server,'TRIGTIME')
        sequence = ktl.cache(server,'SEQUENCE')
	ttime = ktl.cache(server,'EXPTIME')
        startex = ktl.cache(server,'STARTEX')
        loutfile = ktl.cache(server,'LASTFILE')
    except:
        raise RuntimeError("Failed to read detector keywords. KBDS might not be running")

    DS9 = ds9(displayName)
    DS9.zoom = "to fit"
    global process_name
    process_name = DS9.process_name
    initializeZoom = 1 



    monitoredKeywords = (ttime, startex,loutfile,trigtime,sequence)

    setupMonitoring(monitoredKeywords, wait=True)

    # infinite loop
    while True:
      # wait for STARTEX to be 1
      say("Waiting for new exposure")
      # this while True loop is necessary to handle CTRL+C signals
      # otherwise they get ignored
      while True:
              result = startex.waitFor('==1',timeout=2)
              if result==True:
                      break

      # what is the exposure time
      exposureTime = ttime
      numberOfExposures = sequence
      triggerTime = trigtime

      waitTime = (exposureTime+trigtime)*numberOfExposures

      # wait for exposure
      time.sleep(waitTime+2)

      # display
      zoomflag = DS9.zoom
      say("Displaying image..."+str(loutfile['ascii']))
      DS9.open(loutfile['ascii'], 1)

      # deal with zoom parameters
      if zoomflag == "to fit" and initializeZoom:
        DS9.xpaset("zoom to fit")
        initializeZoom = 0
        # change the value so that on subsequent displays we
        # do not override the zoom factor set by the
        # observer...
      if not initializeZoom:
        DS9.zoom = None
      # wait 5 second before checking for a new exposure to avoid race condition
      time.sleep(2)


if __name__ == '__main__':
   args = parser.parse_args()
   signal.signal(signal.SIGINT,sig_handler)
   signal.signal(signal.SIGILL,sig_handler)
   signal.signal(signal.SIGTERM,sig_handler)
   autodisplayfpc(args.displayName)