#!/bin/tcsh -ef

#
# ABOUT   : NOTCam instrument setup script for spectroscopy.
#           User selects camera, filter, slit and grism. 
#
#           NB! Neither grism nor slit wheels are moved, but 
#           to get the internal focus right they must be set.
#
#           NB! The files filter1new.def and filter2new.def, in
#           the versions that are attached at the end of
#           this script, must be available!          
#
# USAGE   : notcam.setup-spec [-e] camera filter slit
#           -e      : Echo script on stdout, do not execute
#           camera  : Camera to be selected (wf, hr)
#           filter  : Filter to be selected (NOT ID number)
#           slit    : Slit to be used (128, 64, 44)
#           grism   : Grism to be used (1)
#
# DEPENDS : notcam.lens, notcam.aperture, notcam.stop,
#           notcam.filter1, notcam.filter2, notcam.grism, 
#           notcam.notcamfocus, notcam.wait_notcam_ready,
#           tcs.focus-delta, tcs.getstatus
#
# PROVIDES: notcam.setup-spec
#
# TFUNCT  : ?
#
# AUTHOR  : Anlaug Amanda Djupvik
#
# HISTORY : 26/11/2009 (Initial release, AAD)
#           18/11/2010 Updated to adjust tcs.focus-delta if power is on, AAD  
#           18/11/2010 Added John's "bombNow" exit procedure, AAD
#           12/02/2011 Allowed for new filters Z and Y, AAD
#           13/04/2011 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"`
set fid = `echo "$2" | sed -n -e '/^[0-9.]*$/p'`
set sid = `echo "$3" | sed -n -e '/^[0-9.]*$/p'`
set gid = `echo "$4" | sed -n -e '/^[0-9.]*$/p'`

# Check for errors in arguments
if ($cam == '' | $fid == '' | $sid == '' | $gid == '') then
echo "Error in arguments"
echo "Usage: notcam.setup-spec cam fid sid gid \n"
echo "       cam = camera (wf, hr)"
echo "       fid = filter ID (Z = 237, Y = 236, J = 201, H = 203, K = 208, Ks = 207, K' = 205)"
echo "       sid = slit width in microns (0.6'' = 128, 0.3''= 64, 0.2'' = 44)"
echo "       gid = grism ID (1)"
echo " "
echo "Example:  setup-spec wf 208 128 1 \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`

# Check current telescope status
set power_on = `tcs.getstatus PowerIsOn`

if ($power_on == '1') then
   # Warn about focus-delta changes based on assuming foc-pos is for WF+K!
   echo "\e[00;31m NB! Adjusts tcs.focus-delta because telescope power is ON! \e[00m" 
   echo "\e[00;31m Assumes tcs.focus-position is set for WF-Cam + Ks filter! \e[00m"
   echo ""
endif
if ($power_on == '0') then
   echo "\e[00;31m Telescope power is OFF. No tcs.focus-delta adjustment.\e[00m"
endif


if ($fid < 201 || $fid > 237) then
   echo "$fid - Invalid filter ID. Exits.\n"
   exit 1
endif
if ("$sid" != "128" && "$sid" != "64" && "$sid" != "44") then
   echo "$sid - Invalid slit width. Must be 128, 64 or 44. Exits.\n"
   exit 1
endif
if ("$gid" != "1") then
   echo "Grism#$gid - Invalid grism ID. Must be 1. Exits.\n"
   exit 1
endif
if ($cam != "wf" && $cam != "WF" && $cam != "hr" && $cam != "HR") then
   echo "Camera $cam - Invalid camera. Must be wf or hr. Exits.\n"
endif


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


## Define John's "bombNow" exit procedure when sequencer command returns error
alias bombNow 'echo "Error detected: exiting notcam.setup-spec";    logger -p local0.debug -t notcam.setup-spec "[WARNING]: Script ended on error" ;      playphone;   exit 2'

#--------------------------------------------------------------
#     Set stop wheel to default 15mm ring if not already there
#--------------------------------------------------------------

if ("$stop" != "12") then 
   $e notcam.stop 12 &
endif
if ($? != 0) then
   bombNow
endif

#-----------------------------------------------------------------
#                    Filter selection
#                   
# Read in the filterwheel position from filter definition files
# "filter1new.def" and "filter2new.def" which must be available.
#-----------------------------------------------------------------

set fwheel1 = '/home/software/setupfiles/NOTcam/filter1new.def'
set fil1 = `head -16 $fwheel1 | grep "$fid *"\$ | awk '{print($1)}'`

if ("$fil1" == '') then
# Filter is not in filterwheel 1
# Set filterwheel 1 to open (pos 15) if not already there
  if ("$f1" != "15") then
      $e notcam.filter1 15 &
  endif
  if ($? != 0) then
     bombNow
  endif
else
# Set filterwheel 1 to requested position if not already there
  if ("$f1" != "$fil1") then
     $e notcam.filter1 $fil1 &
  endif
  if ($? != 0) then
   bombNow
  endif
endif

#set $1 = ''
set fwheel2 = '/home/software/setupfiles/NOTcam/filter2new.def'
set fil2 = `head -16 $fwheel2 | grep "$fid *"\$ | awk '{print($1)}'`

