#!/bin/csh -f
#+
#
# gomark
#
# moves to the position defined by the keywords "raoffset" and
# "decoffset", which are normally loaded by the command "mark".
#
#-
#
# 20051122 - MB: added -noask flag to wfao
# 20190322 - jlyke: Only send move if above a threshold
# 20210909 - jlyke: Only send moves if OSIRIS is selected instrument
#

# set threshold for moves (milliarcseconds)
set thresh = 1

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

if ("$1" == "-h") then
    help $0
    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 ` 
 unset noglob

if ("$CheckStatus" != "OK") then
    help $0
    exit 1
endif

# End of help/syncheck boiler plate.

# Only send move if we are the selected instrument
set currinst = `show -s dcs -terse currinst`
set myinstr = `printenv INSTRUMENT | /usr/bin/tr "[a-z]" "[A-Z"]`
if ( $currinst != $myinstr ) then
  echo "$myinstr is not selected instrument: $currinst.  Exiting".  
  exit 
endif

# getTelOffsets is in arcseconds
set curr_off = (`getTelOffsets`)
set curr_ra_off = $curr_off[1]
set curr_dec_off = $curr_off[2]
set ra_off = `show -s osiris -terse raoffset`
set dec_off = `show -s osiris -terse decoffset`
# find the absolute value of the difference between the two
set ra_diff = `math "$curr_ra_off - $ra_off" | sed -e 's/-//g'`
set dec_diff = `math "$curr_dec_off - $dec_off" | sed -e 's/-//g'`

set ra_diff_mas = `math "$ra_diff x 1000" | cut -f 1 -d.`
set dec_diff_mas = `math "$dec_diff x 1000" | cut -f 1 -d.`
if ($ra_diff_mas >= $thresh || $dec_diff_mas >= $thresh) then
  wfao -noask
  modify -s dcs raoff = $ra_off decoff = $dec_off rel2base = t
else
  echo "Requested moves: $ra_diff_mas $dec_diff_mas (mas) less than threshold: $thresh"
  exit
endif
