#!/bin/tcsh -ef
#
# ABOUT   : NOTCam imaging twilight sky flats.
#           Takes 8 skyflats with 10" random dithers between. 
#           The exptime must be given as an input argument.
#           Only the "expose" mode is used.
#
#           Note that you must run the script twice for each
#           camera/filter setting to have bright and faint
#           images to obtain differential flats, and the same 
#           exptime(!) must be used for bright and faint flats.
#
# USAGE   : notcam.skyflat [-e] t
#           -e      : Echo script on stdout, do not execute
#           t       : Exposure time in seconds
#
# DEPENDS : notcam.expose, notcam.teloffset, notcam.autosave_on
#           notcam.imtype, notcam.object, notcam.wait-shutter-closed
#
# PROVIDES: notcam.skyflat
#
# TFUNCT  : (Texp + Tread + Tsave + Tteloffset)*8
#
# AUTHOR  : Anlaug Amanda Djupvik
#
# HISTORY : 2008/05/20 (Initial release, AAD)
#           2009/12/04 Added notcam.clearobject at the end (AAD) 
#           2011/04/13 Added script parameters to talker log (AAD)
#           2011/09/13 Now teloffset while reading out - to speed up (AAD)
  

if ("$1" == "-e") then
  set e = "echo"
  shift
else
  set e = ""
endif

# Get command line arguments
set t = `echo "$1" | sed -n -e '/^[0-9.]*$/p'`

# Check for errors in arguments
if ($t == '') then
echo "Error in arguments"
echo "Usage : notcam.skyflat t \n"
echo "        t = exptime in seconds \n"
exit 1
endif

# Warn that if "t" in notcam.expose is < 3 seconds then shutter effects
set tt = `echo "10*$t" | bc -l | awk '{printf ("%5i"),$1}'`
if ($tt == 0) then
    echo "Zero exposure time not allowed."
    exit 1
endif
if ($tt < 30) then
echo "For t < 3 seconds shutter effects can alter the flux by more than 1%."
echo "For t = 2 seconds the errors introduced are around 3% and increase with smaller t." 
echo "Continue anyhow (y/n)?"
set answ = $<
  if ("$answ" == "n") then
     echo  " Interrupted. \n"
     exit 1
  else if ("$answ" == "y") then
     echo "Be warned!"
  else
     echo " Invalid option."
    exit 1
   endif
endif

# Write to Talker
logger -p local0.debug -t "$0" "[NOTE]: Script started as $0 $*"
#logger -p local0.debug -t "notcam.skyflat" "[NOTE]: notcam.skyflat started"

# Execute or Echo script
$e notcam.autosave_on
$e notcam.rempath /data/service/calib/
$e notcam.remsave_on
$e notcam.imtype FLAT

$e notcam.object "skyflat 1"
$e notcam.expose $t &
$e notcam.wait-shutter closed
$e notcam.teloffset -10 0
$e wait

$e notcam.object "skyflat 2"
$e notcam.expose $t &
$e notcam.wait-shutter closed 
$e notcam.teloffset 0 -10
$e wait

$e notcam.object "skyflat 3"
$e notcam.expose $t &
$e notcam.wait-shutter closed
$e notcam.teloffset 10 0
$e wait

$e notcam.object "skyflat 4"
$e notcam.expose $t &
$e notcam.wait-shutter closed 
$e notcam.teloffset 10 0
$e wait

$e notcam.object "skyflat 5"
$e notcam.expose $t &
$e notcam.wait-shutter closed
$e notcam.teloffset 0 10
$e wait

$e notcam.object "skyflat 6"
$e notcam.expose $t &
$e notcam.wait-shutter closed 
$e notcam.teloffset 0 10
$e wait

$e notcam.object "skyflat 7"
$e notcam.expose $t &
$e notcam.wait-shutter closed 
$e notcam.teloffset -10 0
$e wait

$e notcam.object "skyflat 8"
$e notcam.expose $t &
$e notcam.wait-shutter closed
$e notcam.teloffset 0 -10
$e wait

$e astrowakeup
$e notcam.remsave_off
$e notcam.clearobject
$e notcam.imtype OBJECT

# All done. Exit with error status 0 and write to Talker
logger -p local0.debug -t "notcam.skyflat" "[NOTE]: notcam.skyflat ended successfully"
exit 0



