#!/bin/csh -f
#+
# abortex -- kill any ongoing exposure
#
# Purpose:
#	Immediately stop any exposure on DEIMOS science array.  Reset
#	all CCD flags to the nominal state (false).
#
# Usage:
#	abort
# 
# Arguments:
#	None
# 
# Output:
#	None
# 
# Restrictions:
#	None
# 
# Exit values:
#	 0 = normal completion
#	 1 = wrong number of arguments
#	 2 = abort failed
#
# Example:
#	1) Abort the current exposure:
#		abortex
#-
# Modification history:
#	2002-Mar-06	GDW	Original version
#	2002-Jun-06	GDW	Adapted for DEIMOS
#-----------------------------------------------------------------------

# set defaults...
set lib = deiccd
set buf = $0
set cmd = $buf:t
set usage = "Usage: $cmd"

# verify args...
if ( $#argv > 0 ) then
  printf "$usage\n"
  exit 1
endif

# check exposure status...
set buf = `show -s $lib -terse eraseip exposip`
set eraseip = $buf[1]
set exposip = $buf[2]

# can't abort during erase, so wait until erase completes...
if ( "$eraseip" != "false" ) then
    printf "Aborting (after erase)..."
    waitfor -s $lib -t 10 eraseip=false
    sleep 1
    set exposip = `show -s $lib -terse exposip`
endif

# can't stop if no exposure in progress...
if ( "$exposip" == "false" ) then
    printf "WARNING: no exposure is in progress; nothing to abort\n"
    exit 0
endif

# stop the exposure...
printf "Aborting DEIMOS exposure..."
modify -s $lib abortex=1 silent
if ( $status != 0 ) then
    printf "ERROR: abort failed\n"
    exit 2
endif

# reset ccd flags...
printf "done.\n"
exit
