#!/bin/csh -f
# load the file, determine bias and sky, then extract a subraster
# write out the subraster 

setenv PATH /bin:/usr/bin:/usr/local/bin:/usr/bin/X11:/apps/elixir/bin

set tmpout=""
set args=""
while ("$1" != "") 
 switch ($1)
  case -output: 
   shift
   set tmpout=$1
   breaksw;
  case -*: 
   goto escape
   breaksw;
  default:
   set args=($args $1);
   breaksw;
 endsw
 shift
end

if ($#args != 3) goto escape

# kill off other processes with this name, and their children
set procname=getfocus
set list=`/bin/ps -eo pid,user,comm | awk -v n=$procname -v m=$$ '($3==n)&&($1!=m){print $1}'`
foreach pid ($list)
 echo "kill $pid"
 set children=`/bin/ps -eo pid,ppid | awk -v p=$pid '($2==p){print $1}'`
 kill $pid
 foreach child ($children)
   echo "kill $child"
   kill $child
 end
end

set fdir=$args[1]
set name=$args[2]
set mode=$args[3]

# set script=/apps/elixir/config/mana/focus.pro
set datdir  = `gconfig DATDIR`
set confdir = `gconfig CONFDIR`

# sextractor needs full paths to:
set sexconf = "$confdir/sextract/focus.sex"
set sexpars = "$confdir/sextract/focus.param"
set sexfilt = "$confdir/sextract/default.conv"
set sexnnw  = "$confdir/sextract/default.nnw"

set script  = "$confdir/mana/focus.pro"
set output1 = "$datdir/plots/focus.unit.gif"
set output2 = "$datdir/plots/$name.focus.gif"
if ("$tmpout" != "") then 
 set output1=$tmpout.ppm
 set output2=$tmpout.gif
endif

# test named Xserver, or start vnc on specified machine
set xhost = `gconfig XHOST.SUMMIT`
set xdisp = `gconfig XDISP.SUMMIT`
xdpyinfo -display $xdisp >& /dev/null
if ($status) then
  # X server is not running. start it now
  echo "X server is not running. start it now"
  exit 1;
endif
setenv DISPLAY $xdisp

set temp=`mktemp /tmp/focus.XXXXXX`
rm -f $temp

if ("$mode" == "on") goto mef
if ("$mode" == "off") goto split
echo "invalid mode $mode in getfocus"
exit 1;

### SPLIT mode
split:

# make sure all four files exist
foreach ccd (01 04 07 10)
 block $fdir/$name/$name$ccd.fits -t 60 || goto failure;
end

foreach ccd (01 04 07 10)
 sex $fdir/$name/$name$ccd.fits \
    -c $sexconf \
    -PARAMETERS_NAME $sexpars \
    -FILTER_NAME     $sexfilt \
    -STARNNW_NAME    $sexnnw \
    -CATALOG_NAME    $temp.$ccd; \
    mv $temp.$ccd $temp.sdat.$ccd &
end
# sextract writes output to catalog while processing;
# mv to real file *after* sextract is done

foreach ccd (01 04 07 10)
 block $temp.sdat.$ccd -t 60 || goto failure;
 awk '($6 > -14) && ($6 < -8){print $0}' $temp.sdat.$ccd | sort -k 6n | head -300 | sort -k 2n > $temp.$ccd.sdat
 rm -f $temp.sdat.$ccd
end

rm -f $temp.pro
set ccd=00
echo "plotfocus $temp $temp.ppm $name $fdir/$name/$name$ccd.fits" >> $temp.pro
mana --norc --only $script $temp.pro
if ($status) goto failure

# save resulting image for posterity
ppmtogif < $temp.ppm > $output1
cp $output1 $output2

rm -f $temp $temp.sdat $temp.pro $temp.ppm
foreach ccd (01 04 07 10)
 rm -f $temp.$ccd.sdat 
end
exit 0;

### MEF mode
mef:
 
block $fdir/$name.fits -t 60 || goto failure;

rm -f $temp.pro
foreach ccd (01 04 07 10)
 echo "split $fdir/$name.fits $ccd $temp.$ccd.fits" >> $temp.pro
end

mana --norc --only $script $temp.pro

foreach ccd (01 04 07 10)
 sex $temp.$ccd.fits \
    -c $sexconf \
    -PARAMETERS_NAME $sexpars \
    -FILTER_NAME     $sexfilt \
    -STARNNW_NAME    $sexnnw \
    -CATALOG_NAME    $temp.$ccd; \
    mv $temp.$ccd $temp.sdat.$ccd &
end
# sextract writes output to catalog while processing;
# mv to real file *after* sextract is done

foreach ccd (01 04 07 10)
 block $temp.sdat.$ccd -t 30 || goto failure;
 awk '($6 > -14) && ($6 < -8){print $0}' $temp.sdat.$ccd | sort -k 6n | head -300 | sort -k 2n > $temp.$ccd.sdat
 rm -f $temp.sdat.$ccd
end

rm -f $temp.pro
set ccd=01
echo "plotfocus $temp $temp.ppm $name $temp.$ccd.fits" >> $temp.pro
mana --norc --only $script $temp.pro
if ($status) goto failure

# save resulting image for posterity
ppmtogif < $temp.ppm > $output1
cp $output1 $output2

rm -f $temp $temp.sdat $temp.pro $temp.ppm
foreach ccd (01 04 07 10)
 rm -f $temp.$ccd.sdat $temp.$ccd.fits
end
exit 0;

failure: 
 echo "****** error with focus"
 rm -f $temp $temp.sdat $temp.pro
 foreach ccd (01 04 07 10)
  rm -f $temp.$ccd.sdat $temp.$ccd.fits
 end
 exit 1;

escape:
 echo "USAGE: getfocus (fdir) (file) (mode)"
 echo "(mode) may be 'on' for mef, 'off' for split"
 exit 2;


 sex $temp.$ccd.fits -c $config -CATALOG_NAME $temp.sdat
 sort -k 2n $temp.sdat | head -300 > $temp.$ccd.sdat

rm -f $temp.pro
rm -f $temp.ppm
echo "plotfocus $temp $temp.ppm $name $fdir/$name.fits" >> $temp.pro
mana --norc --only $script $temp.pro
if ($status) goto failure

