#!/bin/csh
#checks a candidate file for a 'good candidate
# if so then it submits a new request for the search
# a non-zero exit code occurs if the magdiff is too large or 
#                      not enough matches are made
#  1/17/93 put more error messages also changed sign of magdiff
#  3/4/93  put in OBSERVER
#  5/7/93 change MIN_MATCH from 3-->2
# 3/17/94 revised heavily
# 4/1/94 added sn_sendmail
# 4/14/94 remove non submit messages
# 4/16/94 put in exit 1 if magdiff or sky bad
# 7/25/94 modify ps for Solaris
# 9/16/94 sendmail moved to sn_search


if ($#argv < 1) then
	stderr "sn_submit: no candidate file specified"
	exit 1
endif

set fname=$argv[1]
set datid=$fname:r

if (-e $fname == 0) then
	stderr "sn_submit: $fname not found "
	exit 1
endif


set MIN_MATCH=1		# minimum number of fiducial stars
set MAX_MAGDIFF=-1.0 # as much cloud as you can tolerate 
set MAX_SKY=1000	# maximum absolute sky value	
set MIN_MAG=17.5    # minimum candidate magnitude
set MAX_SKYSIG=100  # maximum sky sigma value of candidate phot 
set DELAY=3600      # number of time to wait before re-submission
set TMP=snsub$$.tmp 


#require fiducial star matching
set nmatch=`picksym file=$fname nmatch default=0` 
if ( $nmatch <  $MIN_MATCH ) then
#	echo sn_submit: $fname skipped nmatch=$nmatch 
	exit 0
endif

#check to see the extinction
set magdiff=`picksym file=$fname magdiff3`
if (`val "$magdiff <  $MAX_MAGDIFF"` ) then
#	echo "sn_submit: $fname skipped magdiff=$magdiff"
	exit 1
endif

set sky=`picksym file=$fname sky`
if (`val "$sky >  $MAX_SKY"` ) then
#	echo "sn_submit: $fname skipped sky=$sky"
	exit 1
endif


#now we look at the candidates 
#if you want to confine to the galaxy box
#let box12=`picksym file=$fname newbox`
#subheader $fname |listshift - box=12  > $TMP 
# 

subheader $fname 							|	\
	awk '( $6 < '$MIN_MAG')    {print $0}' 	|	\
	awk '( $5 < '$MAX_SKYSIG') {print $0}'  > $TMP	


if ( -z $TMP ) then
#	echo "sn_submit: $fname no suitable candidates"
	rm $TMP
	exit 0
endif

set bs=`sort -n +5 $TMP | head -1` 
mv $TMP snsubmit.tmp

#you have now made it thru the labors of Hercules and we will write
# up a request
# if insgen is present

ps -u $LOGNAME | grep -v grep |grep -v tail | grep -s insgen 
if ($status) then
	echo "sn_submit: $fname insgen not found"
	exit 1
endif

set object=`picksym file=$fname object`
set req=`mkid`.rqs
echo "sn_submit: $fname submits $object $req mag=$bs[6] in $DELAY secs"

#now we get to the meat where the request file is written up
#

#copy the fits header
set today=`utday`

grep RA $fname 				>> $req
grep DEC $fname 			>> $req
grep EPOCH $fname 			>> $req
grep OBJECT $fname 			>> $req
grep FILTERS $fname		 	>> $req
grep EXPTIME $fname		 	>> $req
echo "PROCEDUR = 'sn' " 	>> $req
echo "DAYEND   = '$today'" 	>> $req
echo "NUM-OBS = 1  " 		>> $req
echo "PRIORITY = 1  " 		>> $req
echo "OBSERVER = 'SN'  " 	>> $req
echo END       				>> $req

# now we sleep before submitting
sleep $DELAY

mv $req $HOME_DIR/targets
tin newreqs $req

exit 0
