#!/bin/csh -f
#+
# align_check -- check slitmask alignment between spectral exposures
#
# Purpose:
#	Tilt the grating to the zeroth-order imaging position and
#	acquire a direct image with the current filter to check
#	slitmask alignment in between spectral exposures.  If
#       started while an exposure is in progress, the script will
#       wait for the current exposure to finish before reconfiguring
#       the grating. The observer must run xbox in and IRAF session
#       on the resulting image to correct pointing.
#
#	The script reconfigures DEIMOS for spectroscopy immediately
#	after the image starts reading out on the assumption that only 
#	a small shift will be required to fix the alignment.  If a
#	significant shift or a significant rotation is recommended by
#	xbox, then the script should be re-run to verify alignment
#	following the move.
#
# Usage:
#	align_check [-n] [ttime]
# 
# Arguments:
#	ttime = exposure time for alignment check
# 
# Flags:
#	-n = noquery mode
# 
# Restrictions:
#	- Can only be used when slider 3 or 4 is in use
# 
# Procedure:
#	- Wait for any ongoing image to complete
#	- Configure DEIMOS for zeroth order imaging on the current slider
#	- Acquire a direct image of the specified integration time
#	  to check slitmask alignment and wait for readout to commence
#	- Reconfigure spectrograph to spectroscopy mode settings
#	- Verify proper reconfiguration
#	- Pause until observer has sent moves
#	- Wait for FCS to begin tracking
#	- Launch a new exposure
# 
# Time requirements:
#	- completes in 2:05, assuming 20 second exposure time
# 
# Exit values:
#	 0 = normal completion
#	 1 = wrong number of arguments
#
# Example:
#	1) Make an alignment check using default exposure time:
#		align_check
#
#	2) Make an alignment check using longer exposure time:
#		align_check 40
#-
# Modification history:
#	2002-Aug-12	GDW	Original version
#	2002-Aug-13	GDW	Extended to grating 4
#	2003-Mar-26	GDW	Added check for FCS tracking
#	2003-Apr-24	GDW	Modified for slider 4 zeroth order
#	2012-May-29	GDW	Added noquery option and put goi in background
#	2012-Dec-12	GDW	Wait for exposure to start before exit
#-----------------------------------------------------------------------

# prelims...
set buf = $0
set cmd = $buf:t
set usage = "Usage: $cmd [ttime]"
set query = 1

# set defaults...
set ttime = 20

# parse flags...
while ( $#argv > 0 )

  # check for -noquery flag...
  if ( "$1" =~ -n* ) then
    printf "NOQUERY MODE ENABLED\n"
    set query = 0
    shift
    continue
  endif

  # check for bad flag...
  if ( "$1" =~ -* ) then
    printf "[$cmd] ERROR: invalid option '$1' -- abort!\n\a"
    exit 1
  endif

  # exit flag check if no flags remain...
  break

end

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

# parse args...
if ( $#argv >= 1 ) then
    set ttime = $1
    shift
endif

cat <<EOF2
------------------------------------------------------------------------
		  Welcome to the ALIGN_CHECK script
------------------------------------------------------------------------

This script will:
	- Wait for any ongoing image to complete
	- Configure DEIMOS for zeroth-order imaging on the current slider
	- Acquire a $ttime sec direct image through the mask
	  to check alignment
	- Reconfigure spectrograph to spectroscopy mode settings
	- Verify proper reconfiguration
	- Pause until observer has sent moves
	- Wait for FCS to begin tracking
	- Launch a new exposure

EOF2

set command = "show -s deiccd -terse pauseip exposip"
set buf = (`$command`)
if ( $#buf == 2 ) then
    if ( $buf[1] != "false" || $buf[2] != "false" ) then
	printf "Waiting for current exposure to begin readout...\n"
    endif
else
    printf "WARNING: bad return from command:\n\t$command\n\a"
endif

# verify that no image is in progress...
waitfor -s deiccd pauseip = f
waitfor -s deiccd exposip = f

# save current instrument settings...
save_state -all -clobber -verbose
set old_object = `object`
set old_fcsmode = `show -s deifcs -terse fcsmode`

onintr abort

# set up instrument for zeroth-order imaging...
set slider = `slider`
if ( "$slider" == "3" || "$slider" == "4" ) then
    gozero
else
    printf "ERROR: we are not in spectroscopy mode!\n"
    exit 2
endif

# turn off the FCS so that it does not attempt to move during exposure...
modify -s deifcs fcsmode=Off

# configure detector parameters...
modify -s deiccd MOSMODE=Direct
tint $ttime
object Alignment check
goi -nowait

# restore instrument state...
@ fails = 0
while (1)
    restore_state -verify -debug
    if ( $status == 0 ) break
    @ fails++
    printf "[$cmd] WARNING: spectrograph state was not restored properly!\n"
    if ( $fails > 1 ) exit 3

    # query whether to try again...
    if ( $query == 1 ) then
	printf "Try again? (y/n) [y]: "
	set ans = $<
	if ( "$ans" == "" || "$ans" == "y" || "$ans" == "Y" ) then
	    printf "Trying again...\n"
	else
	    exit 3
	endif
    endif

end
object "$old_object"

# open the shutter to allow FCS to lock...
modify -s deifcs fcsmode=$old_fcsmode
modify -s deiccd cshutter=open

# exit here in noquery mode...
if ( ! $query ) then
    exit
endif

#  pause until FCS is working again...
cat <<EOF 

------------------------------------------------------------------------
	Press ENTER after your telescope moves have completed.
   I'll start the next exposure for you after FCS starts tracking.
------------------------------------------------------------------------

EOF

# wait for user response...
pause

printf "Waiting for FCS to track..."
wffcs

# expose...
printf "exposing!\n"
goi &

# wait for exposure to start, then exit...
waitfor -s deiccd exposip=t
exit

abort:
modify -s deifcs fcsmode=$old_fcsmode
exit 1
