#!/bin/csh -f
#+
# OSIRIS library of scripts
#
# NAME 
#       flatlamp -- sets the new flatlamps on K1.
#
# SYNOPSIS
#       flatlamp [on|off] [power] 
#
# DESCRIPTION
#       Turn on or off the flat lamps originally developed
#       for MOSFIRE use and set a power level.
#        - power level of 20 is low power
#        - power level of 0  is high power 
#           (opposite to what you might think)
#       
# ARGUMENTS
#       on    -turn on power
#       off   -turn off power
#       power - value 0-20 for high and low setting.
# OPTIONS
#       none
#
# EXAMPLES
#       1) Turn lamps off
#             flatlamp off
#
#       2) Turn lamps on
#             flatlamp off
#
#       3) Set power level
#             flatlamp on 14.5
#              
# EXIT STATUS
#     0 - normal exit, no error
#     1 - not a valid argument
#
#+
# Modification History:
# 20190520  jlyke - Original adapted from MOSFIRE
# 20220217  jlyke - kwds moved from flat service to dcs
#------------------------------------------------------------------------------

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

set usage = "$0 on|off|0-20|-f(orce)"

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

set service = dcs
# If no arguments are specified. Show the current values
# of the lamps
if ( $#argv == 0 ) then 
     show -s $service flamp1 fpower1
     show -s $service flamp2 fpower2
     exit $status
endif

set force = 0
set problem = 0

# parse arguments
while ( $#argv > 0 )
  set var = $1
  if (`isfloat $1`) then
    #set the power level
    if ( `compareNumbers $1 ">=" 0.0` && `compareNumbers $1 "<=" 20.0`) then
      set fpower = $1
    else 
      echo "Power level $1 is outside the allowed range: 0 (bright) to 20 (faint)."
      exit 1
    endif
  else
    set var = $1
    # lower case for easier case matching
    set var = `echo $var | /usr/bin/tr "[A-Z]" "[a-z]"`
    switch ($var)
      case on:
        set action = on
        breaksw
      case off: 
        set action = off
        breaksw
      case -f*:
        set force = 1
        breaksw
      default:
        breaksw
    endsw
  endif
  shift
end

if ($force == 1 ) then
  set makechange = 1
else
  set makechange = 0
  # check that we *should* change the flat lamps
  set currinst = `show -s dcs -terse currinst`
  set tertposn = `show -s dcs -terse tertposn`
  set aosim = `show -s ao -terse -binary aosim`
  if ($currinst == "OSIRIS" && $tertposn == "lnas" && $aosim == 1) then 
    set makechange = 1
  else 
    echo "Expect: currinst=OSIRIS  Got: $currinst"
    echo "Expect: tertposn=lnas    Got: $tertposn"
    echo "Expect: aosim=1          Got: $aosim"
    echo ""
    echo "Use the '-f' option to force changes if needed"
    @ problem++
  endif
endif

if ($makechange == 1 ) then
  if ($?fpower) then
    echo "Setting power level to $fpower"
    modify -s $service fpower=$fpower
    if ($status != 0 ) then
      echo "Error setting dome flat power"
      @ problem++
    endif
  endif
  if ($action == "on" || $action == "off" ) then
    echo "Turning flat lamp $action" 
    modify -s $service flamp=$action
    if ($status != 0 ) then
      echo "Error setting dome flat $action"
      @ problem++
    endif
  endif
endif     

exit $problem

