#!/bin/tcsh -ef

#
# ABOUT   : NOTCam "easy calibration" script meant for in-situ (on target)
#           calibration lamp exposures of default exposure times. The
#           script takes:
#
#               1 Argon spectrum
#               1 Xenon spectrum
#               3 Halogen flats 
#
#           for the selected wavelength regions, which can be one or more of
#           the 5 possible: (Z=237, Y=236, J=201, H=203, K=208)
#
#           The script checks that a slit and grism is already in and aborts
#           if this is not the case, i.e. this script does not move any slit
#           or grism.  
#
#           Due to the problem with the slow Argon lamp, the script starts by
#           taking Argon lamps for all regions without switching the lamp off.
#           Then it does region by region Xenon and Halogen.
#
# USAGE   : notcam.easy-calib [-e] fid1 fid2 fid3 fid4 fid5
#           -e      : Echo script on stdout, do not execute
#           fid1   : First wave region (give filter ID)
#           fid2   : Second wave region (filter ID)
#           fid3   : Third wave region (give filter ID)
#           fid4   : Fourth wave region (give filter ID)
#           fid5   : Fifth wave region (give filter ID)
#
#
# DEPENDS : notcam.setup-ima, notcam.filter2, notcam.object,
#           notcam.wait_notcam_ready, tcs.focus-delta,
#           notcam.autosave_on, notcam.teloffset, notcam.frames,
#           notcam.imtype, tcs.enter-object, tcs.goto-object,
#           tcs.auto-positioning-full
#
# PROVIDES: notcam.easy-calib
#
# TFUNCT  : 1 wave regions takes ~2.5 min (including o-m-c and c-m-c)
#           3 wave regions takes ~8 min
#           5 wave regions takes ~14 min
#
# AUTHOR  : Anlaug Amanda Djupvik
#
# HISTORY : 2012/06/11 (Initial release, AAD)


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

# Get command line arguments and check for errors
set fid1 = `echo "$1" | sed -n -e '/^[0-9.]*$/p'`
set fid2 = `echo "$2" | sed -n -e '/^[0-9.]*$/p'`
set fid3 = `echo "$3" | sed -n -e '/^[0-9.]*$/p'`
set fid4 = `echo "$4" | sed -n -e '/^[0-9.]*$/p'`
set fid5 = `echo "$5" | sed -n -e '/^[0-9.]*$/p'`

if ($fid1 == "") then
   echo " "
   echo "[00;36m Usage: notcam.easy-calib fid1 [fid2] [fid3] [fid4] [fid5][00m \n"
   echo "        fid = filter ID number "
   echo "        J = 201, H = 203, K = 208, Y = 236, Z = 237 "
   echo " "
   echo "        [00;32m Example: easy-calib 208 201 203 [00m "
   echo "        [00;32m          easy-calib 208 201 203 236 237 [00m \n"
   exit 1
endif

if ($fid2 == "" && $fid3 == "" && $fid4 == "" && $fid5 == "") then  
   set N = 1
else if ($fid3 == "" && $fid4 == "" && $fid5 == "") then
   set N = 2
else if ($fid4 == "" && $fid5 == "") then
   set N = 3
else if ($fid5 == "") then
   set N = 4
else
   set N = 5
endif

# Trap CNTR-C
onintr trap

# Define exit procedure as alias
alias bombNow 'echo "Error detected: exiting notcam.easy-calib";    logger -p local0.debug -t notcam.easy-calib "[WARNING]: Script ended on error" ;      playphone;   exit 2'

# Write to Talker
logger -p local0.debug -t "notcam.easy-calib" "[NOTE]: easy-calib started"

# Reads current instrument setup
set lens = `notcam.lens pos`
set grism = `notcam.grism pos`
set apert = `notcam.aperture pos`
set stop = `notcam.stop pos`
set f1 = `notcam.filter1 pos`
set f2 = `notcam.filter2 pos`


# Check that a valid camera, slit and grism is in the beam. Otherwise abort.

if ($apert == "10") then
    set slit = 128
#    echo "Slit is: $slit"
  else if ($apert == "1") then
    set slit = 44
#    echo "Slit is: $slit"
  else 
    echo " [00;31m No valid slit in aperture wheel. Exiting [00m "
    exit 1
endif

if ($grism == "1") then
    set gr = 1
#    echo "Grism is: $gr"
  else
    echo " [00;31m No valid grism in grism wheel. Exiting.[00m  "
    exit 1
endif

if ($lens == "1") then
    set cam = "wf"
#    echo "Camera is: $cam"
  else if ($lens == "3") then
    set cam = "hr"
#    echo "Camera is: $cam"
  else
    echo " [00;31m No valid camera in lens wheel. Exiting [00m "
    exit 1
endif


#-----------------------------------------------------------------
#      Default lamp exposure times assuming standard setup.
#      WF-cam + 128 micron slit or HR-cam + 44 mu slit. 
#      Lamps are denoted: halogen = 1, argon = 2, xenon = 3
#-----------------------------------------------------------------
set t1kw = 7 
set t1hw = 7
set t1jw = 8
set t1yw = 15
set t1zw = 15
set t2kw = 10
set t2hw = 2
set t2jw = 1.5
set t2yw = 2
set t2zw = 0.6
set t3kw = 20
set t3hw = 10
set t3jw = 15
set t3yw = 0.6
set t3zw = 0.6
set t1kh = 60
set t1hh = 60
set t1jh = 60
set t2kh = 30
set t2hh = 30
set t2jh = 10
set t3kh = 60
set t3hh = 60
#-----------------------------------------------------------------

