#!/bin/csh -f
# Create complementary skys from a pair of images

if ( $#argv < 3 ) then
caterr <<'EOF'
Create complementary skys from a pair of images
  usage: skys2 threshold area image1 image2
  Arguments: threshold - detection threshold in sky sigma
	     area - minimum detection area in pixels
	     image1 image2 - images to extract skys for

  A difference image is formed and the specified detection threshold
  and minimum area is used with a 5 x 5 filter to detect objects in
  the image.  The detected objects are then cleaned from the raw images
  with a growing parameter of 1 and files image1.sky and image2.sky
  are created.  A temporary working directory skys2.tmp is created and
  removed.
'EOF'
exit
endif

set th=$1
set ar=$2
shift
shift
# Make working directory
mkdir skys2.tmp
cd skys2.tmp

cp ../$1 a$1
cp ../$2 a$2
opimg a$1 a$2 - = skys2 >& /dev/null
setcat skys2.cat >&/dev/null <<EOF
y
skys2











$th
$th

$ar



y
y
EOF
detect skys2.cat
mv a$1 skys2
clean 1 G D <skys2.cat
mv skys2 ../${1}.sky
mv a$2 skys2
clean 1 F D <skys2.cat
mv skys2 ../${2}.sky
cd ..
rm -r skys2.tmp
endif
