#! /bin/tcsh -f

echo "SOURCE DETECTION IN LOCAL MODE"

echo "*********************************"
echo "*  start eboxdetect_LM_bin.csh  *"
echo "*********************************"
date

cd $result_dir
set ExpDetectPrep = $an_dir/ExpDetectPrep
set det = $result_dir/$dir0/detection
cd $an_dir
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 '`
set bmin_tot=`awk -F \; -v nr=$b_tot '{ if (NR==nr) print $1}' $result_dir/$dir0/energy_band.txt`
set bmax_tot=`awk -F \; -v nr=$b_tot '{ if (NR==nr) print $2}' $result_dir/$dir0/energy_band.txt`

if ( $DET == 1 ) then
 if (-e $ExpDetectPrep/ExpDetect_MOS1_LE_noise_unfiltered_image_"$b_tot"_"$start"-"$stop".fits || -e $ExpDetectPrep/ExpDetect_MOS2_LE_noise_unfiltered_image_"$b_tot"_"$start"-"$stop".fits) then
  echo "N. energy band: ${N_EN_BAND}" 
  set N_evtM1=`ls $ExpDetectPrep/ExpDetectPrep_MOS1_merged_event_list_*.fits | perl -ne 'print unless $.<2 ' | wc -l | awk '{print $1}'`
  set N_evtM2=`ls $ExpDetectPrep/ExpDetectPrep_MOS2_merged_event_list_*.fits | perl -ne 'print unless $.<2 ' | wc -l | awk '{print $1}'`
  echo "N. of MOS1 event files: $N_evtM1"    
  echo "N. of MOS2 event files: $N_evtM2"
  
  set ima_btot = `ls $ExpDetectPrep/ExpDetect_MOS1_LE_noise_unfiltered_image_"$b_tot"_"$start"-"$stop".fits $ExpDetectPrep/ExpDetect_MOS2_LE_noise_unfiltered_image_"$b_tot"_"$start"-"$stop".fits | wc -l | awk '{print $1}'`
  set ima_mos1 = `ls $ExpDetectPrep/ExpDetect_MOS1_LE_noise_unfiltered_image_"$b_tot"_"$start"-"$stop".fits`
  set ima_mos2 = `ls $ExpDetectPrep/ExpDetect_MOS2_LE_noise_unfiltered_image_"$b_tot"_"$start"-"$stop".fits`
  set expo_mos1 = `ls $ExpDetectPrep/ExpDetect_MOS1_LE_noise_filtered_expo_"$b_tot"_"$start"-"$stop".fits`
  set expo_mos2 = `ls $ExpDetectPrep/ExpDetect_MOS2_LE_noise_filtered_expo_"$b_tot"_"$start"-"$stop".fits`
  set ima1 = `ls $ExpDetectPrep/ExpDetect_MOS1_LE_noise_unfiltered_image_*_"$start"-"$stop".fits | perl -ne 'print unless $.<2 '`
  set ima2 = `ls $ExpDetectPrep/ExpDetect_MOS2_LE_noise_unfiltered_image_*_"$start"-"$stop".fits | perl -ne 'print unless $.<2 '`
  set expo1 = `ls $ExpDetectPrep/ExpDetect_MOS1_LE_noise_filtered_expo_*_"$start"-"$stop".fits | perl -ne 'print unless $.<2 '`
  set expo2 = `ls $ExpDetectPrep/ExpDetect_MOS2_LE_noise_filtered_expo_*_"$start"-"$stop".fits | perl -ne 'print unless $.<2 '`
  set mask_soft = `ls $an_dir/"$dir0"_MOS1_detmask_"$b_soft"_"$start"-"$stop".fits $an_dir/"$dir0"_MOS2_detmask_"$b_soft"_"$start"-"$stop".fits | wc -l | awk '{print $1}'`
  if ($mask_soft == $ima_btot) then
   set mask = `ls $an_dir/"$dir0"_MOS*_detmask_"$b_soft"_"$start"-"$stop".fits`
  else
   set mask = `ls $an_dir/"$dir0"_MOS*_detmask_"$b_tot"_"$start"-"$stop".fits`
  endif

  echo "#! /bin/tcsh -f" > $an_dir/eboxdetect_LM.csh

   if ($ima_btot == 2) then
   if ($ima_mos1 == "$ExpDetectPrep/ExpDetect_MOS1_LE_noise_unfiltered_image_"$b_tot"_"$start"-"$stop".fits" && $ima_mos2 == "$ExpDetectPrep/ExpDetect_MOS2_LE_noise_unfiltered_image_"$b_tot"_"$start"-"$stop".fits") then
  ##CF....
     set filterM1=`ftlist ${ima_mos1}+0 K | grep "FILTER" | sed -e "s/'/ /g"  | cut -d' ' -f5 | awk '{print $1}' | cut -c "1-4"`
     set filterM2=`ftlist ${ima_mos2}+0 K | grep "FILTER" | sed -e "s/'/ /g"  | cut -d' ' -f5 | awk '{print $1}' | cut -c "1-4"`
     if (${N_evtM1} == ${N_EN_BAND} && ${N_evtM2} == ${N_EN_BAND}) then
      echo "Performing eboxdetect LM with MOS1 MOS2 for all energy bands"
      if (${filterM1} == "Thin") then
        set CF_M1=`awk '{printf("%.4f \n", $1)}' $det/cf1.txt | perl -ne 'print unless $.<2 '`
      endif
      if (${filterM2} == "Thin") then
        set CF_M2=`awk '{printf("%.4f \n", $1)}' $det/cf2.txt | perl -ne 'print unless $.<2 '`
      endif
      if (${filterM1} == "Medi") then
        set CF_M1=`awk '{printf("%.4f \n", $2)}' $det/cf1.txt | perl -ne 'print unless $.<2 '`
      endif
      if (${filterM2} == "Medi") then
        set CF_M2=`awk '{printf("%.4f \n", $2)}' $det/cf2.txt | perl -ne 'print unless $.<2 '`
      endif
      if (${filterM1} == "Thic") then
        set CF_M1=`awk '{printf("%.4f \n", $3)}' $det/cf1.txt | perl -ne 'print unless $.<2 '`
      endif
      if (${filterM2} == "Thic") then
        set CF_M2=`awk '{printf("%.4f \n", $3)}' $det/cf2.txt | perl -ne 'print unless $.<2 '`
      endif
      #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_lb boxsize=$boxsize_l nruns=1 withdetmask=yes detmasksets='$mask' imagesets='$ima1 $ima2' expimagesets='$expo1 $expo2' ecf='${CF_M1} ${CF_M2}' pimin='$bmin $bmin' pimax='$bmax $bmax' boxlistset=eboxlist_local_"$start"-"$stop".fits usematchedfilter=yes -w "$warn" -V "$verb"" >> $an_dir/eboxdetect_LM.csh
     endif
     if (${N_evtM1} < ${N_EN_BAND} || ${N_evtM2} < ${N_EN_BAND}) then
      echo "Performing eboxdetect LM with MOS1 MOS2 for total energy band"
      if (${filterM1} == "Thin") then
        set CF_M1=`awk -v nr=$b_tot '{if (NR==nr) printf("%.4f \n", $1)}' $det/cf1.txt`
      endif
      if (${filterM2} == "Thin") then
        set CF_M2=`awk -v nr=$b_tot '{if (NR==nr) printf("%.4f \n", $1)}' $det/cf2.txt`
      endif
      if (${filterM1} == "Medi") then
        set CF_M1=`awk -v nr=$b_tot '{if (NR==nr) printf("%.4f \n", $2)}' $det/cf1.txt`
      endif
      if (${filterM2} == "Medi") then
        set CF_M2=`awk -v nr=$b_tot '{if (NR==nr) printf("%.4f \n", $2)}' $det/cf2.txt`
      endif
      if (${filterM1} == "Thic") then
        set CF_M1=`awk -v nr=$b_tot '{if (NR==nr) printf("%.4f \n", $3)}' $det/cf1.txt`
      endif
      if (${filterM2} == "Thic") then
        set CF_M2=`awk -v nr=$b_tot '{if (NR==nr) printf("%.4f \n", $3)}' $det/cf2.txt`
      endif
      #set CF=`awk -v nr=$b_tot '{ if (NR==nr) printf("%.4f \n", $3)}' $det/cf1.txt && awk -v nr=$b_tot '{ if (NR==nr) printf("%.4f \n", $3)}' $det/cf2.txt`
      echo "/usr/bin/time -p eboxdetect usemap=no likemin=$likemin_lb boxsize=$boxsize_l nruns=1 withdetmask=yes detmasksets='$mask' imagesets='$ima_mos1 $ima_mos2' expimagesets='$expo_mos1 $expo_mos2' ecf='$CF_M1 $CF_M2' pimin='$bmin_tot $bmin_tot' pimax='$bmax_tot $bmax_tot' boxlistset=eboxlist_local_"$start"-"$stop".fits usematchedfilter=yes -w "$warn" -V "$verb"" >> $an_dir/eboxdetect_LM.csh
     endif
   endif
   endif 
   if ($ima_btot == 1) then
    if ($ima_mos1 == "$ExpDetectPrep/ExpDetect_MOS1_LE_noise_unfiltered_image_"$b_tot"_"$start"-"$stop".fits") then
     set filterM1=`ftlist ${ima_mos1}+0 K | grep "FILTER" | sed -e "s/'/ /g"  | cut -d' ' -f5 | awk '{print $1}' | cut -c "1-4"`
     if (${N_evtM1} == ${N_EN_BAND}) then
      echo "Performing eboxdetect LM with MOS1 for all energy bands"
      if (${filterM1} == "Thin") then
        set CF_M1=`awk '{printf("%.4f \n", $1)}' $det/cf1.txt | perl -ne 'print unless $.<2 '`
      endif
      if (${filterM1} == "Medi") then
        set CF_M1=`awk '{printf("%.4f \n", $2)}' $det/cf1.txt | perl -ne 'print unless $.<2 '`
      endif
      if (${filterM1} == "Thic") then
        set CF_M1=`awk '{printf("%.4f \n", $3)}' $det/cf1.txt | perl -ne 'print unless $.<2 '`
      endif
      #set CF=`awk '{printf("%.4f \n", $3)}' $det/cf1.txt | perl -ne 'print unless $.<2 '`
      echo "/usr/bin/time -p eboxdetect usemap=no likemin=$likemin_lb boxsize=$boxsize_l nruns=1 withdetmask=yes detmasksets='$mask' imagesets='$ima1' expimagesets='$expo1' ecf='$CF_M1' pimin='$bmin' pimax='$bmax' boxlistset=eboxlist_local_"$start"-"$stop".fits usematchedfilter=yes -w 1 -V "$verb"" >> $an_dir/eboxdetect_LM.csh
     endif
     if (${N_evtM1} < ${N_EN_BAND}) then
      echo "Performing eboxdetect LM with MOS1 for total energy band"
      if (${filterM1} == "Thin") then
        set CF_M1=`awk -v nr=$b_tot '{if (NR==nr) printf("%.4f \n", $1)}' $det/cf1.txt`
      endif
      if (${filterM1} == "Medi") then
        set CF_M1=`awk -v nr=$b_tot '{if (NR==nr) printf("%.4f \n", $2)}' $det/cf1.txt`
      endif
      if (${filterM1} == "Thic") then
        set CF_M1=`awk -v nr=$b_tot '{if (NR==nr) printf("%.4f \n", $3)}' $det/cf1.txt`
      endif
      #set CF=`awk -v nr=$b_tot '{ if (NR==nr) printf("%.4f \n", $3)}' $det/cf1.txt`
      echo "/usr/bin/time -p eboxdetect usemap=no likemin=$likemin_lb boxsize=$boxsize_l nruns=1 withdetmask=yes detmasksets='$mask' imagesets='$ima_mos1' expimagesets='$expo_mos1' ecf='$CF_M1' pimin='$bmin_tot' pimax='$bmax_tot' boxlistset=eboxlist_local_"$start"-"$stop".fits usematchedfilter=yes -w "$warn" -V "$verb"" >> $an_dir/eboxdetect_LM.csh
     endif
    endif
    if ($ima_mos2 == "$ExpDetectPrep/ExpDetect_MOS2_LE_noise_unfiltered_image_"$b_tot"_"$start"-"$stop".fits") then
     set filterM2=`ftlist ${ima_mos2}+0 K | grep "FILTER" | sed -e "s/'/ /g"  | cut -d' ' -f5 | awk '{print $1}' | cut -c "1-4"`
     if (${N_evtM2} == ${N_EN_BAND}) then
      echo "Performing eboxdetect LM with MOS2 for all energy bands"
      if (${filterM2} == "Thin") then
        set CF_M2=`awk '{printf("%.4f \n", $1)}' $det/cf2.txt | perl -ne 'print unless $.<2 '`
      endif
      if (${filterM2} == "Medi") then
        set CF_M2=`awk '{printf("%.4f \n", $2)}' $det/cf2.txt | perl -ne 'print unless $.<2 '`
      endif
      if (${filterM2} == "Thic") then
        set CF_M2=`awk '{printf("%.4f \n", $3)}' $det/cf2.txt | perl -ne 'print unless $.<2 '`
      endif
     # set CF=`awk '{printf("%.4f \n", $3)}' $det/cf2.txt | perl -ne 'print unless $.<2 '`
      echo "/usr/bin/time -p eboxdetect usemap=no likemin=$likemin_lb boxsize=$boxsize_l nruns=1 withdetmask=yes detmasksets='$mask' imagesets='$ima2' expimagesets='$expo2' ecf='$CF_M2' pimin='$bmin' pimax='$bmax' boxlistset=eboxlist_local_"$start"-"$stop".fits usematchedfilter=yes -w 1 -V "$verb"" >> $an_dir/eboxdetect_LM.csh
     endif
     if (${N_evtM2} < ${N_EN_BAND}) then
      echo "Performing eboxdetect LM with MOS2 for total energy band"
      if (${filterM2} == "Thin") then
        set CF_M2=`awk -v nr=$b_tot '{if (NR==nr) printf("%.4f \n", $1)}' $det/cf2.txt`
      endif
      if (${filterM2} == "Medi") then
        set CF_M2=`awk -v nr=$b_tot '{if (NR==nr) printf("%.4f \n", $2)}' $det/cf2.txt`
      endif
      if (${filterM2} == "Thic") then
        set CF_M2=`awk -v nr=$b_tot '{if (NR==nr) printf("%.4f \n", $3)}' $det/cf2.txt`
      endif
      #set CF=`awk -v nr=$b_tot '{ if (NR==nr) printf("%.4f \n", $3)}' $det/cf2.txt`
      echo "/usr/bin/time -p eboxdetect usemap=no likemin=$likemin_lb boxsize=$boxsize_l nruns=1 withdetmask=yes detmasksets='$mask' imagesets='$ima_mos2' expimagesets='$expo_mos2' ecf='$CF_M2' pimin='$bmin_tot' pimax='$bmax_tot' boxlistset=eboxlist_local_"$start"-"$stop".fits usematchedfilter=yes -w "$warn" -V "$verb"" >> $an_dir/eboxdetect_LM.csh
     endif
    endif
   endif
  source $an_dir/eboxdetect_LM.csh
  rm $an_dir/eboxdetect_LM.csh
  echo "OK: eboxlist_local_"$start"-"$stop".fits done in energy band (eV) ["$bmin":"$bmax"]"

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

