#!/bin/csh
# shows all the images of the requested galaxy (specify Name)
# found in the file SNall
# it then presents them on the screen and asks you to kill all
# but the one you want.
# selects the image to use as the reference 
# removes the weaker stars from the .coo file
# 4/1/93 DATA_DIR now from ait.config
#         reference images are now copies
# 5/10/93 install Defect
# 8/16/93 remove SNbad
# 8/19/94 refs in sn/ref
# 10/30/94 adjust for Solaris
# 7/31/97 for KAIT

set TMP=tmp
set N_FIDUCIAL=1		#minimum number of fiducial stars

setenv HOME_DIR ~kait
set DATA_DIR=`picksym data_dir`
set REF_DIR=$DATA_DIR/sn/ref


if ($#argv < 1) then
	stderr usage: mkref galaxy_name
	exit 1
endif

set ps=`ps -f | grep tv | grep -v grep | awk '{print $2}'`
if ($#ps > 0 ) then
	while($#ps > 0 )
		stderr sorry you cannot have any tv processes running .. please kill
		set ps=`ps -f | grep tv | grep -v grep | awk '{print $2}'`
		sleep 1
	end
endif

set name=$argv[1]

echo $name

set refname=$REF_DIR/$name
set xo=0
set yo=0

if (-e $refname.fts == 1 ) then
	stderr "reference already exists" `picksym file=$refname.fts datid` 
	tv $refname zoom=.5 xo=$xo yo=$yo > /dev/null
	sleep 2
	@ xo+= 300
	if ($status) exit 1
endif

set f=`grep -w $name SNall | awk '{print $1}'`

set n=0

foreach i ($f)
	if (-e $i.fts == 0 ) then
		if (-e $i.fts.Z ) then
			uncompress $i.fts
		else
			echo $i.fts not found
			continue
		endif
	endif
	set slit=`picksym file=$i.fts quiet domeslit`
	if ($status == 0 ) then
		echo domeslit was $slit for $i
		continue
	endif
	tv $i zoom=.5 xo=$xo yo=$yo > /dev/null
	@ xo+= 300
	if ($xo > 1000) then
		set xo=0
		@ yo+= 300
	endif
	if (-e $i.coo == 0) then
		set h=$i:h
		tar xf $h/coo.tar $i.coo
		if (-e $i.coo == 0) then
			echo doing finder for $i
			sn_finder $i
			if ($status) then
				exit 1
			endif
		endif
	endif
	picksym file=$i.coo msky >& /dev/null
	if ($status) then
		echo doing status for $i
		sn_merit $i
	endif
	set msky=`picksym file=$i.coo msky`
	set mskysig=`picksym file=$i.coo mskysig`
	set mnstar=`picksym file=$i.coo mnstar`
	set mfwhm=`picksym file=$i.coo mfwhm`
	echo $i $msky $mskysig $mnstar $mfwhm
	@ n++
end

if ($n == 0 ) then
	echo No good data
	exit 1
endif

echo "--------------------"
gbeep
mbutton "Leave One Good Galaxy" "Continue" "Quit" 
if ($status) then
	set ps=`ps -f | grep tv | grep -v grep | awk '{print $2}'`
	kill -9 $ps
	exit 1
endif

set np=2
while ($np > 1)
	set ps=`ps -f | grep tv | grep -v grep | awk '{print $2}'`
	set np=$#ps
	sleep 1
end

#Determine which file was selected by the last tv process
set ps=`ps -f | grep tv | grep -v grep`
if ($#ps == 0 ) then
	echo no reference selected
	exit 1
endif
set good=$ps[9]
echo $good

kill -9 $ps[2]

set n=`subheader $good.coo | wc | awk '{print $1}'` 
set object=`picksym file=$good.coo object` 
echo nfiducials=$n

#install the proper defect file
#Removed 1/7/96
set flatid=`picksym file=$good.fts flatid`
#if (-e $REF_DIR/$flatid.pht  == 0 ) then
#	echo generating defect file
#	pushd $DATA_DIR/`echo $flatid | mkid inverse`
#	if ($status) then
#		set flatid=`ok "Enter id:"`
#		if ($status) exit 1
#	else
#		sn_defect $flatid
#		mv $flatid.pht $REF_DIR
#	endif
#	popd
#endif


#copy the image file in the SN ref directory
if (-e $refname.fts == 1) then
	rm -f $refname.fts 
endif
cp $good.fts $refname.fts


#Now let us add some extra information into the .coo (a.k.a .ref)
tv $refname zoom=.5  >& /dev/null
sleep 3

#now strip out the weaker stars

sn_filter $good.coo > $refname.ref
subheader $refname.ref | marks number

#now indicate the location of the galaxy
mbutton "mark galaxy box " "Continue" "cancel"  -Wp 600 600 >& /dev/null
if ($status) then
	set ps=`ps -f | grep tv | grep -v grep | awk '{print $2}'`
	kill -9 $ps
	rm $refname.fts $refname.ref
	exit 1
endif

box 1 int >& /dev/null
if ($status) exit 1
set c=`let box1`
putsym file=$refname.ref "galbox='$c'"
putsym file=$refname.ref "defect='$flatid.pht'"


#kill off the other tv processes
set ps=`ps -f | grep tv | grep -v grep | awk '{print $2}'`
kill -9 $ps

exit 0
