#!/bin/tcsh -ef

#
# ABOUT   : NOTCam instrument setup script for imaging.
#           User selects camera and filter.
#
#           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-ima [-e] camera filter [stop]
#           -e      : Echo script on stdout, do not execute
#           camera  : Camera to be selected (wf, hr)
#           filter  : Filter to be selected (NOT ID number)
#           [stop]  : Optional stop size if different from default (15)
#
# 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-ima
#
# TFUNCT  : ?
#
# AUTHOR  : Anlaug Amanda Djupvik
#
# HISTORY : 08/06/2009 (Initial release, AAD)
#           02/02/2010 Added tcs.focus-delta changes, AAD
#           05/05/2010 Updated to allow WF,HR in capital letters
#                      Updated to log errors in arguments that lead to exit
#                      as ERRORs to the talker.
#           08/09/2010 Checks telescope power before doing tcs.focus-delta, AAD
#           13/09/2010 Added John's "bombNow" exit procedure, AAD
#           12/02/2011 Allowed for new filters Z and Y, AAD   
#           12/02/2011 Inserted updated focus-delta values, AAD
#           11/04/2011 Changed input files to filter*new.def, AAD
#           13/04/2011 Added script parameters to talker log, AAD
#           16/03/2012 Allowed for new filters #238 and #239, AAD
#           08/11/2013 Allowed for new filter #240, AAD
#           09/09/2020 Added as optional 3rd input parameter: stop (in mm), AAD

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

#Get command line arguments
set cam = `echo "$1"`
#set cam = `echo $cam | awk '{print tolower($1)}'`
set fid = `echo "$2" | sed -n -e '/^[0-9.]*$/p'`
set stopmm = `echo "$3" | sed -n -e '/^[0-9.]*$/p'`

# Check for errors in arguments
if ($cam == '' | $fid == '') then
echo "[00;31m Error in arguments [00m"
echo "Usage: notcam.setup-im [-e] cam fid [stop] \n"
echo "       cam = camera (wf, hr)"
echo "       fid = filter ID number (see below)"
echo "    [stop] = optional stop size (mm) if different from default (15) \n"
echo "Z  = 237    KG2       = 240"
echo "Y  = 236    Yn        = 222"
echo "            He-IA     = 213   Pa-Gamma  = 214 "
echo "J  = 201    Jcnt1.207 = 215   Pa-Beta   = 216 "
echo "H  = 203    Hcnt1.574 = 211    " 
echo "            CH4-s     = 223   CH4-l     = 224 "
echo "            [FeII]    = 212   [FeII]cnt = 228 "
echo "Ks = 207    He-IB     = 217   H2-2.121  = 218 " 
echo "            H2cnt     = 230   Br-Gamma  = 209 "     
echo "K  = 208    He-IC     = 219   H2-2.251  = 220 "  
echo "            Kcnt2.267 = 210   CO-(2-0)  = 221 "
echo "Kp = 205    BK7       = 238   KG4       = 239 \n "
echo "Available smaller stops (mm): 5, 6, 7, 9, 13, 14 \n " 
exit 1
endif

if ($fid < 201 || $fid > 240) then
   echo "$fid - Invalid filter ID number. Must be in range 201 to 240."
  logger -p local0.debug -t "notcam.setup-ima" "[ERROR]: Invalid filter ID. Exits."
   exit 1
endif

if ($cam != "wf" && $cam != "hr" && $cam != "WF" && $cam != "HR") then
  echo "Invalid camera name. Exits. "
  logger -p local0.debug -t "notcam.setup-ima" "[ERROR]: Invalid camera. Exits."  
  exit 1
endif


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

# 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`

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


# Execute or Echo script

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

# Set grism wheel to OPEN if not already there
if ("$grism" != "2") then
   $e notcam.grism 2 &
endif
if ($? != 0) then
   bombNow
endif


# Set stop wheel to default if not already there and no optional stop is selected
if ($stopmm == '' && $stop != '12' ) then
   $e notcam.stop 12 &
endif 
if ($? != 0) then
   bombNow
endif

# If another than default stop is selected
if ($stopmm == '5') then
   $e notcam.stop 6 &
endif
if ($? != 0) then
   bombNow
endif

if ($stopmm == '6') then
   $e notcam.stop 15 &
endif
if ($? != 0) then
   bombNow
endif

if ($stopmm == '7') then
   $e notcam.stop 16 &
endif
if ($? != 0) then
   bombNow
endif

if ($stopmm == '9') then
   $e notcam.stop 14 &
endif
if ($? != 0) then
   bombNow
endif

if ($stopmm == '13') then
   $e notcam.stop 10 &
endif
if ($? != 0) then
   bombNow
endif

if ($stopmm == '14') then
   $e notcam.stop 11 &
endif
if ($? != 0) then
   bombNow
endif


if ($stopmm != '' && $stopmm != '5'  && $stopmm != '6' && $stopmm != '7' && $stopmm != '9' && $stopmm != '13' && $stopmm != '14') then
  echo " [00;31m Not valid stop. Exiting \e[00m "
exit 1
endif


# Determine internal and external focus values dependent on Camera and filter
# For WF camera

if ("$cam" == "wf" || "$cam" == "WF") then
#  Check for filter selection to set the tcs.focus-offset correctly
  if ($fid == '207' || $fid == '208' || $fid == '206' || $fid == '218' || $fid == '217' || $fid == '230' || $fid == '209' || $fid == '219' || $fid == '220' || $fid == '210' || $fid == '221') then 
     if ($power_on == '1') then
        tcs.focus-delta 0 &
        echo "foc-del 0\n"
     endif
     if ($? != 0) then
         bombNow
     endif
  endif
  if ($fid == '203' || $fid == '211' || $fid == '223' || $fid == '224' || $fid == '212' || $fid == '228') then
     if ($power_on == '1') then    
