#! /bin/tcsh -f

echo "***********************************"
echo "*  start total_exposure_count.csh *"
echo "***********************************"
date

cd $result_dir/
set filter_dir = $result_dir/$dir0/filter
set filter_dir_closetosrc = $filter_dir/closetosrc
set det = $result_dir/$dir0/detection

if ($outtosrc == "yes") then
 cd $filter_dir/
endif

set ctrl_expo = 0
if ($type_baye_cat2 == "3xmm") then
 if ($outtosrc == "yes") then
   cp $xmmdac_dir/xtcheesemask.py $filter_dir/
   chmod 755 $filter_dir/xtcheesemask.py
  # set list_evt="list_evt_filtered_baye_cheese_3xmm.txt"
   set list_evt="list_ctrl_type_cheese.txt"
 endif

 if ($outtosrc == "yes") then
  cd $filter_dir/
  set ctrl = `wc $filter_dir/$list_evt | awk '{print $1}'`
  if ( $ctrl == 0) then
      echo "============================================================"
      echo "FATAL ERROR: no events files in $filter_dir"
      echo "============================================================"
      echo "FATAL ERROR: no events files in $filter_dir" >> $log_dir/messages.log
      break
  else
      echo ""
      echo "******************************"
      echo "* OUT TO SRC ANALYSIS (3XMM) * "
      echo "******************************"
      echo ""
      foreach evt (`cat $filter_dir/$list_evt`)
       fkeypar ${evt}+1 NAXIS2
       set evt_rows = `pget fkeypar.par value`
       if (${evt_rows} == 0) then
        set base_name_evt = `echo $evt | cut -d'.' -f1`
        echo "#RA Dec Tstart Tstop Probability" > ${base_name_evt}_res.txt
        set epic_new = `echo $evt | cut -c 9-10`
        set epic_fin = `echo $evt | cut -c 11-12`
        set epic = `echo $evt | cut -c 9-12`
        set type = `echo $evt | cut -c 14-17`
        set ccd_n = `echo $evt | cut -d'.' -f1 | cut -d'_' -f7`
        if (${epic_new} == "PN") then
          echo " PN QUADRANT: ${ccd_n}"
        else
         if (${epic_fin} == "S1") then
          set epic_new = "M1" 
          echo " MOS1 CCD: ${ccd_n}"
         endif
         if (${epic_fin} == "S2") then
          set epic_new = "M2" 
          echo " MOS2 CCD: ${ccd_n}"
         endif
        endif
        echo "WARNING: the event file ${evt} is empty after filtering"
        echo "Skipping the analysis of the event file ${evt} with xtdac.py task..."
        set ctrl_expo = 1
       else
       set ctrl_PNexpo_cheese1 = 0 
       set ctrl_PNexpo_cheese2 = 0
       set ctrl_PNexpo_cheese3 = 0
       set ctrl_M1expo_cheese1 = 0
       set ctrl_M1expo_cheese2 = 0
       set ctrl_M1expo_cheese3 = 0
       set ctrl_M2expo_cheese1 = 0
       set ctrl_M2expo_cheese2 = 0
       set ctrl_M2expo_cheese3 = 0
       set epic_new = `echo $evt | cut -c 9-10`
       set epic_fin = `echo $evt | cut -c 11-12`
       set epic = `echo $evt | cut -c 9-12`
       set type = `echo $evt | cut -c 14-17`
       set base_name_evt = `echo $evt | cut -d'.' -f1`
       set ccd_n = `echo $evt | cut -d'.' -f1 | cut -d'_' -f7`
       set mask="${filter_dir}/${epic}200_12000_${type}_${ccd_n}.mask"
       if (${epic_new} == "PN") then
        echo " PN QUADRANT: ${ccd_n}"
       else
        if (${epic_fin} == "S1") then
         set epic_new = "M1" 
         echo " MOS1 CCD: ${ccd_n}"
        endif
        if (${epic_fin} == "S2") then
         set epic_new = "M2" 
         echo " MOS2 CCD: ${ccd_n}"
        endif
       endif
       set expo_expo0="$det/${dir0}_${epic}_exponovign_1.fits"
       if ( -e ${expo_expo0} ) then
          echo "Starting to perform the exposure maps analysis..."
          set filtered=$evt
          set emin=`echo $filtered | grep "$type" | cut -d'_' -f3 | cut -d' ' -f1`
          set emax=`echo $filtered | grep "$type" | cut -d'_' -f4 | cut -d' ' -f1`
          set nband_baye = `cat $result_dir/$dir0/energy_band_bayesian.txt | wc -l`
          if ($emin == 200 && $emax == 12000) then
            echo "ENERGY BAND (eV): ${emin} - ${emax}"
            set expo_expo0="$det/${dir0}_${epic}_exponovign_1.fits"
            set expo_mask1="${dir0}${epic_new}${type}EXPMAP_1.fits"
            set expo_mask="${dir0}${epic_new}${type}EXPMAP_1_${ccd_n}.fits"
            set expo_mask_cheese="${dir0}${epic_new}${type}EXPMAP_1_cheese_${ccd_n}.fits"
            if (-e $expo_expo0) then
             if (-e $expo_mask) then
              echo "$expo_mask already done"
              set ctrl_expo = 1
             else
               cp $expo_expo0 $expo_mask1
             # gunzip -c $expo_expo0 > $expo_mask1
              if (-e $expo_mask1) then
               ftimgcalc a=A=${expo_mask1} b=B=${mask} expr="(A * B)" outfile=${expo_mask}
              endif
              set ctrl_expo = 1
             endif
            else
             echo "WARNING: ${expo_expo0} does not exist !"
             set ctrl_expo = 0
            endif
             if ($epic_new == "PN" && $ctrl_PNexpo_cheese1 == 0) then
             if ( -e ${expo_expo0}) then
              echo "Making exposure map cheesed in the energy band $emin - $emax eV for pn instrument..."
              set ctrl_PNexpo_cheese1 = 1
              if ($ambient == "cluster") then
               echo "#\!/bin/bash" > ${filter_dir}/xtcheesemask.csh
               echo "/usr/bin/time -p xtcheesemask.py -i $expo_mask --no-reverse -r ${filtered} -o $expo_mask_cheese -v $info" >> ${filter_dir}/xtcheesemask.csh
               chmod 755 ${filter_dir}/xtcheesemask.csh
               source ${filter_dir}/xtcheesemask.csh
               rm ${filter_dir}/xtcheesemask.csh
              endif
              if ($ambient == "mac") then
               echo "/usr/bin/time -p ./xtcheesemask.py -i $expo_mask --no-reverse -r ${filtered} -o $expo_mask_cheese -v $info" > ${filter_dir}/xtcheesemask.csh
               source ${filter_dir}/xtcheesemask.csh
               rm ${filter_dir}/xtcheesemask.csh
              endif
              cp $expo_expo0 $prod_start_dir/$dir0
              cp $expo_mask_cheese $prod_start_dir/$dir0
             endif
             endif
             if ($epic_new == "M1" && $ctrl_M1expo_cheese1 == 0) then
             if ( -e ${expo_expo0}) then
              echo "Making exposure map cheesed in the energy band $emin - $emax eV for MOS1 instrument..."
              set ctrl_M1expo_cheese1 = 1
              if ($ambient == "cluster") then
               echo "#\!/bin/bash" > ${filter_dir}/xtcheesemask.csh
               echo "/usr/bin/time -p xtcheesemask.py -i $expo_mask --no-reverse -r ${filtered} -o $expo_mask_cheese -v $info" >> ${filter_dir}/xtcheesemask.csh
               chmod 755 ${filter_dir}/xtcheesemask.csh
               source ${filter_dir}/xtcheesemask.csh
               rm ${filter_dir}/xtcheesemask.csh
              endif
              if ($ambient == "mac") then
               echo "/usr/bin/time -p xtcheesemask.py -i $expo_mask --no-reverse -r ${filtered} -o $expo_mask_cheese -v $info" > ${filter_dir}/xtcheesemask.csh
               source ${filter_dir}/xtcheesemask.csh
               rm ${filter_dir}/xtcheesemask.csh
              endif
              cp $expo_expo0 $prod_start_dir/$dir0
              cp $expo_mask_cheese $prod_start_dir/$dir0
             endif
             endif
             if ($epic_new == "M2" && $ctrl_M2expo_cheese1 == 0) then
             if ( -e ${expo_expo0}) then
              echo "Making exposure map cheesed in the energy band $emin - $emax eV for MOS2 instrument..."
              set ctrl_M2expo_cheese1 = 1
              if ($ambient == "cluster") then
               echo "#\!/bin/bash" > ${filter_dir}/xtcheesemask.csh
               echo "/usr/bin/time -p xtcheesemask.py -i $expo_mask --no-reverse -r ${filtered} -o $expo_mask_cheese -v $info" >> ${filter_dir}/xtcheesemask.csh
               chmod 755 ${filter_dir}/xtcheesemask.csh
               source ${filter_dir}/xtcheesemask.csh
               rm ${filter_dir}/xtcheesemask.csh
              endif
              if ($ambient == "mac") then
               echo "/usr/bin/time -p xtcheesemask.py -i $expo_mask --no-reverse -r ${filtered} -o $expo_mask_cheese -v $info" > ${filter_dir}/xtcheesemask.csh
               source ${filter_dir}/xtcheesemask.csh
               rm ${filter_dir}/xtcheesemask.csh
              endif
              cp $expo_expo0 $prod_start_dir/$dir0
              cp $expo_mask_cheese $prod_start_dir/$dir0
             endif
             endif
          endif
          if ($emin == 500 && $emax == 2000) then
            echo "ENERGY BAND (eV): ${emin} - ${emax}"
            set nband1 = 2000
            set nband2 = 3000
            set expo_expo1="${pps_archive_dir}"/"${dir0}"/pps/"P${dir0}${epic_new}${type}EXPMAP${nband1}.FTZ"
            set expo_expo2="${pps_archive_dir}"/"${dir0}"/pps/"P${dir0}${epic_new}${type}EXPMAP${nband2}.FTZ"
            set expo_expo1new="P${dir0}${epic_new}${type}EXPMAP${nband1}.fits"
            set expo_expo2new="P${dir0}${epic_new}${type}EXPMAP${nband2}.fits"
            set expo_mask1="${dir0}${epic_new}${type}EXPMAP_2.fits"
            set expo_mask="${dir0}${epic_new}${type}EXPMAP_2_${ccd_n}.fits"
            set expo_mask_cheese="${dir0}${epic_new}${type}EXPMAP_2_cheese_${ccd_n}.fits"
            if (-e $expo_expo1 && -e $expo_expo2) then
             if (-e $expo_mask) then
              echo "$expo_mask already done"
              set ctrl_expo = 1
             else
              gunzip -c $expo_expo1 > $expo_expo1new
              gunzip -c $expo_expo2 > $expo_expo2new
              echo "imweightadd imagesets=' ${expo_expo1new} ${expo_expo2new} ' outimageset=${expo_mask1} tempset=temporary.fits withweights=no -w "$warn" -V "$verb"" > imweightadd.csh
              source imweightadd.csh
              rm imweightadd.csh
              rm $expo_expo1new
              rm $expo_expo2new
              if (-e $expo_mask1) then
               ftimgcalc a=A=${expo_mask1} b=B=${mask} expr="(A * B)" outfile=${expo_mask}
              endif
              set ctrl_expo = 1
             endif
            else
             echo "WARNING: ${expo_expo1} and ${expo_expo2} do not exist !"
             set ctrl_expo = 0
            endif
             if ($epic_new == "PN" && $ctrl_PNexpo_cheese2 == 0) then
             if ( -e ${expo_expo1} && -e ${expo_expo2}) then
              echo "Making exposure map cheesed in the energy band $emin - $emax eV for pn instrument..."
              set ctrl_PNexpo_cheese2 = 1
              if ($ambient == "cluster") then
               echo "#\!/bin/bash" > ${filter_dir}/xtcheesemask.csh
               echo "/usr/bin/time -p xtcheesemask.py -i $expo_mask --no-reverse -r ${filtered} -o $expo_mask_cheese -v $info" >> ${filter_dir}/xtcheesemask.csh
               chmod 755 ${filter_dir}/xtcheesemask.csh
               source ${filter_dir}/xtcheesemask.csh
               rm ${filter_dir}/xtcheesemask.csh
              endif
              if ($ambient == "mac") then
               echo "/usr/bin/time -p xtcheesemask.py -i $expo_mask --no-reverse -r ${filtered} -o $expo_mask_cheese -v $info" > ${filter_dir}/xtcheesemask.csh
               source ${filter_dir}/xtcheesemask.csh
               rm ${filter_dir}/xtcheesemask.csh
              endif
             endif
             endif
             if ($epic_new == "M1" && $ctrl_M1expo_cheese2 == 0) then
             if ( -e ${expo_expo1} && -e ${expo_expo2}) then
              echo "Making exposure map cheesed in the energy band $emin - $emax eV for MOS1 instrument..."
              set ctrl_M1expo_cheese2 = 1
              if ($ambient == "cluster") then
               echo "#\!/bin/bash" > ${filter_dir}/xtcheesemask.csh
               echo "/usr/bin/time -p xtcheesemask.py -i $expo_mask --no-reverse -r ${filtered} -o $expo_mask_cheese -v $info" >> ${filter_dir}/xtcheesemask.csh
               chmod 755 ${filter_dir}/xtcheesemask.csh
               source ${filter_dir}/xtcheesemask.csh
               rm ${filter_dir}/xtcheesemask.csh
              endif
              if ($ambient == "mac") then
               echo "/usr/bin/time -p xtcheesemask.py -i $expo_mask --no-reverse -r ${filtered} -o $expo_mask_cheese -v $info" > ${filter_dir}/xtcheesemask.csh
               source ${filter_dir}/xtcheesemask.csh
               rm ${filter_dir}/xtcheesemask.csh
              endif
             endif
             endif
             if ($epic_new == "M2" && $ctrl_M2expo_cheese2 == 0) then
             if ( -e ${expo_expo1} && -e ${expo_expo2}) then
              echo "Making exposure map cheesed in the energy band $emin - $emax eV for MOS2 instrument..."
              set ctrl_M2expo_cheese2 = 1
              if ($ambient == "cluster") then
               echo "#\!/bin/bash" > ${filter_dir}/xtcheesemask.csh
               echo "/usr/bin/time -p xtcheesemask.py -i $expo_mask --no-reverse -r ${filtered} -o $expo_mask_cheese -v $info" >> ${filter_dir}/xtcheesemask.csh
               chmod 755 ${filter_dir}/xtcheesemask.csh
               source ${filter_dir}/xtcheesemask.csh
               rm ${filter_dir}/xtcheesemask.csh
              endif
              if ($ambient == "mac") then
               echo "/usr/bin/time -p xtcheesemask.py -i $expo_mask --no-reverse -r ${filtered} -o $expo_mask_cheese -v $info" > ${filter_dir}/xtcheesemask.csh
               source ${filter_dir}/xtcheesemask.csh
               rm ${filter_dir}/xtcheesemask.csh
              endif
             endif
             endif
          endif
          if ($emin == 2000 && $emax == 10000) then
            echo "ENERGY BAND (eV): ${emin} - ${emax}"
            set nband = 4000
            set expo_expo3="${pps_archive_dir}"/"${dir0}"/pps/"P${dir0}${epic_new}${type}EXPMAP${nband}.FTZ"
            set expo_mask1="${dir0}${epic_new}${type}EXPMAP_3.fits"
            set expo_mask="${dir0}${epic_new}${type}EXPMAP_3_${ccd_n}.fits"
            set expo_mask_cheese="${dir0}${epic_new}${type}EXPMAP_3_cheese_${ccd_n}.fits"
            if (-e $expo_expo3) then
             if (-e $expo_mask) then
              echo "$expo_mask already done"
              set ctrl_expo = 1
             else
              gunzip -c $expo_expo3 > $expo_mask1
              if (-e $expo_mask1) then
               ftimgcalc a=A=${expo_mask1} b=B=${mask} expr="(A * B)" outfile=${expo_mask}
              endif
              set ctrl_expo = 1
             endif
            else
             echo "WARNING: ${expo_expo3} does not exist !"
             set ctrl_expo = 0
            endif
             if ($epic_new == "PN" && $ctrl_PNexpo_cheese3 == 0) then
             if ( -e ${expo_expo3}) then
              echo "Making exposure map cheesed in the energy band $emin - $emax eV for pn instrument..."
              set ctrl_PNexpo_cheese3 = 1
              if ($ambient == "cluster") then
               echo "#\!/bin/bash" > ${filter_dir}/xtcheesemask.csh
               echo "/usr/bin/time -p xtcheesemask.py -i $expo_mask --no-reverse -r ${filtered} -o $expo_mask_cheese -v $info" >> ${filter_dir}/xtcheesemask.csh
               chmod 755 ${filter_dir}/xtcheesemask.csh
               source ${filter_dir}/xtcheesemask.csh
               rm ${filter_dir}/xtcheesemask.csh
              endif
              if ($ambient == "mac") then
               echo "/usr/bin/time -p xtcheesemask.py -i $expo_mask --no-reverse -r ${filtered} -o $expo_mask_cheese -v $info" > ${filter_dir}/xtcheesemask.csh
               source ${filter_dir}/xtcheesemask.csh
               rm ${filter_dir}/xtcheesemask.csh
              endif
             endif
             endif
             if ($epic_new == "M1" && $ctrl_M1expo_cheese3 == 0) then
             if ( -e ${expo_expo3}) then
              echo "Making exposure map cheesed in the energy band $emin - $emax eV for MOS1 instrument..."
              set ctrl_M1expo_cheese3 = 1
              if ($ambient == "cluster") then
               echo "#\!/bin/bash" > ${filter_dir}/xtcheesemask.csh
               echo "/usr/bin/time -p xtcheesemask.py -i $expo_mask --no-reverse -r ${filtered} -o $expo_mask_cheese -v $info" >> ${filter_dir}/xtcheesemask.csh
               chmod 755 ${filter_dir}/xtcheesemask.csh
               source ${filter_dir}/xtcheesemask.csh
               rm ${filter_dir}/xtcheesemask.csh
              endif
              if ($ambient == "mac") then
               echo "/usr/bin/time -p xtcheesemask.py -i $expo_mask --no-reverse -r ${filtered} -o $expo_mask_cheese -v $info" > ${filter_dir}/xtcheesemask.csh
               source ${filter_dir}/xtcheesemask.csh
               rm ${filter_dir}/xtcheesemask.csh
              endif
             endif
             endif
             if ($epic_new == "M2" && $ctrl_M2expo_cheese3 == 0) then
             if ( -e ${expo_expo3}) then
              echo "Making exposure map cheesed in the energy band $emin - $emax eV for MOS2 instrument..."
              set ctrl_M2expo_cheese3 = 1
              if ($ambient == "cluster") then
               echo "#\!/bin/bash" > ${filter_dir}/xtcheesemask.csh
               echo "/usr/bin/time -p xtcheesemask.py -i $expo_mask --no-reverse -r ${filtered} -o $expo_mask_cheese -v $info" >> ${filter_dir}/xtcheesemask.csh
               chmod 755 ${filter_dir}/xtcheesemask.csh
               source ${filter_dir}/xtcheesemask.csh
               rm ${filter_dir}/xtcheesemask.csh
              endif
              if ($ambient == "mac") then
               echo "/usr/bin/time -p xtcheesemask.py -i $expo_mask --no-reverse -r ${filtered} -o $expo_mask_cheese -v $info" > ${filter_dir}/xtcheesemask.csh
               source ${filter_dir}/xtcheesemask.csh
               rm ${filter_dir}/xtcheesemask.csh
              endif
             endif
             endif
          endif
       else
        echo "WARNING: ${expo_expo0} does not exist. No PPS exposure maps are ready to use !"
        set ctrl_expo = 0
       endif
       endif
      end
  endif
 endif
 if ($closetosrc == "yes") then
  cd $filter_dir_closetosrc/
  set ctrl = `wc $filter_dir_closetosrc/$list_evt | awk '{print $1}'`
  if ( $ctrl == 0) then
      echo "============================================================"
      echo "FATAL ERROR: no events files in $filter_dir_closetosrc"
      echo "============================================================"
      echo "FATAL ERROR: no events files in $filter_dir_closetosrc" >> $log_dir/messages.log
      break
  else
      echo ""
      echo "********************************"
      echo "* CLOSE TO SRC ANALYSIS (3XMM) *"
      echo "********************************"
      echo ""
      foreach evt (`cat $filter_dir_closetosrc/$list_evt`)
       fkeypar ${evt}+1 NAXIS2
       set evt_rows = `pget fkeypar.par value`
       if (${evt_rows} == 0) then
        set base_name_evt = `echo $evt | cut -d'.' -f1`
        echo "#RA Dec Tstart Tstop Probability" > ${base_name_evt}_unique_res.txt
        set epic_new = `echo $evt | cut -c 9-10`
        set epic_fin = `echo $evt | cut -c 11-12`
        set epic = `echo $evt | cut -c 9-12`
        set type = `echo $evt | cut -c 14-17`
        set ccd_n = `echo $evt | cut -d'.' -f1 | cut -d'_' -f7`
        if (${epic_new} == "PN") then
          echo " PN QUADRANT: ${ccd_n}"
        else
         if (${epic_fin} == "S1") then
          set epic_new = "M1" 
          echo " MOS1 CCD: ${ccd_n}"
         endif
         if (${epic_fin} == "S2") then
          set epic_new = "M2" 
          echo " MOS2 CCD: ${ccd_n}"
         endif
        endif
        echo "WARNING: the event file ${evt} is empty after filtering"
        echo "Skipping the analysis of the event file ${evt} with xtfbs.py task..."
        set ctrl_expo = 1
       else
       set epic_new = `echo $evt | cut -c 9-10`
       set epic_fin = `echo $evt | cut -c 11-12`
       set epic = `echo $evt | cut -c 9-12`
       set type = `echo $evt | cut -c 14-17`
       set base_name_evt = `echo $evt | cut -d'.' -f1`
       set ccd_n = `echo $evt | cut -d'.' -f1 | cut -d'_' -f7`
       set mask="${filter_dir}/${epic}200_12000_${type}_${ccd_n}.mask"
       if (${epic_new} == "PN") then
         echo " PN QUADRANT: ${ccd_n}"
       else
        if (${epic_fin} == "S1") then
         set epic_new = "M1" 
         echo " MOS1 CCD: ${ccd_n}"
        endif
        if (${epic_fin} == "S2") then
         set epic_new = "M2" 
         echo " MOS2 CCD: ${ccd_n}"
        endif
       endif
       set nband_tot = 8000
       set expo_expo0="${pps_archive_dir}"/"${dir0}"/pps/"P${dir0}${epic_new}${type}EXPMAP${nband_tot}.FTZ"
       if ( -e ${expo_expo0} ) then
       echo "Starting to perform the analysis..."
          set filtered=$evt
          set emin=`echo $filtered | grep "$type" | cut -d'_' -f3 | cut -d' ' -f1`
          set emax=`echo $filtered | grep "$type" | cut -d'_' -f4 | cut -d' ' -f1`
          set nband_baye = `cat $result_dir/$dir0/energy_band_bayesian.txt | wc -l`
          if ($emin == 200 && $emax == 12000) then
            echo "ENERGY BAND (eV): ${emin} - ${emax}"
            set nband = 8000
            set expo_expo0="${pps_archive_dir}"/"${dir0}"/pps/"P${dir0}${epic_new}${type}EXPMAP${nband}.FTZ"
            set expo_mask1="${dir0}${epic_new}${type}EXPMAP_1.fits"
            set expo_mask="${dir0}${epic_new}${type}EXPMAP_1_${ccd_n}.fits"
            if (-e $expo_expo0) then
             if (-e $expo_mask) then
              echo "$expo_mask already done"
              set ctrl_expo = 1
             else
              gunzip -c $expo_expo0 > $expo_mask1
              if (-e $expo_mask1) then
               ftimgcalc a=A=${expo_mask1} b=B=${mask} expr="(A * B)" outfile=${expo_mask}
              endif
              set ctrl_expo = 1
             endif
            else
             echo "WARNING: ${expo_expo0} does not exist !"
             set ctrl_expo = 0
            endif
          endif
          if ($emin == 500 && $emax == 2000) then
            echo "ENERGY BAND (eV): ${emin} - ${emax}"
            set nband1 = 2000
            set nband2 = 3000
            set expo_expo1="${pps_archive_dir}"/"${dir0}"/pps/"P${dir0}${epic_new}${type}EXPMAP${nband1}.FTZ"
            set expo_expo2="${pps_archive_dir}"/"${dir0}"/pps/"P${dir0}${epic_new}${type}EXPMAP${nband2}.FTZ"
            set expo_expo1new="P${dir0}${epic_new}${type}EXPMAP${nband1}.fits"
            set expo_expo2new="P${dir0}${epic_new}${type}EXPMAP${nband2}.fits"
            set expo_mask1="${dir0}${epic_new}${type}EXPMAP_2.fits"
            set expo_mask="${dir0}${epic_new}${type}EXPMAP_2_${ccd_n}.fits"
            if (-e $expo_expo1 && -e $expo_expo2) then
             if (-e $expo_mask) then
              echo "$expo_mask already done"
              set ctrl_expo = 1
             else
              gunzip -c $expo_expo1 > $expo_expo1new
              gunzip -c $expo_expo2 > $expo_expo2new
              echo "imweightadd imagesets=' ${expo_expo1new} ${expo_expo2new} ' outimageset=${expo_mask1} tempset=temporary.fits withweights=no -w "$warn" -V "$verb"" > imweightadd.csh
              source imweightadd.csh
              rm imweightadd.csh
              rm $expo_expo1new
              rm $expo_expo2new
              if (-e $expo_mask1) then
               ftimgcalc a=A=${expo_mask1} b=B=${mask} expr="(A * B)" outfile=${expo_mask}
              endif
              set ctrl_expo = 1
             endif
            else
             echo "WARNING: ${expo_expo1} and ${expo_expo2} do not exist !"
             set ctrl_expo = 0
            endif
          endif
          if ($emin == 2000 && $emax == 10000) then
            echo "ENERGY BAND (eV): ${emin} - ${emax}"
            set nband = 4000
            set expo_expo3="${pps_archive_dir}"/"${dir0}"/pps/"P${dir0}${epic_new}${type}EXPMAP${nband}.FTZ"
            set expo_mask1="${dir0}${epic_new}${type}EXPMAP_3.fits"
            set expo_mask="${dir0}${epic_new}${type}EXPMAP_3_${ccd_n}.fits"
            if (-e $expo_expo3) then
             if (-e $expo_mask) then
              echo "$expo_mask already done"
              set ctrl_expo = 1
             else
              gunzip -c $expo_expo3 > $expo_mask1
              if (-e $expo_mask1) then
               ftimgcalc a=A=${expo_mask1} b=B=${mask} expr="(A * B)" outfile=${expo_mask}
              endif
              set ctrl_expo = 1
             endif
            else
             echo "WARNING: ${expo_expo3} does not exist !"
             set ctrl_expo = 0
            endif
          endif

          if ($ctrl_expo == 1) then 
           echo "Starting to perform the analysis on $filtered event file cheesed with 3XMM catalog and on exposure map $expo_mask"
           if ($ambient == "cluster") then
            echo "#\!/bin/bash" > ${filter_dir_closetosrc}/xtfbs.csh
            echo "/usr/bin/time -p xtfbs.py -t ${sftb} -s ${stss} -m ${msz} -e ${filter_dir_closetosrc}/${filtered} -x ${filter_dir_closetosrc}/${expo_mask} -v ${info}" >> ${filter_dir_closetosrc}/xtfbs.csh
            chmod 755 ${filter_dir_closetosrc}/xtfbs.csh
            source ${filter_dir_closetosrc}/xtfbs.csh 
            rm ${filter_dir_closetosrc}/xtfbs.csh
           endif
           if ($ambient == "mac") then
            echo "/usr/bin/time -p xtfbs.py -t ${sftb} -s ${stss} -m ${msz} -e ${filter_dir_closetosrc}/${filtered} -x ${filter_dir_closetosrc}/${expo_mask} -v ${info}" > ${filter_dir_closetosrc}/xtfbs.csh
            source ${filter_dir_closetosrc}/xtfbs.csh
            rm ${filter_dir_closetosrc}/xtfbs.csh
           endif
           if (-e ${base_name_evt}_unique.txt) then
            cp ${base_name_evt}_unique.txt ${base_name_evt}_unique_res.txt
            rm ${base_name_evt}_unique.txt 
           endif
          else
           echo "WARNING: exposure maps $expo_expo0 $expo_expo1 $expo_expo2 $expo_expo3 do not exist or the event file ${evt} is empty !"
          endif
       else
        echo "WARNING: ${expo_expo0} does not exist. No PPS exposure maps are ready to use !"
        set ctrl_expo = 0
       endif
       endif
      end
  endif
 endif
