#! /bin/sh
# 
#  Copyright (C) 1998-2001,2003-2007,2012-2013,2018-2019,2022  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.
#


#

# 2/2005-remove kernel from celldetect/tg-tools;
# 11/19/2012-add 'punlearn tgidselectsrc,tgmatchsrc,celldetect'
#            for parameters defaults (eg.max_separation,snr_ratio_limit)
# 3/27/2013-remove tgdetect.exit when keepexit=no (even it has status 2 in it)
# 9/19/2013-tgdet.script: rm "punlearn tgidselectsrc / tgmatchsrc / celldetect"
#          -            : run tools (eg. celldet) with mode=h
#          -tgdet.par: replace redirected-params with redirected-tool's defaults
#
# tgdetect  Bourne shell script
# Runs several subtools along with celldetect in order to
# get a zero order source list for grating processing.
#

###########################  FUNCTIONS LISTING #########################

# checks to see te status of the line previously run; if
# successful, then, do nothing, if error, then print message
# and exit the tool
last_exit_func()
{
  last_exit=$?
  if test $last_exit -ne 0; then
  echo "ERROR: The last_exit of $PS1: $last_exit"
  echo 0 > $PS2"_tgdetect.exit"
  exit 1
  fi
}

###########################  PARAMETERS SECTION #########################

counter=0
num_auto_params=3
param_list="infile OBI_srclist_file outfile"
entered_param_list=""
root=""
extname=SRCLIST

# soak up parameters in the command line
while test $# -gt 0;
do
  # keep track of the counter, matching parameter order
  counter=`echo $counter | awk '{print $1 + 1}'`

#
# If we already have all the auto params then just pset the rest
#

  if test $counter -gt $num_auto_params
  then
    pset tgdetect $1
  else


  with_param=`echo $1 | grep =`
  if test "$with_param" = "$1"  # when param name is specified
  then
    param=`echo $1 | awk -F= '{print $1}'`
    value=`echo $1 | awk -F= '{print $2}'`
  else                          # when param name is not specified
    param=`echo $param_list | cut -f$counter -d' '`
    value=$1
  fi

  # if the param is not automatic, counter=(counter - 1)
  is_auto_par=`echo $param_list | grep $param`
  if test "$is_auto_par" = ""  #not an auto parameter
  then
    counter=`echo $counter | awk '{print $1 - 1}'`
  else
    # take out the name of the param form entered_param_list
    for test_param in $param_list
    do
      if test "$test_param" = "$param"
      then
        entered_param_list=`echo $entered_param_list $test_param`
      else
	entered_param_list=$entered_param_list
      fi
    done
  fi
  
  case ${param} in

# The multi-value parameters which are read in as quoted strings must 
# have their values re-quoted before feeding them to pset()

   infile | OBI_srclist_file | outfile ) \
     pset tgdetect ${param}="${value}" ;;

   *)  pset tgdetect $1 ;;
  esac

#
# end if num params -gt auto params
#
  fi

  shift

done

# when the counter and num_auto_params are not the same,
# prompt the user for the autoparams, since there was a mismatch
if test $counter -lt $num_auto_params
then
  for in_params in $param_list
  do
    in_or_out=`echo $entered_param_list | grep $in_params`
    if test "$in_or_out" = ""  # has not been entered
    then
      pquery tgdetect $in_params > /dev/null
    fi
  done
fi

# get all the automatic and hidden parameters from thet tool
# and echo them to the screen if the debug is set >=1

ARGUMENTS=`pline tgdetect $ARGUMENTS | tr -d "'"`

for arg in $ARGUMENTS
do
  eval $arg
done

if test $verbose -ge 1; then
  echo ""
  echo "RUNNING tool:  tgdetect"
  echo ""
  echo "####################  PARAMETERS SECTION  ####################"
  echo "  Input parameters are ..."
  for arg in $ARGUMENTS
  do
        echo "     $arg"
  done
fi

