#! /bin/tcsh -f

echo "SOURCE DETECTION IN LOCAL MODE"

echo "*****************************"
echo "*  start eboxdetect_LM.csh  *"
echo "*****************************"
date

cd $result_dir/
set det=$result_dir/$dir0/detection
cd $det/

setenv SAS_ODF $data_dir/$dir0
setenv SAS_CCF $data_dir/$dir0/ccf.cif

set bmin=`awk -F \; '{print $1}' $result_dir/$dir0/energy_band.txt | perl -ne 'print unless $.<2 '`
set bmax=`awk -F \; '{print $2}' $result_dir/$dir0/energy_band.txt | perl -ne 'print unless $.<2 '`

if ($DET == 1 ) then
 if (-e $ExpDetectPrep/ExpDetect_MOS1_LE_noise_unfiltered_image_"$b_tot".fits || -e $ExpDetectPrep/ExpDetect_MOS2_LE_noise_unfiltered_image_"$b_tot".fits) then
  set ima_btot = `ls $ExpDetectPrep/ExpDetect_MOS1_LE_noise_unfiltered_image_"$b_tot".fits $ExpDetectPrep/ExpDetect_MOS2_LE_noise_unfiltered_image_"$b_tot".fits | wc -l | awk '{print $1}'`
  set ima_mos1 = `ls $ExpDetectPrep/ExpDetect_MOS1_LE_noise_unfiltered_image_"$b_tot".fits`
  set ima_mos2 = `ls $ExpDetectPrep/ExpDetect_MOS2_LE_noise_unfiltered_image_"$b_tot".fits`
  set ima1 = `ls $ExpDetectPrep/ExpDetect_MOS1_LE_noise_unfiltered_image_*.fits | perl -ne 'print unless $.<2 '`
  set ima2 = `ls $ExpDetectPrep/ExpDetect_MOS2_LE_noise_unfiltered_image_*.fits | perl -ne 'print unless $.<2 '`
  set expo1 = `ls $ExpDetectPrep/ExpDetect_MOS1_LE_noise_filtered_expo_*.fits | perl -ne 'print unless $.<2 '`
  set expo2 = `ls $ExpDetectPrep/ExpDetect_MOS2_LE_noise_filtered_expo_*.fits | perl -ne 'print unless $.<2 '`
  set mask = `ls $det/"$dir0"_MOS*_detmask_"$b_soft".fits`
  echo "#! /bin/tcsh -f" > $det/eboxdetect_LM.csh

   if ($ima_btot == 2) then
    set CF=`awk '{printf("%.4f \n", $3)}' $det/cf1.txt | perl -ne 'print unless $.<2 ' && awk '{printf("%.4f \n", $3)}' $det/cf2.txt | perl -ne 'print unless $.<2 '`
    echo "/usr/bin/time -p eboxdetect usemap=no likemin=$likemin_l boxsize=$boxsize_l nruns=1 withdetmask=yes detmasksets='$mask' imagesets='$ima1 $ima2' expimagesets='$expo1 $expo2' ecf='$CF' pimin='$bmin $bmin' pimax='$bmax $bmax' boxlistset=eboxlist_local.fits usematchedfilter=yes -w "$warn" -V "$verb"" >> $det/eboxdetect_LM.csh
   endif 
   if ($ima_btot == 1) then
    if ($ima_mos1 == "$ExpDetectPrep/ExpDetect_MOS1_LE_noise_unfiltered_image_"$b_tot".fits") then
     set CF=`awk '{printf("%.4f \n", $3)}' $det/cf1.txt | perl -ne 'print unless $.<2 '`
    endif
    if ($ima_mos2 == "$ExpDetectPrep/ExpDetect_MOS2_LE_noise_unfiltered_image_"$b_tot".fits") then
     set CF=`awk '{printf("%.4f \n", $3)}' $det/cf2.txt | perl -ne 'print unless $.<2 '`
    endif
    echo "/usr/bin/time -p eboxdetect usemap=no likemin=$likemin_l boxsize=$boxsize_l nruns=1 withdetmask=yes detmasksets='$mask' imagesets='$ima1 $ima2' expimagesets='$expo1 $expo2' ecf='$CF' pimin='$bmin' pimax='$bmax' boxlistset=eboxlist_local.fits usematchedfilter=yes -w "$warn" -V "$verb"" >> $det/eboxdetect_LM.csh
   endif 
  source $det/eboxdetect_LM.csh
  rm $det/eboxdetect_LM.csh
  echo "OK: eboxlist_local.fits done in energy band (eV) ["$bmin":"$bmax"]"

 fkeypar eboxlist_local.fits+1  NAXIS2
 set mrows = `pget fkeypar.par value`
 echo "Number of rows in eboxlist = $mrows in eboxlist_local.fits"
 if ( $mrows == 0 ) then
   echo "WARNING $dir0 eboxlist_local.fits is empty"
   echo "WARNING $dir0 eboxlist_local.fits is empty FULLDET" >> $log_dir/messages.log
 endif
 endif
