#! /bin/tcsh -f

echo "************************"
echo "*  GTI_flare_3XMM.csh  *"
echo "************************"
date

cd $result_dir/
cd $dir0/processed/
echo "Extract gti from EPIC flare background time series"
echo "The Soft Protons cleaned analysis is selected"

#####DET=1
if ( $DET == 1 ) then
   setenv SAS_ODF $data_dir/$dir0
   setenv SAS_CCF $data_dir/$dir0/ccf.cif
   
   echo OBS $dir0 MOS1 S
     rm list_GTI_Flare_MOS1.txt
     rm list_GTI_Flare_MOS.txt
     rm list_GTI_Flare.txt
     rm list_GTI_Flare_MOS2.txt
     rm rate_GTI_Flare_MOS1.txt
     rm rate_GTI_Flare_MOS2.txt

   set ds = `ls *EMOS1_S*_FBKTSR.fits`
   set Nds=1
   foreach mos1 ( $ds )
   echo "$mos1"
   set oss1 = ` echo $mos1 | cut -c 1-15`
   set s1 = ` echo $mos1 | cut -c 23-26`
   set mos1S = ` echo "$oss1"_EMOS1_"$s1"_FBKTSR.fits`
   if ( -e $result_dir/$dir0/processed/$mos1S ) then
     echo "MOS1: Start to make "$mos1" S GTI" 
     set rate_m1=`ftlist $mos1S[1] K | grep "FLCUTTHR=" | awk '{print $2}'`
     tabgtigen expression="RATE <= "$rate_m1"" gtiset="$oss1"_EMOS1_Optimised_flare_GTI_"$s1".fits mingtisize=100 table=$mos1S
     set gti_m1="$oss1"_EMOS1_Optimised_flare_GTI_"$s1".fits
     echo $rate_m1 >> rate_GTI_Flare_MOS1.txt
     echo $gti_m1 >> list_GTI_Flare_MOS1.txt
     echo $gti_m1 >> list_GTI_Flare_MOS.txt
     echo $gti_m1 >> list_GTI_Flare.txt
     chmod 766 "$oss1"_EMOS1_Optimised_flare_GTI_"$s1".fits
     echo "MOS1: "$gti_m1" S GTI made"
     if ($barycentr == "yes") then
      set gtiflare="${gti_m1}"
      source $progr_dir/barycenter_flare.csh
     endif
   else
     echo ""$mos1S" is not here: the GTI "$gti_m1" is not done"
   endif
   set Nds = ` expr $Nds + 1`
   end

   echo OBS $dir0 MOS2 S

   set ds = `ls *EMOS2_S*_FBKTSR.fits`
   set Nds=1
   foreach mos2 ( $ds )
   echo "$mos2"
   set oss2 = ` echo $mos2 | cut -c 1-15`
   set s2 = ` echo $mos2 | cut -c 23-26`
   set mos2S = ` echo "$oss2"_EMOS2_"$s2"_FBKTSR.fits`
   if ( -e $result_dir/$dir0/processed/$mos2S ) then
     echo "MOS2: Start to make "$mos2" S GTI" 
     set rate_m2=`ftlist $mos2S[1] K | grep "FLCUTTHR=" | awk '{print $2}'`
     tabgtigen expression="RATE <= "$rate_m2"" gtiset="$oss2"_EMOS2_Optimised_flare_GTI_"$s2".fits mingtisize=100 table=$mos2S
     set gti_m2="$oss2"_EMOS2_Optimised_flare_GTI_"$s2".fits
     echo $rate_m2 >> rate_GTI_Flare_MOS2.txt
     echo $gti_m2 >> list_GTI_Flare_MOS2.txt
     echo $gti_m2 >> list_GTI_Flare_MOS.txt
     echo $gti_m2 >> list_GTI_Flare.txt
     chmod 766 "$oss2"_EMOS2_Optimised_flare_GTI_"$s2".fits
     echo "MOS2: "$gti_m2" S GTI made"
     if ($barycentr == "yes") then
      set gtiflare="${gti_m2}"
      source $progr_dir/barycenter_flare.csh
     endif
   else
     echo ""$mos2S" is not here: the GTI "$gti_m2" is not done"
   endif
   set Nds = ` expr $Nds + 1`
   end

   echo OBS $dir0 MOS1 U 

   set ds = `ls *EMOS1_U*_FBKTSR.fits`
   set Nds=1
   foreach Umos1 ( $ds )
   echo "$Umos1"
   set oss1U = ` echo $Umos1 | cut -c 1-15`
   set u1 = ` echo $Umos1 | cut -c 23-26`
   set mos1U = ` echo "$oss1U"_EMOS1_"$u1"_FBKTSR.fits`
   if ( -e $result_dir/$dir0/processed/$mos1U ) then
     echo "MOS1: Start to make "$mos1U" U GTI" 
     set rate_m1=`ftlist $mos1U[1] K | grep "FLCUTTHR=" | awk '{print $2}'`
     tabgtigen expression="RATE <= "$rate_m1"" gtiset="$oss1U"_EMOS1_Optimised_flare_GTI_"$u1".fits mingtisize=100 table=$mos1U
     set gti_m1="$oss1U"_EMOS1_Optimised_flare_GTI_"$u1".fits
     echo $rate_m1 >> rate_GTI_Flare_MOS1.txt
     echo $gti_m1 >> list_GTI_Flare_MOS1.txt
     echo $gti_m1 >> list_GTI_Flare_MOS.txt
     echo $gti_m1 >> list_GTI_Flare.txt
     chmod 766 "$oss1U"_EMOS1_Optimised_flare_GTI_"$u1".fits
     echo "MOS1: "$gti_m1" U GTI made"
     if ($barycentr == "yes") then
      set gtiflare="${gti_m1}"
      source $progr_dir/barycenter_flare.csh
     endif
   else
     echo ""$mos1U" is not here: the GTI "$gti_m1" is not done"
   endif
   set Nds = ` expr $Nds + 1`
   end

   echo OBS $dir0 MOS2 U 

   set ds = `ls *EMOS2_U*_FBKTSR.fits`
   set Nds=1
   foreach Umos2 ( $ds )
   echo "$Umos2"
   set oss2U = ` echo $Umos2 | cut -c 1-15`
   set u2 = ` echo $Umos2 | cut -c 23-26`
   set mos2U = ` echo "$oss2U"_EMOS2_"$u2"_FBKTSR.fits`
   if ( -e $result_dir/$dir0/processed/$mos2U ) then
     echo "MOS2: Start to make "$mos2U" U GTI" 
     set rate_m2=`ftlist $mos2U[1] K | grep "FLCUTTHR=" | awk '{print $2}'`
     tabgtigen expression="RATE <= "$rate_m2"" gtiset="$oss2U"_EMOS2_Optimised_flare_GTI_"$u2".fits mingtisize=100 table=$mos2U
     set gti_m2="$oss2U"_EMOS2_Optimised_flare_GTI_"$u2".fits
     echo $rate_m2 >> rate_GTI_Flare_MOS2.txt
     echo $gti_m2 >> list_GTI_Flare_MOS2.txt
     echo $gti_m2 >> list_GTI_Flare_MOS.txt
     echo $gti_m2 >> list_GTI_Flare.txt
     chmod 766 "$oss2U"_EMOS2_Optimised_flare_GTI_"$u2".fits
     echo "MOS2: "$gti_m2" U GTI made"
     if ($barycentr == "yes") then
      set gtiflare="${gti_m2}"
      source $progr_dir/barycenter_flare.csh
     endif
   else
     echo ""$mos2U" is not here: the GTI "$gti_m2" is not done"
   endif
   set Nds = ` expr $Nds + 1`
   end