# check the infile for @list
infile_name=`echo $infile | grep "@"`
if (test "$infile_name" != "")
then
  # need to open the @list, read the first entry for the filename
  infile_name=`echo $infile_name | sed 's/@//'`
  name=`sed -n 1p $infile_name`
  # strip off the name, to find the path to the @ file
  dir_names=`echo $infile_name | sed 's/\// /g'`
  num_dirs=`echo $dir_names | wc | awk '{print $2}'`
  num_files_in_list=`cat $infile_name | wc | awk '{print $2}'`
  # if no files exist in the @list, set the variable to NONE
  if test $num_files_in_list -lt "1"
  then
    echo "ERROR: @infile is empty."
    exit 1
  else
     counter=0
     infile=""
     for path in $dir_names
     do
       counter=`echo $counter 1 | awk '{print $1 + $2}'`
       if test $counter -lt $num_dirs
       then
         infile=`echo $infile"/"$path`
       else
         break
       fi
     done
     infile=`echo $infile"/"$name`
  fi
fi

# check the refrence source list for @list
OBI_srclist_file_name=`echo $OBI_srclist_file | grep "@"`
if (test "$OBI_srclist_file_name" != "")
then
  # need to open the @list, read the first entry for the filename
  OBI_srclist_file_name=`echo $OBI_srclist_file_name | sed 's/@//'`
  name=`sed -n 1p $OBI_srclist_file_name`
  # strip off the name, to find the path to the @ file
  dir_names=`echo $OBI_srclist_file_name | sed 's/\// /g'`
  num_dirs=`echo $dir_names | wc | awk '{print $2}'`
  num_files_in_list=`cat $OBI_srclist_file_name | wc | awk '{print $2}'`
  # if no files exist in the @list, set the variable to NONE
  if test $num_files_in_list -lt "1"
  then
     OBI_srclist_file="NONE"
  else
     counter=0
     OBI_srclist_file=""
     for path in $dir_names
     do
       counter=`echo $counter 1 | awk '{print $1 + $2}'`
       if test $counter -lt $num_dirs
       then
         OBI_srclist_file=`echo $OBI_srclist_file"/"$path`
       else
         break
       fi
     done
     OBI_srclist_file=`echo $OBI_srclist_file"/"$name`
  fi
fi

# BEGIN section on output filename generation

# function defn
tgd_strip_dname () 
{
    case $1 in
	# match: everything from beginning to last /
	# substitute: with nothing
        */*) echo $1 | sed 's/^.*\///' ;;
        *)   echo $1                   ;;
    esac
}

# function defn
tgd_strip_fname () 
{
    case $1 in
	# match: every non-/ character at end of line
	# substitute: nothing
        */*) echo $1 | sed 's/[^/]*$//' ;;
        *)   echo ""                    ;;
    esac
}


#
# Test outfile for valid name
#
tempname=`tgd_strip_dname $outfile`
if [ "$tempname" = "." -o -z "$tempname" ]
then
    echo ""
    echo "ERROR: outfile is set to \"$outfile\" "
    echo "       but the resulting filename (less path) would be \"$tempname\""
    echo "       This is not allowed; please chose a proper name and try again"
    echo ""
    exit 1
fi
unset tempname

# if the temproot doesn't include a path, use that of $outfile
temppath=`tgd_strip_fname $temproot`
if [ -z "$temppath" ]
then
    root=`tgd_strip_fname $outfile`$temproot
else
    root=$temproot
fi
unset temppath
tempfiles=""

if [ $verbose -ge 1 ]
then
    echo ""
    echo "   Root name (for temp files) is $root"
    echo "   Output file will be $outfile"
    echo ""
fi

# END section on output filename generation


# check to make sure you can clobber outfile
if [ -f "$outfile" ]
then
  if [ "$clobber" = "no" -o "$clobber" = "NO" ]
  then
    echo ""
    echo "ERROR: output file $outfile exists, but clobber is set to '$clobber'"
    echo ""
    exit 1
    # check to make sure that outfile is writtable is clobber = yes
  else  # clobber = YES
    if [ ! -w "$outfile" ]
    then
      echo ""
      echo "ERROR: Output file $outfile does not have write permission"
      echo ""
      exit 1
    else
      \rm $outfile  #clobber the output file
      if test $verbose -ge 2; then
	echo ""
	echo "   Clobbering output file $outfile"
      fi  # end of verbose
    fi  # end of if -w file
  fi   # end of clobber = yes
fi  # end of test -f outfile

if test $verbose -ge 3; then
  echo ""
  echo "   Performed clobber check on $outfile"
fi

