#!/bin/csh -f
#+
#OSIRIS library of scripts
#
#NAME
#     osirisSetTargwave - sets DCS targwave keywords based on filter
#
#SYNOPSIS
#     osirisSetTargwave filter [-m]
#
#DESCRIPTION
#     based on the filter name provided, the script looks up the info for that
#     filter and sets the DCS targwave keyword.  if in NGS mode, the script
#     then triggers offloading of any resulting DAR (differential atmospheric
#     refraction).  the triggering is not needed if in LGS mode.
#
#OPTIONS
#     filter
#          the name of the filter being used
#
#     -m
#          simulate calls to RPC servers.  during simulated calls, the 
#          command is echoed (instead of being executed).
#
#EXAMPLES
#     osirisSetTargwave Kn1
#          sets DCS targwave to 2.0048
#
#ENVIRONMENT VARIABLES
#     none
#
#FILES
#     osirisFilterSpecs.dat
#
#SERVERS & KEYWORDS
#     service = dcs
#          keywords: targwave
#
#SCRIPTS CALLED
#     list of the scripts called by this script, for example:
#
#     help, syncheck
#
#EXIT STATUS
#     0 - normal exit, no error
#     1 - script aborted by an interrupt
#     2 - syncheck error
#     3 - error parsing command line input
#     other errors...
#
#SEE ALSO
#     ???
#-
#
# Modification History:
# 20050910 - MB: Initial version created
# 20060314 - MB: If Drk filter is selected, don't try to set targwave
# 20080304 - JLW: Updated filter list with new pupil filters
# 20180131 - jlyke: Updated with new imager filters and ensure INSTRUMENT
#                   matches DCS currinst
# 20220216 - jlyke: point to DATDIR
#

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

# A more detailed error checking is performed on the arguments below...

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

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

# End of help/syncheck boiler plate.

# Set these variables for testing
#set test
#set simall

# Set up to trap interrupts (Ctrl-C, etc.)
onintr abort

# Set default variable values
set fcmd = $0
set cmd = ${fcmd:t}
#set filtlist = "[Jbb,Hbb,Kbb,Zbb,Kcb,Jn1,Jn2,Jn3,Jn4,Hn1,Hn2,Hn3,Hn4,Hn5,Kn1,Kn2,Kn3,Kn4,Kn5,Zn4,Kc3,Kc4,Kc5,Drk]"
#set filtlist = "[Kcb,Zbb,Jbb,Hbb,Kbb,Kc3,Kc4,Kc5,Zn4,Jn1,Jn2,Jn3,Jn4,Hn1,Hn2,Hn3,Hn4,Hn5,Kn1,Kn2,Kn3,Kn4,Kn5,PaGamma,FeII,Hcont,Zn3,Y,J,Kp,BrGamma,Kcont,HeI_B,Drk]"
set filtlist = (Kcb Zbb Jbb Hbb Kbb Kc3 Kc4 Kc5 Zn4 Jn1 Jn2 Jn3 Jn4 Hn1 Hn2 Hn3 Hn4 Hn5 Kn1 Kn2 Kn3 Kn4 Kn5 PaGamma FeII Hcont Zn3 Y J Kp BrGamma Kcont HeI_B Drk)
set filter = ""
set lambda = 0.0
set cmdpre = ""
set cmdsuf = ""
set datfiledir = "$KROOT/rel/default/data"
set filterspecsfile = "${datfiledir}/osirisFilterSpecs.dat"
set match = 0

# Simulate the server commands if requested
if ($?simall) then
    set sim
    set cmdpre = "echo ${cmd}: sim:"
endif

# Get and check the command line arguments
set noglob

# Check for other flags
while ($#argv != 0)
#echo "${cmd} : Checking ${1}..."
    switch ($1)
	case -m:
        case -d*:
	    set sim
	    set cmdpre = "echo ${cmd}: sim:"
	    set cmdsuf = "-m"
	    breaksw
	default:
	    foreach filt ($filtlist)
              if ($filt == $1) then
                @ match++
                set filter = $1
              endif
            end
	    breaksw
    endsw
    shift
end

unset noglob

if ($match != 1 ) then
  # no or more than one match...bad
  osirisScriptMsg -T "${cmd}: Number of valid matches does not = 1"
  exit
endif

if (! $?sim) then
  # Only proceed if this is the selected instrument
  set currinst = `show -s dcs -terse currinst | /usr/bin/tr "[A-Z]" "[a-z]"`
  if ($currinst != $INSTRUMENT) then
    osirisScriptMsg -T "${cmd}: Currinst $currinst is not $INSTRUMENT...doing nothing"
    exit
  endif
endif

# If Drk filter was specified, don't have to do anything, just exit
if ($filter == "Drk") then
    osirisScriptMsg -T "${cmd}: targwave not changed for Drk filter."
    goto done
endif

# Get the filter info from the data file (grep -w for EXACT match)
set lambda = `grep -w $filter $filterspecsfile | cut -f2`
set error = $status
if ($error != 0) then
    osirisScriptMsg -T "${cmd}: Unable to read filter info from file - "\
	"targwave will not be set."
    exit 4
endif

# For testing, display the variables and their values
if ($?test) then
    echo ""
    echo "Current Script Variables:"
    echo filter = $filter
    echo lambda = $lambda
    echo filterspecsfile = $filterspecsfile
    echo cmdpre = $cmdpre
    echo cmdsuf = $cmdsuf
#    exit
endif

# Set the targwave DCS keyword
$cmdpre modify -s dcs targwave=$lambda
set error = $status
if ($error != 0) then
    osirisScriptMsg -T "${cmd}: Unknown error setting targwave."
endif

# If DAR is not already tracking (ie. in NGS), then trigger the offset
set aomode = `show -s ao -terse aoopsmode`
if ($aomode == "0") then
    $cmdpre modify -s ao obfmgoim=1
endif

goto done

abort:
# Block of code to handle interrupts.
exit 1

done:
# is there anything that needs to go here?
exit