if ( $DET == 01 ) then
 if (-e $ExpDetectPrep/ExpDetect_MOS1_LE_noise_unfiltered_image_"$b_tot"_"$start"-"$stop".fits) then
  echo "N. energy band: ${N_EN_BAND}" 
  set N_evtM1 = `ls $ExpDetectPrep/ExpDetectPrep_MOS1_merged_event_list_* | perl -ne 'print unless $.<2 ' | wc -l | awk '{print $1}'`
  echo "N. of MOS1 event files: $N_evtM1"    
  set ima_btot = `ls $ExpDetectPrep/ExpDetect_MOS1_LE_noise_unfiltered_image_"$b_tot"_"$start"-"$stop".fits | wc -l | awk '{print $1}'`
  set ima_mos1 = `ls $ExpDetectPrep/ExpDetect_MOS1_LE_noise_unfiltered_image_"$b_tot"_"$start"-"$stop".fits`
  set expo_mos1 = `ls $ExpDetectPrep/ExpDetect_MOS1_LE_noise_filtered_expo_"$b_tot"_"$start"-"$stop".fits`
  set ima1 = `ls $ExpDetectPrep/ExpDetect_MOS1_LE_noise_unfiltered_image_*_"$start"-"$stop".fits | perl -ne 'print unless $.<2 '`
  set expo1 = `ls $ExpDetectPrep/ExpDetect_MOS1_LE_noise_filtered_expo_*_"$start"-"$stop".fits | perl -ne 'print unless $.<2 '`
  set mask_soft = `ls $an_dir/"$dir0"_MOS1_detmask_"$b_soft"_"$start"-"$stop".fits | wc -l | awk '{print $1}'`
  if ($mask_soft == $ima_btot) then
   set mask = `ls $an_dir/"$dir0"_MOS1_detmask_"$b_soft"_"$start"-"$stop".fits`
  else
   set mask = `ls $an_dir/"$dir0"_MOS1_detmask_"$b_tot"_"$start"-"$stop".fits`
  endif
  echo "#! /bin/tcsh -f" > $an_dir/eboxdetect_LM.csh

   if ($ima_btot == 1) then
    if ($ima_mos1 == "$ExpDetectPrep/ExpDetect_MOS1_LE_noise_unfiltered_image_"$b_tot"_"$start"-"$stop".fits") then
     set filterM1=`ftlist ${ima_mos1}+0 K | grep "FILTER" | sed -e "s/'/ /g"  | cut -d' ' -f5 | awk '{print $1}' | cut -c "1-4"`
     if (${N_evtM1} == ${N_EN_BAND}) then
      echo "Performing eboxdetect LM with MOS1 for all energy band"
      if (${filterM1} == "Thin") then
        set CF_M1=`awk '{printf("%.4f \n", $1)}' $det/cf1.txt | perl -ne 'print unless $.<2 '`
      endif
      if (${filterM1} == "Medi") then
        set CF_M1=`awk '{printf("%.4f \n", $2)}' $det/cf1.txt | perl -ne 'print unless $.<2 '`
      endif
      if (${filterM1} == "Thic") then
        set CF_M1=`awk '{printf("%.4f \n", $3)}' $det/cf1.txt | perl -ne 'print unless $.<2 '`
      endif
      #set CF=`awk '{printf("%.4f \n", $3)}' $det/cf1.txt | perl -ne 'print unless $.<2 '`
      echo "/usr/bin/time -p eboxdetect usemap=no likemin=$likemin_lb boxsize=$boxsize_l nruns=1 withdetmask=yes detmasksets='$mask' imagesets='$ima1' expimagesets='$expo1' ecf='$CF_M1' pimin='$bmin' pimax='$bmax' boxlistset=eboxlist_local_"$start"-"$stop".fits usematchedfilter=yes -w "$warn" -V "$verb"" >> $an_dir/eboxdetect_LM.csh
     endif
     if (${N_evtM1} < ${N_EN_BAND}) then
      echo "Performing eboxdetect LM with MOS1 for total energy band"
      if (${filterM1} == "Thin") then
        set CF_M1=`awk -v nr=$b_tot '{if (NR==nr) printf("%.4f \n", $1)}' $det/cf1.txt`
      endif
      if (${filterM1} == "Medi") then
        set CF_M1=`awk -v nr=$b_tot '{if (NR==nr) printf("%.4f \n", $2)}' $det/cf1.txt`
      endif
      if (${filterM1} == "Thic") then
        set CF_M1=`awk -v nr=$b_tot '{if (NR==nr) printf("%.4f \n", $3)}' $det/cf1.txt`
      endif
      #set CF=`awk -v nr=$b_tot '{ if (NR==nr) printf("%.4f \n", $3)}' $det/cf1.txt`
      echo "/usr/bin/time -p eboxdetect usemap=no likemin=$likemin_lb boxsize=$boxsize_l nruns=1 withdetmask=yes detmasksets='$mask' imagesets='$ima_mos1' expimagesets='$expo_mos1' ecf='$CF_M1' pimin='$bmin_tot' pimax='$bmax_tot' boxlistset=eboxlist_local_"$start"-"$stop".fits usematchedfilter=yes -w "$warn" -V "$verb"" >> $an_dir/eboxdetect_LM.csh
     endif
    endif
   endif 
  source $an_dir/eboxdetect_LM.csh
  rm $an_dir/eboxdetect_LM.csh
  echo "OK: eboxlist_local_"$start"-"$stop".fits done in energy band (eV) ["$bmin":"$bmax"]"

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