# check to make sure that input files are readable
inputfile_list="$infile $OBI_srclist_file $expstk $bkgfile $cellfile"
for file in $inputfile_list
do
  if (test "$file" != "none" -a \
   "$file" != "" -a \
   "$file" != "NONE" )
  then
    if [ ! -r "$file" ]
    then
      echo ""
      echo "ERROR: input file $file is unreadable"
      exit 1
    fi
  fi
  # in case infile was entered as none, check input file
  if (test "$file" = "$infile")  
  then
    if [ ! -r "$file" ]
    then
      echo ""
      echo "ERROR: input file $file is unreadable"
      exit 1
    fi
  fi
done

if test $verbose -ge 3; then
  echo ""
  echo "   Performed file access checks on $inputfile_list"
fi

###########################  END PARAMETERS  #########################

###########################  TG_FILT SECTION #########################

# create the header of the intput file
toolname=`echo $0 | awk -F/ '{print $NF}' `
hdr_file=${root}$toolname"_"$$.par
hdr_infile=$infile"[EVENTS]"
PS1="dmmakepar"
PS2=$root
tmp=`dmmakepar input=$hdr_infile output=$hdr_file`
# was the last command successful or not?
last_exit_func

if test $verbose -ge 2; then
  echo ""
  echo "####################  TG_FILT SECTION  ####################"
  echo "RUNNING dmmakepar..."
  echo "   dmmakepar input=$hdr_infile output=$hdr_file"
  fi


#new parameter list of the needed hdr pars
param_list="ra_nom dec_nom instrume detnam grating"

if test $verbose -ge 2; then
  echo "   Wrote hdr file from $infile ->  $hdr_file"
  echo "   Reading hdr parameters: $param_list"
fi

#get the following parameter values out of the hdr_file
for in_params in $param_list
do
  PS1="pget $in_params"
  PS2=$root
  tmp=`pget $hdr_file $in_params`
  last_exit_func
  eval $in_params=$tmp
done

if test $verbose -ge 2; then
  echo "   Values of hdr parameters ..."
  echo "     ra_nom = $ra_nom"
  echo "     dec_nom = $dec_nom"
  echo "     instrume = $instrume"
#  echo "     acsys4 = $acsys4"
  echo "     detnam = $detnam"
  echo "     grating = $grating"
  echo ""
fi

# change grating name to lower case
case $grating in
    NONE | none )
	grating=none
	;;
    HETG | hetg )
	grating=hetg
	;;
    LETG | letg )
	grating=letg
	;;
    *)
	echo ""
	echo "ERROR: Grating ($grating) not recognized."
	exit 1
esac

if test $verbose -ge 4; then
  echo "   Changed grating name to lowercase -> $grating"
  echo ""
fi
  
inst=`echo $instrume | grep -i ACIS`
if (test "$inst" != "" )  # acis case
then
  inst=ACIS
else
  inst=HRC
fi

if test $verbose -ge 4; then
     echo "   Instrument equates to: $inst"
fi

# get the ascsys keyword, depending on the intrument
# get the readmode parameter for ACIS only
readmode=""
if (test "$inst" = "ACIS" )
then
  PS1="pget acsys4"
  PS2=$root
  tmp=`pget $hdr_file acsys4`
  last_exit_func
  acsys=$tmp
  testing=`grep -i readmode $hdr_file`
  if (test "$testing" != "" )  # readmode is in the header file
  then
     PS1="pget readmode"
     PS2=$root
     tmp=`pget $hdr_file readmode`
     last_exit_func
     readmode=$tmp
  fi
else
  PS1="pget acsys5"
  PS2=$root
  tmp=`pget $hdr_file acsys5`
  last_exit_func
  acsys=$tmp
fi

if test $verbose -ge 2; then
  echo "     acsys = $acsys"
  echo "     readmode = $readmode"
fi
  
# CLEAN UP - delete hdr file
if test $verbose -le 4; then
\rm $hdr_file
fi

# determine if CC mode is active:
# (cc=0 => not active;  cc=1 => active)
cc=0
if (test "$readmode" = "continuous" -o \
 "$readmode" = "CONTINUOUS")
then
   cc=1
fi
  
# find the image center using the pixlib parameters
if (test "$zo_pos_x" = "DEFAULT" -o \
 "$zo_pos_x" = "default" -o \
 "$zo_pos_y" = "DEFAULT" -o \
 "$zo_pos_y" = "default")