endif
cd $start_dir/




set ctrl_expo = 0
if ($type_baye_cat1 == "full") then
 if ($outtosrc == "yes") then
   cp $xmmdac_dir/xtdac.py $filter_dir/
   chmod 755 $filter_dir/xtdac.py
   cp $xmmdac_dir/xtcheesemask.py $filter_dir/
   chmod 755 $filter_dir/xtcheesemask.py
   cp $xmmdac_dir/xtfixtimestamps.py $filter_dir/
   chmod 755 $filter_dir/xtfixtimestamps.py
   set list_evt="list_evt_filtered_baye_cheese.txt"
 endif
 if ($closetosrc == "yes") then
   cp $xmmdac_dir/xtfbs.py $filter_dir_closetosrc/
   chmod 755 $filter_dir_closetosrc/xtfbs.py
   set list_evt="list_evt_filtered_baye_cheese.txt"
 endif

 if ($outtosrc == "yes") then
  cd $filter_dir/
  set ctrl = `wc $filter_dir/$list_evt | awk '{print $1}'`
  if ( $ctrl == 0) then
      echo "============================================================"
      echo "FATAL ERROR: no events files in $filter_dir"
      echo "============================================================"
      echo "FATAL ERROR: no events files in $filter_dir" >> $log_dir/messages.log
      break
  else
      echo ""
      echo "******************************"
      echo "* OUT TO SRC ANALYSIS (FULL) *"
      echo "******************************"
      echo ""
      foreach evt (`cat $filter_dir/$list_evt`)
       fkeypar ${evt}+1 NAXIS2
       set evt_rows = `pget fkeypar.par value`
       if (${evt_rows} == 0) then
        set base_name_evt = `echo $evt | cut -d'.' -f1`
        echo "#RA Dec Tstart Tstop Probability" > ${base_name_evt}_res.txt
        set epic_new = `echo $evt | cut -c 9-10`
        set epic_fin = `echo $evt | cut -c 11-12`
        set epic = `echo $evt | cut -c 9-12`
        set type = `echo $evt | cut -c 14-17`
        set ccd_n = `echo $evt | cut -d'.' -f1 | cut -d'_' -f6`
        if (${epic_new} == "PN") then
          echo " PN QUADRANT: ${ccd_n}"
        else
         if (${epic_fin} == "S1") then
          set epic_new = "M1" 
          echo " MOS1 CCD: ${ccd_n}"
         endif
         if (${epic_fin} == "S2") then
          set epic_new = "M2" 
          echo " MOS2 CCD: ${ccd_n}"
         endif
        endif
        echo "WARNING: the event file ${evt} is empty after filtering"
        echo "Skipping the analysis of the event file ${evt} with xtdac.py task..."
        set ctrl_expo = 1
       else
       set ctrl_PNexpo_cheese1 = 0 
       set ctrl_PNexpo_cheese2 = 0
       set ctrl_PNexpo_cheese3 = 0
       set ctrl_M1expo_cheese1 = 0
       set ctrl_M1expo_cheese2 = 0
       set ctrl_M1expo_cheese3 = 0
       set ctrl_M2expo_cheese1 = 0
       set ctrl_M2expo_cheese2 = 0
       set ctrl_M2expo_cheese3 = 0
       set epic_new = `echo $evt | cut -c 9-10`
       set epic_fin = `echo $evt | cut -c 11-12`
       set epic = `echo $evt | cut -c 9-12`
       set type = `echo $evt | cut -c 14-17`
       set base_name_evt = `echo $evt | cut -d'.' -f1`
       set ccd_n = `echo $evt | cut -d'.' -f1 | cut -d'_' -f6`
       set mask="${filter_dir}/${epic}200_12000_${type}_${ccd_n}.mask"
       if (${epic_new} == "PN") then
        echo " PN QUADRANT: ${ccd_n}"
       else
        if (${epic_fin} == "S1") then
         set epic_new = "M1" 
         echo " MOS1 CCD: ${ccd_n}"
        endif
        if (${epic_fin} == "S2") then
         set epic_new = "M2" 
         echo " MOS2 CCD: ${ccd_n}"
        endif
       endif
       set nband_tot = 8000
       set expo_expo0="${pps_archive_dir}"/"${dir0}"/pps/"P${dir0}${epic_new}${type}EXPMAP${nband_tot}.FTZ"
       if ( -e ${expo_expo0} ) then
          echo "Starting to perform the bayesian blocks analysis..."
          set filtered=$evt
          set emin=`echo $filtered | grep "$type" | cut -d'_' -f3 | cut -d' ' -f1`
          set emax=`echo $filtered | grep "$type" | cut -d'_' -f4 | cut -d' ' -f1`
          set nband_baye = `cat $result_dir/$dir0/energy_band_bayesian.txt | wc -l`
          if ($emin == 200 && $emax == 12000) then
            echo "ENERGY BAND (eV): ${emin} - ${emax}"
            set nband = 8000
            set expo_expo0="${pps_archive_dir}"/"${dir0}"/pps/"P${dir0}${epic_new}${type}EXPMAP${nband}.FTZ"
            set expo_mask1="${dir0}${epic_new}${type}EXPMAP_1.fits"
            set expo_mask="${dir0}${epic_new}${type}EXPMAP_1_${ccd_n}.fits"
            set expo_mask_cheese="${dir0}${epic_new}${type}EXPMAP_1_cheese_${ccd_n}.fits"
            if (-e $expo_expo0) then
             if (-e $expo_mask) then
              echo "$expo_mask already done"
              set ctrl_expo = 1
             else
              gunzip -c $expo_expo0 > $expo_mask1
              if (-e $expo_mask1) then
               ftimgcalc a=A=${expo_mask1} b=B=${mask} expr="(A * B)" outfile=${expo_mask}
              endif
              set ctrl_expo = 1
             endif
            else
             echo "WARNING: ${expo_expo0} does not exist !"
             set ctrl_expo = 0
            endif
             if ($epic_new == "PN" && $ctrl_PNexpo_cheese1 == 0) then
             if ( -e ${expo_expo0}) then
              echo "Making exposure map cheesed in the energy band $emin - $emax eV for pn instrument..."
              set ctrl_PNexpo_cheese1 = 1
              if ($ambient == "cluster") then
               echo "#\!/bin/bash" > ${filter_dir}/xtcheesemask.csh
               echo "/usr/bin/time -p xtcheesemask.py -i $expo_mask --no-reverse -r ${filtered} -o $expo_mask_cheese -v $info" >> ${filter_dir}/xtcheesemask.csh
               chmod 755 ${filter_dir}/xtcheesemask.csh
               source ${filter_dir}/xtcheesemask.csh
               rm ${filter_dir}/xtcheesemask.csh
              endif
              if ($ambient == "mac") then
               echo "/usr/bin/time -p xtcheesemask.py -i $expo_mask --no-reverse -r ${filtered} -o $expo_mask_cheese -v $info" > ${filter_dir}/xtcheesemask.csh
               source ${filter_dir}/xtcheesemask.csh
               rm ${filter_dir}/xtcheesemask.csh
              endif
             endif
             endif
             if ($epic_new == "M1" && $ctrl_M1expo_cheese1 == 0) then
             if ( -e ${expo_expo0}) then
              echo "Making exposure map cheesed in the energy band $emin - $emax eV for MOS1 instrument..."
              set ctrl_M1expo_cheese1 = 1
              if ($ambient == "cluster") then
               echo "#\!/bin/bash" > ${filter_dir}/xtcheesemask.csh
               echo "/usr/bin/time -p xtcheesemask.py -i $expo_mask --no-reverse -r ${filtered} -o $expo_mask_cheese -v $info" >> ${filter_dir}/xtcheesemask.csh
               chmod 755 ${filter_dir}/xtcheesemask.csh
               source ${filter_dir}/xtcheesemask.csh
               rm ${filter_dir}/xtcheesemask.csh
              endif
              if ($ambient == "mac") then
               echo "/usr/bin/time -p xtcheesemask.py -i $expo_mask --no-reverse -r ${filtered} -o $expo_mask_cheese -v $info" > ${filter_dir}/xtcheesemask.csh
               source ${filter_dir}/xtcheesemask.csh
               rm ${filter_dir}/xtcheesemask.csh
              endif
             endif
             endif
             if ($epic_new == "M2" && $ctrl_M2expo_cheese1 == 0) then
             if ( -e ${expo_expo0}) then
              echo "Making exposure map cheesed in the energy band $emin - $emax eV for MOS2 instrument..."
              set ctrl_M2expo_cheese1 = 1
              if ($ambient == "cluster") then
               echo "#\!/bin/bash" > ${filter_dir}/xtcheesemask.csh
               echo "/usr/bin/time -p xtcheesemask.py -i $expo_mask --no-reverse -r ${filtered} -o $expo_mask_cheese -v $info" >> ${filter_dir}/xtcheesemask.csh
               chmod 755 ${filter_dir}/xtcheesemask.csh
               source ${filter_dir}/xtcheesemask.csh
               rm ${filter_dir}/xtcheesemask.csh
              endif
              if ($ambient == "mac") then
               echo "/usr/bin/time -p xtcheesemask.py -i $expo_mask --no-reverse -r ${filtered} -o $expo_mask_cheese -v $info" > ${filter_dir}/xtcheesemask.csh
               source ${filter_dir}/xtcheesemask.csh
               rm ${filter_dir}/xtcheesemask.csh
              endif
             endif
             endif
          endif
          if ($emin == 500 && $emax == 2000) then
            echo "ENERGY BAND (eV): ${emin} - ${emax}"
            set nband1 = 2000
            set nband2 = 3000
            set expo_expo1="${pps_archive_dir}"/"${dir0}"/pps/"P${dir0}${epic_new}${type}EXPMAP${nband1}.FTZ"
            set expo_expo2="${pps_archive_dir}"/"${dir0}"/pps/"P${dir0}${epic_new}${type}EXPMAP${nband2}.FTZ"
            set expo_expo1new="P${dir0}${epic_new}${type}EXPMAP${nband1}.fits"
            set expo_expo2new="P${dir0}${epic_new}${type}EXPMAP${nband2}.fits"
            set expo_mask1="${dir0}${epic_new}${type}EXPMAP_2.fits"
            set expo_mask="${dir0}${epic_new}${type}EXPMAP_2_${ccd_n}.fits"
            set expo_mask_cheese="${dir0}${epic_new}${type}EXPMAP_2_cheese_${ccd_n}.fits"
            if (-e $expo_expo1 && -e $expo_expo2) then
             if (-e $expo_mask) then
              echo "$expo_mask already done"
              set ctrl_expo = 1
             else
              gunzip -c $expo_expo1 > $expo_expo1new
              gunzip -c $expo_expo2 > $expo_expo2new
              echo "imweightadd imagesets=' ${expo_expo1new} ${expo_expo2new} ' outimageset=${expo_mask1} tempset=temporary.fits withweights=no -w "$warn" -V "$verb"" > imweightadd.csh
              source imweightadd.csh
              rm imweightadd.csh
              rm $expo_expo1new
              rm $expo_expo2new
              if (-e $expo_mask1) then
               ftimgcalc a=A=${expo_mask1} b=B=${mask} expr="(A * B)" outfile=${expo_mask}
              endif
              set ctrl_expo = 1
             endif
            else
             echo "WARNING: ${expo_expo1} and ${expo_expo2} do not exist !"
             set ctrl_expo = 0
            endif
             if ($epic_new == "PN" && $ctrl_PNexpo_cheese2 == 0) then
             if ( -e ${expo_expo1} && -e ${expo_expo2}) then
              echo "Making exposure map cheesed in the energy band $emin - $emax eV for pn instrument..."
              set ctrl_PNexpo_cheese2 = 1
              if ($ambient == "cluster") then
               echo "#\!/bin/bash" > ${filter_dir}/xtcheesemask.csh
               echo "/usr/bin/time -p xtcheesemask.py -i $expo_mask --no-reverse -r ${filtered} -o $expo_mask_cheese -v $info" >> ${filter_dir}/xtcheesemask.csh
               chmod 755 ${filter_dir}/xtcheesemask.csh
               source ${filter_dir}/xtcheesemask.csh
               rm ${filter_dir}/xtcheesemask.csh
              endif
              if ($ambient == "mac") then
               echo "/usr/bin/time -p xtcheesemask.py -i $expo_mask --no-reverse -r ${filtered} -o $expo_mask_cheese -v $info" > ${filter_dir}/xtcheesemask.csh
               source ${filter_dir}/xtcheesemask.csh
               rm ${filter_dir}/xtcheesemask.csh
              endif
             endif
             endif
             if ($epic_new == "M1" && $ctrl_M1expo_cheese2 == 0) then
             if ( -e ${expo_expo1} && -e ${expo_expo2}) then
              echo "Making exposure map cheesed in the energy band $emin - $emax eV for MOS1 instrument..."
              set ctrl_M1expo_cheese2 = 1
              if ($ambient == "cluster") then
               echo "#\!/bin/bash" > ${filter_dir}/xtcheesemask.csh
               echo "/usr/bin/time -p xtcheesemask.py -i $expo_mask --no-reverse -r ${filtered} -o $expo_mask_cheese -v $info" >> ${filter_dir}/xtcheesemask.csh
               chmod 755 ${filter_dir}/xtcheesemask.csh
               source ${filter_dir}/xtcheesemask.csh
               rm ${filter_dir}/xtcheesemask.csh
              endif
              if ($ambient == "mac") then
               echo "/usr/bin/time -p xtcheesemask.py -i $expo_mask --no-reverse -r ${filtered} -o $expo_mask_cheese -v $info" > ${filter_dir}/xtcheesemask.csh
               source ${filter_dir}/xtcheesemask.csh
               rm ${filter_dir}/xtcheesemask.csh
              endif
             endif
             endif
             if ($epic_new == "M2" && $ctrl_M2expo_cheese2 == 0) then
             if ( -e ${expo_expo1} && -e ${expo_expo2}) then
              echo "Making exposure map cheesed in the energy band $emin - $emax eV for MOS2 instrument..."
              set ctrl_M2expo_cheese2 = 1
              if ($ambient == "cluster") then
               echo "#\!/bin/bash" > ${filter_dir}/xtcheesemask.csh
               echo "/usr/bin/time -p xtcheesemask.py -i $expo_mask --no-reverse -r ${filtered} -o $expo_mask_cheese -v $info" >> ${filter_dir}/xtcheesemask.csh
               chmod 755 ${filter_dir}/xtcheesemask.csh
               source ${filter_dir}/xtcheesemask.csh
               rm ${filter_dir}/xtcheesemask.csh
              endif
              if ($ambient == "mac") then
               echo "/usr/bin/time -p xtcheesemask.py -i $expo_mask --no-reverse -r ${filtered} -o $expo_mask_cheese -v $info" > ${filter_dir}/xtcheesemask.csh
               source ${filter_dir}/xtcheesemask.csh
               rm ${filter_dir}/xtcheesemask.csh
              endif
             endif
             endif
          endif
          if ($emin == 2000 && $emax == 10000) then
            echo "ENERGY BAND (eV): ${emin} - ${emax}"
            set nband = 4000
            set expo_expo3="${pps_archive_dir}"/"${dir0}"/pps/"P${dir0}${epic_new}${type}EXPMAP${nband}.FTZ"
            set expo_mask1="${dir0}${epic_new}${type}EXPMAP_3.fits"
            set expo_mask="${dir0}${epic_new}${type}EXPMAP_3_${ccd_n}.fits"
            set expo_mask_cheese="${dir0}${epic_new}${type}EXPMAP_3_cheese_${ccd_n}.fits"
            if (-e $expo_expo3) then
             if (-e $expo_mask) then
              echo "$expo_mask already done"
              set ctrl_expo = 1
             else
              gunzip -c $expo_expo3 > $expo_mask1
              if (-e $expo_mask1) then
               ftimgcalc a=A=${expo_mask1} b=B=${mask} expr="(A * B)" outfile=${expo_mask}
              endif
              set ctrl_expo = 1
             endif
            else
             echo "WARNING: ${expo_expo3} does not exist !"
             set ctrl_expo = 0
            endif
             if ($epic_new == "PN" && $ctrl_PNexpo_cheese3 == 0) then
             if ( -e ${expo_expo3}) then
              echo "Making exposure map cheesed in the energy band $emin - $emax eV for pn instrument..."
              set ctrl_PNexpo_cheese3 = 1
              if ($ambient == "cluster") then
               echo "#\!/bin/bash" > ${filter_dir}/xtcheesemask.csh
               echo "/usr/bin/time -p xtcheesemask.py -i $expo_mask --no-reverse -r ${filtered} -o $expo_mask_cheese -v $info" >> ${filter_dir}/xtcheesemask.csh
               chmod 755 ${filter_dir}/xtcheesemask.csh
               source ${filter_dir}/xtcheesemask.csh
               rm ${filter_dir}/xtcheesemask.csh
              endif
              if ($ambient == "mac") then
               echo "/usr/bin/time -p xtcheesemask.py -i $expo_mask --no-reverse -r ${filtered} -o $expo_mask_cheese -v $info" > ${filter_dir}/xtcheesemask.csh
               source ${filter_dir}/xtcheesemask.csh
               rm ${filter_dir}/xtcheesemask.csh
              endif
             endif
             endif
             if ($epic_new == "M1" && $ctrl_M1expo_cheese3 == 0) then
             if ( -e ${expo_expo3}) then
              echo "Making exposure map cheesed in the energy band $emin - $emax eV for MOS1 instrument..."
              set ctrl_M1expo_cheese3 = 1
              if ($ambient == "cluster") then
               echo "#\!/bin/bash" > ${filter_dir}/xtcheesemask.csh
               echo "/usr/bin/time -p xtcheesemask.py -i $expo_mask --no-reverse -r ${filtered} -o $expo_mask_cheese -v $info" >> ${filter_dir}/xtcheesemask.csh
               chmod 755 ${filter_dir}/xtcheesemask.csh
               source ${filter_dir}/xtcheesemask.csh
               rm ${filter_dir}/xtcheesemask.csh
              endif
              if ($ambient == "mac") then
               echo "/usr/bin/time -p xtcheesemask.py -i $expo_mask --no-reverse -r ${filtered} -o $expo_mask_cheese -v $info" > ${filter_dir}/xtcheesemask.csh
               source ${filter_dir}/xtcheesemask.csh
               rm ${filter_dir}/xtcheesemask.csh
              endif
             endif
             endif
             if ($epic_new == "M2" && $ctrl_M2expo_cheese3 == 0) then
             if ( -e ${expo_expo3}) then
              echo "Making exposure map cheesed in the energy band $emin - $emax eV for MOS2 instrument..."
              set ctrl_M2expo_cheese3 = 1
              if ($ambient == "cluster") then
               echo "#\!/bin/bash" > ${filter_dir}/xtcheesemask.csh
               echo "/usr/bin/time -p xtcheesemask.py -i $expo_mask --no-reverse -r ${filtered} -o $expo_mask_cheese -v $info" >> ${filter_dir}/xtcheesemask.csh
               chmod 755 ${filter_dir}/xtcheesemask.csh
               source ${filter_dir}/xtcheesemask.csh
               rm ${filter_dir}/xtcheesemask.csh
              endif
              if ($ambient == "mac") then
               echo "/usr/bin/time -p xtcheesemask.py -i $expo_mask --no-reverse -r ${filtered} -o $expo_mask_cheese -v $info" > ${filter_dir}/xtcheesemask.csh
               source ${filter_dir}/xtcheesemask.csh
               rm ${filter_dir}/xtcheesemask.csh
              endif
             endif
             endif
          endif

          if ($ctrl_expo == 1) then 
           echo "Starting to perform the bayesian blocks analysis on $filtered event file cheesed with FULL catalog and on exposure map $expo_mask_cheese"
           if ($ambient == "cluster") then
            echo "#\!/bin/bash" > ${filter_dir}/xtdac.csh
            echo "/usr/bin/time -p python xtfixtimestamps.py --eventfile ${filter_dir}/${filtered} --verbosity $info" >> ${filter_dir}/xtdac.csh
            echo "/usr/bin/time -p python xtdac.py --eventfile ${filter_dir}/${filtered} -x $expo_mask_cheese --ncpus $ncpu --typeIerror $Ierror -s $stss_out --regionsize $regionsize -m $multiplicity --writeRegionFiles $writeReg --verbosity $info --no-html" >> ${filter_dir}/xtdac.csh
            chmod 755 ${filter_dir}/xtdac.csh
            source ${filter_dir}/xtdac.csh 
            rm ${filter_dir}/xtdac.csh
           endif
           if ($ambient == "mac") then
            echo "/usr/bin/time -p python xtfixtimestamps.py --eventfile ${filter_dir}/${filtered} --verbosity $info" >> ${filter_dir}/xtdac.csh
            echo "/usr/bin/time -p python xtdac.py --eventfile ${filter_dir}/${filtered} -x $expo_mask_cheese --ncpus $ncpu --typeIerror $Ierror -s $stss_out --regionsize $regionsize -m $multiplicity --writeRegionFiles $writeReg --verbosity $info --no-html" >> ${filter_dir}/xtdac.csh
            source ${filter_dir}/xtdac.csh
            rm ${filter_dir}/xtdac.csh
           endif
          else
           echo "WARNING: PPS exposure maps do not exist or the event file ${evt} is empty !"
          endif
       else
        echo "WARNING: ${expo_expo0} does not exist. No PPS exposure maps are ready to use !"
        set ctrl_expo = 0
       endif
       endif
      end
  endif
 endif
 if ($closetosrc == "yes") then
  cd $filter_dir_closetosrc/
  set ctrl = `wc $filter_dir_closetosrc/$list_evt | awk '{print $1}'`
  if ( $ctrl == 0) then
      echo "============================================================"
      echo "FATAL ERROR: no events files in $filter_dir_closetosrc"
      echo "============================================================"
      echo "FATAL ERROR: no events files in $filter_dir_closetosrc" >> $log_dir/messages.log
      break
  else
      echo ""
      echo "********************************"
      echo "* CLOSE TO SRC ANALYSIS (FULL) *"
      echo "********************************"
      echo ""
      foreach evt (`cat $filter_dir_closetosrc/$list_evt`)
       fkeypar ${evt}+1 NAXIS2
       set evt_rows = `pget fkeypar.par value`
       if (${evt_rows} == 0) then
        set base_name_evt = `echo $evt | cut -d'.' -f1`
        echo "#RA Dec Tstart Tstop Probability" > ${base_name_evt}_unique_res.txt
        set epic_new = `echo $evt | cut -c 9-10`
        set epic_fin = `echo $evt | cut -c 11-12`
        set epic = `echo $evt | cut -c 9-12`
        set type = `echo $evt | cut -c 14-17`
        set ccd_n = `echo $evt | cut -d'.' -f1 | cut -d'_' -f6`
        if (${epic_new} == "PN") then
          echo " PN QUADRANT: ${ccd_n}"
        else
         if (${epic_fin} == "S1") then
          set epic_new = "M1" 
          echo " MOS1 CCD: ${ccd_n}"
         endif
         if (${epic_fin} == "S2") then
          set epic_new = "M2" 
          echo " MOS2 CCD: ${ccd_n}"
         endif
        endif
        echo "WARNING: the event file ${evt} is empty after filtering"
        echo "Skipping the analysis of the event file ${evt} with xtfbs.py task..."
        set ctrl_expo = 1
       else
       set epic_new = `echo $evt | cut -c 9-10`
       set epic_fin = `echo $evt | cut -c 11-12`
       set epic = `echo $evt | cut -c 9-12`
       set type = `echo $evt | cut -c 14-17`
       set base_name_evt = `echo $evt | cut -d'.' -f1`
       set ccd_n = `echo $evt | cut -d'.' -f1 | cut -d'_' -f6`
       set mask="${filter_dir}/${epic}200_12000_${type}_${ccd_n}.mask"
       echo $mask
       if (${epic_new} == "PN") then
         echo " PN QUADRANT: ${ccd_n}"
       else
        if (${epic_fin} == "S1") then
         set epic_new = "M1" 
         echo " MOS1 CCD: ${ccd_n}"
        endif
        if (${epic_fin} == "S2") then
         set epic_new = "M2" 
         echo " MOS2 CCD: ${ccd_n}"
        endif
       endif
       set nband_tot = 8000
       set expo_expo0="${pps_archive_dir}"/"${dir0}"/pps/"P${dir0}${epic_new}${type}EXPMAP${nband_tot}.FTZ"
       if ( -e ${expo_expo0} ) then
       echo "Starting to perform the analysis..."
          set filtered=$evt
          set emin=`echo $filtered | grep "$type" | cut -d'_' -f3 | cut -d' ' -f1`
          set emax=`echo $filtered | grep "$type" | cut -d'_' -f4 | cut -d' ' -f1`
          set nband_baye = `cat $result_dir/$dir0/energy_band_bayesian.txt | wc -l`
          if ($emin == 200 && $emax == 12000) then
            echo "ENERGY BAND (eV): ${emin} - ${emax}"
            set nband = 8000
            set expo_expo0="${pps_archive_dir}"/"${dir0}"/pps/"P${dir0}${epic_new}${type}EXPMAP${nband}.FTZ"
            set expo_mask1="${dir0}${epic_new}${type}EXPMAP_1.fits"
            set expo_mask="${dir0}${epic_new}${type}EXPMAP_1_${ccd_n}.fits"
            if (-e $expo_expo0) then
             if (-e $expo_mask) then
              echo "$expo_mask already done"
              set ctrl_expo = 1
             else
              gunzip -c $expo_expo0 > $expo_mask1
              if (-e $expo_mask1) then
               ftimgcalc a=A=${expo_mask1} b=B=${mask} expr="(A * B)" outfile=${expo_mask}
              endif
              set ctrl_expo = 1
             endif
            else
             echo "WARNING: ${expo_expo0} does not exist !"
             set ctrl_expo = 0
            endif
          endif
          if ($emin == 500 && $emax == 2000) then
            echo "ENERGY BAND (eV): ${emin} - ${emax}"
            set nband1 = 2000
            set nband2 = 3000
            set expo_expo1="${pps_archive_dir}"/"${dir0}"/pps/"P${dir0}${epic_new}${type}EXPMAP${nband1}.FTZ"
            set expo_expo2="${pps_archive_dir}"/"${dir0}"/pps/"P${dir0}${epic_new}${type}EXPMAP${nband2}.FTZ"
            set expo_expo1new="P${dir0}${epic_new}${type}EXPMAP${nband1}.fits"
            set expo_expo2new="P${dir0}${epic_new}${type}EXPMAP${nband2}.fits"
            set expo_mask1="${dir0}${epic_new}${type}EXPMAP_2.fits"
            set expo_mask="${dir0}${epic_new}${type}EXPMAP_2_${ccd_n}.fits"
            if (-e $expo_expo1 && -e $expo_expo2) then
             if (-e $expo_mask) then
              echo "$expo_mask already done"
              set ctrl_expo = 1
             else
              gunzip -c $expo_expo1 > $expo_expo1new
              gunzip -c $expo_expo2 > $expo_expo2new
              echo "imweightadd imagesets=' ${expo_expo1new} ${expo_expo2new} ' outimageset=${expo_mask1} tempset=temporary.fits withweights=no -w "$warn" -V "$verb"" > imweightadd.csh
              source imweightadd.csh
              rm imweightadd.csh
              rm $expo_expo1new
              rm $expo_expo2new
              if (-e $expo_mask1) then
               ftimgcalc a=A=${expo_mask1} b=B=${mask} expr="(A * B)" outfile=${expo_mask}
              endif
              set ctrl_expo = 1
             endif
            else
             echo "WARNING: ${expo_expo1} and ${expo_expo2} do not exist !"
             set ctrl_expo = 0
            endif
          endif
          if ($emin == 2000 && $emax == 10000) then
            echo "ENERGY BAND (eV): ${emin} - ${emax}"
            set nband = 4000
            set expo_expo3="${pps_archive_dir}"/"${dir0}"/pps/"P${dir0}${epic_new}${type}EXPMAP${nband}.FTZ"
            set expo_mask1="${dir0}${epic_new}${type}EXPMAP_3.fits"
            set expo_mask="${dir0}${epic_new}${type}EXPMAP_3_${ccd_n}.fits"
            if (-e $expo_expo3) then
             if (-e $expo_mask) then
              echo "$expo_mask already done"
              set ctrl_expo = 1
             else
              gunzip -c $expo_expo3 > $expo_mask1
              if (-e $expo_mask1) then
               ftimgcalc a=A=${expo_mask1} b=B=${mask} expr="(A * B)" outfile=${expo_mask}
              endif
              set ctrl_expo = 1
             endif
            else
             echo "WARNING: ${expo_expo3} does not exist !"
             set ctrl_expo = 0
            endif
          endif

          if ($ctrl_expo == 1) then 
           echo "Starting to perform the analysis on $filtered event file cheesed with FULL catalog and on exposure map $expo_mask"
           if ($ambient == "cluster") then
            echo "#\!/bin/bash" > ${filter_dir_closetosrc}/xtfbs.csh
            echo "/usr/bin/time -p xtfbs.py -t ${sftb} -s ${stss} -m ${msz} -e ${filter_dir_closetosrc}/${filtered} -x ${filter_dir_closetosrc}/${expo_mask} -v ${info}" >> ${filter_dir_closetosrc}/xtfbs.csh
            chmod 755 ${filter_dir_closetosrc}/xtfbs.csh
            source ${filter_dir_closetosrc}/xtfbs.csh 
            rm ${filter_dir_closetosrc}/xtfbs.csh
           endif
           if ($ambient == "mac") then
            echo "/usr/bin/time -p xtfbs.py -t ${sftb} -s ${stss} -m ${msz} -e ${filter_dir_closetosrc}/${filtered} -x ${filter_dir_closetosrc}/${expo_mask} -v ${info}" > ${filter_dir_closetosrc}/xtfbs.csh
            source ${filter_dir_closetosrc}/xtfbs.csh
            rm ${filter_dir_closetosrc}/xtfbs.csh
           endif
           if (-e ${base_name_evt}_unique.txt) then
            cp ${base_name_evt}_unique.txt ${base_name_evt}_unique_res.txt
            rm ${base_name_evt}_unique.txt 
           endif
          else
           echo "WARNING: exposure maps $expo_expo0 $expo_expo1 $expo_expo2 $expo_expo3 do not exist or the event file ${evt} is empty !"
          endif
       else
        echo "WARNING: ${expo_expo0} does not exist. No PPS exposure maps are ready to use !"
        set ctrl_expo = 0
       endif
       endif
      end
  endif
 endif
endif
cd $start_dir/

date
echo "stop total_exposure_count.csh"
