#! /bin/tcsh -f

echo "*******************************"
echo "*  start select_time_bin.csh  *"
echo "*******************************"
date

cd $result_dir/
set proc = $result_dir/$dir0/processed
set det = $result_dir/$dir0/detection
set an_dir = $result_dir/$dir0/binT
if ( -r $an_dir ) then
 rm -rf $an_dir
 echo "old $an_dir removed"
 mkdir $an_dir
else
 mkdir $an_dir
endif
cd $an_dir/
if ( $DET == 01 ) then
  set list_evt="list_evt_${dir0}_MOS1.txt"
endif
if ( $DET == 02 ) then
  set list_evt="list_evt_${dir0}_MOS2.txt"
endif
if ( $DET == 03 ) then
  set list_evt="list_evt_${dir0}_PN.txt"
endif
if ( $DET == 1 ) then
  set list_evt="list_evt_${dir0}_MOS.txt"
endif
if ( $DET == 2 ) then
  set list_evt="list_evt_${dir0}.txt"
endif

set evt_list_filt="list_evt_filtered.txt"
echo "OBS $dir0"

set ctrl = `wc $det/$list_evt | awk '{print $1}'`
if ( $ctrl == 0) then
      echo "============================================================"
      echo "FATAL ERROR: no events files in $det"
      echo "============================================================"
      echo "FATAL ERROR: no events files in $det" >> $log_dir/messages.log
      break
else
      set Nflare = 1
      foreach evt (`cat $det/$list_evt`)
         set epic = `echo $evt | cut -c 18-21`
         set type = `echo $evt | cut -c 23-26`
             echo "#! /bin/tcsh -f" > $det/select.csh
             if ($filter1 == "SP") then
              echo "evt file SP cleaned"
              set flarefile=`awk -v nr=$Nflare '{ if (NR == nr) print $1}' $proc/list_GTI_Flare.txt`
              if ( -e "$data_dir"/"$dir0"/"$dir0"_GlobalHK-Attitude_GTI.fits) then
               echo "/usr/bin/time -p evselect expression='GTI("$data_dir"/"$dir0"/"$dir0"_GlobalHK-Attitude_GTI.fits, TIME) && GTI("$proc"/$flarefile,TIME)' filteredset="$an_dir"/"$dir0"_event_"$epic"_"$type".fits imagebinning=binSize imagedatatype=Int32 imageset='' keepfilteroutput=Y table="$det"/"$evt":EVENTS updateexposure=true withfilteredset=Y withimagedatatype=no withimageset=no writedss=true xcolumn=X ximagebinsize=80 ycolumn=Y yimagebinsize=80 -w "$warn" -V "$verb"" >> $det/select.csh
               echo "ATTENTION: The Soft Protons cleaned analysis is selected"
               echo "           It is filtered with Global HK Attitude GTI"
               echo "           It is filtered with GTI from EPIC flare background timeseries"
               echo "$dir0"_event_"$epic"_"$type".fits >> $an_dir/$evt_list_filt 
              else
               echo "/usr/bin/time -p evselect expression='GTI("$proc"/$flarefile,TIME)' filteredset="$an_dir"/"$dir0"_event_"$epic"_"$type".fits imagebinning=binSize imagedatatype=Int32 imageset='' keepfilteroutput=Y table="$det"/"$evt":EVENTS updateexposure=true withfilteredset=Y withimageset=no writedss=true xcolumn=X ximagebinsize=80 ycolumn=Y yimagebinsize=80 -w "$warn" -V "$verb"" >> $det/select.csh
               echo "ATTENTION: The Soft Protons cleaned analysis is selected"
               echo "           It is not filtered with Global HK Attitude GTI"
               echo "           It is filtered with GTI from EPIC flare background timeseries"
               echo "$dir0"_event_"$epic"_"$type".fits >> $an_dir/$evt_list_filt 
              endif
             endif
             if ($filter1 == "notSP") then
              echo "evt file not SP cleaned"
              if ( -e "$data_dir"/"$dir0"/"$dir0"_GlobalHK-Attitude_GTI.fits) then
               echo "/usr/bin/time -p evselect expression='GTI("$data_dir"/"$dir0"/"$dir0"_GlobalHK-Attitude_GTI.fits, TIME)' filteredset="$an_dir"/"$dir0"_event_"$epic"_"$type".fits imagebinning=binSize imagedatatype=Int32 imageset='' keepfilteroutput=Y table="$det"/"$evt":EVENTS updateexposure=true withfilteredset=Y withimagedatatype=no withimageset=no writedss=true xcolumn=X ximagebinsize=80 ycolumn=Y yimagebinsize=80 -w "$warn" -V "$verb"" >> $det/select.csh
               echo "ATTENTION: The Soft Protons cleaned analysis is not selected"
               echo "           It is filtered with Global HK Attitude GTI"
               echo "$dir0"_event_"$epic"_"$type".fits >> $an_dir/$evt_list_filt 
              else
               echo "cp "$det"/"$evt" "$an_dir"/"$dir0"_event_"$epic"_"$type".fits" >> $det/select.csh
               echo "ATTENTION: The Soft Protons cleaned analysis is not selected"
               echo "           It is not filtered with Global HK Attitude GTI"
               echo "$dir0"_event_"$epic"_"$type".fits >> $an_dir/$evt_list_filt 
              endif
             endif
             source $det/select.csh
             rm $det/select.csh
      set Nflare = ` expr $Nflare + 1`
      end

   set ctrl = `wc $an_dir/$evt_list_filt | awk '{print $1}'`
   if ( $ctrl == 0) then
      echo "============================================================"
      echo "FATAL ERROR: no events files in $an_dir"
      echo "============================================================"
      echo "FATAL ERROR: no events files in $an_dir" >> $log_dir/messages.log
   else
      foreach evfile (`cat $an_dir/$evt_list_filt`)
         set min=`fkeyprint $an_dir/$evfile+1 TSTART| grep '=' | cut -d' ' -f4| awk '{print int($1)}'`
         set max=`fkeyprint $an_dir/$evfile+1 TSTOP| grep '=' | cut -d' ' -f5| awk '{print int($1)}'`
         echo $min $max $evfile >> $an_dir/"$dir0"_start_stop_evt.txt
      end
         cat $an_dir/"$dir0"_start_stop_evt.txt | awk '{print $2}' | sort -nr | awk -v nr=1 '{ if (NR==nr) print $1}' > $an_dir/"$dir0"_ref_stop.txt
         cat $an_dir/"$dir0"_start_stop_evt.txt | awk '{print $1}' | sort -n | awk -v nr=1 '{ if (NR==nr) print $1}' > $an_dir/"$dir0"_ref_start.txt

   endif
   rm $an_dir/*.fits
endif
cd $start_dir/

date
echo "stop select_time_bin.csh"
