#! /bin/tcsh -f

echo "***************************"
echo "*  start analysis_bin.csh *"
echo "***************************"
date

cd $result_dir/
set type_stringa = "binfixed"
set an_dir = $bin_dir/$dir0
set tim_dir=$result_dir/$dir0/binT
set cont="$tim_dir"/"$dir0"_start_bin.txt

if ($DET_SRC == 01) then
 set DET = 01
 set list_evt="list_evt_${dir0}_MOS1.txt"
 echo "ATTENTION: performing the source detection on time bins for the MOS1"
endif
if ($DET_SRC == 02) then
 set DET = 02
 set list_evt="list_evt_${dir0}_MOS2.txt"
 echo "ATTENTION: performing the source detection on time bins for the MOS2"
endif
if ($DET_SRC == 03) then
 set DET = 03
 set list_evt="list_evt_${dir0}_PN.txt"
 echo "ATTENTION: performing the source detection on time bins for the pn"
endif
if ($DET_SRC == 1) then
 set DET = 1
 set list_evt="list_evt_${dir0}_MOS.txt"
 echo "ATTENTION: performing the source detection on time bins for MOS1 and MOS2"
endif
if ($DET_SRC == 2) then
 set DET = 2
 echo "ATTENTION: performing the source detection on time bins for MOS1 MOS2 and pn"
endif

  if ( -e $cont ) then
    if ($DET== 1) then
     if (-e $det/cf1.txt && -e $det/cf2.txt) then
      echo "CF already calculated"
     else
#      source $progr_dir/CF.csh
    source $progr_dir/CF_test.csh >>& $log_dir/analysis.log
     endif
    endif
    if ($DET== 2) then
     if (-e $det/cf1.txt && -e $det/cf2.txt && -e $det/cf3.txt) then
      echo "CF already calculated"
     else
#      source $progr_dir/CF.csh
    source $progr_dir/CF_test.csh >>& $log_dir/analysis.log
     endif
    endif
    if ($DET== 01) then
     if (-e $det/cf1.txt) then
      echo "CF already calculated"
     else
#      source $progr_dir/CF.csh
    source $progr_dir/CF_test.csh >>& $log_dir/analysis.log
     endif
    endif
    if ($DET== 02) then
     if (-e $det/cf2.txt) then
      echo "CF already calculated"
     else
#      source $progr_dir/CF.csh
    source $progr_dir/CF_test.csh >>& $log_dir/analysis.log
     endif
    endif
    if ($DET== 03) then
     if (-e $det/cf3.txt) then
      echo "CF already calculated"
     else
#      source $progr_dir/CF.csh
    source $progr_dir/CF_test.csh >>& $log_dir/analysis.log
     endif
    endif
    set tb = 1 
    foreach T (`cat $tim_dir/"$dir0"_start_bin.txt`)
      set start=`awk -v nr=$tb '{ if (NR == nr) print $1}' $tim_dir/"$dir0"_start_bin.txt`
      set stop=`awk -v nr=$tb '{ if (NR == nr) print $1}' $tim_dir/"$dir0"_stop_bin.txt`
      echo "TIME BIN (N.$tb): $start - $stop"
      source $progr_dir/make_image_bin.csh
      if ($ctrl_empty_bin == 0) then
       source $progr_dir/make_expo_bin.csh
       source $progr_dir/ExpDetectPrep_weight_image_3XMM_bin.csh 
       source $progr_dir/ExpDetectPrep_merge_evt_list_3XMM_bin.csh 
       source $progr_dir/ExpDetect_detmask_3XMM_bin.csh 
       source $progr_dir/eboxdetect_LM_bin.csh
       source $progr_dir/esplinemap_LM_bin.csh
       source $progr_dir/eboxdetect_MM_bin.csh
       source $progr_dir/emldetect_bin.csh
       source $progr_dir/emosaic_bin.csh
       source $progr_dir/match_bin.csh
      else
        echo "WARNING: skip the time bin (N.$tb) $start - $stop analysis, the events files of all instruments are empty after the filtering"
        echo "WARNING: skip the time bin (N.$tb) $start - $stop analysis, the events files of all instruments are empty after the filtering" >> $log_dir/messages.log
      endif
  set tb = ` expr $tb + 1`
  end 
    source $progr_dir/merge_evt_list_3XMM.csh
    source $progr_dir/search_var_bin.csh
    source $progr_dir/cat_trans.csh
    if ($single_instrument == "yes") then
      source $progr_dir/cat_var.csh
    endif
    source $progr_dir/collect_results.csh $type_stringa
    source $progr_dir/collect_results_header.csh $type_stringa
  else
   echo "WARNING: $cont does not exist !"
   echo "Can not perform the time bins source detection "
  endif
date
echo "stop analysis_bin.csh"
