#! /bin/sh

# 
#  Copyright (C) 2009-2010  Smithsonian Astrophysical Observatory
#
#
#  This program is free software; you can redistribute it and/or modify
#  it under the terms of the GNU General Public License as published by
#  the Free Software Foundation; either version 3 of the License, or
#  (at your option) any later version.
#
#  This program is distributed in the hope that it will be useful,
#  but WITHOUT ANY WARRANTY; without even the implied warranty of
#  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
#  GNU General Public License for more details.
#
#  You should have received a copy of the GNU General Public License along
#  with this program; if not, write to the Free Software Foundation, Inc.,
#  51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
#


MYNAME=pileup_map

#
# Load parameters
#    sed is there to escape any ()'s
#
for pp in `pdump ${MYNAME} | cut -d= -f1 | grep -v '^# EOF'` 
do
  eval ${pp}=`echo $pp | pquery ${MYNAME} - "$@" | sed 's/[ ()\[]/\\\&/g'`
done

#
# Just convolve w/ 3x3 unormalized box and divide by the number of exposures
#
aconvolve infile="$infile" outfile=- kernelspec="lib:box(2,1,3,3)" \
     meth=slide norm=none edge=const const=0 | \
  dmimgcalc infile=- infile2=none outfile="$outfile" \
     op="imgout=(img1/((float)(img1_exposure/img1_exptime)))" clobber=$clobber

if test $? -ne 0
then
  echo "" 1>&2
  echo "# ${MYNAME} : Error making pileup map" 1>&2
  echo "" 1>&2
  exit 1
fi

dmhedit "$outfile" file= op=add key=BUNIT value="'count/exposure'"

dmhistory "$outfile" pileup_map action=put


exit 0