endif #DET=1

#####DET=01
if ( $DET == 01 ) then
   setenv SAS_ODF $data_dir/$dir0
   setenv SAS_CCF $data_dir/$dir0/ccf.cif

   echo OBS $dir0 MOS1 S
     rm list_GTI_Flare_MOS1.txt
     rm list_GTI_Flare.txt
     rm rate_GTI_Flare_MOS1.txt

   set ds = `ls *EMOS1_S*_FBKTSR.fits`
   set Nds=1
   foreach mos1 ( $ds )
   echo "$mos1"
   set oss1 = ` echo $mos1 | cut -c 1-15`
   set s1 = ` echo $mos1 | cut -c 23-26`
   set mos1S = ` echo "$oss1"_EMOS1_"$s1"_FBKTSR.fits`
   if ( -e $result_dir/$dir0/processed/$mos1S ) then
     echo "MOS1: Start to make "$mos1S" S GTI" 
     set rate_mos1=`ftlist $mos1S[1] K | grep "FLCUTTHR=" | awk '{print $2}'`
     tabgtigen expression="RATE <= "$rate_mos1"" gtiset="$oss1"_EMOS1_Optimised_flare_GTI_"$s1".fits mingtisize=100 table=$mos1S
     set gti_mos1="$oss1"_EMOS1_Optimised_flare_GTI_"$s1".fits
     echo $rate_mos1 >> rate_GTI_Flare_MOS1.txt
     echo $gti_mos1 >> list_GTI_Flare_MOS1.txt
     echo $gti_mos1 >> list_GTI_Flare.txt
     chmod 766 "$oss1"_EMOS1_Optimised_flare_GTI_"$s1".fits
     echo "MOS1: "$gti_mos1" S GTI made"
     if ($barycentr == "yes") then
      set gtiflare="${gti_m1}"
      source $progr_dir/barycenter_flare.csh
     endif
   else
     echo ""$mos1S" is not here: the GTI "$gti_mos1" is not done"
   endif
   set Nds = ` expr $Nds + 1`
   end

   echo OBS $dir0 MOS1 U
   
   set ds = `ls *EMOS1_U*_FBKTSR.fits`
   set Nds=1
   foreach mos1 ( $ds )
   echo "$mos1"
   set oss1 = ` echo $mos1 | cut -c 1-15`
   set u1 = ` echo $mos1 | cut -c 23-26`
   set mos1U = ` echo "$oss1"_EMOS1_"$u1"_FBKTSR.fits`
   if ( -e $result_dir/$dir0/processed/$mos1U ) then
     echo "MOS1: Start to make "$mos1U" U GTI" 
     set rate_mos1=`ftlist $mos1U[1] K | grep "FLCUTTHR=" | awk '{print $2}'`
     tabgtigen expression="RATE <= "$rate_mos1"" gtiset="$oss1"_EMOS1_Optimised_flare_GTI_"$u1".fits mingtisize=100 table=$mos1U
     set gti_mos1="$oss1"_EMOS1_Optimised_flare_GTI_"$u1".fits
     echo $rate_mos1 >> rate_GTI_Flare_MOS1.txt
     echo $gti_mos1 >> list_GTI_Flare_MOS1.txt
     echo $gti_mos1 >> list_GTI_Flare.txt
     chmod 766 "$oss1"_EMOS1_Optimised_flare_GTI_"$u1".fits
     echo "MOS1: "$gti_mos1" U GTI made"
     if ($barycentr == "yes") then
      set gtiflare="${gti_m1}"
      source $progr_dir/barycenter_flare.csh
     endif
   else
     echo ""$mos1U" is not here: the GTI "$gti_mos1" is not done"
   endif
   set Nds = ` expr $Nds + 1`
   end
