#! /bin/tcsh -f

echo "*************************"
echo "*  start emldetect.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 (-e $det/eboxlist_map.fits) then
fkeypar eboxlist_map.fits+1  NAXIS2
set nrows = `pget fkeypar.par value`
if ( $nrows > 0 ) then
 if ($DET == 1 ) 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`
    set bkg1 = `ls $det/"$dir0"_MOS1_bkg_*.fits | perl -ne 'print unless $.<2 '`
    set bkg2 = `ls $det/"$dir0"_MOS2_bkg_*.fits | perl -ne 'print unless $.<2 '`
    echo "#! /bin/tcsh -f" > $det/emldetect.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 emldetect imagesets='$ima1 $ima2' expimagesets='$expo1 $expo2' bkgimagesets='$bkg1 $bkg2' detmasksets='$mask' pimin='$bmin $bmin' pimax='$bmax $bmax' ecf='$CF' boxlistset=eboxlist_map.fits mlmin="$mlmin_m" mllistset=emllist.fits dmlextmin="$dmlextmin" scut="$scut_m" ecut="$ecut_m" fitextent="$fitextent" extentmodel="$extentmodel" psfmodel="$psfmodel" nmaxfit="$nmaxfit_m" nmulsou="$nmulsou" hrm1def='$HR' hrm2def='$HR' withthreshold=yes threshold="$threshold" withdetmask=true threshcolumn=LIKE withsourcemap=false withtwostage=true -w "$warn" -V "$verb"" >> $det/emldetect.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 emldetect imagesets='$ima1 $ima2' expimagesets='$expo1 $expo2' bkgimagesets='$bkg1 $bkg2' detmasksets='$mask' pimin='$bmin' pimax='$bmax' ecf='$CF' boxlistset=eboxlist_map.fits mlmin="$mlmin_m" mllistset=emllist.fits dmlextmin="$dmlextmin" scut="$scut_m" ecut="$ecut_m" fitextent="$fitextent" extentmodel="$extentmodel" psfmodel="$psfmodel" nmaxfit="$nmaxfit_m" nmulsou="$nmulsou" hrm1def='$HR' hrm2def='$HR' withthreshold=yes threshold="$threshold" withdetmask=true threshcolumn=LIKE withsourcemap=false withtwostage=true -w "$warn" -V "$verb"" >> $det/emldetect.csh
     endif 
    source $det/emldetect.csh
    rm $det/emldetect.csh
    echo "OK: emllist.fits done in energy band (eV) ["$bmin":"$bmax"]"
 endif

 if ($DET == 01 ) 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 CF=`awk '{printf("%.4f \n", $3)}' $det/cf1.txt | perl -ne 'print unless $.<2 '`
    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`
    set bkg1 = `ls $det/"$dir0"_MOS1_bkg_*.fits | perl -ne 'print unless $.<2 '`
    echo "#! /bin/tcsh -f" > $det/emldetect.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 emldetect imagesets='$ima1' expimagesets='$expo1' bkgimagesets='$bkg1' detmasksets='$mask' pimin='$bmin' pimax='$bmax' ecf='$CF' boxlistset=eboxlist_map.fits mlmin="$mlmin_m" mllistset=emllist.fits dmlextmin="$dmlextmin" scut="$scut_m" ecut="$ecut_m" fitextent="$fitextent" extentmodel="$extentmodel" psfmodel="$psfmodel" nmaxfit="$nmaxfit_m" nmulsou="$nmulsou" hrm1def='$HR' withthreshold=yes threshold="$threshold" withdetmask=true threshcolumn=LIKE withsourcemap=false withtwostage=true -w "$warn" -V "$verb"" >> $det/emldetect.csh
     endif 
    source $det/emldetect.csh
    rm $det/emldetect.csh
    echo "OK: emllist.fits done in energy band (eV) ["$bmin":"$bmax"]"
 endif

 if ($DET == 02 ) 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`
    set bkg2 = `ls $det/"$dir0"_MOS2_bkg_*.fits | perl -ne 'print unless $.<2 '`
    echo "#! /bin/tcsh -f" > $det/emldetect.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 emldetect imagesets='$ima2' expimagesets='$expo2' bkgimagesets='$bkg2' detmasksets='$mask' pimin='$bmin' pimax='$bmax' ecf='$CF' boxlistset=eboxlist_map.fits mlmin="$mlmin_m" mllistset=emllist.fits dmlextmin="$dmlextmin" scut="$scut_m" ecut="$ecut_m" extentmodel="$extentmodel" fitextent="$fitextent" extentmodel="$extentmodel" psfmodel="$psfmodel" nmaxfit="$nmaxfit_m" nmulsou="$nmulsou" hrm2def='$HR' withthreshold=yes threshold="$threshold" withdetmask=true threshcolumn=LIKE withsourcemap=false withtwostage=true -w "$warn" -V "$verb"" >> $det/emldetect.csh
     endif 
    source $det/emldetect.csh
    rm $det/emldetect.csh
    echo "OK: emllist.fits done in energy band (eV) ["$bmin":"$bmax"]"
 endif

 if ($DET == 03) 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`
    set bkg = `ls $det/"$dir0"_PN_bkg_*.fits | perl -ne 'print unless $.<2 '`
    echo "/usr/bin/time -p emldetect imagesets='$ima' expimagesets='$expo' bkgimagesets='$bkg' detmasksets='$mask' pimin='$bmin' pimax='$bmax' ecf='$CF' boxlistset=eboxlist_map.fits mlmin="$mlmin_m" mllistset=emllist.fits dmlextmin="$dmlextmin" scut="$scut_m" ecut="$ecut_m" fitextent="$fitextent" extentmodel="$extentmodel" psfmodel="$psfmodel" nmaxfit="$nmaxfit_m" nmulsou="$nmulsou" hrpndef='$HR' withthreshold=yes threshold="$threshold" withdetmask=true threshcolumn=LIKE withsourcemap=false withtwostage=true -w "$warn" -V "$verb"" >> $det/emldetect.csh
    source $det/emldetect.csh
    rm $det/emldetect.csh
    echo "OK: emllist.fits done in energy band (eV) ["$bmin":"$bmax"]"
 endif

 if ($DET == 2 ) 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` 
    set bkg1 = `ls $det/"$dir0"_MOS1_bkg_*.fits | perl -ne 'print unless $.<2 '` 
    set bkg2 = `ls $det/"$dir0"_MOS2_bkg_*.fits | perl -ne 'print unless $.<2 '` 
    set bkg3 = `ls $det/"$dir0"_PN_bkg_*.fits | perl -ne 'print unless $.<2 '`

     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 emldetect imagesets='$ima3 $ima1 $ima2' expimagesets='$expo3 $expo1 $expo2' bkgimagesets='$bkg3 $bkg1 $bkg2' detmasksets='$mask1 $mask2' pimin='$bmin $bmin $bmin' pimax='$bmax $bmax $bmax' ecf='$CF' boxlistset=eboxlist_map.fits mlmin="$mlmin_m" mllistset=emllist.fits dmlextmin="$dmlextmin" scut="$scut_m" ecut="$ecut_m" fitextent="$fitextent" extentmodel="$extentmodel" psfmodel="$psfmodel" nmaxfit="$nmaxfit_m" nmulsou="$nmulsou" hrm1def='$HR' hrm2def='$HR' hrpndef='$HR' withthreshold=yes threshold="$threshold" withdetmask=true threshcolumn=LIKE withsourcemap=false withtwostage=true -w "$warn" -V "$verb"" >> $det/emldetect.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 emldetect imagesets='$ima3 $ima1 $ima2' expimagesets='$expo3 $expo1 $expo2' bkgimagesets='$bkg3 $bkg1 $bkg2' detmasksets='$mask1 $mask2' pimin='$bmin $bmin' pimax='$bmax $bmax' ecf='$CF' boxlistset=eboxlist_map.fits mlmin="$mlmin_m" mllistset=emllist.fits dmlextmin="$dmlextmin" scut="$scut_m" ecut="$ecut_m" fitextent="$fitextent" extentmodel="$extentmodel" psfmodel="$psfmodel" nmaxfit="$nmaxfit_m" nmulsou="$nmulsou" hrm1def='$HR' hrm2def='$HR' hrpndef='$HR' withthreshold=yes threshold="$threshold" withdetmask=true threshcolumn=LIKE withsourcemap=false withtwostage=true -w "$warn" -V "$verb"" >> $det/emldetect.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 emldetect imagesets='$ima3 $ima1 $ima2' expimagesets='$expo3 $expo1 $expo2' bkgimagesets='$bkg3 $bkg1 $bkg2' detmasksets='$mask1 $mask2' pimin='$bmin' pimax='$bmax' ecf='$CF' boxlistset=eboxlist_map.fits mlmin="$mlmin_m" mllistset=emllist.fits dmlextmin="$dmlextmin" scut="$scut_m" ecut="$ecut_m" fitextent="$fitextent" extentmodel="$extentmodel" psfmodel="$psfmodel" nmaxfit="$nmaxfit_m" nmulsou="$nmulsou" hrm1def='$HR' hrm2def='$HR' hrpndef='$HR' withthreshold=yes threshold="$threshold" withdetmask=true threshcolumn=LIKE withsourcemap=false withtwostage=true -w "$warn" -V "$verb"" >> $det/emldetect.csh
     endif 
    source $det/emldetect.csh
    rm $det/emldetect.csh
    echo "OK: emllist.fits done in energy band (eV) ["$bmin":"$bmax"]"
 endif
 
#CATALOGS
 echo "start to create the catalogs"
 source $progr_dir/catalog.csh
else
   echo "eboxlist_map.fits is void: src = $nrows"
   echo "WARNING $dir0 emllist.fits is empty"
   echo "WARNING $dir0 emllist.fits is empty FULLDET" >> $log_dir/messages.log
endif
endif
cd $start_dir/

date
echo "stop emldetect.csh"
