# Script to automate taking exposures of a supernova
# in UBVRI filters
# Adapt this script to other SNe by changing coordinates.
# R.A. Jansen    April 4 1995.
# P.M. Garnavich Feb 1997
# S. Jha	 Sept 1997
# S. Jha         June 1999 added abort features
# Watch out for 4-shooter offset !!!!!
# M. Hicken 	July 2005 expU=60 (from 45), expB=40 (from 30)
# M. Hicken May 2006 detects correct filter position and expr=10, expi
# =12, expR=10, expI=12

#-----------------------------Make changes here for new SNe
# set exposure times for different filters
set expU=0
set expB=40
set expV=20
set expR=10
set expI=12
set expr=10
set expi=12
#------------------------------------------------------------

echo "This script will take a UBVRI standard sequence, it assumes"
echo "that you have already positioned the telescope correctly, "
echo "including offseting if necessary."

echo -n "Please enter the standard field name (e.g. PG1047):  "
set name=$<

set OFILT=`tele filter`
echo -n "START FILT = " $OFILT
set SFILT=`grep -n $OFILT /Realtime/lib/filt.dat | cut -f1 -d:`
echo " SLOT = "$SFILT

if ( { test -f /tmp/ccd_ram_prog } ) then
   cp /tmp/ccd_ram_prog /tmp/rudy.pre.prog
endif
echo "Setting for small files"
ccd bin 2 

# make sure right name appears in title and comment field
object "$name"
setcom kepccd Object "$name"

# make sure abort stuff is in place
if (!(${?scriptdir})) then
	echo "No scriptdir set, using default: $HOME/scripts"
        set scriptdir = $HOME/scripts
endif

if (!(${?snabortfile})) then
	echo "No abortfile set, using default: $scriptdir/.SNABORT"
	set snabortfile = $scriptdir/.SNABORT
endif

#if (-e $snabortfile) source $scriptdir/snabort.script
## change filter to U and take exposures of 'expU' seconds
#echo -n "Moving filter wheel to "
#set filter="U"
#set nfilt=`grep -n $filter /Realtime/lib/filt.dat | cut -f1 -d:`
#if ($nfilt != "") then
#  tele filter $nfilt
#  echo -n "Moving Focus -115 for UH "
#  tele focus -115
#  echo "Starting U exposure 1 ($expU sec) at "`date`
#  ccd gowait $expU 
#  dstore
#  echo "U exposure 1 done"
#  echo -n "Moving Focus Back "
#  tele focus +115
#endif

if (-e $snabortfile) source $scriptdir/snabort.script
# change filter to B and take exposures of expB seconds
echo -n "Moving filter wheel to "
set filter="B"
set nfilt=`grep -n $filter /Realtime/lib/filt.dat | cut -f1 -d:`
if ($nfilt != "") then
  tele filter $nfilt
  echo "Starting B exposure 1 ($expB sec) at "`date`
  ccd gowait $expB
  dstore
  echo "B exposure 1 done"
endif

if (-e $snabortfile) source $scriptdir/snabort.script
# change filter to V and take exposure of expV 
echo -n "Moving filter wheel to "
set filter="V"
set nfilt=`grep -n $filter /Realtime/lib/filt.dat | cut -f1 -d:`
if ($nfilt != "") then
  tele filter $nfilt
  echo "Starting V exposure 1 ($expV sec) at "`date`
  ccd gowait $expV 
  dstore
  echo "V exposure 1 done"
endif

if (-e $snabortfile) source $scriptdir/snabort.script
# change filter to R and take an exposure of expR
# seconds
echo -n "Moving filter wheel to "
set filter="R"
set nfilt=`grep -n $filter /Realtime/lib/filt.dat | cut -f1 -d:`
if ($nfilt != "") then
  tele filter $nfilt
  echo "Starting R exposure 1 ($expR sec) at "`date`
  ccd gowait $expR 
  dstore
  echo "R exposure 1 done"
endif

if (-e $snabortfile) source $scriptdir/snabort.script
# change filter to I and take an exposure of expI
# seconds
echo -n "Moving filter wheel to I"
set filter="I"
set nfilt=`grep -n $filter /Realtime/lib/filt.dat | cut -f1 -d:`
if ($nfilt != "") then
  tele filter $nfilt
  echo "Starting I exposure 1 ($expI sec) at "`date`
  ccd gowait $expI 
  dstore
  echo "I exposure 1 done"
endif
                                                                                
if (-e $snabortfile) source $scriptdir/snabort.script
# change filter to r and take an exposure of expr
# seconds
echo -n "Moving filter wheel to r"
set filter="r" 
set nfilt=`grep -n $filter /Realtime/lib/filt.dat | cut -f1 -d:`
if ($nfilt != "") then
  tele filter $nfilt
  echo "Starting r exposure 1 ($expr sec) at "`date`
  ccd gowait $expr
  dstore
  echo "r exposure 1 done"
endif
                                                                                
if (-e $snabortfile) source $scriptdir/snabort.script
# change filter to i and take an exposure of expi
# seconds
echo -n "Moving filter wheel to i"
set filter="i"
set nfilt=`grep -n $filter /Realtime/lib/filt.dat | cut -f1 -d:`
if ($nfilt != "") then
  tele filter $nfilt
  echo "Starting i exposure 1 ($expi sec) at "`date`
  ccd gowait $expi
  dstore
  echo "i exposure 1 done"
endif

echo -n "Returning Filter wheel to Filter $OFILT"
tele filter $SFILT
if ( { test -f /tmp/rudy.pre.prog } ) then
   echo "Returning program to original state"
   ccd program -f /tmp/rudy.pre.prog
   \rm -f /tmp/rudy.pre.prog
endif

# end message
echo " "
echo "All Done, Thank You"
echo " "