endif

if ($DET == 01 ) then
 if (-e $ExpDetectPrep/ExpDetect_MOS1_LE_noise_unfiltered_image_"$b_tot".fits) then
  set ima_btot = `ls $ExpDetectPrep/ExpDetect_MOS1_LE_noise_unfiltered_image_"$b_tot".fits | wc -l | awk '{print $1}'`
  set ima_mos1 = `ls $ExpDetectPrep/ExpDetect_MOS1_LE_noise_unfiltered_image_"$b_tot".fits`
  set ima1 = `ls $ExpDetectPrep/ExpDetect_MOS1_LE_noise_unfiltered_image_*.fits | perl -ne 'print unless $.<2 '`
  set expo1 = `ls $ExpDetectPrep/ExpDetect_MOS1_LE_noise_filtered_expo_*.fits | perl -ne 'print unless $.<2 '`
  set mask = `ls $det/"$dir0"_MOS1_detmask_"$b_soft".fits`
  echo "#! /bin/tcsh -f" > $det/eboxdetect_LM.csh

   if ($ima_btot == 1) then
    if ($ima_mos1 == "$ExpDetectPrep/ExpDetect_MOS1_LE_noise_unfiltered_image_"$b_tot".fits") then
     set CF=`awk '{printf("%.4f \n", $3)}' $det/cf1.txt | perl -ne 'print unless $.<2 '`
    endif
    echo "/usr/bin/time -p eboxdetect usemap=no likemin=$likemin_l boxsize=$boxsize_l nruns=1 withdetmask=yes detmasksets='$mask' imagesets='$ima1' expimagesets='$expo1' ecf='$CF' pimin='$bmin' pimax='$bmax' boxlistset=eboxlist_local.fits usematchedfilter=yes -w "$warn" -V "$verb"" >> $det/eboxdetect_LM.csh
   endif 
  source $det/eboxdetect_LM.csh
  rm $det/eboxdetect_LM.csh
  echo "OK: eboxlist_local.fits done in energy band (eV) ["$bmin":"$bmax"]"

 fkeypar eboxlist_local.fits+1  NAXIS2
 set mrows = `pget fkeypar.par value`
 echo "Number of rows in eboxlist = $mrows in eboxlist_local.fits"
 if ( $mrows == 0 ) then
   echo "WARNING $dir0 eboxlist_local.fits is empty"
   echo "WARNING $dir0 eboxlist_local.fits is empty FULLDET" >> $log_dir/messages.log
 endif
 endif
endif

