#!/bin/csh -f
#+
#OSIRIS library of scripts
#
#NAME
#     osirisCoordI2S - convert IMAG pixel coords to instrument coord (in ")
#
#SYNOPSIS
#     osirisCoordI2S x y
#
#DESCRIPTION
#     this script will calculate the instrument coordinates (that is x and y
#     offsets in arcsec from the center of the spec field of view) from
#     a pixel (x,y) on the imager.
#
#OPTIONS
#     x y
#          imager pixel to be converted to instrument coordinates.
#          only integer pixel coordinates are allowed - if decimal values are
#          given, they are automatically truncated to the integer-only part.
#
#EXAMPLES
#     osirisCoordI2S 900 700
#          Imager pixel 900,700 will be converted into instrument coordinates
#
#ENVIRONMENT VARIABLES
#     list of environment variables used
#
#FILES
#     list of files used
#
#SERVERS & KEYWORDS
#     list of the servers and keywords used, for example:
#
#     service = osiris
#          keywords: sfilter/ifilter, sscale
#
#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:
# 20050222 - MB: Initial version created
# 20050823 - MB: Cleaned up and updated imager PA and offsets
# 20051118 - MB: Automatically truncates decimal pixels to integer-only
# 20051119 - MB: Rounds output to three decimal places
# 20101104 - jlyke: Change truncation to rounding
# 20181020 - jlyke: New OSIRIS imager 2kx2k, 10mas pix
#

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

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

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

# End of help/syncheck boiler plate.

# Set this variable for testing mode
#set test

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

# Set default variable values
set fcmd = $0
set cmd = ${fcmd:t}
set DtoR = `math 3.1415926536 / 180`

# Get the command line arguments (automatically truncate to integer-only part)
#set Xi = `echo $1 | cut -f1 -d"."`
#set Yi = `echo $2 | cut -f1 -d"."`
set Xi = `round $1`
set Yi = `round $2`

set X = $1
set Y = $2

# Check that the supplied imager coordinates are valid
if (($Xi < 0) || ($Xi > 2047)) then
    osirisScriptMsg -T "${cmd}: Imager x-coordinate <${Xi}> is out of range"\
	"[0,2047] - script aborted."
    exit 3
endif

if (($Yi < 0) || ($Yi > 2047)) then
    osirisScriptMsg -T "${cmd}: Imager y-coordinate <${Yi}> is out of range"\
	"[0,2047] - script aborted."
    exit 3
endif

# Parameters for IMAG and SPEC fields in the instrument coordinate plane
# note that instrument coord plane is defined by the SPEC 20mas scale
set PSi = 0.0091     # imager platescale in "/pix
set Xicp = 1024       # imager center x-pixel
set Yicp = 1024       # imager center y-pixel
set Xica = -16.216     # imager center x-offset from spec center
set Yica = -14.783      # imager center y-offset from spec center
set Iang = 42.5      # imager rotation from spec in degrees
set Iang_r = `echo "${DtoR} * $Iang" | bc -l` #convert to radians

# Calculate new coords
set rix = `echo "$Xi - $Xicp" | bc -l`
set riy = `echo "$Yi - $Yicp" | bc -l`

set rx = `math $X - $Xicp`
set ry = `math $Y - $Yicp`

set r_pix = `echo "sqrt(${rx}^2 + ${ry}^2)" | bc -l`
set r = `echo "$PSi * $r_pix" | bc -l`

if (($rix == 0) && ($riy == 0)) then
    set ang = `echo "90 * $DtoR + $Iang_r" | bc -l`
    set r = 0
else if (($rix == 0) && ($riy > 0)) then
    set ang = `echo "90 * $DtoR + $Iang_r" | bc -l`
else if (($rix == 0) && ($riy < 0)) then
    set ang = `echo "270 * $DtoR + $Iang_r" | bc -l`
else if (($rix >= 0) && ($riy >= 0)) then
    set ang = `echo "a($ry/$rx) + ${Iang_r}" | bc -l`
else if (($rix < 0) && ($riy >= 0)) then
    set ang = `echo "(180 * $DtoR) + a($ry/$rx) + ${Iang_r}" | bc -l`
else if (($rix < 0) && ($riy < 0)) then
    set ang = `echo "(180 * $DtoR) + a($ry/$rx) + ${Iang_r}" | bc -l`
else if (($rix >= 0) && ($riy < 0)) then
    set ang = `echo "a($ry/$rx) + ${Iang_r}" | bc -l`
endif

# Calculate the positions in the instrument coordinates (to 3 decimals)
set Sx = `echo "$r * c(${ang}) + $Xica" | bc -l`
set Sx = `echo $Sx | awk '{printf "%0.3f\n", $1}'`
set Sy = `echo "$r * s(${ang}) + $Yica" | bc -l`
set Sy = `echo $Sy | awk '{printf "%0.3f\n", $1}'`

# Print variables for testing mode
if ($?test) then
    echo Rx,Ry $rx $ry
    echo Rix,Riy $rix $riy
    echo r_pix $r_pix
    echo r $r
    set ang_d = `echo "$ang / $DtoR" | bc -l`
    echo ang $ang radians, $ang_d degrees
    echo Imager $X $Y
    echo Imager $Xi $Yi
    echo Spec $Sx $Sy
    echo ""
endif

# Return the instrument coordinates
echo $Sx $Sy

goto done

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

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