if ( $DET == 02 ) then
 if (-e $ExpDetectPrep/ExpDetect_MOS2_LE_noise_unfiltered_image_"$b_tot"_"$start"-"$stop".fits) then
  echo "N. energy band: ${N_EN_BAND}" 
   set N_evtM2 = `ls $ExpDetectPrep/ExpDetectPrep_MOS2_merged_event_list_* | perl -ne 'print unless $.<2 ' | wc -l | awk '{print $1}'`
  echo "N. of MOS2 event files: $N_evtM2"    
   set ima_btot = `ls $ExpDetectPrep/ExpDetect_MOS2_LE_noise_unfiltered_image_"$b_tot"_"$start"-"$stop".fits | wc -l | awk '{print $1}'`
   set ima_mos2 = `ls $ExpDetectPrep/ExpDetect_MOS2_LE_noise_unfiltered_image_"$b_tot"_"$start"-"$stop".fits`
   set expo_mos2 = `ls $ExpDetectPrep/ExpDetect_MOS2_LE_noise_filtered_expo_"$b_tot"_"$start"-"$stop".fits`
   set ima2 = `ls $ExpDetectPrep/ExpDetect_MOS2_LE_noise_unfiltered_image_*_"$start"-"$stop".fits | perl -ne 'print unless $.<2 '`
   set expo2 = `ls $ExpDetectPrep/ExpDetect_MOS2_LE_noise_filtered_expo_*_"$start"-"$stop".fits | perl -ne 'print unless $.<2 '`
  set mask_soft = `ls $an_dir/"$dir0"_MOS2_detmask_"$b_soft"_"$start"-"$stop".fits | wc -l | awk '{print $1}'`
  if ($mask_soft == $ima_btot) then
   set mask = `ls $an_dir/"$dir0"_MOS2_detmask_"$b_soft"_"$start"-"$stop".fits`
  else
   set mask = `ls $an_dir/"$dir0"_MOS2_detmask_"$b_tot"_"$start"-"$stop".fits`
  endif
   echo "#! /bin/tcsh -f" > $an_dir/eboxdetect_LM.csh

   if ($ima_btot == 1) then
    if ($ima_mos2 == "$ExpDetectPrep/ExpDetect_MOS2_LE_noise_unfiltered_image_"$b_tot"_"$start"-"$stop".fits") then
     set filterM2=`ftlist ${ima_mos2}+0 K | grep "FILTER" | sed -e "s/'/ /g"  | cut -d' ' -f5 | awk '{print $1}' | cut -c "1-4"`
     if (${N_evtM2} == ${N_EN_BAND}) then
      echo "Performing eboxdetect LM with MOS2 for all energy band"
      if (${filterM2} == "Thin") then
        set CF_M2=`awk '{printf("%.4f \n", $1)}' $det/cf2.txt | perl -ne 'print unless $.<2 '`
      endif
      if (${filterM2} == "Medi") then
        set CF_M2=`awk '{printf("%.4f \n", $2)}' $det/cf2.txt | perl -ne 'print unless $.<2 '`
      endif
      if (${filterM2} == "Thic") then
        set CF_M2=`awk '{printf("%.4f \n", $3)}' $det/cf2.txt | perl -ne 'print unless $.<2 '`
      endif
      #set CF=`awk '{printf("%.4f \n", $3)}' $det/cf2.txt | perl -ne 'print unless $.<2 '`
      echo "/usr/bin/time -p eboxdetect usemap=no likemin=$likemin_lb boxsize=$boxsize_l nruns=1 withdetmask=yes detmasksets='$mask' imagesets='$ima2' expimagesets='$expo2' ecf='$CF_M2' pimin='$bmin' pimax='$bmax' boxlistset=eboxlist_local_"$start"-"$stop".fits usematchedfilter=yes -w "$warn" -V "$verb"" >> $an_dir/eboxdetect_LM.csh
     endif
     if (${N_evtM2} < ${N_EN_BAND}) then
      echo "Performing eboxdetect LM with MOS2 for total energy band"
      if (${filterM2} == "Thin") then
        set CF_M2=`awk -v nr=$b_tot '{if (NR==nr) printf("%.4f \n", $1)}' $det/cf2.txt`
      endif
      if (${filterM2} == "Medi") then
        set CF_M2=`awk -v nr=$b_tot '{if (NR==nr) printf("%.4f \n", $2)}' $det/cf2.txt`
      endif
      if (${filterM2} == "Thic") then
        set CF_M2=`awk -v nr=$b_tot '{if (NR==nr) printf("%.4f \n", $3)}' $det/cf2.txt`
      endif
      #set CF=`awk -v nr=$b_tot '{ if (NR==nr) printf("%.4f \n", $3)}' $det/cf2.txt`
      echo "/usr/bin/time -p eboxdetect usemap=no likemin=$likemin_lb boxsize=$boxsize_l nruns=1 withdetmask=yes detmasksets='$mask' imagesets='$ima_mos2' expimagesets='$expo_mos2' ecf='$CF_M2' pimin='$bmin_tot' pimax='$bmax_tot' boxlistset=eboxlist_local_"$start"-"$stop".fits usematchedfilter=yes -w "$warn" -V "$verb"" >> $an_dir/eboxdetect_LM.csh
     endif
    endif
   endif 
  source $an_dir/eboxdetect_LM.csh
  rm $an_dir/eboxdetect_LM.csh
  echo "OK: eboxlist_local_"$start"-"$stop".fits done in energy band (eV) ["$bmin":"$bmax"]"

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