endif #DET=01

#####DET=02
if ( $DET == 02 ) then
   setenv SAS_ODF $data_dir/$dir0
   setenv SAS_CCF $data_dir/$dir0/ccf.cif

   echo OBS $dir0 MOS2 S
     rm list_GTI_Flare_MOS2.txt
     rm list_GTI_Flare.txt
     rm rate_GTI_Flare_MOS2.txt

   set ds = `ls *EMOS2_S*_FBKTSR.fits`
   set Nds=1
   foreach mos2 ( $ds )
   echo "$mos2"
   set oss1 = ` echo $mos2 | cut -c 1-15`
   set s1 = ` echo $mos2 | cut -c 23-26`
   set mos2S = ` echo "$oss1"_EMOS2_"$s1"_FBKTSR.fits`
   if ( -e $result_dir/$dir0/processed/$mos2S ) then
     echo "MOS2: Start to make "$mos2S" S GTI" 
     set rate_mos2=`ftlist $mos2S[1] K | grep "FLCUTTHR=" | awk '{print $2}'`
     tabgtigen expression="RATE <= "$rate_mos2"" gtiset="$oss1"_EMOS2_Optimised_flare_GTI_"$s1".fits mingtisize=100 table=$mos2S
     set gti_mos2="$oss1"_EMOS2_Optimised_flare_GTI_"$s1".fits
     echo $rate_mos2 >> rate_GTI_Flare_MOS2.txt
     echo $gti_mos2 >> list_GTI_Flare_MOS2.txt
     echo $gti_mos2 >> list_GTI_Flare.txt
     chmod 766 "$oss1"_EMOS2_Optimised_flare_GTI_"$s1".fits
     echo "MOS2: "$gti_mos2" S GTI made"
     if ($barycentr == "yes") then
      set gtiflare="${gti_m2}"
      source $progr_dir/barycenter_flare.csh
     endif
   else
     echo ""$mos2S" is not here: the GTI "$gti_mos2" is not done"
   endif
   set Nds = ` expr $Nds + 1`
   end

   echo OBS $dir0 MOS2 U
   
   set ds = `ls *EMOS2_U*_FBKTSR.fits`
   set Nds=1
   foreach mos2 ( $ds )
   echo "$mos2"
   set oss1 = ` echo $mos2 | cut -c 1-15`
   set u1 = ` echo $mos2 | cut -c 23-26`
   set mos2U = ` echo "$oss1"_EMOS2_"$u1"_FBKTSR.fits`
   if ( -e $result_dir/$dir0/processed/$mos2U ) then
     echo "MOS2: Start to make "$mos2U" U GTI" 
     set rate_mos2=`ftlist $mos2U[1] K | grep "FLCUTTHR=" | awk '{print $2}'`
     tabgtigen expression="RATE <= "$rate_mos2"" gtiset="$oss1"_EMOS2_Optimised_flare_GTI_"$u1".fits mingtisize=100 table=$mos2U
     set gti_mos2="$oss1"_EMOS2_Optimised_flare_GTI_"$u1".fits
     echo $rate_mos2 >> rate_GTI_Flare_MOS2.txt
     echo $gti_mos2 >> list_GTI_Flare_MOS2.txt
     echo $gti_mos2 >> list_GTI_Flare.txt
     chmod 766 "$oss1"_EMOS2_Optimised_flare_GTI_"$u1".fits
     echo "MOS2: "$gti_mos2" U GTI made"
     if ($barycentr == "yes") then
      set gtiflare="${gti_m2}"
      source $progr_dir/barycenter_flare.csh
     endif
   else
     echo ""$mos2U" is not here: the GTI "$gti_mos2" is not done"
   endif
   set Nds = ` expr $Nds + 1`
   end