then
  
   PS1="center position calculations"
   PS2=$root

    case $detnam in
	ACIS* | acis* )
	    center=4096.5
	    ;;
	HRC-I | hrc-i )
	    center=16384.5
	    ;;
	HRC* | hrc* )
	    center=32768.5
	    ;;
	*)
	    echo ""
	    echo "ERROR: Instrument ($instrume) not recognized."
	    exit 1
    esac

   last_exit_func

   center_int=`echo $center | awk -F. '{print $1}'`
   # check to make sure the center position > 1
   if test $center_int -le 1; then
     echo ""
     echo "ERROR: center position is <= 1; impossible."
     echo ""
     exit 1
   fi

   if (test "$zo_pos_x" = "DEFAULT" -o \
    "$zo_pos_x" = "default")
   then
     center_x=$center   
   elif (test "$zo_pos_x" != "DEFAULT" -a \
    "$zo_pos_x" != "default")
   then
     center_x=$zo_pos_x
   fi
   
   if  (test "$zo_pos_y" = "DEFAULT" -o \
    "$zo_pos_y" = "default")
   then
     center_y=$center
   elif  (test "$zo_pos_y" != "DEFAULT" -a \
    "$zo_pos_y" != "default")
   then
     center_y=$zo_pos_y
   fi
fi

if (test "$zo_pos_x" != "DEFAULT" -a \
 "$zo_pos_x" != "default")
then
  center_x=$zo_pos_x
fi

if (test "$zo_pos_y" != "DEFAULT" -a \
 "$zo_pos_y" != "default")
then
  center_y=$zo_pos_y
fi

# for portability, make integer versions
center_x_int=`echo $center_x | awk -F. '{print $1}'`
center_y_int=`echo $center_y | awk -F. '{print $1}'`

# check to make sure the center position > 1
if test $center_x_int -le 1; then
  echo ""
  echo "ERROR: center x position is <= 1; impossible."
  echo ""
  exit 1
fi
if test $center_y_int -le 1; then
  echo ""
  echo "ERROR: center y position is <= 1; impossible."
  echo ""
  exit 1
fi

# check the max range of the center position, depending on detector
# NOTE - for the time being, ACIS is always ACIS-S, and HRC is always
#        HRC-S, for range calculations;  need to update these, for
#        more specific ranges for ACIS-I and HRC-I, for completeness
if (test $center_x_int -gt 8192 -a \
    "$inst" = "ACIS")
then
  echo "ERROR: zo_pos_x position ($center_x) is out of range (1:8192)"
  exit 1
elif (test $center_y_int -gt 8192 -a \
     "$inst" = "ACIS")
then
  echo "ERROR: zo_pos_y position ($center_y) is out of range (1:8192)"
  exit 1
elif (test $center_x_int -gt 65536 -a \
     "$inst" = "HRC")
then
  echo "ERROR: zo_pos_x position ($center_x) is out of range (1:65536)"
  exit 1
elif (test $center_y_int -gt 65536 -a \
     "$inst" = "HRC")
then
  echo "ERROR: zo_pos_y position ($center_y) is out of range (1:65536)"
  exit 1
fi
  
if test $verbose -ge 2; then
   echo ""
   echo "CENTER of the sky pixel plane is: $center_x, $center_y"
fi


# default sizes for ACIS and HRC
sz_def_acis=400
if test $cc -eq 1; then
  sz_def_acis=400
fi
sz_def_hrc=1800
size_x=0
size_y=0

# if default filter sizes are used, set them here
if (test "$zo_sz_filt_x" = "default" -o \
    "$zo_sz_filt_x" = "DEFAULT")
then
  if  test "$inst" = "ACIS"
  then
       size_x=$sz_def_acis
  else
       size_x=$sz_def_hrc
  fi
  if test $verbose -ge 2; then
       echo ""
       echo "SETTING the default gzo filter x = $size_x"
  fi
else
  size_x=$zo_sz_filt_x
fi

if (test "$zo_sz_filt_y" = "default" -o \
    "$zo_sz_filt_y" = "DEFAULT")
then
  if  test "$inst" = "ACIS"
  then
       size_y=$sz_def_acis
  else
       size_y=$sz_def_hrc
  fi
  if test $verbose -ge 2; then
       echo ""
       echo "SETTING the default gzo filter y = $size_y"
  fi