if ($DET == 02 ) then
 if (-e $ExpDetectPrep/ExpDetect_MOS2_LE_noise_unfiltered_image_"$b_tot".fits) then
  set ima_btot = `ls $ExpDetectPrep/ExpDetect_MOS2_LE_noise_unfiltered_image_"$b_tot".fits | wc -l | awk '{print $1}'`
  set ima_mos2 = `ls $ExpDetectPrep/ExpDetect_MOS2_LE_noise_unfiltered_image_"$b_tot".fits`
  set ima2 = `ls $ExpDetectPrep/ExpDetect_MOS2_LE_noise_unfiltered_image_*.fits | perl -ne 'print unless $.<2 '`
  set expo2 = `ls $ExpDetectPrep/ExpDetect_MOS2_LE_noise_filtered_expo_*.fits | perl -ne 'print unless $.<2 '`
  set mask = `ls $det/"$dir0"_MOS2_detmask_"$b_soft".fits`
  echo "#! /bin/tcsh -f" > $det/eboxdetect_LM.csh

   if ($ima_btot == 1) then
    if ($ima_mos2 == "$ExpDetectPrep/ExpDetect_MOS2_LE_noise_unfiltered_image_"$b_tot".fits") then
     set CF=`awk '{printf("%.4f \n", $3)}' $det/cf2.txt | perl -ne 'print unless $.<2 '`
    endif
    echo "/usr/bin/time -p eboxdetect usemap=no likemin=$likemin_l boxsize=$boxsize_l nruns=1 withdetmask=yes detmasksets='$mask' imagesets='$ima2' expimagesets='$expo2' ecf='$CF' pimin='$bmin' pimax='$bmax' boxlistset=eboxlist_local.fits usematchedfilter=yes -w "$warn" -V "$verb"" >> $det/eboxdetect_LM.csh
   endif 
  source $det/eboxdetect_LM.csh
  rm $det/eboxdetect_LM.csh
  echo "OK: eboxlist_local.fits done in energy band (eV) ["$bmin":"$bmax"]"

 fkeypar eboxlist_local.fits+1  NAXIS2
 set mrows = `pget fkeypar.par value`
 echo "Number of rows in eboxlist = $mrows in eboxlist_local.fits"
 if ( $mrows == 0 ) then
   echo "WARNING $dir0 eboxlist_local.fits is empty"
   echo "WARNING $dir0 eboxlist_local.fits is empty FULLDET" >> $log_dir/messages.log
 endif
 endif
endif

if ($DET == 03) then
 if (-e $ExpDetectPrep/ExpDetectPrep_PN_merged_image_"$b_tot".fits) then
  set CF=`awk '{printf("%.4f \n", $3)}' $det/cf3.txt | perl -ne 'print unless $.<2 '`
  set ima = `ls $ExpDetectPrep/ExpDetectPrep_PN_merged_image_*.fits | perl -ne 'print unless $.<2 '`
  set expo = `ls $ExpDetectPrep/ExpDetectPrep_PN_merged_expo_*.fits | perl -ne 'print unless $.<2 '`
  set mask = `ls $det/"$dir0"_PN_detmask_"$b_soft".fits`
  echo "#! /bin/tcsh -f" > $det/eboxdetect_LM.csh

  echo "/usr/bin/time -p eboxdetect usemap=no likemin=$likemin_l boxsize=$boxsize_l nruns=1 withdetmask=yes detmasksets='$mask' imagesets='$ima' expimagesets='$expo' ecf='$CF' pimin='$bmin' pimax='$bmax' boxlistset=eboxlist_local.fits usematchedfilter=yes -w "$warn" -V "$verb"" >> $det/eboxdetect_LM.csh
  source $det/eboxdetect_LM.csh
  rm $det/eboxdetect_LM.csh
  echo "OK: eboxlist_local.fits done in energy band (eV) ["$bmin":"$bmax"]"

 fkeypar eboxlist_local.fits+1  NAXIS2
 set mrows = `pget fkeypar.par value`
 echo "Number of rows in eboxlist = $mrows in eboxlist_local.fits"
 if ( $mrows == 0 ) then
   echo "WARNING $dir0 eboxlist_local.fits is empty"
   echo "WARNING $dir0 eboxlist_local.fits is empty FULLDET" >> $log_dir/messages.log
 endif
 endif
