#!/bin/csh -f
#+
# telfocloop -- acquire images over a range of secondary piston positions
#
# Purpose:
#	Acquire a series of DEIMOS images 
#	while stepping the telescope secondary 
#       focus values between the given endpoints
#
# Usage:
#	telfocloop <start> [<n> [incr]]
# 
# Arguments:
#	start = starting secondary position [mm]
#	n     = number of images to acquire (default=7)
#	incr  = increment in focus [mm] (default=0.1)
# 
# Output:
# 
# Restrictions:
# 
# Exit values:
#	0 = normal completion
#	1 = wrong number of arguments
#	3 = start and/or incr are not valid floats
#	4 = n too large
#
# Example:
#
#-
# Modification history:
#	Date unknown	RWG	Original version
#	2000-Jul-11	GDW	Added documentation
#       2002-Jun-06     GDW     Adapted for DEIMOS
#-----------------------------------------------------------------------

# define defaults...
set incr = 0.1
@ n = 7
@ n_max = 100

set buf = $0
set cmd = $buf:t
set usage = "Usage: $cmd start [n [incr]]"

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

# grab arguments...
if ( $#argv >= 1 ) then
    set start = $1
    shift
endif
if ( $#argv >= 1 ) then
    set n = $1
    shift
endif
if ( $#argv >= 1 ) then
    set incr = $1
    shift
endif

# validate args...
is_float $start $incr > /dev/null
if ( $status ) then
  echo "ERROR -- start and/or incr are not valid floats"
  exit 3
endif

if ( $n > $n_max ) then
  echo "ERROR --- n too large (max is $n_max)"
  exit 4
endif

# acquire data...
@ i = 0
while ( $i < $n )

    # compute new secondary position...
    set focus = `calc "$start + $i*$incr"

    # change secondary position...
    telfoc $focus

    # report new position...
    telfoc

    # acquire image(s) using the appropriate command...
    goi

    # increment...
    @ i ++
end
