kcwiDisplayb
Automatic display for Science Camera
usage: kcwiDisplayb: [-h, --help] displayName
#! @KPYTHON@
import ktl
import sys
import argparse
import time
import signal
from KCWI.Helper import setupMonitoring, checkInitialValues, say
from KCWI.KcwiDS9 import ds9
description = "Automatic display for Science Camera"
parser = argparse.ArgumentParser(description=description)
parser.add_argument("displayName",nargs="?",default="AutoDisplayB",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 autodisplayb(displayName):
# start keyword monitoring
server = 'kbds'
try:
outdir = ktl.cache(server,'OUTDIR')
exposeip = ktl.cache(server,'EXPOSIP')
rdoutip = ktl.cache(server,'RDOUTIP')
ttime = ktl.cache(server,'TTIME')
ltodisk = ktl.cache(server,'LTODISK')
outfile = ktl.cache(server,'OUTFILE')
frameno = ktl.cache(server,'FRAMENO')
startex = ktl.cache(server,'STARTEX')
loutfile = ktl.cache(server,'LOUTFILE')
except:
say("KcwiDisplayb cannot start because KBDS keywords cannot be reached")
raise RuntimeError("Failed to read detector keywords. KBDS might not be running")
# Start ds9
DS9 = ds9(displayName)
DS9.zoom = "to fit"
global process_name
process_name = DS9.process_name
initializeZoom = 1
monitoredKeywords = (outdir,exposeip,rdoutip,ltodisk,outfile,frameno,loutfile,startex, ttime)
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
# wait for exposure to start
exposeip.waitFor('==1',timeout=20)
say("Exposing")
#
cond = "!='"+loutfile['ascii']+"'"
# what is the exposure time
exposureTime = ttime
# wait for readout
rdoutip.waitFor('==1',timeout=exposureTime+10)
say("Reading out")
# wait for readout
rdoutip.waitFor('==0',timeout=250)
say("Readout complete")
# wait for last file to change
loutfile.waitFor(cond, timeout=30)
say('Last file is on disk and it is: '+loutfile['ascii'])
time.sleep(2)
# display
zoomflag = DS9.zoom
say("Displaying image..."+str(loutfile.read()))
DS9.open(loutfile.read(), 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)
autodisplayb(args.displayName)