#!/bin/csh -f
#+
# goifcs -- acquire DEIMOS flexure compensation system image(s)
#
# Purpose:
#	Take one or more images with the DEIMOS FCS detector, using the
#	current instrument settings
#
# Usage:
#	goifcs [-nowait] [-sleep nsleep] [nexp]
# 
# Arguments:
#	-nowait	= return before readout of last image in loop
#	-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
#
# Example:
#	1) Take a single DEIMOS exposure:
#		goifcs
#
#	2) Take 10 DEIMOS exposures:
#		goifcs 10
#
#	3) Take 10 DEIMOS exposures, and exit when the final one
#	begins to transfer to polo:
#		goifcs -nowait 10
# 
# 	4) Take 5 exposures and pause 30 sec in between:
#		goifcs -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-Feb-05	GDW	Adapted for DEIMOS FCS
#-----------------------------------------------------------------------

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

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

# flag check...
while (1)

    # check optional argument...
    if ( "$1" == "-nowait" ) then
	set nowait = 1
	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

onintr last

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

    # ensure no exposure is in progress by checking all 
    # exposure-related keywords until they are all false on 
    # 2 consecutive attemps...
    @ m=0
    while ( $m < 2 )
	@ okay=1
	set buf = ( `show -terse -s deifcs pauseip eraseip exposip wcrate` )
	foreach word ( $buf )
	    if ( "$word" != "false" ) then
		@ okay=0
		break
	    endif
	end
	if ( $okay) @ m++
	if ( $m < 2 ) sleep 1
    end

    # type out the starting time for benefit of the user...
    if ( $n == 1 ) date

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

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

    # begin exposure...
    printf "  Exposing..."
    modify -s deifcs 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 deifcs wcrate=true
    printf "reading out..."
    if ( $nowait && $n == $nexp ) then
	printf "done (last read skipped).\n"
	break
    endif
    waitfor -s deifcs wcrate=false
    printf "done.\n"

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

    # increment counter...
    @ n++

end   

# restore proper operation of shutter...
last:
printf "  Exposure sequence completed.\n"
exit $stat