else
  size_y=$zo_sz_filt_y
fi

# check to make sure the sizes are > 0
if [ $size_x -lt 1 ]
then
  echo "WARNING: Zero order size x ($size_x) < 1"
  if  test "$inst" = "ACIS"
  then
       size_x=$sz_def_acis
  else
       size_x=$sz_def_hrc
  fi
  if test $verbose -ge 2; then
    echo "  RESETTING the gzo filter x = $size_x"
    echo ""
  fi
elif [ $size_y -lt 1 ]
then
  echo "WARNING: Zero order size y ($size_y) < 1"
  if  test "$inst" = "ACIS"
  then
       size_y=$sz_def_acis
  else
       size_y=$sz_def_hrc
  fi
  if test $verbose -ge 2; then
    echo "  RESETTING the gzo filter y = $size_y"
    echo ""
  fi
fi 

# check the max range of the box size's, depending on detector
# NOTE - for the time being, ACIS is always ACIS-S, and HRC is always
#        HRC-S, for range calculations;  need to update these, for
#        more specific ranges for ACIS-I and HRC-I, for completeness
if (test $size_x -gt 8192 -a \
    "$inst" = "ACIS")
then
  echo "ERROR: zo_sz_filt_x position ($size_x) is out of range (1:8192)"
  exit 1
elif (test $size_y -gt 8192 -a \
     "$inst" = "ACIS")
then
  echo "ERROR: zo_sz_filt_y position ($size_y) is out of range (1:8192)"
  exit 1
elif (test $size_x -gt 65536 -a \
     "$inst" = "HRC")
then
  echo "ERROR: zo_sz_filt_x position ($size_x) is out of range (1:65536)"
  exit 1
elif (test $size_y -gt 65536 -a \
     "$inst" = "HRC")
then
  echo "ERROR: zo_sz_filt_y position ($size_y) is out of range (1:65536)"
  exit 1
fi


# calculate the region of extraction
x_lo=`echo $center_x $size_x | awk '{print (int($1 - $2/2))}'`
x_hi=`echo $center_x $size_x | awk '{print (int($1 + $2/2))}'`
y_lo=`echo $center_y $size_y | awk '{print (int($1 - $2/2))}'`
y_hi=`echo $center_y $size_y | awk '{print (int($1 + $2/2))}'`


celldet_file=`echo $infile"[EVENTS][bin x="$x_lo":"$x_hi",y="$y_lo":"$y_hi"][opt type=i4]"`
if test $verbose -ge 2; then
  echo ""
  echo "Creating image filter $celldet_file for celldetect input"
fi
#########################  TG_FILT SECTION END  ######################


#########################   CELLDETECT SECTION  ######################

# add the word 'none' for parameter srcfile if set to nothing
if test "$snrfile" = ""
then
  snrfile=none
fi

if [ "$psffile" != "CALDB" -a "$fixedcell" -gt 0 ]
then
  echo "ERROR: Specified PSF file and Fixed Cell size - Choose one or the other."
  echo 0 > $PS2"_tgdetect.exit"
  exit 1
fi

# psfmap image filter adjustment to create equivalent event images and psfmap images
x_hi=`expr $x_hi - 1`
y_hi=`expr $y_hi - 1`

# run mkpsfmap 
PS1="mkpsfmap"
PS2=$root

if [ "$psffile" = "CALDB" -a "$fixedcell" -eq 0 ]
then
     psffile_out=$root"_psfout".fits
if test $verbose -ge 2; then
     echo ""
     echo "####################  MKPSFMAP SECTION  ####################"
     echo "RUNNING mkpsfmap..."
     echo "mkpsfmap infile="$celldet_file" outfile=$psffile_out psffile=$psffile energy=$eband ecf=$eenergy spectrum=$spectrum units=$units clobber=yes mode=h geompar=$geompar" 
fi    
     mkpsfmap infile="$celldet_file" outfile=$psffile_out psffile=$psffile energy=$eband ecf=$eenergy spectrum=$spectrum units=$units clobber=yes mode=h geompar=$geompar

     psffile=$psffile_out

else if [ "$psffile" != "CALDB" -a "$fixedcell" -eq 0 ]
  then
     psffile=`echo $psffile"[1][bin x="$x_lo":"$x_hi",y="$y_lo":"$y_hi"][opt type=r4]"`
  else 
     psffile=""
  fi