#        tcs.focus-delta -30 &
        tcs.focus-delta -45 &
        echo "foc-del -45\n"
     endif
      if ($? != 0) then
         bombNow
     endif    
  endif 
  if ($fid == '201' || $fid == '215' || $fid == '216') then
     if ($power_on == '1') then
 #     tcs.focus-delta -75 &
       tcs.focus-delta -100 &    
       echo "foc-del -100\n"
     endif
     if ($? != 0) then
         bombNow
     endif     
  endif
  if ($fid == '236' || $fid == '222' || $fid == '213' || $fid == '214') then 
     if ($power_on == '1') then
#       tcs.focus-delta -145 & 
        tcs.focus-delta -160  &
        echo "foc-del -160\n"
     endif
     if ($? != 0) then
         bombNow
     endif      
  endif
  if ($fid == '237' ) then 
     if ($power_on == '1') then
        tcs.focus-delta -200  &
        echo "foc-del -200\n"
     endif
     if ($? != 0) then
         bombNow
     endif      
  endif

#  Set WF lens, set open aperture, set int focus to 5650
   if ("$lens" != "1") then 
     $e notcam.lens 1 &
   endif
   if ($? != 0) then
      bombNow
   endif    
#   if ("$focus" != "5650") then
      $e notcam.notcamfocus 5650 &
   if ($? != 0) then
      bombNow
   endif    
#   endif
   if ("$apert" != "4") then 
      $e notcam.aperture 4 &
   endif
   if ($? != 0) then
      bombNow
   endif    
endif

if ("$cam" == "hr" || "$cam" == "HR") then
   if ($power_on == '1') then
     echo "\e[00;32m Note that the focus-pos shift for the HR-cam \e[00m" 
     echo "\e[00;32m is here embedded into the focus-delta offsets.\e[00m"
     echo " "
   endif
#  Check for filter selection to set the tcs.focus-offset correctly
#  NB! Note that the focus-pos shift for HR is embedded into the focus-delta offset

#  New values for HR offset (+110??)
#  HR + K = +110? - 0 = 110
#  HR + H = +110? - 60 = 70
#  HR + J = +205 - 110 = 30
#  HR + Y = +205 - 180 = -50
#  HR + Z = +205 - 220 = -75

#  Old values (feb 2011 to June 2011) for HR offset (+205)
#  HR + K = +205 - 0 = 205
#  HR + H = +205 - 60 = 145
#  HR + J = +205 - 110 = 95
#  HR + Y = +205 - 180 = 25
#  HR + Z = +205 - 220 = -15

#  Old values (from before 2011) for HR offset (+136)
#  HR + K = +136 - 0 = 136
#  HR + H = +136 - 35 = 101
#  HR + J = +136 - 90 = 46
#  HR + Y = +136 - 170 = -34

#  Old values (from before 2009) for HR offset (+90)
#  HR + K = +90 - 0 = 90
#  HR + H = +90 - 35 = 55
#  HR + J = +90 - 90 = 0
#  HR + Y = +90 - 170 = -80

  if ($fid == '207' || $fid == '208' || $fid == '206' || $fid == '218' || $fid == '217' || $fid == '230' || $fid == '209' || $fid == '219' || $fid == '220' || $fid == '210' || $fid == '221') then 
     if  ($power_on == '1') then
          tcs.focus-delta 110 &
          echo "foc-del 110\n"
     endif          
  endif
if ($? != 0) then
  bombNow
endif
  if ($fid == '203' || $fid == '211' || $fid == '223' || $fid == '224' || $fid == '212' || $fid == '228') then 
     if  ($power_on == '1') then
        tcs.focus-delta 70 &
        echo "foc-del 70\n"
     endif
  endif 
if ($? != 0) then
  bombNow
endif
  if ($fid == '201' || $fid == '215' || $fid == '216') then 
     if  ($power_on == '1') then 
         tcs.focus-delta 30 &
         echo "foc-del 30\n"
     endif
  endif 
if ($? != 0) then
  bombNow
endif
  if ($fid == '222' || $fid == '213' || $fid == '214') then 
     if  ($power_on == '1') then 
        tcs.focus-delta -50 &
        echo "foc-del -50\n"
     endif  
  endif
if ($? != 0) then
  bombNow
endif
  if ($fid == '237') then 
     if  ($power_on == '1') then 
        tcs.focus-delta -75 &
        echo "foc-del -75\n"
     endif  
  endif
if ($? != 0) then
  bombNow
endif

#  Set HR lens, set HR imaging mask in aperture, set int focus to 20
   if ("$lens" != "3") then 
      $e notcam.lens 3 &
   endif
if ($? != 0) then
  bombNow
endif
#   if ("$focus" != "20") then
      $e notcam.notcamfocus 20 &
if ($? != 0) then
  bombNow
endif
#   endif
   if ("$apert" != "8") then 
      $e notcam.aperture 8 &
   endif
if ($? != 0) then
  bombNow
endif
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 &
#  if ("$f1" != "14") then
#      $e notcam.filter1 14 &
  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."
logger -p local0.debug -t "notcam.setup-ima" "[ERROR]: Filter ID not found. Exits."
     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

# 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-ima" "[DEBUG]: notcam.setup-ima $cam $fid $stopmm successful"
echo "\n [00;32m notcam.setup-ima ready: $cam camera, filter ID $fid $stopmm [00m\n"
exit 0


# more filter1new.def
#
#1    "KG2"          240
#2    "empty"         0
#3    "empty"         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   "BK7"          238
#15   "OPEN"          0
#16   "KG4"          239
#
# 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  "empty"         0
#16  "Y"           236
#
