#!/bin/csh -f
# Create an average sky from specified images

if ( $#argv < 5 ) then
caterr <<'EOF'
Create an average sky from specified images
  usage: skyavg2 result threshold area image1 [...]
  Arguments: result - average sky image file to be created
	     threshold - the detection threshold in sky sigma
	     area - the minimum detection area
	     image1 ... - images to be used
  The objects in the images are detected with the specified detection
  threshold and minimum area and a 5 x 5 detection filter.  The images
  are then averaged excluding the detected objects.  If objects overlap
  on all images a zero value is assigned.  A temporary working directory
  skyavg.tmp is created and removed.
'EOF'
exit
endif

set sky=$1
set th=$2
set ar=$3
shift
shift
shift
# Make working directory
mkdir skyavg.tmp
foreach i ($argv)
ln $i skyavg.tmp
end
cd skyavg.tmp

set cat=($argv)
set i=1
while ($i <= $#argv)
set cat[$i]=${argv[$i]}.cat
setcat $cat[$i] >&/dev/null <<EOF
y
$argv[$i]











$th
$th

$ar



y
y
EOF
detect $cat[$i]
@ i++
end
avgsky ../$sky $cat
cd ..
rm -r skyavg.tmp
