#!/bin/csh -f
#+
# configAOforFlats
#
# Configure the AO bench for flat lamp calibration:
#     - Send the SPF stage to the telescope position
#     - Open the AO hatch
#
#-
#
# Modifications:
#   Original    03mar2006
#   SY          20jul2020  move SFP in 3 stages to telescope
#                          remove TRICK dichroic

# Boiler plate for "-h" support for command autohelp.


if ("$1" == "-h") then
    help $0
    exit $status
endif

# Boilerplate for syncheck.
# Note that the boiler plate should be transparent for all usages,
# but to make correct use of syncheck you will need to specify the
# correct pattern.

set noglob
set CheckStatus = `syncheck -command $0 $* -pattern`  
unset noglob

if ("$CheckStatus" != "OK") then
    help $0
    exit 1
endif

# End of help/syncheck boiler plate.

#
# Provide an opportunity for the user to back out following a warning
#
set default = y
echo    "Before reconfiguring AO you should be sure that the AO bench is"
echo    "not in use by someone else and that it is safe to move AO stages."
echo -n "Proceed? [$default] "
set answer = $<
if ("$answer" == "") set answer = "$default"

if ("$answer" != "y") then
    echo "ConfigAOforFlats is aborting."
    exit 0
endif


unalias sao
unalias mao

alias sao show -s ao -terse
alias mao modify -s ao

#
#  Send the AO stage housing the fiber (the SFP) to the correct position.
#
set sfppos = `show -s ao -terse obsfname`
if ( "$sfppos" != "telescope" ) then
  echo "Sending SFP to prehome"
  mao obsfname=prehome  #somehow sfp stage jumps to home rightaway if no "sleep"
  
  sleep 10

  set ready = `show -s ao -terse obsfstst`
       while ($ready != "INPOS")
        sleep 5
        set ready = `show -s ao -terse  obsfstst`
       end
  unset ready
  sleep 2     
  
  echo "Sending SFP to home"
  mao obsfname=home

  sleep 2

  set ready = `show -s ao -terse obsfstst`
       while ($ready != "INPOS")
        sleep 5
        set ready = `show -s ao -terse  obsfstst`
       end
  unset ready
  sleep 2     
  
  echo "Sending SFP to telescope"
  mao obsfname=telescope

  sleep 2

  set ready = `show -s ao -terse obsfstst`
       while ($ready != "INPOS")
        sleep 5
        set ready = `show -s ao -terse  obsfstst`
       end
  unset ready
  sleep 2     

else
  echo "SFP is already at telescope"
endif
if ( $status != 0 ) then
  echo "Error sending SFP to telescope.  ConfigAOforFlats is aborting."
  exit -1
endif

#
#  Open the AO hatch
#
#set hatch = `aohatch`
#If ( $hatch[3] != "open" ) then
#  echo "Opening the AO hatch"
  aohatch open
#else
#  echo "The AO hatch is already open."
#endif
if ( $status != 0 ) then
  echo "Error opening the AO hatch.  ConfigAOforFlats is aborting."
  exit -1
endif

#
#  Remove TRICK dichroic
#

set tddpos = `show -s ao -terse obtdname`
if ( "$tddpos" != "open" ) then
  echo "Sending TRICK dichroic to open"
  mao obtdname=open
  sleep 2
  set ready = `show -s ao -terse obtdstst`
       while ($ready != "INPOS")
        sleep 2
        set ready = `show -s ao -terse  obtdstst`
       end
  unset ready
  sleep 2   
else
  echo "TRICK dchroic is already at open"
endif
if ( $status != 0 ) then
  echo "Error sending TRICK dichroic to open.  ConfigAOforFlats is aborting."
  exit -1
endif


#
# Clena up
#

unalias sao
unalias mao

echo "configAOforDomeFlats is done."

#
# Put in the OSIRIS Fold Mirror
#
#set foldMirror = `show -s ao obof` 
#set foldNumber = `echo $foldMirror | cut -d . -f1`
#if ( $foldNumber > 140 ) then
#  echo "OSIRIS Fold Mirror is okay"
#else
#  echo "Putting in the OSIRIS fold Mirror"
#  modify -s ao obofname = "OSIRIS"
#endif

#if ( $status != 0 ) then
#  echo "Error setting the Fold Mirror.  ConfigAOforFlats is aborting."
#  exit -1
#endif

#
#  All is well (we think)
#
exit 0