endif #DET=01

#####DET=03
if ( $DET == 03 ) then
   setenv SAS_ODF $data_dir/$dir0
   setenv SAS_CCF $data_dir/$dir0/ccf.cif

   echo OBS $dir0 pn S
     rm list_GTI_Flare_PN.txt
     rm list_GTI_Flare.txt
     rm rate_GTI_Flare_PN.txt

   set ds = `ls *EPN00_S*_FBKTSR.fits`
   set Nds=1
   foreach pn ( $ds )
   echo "$pn"
   set oss1 = ` echo $pn | cut -c 1-15`
   set s1 = ` echo $pn | cut -c 23-26`
   set pnS = ` echo "$oss1"_EPN00_"$s1"_FBKTSR.fits`
   if ( -e $result_dir/$dir0/processed/$pnS ) then
     echo "pn: Start to make "$pnS" S GTI" 
     set rate_pn=`ftlist $pnS[1] K | grep "FLCUTTHR=" | awk '{print $2}'`
      echo "Soft protons clean is not possible"
     tabgtigen expression="RATE <= "$rate_pn"" gtiset="$oss1"_EPN_Optimised_flare_GTI_"$s1".fits mingtisize=100 table=$pnS
     set gti_pn="$oss1"_EPN_Optimised_flare_GTI_"$s1".fits
     echo $rate_pn >> rate_GTI_Flare_PN.txt
     echo $gti_pn >> list_GTI_Flare_PN.txt
     echo $gti_pn >> list_GTI_Flare.txt
     chmod 766 "$oss1"_EPN_Optimised_flare_GTI_"$s1".fits
     echo "pn: "$gti_pn" S GTI made"
     if ($barycentr == "yes") then
      set gtiflare="${gti_pn}"
      source $progr_dir/barycenter_flare.csh
     endif
   else
     echo ""$pnS" is not here: the GTI "$gti_pn" is not done"
   endif
   set Nds = ` expr $Nds + 1`
   end

   echo OBS $dir0 pn U
   
   set ds = `ls *EPN00_U*_FBKTSR.fits`
   set Nds=1
   foreach pn ( $ds )
   echo "$pn"
   set oss1 = ` echo $pn | cut -c 1-15`
   set u1 = ` echo $pn | cut -c 23-26`
   set pnU = ` echo "$oss1"_EPN00_"$u1"_FBKTSR.fits`
   if ( -e $result_dir/$dir0/processed/$pnU ) then
     echo "pn: Start to make "$pnU" U GTI" 
     set rate_pn=`ftlist $pnU[1] K | grep "FLCUTTHR=" | awk '{print $2}'`
     tabgtigen expression="RATE <= "$rate_pn"" gtiset="$oss1"_EPN_Optimised_flare_GTI_"$u1".fits mingtisize=100 table=$pnU
     set gti_pn="$oss1"_EPN_Optimised_flare_GTI_"$u1".fits
     echo $rate_pn >> rate_GTI_Flare_PN.txt
     echo $gti_pn >> list_GTI_Flare_PN.txt
     echo $gti_pn >> list_GTI_Flare.txt
     chmod 766 "$oss1"_EPN_Optimised_flare_GTI_"$u1".fits
     echo "pn: "$gti_pn" U GTI made"
     if ($barycentr == "yes") then
      set gtiflare="${gti_pn}"
      source $progr_dir/barycenter_flare.csh
     endif
   else
     echo ""$pnU" is not here: the GTI "$gti_pn" is not done"
   endif
   set Nds = ` expr $Nds + 1`
   end