endif

if ($DET == 2 ) then
 if (-e $ExpDetectPrep/ExpDetect_MOS1_LE_noise_unfiltered_image_"$b_tot".fits || -e $ExpDetectPrep/ExpDetect_MOS2_LE_noise_unfiltered_image_"$b_tot".fits || -e $ExpDetectPrep/ExpDetectPrep_PN_merged_image_"$b_tot".fits) then
  set ima_btot = `ls $ExpDetectPrep/ExpDetect_MOS1_LE_noise_unfiltered_image_"$b_tot".fits $ExpDetectPrep/ExpDetect_MOS2_LE_noise_unfiltered_image_"$b_tot".fits $ExpDetectPrep/ExpDetectPrep_PN_merged_image_"$b_tot".fits | wc -l | awk '{print $1}'`
  set ima_mos1 = `ls $ExpDetectPrep/ExpDetect_MOS1_LE_noise_unfiltered_image_"$b_tot".fits`
  set ima_mos2 = `ls $ExpDetectPrep/ExpDetect_MOS2_LE_noise_unfiltered_image_"$b_tot".fits`
  set ima_pn = `ls $ExpDetectPrep/ExpDetectPrep_PN_merged_image_"$b_tot".fits`
  set ima1 = `ls $ExpDetectPrep/ExpDetect_MOS1_LE_noise_unfiltered_image_*.fits | perl -ne 'print unless $.<2 '`
  set ima2 = `ls $ExpDetectPrep/ExpDetect_MOS2_LE_noise_unfiltered_image_*.fits | perl -ne 'print unless $.<2 '`
  set ima3 = `ls $ExpDetectPrep/ExpDetectPrep_PN_merged_image_*.fits | perl -ne 'print unless $.<2 '`
  set expo1 = `ls $ExpDetectPrep/ExpDetect_MOS1_LE_noise_filtered_expo_*.fits | perl -ne 'print unless $.<2 '`
  set expo2 = `ls $ExpDetectPrep/ExpDetect_MOS2_LE_noise_filtered_expo_*.fits | perl -ne 'print unless $.<2 '`
  set expo3 = `ls $ExpDetectPrep/ExpDetectPrep_PN_merged_expo_*.fits | perl -ne 'print unless $.<2 '`
  set mask1 = `ls $det/"$dir0"_PN_detmask_"$b_soft".fits`
  set mask2 = `ls $det/"$dir0"_MOS*_detmask_"$b_soft".fits` 
  echo "#! /bin/tcsh -f" > $det/eboxdetect_LM.csh

   if ($ima_btot == 3) then
    set CF=`awk '{printf("%.4f \n", $3)}' $det/cf3.txt | perl -ne 'print unless $.<2 ' && awk '{printf("%.4f \n", $3)}' $det/cf1.txt | perl -ne 'print unless $.<2 ' && awk '{printf("%.4f \n", $3)}' $det/cf2.txt | perl -ne 'print unless $.<2 '`
    echo "/usr/bin/time -p eboxdetect usemap=no likemin=$likemin_l boxsize=$boxsize_l nruns=1 withdetmask=yes detmasksets='$mask1 $mask2' imagesets='$ima3 $ima1 $ima2' expimagesets='$expo3 $expo1 $expo2' ecf='$CF' pimin='$bmin $bmin $bmin' pimax='$bmax $bmax $bmax' boxlistset=eboxlist_local.fits usematchedfilter=yes -w "$warn" -V "$verb"" >> $det/eboxdetect_LM.csh
   endif 
   if ($ima_btot == 2) then
    if ($ima_mos1 == "$ExpDetectPrep/ExpDetect_MOS1_LE_noise_unfiltered_image_"$b_tot".fits" && $ima_mos2 == "$ExpDetectPrep/ExpDetect_MOS2_LE_noise_unfiltered_image_"$b_tot".fits") then
     set CF=`awk '{printf("%.4f \n", $3)}' $det/cf1.txt | perl -ne 'print unless $.<2 ' && awk '{printf("%.4f \n", $3)}' $det/cf2.txt | perl -ne 'print unless $.<2 '`
    endif
    if ($ima_mos1 == "$ExpDetectPrep/ExpDetect_MOS1_LE_noise_unfiltered_image_"$b_tot".fits" && $ima_pn == "$ExpDetectPrep/ExpDetectPrep_PN_merged_image_"$b_tot".fits") then
     set CF=`awk '{printf("%.4f \n", $3)}' $det/cf3.txt | perl -ne 'print unless $.<2 ' && awk '{printf("%.4f \n", $3)}' $det/cf1.txt | perl -ne 'print unless $.<2 '`
    endif
    if ($ima_mos2 == "$ExpDetectPrep/ExpDetect_MOS2_LE_noise_unfiltered_image_"$b_tot".fits" && $ima_pn == "$ExpDetectPrep/ExpDetectPrep_PN_merged_image_"$b_tot".fits") then
     set CF=`awk '{printf("%.4f \n", $3)}' $det/cf3.txt | perl -ne 'print unless $.<2 ' && awk '{printf("%.4f \n", $3)}' $det/cf2.txt | perl -ne 'print unless $.<2 '`
    endif
    echo "/usr/bin/time -p eboxdetect usemap=no likemin=$likemin_l boxsize=$boxsize_l nruns=1 withdetmask=yes detmasksets='$mask1 $mask2' imagesets='$ima3 $ima1 $ima2' expimagesets='$expo3 $expo1 $expo2' ecf='$CF' pimin='$bmin $bmin' pimax='$bmax $bmax' boxlistset=eboxlist_local.fits usematchedfilter=yes -w "$warn" -V "$verb"" >> $det/eboxdetect_LM.csh
   endif 
   if ($ima_btot == 1) then
    if ($ima_mos1 == "$ExpDetectPrep/ExpDetect_MOS1_LE_noise_unfiltered_image_"$b_tot".fits") then
     set CF=`awk '{printf("%.4f \n", $3)}' $det/cf1.txt | perl -ne 'print unless $.<2 '`
    endif
    if ($ima_mos2 == "$ExpDetectPrep/ExpDetect_MOS2_LE_noise_unfiltered_image_"$b_tot".fits") then
     set CF=`awk '{printf("%.4f \n", $3)}' $det/cf2.txt | perl -ne 'print unless $.<2 '`
    endif
    if ($ima_pn == "$ExpDetectPrep/ExpDetectPrep_PN_merged_image_"$b_tot".fits") then
     set CF=`awk '{printf("%.4f \n", $3)}' $det/cf3.txt | perl -ne 'print unless $.<2 '`
    endif
    echo "/usr/bin/time -p eboxdetect usemap=no likemin=$likemin_l boxsize=$boxsize_l nruns=1 withdetmask=yes detmasksets='$mask1 $mask2' imagesets='$ima3 $ima1 $ima2' expimagesets='$expo3 $expo1 $expo2' ecf='$CF' pimin='$bmin' pimax='$bmax' boxlistset=eboxlist_local.fits usematchedfilter=yes -w 1 -V "$verb"" >> $det/eboxdetect_LM.csh
   endif 
  source $det/eboxdetect_LM.csh
  rm $det/eboxdetect_LM.csh
  echo "OK: eboxlist_local.fits done in energy band (eV) ["$bmin":"$bmax"]"

 fkeypar eboxlist_local.fits+1  NAXIS2
 set mrows = `pget fkeypar.par value`
 echo "Number of rows in eboxlist = $mrows in eboxlist_local.fits"
 if ( $mrows == 0 ) then
   echo "WARNING $dir0 eboxlist_local.fits is empty"
   echo "WARNING $dir0 eboxlist_local.fits is empty FULLDET" >> $log_dir/messages.log
 endif
 endif
endif
cd $start_dir/

date
echo "stop eboxdetect_LM.csh"
