#!/bin/csh -f
# Clean objects from an image

if ( $#argv < 3 ) then
caterr <<'EOF'
Clean objects from an image
  usage: autoclean image threshold1 threshold2 [filter options]
  Arguments: image - image file to be cleaned
	     threshold1 - detection sigma above sky
	     threshold2 - detection sigma below sky
	     filter options - objects to be cleaned
  The objects in image are detected with no detection filter and
  no minimum area.  Then clean(2) is called with a grow parameter
  of 1 and the specified filter options.  A temporary working directory
  clean.tmp is created and removed.  
  Warning: The image file is modified
'EOF'
exit
endif

set image=$1
set th0=$2
set th1=$3
shift
shift
shift

mkdir clean.tmp
ln $image clean.tmp
cd clean.tmp

setcat ${image}.cat >& /dev/null <<EOF
y
$image











$th0
$th1

1
EOF
detect ${image}.cat
clean 1 $* <${image}.cat
cd ..
rm -r clean.tmp