endif #DET=2

#####DET=2
 if ( $DET == 2 ) then
   setenv SAS_ODF $data_dir/$dir0
   setenv SAS_CCF $data_dir/$dir0/ccf.cif

   echo OBS $dir0 MOS1 S
     rm list_GTI_Flare_MOS1.txt
     rm list_GTI_Flare_MOS.txt
     rm list_GTI_Flare.txt
     rm list_GTI_Flare_MOS2.txt
     rm list_GTI_Flare_PN.txt
     rm rate_GTI_Flare_PN.txt
     rm rate_GTI_Flare_MOS1.txt
     rm rate_GTI_Flare_MOS2.txt
   
   set ds = `ls *EMOS1_S*_FBKTSR.fits`
   set Nds=1
   foreach mos1 ( $ds )
   echo "$mos1"
   set oss1 = ` echo $mos1 | cut -c 1-15`
   set s1 = ` echo $mos1 | cut -c 23-26`
   set mos1S = ` echo "$oss1"_EMOS1_"$s1"_FBKTSR.fits`
   if ( -e $result_dir/$dir0/processed/$mos1S ) then
     echo "MOS1: Start to make "$mos1" S GTI" 
     set rate_m1=`ftlist $mos1S[1] K | grep "FLCUTTHR=" | awk '{print $2}'`
     tabgtigen expression="RATE <= "$rate_m1"" gtiset="$oss1"_EMOS1_Optimised_flare_GTI_"$s1".fits mingtisize=100 table=$mos1S
     set gti_m1="$oss1"_EMOS1_Optimised_flare_GTI_"$s1".fits
     echo $rate_m1 >> rate_GTI_Flare_MOS1.txt
     echo $gti_m1 >> list_GTI_Flare_MOS1.txt
     echo $gti_m1 >> list_GTI_Flare_MOS.txt
     echo $gti_m1 >> list_GTI_Flare.txt
     chmod 766 "$oss1"_EMOS1_Optimised_flare_GTI_"$s1".fits
     echo "MOS1: "$gti_m1" S GTI made"
     if ($barycentr == "yes") then
      set gtiflare="${gti_m1}"
      source $progr_dir/barycenter_flare.csh
     endif
   else
     echo ""$mos1S" is not here: the GTI "$gti_m1" is not done"
   endif
   set Nds = ` expr $Nds + 1`
   end

   echo OBS $dir0 MOS2 S
   
   set ds = `ls *EMOS2_S*_FBKTSR.fits`
   set Nds=1
   foreach mos2 ( $ds )
   echo "$mos2"
   set oss2 = ` echo $mos2 | cut -c 1-15`
   set s2 = ` echo $mos2 | cut -c 23-26`
   set mos2S = ` echo "$oss2"_EMOS2_"$s2"_FBKTSR.fits`
   if ( -e $result_dir/$dir0/processed/$mos2S ) then
     echo "MOS2: Start to make "$mos2" S GTI" 
     set rate_m2=`ftlist $mos2S[1] K | grep "FLCUTTHR=" | awk '{print $2}'`
     tabgtigen expression="RATE <= "$rate_m2"" gtiset="$oss2"_EMOS2_Optimised_flare_GTI_"$s2".fits mingtisize=100 table=$mos2S
     set gti_m2="$oss2"_EMOS2_Optimised_flare_GTI_"$s2".fits
     echo $rate_m2 >> rate_GTI_Flare_MOS2.txt
     echo $gti_m2 >> list_GTI_Flare_MOS2.txt
     echo $gti_m2 >> list_GTI_Flare_MOS.txt
     echo $gti_m2 >> list_GTI_Flare.txt
     chmod 766 "$oss2"_EMOS2_Optimised_flare_GTI_"$s2".fits
     echo "MOS2: "$gti_m2" S GTI made"
     if ($barycentr == "yes") then
      set gtiflare="${gti_m2}"
      source $progr_dir/barycenter_flare.csh
     endif
   else
     echo ""$mos2S" is not here: the GTI "$gti_m2" is not done"
   endif
   set Nds = ` expr $Nds + 1`
   end

   echo OBS $dir0 MOS1 U

   set ds = `ls *EMOS1_U*_FBKTSR.fits`
   set Nds=1
   foreach Umos1 ( $ds )
   echo "$Umos1"
   set oss1U = ` echo $Umos1 | cut -c 1-15`
   set u1 = ` echo $Umos1 | cut -c 23-26`
   set mos1U = ` echo "$oss1U"_EMOS1_"$u1"_FBKTSR.fits`
   if ( -e $result_dir/$dir0/processed/$mos1U ) then
     echo "MOS1: Start to make "$mos1U" U GTI" 
     set rate_m1=`ftlist $mos1U[1] K | grep "FLCUTTHR=" | awk '{print $2}'`
     tabgtigen expression="RATE <= "$rate_m1"" gtiset="$oss1U"_EMOS1_Optimised_flare_GTI_"$u1".fits mingtisize=100 table=$mos1U
     set gti_m1="$oss1U"_EMOS1_Optimised_flare_GTI_"$u1".fits
     echo $rate_m1 >> rate_GTI_Flare_MOS1.txt
     echo $gti_m1 >> list_GTI_Flare_MOS1.txt
     echo $gti_m1 >> list_GTI_Flare_MOS.txt
     echo $gti_m1 >> list_GTI_Flare.txt
     chmod 766 "$oss1U"_EMOS1_Optimised_flare_GTI_"$u1".fits
     echo "MOS1: "$gti_m1" U GTI made"
     if ($barycentr == "yes") then
      set gtiflare="${gti_m1}"
      source $progr_dir/barycenter_flare.csh
     endif
   else
     echo ""$mos1U" is not here: the GTI "$gti_m1" is not done"
   endif
   set Nds = ` expr $Nds + 1`
   end

   echo OBS $dir0 MOS2 U

   set ds = `ls *EMOS2_U*_FBKTSR.fits`
   set Nds=1
   foreach Umos2 ( $ds )
   echo "$Umos2"
   set oss2U = ` echo $Umos2 | cut -c 1-15`
   set u2 = ` echo $Umos2 | cut -c 23-26`
   set mos2U = ` echo "$oss2U"_EMOS2_"$u2"_FBKTSR.fits`
   if ( -e $result_dir/$dir0/processed/$mos2U ) then
     echo "MOS2: Start to make "$mos2U" U GTI" 
     set rate_m2=`ftlist $mos2U[1] K | grep "FLCUTTHR=" | awk '{print $2}'`
     tabgtigen expression="RATE <= "$rate_m2"" gtiset="$oss2U"_EMOS2_Optimised_flare_GTI_"$u2".fits mingtisize=100 table=$mos2U
     set gti_m2="$oss2U"_EMOS2_Optimised_flare_GTI_"$u2".fits
     echo $rate_m2 >> rate_GTI_Flare_MOS2.txt
     echo $gti_m2 >> list_GTI_Flare_MOS2.txt
     echo $gti_m2 >> list_GTI_Flare_MOS.txt
     echo $gti_m2 >> list_GTI_Flare.txt
     chmod 766 "$oss2U"_EMOS2_Optimised_flare_GTI_"$u2".fits
     echo "MOS2: "$gti_m2" U GTI made"
     if ($barycentr == "yes") then
      set gtiflare="${gti_m2}"
      source $progr_dir/barycenter_flare.csh
     endif
   else
     echo ""$mos2U" is not here: the GTI "$gti_m2" is not done"
   endif
   set Nds = ` expr $Nds + 1`
   end
