#!/bin/csh -f
#+
#OSIRIS library of scripts
#
#NAME
#     setdar - sets dar mode and tells wfao to wait/not wait for dar
#
#SYNOPSIS
#     setdar mode
#
#DESCRIPTION
# sets the keyword WFDAR to on or off, telling the wfao script whether or not
# to force an offload of the DAR shifts to the field steering mirrors.
#
# if no mode is selected, script returns the current DAR settings.
#
# setdar  [on,slew,fsm,off]
#   option:    wait4dar   AODRENA     AODRDTEN    OBFMDREN
#     off      off           0           0            0
#    slew      off           1           0            1
#     fsm      on            1           0            1
#      on      on            1           1            1
#   
#  option off: 
#    No compensation for DAR at all, no waiting for dar from the OSIRIS side. 
#    
#  option slew or nod:  
#    DAR compensation only go to the FSM when one moves the FSM. Nothing 
#    goes to the DT ctrl offset. If one keeps integrating on a star, DAR 
#    would NOT be applied (since wait4dar is off). DAR is applied only 
#    during dither or nod. Helps acquiring the star faster, 
#    no overhead, DLM's favorite!
#
#  option fsm:
#   DAR compensation is sent to the FSM, nothing to the DT ctrl offset. 
#   Dar compensation is sent for each frame. Good for consecutive 
#   long exposures at the same location. 
#
# option on :
#   DAR compensation is applied at all time and offloaded to the 
#   FSM for every frame
#
#OPTIONS
#     mode
#          sets dar mode to one of [on,slew,fsm,off]
#
#EXAMPLES
#     setdar slew
#          do not wait for dar, and apply compensation only during
#          telescope moves
#
#ENVIRONMENT VARIABLES
#     none
#
#FILES
#     none
#
#SERVERS & KEYWORDS
#     service = osiris
#          keywords: wfdar
#
#     service = ao
#          keywords: AODRENA, AODRDTEN, OBFMDREN
#
#SCRIPTS CALLED
#     help, syncheck
#
#EXIT STATUS
#     0 - normal exit, no error
#     1 - script aborted by an interrupt
#     2 - syncheck error
#
#SEE ALSO
#     ???
#-
#
# Modification History:
# 20030722 - rdc: based on input from DLM
# 20050220 - MB: Adapted from NIRC2 scripts
#

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

if ("$1" == "-h") then
    help $0 | more
    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 {[on,slew,fsm,off]}`  
unset noglob

if ("$CheckStatus" != "OK") then
    help $0 | more
    exit 2
endif

# End of help/syncheck boiler plate.

if ($#argv == 0) then
    set wfdar = `show -s osiris -terse wfdar`
    set darena = `show -s ao -terse AODRENA`
    set dardt = `show -s ao -terse AODRDTEN`
    set darfsm = `show -s ao -terse OBFMDREN`
    cat <<END

Current settings of DAR "wait-for" keywords:

    OSIRIS wait4 diff. atm. refract. :    $wfdar
    AO DAR enable                    :    $darena
    AO DAR dt control offset enable  :    $dardt
    AO DAR fsm offset enable         :    $darfsm

END
    exit 0
endif

switch ($1)
case "on":
    set wfdar = on
    set darena = 1
    set dardt = 1
    set darfsm = 1
    breaksw

case "slew":
    set wfdar = off
    set darena = 1
    set dardt = 0
    set darfsm = 1
    breaksw

case "fsm":
    set wfdar = on
    set darena = 1
    set dardt = 0
    set darfsm = 1
    breaksw

case "off":
    set wfdar = off
    set darena = 0
    set dardt = 0
    set darfsm = 0
    breaksw

default:
    set wfdar = `show -s osiris -terse wfdar`
    set darena = `show -s ao -terse AODRENA`
    set dardt = `show -s ao -terse AODRDTEN`
    set darfsm = `show -s ao -terse OBFMDREN`
    cat <<END

Current settings of DAR "wait-for" keywords:

    OSIRIS wait4 diff. atm. refract. :    $wfdar
    AO DAR enable                    :    $darena
    AO DAR dt control offset enable  :    $dardt
    AO DAR fsm offset enable         :    $darfsm

END
    exit 0
    breaksw
endsw

modify -s osiris silent wfdar = $wfdar
modify -s ao silent AODRENA = $darena
modify -s ao silent AODRDTEN = $dardt
modify -s ao silent OBFMDREN = $darfsm
