#!/bin/tcsh -ef

#
# ABOUT   : NOTCam setup script for focusing in imaging mode.
#           Default filter for focusing is Ks. Camera can be
#           selected to be WF or HR.
#          
#
# USAGE   : notcam.setup-focpyr [-e] camera
#           -e      : Echo script on stdout, do not execute
#           camera  : Camera to be used (wf, hr)
#
#
# DEPENDS : notcam.lens, notcam.aperture, notcam.stop,
#           notcam.filter1, notcam.grism, notcam.notcamfocus,
#           notcam.wait_notcam_ready
#
# PROVIDES: notcam.setup-focpyr
#
# TFUNCT  : ?
#
# AUTHOR  : Anlaug Amanda Djupvik
#
# HISTORY : 2009/06/07 Initial release, 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 cam = `echo "$1"`

# Check for errors in arguments
if ($cam == '') then
echo "Error in arguments"
echo "Usage: notcam.setup-focpyr cam \n"
echo "       cam = camera (wf, hr) \n"
exit 1
endif

#Check current wheel positions
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`

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

# Execute or Echo script

if ("$cam" == "wf" | "$cam" == "WF") then
#    If selection is WF, then set WF lens, open aperture, 
#    internal focus to 5650
   if ("$lens" != "1") then
      $e notcam.lens 1 &
   endif
      $e notcam.notcamfocus 5650 &
      $e tcs.focus-delta 0 &
   if ("$apert" != "4") then
      $e notcam.aperture 4 &
   endif
else if ("$cam" == "hr" | "$cam" == "HR") then
#    If selection is HR, then set HR lens, HR imaging mask in aperture, 
#    internal focus to 20
   if ("$lens" != "3") then
      $e notcam.lens 3 &
   endif
      $e notcam.notcamfocus 20 &
      $e tcs.focus-delta 0 &
   if ("$apert" != "8") then
      $e notcam.aperture 8 &
   endif
else 
   echo "Invalid camera choice."
   exit 1
endif

# Put in the focus pyramid if not already there
if ("$grism" != "9") then
    $e notcam.grism 9 &
endif

# Put in the default cold stop if not already there
if ("$stop" != "12") then
   $e notcam.stop 12 &
endif

# Set filter to Ks filter, the default one for focusing with the pyramid.
if ("$f1" != "15") then
   $e notcam.filter1 15 &
endif
if ("$f2" != "10") then
   $e notcam.filter2 10 &
endif

# Wait for all wheels to be in position
$e notcam.wait_notcam_ready

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

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