fi


if test $verbose -ge 2; then
  if test $cc -eq 0; then
     echo ""
     echo "####################  CELLDETECT SECTION  ####################"
     echo "RUNNING celldetect..."
     echo "   celldetect infile="$celldet_file" outfile=$root"_cell_out".fits log=$celldetect_log verbose=$verbose clobber=yes psffile="$psffile" fixedcell=$fixedcell expstk=$expstk bkgfile=$bkgfile bkgvalue=$bkgvalue bkgerrvalue=$bkgerrvalue thresh=$thresh ellsigma=$ellsigma expratio=$expratio findpeaks=$findpeaks snrfile=$snrfile convolve=$convolve cellfile=$cellfile centroid=$centroid snr_diminution=$snr_diminution xoffset=$xoffset yoffset=$yoffset mode=h"
  else
     echo ""
     echo "####################  CELLDETECT SECTION  ####################"
     echo "RUNNING celldetect..."
     echo "   celldetect infile="$celldet_file" outfile=$root"_cell_out".fits log=$celldetect_log verbose=$verbose clobber=yes psffile="$psffile" fixedcell=$fixedcell_cc_mode expstk=$expstk bkgfile=$bkgfile bkgvalue=$bkgvalue bkgerrvalue=$bkgerrvalue thresh=$thresh ellsigma=$ellsigma expratio=$expratio findpeaks=$findpeaks snrfile=$snrfile convolve=$convolve cellfile=$cellfile centroid=$centroid snr_diminution=$snr_diminution xoffset=$xoffset yoffset=$yoffset mode=h"
  fi
fi

# run celldetect w/the image filter, to get 1st source table
PS1="celldetect"
PS2=$root
if test $cc -eq 0; then
   celldetect infile="$celldet_file" outfile=$root"_cell_out".fits psffile="$psffile" log=$celldetect_log verbose=$verbose clobber=yes fixedcell=$fixedcell expstk=$expstk bkgfile=$bkgfile bkgvalue=$bkgvalue bkgerrvalue=$bkgerrvalue thresh=$thresh ellsigma=$ellsigma expratio=$expratio findpeaks=$findpeaks snrfile=$snrfile convolve=$convolve cellfile=$cellfile centroid=$centroid snr_diminution=$snr_diminution xoffset=$xoffset yoffset=$yoffset mode=h

   last_exit_func
else
     celldetect infile="$celldet_file" outfile=$root"_cell_out".fits psffile="$psffile" log=$celldetect_log verbose=$verbose clobber=yes fixedcell=$fixedcell_cc_mode expstk=$expstk bkgfile=$bkgfile bkgvalue=$bkgvalue bkgerrvalue=$bkgerrvalue thresh=$thresh ellsigma=$ellsigma expratio=$expratio findpeaks=$findpeaks snrfile=$snrfile convolve=$convolve cellfile=$cellfile centroid=$centroid snr_diminution=$snr_diminution xoffset=$xoffset yoffset=$yoffset mode=h

     last_exit_func
fi

celldetect_out=$root"_cell_out".fits
tempfiles="${celldetect_out} ${tempfiles}"

if test $verbose -ge 2; then
  echo "   OUTPUT file from celldetect is: $celldetect_out"
fi

counter=0
num_rows=0

# check the output file to make sure that there is at least
# one source found by celldetect
num_rows=`dmlist $celldetect_out data verbose=1 | grep -i $extname | grep -i cols | grep -i rows | awk '{print $8}' `
sources_found=num_rows
if (test "$num_rows" = 0 ) ; then
      echo "WARNING:  Celldetect output contains 0 detected sources"
      echo ""
      #exit 2
fi

#########################    END CELL DETECT    ######################

#####################    SRC SELECT SNR SECTION    ###################

if test $verbose -ge 2; then
  echo ""
  echo "####################  SRC SELECT SNR SECTION  ####################"
  echo "RUNNING dmcopy w/ snr threshold filter..."
fi

