#!/bin/bash

#Procedure: 
#1. slew to target ideally with rotator mode EQU -90.2
#2. start guide
#3. in FIREAcq use Slit mode to put on slit
#4. stop guiding
#6. prepare input for new firescan command (firescan Slitwidth Exposure) 
#where Slitwidth is in arcsec, Exposure is desired exposure time

SLIT=$1 # slit width in arcsec
EXP=$2 # exposure time in sec
SAMP=$3 # semi-amplitude of offslit motion
DITH=$4 # dithering semi-amplitude

if [ "$1" = "" ]; then
    echo "Slit width defaults to 0.45 arcsec"
    SLIT=0.45
fi

if [ "$2" = "" ]; then
    echo "Exposure time defaults to 5 sec"
    EXP=5
fi

if [ "$3" = "" ]; then
    echo "Semi-amp across the slit defaults to 1.35 arcsec"
    SAMP=1.35
fi

if [ "$4" = "" ]; then
    echo "Dithering defaults to 1.5 arcsec"
    DITH=1.5
fi

#set SIGN = $3 # 1 or -1 direction of rate to apply negative=South
rate=`echo $SLIT $EXP |awk '{print ($1/$2)}'` 
negrate=`echo $SLIT $EXP |awk '{print -($1/$2)}'` 
#set nrate = `echo $SLIT $EXP $SIGN |awk '{print $3*($1/$2)}'`
nrate=`echo $SLIT $EXP  |awk '{print -1.0*($1/$2)}' `
negnrate=`echo $SLIT $EXP  |awk '{print 1.0*($1/$2)}' `
ttime=`echo $rate $SAMP |awk '{print 2.0*($2/$1)}'`
exptime=`echo $ttime | awk '{print ($1)}'`
echo " "

echo "Stop guiding and WFS"
tcsset magellan1 gafone 0
sleep 1
tcsset magellan1 gbfone 0
sleep 1

tcsoffset magellan1 $DITH $SAMP 2000.0
echo "A position"
echo "offset telescope " $DITH"E " $SAMP"N"

echo " "
echo "Taking exposure of "$EXP " sec across "$SLIT " arcsec slit in the A position (+"$DITH"arcsec)"
echo "please start the exposure of " $exptime " sec and press any key when ready"
read -n 1
echo "Sleeping for 33sec waiting for the FireEngine to finish reset+readout"
echo "Press ENTER to continue of wait for 33sec"
read -t 33
echo "start track at rate" $rate
otrack 0 $nrate
echo "ttime=" $ttime
sleep $ttime
otrack 0 0 
echo "stop track"
echo "sleeping for 30 sec before moving to the B position"
echo "press Enter to continue immediately"
read -t 30
echo " "
echo "======================================================================"
echo "B position"
tcsoffset magellan1 -$DITH 0.0 2000.0
echo "offset telescope "$DITH"W 0.0N"
tcsoffset magellan1 -$DITH 0.0 2000.0
echo "offset telescope "$DITH"W 0.0N"

echo " "
echo "Taking exposure of "$EXP " sec across "$SLIT " arcsec slit in the B position (-"$DITH"arcsec)"
echo "please start the exposure of " $exptime " sec and press any key when ready"
read -n 1
echo "Sleeping for 33sec waiting for the FireEngine to finish reset+readout"
echo "Press ENTER to continue of wait for 33sec"
read -t 33
echo "start track at rate" $negrate
otrack 0 $negnrate
echo "ttime=" $ttime
sleep $ttime
otrack 0 0 
echo "stop track"

echo "Sleeping for 30sec waiting for the FireEngine to finish readout"
echo "Press ENTER to continue of wait for 30sec"
read -t 30
echo " "
echo "press any key to finish the sequence and move the telescope back"
read -n 1
tcsoffset magellan1 $DITH -$SAMP 2000.0
echo "offset telescope "$DITH"E " $SAMP"S"
echo "done"

