#!/bin/tcsh -f

# ISIS flexure test script 
# Robert Greimel & Frank Gribbin 
#modified by Licandro on Feb28 to produce a shorter version

# 18-Feb-2005 Original for tests with Javier Licandro  

# check that we have the required arguments
if ( $#argv != 2 ) then
  echo "usage: $0 <red arm exposure time> <blue arm exposure time>"
  exit 1
endif

# for testing
if ( 0 ) then
  alias cmd echo cmd
  alias run echo run
endif

# loop over mount position angle
# start at zenith 
echo "Parking the telescope (put at alt 90)"  
cmd TCS PARK ZEN
@ previous_el = 90 
set cpa = 0
foreach pa (-90  0  90  180  -90)
  @ cpa++
  echo "moving to mount position angle $pa ($cpa of 5)"
  cmd TCS ROTATOR MOUNT "$pa"
  set cel = 0
  # loop over elevation
  foreach el (90  60  30 )
    @ cel++
    echo "moving to elevation $el ($cel of 3)"
    cmd TCS USER "ALT $el"

    # calculate delay 
    @ difference = $el - $previous_el
    # 1 degree/sec plus startup etc 
    if ($difference < 0) then 
       @ difference = - $difference
    endif  
    echo "difference " $difference 
    @ delay = $difference + 15       
    echo "wait for $delay seconds" 
    sleep $delay  
    @ previous_el = $el 

    # take data
    @ num = 3 * ($cpa - 1) + $cel
    echo "taking data ($num of 15)"
    run red $argv[1]  "flex red  $pa $el" &
    run blue $argv[2] "flex blue $pa $el" &
    # wait for data taking to be finished
    wait
  end
end