# master while loop over the snr, in case none are found
while [ "$snr_thresh" -ge "$thresh" ]
do
  counter=`echo $counter | awk '{print $1 + 1}'`


  # cut the threshold in half every time
  if (test "$counter" != 1 ); then
     snr_thresh=`echo $snr_thresh | awk '{print int($1/2)}'`
  fi

  if test $verbose -ge 2; then
    echo "   Using snr_thresh = $snr_thresh"
  fi
  
  # create fileter for snr source selection
  snr_filter=$celldetect_out"["$extname"][snr="$snr_thresh":,double=false]"

  PS1="dmcopy snr"
  PS2=$root
  if test $verbose -ge 2; then
    echo "   dmcopy infile=$snr_filter outfile=$root"_snr_select".fits verbose=$verbose clobber=yes"
  fi
  
  # for now, delete the output, before running; waiting for clobber
  dmcopy infile=$snr_filter outfile=$root"_snr_select".fits verbose=$verbose clobber=yes
  last_exit_func

  snr_select_out=$root"_snr_select".fits
  tempfiles="${snr_select_out} ${tempfiles}"

  if test $verbose -ge 2; then
    echo "   OUTPUT file from dmcopy snr select is: $snr_select_out"
  fi

  # find out if there are any rows in the output file
  num_rows=`dmlist $snr_select_out data verbose=1 | grep -i $extname | grep -i cols | grep -i rows | awk '{print $8}'`
  if test $num_rows -gt 0; then
    break
  else
    if test $verbose -ge 0; then
      next_snr=`echo $snr_thresh | awk '{print int($1/2)}'`
      if test $next_snr -ge $thresh; then
        echo "WARNING:   SNR = $snr_thresh, selection had < 1 source; "
        echo "           trying again w/lower SNR = $snr_thresh/2 ..."
        echo ""
      else
        echo "WARNING:   Next SNR threshold attempt would have been $snr_thresh/2 = $next_snr, "
        echo "           but celldetect thresh=$thresh.  Exiting search!"
        break
      fi
    fi
  fi
  
done  # end of while loop

# check to make sure that there is data in the output file
# if no source, exit the program
if (test "$snr_thresh" -lt "$thresh"  -o \
   "$num_rows" -lt 1)
then
  echo ""
  echo "WARNING: no sources found with snr >= $snr_thresh"
  echo ""
  sources_found=0
  #exit 2
else
  if test $verbose -ge 3; then
  echo "   Successfuly ran dmcopy on snr selection; found $num_rows sources"
fi
fi

if (test "$num_rows" -gt 0)
then
  # run dmsort to sort on SNR, from highest to lowest
  if test $verbose -ge 2; then
    echo ""
    echo "RUNNING dmsort on snr..."
    echo "dmsort infile=$snr_select_out"["$extname"]" outfile=$root"_sort_out".fits keys=-snr copyall=yes clobber=yes verbose=yes"
  fi

  PS1="dmsort snr"
  PS2=$root
  dmsort infile=$snr_select_out"["$extname"]" outfile=$root"_sort_out".fits keys=-snr copyall=yes clobber=yes verbose=$verbose
  last_exit_func

  dmsort_out=$root"_sort_out".fits
else  # when no source, skip dmsort, copy into output file
  cp $snr_select_out $root"_sort_out".fits
  dmsort_out=$root"_sort_out".fits
fi

tempfiles="${dmsort_out} ${tempfiles}"
  
if test $verbose -ge 2; then
  echo "   OUTPUT file from dmsort is: $dmsort_out"
fi

#####################      END SRC SELECT SNR      ###################

#####################     TGIDSELECTSRC SECTION    ###################

if test $verbose -ge 2; then
  echo ""
  echo "####################  TGIDSELECTSRC SNR SECTION  ####################"
  echo "RUNNING tgidselectsrc..."
  echo "tgidselectsrc infile=$dmsort_out"["$extname"]" outfile=$root"_tgid_out".fits snr_ratio_limit=$snr_ratio_limit setsrcid=$setsrcid clobber=yes verbose=$verbose mode=h"
fi

PS1="tgidselectsrc"
PS2=$root
tgidselectsrc infile=$dmsort_out"["$extname"]" outfile=$root"_tgid_out".fits snr_ratio_limit=$snr_ratio_limit setsrcid=$setsrcid clobber=yes verbose=$verbose mode=h
last_exit_func

tgid_out=$root"_tgid_out".fits
tempfiles="${tgid_out} ${tempfiles}"

if test $verbose -ge 2; then
  echo "   OUTPUT file from tgidselectsrc is: $tgid_out"
