#!/bin/csh -f
#+
#OSIRIS library of scripts
#
#NAME
#     osirisHeaderPA - calculates SPEC and IMAG PA's and updates OGS keywords
#
#SYNOPSIS
#     osirisHeaderPA [-m]
#
#DESCRIPTION
#     calculates the current sky PA for the SPEC and IMAG, and then updates
#     the corresponding keywords in the global server.  once there, they
#     are recorded in the FITS headers.
#
#OPTIONS
#     -m
#          simulate calls to RPC servers.  during simulated calls, the 
#          command is echoed (instead of being executed).
#
#EXAMPLES
#     osirisHeaderPA
#          sets PA keywords in OGS with current values
#
#ENVIRONMENT VARIABLES
#     none
#
#FILES
#     none
#
#SERVERS & KEYWORDS
#     service = dcs
#          keywords: instangl, rotposn
#
#     service = osiris
#          keywords: pa_spec, pa_imag
#
#SCRIPTS CALLED
#     help, math
#
#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
# 20190501 - jlyke: Set to 0 if not selected instrument 
#                   Update Iang 47.5 -> 42.5
# 20210825 - jlyke: Generalize to VA mode (currently only for PA)
#

# 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 up to trap interrupts (Ctrl-C, etc.)
onintr abort

# Set default variable values
set fcmd = $0
set cmd = ${fcmd:t}
set cmdpre = ""
set cmdsuf = ""
set Iang = 42.5

# Check for command line arguments
while ($#argv != 0)
#echo "${cmd} : Checking ${1}..."
    switch ($1)
	case -m:
	    set sim
	    set cmdpre = "echo ${cmd}: sim:"
	    set cmdsuf = "-m"
	    breaksw
	default:
	    echo "${cmd}: Invalid command line flag $1 specified."
	    # set the error code for an error with command line input
	    exit 3
	    breaksw
    endsw
    shift
end

# Get currinst from DCS
set currinst =  `show -s dcs -terse currinst`

if ($currinst == "OSIRIS") then
  # Get some more dcs keywords
  set keys = (`show -s dcs -terse rotpposn parantel el instangl`)
  set rotpposn=$keys[1]
  set parantel=$keys[2]
  set el=$keys[3]
  set instangl=$keys[4]
  # Calculate the SPEC PA
  set pa_spec = `math "$rotpposn + $parantel - $el - $instangl"`
  # Calculate the IMAG PA
  set pa_imag = `math $pa_spec + $Iang`
#  # Get the instangl and current rotator position from DCS
#  set instangle = `show -s dcs -terse instangl`
#  set curangle = `show -s dcs -terse rotposn`

#  # Calculate the SPEC PA
#  set pa_spec = `math $curangle - $instangle`

#  # Calculate the IMAG PA
#  set pa_imag = `math $pa_spec + $Iang`
else
  echo "dcs.CURRINST = $currinst, setting to default values"
  set pa_spec = 0
  set pa_imag = 0
endif

# Set the PAs
$cmdpre modify -s osiris pa_spec=${pa_spec}
$cmdpre modify -s osiris pa_imag=${pa_imag}



goto done

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

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