###
#PN 
   echo OBS $dir0 pn S
   
   set ds = `ls *EPN00_S*_FBKTSR.fits`
   set Nds=1
   foreach pn ( $ds )
   echo "$pn"
   set oss1 = ` echo $pn | cut -c 1-15`
   set s1 = ` echo $pn | cut -c 23-26`
   set pnS = ` echo "$oss1"_EPN00_"$s1"_FBKTSR.fits`
   if ( -e $result_dir/$dir0/processed/$pnS ) then
     echo "pn: Start to make "$pnS" S GTI" 
     set rate_pn=`ftlist $pnS[1] K | grep "FLCUTTHR=" | awk '{print $2}'`
     tabgtigen expression="RATE <= "$rate_pn"" gtiset="$oss1"_EPN_Optimised_flare_GTI_"$s1".fits mingtisize=100 table=$pnS
     set gti_pn="$oss1"_EPN_Optimised_flare_GTI_"$s1".fits
     echo $rate_pn >> rate_GTI_Flare_PN.txt
     echo $gti_pn >> list_GTI_Flare_PN.txt
     echo $gti_pn >> list_GTI_Flare.txt
     chmod 766 "$oss1"_EPN_Optimised_flare_GTI_"$s1".fits
     echo "pn: "$gti_pn" S GTI made"
     if ($barycentr == "yes") then
      set gtiflare="${gti_pn}"
      source $progr_dir/barycenter_flare.csh
     endif
   else
     echo ""$pnS" is not here: the GTI "$gti_pn" is not done"
   endif
   set Nds = ` expr $Nds + 1`
   end

   echo OBS $dir0 pn U

   set ds = `ls *EPN00_U*_FBKTSR.fits`
   set Nds=1
   foreach pn ( $ds )
   echo "$pn"
   set oss1 = ` echo $pn | cut -c 1-15`
   set u1 = ` echo $pn | cut -c 23-26`
   set pnU = ` echo "$oss1"_EPN00_"$u1"_FBKTSR.fits`
   if ( -e $result_dir/$dir0/processed/$pnU ) then
     echo "pn: Start to make "$pnU" U GTI" 
     set rate_pn=`ftlist $pnU[1] K | grep "FLCUTTHR=" | awk '{print $2}'`
     tabgtigen expression="RATE <= "$rate_pn"" gtiset="$oss1"_EPN_Optimised_flare_GTI_"$u1".fits mingtisize=100 table=$pnU
     set gti_pn="$oss1"_EPN_Optimised_flare_GTI_"$u1".fits
     echo $rate_pn >> rate_GTI_Flare_PN.txt
     echo $gti_pn >> list_GTI_Flare_PN.txt
     echo $gti_pn >> list_GTI_Flare.txt
     chmod 766 "$oss1"_EPN_Optimised_flare_GTI_"$u1".fits
     echo "pn: "$gti_pn" U GTI made"
     if ($barycentr == "yes") then
      set gtiflare="${gti_pn}"
      source $progr_dir/barycenter_flare.csh
     endif
   else
     echo ""$pnU" is not here: the GTI "$gti_pn" is not done"
   endif
   set Nds = ` expr $Nds + 1`
   end
 endif #DET=3

cd $result_dir/
cd $start_dir/

date
echo "stop GTI_flare_3XMM.csh"
