#!/bin/tcsh -ef

#
# ABOUT   : A script which finds the rotation centre for both
#           NOTCam cameras (WF and HR). The user must first go
#           to a focus star which is high in the sky (zenith 
#           distance < 30 degrees).
#          
#
# USAGE   : notcam.rotation-centre [-e] exptime
#           -e      : Echo script on stdout, do not execute
#           exptime : Useful exptime for WF camera
#
#
# DEPENDS : notcam.setup-ima
#           notcam.object
#           notcam.expose
#           notcam.teloffset
#           tcs.focus-delta
#           tcs.ag-off
#           tcs.field-rotation 
#
# PROVIDES: notcam.rotation-centre
#
# TFUNCT  : ?
#
# AUTHOR  : Anlaug Amanda Djupvik
#
# HISTORY : 2009/09/09 Initial release, AAD
#           2010/03/15 Updated script to account for new setup-ima
#                      script which also sets focus-delta, AAD     
#           2011/04/13 Added script parameters to talker log, AAD

if ("$1" == "-e") then
  set e = "echo"
  shift
else
  set e = ""
endif

# Get command line arguments
set exp = `echo "$1" | sed -n -e '/^[0-9.]*$/p'`

# Adjust up exptime for HR camera
set exp2 = `echo "2*$exp" | bc -l | awk '{printf ("%5i"),$1}'`

# Check for errors in arguments
if ($exp == '') then
echo "Error in arguments"
echo "Usage: notcam.rotation-centre exp \n"
echo "       exp = exptime (in seconds) for WF camera \n"
exit 1
endif

# Write to Talker
logger -p local0.debug -t "$0" "[NOTE]: Script started as $0 $*"
#logger -p local0.debug -t "notcam.rotation-centre" "[NOTE]: script started"

# Execute or Echo script
echo "\n"
echo "\e[00;31mMake sure your field-r = -90 and your focus star is high in the sky!\e[00m"
echo "\e[00;31mHit return if OK. Otherwise Ctrl-C.\e[00m"
set answ = $<

#$e tcs.ag-off
#$e tcs.field-rotation -90

$e notcam.setup-ima wf 201
# Below line not needed as setup-ima changes focus-delta
#$e tcs.focus-delta -75
$e notcam.object "WF field-r=-90"
$e notcam.expose $exp
$e tcs.ag-off
$e tcs.field-rotation 90

echo "\n"
echo "\e[00;31mRotator is turning 180 degrees to FRot=+90!\e[00m"
echo "\e[00;31mHit return when tracking!\e[00m"
set answ = $<

$e notcam.object "WF field-r=+90"
$e notcam.expose $exp
$e notcam.setup-ima hr 201
$e notcam.object "HR field-r=+90"
$e notcam.expose $exp2
$e tcs.field-rotation -90

echo "\n"
echo "\e[00;31mRotator is turning 180 degrees to FRot=-90!\e[00m"
echo "\e[00;31mHit return when tracking!\e[00m"
set answ = $<

$e notcam.object "HR field-r=-90"
$e notcam.expose $exp2

# Go back to WF camera and J band
$e notcam.setup-ima wf 201
$e astrowakeup

# All done. Exit with error status 0 and write to Talker

logger -p local0.debug -t "notcam.rotation-centre" "[NOTE]: script ended successfully"
exit 0
