#!/bin/csh -f
#+
# goi -- acquire DEIMOS image(s)
#
# Purpose:
#	Take one or more images with the DEIMOS detector, using the
#	current instrument settings
#
# Usage:
#	goi [-quiet] [-nowait] [-dark] [-sleep nsleep] [nexp]
# 
# Arguments:
#	-quiet   = suppress certain output
#	-nowait	= return before readout of last image in loop
#	-dark	= take DARK exposure (shutter does not open)
#	-sleep	= sleep for the specified number of seconds on each iteration
#	 nexp	= number of exposure to take (default=1)
# 
# Output:
#	Text feeback is sent to the terminal.
# 
# Exit values:
#	 0 = normal completion
#	 1 = wrong number of arguments
#	 2 = error during exposure
# Note:
#	A challenge here is to avoid race conditions.  The normal sequence of
#	keyword transitions at the end of the exposure is:
#		WCRATE=true
#		WSERV=true
#		WCRATE=false
#		WSERV=false
#		WDISK=true
#		WDISK=false
#	However, I have seen cases in which WSERV stays true until 
#	WDISK has made BOTH transitions:
#		WCRATE=true
#		WSERV=true
#		WCRATE=false
#		WDISK=true
#		WDISK=false
#		WSERV=false
#	In such cases, the script will hang waiting to check for WDISK=true.
#	To prevent it from hanging for long, we put a 5 sec timeout on
#	this waitfor.  Crude but effective.
#
# EXAMPLE:
#	1) Take a single DEIMOS exposure:
#		goi
#
#	2) Take 10 DEIMOS exposures:
#		goi 10

#	3) Take 10 DEIMOS exposures, and exit when the final one
#	begins to transfer to polo:
#		goi -nowait 10
# 
# 	4) Take 5 exposures and pause 30 sec in between:
#		goi -sleep 30 5
#
#-
# Modification history:
#	1999-Feb-24	GDW	Original version
#	2001-Apr-23	GDW	Added timeout on wcrate=F to prevent hangups
#	2001-May-02	GDW	Added check for return status on m expose=t
#	2001-May-07	GDW	Changed logic in datataking loop
#	2002-Jun-06	GDW	Adapted for DEIMOS
#	2002-Aug-11	GDW	Included wait for "wdisk" to be true;
#				Removed "timeout"
#	2002-Aug-26	GDW	Included waitfor wserv=false
#	2002-Sep-15	GDW	Added -sleep
#	2002-Nov-02	RIK	Add check for WSERV going true
#	2003-Aug-21	GDW	Added loop on pre-exposure checks
#	2006-Feb-03	GDW	Increased timeout on wfd from 15 to 60 sec
#	2009-Mar-30	GDW	Make -wfd the default
#	2013-Nov-30	GDW	Decreased timeout on wfd from 60 sec to 5.
#				See note above.
#	2014-Jun-17	GDW	Add -quiet flag
#	2014-Jun-30	GDW	Changed timeout on wdisk from 5 to 10 
#				because we were seeing cases in which
#				it would pause up to 10 sec.
#-----------------------------------------------------------------------

# define defaults...
@ nexp = 1
set nowait = 0
set autoshut = 1
set sleep = 0
set msg = ""
set stat = 0
set wfd = 1
set quiet = 0

set buf = $0
set cmd = $buf:t
set usage = "Usage: $cmd [-quiet] [-nowait] [-nowfd] [-dark] [-sleep n] [nexp]"

# flag check...
while (1)

    # check for "quiet" option...
    if ( "$1" =~ -q* ) then
	set quiet = 1
	shift
	continue
    endif

    # check optional argument...
    if ( "$1" == "-nowait" ) then
	set nowait = 1
	shift
	continue
    endif

    # check optional argument...
    if ( "$1" == "-dark" ) then
	set autoshut = 0
	set msg = "DARK "
	shift
	continue
    endif

    # check optional argument...
    if ( "$1" == "-nowfd" ) then
	set wfd = 0
	shift
	continue
    endif

    # note that "wfd" is now default, so skip it if found...
    if ( "$1" == "-wfd" ) then
	shift
	continue
    endif

    # check optional argument...
    if ( "$1" == "-sleep" ) then
	shift
	if ( $#argv < 1 ) then
	    printf "$usage\n"
	    exit 1
	endif
	@ sleep = $1
	shift
	continue
    endif

    break
end

# parse arguments...
if ( $#argv > 1 ) then
    printf "$usage\n"
    exit 1
endif
if ( $#argv >= 1 ) @ nexp = $1

# type out the starting time for benefit of the user...
date

# save current shutter mode for later restoration...
set autoshut_old = `show -s deiccd -terse autoshut`
modify -s deiccd silent autoshut=$autoshut
onintr last

# loop over number of exposures...
@ n=1
while ( $n <= $nexp )

    # ensure no exposure is in progress.  NOTE: we repeat these several times because 
    # a race condition may occur...
    @ m=1
    while ( $m <= 3 )
	waitfor -s deiccd pauseip=false
	waitfor -s deiccd exposip=false
	waitfor -s deiccd wcrate=false
	waitfor -s deiccd wserv=false
	@ m++
    end

    # print message...
    set nextimage = `nextimage -tail`
    printf "  %sExposure %d/%d Image %s\n" "$msg" $n $nexp $nextimage

    # get exposure time...
    @ ttime = `show -s deiccd -terse ttime`

    # begin exposure...
    if ( $quiet == 0 ) then
	printf "  Exposing..."
    endif

    modify -s deiccd silent expose=t wait
    if ( $status ) then
	printf "ERROR trying to start exposure\n"
	set stat = 1
	break
    endif

    # wait for end of exposure...
    waitfor -s deiccd wcrate=true
    if ( $quiet == 0 ) then
	printf "reading out..."
    endif
    waitfor -s deiccd wserv=true
    if ( $quiet == 0 ) then
	printf "transferring..."
    endif
    if ( $nowait && $n == $nexp ) then
	if ( $quiet == 0 ) then
	    printf "done (last read skipped).\n"
	endif
	break
    endif
    waitfor -s deiccd wcrate=false
    waitfor -s deiccd wserv=false

    # wait for disk write if desired...
    if ( $wfd ) then
	waitfor -s deiccd -t 10 wdisk=true
	if ( $quiet == 0 ) then
	    printf "writing to disk..."
	endif
	waitfor -s deiccd wdisk=false
    endif

    # complete output...
    printf "done.\n"

    # sleep if desired...
    if ( $sleep > 0 && $n < $nexp ) then
	if ( $quiet == 0 ) then
	    printf "  Sleeping $sleep seconds"
	    sleepdots $sleep
	endif
    endif

    # increment counter...
    @ n++

end   

# restore proper operation of shutter...
last:
modify -s deiccd silent autoshut=$autoshut_old
printf "  Exposure sequence completed.\n"
exit $stat
