#!/bin/tcsh -ef

#
# ABOUT   : A through focus test is made to check the the focus 
#           pyramid calibration. Instrument must be setup first.
#           Exptime must be set by user to be
#           a useful number of seconds for the star(s) when
#           no pyramid is in the beam. The exptime will be
#           enhanced by x4 in the script when the pyramid 
#           is in.
#          
#
# USAGE   : notcam.through-focus [-e] camera
#           -e      : Echo script on stdout, do not execute
#           exptime : Useful exptime without focus pyramid
#
#
# DEPENDS : 
#           tcs.focus-delta
#           notcam.object
#           notcam.expose, notcam.grism
#           notcam.teloffset
#
# PROVIDES: notcam.through-focus
#
# TFUNCT  : ?
#
# AUTHOR  : Anlaug Amanda Djupvik
#
# HISTORY : 2009/09/09 (Initial release, AAD)
#           2010/03/11 Updated to set focus-delta after setup-ima since the
#                      latter script has been upgraded to set focus-delta to
#                      default since Feb-2010, AAD)
#           2010/11/18 Script is simplified, 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'`

# Calculate exptime for focus pyramid
set exp2 = `echo "4*$exp" | bc -l | awk '{printf ("%5i"),$1}'`

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

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

# Execute or Echo script
echo "\n"
echo "\e[00;31mMake sure the instrument setup is ok. Filter Ks is recommended!\e[00m"
echo "\e[00;31mSet foc-pos to best guess value for this camera and foc-del to 0!\e[00m"
echo "\n"
echo "\e[00;31mHit return to continue\e[00m"
set answ = $<


$e notcam.object "ref backgr"
$e notcam.expose $exp
$e notcam.grism 9
$e notcam.object "pyr backgr"
$e notcam.expose $exp2
$e notcam.teloffset 10 10

$e notcam.grism 2
$e tcs.focus-delta -150
$e sleep 5
$e notcam.object "ref -150"
$e notcam.expose $exp
$e notcam.grism 9
$e notcam.object "pyr -150"
$e notcam.expose $exp2

$e notcam.grism 2
$e tcs.focus-delta -125
$e sleep 5
$e notcam.object "ref -125"
$e notcam.expose $exp
$e notcam.grism 9
$e notcam.object "pyr -125"
$e notcam.expose $exp2

$e notcam.grism 2
$e tcs.focus-delta -100
$e sleep 5
$e notcam.object "ref -100"
$e notcam.expose $exp
$e notcam.grism 9
$e notcam.object "pyr -100"
$e notcam.expose $exp2

$e notcam.grism 2
$e tcs.focus-delta -75
$e sleep 5
$e notcam.object "ref -75"
$e notcam.expose $exp
$e notcam.grism 9
$e notcam.object "pyr -75"
$e notcam.expose $exp2

$e notcam.grism 2
$e tcs.focus-delta -50
$e sleep 5
$e notcam.object "ref -50"
$e notcam.expose $exp
$e notcam.grism 9
$e notcam.object "pyr -50"
$e notcam.expose $exp2

$e notcam.grism 2
$e tcs.focus-delta -25
$e sleep 5
$e notcam.object "ref -25"
$e notcam.expose $exp
$e notcam.grism 9
$e notcam.object "pyr -25"
$e notcam.expose $exp2

$e notcam.grism 2
$e tcs.focus-delta 0
$e sleep 5
$e notcam.object "ref 0"
$e notcam.expose $exp
$e notcam.grism 9
$e notcam.object "pyr 0"
$e notcam.expose $exp2

$e notcam.grism 2
$e tcs.focus-delta 25
$e sleep 5
$e notcam.object "ref 25"
$e notcam.expose $exp
$e notcam.grism 9
$e notcam.object "pyr 25"
$e notcam.expose $exp2

$e notcam.grism 2
$e tcs.focus-delta 50
$e sleep 5
$e notcam.object "ref 50"
$e notcam.expose $exp
$e notcam.grism 9
$e notcam.object "pyr 50"
$e notcam.expose $exp2

$e notcam.grism 2
$e tcs.focus-delta 75
$e sleep 5
$e notcam.object "ref 75"
$e notcam.expose $exp
$e notcam.grism 9
$e notcam.object "pyr 75"
$e notcam.expose $exp2

$e notcam.grism 2
$e tcs.focus-delta 100
$e sleep 5
$e notcam.object "ref 100"
$e notcam.expose $exp
$e notcam.grism 9
$e notcam.object "pyr 100"
$e notcam.expose $exp2

$e notcam.grism 2
$e tcs.focus-delta 125
$e sleep 5
$e notcam.object "ref 125"
$e notcam.expose $exp
$e notcam.grism 9
$e notcam.object "pyr 125"
$e notcam.expose $exp2

$e notcam.grism 2
$e tcs.focus-delta 150
$e sleep 5
$e notcam.object "ref 150"
$e notcam.expose $exp
$e notcam.grism 9
$e notcam.object "pyr 150"
$e notcam.expose $exp2

$e notcam.grism 2
$e tcs.focus-delta 0
$e astrowakeup

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

logger -p local0.debug -t "notcam.through-focus" "[NOTE]: notcam.through-focus ended successfully"
exit 0