if ( $DET == 03 ) then
 if (-e $ExpDetectPrep/ExpDetectPrep_PN_merged_image_"$b_tot"_"$start"-"$stop".fits) then
  echo "N. energy band: ${N_EN_BAND}" 
  set N_evtPN = `ls $ExpDetectPrep/ExpDetectPrep_PN_merged_event_list_* | perl -ne 'print unless $.<2 ' | wc -l | awk '{print $1}'`
  echo "N. of pn event files: $N_evtPN"    
  set ima_btot = `ls $ExpDetectPrep/ExpDetectPrep_PN_merged_image_"$b_tot"_"$start"-"$stop".fits | wc -l | awk '{print $1}' `
  set ima_pn = `ls $ExpDetectPrep/ExpDetectPrep_PN_merged_image_"$b_tot"_"$start"-"$stop".fits`
  set expo_pn = `ls $ExpDetectPrep/ExpDetectPrep_PN_merged_expo_"$b_tot"_"$start"-"$stop".fits`
  set ima = `ls $ExpDetectPrep/ExpDetectPrep_PN_merged_image_*_"$start"-"$stop".fits | perl -ne 'print unless $.<2 '`
  set expo = `ls $ExpDetectPrep/ExpDetectPrep_PN_merged_expo_*_"$start"-"$stop".fits | perl -ne 'print unless $.<2 '`
  set mask_soft = `ls $an_dir/"$dir0"_PN_detmask_"$b_soft"_"$start"-"$stop".fits | wc -l | awk '{print $1}'`
  if ($mask_soft == $ima_btot) then
   set mask = `ls $an_dir/"$dir0"_PN_detmask_"$b_soft"_"$start"-"$stop".fits`
  else
   set mask = `ls $an_dir/"$dir0"_PN_detmask_"$b_tot"_"$start"-"$stop".fits`
  endif
  echo "#! /bin/tcsh -f" > $an_dir/eboxdetect_LM.csh

 if ($ima_pn ==  "$ExpDetectPrep/ExpDetectPrep_PN_merged_image_"$b_tot"_"$start"-"$stop".fits") then
  set filterPN=`ftlist ${ima_pn}+0 K | grep "FILTER" | sed -e "s/'/ /g"  | cut -d' ' -f5 | awk '{print $1}' | cut -c "1-4"`
  if (${N_evtPN} == ${N_EN_BAND}) then
   echo "Performing eboxdetect LM with pn for all energy bands"
      if (${filterPN} == "Thin") then
        set CF_PN=`awk '{printf("%.4f \n", $1)}' $det/cf3.txt | perl -ne 'print unless $.<2 '`
      endif
      if (${filterPN} == "Medi") then
        set CF_PN=`awk '{printf("%.4f \n", $2)}' $det/cf3.txt | perl -ne 'print unless $.<2 '`
      endif
      if (${filterPN} == "Thic") then
        set CF_PN=`awk '{printf("%.4f \n", $3)}' $det/cf3.txt | perl -ne 'print unless $.<2 '`
      endif
   #set CF=`awk '{printf("%.4f \n", $3)}' $det/cf3.txt | perl -ne 'print unless $.<2 '`
   echo "/usr/bin/time -p eboxdetect usemap=no likemin=$likemin_lb boxsize=$boxsize_l nruns=1 withdetmask=yes detmasksets='$mask' imagesets='$ima' expimagesets='$expo' ecf='$CF_PN' pimin='$bmin' pimax='$bmax' boxlistset=eboxlist_local_"$start"-"$stop".fits usematchedfilter=yes -w "$warn" -V "$verb"" >> $an_dir/eboxdetect_LM.csh
  endif
  if (${N_evtPN} < ${N_EN_BAND}) then
    echo "Performing eboxdetect LM with pn for total energy band"
      if (${filterPN} == "Thin") then
        set CF_PN=`awk -v nr=$b_tot '{if (NR==nr) printf("%.4f \n", $1)}' $det/cf3.txt`
      endif
      if (${filterPN} == "Medi") then
        set CF_PN=`awk -v nr=$b_tot '{if (NR==nr) printf("%.4f \n", $2)}' $det/cf3.txt`
      endif
      if (${filterPN} == "Thic") then
        set CF_PN=`awk -v nr=$b_tot '{if (NR==nr) printf("%.4f \n", $3)}' $det/cf3.txt`
      endif
    #set CF=`awk -v nr=$b_tot '{ if (NR==nr) printf("%.4f \n", $3)}' $det/cf3.txt`
    echo "/usr/bin/time -p eboxdetect usemap=no likemin=$likemin_lb boxsize=$boxsize_l nruns=1 withdetmask=yes detmasksets=' $mask' imagesets='$ima_pn' expimagesets='$expo_pn' ecf='$CF_PN' pimin='$bmin_tot' pimax='$bmax_tot' boxlistset=eboxlist_local_"$start"-"$stop".fits usematchedfilter=yes -w "$warn" -V "$verb"" >> $an_dir/eboxdetect_LM.csh
  endif
  source $an_dir/eboxdetect_LM.csh
  rm $an_dir/eboxdetect_LM.csh
  echo "OK: eboxlist_local_"$start"-"$stop".fits done in energy band (eV) ["$bmin":"$bmax"]"

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

