#! /bin/sh

# test script :  create_bkg_map.t

######################################################################
# subroutine
# error_exit <message>
# Fatal error exit

error_exit()
{
  echo ""   | tee -a $LOGFILE
  echo "$1" | tee -a $LOGFILE
  echo ""   | tee -a $LOGFILE
  echo "${toolname} : FAIL" | tee -a $LOGFILE
  exit 1
}

######################################################################
# Initialization

# !!3
toolname="create_bkg_map"

# set up list of tests
# !!4
alltests="test1 test2 test3 test4 test_hrc test5"

# "short" test to run
# !!5
shortlist="test1"


# compute date string for log file
DT=`date +'%d%b%Y_%T'`


# check for directory environment variables
if test "x${TESTIN}" = "x" -o "x${TESTOUT}" = "x" -o "x${TESTSAV}" = "x" \
   -o "x${TESTLOG}" = "x" ; then
  error_exit "one or more of TESTIN/TESTOUT/TESTSAV/TESTLOG not defined" 
fi


# convenience definitions
OUTDIR=$TESTOUT/$toolname
SAVDIR=$TESTSAV/$toolname
INDIR=$TESTIN/$toolname
LOGDIR=$TESTLOG/$toolname

# set up log file name
LOGFILE=$LOGDIR/${toolname}_log.$DT

#get rid of old logs
rm -f $LOGDIR/${toolname}_log.*

# Any tests specified on command line?
if test $# -gt 0; then
  # yes, do those tests
  testlist=$*
else
  # No, see if we are to do "short" test
  if test "x$SHORTTEST" = "x" ; then
    # No, do everything
    testlist=$alltests
  else
    # yes, do short test
    testlist=$shortlist
  fi
fi

# Make sure we have a log directory
if test -d $LOGDIR ; then
 :
else
  mkdir -p $LOGDIR 
  if test $? -ne 0 ; then
    error_exit ""
  fi
fi

# Make sure we have an output directory
if test -d $OUTDIR ; then
 :
else
  mkdir -p $OUTDIR >> $LOGFILE 2>&1
  if test $? -ne 0 ; then
    error_exit "can't create output directory $OUTDIR"
  fi
fi

# Make sure we have an input directory
if test -d $INDIR ; then
:
else
  if test $? -ne 0 ; then
    error_exit "can't find input directory : $INDIR"
  fi
fi

# Make sure we have a save directory
if test -d $SAVDIR ; then
:
else
  if test $? -ne 0 ; then
    error_exit "can't find save directory : $SAVDIR"
  fi
fi

echo ""
echo "${toolname} regression" | tee $LOGFILE
echo "" | tee -a $LOGFILE

bose=`pget $toolname verbose`
punlearn $toolname
pset $toolname verbose=$bose

script_succeeded=0

######################################################################
# Begin per-test loop

