#!/bin/csh -f

#+
# twilight_zone_setup - runs setup scripts for de Pater, de Kleer, Molter cadenced twilight observations of planets
#
# Purpose:
#
#       Make running the Twilight Zone observing program as straightforward as
#		possible for observers and OA's without any affiliation to the project.
#		Defines easy-to-use variables that run the setup scripts requested
#		for the project.
#
# Usage:
#
#       ./twilight_zone_setup [-timeit] [-target <value>]
#
# Arguments:
#
#       target:      Name of the planet or moon you're observing (options Io, Titan, Uranus, Neptune)
#       timeit:      Print start and end times to logfile - used for testing
#
# Output:
#
#		Changes in filters, subarrays, other settings to set up observations
#		If -timeit 1, output a .log file containing start and end time
#
# Help:
#		./twilight_zone_setup -h
#
# Examples:
#
#      twilight_zone_setup -target Uranus
#
# Modification history:
#
#       2017-07-10     Ned Molter         Original version.
#	2019-07-02     Erin Redwing	  Modified for OSIRIS
#       2020-12-28     CAAI               Adapted to the OSIRIS Linux upgrade
#-----------------------------------------------------------------------






# set usage message

set buf = $0
set cmd = $buf:t
set usage = "Usage: $cmd [-target <value>] [-timeit]"

# Set defaults...

set target = 0
set timeit = 0
set rottest = 269.50
set wfaotest = on
#set rottest = 0.00
#set wfaotest = off
modify -s osiris scriptrun = 0
modify -s osiris scriptreply = y
sleep 3

# check if any inputs at all - common mistake
if ( "$1" == "" ) then
	echo "No flags specified."
	echo "Exiting. Please use flag -h to see possible integration times, filters, and usage."
	exit 1
endif

while (1)

    if ( "$1" == "-h" || "$1" == "-help" ) then
	echo "--------------------------"
	echo "--- twilight_zone help ---"
	echo "--------------------------"
	echo "PURPOSE: Set up short OSIRIS observations of planets with minimal"
        echo "user input. Companion to twilight_zone"
	echo " "
	echo "USAGE: twilight_zone_setup [-target <value>]"
	echo " "
	echo "EXAMPLE: twilight_zone_setup -target Uranus"
	echo " "
	echo "FLAGS:"
	echo "-target :: Specifies which body you are observing."
	echo "  Possible inputs: Io, Titan, Uranus, Neptune"
	echo "                           "
	echo "    target   //   run time"
	echo "        Io   //   X minutes"
	echo "     Titan   //   X minutes"
	echo "    Uranus   //   X minutes"
	echo "   Neptune   //   X minutes"
	echo "--------------------------"

	exit 1

    else if ( "$1" == "-timeit" ) then
        echo "Timing mode enabled. Time spent on each filter will be shown."
        set timeit = 1
        shift
        continue

	else if ("$1" == "-target") then
		set target = {$2}
	    shift
	    shift
	    continue

	# exit flag check if no flags remain...
	else if ("$1" == "") then
		break

	#errant flag check
	else
		echo "Invalid flag specified: $1"
		echo "Exiting. Please use flag -h to see possible integration times, filters, and usage."
		exit 1

	endif

end



#make log file if running timeit
if ( $timeit == 1 ) then
	set now = `date`
	set outdir = `gshow -terse -s oids outdir`
	set datetime = `date +%y-%m-%d_%H:%M`
	set logfile = $outdir"/tz_setup_$datetime.log"
	echo "-----------------------------------------" > $logfile
	echo "-----$now-----" >> $logfile
	echo "-------------New Execution---------------" >> $logfile
endif

################
### Io stuff ###
################

if ( $target == io || $target == Io || $target == IO ) then

	wait4ao $wfaotest
	wait4dar off
	set rotdest = `gshow -s dcs -terse rotdest`
	if ( $rotdest != $rottest ) then
		echo "Rotdest must be equal to -42.5."
		exit 1
		endif

	set cmd = `tz_convert Io setup` # setup ddf, filt1, tint 1.5, coadds=1




###################
### Titan stuff ###
###################

else if ( $target == titan || $target == Titan || $target == TITAN ) then

	# rotdest error message

	wait4ao $wfaotest
	wait4dar off
	set rotdest = `gshow -s dcs -terse rotdest`
	if ( $rotdest != $rottest ) then
		echo "Rotdest must be equal to -42.5."
		exit 1
		endif

	set cmd = `tz_convert Titan setup` # setup ddf, kp, tint 1.5, coadds=1




####################
### Uranus stuff ###
####################

else if ( $target == uranus || $target == Uranus || $target == URANUS ) then

	wait4ao $wfaotest
	wait4dar off
	set rotdest = `gshow -s dcs -terse rotdest`
	if ( $rotdest != $rottest ) then
		echo "Rotdest must be equal to -42.5."
		exit 1
		endif

	set cmd = `tz_convert Uranus setup` # setup ddf, kp, tint 1.5, coadds=1



#####################
### Neptune stuff ###
#####################

else if ( $target == neptune || $target == Neptune || $target == NEPTUNE ) then

	wait4ao $wfaotest
	wait4dar off
	set rotdest = `gshow -s dcs -terse rotdest`
	if ( $rotdest != $rottest ) then
		echo "Rotdest must be equal to -42.5."
		exit 1
		endif

	set cmd = `tz_convert Neptune setup` # setup ddf, filt1, tint 1.5, coadds=1


### Invalid target catch ###
else
	echo "Invalid target name. Choose from Io, Titan, Neptune, and Uranus."
	echo "Exiting. Please use flag -h to see usage."
	exit 1
endif

eval $cmd
echo $target "setup script complete"

if ( $timeit == 1 ) then
	set now = `date`
	echo "-------------End Execution---------------" >> $logfile
	echo "-----$now-----" >> $logfile
	echo "-----------------------------------------" >> $logfile
endif