fi

#####################       END TGIDSELECTSRC      ###################

#####################      TGMATCGSRC SECTION      ###################

if test $verbose -ge 2; then
  echo ""
  echo "####################  TGMATCGSRC SNR SECTION  ####################"
  echo "RUNNING tgmatchsrc..."
  echo "   tgmatchsrc infile=$tgid_out"["$extname"]" refsrcfile=$OBI_srclist_file outfile=$outfile max_separation=$max_separation clobber=yes verbose=$verbose mode=h"
fi

PS1="tgmatchsrc"
PS2=$root
tgmatchsrc infile=$tgid_out"["$extname"]" refsrcfile=$OBI_srclist_file outfile=$outfile max_separation=$max_separation clobber=yes verbose=$verbose mode=h
last_exit_func

if test $verbose -ge 2; then
  echo "   OUTPUT file from tgidselectsrc is: $outfile"
fi

#####################       HDU INFO SECTION      ###################

# need to add in some HDU keys into the header

# create a par file with the header information
par_file=$root"_hdu_hdr.par"
if [ -f "$par_file" ]
then
  \rm $par_file
  if test $verbose -ge 2; then
  echo " Deleting pre-existing hdu parameter file ($par_file)"
  fi
fi

if test $verbose -ge 2; then
  echo ""
  echo "####################  HDU INFO SECTION  ####################"
  echo "CREATING parameter file with HDU information..."
  echo ""
fi

echo "HDUVERS,s,h,"1.0.0",,,""" >> $par_file
echo "HDUVERS1,s,h,"1.0.0",,,""" >> $par_file
echo "HDUCLASS,s,h,"OGIP",,,""" >> $par_file
echo "HDUCLASS1,s,h,"SRCLIST",,,""" >> $par_file
echo "CONTENT,s,h,"TGSRC",,,""" >> $par_file
echo "CREATOR,s,h,"tgdetect",,,""" >> $par_file

if test $verbose -ge 2; then
  echo " The HDU parameter file contains the following keywords:"
  cat $par_file
fi

if test $verbose -ge 2; then
  echo ""
  echo "RUNNING dmreadpar..."
  echo "   dmreadpar $par_file $outfile template="" case=upper clobber=yes verbose=$verbose"
fi

PS1="dmreadpar"
PS2=$root

  dmreadpar $par_file "$outfile"["$extname"]"" template="" case=upper clobber=yes verbose=$verbose
  last_exit_func


if test $verbose -ge 2; then
  echo "   OUTPUT file from dmreadpar is: $outfile"
fi

#clean up par file
\rm $par_file

dmhistory "${outfile}" tgdetect action=put

#####################        END TGMATCGSRC        ###################

# if no source, exit the program with appropriate status
num_rows=`dmlist $outfile data verbose=1 | grep -i $extname | grep -i cols | grep -i rows | awk '{print $8}'`

if (test "$num_rows" -lt 1)
then

  echo ""
  echo "WARNING: no sources in $outfile"
  if test $verbose -ge 1; then
    echo "         Use following as source position: ($center_x,$center_y)"
  fi
  echo ""

  # put the exit status in a file (for pipeline control)
  echo 2 > $root"_tgdetect.exit"


  # 3/27/2013-pipeline is now using tgdetect2.exit instead of tgdetect.exit.
  if [ "$keepexit" = "no" -o "$keepexit" = "NO" ]
  then
      \rm $root"_tgdetect.exit"
  fi


  exit 0     # keep this
fi

if test $verbose -ge 1; then
  echo ""
  echo "TGDETECT output file with zero order source(s) is: $outfile"
fi

#
# Clean up temp files
#
if [ "$keeptemp" = "no" -o "$keeptemp" = "NO" ]
then
    \rm -f $tempfiles
else 
  dmhistory "${celldetect_out}" tgdetect action=put
  dmhistory "${snr_select_out}" tgdetect action=put
  dmhistory "${dmsort_out}" tgdetect action=put
  dmhistory "${tgid_out}" tgdetect action=put
fi

#
# Prepare to exit, remove exit file if requested
#
echo 0 > $root"_tgdetect.exit"

if [ "$keepexit" = "no" -o "$keepexit" = "NO" ]
then
    \rm $root"_tgdetect.exit"
fi

exit 0