echo " [00;32m Switches off automatic quickspec [00m "
$e notcam.quickspec-config auto=no

# Start loop through all bands to take the Argon lamp spectra
set loop = 0
while ($loop < $N)
 @ loop++
    if ($loop == 1) set fid = $fid1
    if ($loop == 2) set fid = $fid2
    if ($loop == 3) set fid = $fid3
    if ($loop == 4) set fid = $fid4
    if ($loop == 5) set fid = $fid5   

# The setup-spec script changes only filter and internal focus!
$e notcam.setup-spec $cam $fid $slit $gr
echo "        [00;36m Wave region: $fid [00m"

if ($cam == "wf") then
   if ($slit == "128") then   
       if ($fid == "208") set t2 = $t2kw
       if ($fid == "203") set t2 = $t2hw
       if ($fid == "201") set t2 = $t2jw
       if ($fid == "236") set t2 = $t2yw
       if ($fid == "237") set t2 = $t2zw
    endif
#   echo "OK wf setup."
   else
   echo " [00;31m Not a standard setup. Exiting.[00m  "
endif
else
if ($cam == "hr") then
   if ($slit == "44") then   
       if ($fid == "208") set t2 = $t2kh
       if ($fid == "203") set t2 = $t2hh
       if ($fid == "201") set t2 = $t2jh
#       if ($fid == "236") set t2 = $t2yh
#       if ($fid == "237") set t2 = $t2zh
    endif
#    echo "OK hr setup."
   else
   echo " [00;31m Not a standard setup. Exiting.[00m "
   endif
endif
else
   echo " [00;31m Not a standard setup. Exiting.[00m "
   exit 1
endif

$e notcam.calibexp -lmc -llo Argon $t2
#echo " [00;32mDone Argon lamp for band number [00m: " $loop

end
echo " [00;32mFinished Argon lamp for all bands [00m "


# Start loop over all bands to take Xenon and Halogen lamps
set loop = 0
while ( $loop < $N)
 @ loop++
   if ($loop == 1) set fid = $fid1
   if ($loop == 2) set fid = $fid2
   if ($loop == 3) set fid = $fid3
   if ($loop == 4) set fid = $fid4
   if ($loop == 5) set fid = $fid5   
$e notcam.setup-spec $cam $fid $slit $gr
echo "        [00;36m Wave region: $fid [00m"
if ($cam == "wf") then
   if ($slit == "128") then   
       if ($fid == "208") then 
           set t3 = $t3kw
           set t1 = $t1kw
       endif
       if ($fid == "203") then 
           set t3 = $t3hw
           set t1 = $t1hw
       endif
       if ($fid == "201") then 
           set t3 = $t3jw
           set t1 = $t1jw
       endif
       if ($fid == "236") then 
           set t3 = $t3yw
           set t1 = $t1yw
       endif
       if ($fid == "237") then 
           set t3 = $t3zw
           set t1 = $t1zw
       endif
    endif
   else
   echo " Not a standard setup. Exiting. "
   endif
else
if ($cam == "hr") then
   if ($slit == "44") then   
       if ($fid == "208") then 
           set t3 = $t3kh
           set t1 = $t1kh
       endif
       if ($fid == "203") then 
           set t3 = $t3hh
           set t1 = $t1hh
       endif
       if ($fid == "201") then 
           set t3 = $t3jh
           set t1 = $t1jh
       endif
#       if ($fid == "236") set t = $t2yh
#       if ($fid == "237") set t = $t2zh
    endif
   else
   echo " [00;31m Not a standard setup. Exiting. [00m"
   endif
else
   echo " [00;31m Not a standard setup. Exiting. [00m"
   exit 1
endif
$e notcam.calibexp -lmc Xenon $t3
if ($loop == $N) then
   echo " This is the last wavelength region. Goes back to sky. "
   $e notcam.calibexp -n 3 Halogen $t1
else
   $e notcam.calibexp -lmc -n 3 Halogen $t1
endif
# echo " Done Xenon and Halogen lamp for band number [00m : " $loop
end

#echo " [00;32mFinished Xenon and Halogen lamp for all bands [00m "

# Notify
$e notcam.imtype OBJECT
$e astrowakeup
echo " [00;32m Switches on automatic quickspec [00m "
$e notcam.quickspec-config auto=yes

# All done. Exit with error status 0 and write to Talker
logger -p local0.debug -t "notcam.easy-calib" "[NOTE]: easy-calib ended successfully"

echo "\n [00;32m notcam.easy-calib ready [00m\n"
exit 0


########## CNTR-C handler ##########

trap:
echo "\n [00;31mCNTR-C detected\! [00m"

echo "\n [00;31m Wait for exposure to finish. NOTcam exposures can not be aborted! [00m"

# Exit with error status 3 and write to Talker
logger -p local0.debug -t "notcam.easy-calib" "[WARNING]: Script ended by CNTR-C"
echo "\n [00;32m notcam.easy-calib stopped \![00m \n"
exit 3