if ( $DET == 2 ) then
 if (-e $ExpDetectPrep/ExpDetect_MOS1_LE_noise_unfiltered_image_"$b_tot"_"$start"-"$stop".fits || -e $ExpDetectPrep/ExpDetect_MOS2_LE_noise_unfiltered_image_"$b_tot"_"$start"-"$stop".fits || -e $ExpDetectPrep/ExpDetectPrep_PN_merged_image_"$b_tot"_"$start"-"$stop".fits) then
  
  set ima_btot = `ls $ExpDetectPrep/ExpDetect_MOS1_LE_noise_unfiltered_image_"$b_tot"_"$start"-"$stop".fits $ExpDetectPrep/ExpDetect_MOS2_LE_noise_unfiltered_image_"$b_tot"_"$start"-"$stop".fits $ExpDetectPrep/ExpDetectPrep_PN_merged_image_"$b_tot"_"$start"-"$stop".fits | wc -l | awk '{print $1}'`
  echo "N. energy band: ${N_EN_BAND}" 
  set N_evtM1=`ls $ExpDetectPrep/ExpDetectPrep_MOS1_merged_event_list_*.fits | perl -ne 'print unless $.<2 ' | wc -l | awk '{print $1}'`
  set N_evtM2=`ls $ExpDetectPrep/ExpDetectPrep_MOS2_merged_event_list_*.fits | perl -ne 'print unless $.<2 ' | wc -l | awk '{print $1}'`
  set N_evtPN=`ls $ExpDetectPrep/ExpDetectPrep_PN_merged_event_list_*.fits | perl -ne 'print unless $.<2 ' | wc -l | awk '{print $1}'`
  echo "N. of MOS1 event files: $N_evtM1"    
  echo "N. of MOS2 event files: $N_evtM2"
  echo "N. of pn event files: $N_evtPN"
  set ima_mos1 = `ls $ExpDetectPrep/ExpDetect_MOS1_LE_noise_unfiltered_image_"$b_tot"_"$start"-"$stop".fits`
  set ima_mos2 = `ls $ExpDetectPrep/ExpDetect_MOS2_LE_noise_unfiltered_image_"$b_tot"_"$start"-"$stop".fits`
  set ima_pn = `ls $ExpDetectPrep/ExpDetectPrep_PN_merged_image_"$b_tot"_"$start"-"$stop".fits`
  set expo_mos1 = `ls $ExpDetectPrep/ExpDetect_MOS1_LE_noise_filtered_expo_"$b_tot"_"$start"-"$stop".fits`
  set expo_mos2 = `ls $ExpDetectPrep/ExpDetect_MOS2_LE_noise_filtered_expo_"$b_tot"_"$start"-"$stop".fits`
  set expo_pn = `ls $ExpDetectPrep/ExpDetectPrep_PN_merged_expo_"$b_tot"_"$start"-"$stop".fits`
  set ima1 = `ls $ExpDetectPrep/ExpDetect_MOS1_LE_noise_unfiltered_image_*_"$start"-"$stop".fits | perl -ne 'print unless $.<2 '`
  set ima2 = `ls $ExpDetectPrep/ExpDetect_MOS2_LE_noise_unfiltered_image_*_"$start"-"$stop".fits | perl -ne 'print unless $.<2 '`
  set ima3 = `ls $ExpDetectPrep/ExpDetectPrep_PN_merged_image_*_"$start"-"$stop".fits | perl -ne 'print unless $.<2 '`
  set expo1 = `ls $ExpDetectPrep/ExpDetect_MOS1_LE_noise_filtered_expo_*_"$start"-"$stop".fits | perl -ne 'print unless $.<2 '`
  set expo2 = `ls $ExpDetectPrep/ExpDetect_MOS2_LE_noise_filtered_expo_*_"$start"-"$stop".fits | perl -ne 'print unless $.<2 '`
  set expo3 = `ls $ExpDetectPrep/ExpDetectPrep_PN_merged_expo_*_"$start"-"$stop".fits | perl -ne 'print unless $.<2 '`
  set mask_soft = `ls $an_dir/"$dir0"_MOS1_detmask_"$b_soft"_"$start"-"$stop".fits $an_dir/"$dir0"_MOS2_detmask_"$b_soft"_"$start"-"$stop".fits $an_dir/"$dir0"_PN_detmask_"$b_soft"_"$start"-"$stop".fits | wc -l | awk '{print $1}'`
  if ($mask_soft == $ima_btot) then
   set mask1 = `ls $an_dir/"$dir0"_PN_detmask_"$b_soft"_"$start"-"$stop".fits`
   set mask2 = `ls $an_dir/"$dir0"_MOS*_detmask_"$b_soft"_"$start"-"$stop".fits`
  else
   set mask1 = `ls $an_dir/"$dir0"_PN_detmask_"$b_tot"_"$start"-"$stop".fits`
   set mask2 = `ls $an_dir/"$dir0"_MOS*_detmask_"$b_tot"_"$start"-"$stop".fits`
  endif
  echo "#! /bin/tcsh -f" > $an_dir/eboxdetect_LM.csh

   if (${ima_btot} == 3) then
   if ($ima_mos1 == "$ExpDetectPrep/ExpDetect_MOS1_LE_noise_unfiltered_image_"$b_tot"_"$start"-"$stop".fits" && $ima_mos2 == "$ExpDetectPrep/ExpDetect_MOS2_LE_noise_unfiltered_image_"$b_tot"_"$start"-"$stop".fits" && $ima_pn == "$ExpDetectPrep/ExpDetectPrep_PN_merged_image_"$b_tot"_"$start"-"$stop".fits") then
     set filterM1=`ftlist ${ima_mos1}+0 K | grep "FILTER" | sed -e "s/'/ /g"  | cut -d' ' -f5 | awk '{print $1}' | cut -c "1-4"`
     set filterM2=`ftlist ${ima_mos2}+0 K | grep "FILTER" | sed -e "s/'/ /g"  | cut -d' ' -f5 | awk '{print $1}' | cut -c "1-4"`
     set filterPN=`ftlist ${ima_pn}+0 K | grep "FILTER" | sed -e "s/'/ /g"  | cut -d' ' -f5 | awk '{print $1}' | cut -c "1-4"`
    if (${N_evtM1} == ${N_EN_BAND} && ${N_evtM2} == ${N_EN_BAND} && ${N_evtPN} == ${N_EN_BAND}) then
     echo "Performing eboxdetect LM with MOS1 MOS2 pn for all energy bands"
      if (${filterM1} == "Thin") then
        set CF_M1=`awk '{printf("%.4f \n", $1)}' $det/cf1.txt | perl -ne 'print unless $.<2 '`
      endif
      if (${filterM2} == "Thin") then
        set CF_M2=`awk '{printf("%.4f \n", $1)}' $det/cf2.txt | perl -ne 'print unless $.<2 '`
      endif
      if (${filterPN} == "Thin") then
        set CF_PN=`awk '{printf("%.4f \n", $1)}' $det/cf3.txt | perl -ne 'print unless $.<2 '`
      endif
      if (${filterM1} == "Medi") then
        set CF_M1=`awk '{printf("%.4f \n", $2)}' $det/cf1.txt | perl -ne 'print unless $.<2 '`
      endif
      if (${filterM2} == "Medi") then
        set CF_M2=`awk '{printf("%.4f \n", $2)}' $det/cf2.txt | perl -ne 'print unless $.<2 '`
      endif
      if (${filterPN} == "Medi") then
        set CF_PN=`awk '{printf("%.4f \n", $2)}' $det/cf3.txt | perl -ne 'print unless $.<2 '`
      endif
      if (${filterM1} == "Thic") then
        set CF_M1=`awk '{printf("%.4f \n", $3)}' $det/cf1.txt | perl -ne 'print unless $.<2 '`
      endif
      if (${filterM2} == "Thic") then
        set CF_M2=`awk '{printf("%.4f \n", $3)}' $det/cf2.txt | perl -ne 'print unless $.<2 '`
      endif
      if (${filterPN} == "Thic") then
        set CF_PN=`awk '{printf("%.4f \n", $3)}' $det/cf3.txt | perl -ne 'print unless $.<2 '`
      endif
     #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_lb boxsize=$boxsize_l nruns=1 withdetmask=yes detmasksets='$mask1 $mask2' imagesets='$ima3 $ima1 $ima2' expimagesets='$expo3 $expo1 $expo2' ecf='$CF_PN $CF_M1 $CF_M2' pimin='$bmin $bmin $bmin' pimax='$bmax $bmax $bmax' boxlistset=eboxlist_local_"$start"-"$stop".fits usematchedfilter=yes -w "$warn" -V "$verb"" >> $an_dir/eboxdetect_LM.csh
    endif
    if (${N_evtM1} < ${N_EN_BAND} || ${N_evtM2} < ${N_EN_BAND} || ${N_evtPN} < ${N_EN_BAND}) then
     echo "Performing eboxdetect LM with MOS1 MOS2 pn for total energy band"
      if (${filterM1} == "Thin") then
        set CF_M1=`awk -v nr=$b_tot '{if (NR==nr) printf("%.4f \n", $1)}' $det/cf1.txt`
      endif
      if (${filterM2} == "Thin") then
        set CF_M2=`awk -v nr=$b_tot '{if (NR==nr) printf("%.4f \n", $1)}' $det/cf2.txt`
      endif
      if (${filterPN} == "Thin") then
        set CF_PN=`awk -v nr=$b_tot '{if (NR==nr) printf("%.4f \n", $1)}' $det/cf3.txt`
      endif
      if (${filterM1} == "Medi") then
        set CF_M1=`awk -v nr=$b_tot '{if (NR==nr) printf("%.4f \n", $2)}' $det/cf1.txt`
      endif
      if (${filterM2} == "Medi") then
        set CF_M2=`awk -v nr=$b_tot '{if (NR==nr) printf("%.4f \n", $2)}' $det/cf2.txt`
      endif
      if (${filterPN} == "Medi") then
        set CF_PN=`awk -v nr=$b_tot '{if (NR==nr) printf("%.4f \n", $2)}' $det/cf3.txt`
      endif
      if (${filterM1} == "Thic") then
        set CF_M1=`awk -v nr=$b_tot '{if (NR==nr) printf("%.4f \n", $3)}' $det/cf1.txt`
      endif
      if (${filterM2} == "Thic") then
        set CF_M2=`awk -v nr=$b_tot '{if (NR==nr) printf("%.4f \n", $3)}' $det/cf2.txt`
      endif
      if (${filterPN} == "Thic") then
        set CF_PN=`awk -v nr=$b_tot '{if (NR==nr) printf("%.4f \n", $3)}' $det/cf3.txt`
      endif
     echo ${ima_btot}
     #set CF=`awk -v nr=$b_tot '{ if (NR==nr) printf("%.4f \n", $3)}' $det/cf3.txt && awk -v nr=$b_tot '{ if (NR==nr) printf("%.4f \n", $3)}' $det/cf1.txt && awk -v nr=$b_tot '{ if (NR==nr) printf("%.4f \n", $3)}' $det/cf2.txt`
     echo "/usr/bin/time -p eboxdetect usemap=no likemin=$likemin_lb boxsize=$boxsize_l nruns=1 withdetmask=yes detmasksets='$mask1 $mask2' imagesets='$ima_pn $ima_mos1 $ima_mos2' expimagesets='$expo_pn $expo_mos1 $expo_mos2' ecf='$CF_PN $CF_M1 $CF_M2' pimin='$bmin_tot $bmin_tot $bmin_tot' pimax='$bmax_tot $bmax_tot $bmax_tot' boxlistset=eboxlist_local_"$start"-"$stop".fits usematchedfilter=yes -w "$warn" -V "$verb"" >> $an_dir/eboxdetect_LM.csh
    endif
   endif
   endif
   if ($ima_btot == 2) then
    if ($ima_mos1 == "$ExpDetectPrep/ExpDetect_MOS1_LE_noise_unfiltered_image_"$b_tot"_"$start"-"$stop".fits" && $ima_mos2 == "$ExpDetectPrep/ExpDetect_MOS2_LE_noise_unfiltered_image_"$b_tot"_"$start"-"$stop".fits") then
     set filterM1=`ftlist ${ima_mos1}+0 K | grep "FILTER" | sed -e "s/'/ /g"  | cut -d' ' -f5 | awk '{print $1}' | cut -c "1-4"`
     set filterM2=`ftlist ${ima_mos2}+0 K | grep "FILTER" | sed -e "s/'/ /g"  | cut -d' ' -f5 | awk '{print $1}' | cut -c "1-4"`
     if (${N_evtM1} == ${N_EN_BAND} && ${N_evtM2} == ${N_EN_BAND}) then
      echo "Performing eboxdetect LM with MOS1 MOS2 for all energy bands"
      if (${filterM1} == "Thin") then
        set CF_M1=`awk '{printf("%.4f \n", $1)}' $det/cf1.txt | perl -ne 'print unless $.<2 '`
      endif
      if (${filterM2} == "Thin") then
        set CF_M2=`awk '{printf("%.4f \n", $1)}' $det/cf2.txt | perl -ne 'print unless $.<2 '`
      endif
      if (${filterM1} == "Medi") then
        set CF_M1=`awk '{printf("%.4f \n", $2)}' $det/cf1.txt | perl -ne 'print unless $.<2 '`
      endif
      if (${filterM2} == "Medi") then
        set CF_M2=`awk '{printf("%.4f \n", $2)}' $det/cf2.txt | perl -ne 'print unless $.<2 '`
      endif
      if (${filterM1} == "Thic") then
        set CF_M1=`awk '{printf("%.4f \n", $3)}' $det/cf1.txt | perl -ne 'print unless $.<2 '`
      endif
      if (${filterM2} == "Thic") then
        set CF_M2=`awk '{printf("%.4f \n", $3)}' $det/cf2.txt | perl -ne 'print unless $.<2 '`
      endif
      #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_lb boxsize=$boxsize_l nruns=1 withdetmask=yes detmasksets='$mask2' imagesets='$ima1 $ima2' expimagesets='$expo1 $expo2' ecf='$CF_M1 $CF_M2' pimin='$bmin $bmin' pimax='$bmax $bmax' boxlistset=eboxlist_local_"$start"-"$stop".fits usematchedfilter=yes -w "$warn" -V "$verb"" >> $an_dir/eboxdetect_LM.csh
     endif
     if (${N_evtM1} < ${N_EN_BAND} || ${N_evtM2} < ${N_EN_BAND}) then
      echo "Performing eboxdetect LM with MOS1 MOS2 for total energy band"
      if (${filterM1} == "Thin") then
        set CF_M1=`awk -v nr=$b_tot '{if (NR==nr) printf("%.4f \n", $1)}' $det/cf1.txt`
      endif
      if (${filterM2} == "Thin") then
        set CF_M2=`awk -v nr=$b_tot '{if (NR==nr) printf("%.4f \n", $1)}' $det/cf2.txt`
      endif
      if (${filterM1} == "Medi") then
        set CF_M1=`awk -v nr=$b_tot '{if (NR==nr) printf("%.4f \n", $2)}' $det/cf1.txt`
      endif
      if (${filterM2} == "Medi") then
        set CF_M2=`awk -v nr=$b_tot '{if (NR==nr) printf("%.4f \n", $2)}' $det/cf2.txt`
      endif
      if (${filterM1} == "Thic") then
        set CF_M1=`awk -v nr=$b_tot '{if (NR==nr) printf("%.4f \n", $3)}' $det/cf1.txt`
      endif
      if (${filterM2} == "Thic") then
        set CF_M2=`awk -v nr=$b_tot '{if (NR==nr) printf("%.4f \n", $3)}' $det/cf2.txt`
      endif
      #set CF=`awk -v nr=$b_tot '{ if (NR==nr) printf("%.4f \n", $3)}' $det/cf1.txt && awk -v nr=$b_tot '{ if (NR==nr) printf("%.4f \n", $3)}' $det/cf2.txt`
      echo "/usr/bin/time -p eboxdetect usemap=no likemin=$likemin_lb boxsize=$boxsize_l nruns=1 withdetmask=yes detmasksets='$mask2' imagesets='$ima_mos1 $ima_mos2' expimagesets='$expo_mos1 $expo_mos2' ecf='$CF_M1 $CF_M2' pimin='$bmin_tot $bmin_tot' pimax='$bmax_tot $bmax_tot' boxlistset=eboxlist_local_"$start"-"$stop".fits usematchedfilter=yes -w "$warn" -V "$verb"" >> $an_dir/eboxdetect_LM.csh
     endif
    endif
    if ($ima_mos1 == "$ExpDetectPrep/ExpDetect_MOS1_LE_noise_unfiltered_image_"$b_tot"_"$start"-"$stop".fits" && $ima_pn == "$ExpDetectPrep/ExpDetectPrep_PN_merged_image_"$b_tot"_"$start"-"$stop".fits") then
     set filterM1=`ftlist ${ima_mos1}+0 K | grep "FILTER" | sed -e "s/'/ /g"  | cut -d' ' -f5 | awk '{print $1}' | cut -c "1-4"`
     set filterPN=`ftlist ${ima_pn}+0 K | grep "FILTER" | sed -e "s/'/ /g"  | cut -d' ' -f5 | awk '{print $1}' | cut -c "1-4"`
     if (${N_evtM1} == ${N_EN_BAND} && ${N_evtPN} == ${N_EN_BAND}) then
      echo "Performing eboxdetect LM with MOS1 pn for all energy bands"
      if (${filterM1} == "Thin") then
        set CF_M1=`awk '{printf("%.4f \n", $1)}' $det/cf1.txt | perl -ne 'print unless $.<2 '`
      endif
      if (${filterPN} == "Thin") then
        set CF_PN=`awk '{printf("%.4f \n", $1)}' $det/cf3.txt | perl -ne 'print unless $.<2 '`
      endif
      if (${filterM1} == "Medi") then
        set CF_M1=`awk '{printf("%.4f \n", $2)}' $det/cf1.txt | perl -ne 'print unless $.<2 '`
      endif
      if (${filterPN} == "Medi") then
        set CF_PN=`awk '{printf("%.4f \n", $2)}' $det/cf3.txt | perl -ne 'print unless $.<2 '`
      endif
      if (${filterM1} == "Thic") then
        set CF_M1=`awk '{printf("%.4f \n", $3)}' $det/cf1.txt | perl -ne 'print unless $.<2 '`
      endif
      if (${filterPN} == "Thic") then
        set CF_PN=`awk '{printf("%.4f \n", $3)}' $det/cf3.txt | perl -ne 'print unless $.<2 '`
      endif
      #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 '`
      echo "/usr/bin/time -p eboxdetect usemap=no likemin=$likemin_lb boxsize=$boxsize_l nruns=1 withdetmask=yes detmasksets='$mask1 $mask2' imagesets='$ima3 $ima1' expimagesets='$expo3 $expo1' ecf='$CF_PN $CF_M1' pimin='$bmin $bmin' pimax='$bmax $bmax' boxlistset=eboxlist_local_"$start"-"$stop".fits usematchedfilter=yes -w "$warn" -V "$verb"" >> $an_dir/eboxdetect_LM.csh
     endif
     if (${N_evtM1} < ${N_EN_BAND} || ${N_evtPN} < ${N_EN_BAND}) then
      echo "Performing eboxdetect LM with MOS1 pn for total energy band"
      if (${filterM1} == "Thin") then
        set CF_M1=`awk -v nr=$b_tot '{if (NR==nr) printf("%.4f \n", $1)}' $det/cf1.txt`
      endif
      if (${filterPN} == "Thin") then
        set CF_PN=`awk -v nr=$b_tot '{if (NR==nr) printf("%.4f \n", $1)}' $det/cf3.txt`
      endif
      if (${filterM1} == "Medi") then
        set CF_M1=`awk -v nr=$b_tot '{if (NR==nr) printf("%.4f \n", $2)}' $det/cf1.txt`
      endif
      if (${filterPN} == "Medi") then
        set CF_PN=`awk -v nr=$b_tot '{if (NR==nr) printf("%.4f \n", $2)}' $det/cf3.txt`
      endif
      if (${filterM1} == "Thic") then
        set CF_M1=`awk -v nr=$b_tot '{if (NR==nr) printf("%.4f \n", $3)}' $det/cf1.txt`
      endif
      if (${filterPN} == "Thic") then
        set CF_PN=`awk -v nr=$b_tot '{if (NR==nr) printf("%.4f \n", $3)}' $det/cf3.txt`
      endif
      #set CF=`awk -v nr=$b_tot '{ if (NR==nr) printf("%.4f \n", $3)}' $det/cf3.txt && awk -v nr=$b_tot '{ if (NR==nr) printf("%.4f \n", $3)}' $det/cf1.txt`
      echo "/usr/bin/time -p eboxdetect usemap=no likemin=$likemin_lb boxsize=$boxsize_l nruns=1 withdetmask=yes detmasksets=' $mask1 $mask2' imagesets='$ima_pn $ima_mos1' expimagesets='$expo_pn $expo_mos1' ecf='$CF_PN $CF_M1' pimin='$bmin_tot $bmin_tot' pimax='$bmax_tot $bmax_tot' boxlistset=eboxlist_local_"$start"-"$stop".fits usematchedfilter=yes -w "$warn" -V "$verb"" >> $an_dir/eboxdetect_LM.csh

     endif
    endif
    if ($ima_mos2 == "$ExpDetectPrep/ExpDetect_MOS2_LE_noise_unfiltered_image_"$b_tot"_"$start"-"$stop".fits" && $ima_pn == "$ExpDetectPrep/ExpDetectPrep_PN_merged_image_"$b_tot"_"$start"-"$stop".fits") then
     set filterM2=`ftlist ${ima_mos2}+0 K | grep "FILTER" | sed -e "s/'/ /g"  | cut -d' ' -f5 | awk '{print $1}' | cut -c "1-4"`
     set filterPN=`ftlist ${ima_pn}+0 K | grep "FILTER" | sed -e "s/'/ /g"  | cut -d' ' -f5 | awk '{print $1}' | cut -c "1-4"`
     if (${N_evtM2} == ${N_EN_BAND} && ${N_evtPN} == ${N_EN_BAND}) then
      echo "Performing eboxdetect LM with MOS2 pn for all energy bands"
      if (${filterM2} == "Thin") then
        set CF_M2=`awk '{printf("%.4f \n", $1)}' $det/cf2.txt | perl -ne 'print unless $.<2 '`
      endif
      if (${filterPN} == "Thin") then
        set CF_PN=`awk '{printf("%.4f \n", $1)}' $det/cf3.txt | perl -ne 'print unless $.<2 '`
      endif
      if (${filterM2} == "Medi") then
        set CF_M2=`awk '{printf("%.4f \n", $2)}' $det/cf2.txt | perl -ne 'print unless $.<2 '`
      endif
      if (${filterPN} == "Medi") then
        set CF_PN=`awk '{printf("%.4f \n", $2)}' $det/cf3.txt | perl -ne 'print unless $.<2 '`
      endif
      if (${filterM2} == "Thic") then
        set CF_M2=`awk '{printf("%.4f \n", $3)}' $det/cf2.txt | perl -ne 'print unless $.<2 '`
      endif
      if (${filterPN} == "Thic") then
        set CF_PN=`awk '{printf("%.4f \n", $3)}' $det/cf3.txt | perl -ne 'print unless $.<2 '`
      endif
      #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 '`
      echo "/usr/bin/time -p eboxdetect usemap=no likemin=$likemin_lb boxsize=$boxsize_l nruns=1 withdetmask=yes detmasksets='$mask1 $mask2' imagesets='$ima3 $ima2' expimagesets='$expo3 $expo2' ecf='$CF_PN $CF_M2' pimin='$bmin $bmin' pimax='$bmax $bmax' boxlistset=eboxlist_local_"$start"-"$stop".fits usematchedfilter=yes -w "$warn" -V "$verb"" >> $an_dir/eboxdetect_LM.csh
     endif
     if (${N_evtM2} < ${N_EN_BAND} || ${N_evtPN} < ${N_EN_BAND}) then
      echo "Performing eboxdetect LM with MOS2 pn for total energy band"
      if (${filterM2} == "Thin") then
        set CF_M2=`awk -v nr=$b_tot '{if (NR==nr) printf("%.4f \n", $1)}' $det/cf2.txt`
      endif
      if (${filterPN} == "Thin") then
        set CF_PN=`awk -v nr=$b_tot '{if (NR==nr) printf("%.4f \n", $1)}' $det/cf3.txt`
      endif
      if (${filterM2} == "Medi") then
        set CF_M2=`awk -v nr=$b_tot '{if (NR==nr) printf("%.4f \n", $2)}' $det/cf2.txt`
      endif
      if (${filterPN} == "Medi") then
        set CF_PN=`awk -v nr=$b_tot '{if (NR==nr) printf("%.4f \n", $2)}' $det/cf3.txt`
      endif
      if (${filterM2} == "Thic") then
        set CF_M2=`awk -v nr=$b_tot '{if (NR==nr) printf("%.4f \n", $3)}' $det/cf2.txt`
      endif
      if (${filterPN} == "Thic") then
        set CF_PN=`awk -v nr=$b_tot '{if (NR==nr) printf("%.4f \n", $3)}' $det/cf3.txt`
      endif
      #set CF=`awk -v nr=$b_tot '{ if (NR==nr) printf("%.4f \n", $3)}' $det/cf3.txt && awk -v nr=$b_tot '{ if (NR==nr) printf("%.4f \n", $3)}' $det/cf2.txt`
      echo "/usr/bin/time -p eboxdetect usemap=no likemin=$likemin_lb boxsize=$boxsize_l nruns=1 withdetmask=yes detmasksets=' $mask1 $mask2' imagesets='$ima_pn $ima_mos2' expimagesets='$expo_pn $expo_mos2' ecf='$CF_PN $CF_M2' pimin='$bmin_tot $bmin_tot' pimax='$bmax_tot $bmax_tot' boxlistset=eboxlist_local_"$start"-"$stop".fits usematchedfilter=yes -w "$warn" -V "$verb"" >> $an_dir/eboxdetect_LM.csh
     endif
    endif
   endif 
   if ($ima_btot == 1) then
    if ($ima_mos1 == "$ExpDetectPrep/ExpDetect_MOS1_LE_noise_unfiltered_image_"$b_tot"_"$start"-"$stop".fits") then
     set filterM1=`ftlist ${ima_mos1}+0 K | grep "FILTER" | sed -e "s/'/ /g"  | cut -d' ' -f5 | awk '{print $1}' | cut -c "1-4"`
     if (${N_evtM1} == ${N_EN_BAND}) then
      echo "Performing eboxdetect LM with MOS1 for all energy bands"
      if (${filterM1} == "Thin") then
        set CF_M1=`awk '{printf("%.4f \n", $1)}' $det/cf1.txt | perl -ne 'print unless $.<2 '`
      endif
      if (${filterM1} == "Medi") then
        set CF_M1=`awk '{printf("%.4f \n", $2)}' $det/cf1.txt | perl -ne 'print unless $.<2 '`
      endif
      if (${filterM1} == "Thic") then
        set CF_M1=`awk '{printf("%.4f \n", $3)}' $det/cf1.txt | perl -ne 'print unless $.<2 '`
      endif
      #set CF=`awk '{printf("%.4f \n", $3)}' $det/cf1.txt | perl -ne 'print unless $.<2 '`
      echo "/usr/bin/time -p eboxdetect usemap=no likemin=$likemin_lb boxsize=$boxsize_l nruns=1 withdetmask=yes detmasksets='$mask2' imagesets='$ima1' expimagesets='$expo1' ecf='$CF_M1' pimin='$bmin' pimax='$bmax' boxlistset=eboxlist_local_"$start"-"$stop".fits usematchedfilter=yes -w 1 -V "$verb"" >> $an_dir/eboxdetect_LM.csh
     endif
     if (${N_evtM1} < ${N_EN_BAND}) then
      echo "Performing eboxdetect LM with MOS1 for total energy band"
      if (${filterM1} == "Thin") then
        set CF_M1=`awk -v nr=$b_tot '{if (NR==nr) printf("%.4f \n", $1)}' $det/cf1.txt`
      endif
      if (${filterM1} == "Medi") then
        set CF_M1=`awk -v nr=$b_tot '{if (NR==nr) printf("%.4f \n", $2)}' $det/cf1.txt`
      endif
      if (${filterM1} == "Thic") then
        set CF_M1=`awk -v nr=$b_tot '{if (NR==nr) printf("%.4f \n", $3)}' $det/cf1.txt`
      endif
      #set CF=`awk -v nr=$b_tot '{ if (NR==nr) printf("%.4f \n", $3)}' $det/cf1.txt`
      echo "/usr/bin/time -p eboxdetect usemap=no likemin=$likemin_lb boxsize=$boxsize_l nruns=1 withdetmask=yes detmasksets='$mask2' imagesets='$ima_mos1' expimagesets='$expo_mos1' ecf='$CF_M1' pimin='$bmin_tot' pimax='$bmax_tot' boxlistset=eboxlist_local_"$start"-"$stop".fits usematchedfilter=yes -w "$warn" -V "$verb"" >> $an_dir/eboxdetect_LM.csh
     endif
    endif
    if ($ima_mos2 == "$ExpDetectPrep/ExpDetect_MOS2_LE_noise_unfiltered_image_"$b_tot"_"$start"-"$stop".fits") then
     set filterM2=`ftlist ${ima_mos2}+0 K | grep "FILTER" | sed -e "s/'/ /g"  | cut -d' ' -f5 | awk '{print $1}' | cut -c "1-4"`
     if (${N_evtM2} == ${N_EN_BAND}) then
      echo "Performing eboxdetect LM with MOS2 for all energy bands"
      if (${filterM2} == "Thin") then
        set CF_M2=`awk '{printf("%.4f \n", $1)}' $det/cf2.txt | perl -ne 'print unless $.<2 '`
      endif
      if (${filterM2} == "Medi") then
        set CF_M2=`awk '{printf("%.4f \n", $2)}' $det/cf2.txt | perl -ne 'print unless $.<2 '`
      endif
      if (${filterM2} == "Thic") then
        set CF_M2=`awk '{printf("%.4f \n", $3)}' $det/cf2.txt | perl -ne 'print unless $.<2 '`
      endif
      #set CF=`awk '{printf("%.4f \n", $3)}' $det/cf2.txt | perl -ne 'print unless $.<2 '`
      echo "/usr/bin/time -p eboxdetect usemap=no likemin=$likemin_lb boxsize=$boxsize_l nruns=1 withdetmask=yes detmasksets='$mask2' imagesets='$ima2' expimagesets='$expo2' ecf='$CF_M2' pimin='$bmin' pimax='$bmax' boxlistset=eboxlist_local_"$start"-"$stop".fits usematchedfilter=yes -w 1 -V "$verb"" >> $an_dir/eboxdetect_LM.csh
     endif
     if (${N_evtM2} < ${N_EN_BAND}) then
      echo "Performing eboxdetect LM with MOS2 for total energy band"
      if (${filterM2} == "Thin") then
        set CF_M2=`awk -v nr=$b_tot '{if (NR==nr) printf("%.4f \n", $1)}' $det/cf2.txt`
      endif
      if (${filterM2} == "Medi") then
        set CF_M2=`awk -v nr=$b_tot '{if (NR==nr) printf("%.4f \n", $2)}' $det/cf2.txt`
      endif
      if (${filterM2} == "Thic") then
        set CF_M2=`awk -v nr=$b_tot '{if (NR==nr) printf("%.4f \n", $3)}' $det/cf2.txt`
      endif
      #set CF=`awk -v nr=$b_tot '{ if (NR==nr) printf("%.4f \n", $3)}' $det/cf2.txt`
      echo "/usr/bin/time -p eboxdetect usemap=no likemin=$likemin_lb boxsize=$boxsize_l nruns=1 withdetmask=yes detmasksets='$mask2' imagesets='$ima_mos2' expimagesets='$expo_mos2' ecf='$CF_M2' pimin='$bmin_tot' pimax='$bmax_tot' boxlistset=eboxlist_local_"$start"-"$stop".fits usematchedfilter=yes -w "$warn" -V "$verb"" >> $an_dir/eboxdetect_LM.csh
     endif
    endif
    if ($ima_pn == "$ExpDetectPrep/ExpDetectPrep_PN_merged_image_"$b_tot"_"$start"-"$stop".fits") then
    set filterPN=`ftlist ${ima_pn}+0 K | grep "FILTER" | sed -e "s/'/ /g"  | cut -d' ' -f5 | awk '{print $1}' | cut -c "1-4"`
     if (${N_evtPN} == ${N_EN_BAND}) then
      echo "Performing eboxdetect LM with pn for all energy bands"
      if (${filterPN} == "Thin") then
        set CF_PN=`awk '{printf("%.4f \n", $1)}' $det/cf3.txt | perl -ne 'print unless $.<2 '`
      endif
      if (${filterPN} == "Medi") then
        set CF_PN=`awk '{printf("%.4f \n", $2)}' $det/cf3.txt | perl -ne 'print unless $.<2 '`
      endif
      if (${filterPN} == "Thic") then
        set CF_PN=`awk '{printf("%.4f \n", $3)}' $det/cf3.txt | perl -ne 'print unless $.<2 '`
      endif
      #set CF=`awk '{printf("%.4f \n", $3)}' $det/cf3.txt | perl -ne 'print unless $.<2 '`
      echo "/usr/bin/time -p eboxdetect usemap=no likemin=$likemin_lb boxsize=$boxsize_l nruns=1 withdetmask=yes detmasksets='$mask1' imagesets='$ima3' expimagesets='$expo3' ecf='$CF_PN' pimin='$bmin' pimax='$bmax' boxlistset=eboxlist_local_"$start"-"$stop".fits usematchedfilter=yes -w 1 -V "$verb"" >> $an_dir/eboxdetect_LM.csh
     endif
     if (${N_evtPN} < ${N_EN_BAND}) then
      echo "Performing eboxdetect LM with pn for total energy band"
      if (${filterPN} == "Thin") then
        set CF_PN=`awk -v nr=$b_tot '{if (NR==nr) printf("%.4f \n", $1)}' $det/cf3.txt`
      endif
      if (${filterPN} == "Medi") then
        set CF_PN=`awk -v nr=$b_tot '{if (NR==nr) printf("%.4f \n", $2)}' $det/cf3.txt`
      endif
      if (${filterPN} == "Thic") then
        set CF_PN=`awk -v nr=$b_tot '{if (NR==nr) printf("%.4f \n", $3)}' $det/cf3.txt`
      endif
      #set CF=`awk -v nr=$b_tot '{ if (NR==nr) printf("%.4f \n", $3)}' $det/cf3.txt`
      echo "/usr/bin/time -p eboxdetect usemap=no likemin=$likemin_lb boxsize=$boxsize_l nruns=1 withdetmask=yes detmasksets='$mask1' imagesets='$ima_pn' expimagesets='$expo_pn' ecf='$CF_PN' pimin='$bmin_tot' pimax='$bmax_tot' boxlistset=eboxlist_local_"$start"-"$stop".fits usematchedfilter=yes -w "$warn" -V "$verb"" >> $an_dir/eboxdetect_LM.csh
     endif
    endif
   endif 
   source $an_dir/eboxdetect_LM.csh
   rm $an_dir/eboxdetect_LM.csh
   echo "OK: eboxlist_local_"$start"-"$stop".fits done in energy band (eV) ["$bmin":"$bmax"]"

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

date
echo "stop eboxdetect_LM_bin.csh"
