#!/bin/csh -f
#+
#OSIRIS library of scripts
#
#NAME
#     rotate - move rotator and set mode
#
#SYNOPSIS
#     rotate angle mode [-i|-s] [-m]
#
#DESCRIPTION
#     moves the rotator to angle x in mode "mode". Legal values of "mode" are:
#
#     posang
#     stationary
#     vertang
#
#OPTIONS
#     angle
#          desired rotator angle
#
#     mode
#          rotator mode, one of [posang,vertang,stationary]
#
#     -i 
#          imager-centric (set PA on the imager)
#
#     -s   
#          spec-centric (default), set PA on SPEC      
#     -m
#          simulate calls to RPC servers.  during simulated calls, the 
#          command is echoed (instead of being executed).
#
#EXAMPLES
#     rotate 45.5 vertang
#          Sets the rotator to vertang mode at 45.5 degrees
#
#ENVIRONMENT VARIABLES
#     none
#
#FILES
#     none
#
#SERVERS & KEYWORDS
#     service = dcs
#          keywords: rotdest, rotmode, rotstat, instangl
#
#     service = ao
#          keywords: aodtstat, aodmstat, obfmgoim
#
#SCRIPTS CALLED
#     help, syncheck
#
#EXIT STATUS
#     0 - normal exit, no error
#     1 - script aborted by an interrupt
#     2 - syncheck error
#
#SEE ALSO
#     ???
#-
#
# Modification History:
# 20050220 - MB: Adapted from NIRC2 scripts
# 20050824 - MB: Added trigger for DAR offloading in NGS mode
# 20050910 - MB: Fixed logic for checking AO status before rotating & added
#                simulate "-m" support
# 20190501 - jlyke Add osirisHeaderPA to end so headers are correct even
#                  if osirisTakeDataset is not run
# 20210402 - jlyke check for currinst, allow imager-centric PA setting
#

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

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

# check that our instrument matches the dcs.CURRINST
# envar INSTRUMENT is lower case, dcs.CURRINST is upper case
set myinst = `printenv INSTRUMENT | /usr/bin/tr '[a-z]' '[A-Z]'`
set currinst = `show -s dcs -terse currinst`
if ( $myinst != $currinst ) then
  echo "dcs.CURRINST: $currinst is not $myinst...exiting"
  exit 2
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.

# More detailed error checking is performed on the arguments below...

#set noglob
#set CheckStatus = `syncheck -command $0 $* -pattern float [posang,vertang,stationary]` 
#unset noglob

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

# End of help/syncheck boiler plate.

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

# Set default variables
set fcmd = $0
set cmd = ${fcmd:t}
set modelist = "[posang,vertang,stationary]"
set cmdpre = ""
set cmdsuf = ""
set Iang = -42.5

# Get and check the required command line arguments
set noglob

# Get the angle and rotator mode
#set CheckStatus = `syncheck -command $0 ${argv[1-2]} -pattern float $modelist`
#if ("$CheckStatus" == "OK") then
set angle = $1
shift
switch ($1)
  case "posang":
  case p*:
    set mode = "position angle"
    breaksw
  case "vertang":
  case v*:
    set mode = "vertical angle"
    breaksw
  case "stationary":
  case s*:
    set mode = "stationary"
    breaksw
  default:
    # should never get here since we already did a syncheck
    osirisScriptMsg -T "${cmd}: Invalid rotator mode - script aborted."
    exit 3
    breaksw
endsw
shift
#else
#    osirisScriptMsg -T "${cmd}: Invalid angle or rotator mode specified"\
#	"- script aborted."
#    exit 3
#endif

# Check for other flags
while ($#argv != 0)
#echo "${cmd}: Checking ${1}..."
  switch ($1)
    case -m:
      set sim
      set cmdpre = "echo ${cmd}: sim:"
      set cmdsuf = "-m"
      breaksw
    case -i*:
      set imager_pa
      breaksw  
    case -s*:
      unset imager_pa
      breaksw  
    default:
      echo "${cmd}: Invalid command line flag $1 specified."
      echo "${cmd}: Usage: ${cmd} filter_name [-m]"
      # set the error code for an error with command line input
      exit 3
      breaksw
  endsw
  shift
end
unset noglob

# Do not rotate if AO loops are closed
set ttloop = `show -s ao -terse aodtstat`
set error1 = $status
set dmloop = `show -s ao -terse aodmstat`
set error2 = $status
if (($error1 != 0) || ($error2 != 0)) then
    osirisScriptMsg -T "${cmd}: Unable to get AO status - script aborted."
    exit 4
endif
if (("$ttloop" != "open") || ("$dmloop" != "open")) then
    osirisScriptMsg -T "${cmd}: AO loops are not open - unable to rotate."
    exit 4
endif

# Do not rotate if guiding
set guiding = `show -s dcs -terse autactiv`
set error = $status
if ($error != 0) then
    osirisScriptMsg -T "${cmd}: Unable to get guider status - script aborted."
    exit 4
endif
if ("$guiding" == "yes") then
    osirisScriptMsg -T "${cmd}: Guiding is active - unable to rotate."
    exit 4
endif

# Get the instangle
set instangl = `show -s dcs -terse instangl`

# Check if imager PA is passed--default is spec-centric
if ($?imager_pa) then
  set angle = `math "$angle + $Iang"`
endif

# Add the instangle to the desired PA
set rotpos = `math $angle + $instangl`

# Configure the rotator
$cmdpre modify -s dcs rotdest=$rotpos
$cmdpre modify -s dcs rotmode="$mode"
sleep 5

# Wait until rotator is tracking
echo "${cmd}: Waiting until rotator is tracking."
set test = `show -s dcs -terse rotstat`
while("$test" != "tracking")
    echo -n .
    sleep 1
    set test = `show -s dcs -terse rotstat`
end
echo "OK"

# If in NGS mode, trigger the application of any existing DAR offset
set aomode = `show -s ao -terse aoopsmode`
if ($aomode == 0) then
    $cmdpre modify -s ao obfmgoim=1
endif

# Update global server keywords for header PA
osirisHeaderPA

goto done

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

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

