#!/bin/csh
# assesses the image quality of an image 
# places the following strings into the .coo file
#   MSKY
#	MSKYSIG
#	MNSTAR (if possible)
#	MFWHM (if possible)

# 7/27/93 symbols not placed if no stars present
# 3/21/94 stat median now used instead of stat
# 5/30/96 cosmetic changes

if ($#argv == 0) then
	stderr sn_merit: no datid specified 
	exit 1
endif

set filestem=$argv[1]

if (-e $filestem.fts == 0) then
	stderr sn_merit: $filestem.fts does not exist
	exit 1
endif

if (-e $filestem.coo == 0) then
	stderr sn_merit: $filestem.coo does not exist
	exit 1
endif

set TMP=tmp$$

#the sky gradient is determined from boxes in the corners
box 1 cr=50 cc=50 nr=50 nc=60	> /dev/null
box 2 cr=450 cc=50 nr=50 nc=60	> /dev/null
box 3 cr=50 cc=450 nr=50 nc=60	> /dev/null
box 4 cr=450 cc=450 nr=50 nc=60	> /dev/null
sky $filestem box=1 | getkey sky >  $TMP 
sky $filestem box=2 | getkey sky >> $TMP 
sky $filestem box=3 | getkey sky >> $TMP 
sky $filestem box=4 | getkey sky >> $TMP 

set t=`stat $TMP `
putsym file=$filestem.coo msky=$t[2]
putsym file=$filestem.coo mskysig=$t[3]

# now calculate the fwhm of the brighter stars
# if there are any
set t=`subheader $filestem.coo |  awk '($4 > 50) {print $5}' | stat median`

if ($t[1] > 0) then
	putsym file=$filestem.coo mnstar=$t[1]
	putsym file=$filestem.coo mfwhm=$t[2]
endif


rm $TMP

exit 0