for testid in $testlist
do
  ####################################################################
  # Init per-test error flag
  #
  mismatch=1    

  # delete old outputs
  rm -f $OUTDIR/${testid}.fits

  # Set up file names
  outfile=$OUTDIR/${testid}.fits
  savfile=$SAVDIR/${testid}.fits

  echo "running $testid" | tee -a $LOGFILE
  echo "" | tee -a $LOGFILE
  ####################################################################
  # run the tool
  case ${testid} in
    # !!6
    test1 ) test1_string="create_bkg_map \
               infile=$INDIR/acisf05794_evt3.fits \
               fovfile=$INDIR/acisf05794_fov1.fits \
               expmap=$INDIR/acisf05794_7B1_tmpemap3.fits \
               outfile=${outfile}  tmpdir=${ASCDS_WORK_PATH} \
               clobber=yes expcorr=yes \
               xygrid="3072.5:5120.5:1,3072.5:5120.5:1" \
               scale=64     > /dev/null"
            echo $test1_string | tee -a  $LOGFILE 
            echo "" | tee -a  $LOGFILE
	    eval $test1_string
            ;;
    test2 ) test1_string="create_bkg_map \
               infile=$INDIR/acisf00635_000N001_evt3.fits \
               fovfile=$INDIR/acisf00635_000N001_fov3.fits \
               expmap=$INDIR/acisf00635_000N001_b_exp3.fits \
               outfile=${outfile}  tmpdir=${ASCDS_WORK_PATH} \
               clobber=yes expcorr=yes \
               xygrid=0.5:8192.5:4,0.5:8192.5:4 \
               scale=64     > /dev/null"
            echo $test1_string | tee -a  $LOGFILE 
            echo "" | tee -a  $LOGFILE
	    eval $test1_string
            ;;
    test3 ) test1_string="create_bkg_map \
               infile=$INDIR/acisf00635_000N001_evt3.fits \
               fovfile=$INDIR/acisf00635_000N001_fov3.fits \
               expmap=$INDIR/acisf00635_000N001_b_exp3.fits \
               outfile=${outfile}  tmpdir=${ASCDS_WORK_PATH} \
               clobber=yes expcorr=no  \
               xygrid=0.5:8192.5:4,0.5:8192.5:4 \
               scale=64     > /dev/null"
            echo $test1_string | tee -a  $LOGFILE
            echo "" | tee -a  $LOGFILE
            eval $test1_string
            ;;
    # same as test3 except outimgmap,outstreakmap,outlowfreqmap.
    test4 ) test1_string="create_bkg_map \
               infile=$INDIR/acisf00635_000N001_evt3.fits \
               fovfile=$INDIR/acisf00635_000N001_fov3.fits \
               expmap=$INDIR/acisf00635_000N001_b_exp3.fits \
               outfile=${outfile}  tmpdir=${ASCDS_WORK_PATH} \
               outimgfile=$OUTDIR/test4_image.fits \
               outstreakmap=$OUTDIR/test4_streakmap.fits \
               outlowfreqmap=$OUTDIR/test4_lfmap.fits \
               clobber=yes expcorr=no  \
               xygrid=0.5:8192.5:4,0.5:8192.5:4 \
               scale=64     > /dev/null"
            echo $test1_string | tee -a  $LOGFILE
            echo "" | tee -a  $LOGFILE
            eval $test1_string
            ;;
    test_hrc ) test1_string="create_bkg_map \
               infile=$INDIR/hrcf04594_000N001_evt3.fits \
               fovfile=$INDIR/hrcf04594_000N001_fov3.fits \
               expmap=$INDIR/hrcf04594_000N001_w_exp3.fits \
               outfile=${outfile}  tmpdir=${ASCDS_WORK_PATH} \
               clobber=yes expcorr=no   \
               xygrid=6144.5:26624.5:10,6144.5:26624.5:10 \
               scale=64     > /dev/null"
            echo $test1_string | tee -a  $LOGFILE
            echo "" | tee -a  $LOGFILE
            eval $test1_string
            ;;
    # same as test4 except throw an error to check for non-zero output status
    test5 ) test1_string="create_bkg_map \
               infile=/ImAFile/thatDoesnt/exist.fits \
               fovfile=$INDIR/acisf00635_000N001_fov3.fits \
               expmap=$INDIR/acisf00635_000N001_b_exp3.fits \
               outfile=${outfile}  tmpdir=${ASCDS_WORK_PATH} \
               outimgfile=$OUTDIR/test4_image.fits \
               outstreakmap=$OUTDIR/test4_streakmap.fits \
               outlowfreqmap=$OUTDIR/test4_lfmap.fits \
               clobber=yes expcorr=no  \
               xygrid=0.5:8192.5:4,0.5:8192.5:4 \
               scale=64"
            echo $test1_string | tee -a  $LOGFILE
            echo "" | tee -a  $LOGFILE
            rm -rf $OUTDIR/scrub_${testid}_out.tmp
            eval $test1_string > $OUTDIR/scrub_${testid}_out.tmp 2>&1
            test5status="$?"
            cat $OUTDIR/scrub_${testid}_out.tmp >> ${LOGFILE}
            ;;
  esac

  ####################################################################
  # if the tool failed to run, set mismatch to 0.
  #
  if test $? -ne 0; then
    # we expect an exit status of 1 for test5.
    # skip this status check here.
    if test ${testid} = "test5" ; then
      :
    fi
    echo "" | tee -a $LOGFILE
    echo "$toolname failed to run" | tee -a $LOGFILE
    echo "" | tee -a $LOGFILE
    mismatch=0           
  fi

  ####################################################################
  # check the outputs
  if test ${testid} = "test5"; then
    # exit status should be 1
    if [ $test5status != 1 ]; then
      echo "" | tee -a $LOGFILE
      echo "ERROR: MISMATCH in exit status for ${testid}" | tee -a $LOGFILE
      echo "" | tee -a $LOGFILE
      mismatch=0 
    fi
    # make sure the error message is the same as the expected message.
    sed -e "s;${INDIR};;g" $OUTDIR/scrub_${testid}_out.tmp | sed -e "s;${OUTDIR};;g" | sed -e "s/([^()]*)//g" > $OUTDIR/${testid}_output.txt
    diff $OUTDIR/${testid}_output.txt $SAVDIR/${testid}_output.txt > /dev/null 2>> ${LOGFILE}
    if  test $? -ne 0 ; then
      echo "ERROR: TEXT MISMATCH in $OUTDIR/${testid}_output.txt" >> $LOGFILE
      mismatch=0
    fi
  else
    dmdiff $outfile $savfile tol=$SAVDIR/tolerance >/dev/null 2>>$LOGFILE
    if  test $? -ne 0 ; then
      echo "ERROR: MISMATCH in $outfile" >> $LOGFILE
      mismatch=0
    fi
  fi

  ####################################################################
  # Did we get an error?
  if test $mismatch -eq 0 ; then
    # Yes
    echo "${testid} NOT-OK"
    script_succeeded=1
  else
    # No
    echo "${testid} OK"
  fi

  echo "" | tee -a $LOGFILE

done
# end per-test loop

######################################################################
# report results

# blank line
echo "" | tee -a $LOGFILE

if test $script_succeeded -eq 0; then
    echo "${toolname} : PASS" | tee -a $LOGFILE
else
    echo "${toolname} : FAIL" | tee -a $LOGFILE
fi

echo ""
echo "log file in ${LOGFILE}"
echo ""

exit $script_succeeded