if ("$fil2" == '') then
  if ("$fil1" == '') then
     # Selected filter ID was not found in any wheel
     echo "$fid - This filter ID is not found in any wheel. Exiting."
     exit 1
  endif
# Filter is not in filterwheel 2 
# Set filterwhel 2 to open (pos 1) if not already
  if ("$f2" != "1") then
      $e notcam.filter2 1 &
  endif
  if ($? != 0) then
     bombNow
  endif
else
# Set filterwheel 2 to requested position if not already there
  if ("$f2" != "$fil2") then
     $e notcam.filter2 $fil2 &
  endif
  if ($? != 0) then
     bombNow
  endif
endif

#------------------------------------------------------------------------------
#    Camera selection and internal focus setting depending on slit and filter
#------------------------------------------------------------------------------

#echo "$cam" camera
#echo "$sid" slit
#echo "$fid" filter
#echo "$gid" grism

#echo "$lens" camera
#echo "$apert" slit
#echo "$f2" filter
#echo "$grism" grism
#echo " "

if ("$cam" == "wf" || "$cam" == "WF") then
   if ("$lens" != "1") then 
       $e notcam.lens 1 &
   endif
   if ($? != 0) then
      bombNow
   endif
   if ("$sid" == "128") then
      if ($power_on == '1') then
          tcs.focus-delta -200 &
          echo "foc-del -200\n"
      endif
      if ($? != 0) then
         bombNow
      endif        
#      if ("$fid" == "222") notcam.notcamfocus 5150
#      if ("$fid" == "201") notcam.notcamfocus 4750
#      if ("$fid" == "203") notcam.notcamfocus 4450
#      if ("$fid" == "208" || "$fid" == "207" || "$fid" == "205") notcam.notcamfocus 4250
      if ("$fid" == "236") notcam.notcamfocus 5750
      if ("$fid" == "201") notcam.notcamfocus 4650
      if ("$fid" == "203") notcam.notcamfocus 4350
      if ("$fid" == "208" || "$fid" == "207" || "$fid" == "205") notcam.notcamfocus 4150
   endif
   if ($? != 0) then
      bombNow
   endif
   if ("$sid" == "64") then
      if ($power_on == '1') then
          tcs.focus-delta -200 &
          echo "foc-del -200\n"
      endif
     if ($? != 0) then
        bombNow
     endif        
      if ("$fid" == "222") notcam.notcamfocus 4950
      if ("$fid" == "201") notcam.notcamfocus 4600
      if ("$fid" == "203") notcam.notcamfocus 4300
      if ("$fid" == "208" || "$fid" == "207" || "$fid" == "205") notcam.notcamfocus 4050
   endif
     if ($? != 0) then
         bombNow
     endif  
   if ("$sid" == "44") then
      echo "\e[00;31m $sid mu slit - This slit is not useful with the WF camera. Exits.\e[00m"
     exit 1
   endif 

endif

if ("$cam" == "hr" || "$cam" == "HR") then
#   echo "HR cam is selected"
    if ("$lens" != "3") then 
       $e notcam.lens 3 &
    endif
    if ($? != 0) then
        bombNow
    endif
    if ("$sid" == "44") then
      if ($power_on == '1') then
          tcs.focus-delta 150 &
          echo "foc-del +150\n"
      endif 
      if ($? != 0) then
          bombNow
      endif     
      if ("$fid" == "222") notcam.notcamfocus 6000
      if ("$fid" == "201") notcam.notcamfocus 5450
      if ("$fid" == "203") notcam.notcamfocus 3000
      if ("$fid" == "208" || "$fid" == "207" || "$fid" == "205") notcam.notcamfocus 1000
      if ($? != 0) then
          bombNow
      endif
   endif
   if ("$sid" == "64") then
       echo "\e[00;31m This combination is not yet calibrated. Exits.\e[00m"
       exit 1
   endif
   if ("$sid" == "128") then
       echo "\e[00;31m This combination is not yet calibrated. Exits. \e[00m"
       exit 1
   endif
endif
else 
   echo "\e[00;31m Invalid camera choice. Exits. \e[00m"
   exit 1
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-spec" "[NOTE]: Script ended successfully"

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


# more filter1new.def
#
#1    "POL 90"        0
#2    "Pol 45"        0
#3    "Pol  0"        0
#4    "Br gamma"     209
#5    "K cnt 2.27"   210
#6    "H cnt 1.57"   211
#7    "Fe II 1.64"   212
#8    "He IB 2.06"   217
#9    "Pa gamma"     214
#10   "He IC 2.19"   219
#11   "H2 2.25"      220
#12   "He IA"        213
#13   "H2 cnt 2.09"  230
#14   "Empty"         0
#15   "OPEN"          0
#16   "Pol 135 "      0
#
# more filter2new.def
#1   "OPEN"         0
#2   "Z"           237
#3   "J-cnt 1.21"  215
#4   "Pa Beta"     216
#5   "H2 2.122"    218
#6   "CH4_s 1.58"  223
#7   "CH4_l 1.69"  224
#8   "Barr 1.69"   228
#9   "CO 2-0 bh"   221   
#10  "Ks"          207
#11  "K"           208
#12  "Kprime"      205
#13  "J"           201
#14  "H"           203
#15  "Yn"          222
#16  "Y"           236
#


