#! /bin/tcsh -f

echo "************************"
echo "*  start crea_pps.csh  *"
echo "************************"
date

if ( -r ${pps_archive_dir}/${dir0}/pps ) then
 cd ${pps_archive_dir}
 set evt=`ls -1 $pps_archive_dir/$dir0/pps/*MIEVL*.FTZ`
 ls -1 $dir0/pps/*MIEVL*.FTZ > $data_dir/$dir0/list
 ls -1 $dir0/pps/*PIEVL*.FTZ >> $data_dir/$dir0/list
 set MI=`cat $data_dir/$dir0/list | wc -l | awk '{print $1}'`
 rm $data_dir/$dir0/list
 if ( $MI == 0 ) then
  set pps = "NO"
  echo "ATTENTION there are not EPIC Imaging Event Files in $pps_archive_dir/$dir0/pps/" 
  echo "ATTENTION there are not EPIC Imaging Event Files in $pps_archive_dir/$dir0/pps/" >> $log_dir/messages.log 
  ls -1 $dir0/pps/*TIEVL*.FTZ > $data_dir/$dir0/list
  set MT=`cat $data_dir/$dir0/list | wc -l | awk '{print $1}'`
  rm $data_dir/$dir0/list
  if ( $MT == 0 ) then
   echo "ATTENTION there are not EPIC Timing Event Files in $pps_archive_dir/$dir0/pps/" 
   echo "ATTENTION there are not EPIC Timing Event Files in $pps_archive_dir/$dir0/pps/" >> $log_dir/messages.log
  else
   echo "ATTENTION there are EPIC Timing Event Files in $pps_archive_dir/$dir0/pps/" 
   echo "ATTENTION there are EPIC Timing Event Files in $pps_archive_dir/$dir0/pps/" >> $log_dir/messages.log
  endif  
  ls -1 $dir0/pps/*BUEVL*.FTZ > $data_dir/$dir0/list
  set MB=`cat $data_dir/$dir0/list | wc -l | awk '{print $1}'`
  rm $data_dir/$dir0/list
  if ( $MB == 0 ) then
   echo "ATTENTION there are not pn Burst Mode Event Files in $pps_archive_dir/$dir0/pps/" 
   echo "ATTENTION there are not pn Burst Mode Event Files in $pps_archive_dir/$dir0/pps/" >> $log_dir/messages.log
  else
   echo "ATTENTION there are pn Burst Mode Event Files in $pps_archive_dir/$dir0/pps/" 
   echo "ATTENTION there are pn Burst Mode Event Files in $pps_archive_dir/$dir0/pps/" >> $log_dir/messages.log
  endif  
  ls -1 $dir0/pps/*PIEVL*.FTZ > $data_dir/$dir0/list
  set MP=`cat $data_dir/$dir0/list | wc -l | awk '{print $1}'`
  rm $data_dir/$dir0/list
  if ( $MP == 0 ) then
   echo "ATTENTION there are not pn Large or Small Window Event Files in $pps_archive_dir/$dir0/pps/" 
   echo "ATTENTION there are not pn Large or Small Window Event Files in $pps_archive_dir/$dir0/pps/" >> $log_dir/messages.log
  else
   echo "ATTENTION there are pn Large or Small Window Event Files in $pps_archive_dir/$dir0/pps/" 
   echo "ATTENTION there are pn Large or Window Event Files in $pps_archive_dir/$dir0/pps/" >> $log_dir/messages.log
  endif  
 else
 echo "ATTENTION there are $MI EPIC Imaging Event Files in $pps_archive_dir/$dir0/pps/"
 echo ""
 ls -1 $dir0/pps/*TIEVL*.FTZ > $data_dir/$dir0/list
 set MT=`cat $data_dir/$dir0/list | wc -l | awk '{print $1}'`
 rm $data_dir/$dir0/list
 if ( $MT == 0 ) then
  echo "ATTENTION there are not EPIC Timing Event Files in $pps_archive_dir/$dir0/pps/" 
  echo "ATTENTION there are not EPIC Timing Event Files in $pps_archive_dir/$dir0/pps/" >> $log_dir/messages.log
 else
  echo "ATTENTION there are EPIC Timing Event Files in $pps_archive_dir/$dir0/pps/" 
  echo "ATTENTION there are EPIC Timing Event Files in $pps_archive_dir/$dir0/pps/" >> $log_dir/messages.log
 endif  
 ls -1 $dir0/pps/*BUEVL*.FTZ > $data_dir/$dir0/list
 set MB=`cat $data_dir/$dir0/list | wc -l | awk '{print $1}'`
 rm $data_dir/$dir0/list
 if ( $MB == 0 ) then
  echo "ATTENTION there are not pn Burst Mode Event Files in $pps_archive_dir/$dir0/pps/" 
  echo "ATTENTION there are not pn Burst Mode Event Files in $pps_archive_dir/$dir0/pps/" >> $log_dir/messages.log
 else
  echo "ATTENTION there are pn Burst Mode Event Files in $pps_archive_dir/$dir0/pps/" 
  echo "ATTENTION there are pn Burst Mode Event Files in $pps_archive_dir/$dir0/pps/" >> $log_dir/messages.log
 endif  
 ls -1 $dir0/pps/*PIEVL*.FTZ > $data_dir/$dir0/list
 set MP=`cat $data_dir/$dir0/list | wc -l | awk '{print $1}'`
 rm $data_dir/$dir0/list
 if ( $MP == 0 ) then
  echo "ATTENTION there are not pn Large or Small Window Event Files in $pps_archive_dir/$dir0/pps/" 
  echo "ATTENTION there are not pn Large or Small Window Event Files in $pps_archive_dir/$dir0/pps/" >> $log_dir/messages.log
 else
  echo "ATTENTION there are pn Large or Small Window Event Files in $pps_archive_dir/$dir0/pps/" 
  echo "ATTENTION there are pn Large or Window Event Files in $pps_archive_dir/$dir0/pps/" >> $log_dir/messages.log
 endif  

##CCF
  set cal=`ls $dir0/pps/*CAL*`
  set cal_ctrl=`ls $dir0/pps/*CAL* | wc -l | awk '{print $1}'`
  if ($cal_ctrl == 1) then
#   if (${ODF} == "no") then
    echo "========"
    echo "CCF FILE"
    echo "========"
    set cal2=`echo ccf.cif`
    gunzip -c $cal > $data_dir/$dir0/$cal2
    chmod 766 $data_dir/$dir0/$cal2
    echo "the file $cal copied in $data_dir/$dir0/$cal2"
#   endif
   set pps = "YES"

##ATT
   echo "============="
   echo "ATTITUDE FILE"
   echo "============="
   set attitude=`ls $dir0/pps/*ATT*`
   set attitude_ctrl=`ls $dir0/pps/*ATT* | wc -l | awk '{print $1}'`
   if ($attitude_ctrl == 1 ) then
    set obsatt="$dir0"
    set rev=`ftlist $attitude+0 K | grep "REVOLUT =" | sed -e "s/'/ /g"  | cut -d' ' -f4 | awk '{print $1}'`
    set attitude2=`echo "$rev"_"$obsatt"_AttHk.ds`
    gunzip -c $attitude > $data_dir/$dir0/$attitude2
    chmod 766 $data_dir/$dir0/$attitude2
    echo "the file $attitude copied in $data_dir/$dir0/$attitude2"
    fkeypar ${data_dir}/${dir0}/${attitude2}+0 DATE-OBS
    set DSTART = `pget fkeypar.par value`
    fkeypar ${data_dir}/${dir0}/${attitude2}+0 DATE-END
    set DSTOP = `pget fkeypar.par value`
    echo ""
    echo "==================================="
    echo "DATE-OBS: ${DSTART}"
    echo "DATE-END: ${DSTOP}"
    fkeypar ${data_dir}/${dir0}/${attitude2}+0 RA_NOM
    set RA_NOM = `pget fkeypar.par value`
    fkeypar ${data_dir}/${dir0}/${attitude2}+0 DEC_NOM
    set DEC_NOM = `pget fkeypar.par value`
    echo "RA_NOM: ${RA_NOM}"
    echo "DEC_NOM: ${DEC_NOM}"
    echo "==================================="
    echo ""
    set pps = "YES"

#Barycenter the attitude file
#    if ($barycentr == "yes") then
#     set att1=`ls $data_dir/$dir0/*AttHk*`
#     setenv SAS_ODF ${data_dir}/${dir0}
#     setenv SAS_CCF ${data_dir}/${dir0}/ccf.cif
#     cifinsert calindexset=${data_dir}/${dir0}/ccf.cif sets=${SAS_CCFPATH}/EMOS1_CONTAMINATION_0001.CCF
#     cifinsert calindexset=${data_dir}/${dir0}/ccf.cif sets=${SAS_CCFPATH}/EMOS2_CONTAMINATION_0001.CCF
#     echo "bash ${progr_dir}/asc_sum.sh ${dir0} ${data_dir}/${dir0} ${data_dir}/${dir0} ${pps_archive_dir}/${dir0}/pps ${ref} ${ros_asc_dir} ${att1} ${rev}" > ${data_dir}/${dir0}/asc_sum.csh
#     source ${data_dir}/${dir0}/asc_sum.csh
#     rm ${data_dir}/${dir0}/asc_sum.csh
#     set att1_ctrl=`ls $data_dir/$dir0/*AttHk* | wc -l | awk '{print $1}'`
#     if (${att1_ctrl} == 1) then
#      source $progr_dir/barycenter_att.csh
#     endif
#     chmod 766 $data_dir/$dir0/$attitude2
#    endif

##REGION FILE
   echo "==========="
   echo "REGION FILE"
   echo "==========="
   set region_file=`ls $dir0/pps/*EPX000REGION*`
   set region_file_ctrl=`ls $dir0/pps/*EPX000REGION* | wc -l | awk '{print $1}'`
   if ($region_file_ctrl == 1) then
    set region_file2=`echo "$dir0"_SRC_region_file_in.reg`
    set region_file3=`echo "$dir0"_SRC_region_file_out.reg`
    set region_file4=`echo "$dir0"_SRC_region_file_fixed.reg`
    if ($region_cheese == "in") then
    echo "Starting to create the region file $region_file2"
    cp $region_file $data_dir/$dir0/$region_file2
    chmod 766 $data_dir/$dir0/$region_file2
    endif
    if ($region_cheese == "out") then
    echo "Starting to create the region file $region_file3"
    cat $region_file | sed -e "s/circle/-circle/g" > $data_dir/$dir0/$region_file3 
    endif
    if ($region_cheese == "no") then
    echo "Starting to create the region file $region_file4"
    cat $region_file | sed -e "s/circle/-circle/g" > $data_dir/$dir0/region_file_fixed_0.reg 
    cat $data_dir/$dir0/region_file_fixed_0.reg | perl -ne 'print unless $.<3 ' | cut -d',' -f3 | cut -d')' -f1 > $data_dir/$dir0/arcsec.txt
    echo "# Region file format: DS9 version 3.0" > $data_dir/$dir0/region_file_fixed_test.reg
    echo "global font="helvetica 10 normal" edit=0 move=0 delete=1 include=1 fixed=0" >> $data_dir/$dir0/region_file_fixed_test.reg
    set arcsec_N = 1
    foreach arcsec (`cat $data_dir/$dir0/arcsec.txt`)
     set dopo=`echo "${fixed_radius}${vir}) # color=green"`
     set prima=`cat $data_dir/$dir0/region_file_fixed_0.reg | perl -ne 'print unless $.<3 ' | cut -d',' -f-2 | cut -d')' -f1 | awk -v nr="$arcsec_N" '{ if (NR==nr) printf("%s \n", $0)}'`
     echo "${prima},${dopo}" >> $data_dir/$dir0/region_file_fixed_test.reg
    set arcsec_N=`expr $arcsec_N + 1`
    end
    cp $data_dir/$dir0/region_file_fixed_test.reg $data_dir/$dir0/$region_file4
    rm $data_dir/$dir0/region_file_fixed_*.reg
    rm $data_dir/$dir0/arcsec.txt
    endif
    echo "the file $region_file copied in $data_dir/$dir0/$region_file2"
    echo "the file $region_file copied in $data_dir/$dir0/$region_file3"
    set pps = "YES"

##EVENT
  echo "==========="
  echo "EVENT FILES"
  echo "==========="
  
   set ctrl_PN_exists = "yes"
   if ($ODF == "yes") then
    set ctrl_PN_ODF = `ls -1 $dir0/pps/*PIEVL*.FTZ | wc -l`
    set ctrl_M_ODF = `ls -1 $dir0/pps/*MIEVL*.FTZ | wc -l`
    if ( $ctrl_PN_ODF != 0 || $ctrl_M_ODF != 0 ) then
     source ${progr_dir}/processing_data.csh
     cd ${pps_archive_dir}
    endif
   endif

#Barycenter the attitude file
    if ($barycentr == "yes") then
     set att1=`ls $data_dir/$dir0/*AttHk*`
     setenv SAS_ODF ${data_dir}/${dir0}
     setenv SAS_CCF ${data_dir}/${dir0}/ccf.cif
     cifinsert calindexset=${data_dir}/${dir0}/ccf.cif sets=${SAS_CCFPATH}/EMOS1_CONTAMINATION_0001.CCF
     cifinsert calindexset=${data_dir}/${dir0}/ccf.cif sets=${SAS_CCFPATH}/EMOS2_CONTAMINATION_0001.CCF
     echo "bash ${progr_dir}/asc_sum.sh ${dir0} ${data_dir}/${dir0} ${data_dir}/${dir0} ${pps_archive_dir}/${dir0}/pps ${ref} ${ros_asc_dir} ${att1} ${rev}" > ${data_dir}/${dir0}/asc_sum.csh
     source ${data_dir}/${dir0}/asc_sum.csh
     rm ${data_dir}/${dir0}/asc_sum.csh
     set att1_ctrl=`ls $data_dir/$dir0/*AttHk* | wc -l | awk '{print $1}'`
     if (${att1_ctrl} == 1) then
      source $progr_dir/barycenter_att.csh
     endif
     chmod 766 $data_dir/$dir0/$attitude2
    endif

setenv SAS_ODF ${data_dir}/${dir0}
setenv SAS_CCF ${data_dir}/${dir0}/ccf.cif

   if ($barycentr == "no") then
   rm $data_dir/$dir0/operating_mode_*.list
   ls -1 $dir0/pps/*MIEVL*.FTZ > $data_dir/$dir0/list
   ls -1 $dir0/pps/*PIEVL*.FTZ >> $data_dir/$dir0/list
   set N=`cat $data_dir/$dir0/list | wc -l | awk '{print $1}'`
   if ( $N != 0 ) then
    set pps = "YES"
    foreach ev (`cat $data_dir/$dir0/list`)
     if ( -e $ev ) then
      set obsevt="$dir0"
      set str=`echo $ev | cut -c 27-28`
      set str1=`echo $ev | cut -c 33-34`
      set par=`echo $ev | cut -c 29-32`
      if ( $str == "M1") then
      set EXPMAP=`echo P${dir0}${str}${par}EXPMAP8000.FTZ`
      if (-e ${pps_archive_dir}/${dir0}/pps/${EXPMAP}) then
       set modeM1=`ftlist $ev+0 K | grep "SUBMODE =" | sed -e "s/'/ /g"  | cut -d' ' -f4 | cut -c 1-12 | awk '{print $1}'`
       if ( $modeM1 == "PrimePartial" ) then
        set ev2=`echo "$rev"_"$obsatt"_EMOS1_"$par"_ImagingEvts.ds`
        if ($primepartial == "yes") then
         echo ""
         echo "Using the MOS1 $ev2 in PrimePartial mode"
         echo ""
         gunzip -c $ev > $data_dir/$dir0/$ev2
         chmod 766 $data_dir/$dir0/$ev2
         echo "the file $ev copied in $data_dir/$dir0/$ev2"
         echo "ATTENTION: MOS1 event file $ev2 is in "$modeM1" mode"
         echo ""$ev2":"$modeM1"" >> $data_dir/$dir0/operating_mode_"$str".list
        else
         echo ""
         echo "Using the MOS1 $ev2 not in PrimePartial mode"
         echo ""
         echo "/usr/bin/time -p evselect expression='(CCDNR > 1)' filteredset="$data_dir"/"$dir0"/"$ev2"~ imagebinning=binSize imagedatatype=Int32 imageset='' keepfilteroutput=Y table="$ev":EVENTS updateexposure=true withfilteredset=Y withimagedatatype=no withimageset=no writedss=true xcolumn=X ximagebinsize=80 ycolumn=Y yimagebinsize=80 -w "$warn" -V "$verb"" > $data_dir/$dir0/evselect.csh
         source $data_dir/$dir0/evselect.csh
         rm $data_dir/$dir0/evselect.csh
         echo "ftdelhdu "$ap""$data_dir"/"$dir0"/"$ev2"~[BADPIX01]"$ap" "$data_dir"/"$dir0"/"$ev2"~~ clobber=yes" > $data_dir/$dir0/ftdelhdu.csh
         source $data_dir/$dir0/ftdelhdu.csh
         echo "ftdelhdu "$ap""$data_dir"/"$dir0"/"$ev2"~~[STDGTI01]"$ap" "$data_dir"/"$dir0"/"$ev2" clobber=yes" > $data_dir/$dir0/ftdelhdu.csh
         source $data_dir/$dir0/ftdelhdu.csh
         rm $data_dir/$dir0/ftdelhdu.csh
         rm "$data_dir"/"$dir0"/"$ev2"~
         rm "$data_dir"/"$dir0"/"$ev2"~~
         echo "fparkey 0 "$data_dir"/"$dir0"/"$ev2"+1 ONTIME" > $data_dir/$dir0/fparkey.csh
         source $data_dir/$dir0/fparkey.csh
         rm $data_dir/$dir0/fparkey.csh
         echo "fparkey 0 "$data_dir"/"$dir0"/"$ev2"+1 ONTIME01" > $data_dir/$dir0/fparkey.csh
         source $data_dir/$dir0/fparkey.csh
         rm $data_dir/$dir0/fparkey.csh
         echo "fparkey 0 "$data_dir"/"$dir0"/"$ev2"+1 LIVETIME" > $data_dir/$dir0/fparkey.csh
         source $data_dir/$dir0/fparkey.csh
         rm $data_dir/$dir0/fparkey.csh
         echo "fparkey 0 "$data_dir"/"$dir0"/"$ev2"+1 LIVETI01" > $data_dir/$dir0/fparkey.csh
         source $data_dir/$dir0/fparkey.csh
         rm $data_dir/$dir0/fparkey.csh
         chmod 766 $data_dir/$dir0/$ev2
         echo "the file $ev copied in $data_dir/$dir0/$ev2"
         set modeM1=`ftlist $data_dir/$dir0/$ev2+0 K | grep "SUBMODE =" | sed -e "s/'/ /g"  | cut -d' ' -f4 | awk '{print $1}'`
         echo "ATTENTION: MOS1 event file $ev2 is in "$modeM1" mode"
         echo ""$ev2":"$modeM1"" >> $data_dir/$dir0/operating_mode_"$str".list
        endif
       else
        if ( $modeM1 == "FastUncompre" || $modeM1 == "FastCompress") then
          set ev2=`echo "$rev"_"$obsatt"_EMOS1_"$par"_ImagingEvts.ds`
          echo ""
          echo "Using the MOS1 $ev2 in Fast Uncompressed mode or Fast Compressed mode"
          echo ""
          gunzip -c $ev > $data_dir/$dir0/$ev2
          chmod 766 $data_dir/$dir0/$ev2
          echo "the file $ev copied in $data_dir/$dir0/$ev2"
          set attrcomm_on=`echo "$vir"Sum of GTIs for a particular CCD"$vir"`
          set attrname_on=`echo ONTIME01`
          set attrtype_on=`echo real`
          set realval_on=0
          echo "addattribute attributecomment='"$attrcomm_on"' attributename='"$attrname_on"' attributetype='"$attrtype_on"' realvalue='"$realval_on"' set="$data_dir"/"$dir0"/"$ev2":EVENTS -w "$warn" -V "$verb"" > $data_dir/$dir0/addatt_test.csh
          source $data_dir/$dir0/addatt_test.csh
          source $data_dir/$dir0/addatt_test.csh
          set attrcomm_on=`echo "$vir"Live time for a particular CCD"$vir"`
          set attrname_on=`echo LIVETI01`
          set attrtype_on=`echo real`
          set realval_on=0
          echo "addattribute attributecomment='"$attrcomm_on"' attributename='"$attrname_on"' attributetype='"$attrtype_on"' realvalue='"$realval_on"' set="$data_dir"/"$dir0"/"$ev2":EVENTS -w "$warn" -V "$verb"" > $data_dir/$dir0/addatt_test.csh
          source $data_dir/$dir0/addatt_test.csh
          source $data_dir/$dir0/addatt_test.csh
          chmod 766 $data_dir/$dir0/$ev2
          set modeM1=`ftlist $data_dir/$dir0/$ev2+0 K | grep "SUBMODE =" | sed -e "s/'/ /g"  | cut -d' ' -f4 | awk '{print $1}'`
          echo "ATTENTION: MOS1 event file $ev2 is in "$modeM1" mode"
          echo ""$ev2":"$modeM1"" >> $data_dir/$dir0/operating_mode_"$str".list
        else
         set ev2=`echo "$rev"_"$obsatt"_EMOS1_"$par"_ImagingEvts.ds`
         gunzip -c $ev > $data_dir/$dir0/$ev2
         chmod 766 $data_dir/$dir0/$ev2
         set modeM1=`ftlist $data_dir/$dir0/$ev2+0 K | grep "SUBMODE =" | sed -e "s/'/ /g"  | cut -d' ' -f4 | awk '{print $1}'`
         echo ""
         echo "Using the MOS1 $ev2 in $modeM1 mode"
         echo ""
         echo "the file $ev copied in $data_dir/$dir0/$ev2"
         set modeM1=`ftlist $data_dir/$dir0/$ev2+0 K | grep "SUBMODE =" | sed -e "s/'/ /g"  | cut -d' ' -f4 | awk '{print $1}'`
         echo "ATTENTION: MOS1 event file $ev2 is in "$modeM1" mode"
         echo ""$ev2":"$modeM1"" >> $data_dir/$dir0/operating_mode_"$str".list
        endif
       endif
      else
       echo "WARNING: (${dir0} - ${EXPMAP} does not exist) active EPIC cameras don't match respect cameras used to perform the source detection analysis "
       echo "WARNING: (${dir0} - ${EXPMAP} does not exist) active EPIC cameras don't match respect cameras used to perform the source detection analysis " >> $log_dir/messages.log
      endif
      endif
      if ( $str == "M2") then
      set EXPMAP=`echo P${dir0}${str}${par}EXPMAP8000.FTZ`
      if (-e ${pps_archive_dir}/${dir0}/pps/${EXPMAP}) then
       set modeM2=`ftlist $ev+0 K | grep "SUBMODE =" | sed -e "s/'/ /g"  | cut -d' ' -f4 | cut -c 1-12 | awk '{print $1}'`
       if ( $modeM2 == "PrimePartial" ) then
        set ev2=`echo "$rev"_"$obsatt"_EMOS2_"$par"_ImagingEvts.ds`
        if ($primepartial == "yes") then
         echo ""
         echo "Using the MOS2 $ev2 in PrimePartial mode"
         echo ""
         gunzip -c $ev > $data_dir/$dir0/$ev2
         chmod 766 $data_dir/$dir0/$ev2
        else
         echo ""
         echo "Using the MOS2 $ev2 not in PrimePartial mode"
         echo ""
         echo "/usr/bin/time -p evselect expression='(CCDNR > 1)' filteredset="$data_dir"/"$dir0"/"$ev2"~ imagebinning=binSize imagedatatype=Int32 imageset='' keepfilteroutput=Y table="$ev":EVENTS updateexposure=true withfilteredset=Y withimagedatatype=no withimageset=no writedss=true xcolumn=X ximagebinsize=80 ycolumn=Y yimagebinsize=80 -w "$warn" -V "$verb"" > $data_dir/$dir0/evselect.csh
         source $data_dir/$dir0/evselect.csh
         rm $data_dir/$dir0/evselect.csh
         echo "ftdelhdu '"$data_dir"/"$dir0"/"$ev2"~[BADPIX01]' "$data_dir"/"$dir0"/"$ev2"~~ clobber=yes" > $data_dir/$dir0/ftdelhdu.csh
         source $data_dir/$dir0/ftdelhdu.csh
         echo "ftdelhdu '"$data_dir"/"$dir0"/"$ev2"~~[STDGTI01]' "$data_dir"/"$dir0"/"$ev2" clobber=yes" > $data_dir/$dir0/ftdelhdu.csh
         source $data_dir/$dir0/ftdelhdu.csh
         rm $data_dir/$dir0/ftdelhdu.csh
         rm "$data_dir"/"$dir0"/"$ev2"~
         rm "$data_dir"/"$dir0"/"$ev2"~~
         echo "fparkey 0 "$data_dir"/"$dir0"/"$ev2"+1 ONTIME" > $data_dir/$dir0/fparkey.csh
         source $data_dir/$dir0/fparkey.csh
         rm $data_dir/$dir0/fparkey.csh
         echo "fparkey 0 "$data_dir"/"$dir0"/"$ev2"+1 ONTIME01" > $data_dir/$dir0/fparkey.csh
         source $data_dir/$dir0/fparkey.csh
         rm $data_dir/$dir0/fparkey.csh
         echo "fparkey 0 "$data_dir"/"$dir0"/"$ev2"+1 LIVETIME" > $data_dir/$dir0/fparkey.csh
         source $data_dir/$dir0/fparkey.csh
         rm $data_dir/$dir0/fparkey.csh
         echo "fparkey 0 "$data_dir"/"$dir0"/"$ev2"+1 LIVETI01" > $data_dir/$dir0/fparkey.csh
         source $data_dir/$dir0/fparkey.csh
         rm $data_dir/$dir0/fparkey.csh
         chmod 766 $data_dir/$dir0/$ev2
         echo "the file $ev copied in $data_dir/$dir0/$ev2"
         set modeM2=`ftlist $data_dir/$dir0/$ev2+0 K | grep "SUBMODE =" | sed -e "s/'/ /g"  | cut -d' ' -f4 | awk '{print $1}'`
         echo "ATTENTION: MOS2 event file $ev2 is in "$modeM2" mode"
         echo ""$ev2":"$modeM2"" >> $data_dir/$dir0/operating_mode_"$str".list
        endif
       else
        if ( $modeM2 == "FastUncompre" || $modeM2 == "FastCompress") then
          set ev2=`echo "$rev"_"$obsatt"_EMOS2_"$par"_ImagingEvts.ds`
          echo ""
          echo "Using the MOS2 $ev2 in Fast Uncompressed mode or Fast Compressed mode"
          echo ""
          gunzip -c $ev > $data_dir/$dir0/$ev2
          chmod 766 $data_dir/$dir0/$ev2
          echo "the file $ev copied in $data_dir/$dir0/$ev2"
          set attrcomm_on=`echo "$vir"Sum of GTIs for a particular CCD"$vir"`
          set attrname_on=`echo ONTIME01`
          set attrtype_on=`echo real`
          set realval_on=0
          echo "addattribute attributecomment='"$attrcomm_on"' attributename='"$attrname_on"' attributetype='"$attrtype_on"' realvalue='"$realval_on"' set="$data_dir"/"$dir0"/"$ev2":EVENTS -w "$warn" -V "$verb"" > $data_dir/$dir0/addatt_test.csh
          source $data_dir/$dir0/addatt_test.csh
          source $data_dir/$dir0/addatt_test.csh
          set attrcomm_on=`echo "$vir"Live time for a particular CCD"$vir"`
          set attrname_on=`echo LIVETI01`
          set attrtype_on=`echo real`
          set realval_on=0
          echo "addattribute attributecomment='"$attrcomm_on"' attributename='"$attrname_on"' attributetype='"$attrtype_on"' realvalue='"$realval_on"' set="$data_dir"/"$dir0"/"$ev2":EVENTS -w "$warn" -V "$verb"" > $data_dir/$dir0/addatt_test.csh
          source $data_dir/$dir0/addatt_test.csh
          source $data_dir/$dir0/addatt_test.csh
          set modeM2=`ftlist $data_dir/$dir0/$ev2+0 K | grep "SUBMODE =" | sed -e "s/'/ /g"  | cut -d' ' -f4 | awk '{print $1}'`
          echo "ATTENTION: MOS2 event file $ev2 is in "$modeM2" mode"
          echo ""$ev2":"$modeM2"" >> $data_dir/$dir0/operating_mode_"$str".list
        else
         set ev2=`echo "$rev"_"$obsatt"_EMOS2_"$par"_ImagingEvts.ds`
         gunzip -c $ev > $data_dir/$dir0/$ev2
         chmod 766 $data_dir/$dir0/$ev2
         set modeM2=`ftlist $data_dir/$dir0/$ev2+0 K | grep "SUBMODE =" | sed -e "s/'/ /g"  | cut -d' ' -f4 | awk '{print $1}'`
         echo ""
         echo "Using the MOS2 $ev2 in $modeM2 mode"
         echo ""
         echo "the file $ev copied in $data_dir/$dir0/$ev2"
         set modeM2=`ftlist $data_dir/$dir0/$ev2+0 K | grep "SUBMODE =" | sed -e "s/'/ /g"  | cut -d' ' -f4 | awk '{print $1}'`
         echo "ATTENTION: MOS2 event file $ev2 is in "$modeM2" mode"
         echo ""$ev2":"$modeM2"" >> $data_dir/$dir0/operating_mode_"$str".list
        endif
       endif
      else
       echo "WARNING: (${dir0} - ${EXPMAP} does not exist) active EPIC cameras don't match respect cameras used to perform the source detection analysis "
       echo "WARNING: (${dir0} - ${EXPMAP} does not exist) active EPIC cameras don't match respect cameras used to perform the source detection analysis " >> $log_dir/messages.log
      endif
      endif
      if ( $str == "PN" ) then
      set EXPMAP=`echo P${dir0}${str}${par}EXPMAP8000.FTZ`
      if (-e ${pps_archive_dir}/${dir0}/pps/${EXPMAP}) then
        set modePN=`ftlist $ev+0 K | grep "SUBMODE =" | sed -e "s/'/ /g"  | cut -d' ' -f4 | awk '{print $1}'`
        if ( $modePN == "PrimeSmallWindow" ) then
          echo ""
          echo "ATTENTION: pn event file is in "$modePN" mode"
          echo "ATTENTION: pn event file in "$modePN" mode will not be analysed"
          echo ""
        else
         set ev2=`echo "$rev"_"$obsatt"_EPN00_"$par"_ImagingEvts.ds`
         if ($ODF == "yes" && $ctrl_PN_exists == "yes") then
          set ev2_odf=`echo "$rev"_"$obsatt"_EPN_"$par"_ImagingEvts.ds`
          cp $data_dir/$dir0/$ev2_odf $data_dir/$dir0/$ev2
          rm $data_dir/$dir0/$ev2_odf
         else
          gunzip -c $ev > $data_dir/$dir0/$ev2
         endif
         chmod 766 $data_dir/$dir0/$ev2
         set modePN=`ftlist $data_dir/$dir0/$ev2+0 K | grep "SUBMODE =" | sed -e "s/'/ /g"  | cut -d' ' -f4 | awk '{print $1}'`
         echo ""
         echo "Using the PN $ev2 in $modePN mode"
         echo ""
         echo "the file $ev copied in $data_dir/$dir0/$ev2"

####CHECK COUNTING MODE
          source $progr_dir/check_counting_mode.csh
####
          set modePN=`ftlist $data_dir/$dir0/$ev2+0 K | grep "SUBMODE =" | sed -e "s/'/ /g"  | cut -d' ' -f4 | awk '{print $1}'`
          echo ""
          echo "ATTENTION: pn event file is $ev2 in "$modePN" mode"
          echo "ATTENTION: pn event file in "$modePN" mode will be analysed"
          echo ""
          echo ""$ev2":"$modePN"" >> $data_dir/$dir0/operating_mode_"$str".list
        endif
      else
       echo "WARNING: (${dir0} - ${EXPMAP} does not exist) active EPIC cameras don't match respect cameras used to perform the source detection analysis "
       echo "WARNING: (${dir0} - ${EXPMAP} does not exist) active EPIC cameras don't match respect cameras used to perform the source detection analysis " >> $log_dir/messages.log
      endif
      endif
     else
      echo "ERROR the file $pps_archive_dir/$ev does not exist" 
      echo "ERROR the file $pps_archive_dir/$ev does not exist" >> $log_dir/messages.log 
     endif
    end
   else
    set pps = "NO"
    echo "ERROR the file $pps_archive_dir/$ev does not exist" 
    echo "ERROR the file $pps_archive_dir/$ev does not exist" >> $log_dir/messages.log 
   endif
   endif #if barycentr = no

   if ($barycentr == "yes") then
   rm $data_dir/$dir0/operating_mode_*.list
   ls -1 $dir0/pps/*MIEVL*.FTZ > $data_dir/$dir0/list
   ls -1 $dir0/pps/*PIEVL*.FTZ >> $data_dir/$dir0/list
   set N=`cat $data_dir/$dir0/list | wc -l | awk '{print $1}'`
   if ( $N != 0 ) then
    set pps = "YES"
    foreach ev (`cat $data_dir/$dir0/list`)
     if ( -e $ev ) then
      set obsevt="$dir0"
      set str=`echo $ev | cut -c 27-28`
      set str1=`echo $ev | cut -c 33-34`
      set par=`echo $ev | cut -c 29-32`
      if ( $str == "M1") then
      set EXPMAP=`echo P${dir0}${str}${par}EXPMAP8000.FTZ`
      if (-e ${pps_archive_dir}/${dir0}/pps/${EXPMAP}) then
       set modeM1=`ftlist $ev+0 K | grep "SUBMODE =" | sed -e "s/'/ /g"  | cut -d' ' -f4 | cut -c 1-12 | awk '{print $1}'`
       if ( $modeM1 == "PrimePartial" ) then
        set ev2=`echo "$rev"_"$obsatt"_EMOS1_"$par"_ImagingEvts.ds`
        if ($primepartial == "yes") then
         echo ""
         echo "Using the MOS1 $ev2 in PrimePartial mode"
         echo ""
         gunzip -c $ev > $data_dir/$dir0/$ev2
         chmod 766 $data_dir/$dir0/$ev2
         echo "the file $ev copied in $data_dir/$dir0/$ev2"
         source $progr_dir/barycenter_evt.csh
         set modeM1=`ftlist $data_dir/$dir0/$ev2+0 K | grep "SUBMODE =" | sed -e "s/'/ /g"  | cut -d' ' -f4 | awk '{print $1}'`
         echo "ATTENTION: MOS1 event file $ev2 is in "$modeM1" mode"
         echo ""$ev2":"$modeM1"" >> $data_dir/$dir0/operating_mode_"$str".list
        else
         echo ""
         echo "Using the MOS1 $ev2 not in PrimePartial mode"
         echo ""
         echo "/usr/bin/time -p evselect expression='(CCDNR > 1)' filteredset="$data_dir"/"$dir0"/"$ev2"~ imagebinning=binSize imagedatatype=Int32 imageset='' keepfilteroutput=Y table="$ev":EVENTS updateexposure=true withfilteredset=Y withimagedatatype=no withimageset=no writedss=true xcolumn=X ximagebinsize=80 ycolumn=Y yimagebinsize=80 -w "$warn" -V "$verb"" > $data_dir/$dir0/evselect.csh
         source $data_dir/$dir0/evselect.csh
         rm $data_dir/$dir0/evselect.csh
         echo "ftdelhdu "$ap""$data_dir"/"$dir0"/"$ev2"~[BADPIX01]"$ap" "$data_dir"/"$dir0"/"$ev2"~~ clobber=yes" > $data_dir/$dir0/ftdelhdu.csh
         source $data_dir/$dir0/ftdelhdu.csh
         echo "ftdelhdu "$ap""$data_dir"/"$dir0"/"$ev2"~~[STDGTI01]"$ap" "$data_dir"/"$dir0"/"$ev2" clobber=yes" > $data_dir/$dir0/ftdelhdu.csh
         source $data_dir/$dir0/ftdelhdu.csh
         rm $data_dir/$dir0/ftdelhdu.csh
         rm "$data_dir"/"$dir0"/"$ev2"~
         rm "$data_dir"/"$dir0"/"$ev2"~~
         echo "fparkey 0 "$data_dir"/"$dir0"/"$ev2"+1 ONTIME" > $data_dir/$dir0/fparkey.csh
         source $data_dir/$dir0/fparkey.csh
         rm $data_dir/$dir0/fparkey.csh
         echo "fparkey 0 "$data_dir"/"$dir0"/"$ev2"+1 ONTIME01" > $data_dir/$dir0/fparkey.csh
         source $data_dir/$dir0/fparkey.csh
         rm $data_dir/$dir0/fparkey.csh
         echo "fparkey 0 "$data_dir"/"$dir0"/"$ev2"+1 LIVETIME" > $data_dir/$dir0/fparkey.csh
         source $data_dir/$dir0/fparkey.csh
         rm $data_dir/$dir0/fparkey.csh
         echo "fparkey 0 "$data_dir"/"$dir0"/"$ev2"+1 LIVETI01" > $data_dir/$dir0/fparkey.csh
         source $data_dir/$dir0/fparkey.csh
         rm $data_dir/$dir0/fparkey.csh
         chmod 766 $data_dir/$dir0/$ev2
         echo "the file $ev copied in $data_dir/$dir0/$ev2"
         source $progr_dir/barycenter_evt.csh
         set modeM1=`ftlist $data_dir/$dir0/$ev2+0 K | grep "SUBMODE =" | sed -e "s/'/ /g"  | cut -d' ' -f4 | awk '{print $1}'`
         echo "ATTENTION: MOS1 event file $ev2 is in "$modeM1" mode"
         echo ""$ev2":"$modeM1"" >> $data_dir/$dir0/operating_mode_"$str".list
        endif
       else
        if ( $modeM1 == "FastUncompre" || $modeM1 == "FastCompress") then
          set ev2=`echo "$rev"_"$obsatt"_EMOS1_"$par"_ImagingEvts.ds`
          echo ""
          echo "Using the MOS1 $ev2 in Fast Uncompressed mode or Fast Compressed mode"
          echo ""
          gunzip -c $ev > $data_dir/$dir0/$ev2
          chmod 766 $data_dir/$dir0/$ev2
          echo "the file $ev copied in $data_dir/$dir0/$ev2"
          set attrcomm_on=`echo "$vir"Sum of GTIs for a particular CCD"$vir"`
          set attrname_on=`echo ONTIME01`
          set attrtype_on=`echo real`
          set realval_on=0
          echo "addattribute attributecomment='"$attrcomm_on"' attributename='"$attrname_on"' attributetype='"$attrtype_on"' realvalue='"$realval_on"' set="$data_dir"/"$dir0"/"$ev2":EVENTS -w "$warn" -V "$verb"" > $data_dir/$dir0/addatt_test.csh
          source $data_dir/$dir0/addatt_test.csh
          source $data_dir/$dir0/addatt_test.csh
          set attrcomm_on=`echo "$vir"Live time for a particular CCD"$vir"`
          set attrname_on=`echo LIVETI01`
          set attrtype_on=`echo real`
          set realval_on=0
          echo "addattribute attributecomment='"$attrcomm_on"' attributename='"$attrname_on"' attributetype='"$attrtype_on"' realvalue='"$realval_on"' set="$data_dir"/"$dir0"/"$ev2":EVENTS -w "$warn" -V "$verb"" > $data_dir/$dir0/addatt_test.csh
          source $data_dir/$dir0/addatt_test.csh
          source $data_dir/$dir0/addatt_test.csh
          source $progr_dir/barycenter_evt.csh
          chmod 766 $data_dir/$dir0/$ev2
          set modeM1=`ftlist $data_dir/$dir0/$ev2+0 K | grep "SUBMODE =" | sed -e "s/'/ /g"  | cut -d' ' -f4 | awk '{print $1}'`
          echo "ATTENTION: MOS1 event file $ev2 is in "$modeM1" mode"
          echo ""$ev2":"$modeM1"" >> $data_dir/$dir0/operating_mode_"$str".list
        else
         set ev2=`echo "$rev"_"$obsatt"_EMOS1_"$par"_ImagingEvts.ds`
         gunzip -c $ev > $data_dir/$dir0/$ev2
         chmod 766 $data_dir/$dir0/$ev2
         set modeM1=`ftlist $data_dir/$dir0/$ev2+0 K | grep "SUBMODE =" | sed -e "s/'/ /g"  | cut -d' ' -f4 | awk '{print $1}'`
         echo ""
         echo "Using the MOS1 $ev2 in $modeM1 mode"
         echo ""
         echo "the file $ev copied in $data_dir/$dir0/$ev2"
         source $progr_dir/barycenter_evt.csh
         set modeM1=`ftlist $data_dir/$dir0/$ev2+0 K | grep "SUBMODE =" | sed -e "s/'/ /g"  | cut -d' ' -f4 | awk '{print $1}'`
         echo "ATTENTION: MOS1 event file $ev2 is in "$modeM1" mode"
         echo ""$ev2":"$modeM1"" >> $data_dir/$dir0/operating_mode_"$str".list
        endif
       endif
      else
       echo "WARNING: (${dir0} - ${EXPMAP} does not exist) active EPIC cameras don't match respect cameras used to perform the source detection analysis "
       echo "WARNING: (${dir0} - ${EXPMAP} does not exist) active EPIC cameras don't match respect cameras used to perform the source detection analysis " >> $log_dir/messages.log
      endif
      endif
      if ( $str == "M2") then
      set EXPMAP=`echo P${dir0}${str}${par}EXPMAP8000.FTZ`
      if (-e ${pps_archive_dir}/${dir0}/pps/${EXPMAP}) then
       set modeM2=`ftlist $ev+0 K | grep "SUBMODE =" | sed -e "s/'/ /g"  | cut -d' ' -f4 | cut -c 1-12 | awk '{print $1}'`
       if ( $modeM2 == "PrimePartial" ) then
        set ev2=`echo "$rev"_"$obsatt"_EMOS2_"$par"_ImagingEvts.ds`
        if ($primepartial == "yes") then
         echo ""
         echo "Using the MOS2 $ev2 in PrimePartial mode"
         echo ""
         gunzip -c $ev > $data_dir/$dir0/$ev2
         chmod 766 $data_dir/$dir0/$ev2
         echo "the file $ev copied in $data_dir/$dir0/$ev2"
         source $progr_dir/barycenter_evt.csh
         set modeM2=`ftlist $data_dir/$dir0/$ev2+0 K | grep "SUBMODE =" | sed -e "s/'/ /g"  | cut -d' ' -f4 | awk '{print $1}'`
         echo "ATTENTION: MOS2 event file $ev2 is in "$modeM2" mode"
         echo ""$ev2":"$modeM2"" >> $data_dir/$dir0/operating_mode_"$str".list
        else
         echo ""
         echo "Using the MOS2 $ev2 not in PrimePartial mode"
         echo ""
         echo "/usr/bin/time -p evselect expression='(CCDNR > 1)' filteredset="$data_dir"/"$dir0"/"$ev2"~ imagebinning=binSize imagedatatype=Int32 imageset='' keepfilteroutput=Y table="$ev":EVENTS updateexposure=true withfilteredset=Y withimagedatatype=no withimageset=no writedss=true xcolumn=X ximagebinsize=80 ycolumn=Y yimagebinsize=80 -w "$warn" -V "$verb"" > $data_dir/$dir0/evselect.csh
         source $data_dir/$dir0/evselect.csh
         rm $data_dir/$dir0/evselect.csh
         echo "ftdelhdu '"$data_dir"/"$dir0"/"$ev2"~[BADPIX01]' "$data_dir"/"$dir0"/"$ev2"~~ clobber=yes" > $data_dir/$dir0/ftdelhdu.csh
         source $data_dir/$dir0/ftdelhdu.csh
         echo "ftdelhdu '"$data_dir"/"$dir0"/"$ev2"~~[STDGTI01]' "$data_dir"/"$dir0"/"$ev2" clobber=yes" > $data_dir/$dir0/ftdelhdu.csh
         source $data_dir/$dir0/ftdelhdu.csh
         rm $data_dir/$dir0/ftdelhdu.csh
         rm "$data_dir"/"$dir0"/"$ev2"~
         rm "$data_dir"/"$dir0"/"$ev2"~~
         echo "fparkey 0 "$data_dir"/"$dir0"/"$ev2"+1 ONTIME" > $data_dir/$dir0/fparkey.csh
         source $data_dir/$dir0/fparkey.csh
         rm $data_dir/$dir0/fparkey.csh
         echo "fparkey 0 "$data_dir"/"$dir0"/"$ev2"+1 ONTIME01" > $data_dir/$dir0/fparkey.csh
         source $data_dir/$dir0/fparkey.csh
         rm $data_dir/$dir0/fparkey.csh
         echo "fparkey 0 "$data_dir"/"$dir0"/"$ev2"+1 LIVETIME" > $data_dir/$dir0/fparkey.csh
         source $data_dir/$dir0/fparkey.csh
         rm $data_dir/$dir0/fparkey.csh
         echo "fparkey 0 "$data_dir"/"$dir0"/"$ev2"+1 LIVETI01" > $data_dir/$dir0/fparkey.csh
         source $data_dir/$dir0/fparkey.csh
         rm $data_dir/$dir0/fparkey.csh
         chmod 766 $data_dir/$dir0/$ev2
         echo "the file $ev copied in $data_dir/$dir0/$ev2"
         source $progr_dir/barycenter_evt.csh
         set modeM2=`ftlist $data_dir/$dir0/$ev2+0 K | grep "SUBMODE =" | sed -e "s/'/ /g"  | cut -d' ' -f4 | awk '{print $1}'`
         echo "ATTENTION: MOS2 event file $ev2 is in "$modeM2" mode"
         echo ""$ev2":"$modeM2"" >> $data_dir/$dir0/operating_mode_"$str".list
        endif
       else
        if ( $modeM2 == "FastUncompre" || $modeM2 == "FastCompress") then
          set ev2=`echo "$rev"_"$obsatt"_EMOS2_"$par"_ImagingEvts.ds`
          echo ""
          echo "Using the MOS2 $ev2 in Fast Uncompressed mode or Fast Compressed mode"
          echo ""
          gunzip -c $ev > $data_dir/$dir0/$ev2
          chmod 766 $data_dir/$dir0/$ev2
          echo "the file $ev copied in $data_dir/$dir0/$ev2"
          set attrcomm_on=`echo "$vir"Sum of GTIs for a particular CCD"$vir"`
          set attrname_on=`echo ONTIME01`
          set attrtype_on=`echo real`
          set realval_on=0
          echo "addattribute attributecomment='"$attrcomm_on"' attributename='"$attrname_on"' attributetype='"$attrtype_on"' realvalue='"$realval_on"' set="$data_dir"/"$dir0"/"$ev2":EVENTS -w "$warn" -V "$verb"" > $data_dir/$dir0/addatt_test.csh
          source $data_dir/$dir0/addatt_test.csh
          source $data_dir/$dir0/addatt_test.csh
          set attrcomm_on=`echo "$vir"Live time for a particular CCD"$vir"`
          set attrname_on=`echo LIVETI01`
          set attrtype_on=`echo real`
          set realval_on=0
          echo "addattribute attributecomment='"$attrcomm_on"' attributename='"$attrname_on"' attributetype='"$attrtype_on"' realvalue='"$realval_on"' set="$data_dir"/"$dir0"/"$ev2":EVENTS -w "$warn" -V "$verb"" > $data_dir/$dir0/addatt_test.csh
          source $data_dir/$dir0/addatt_test.csh
          source $data_dir/$dir0/addatt_test.csh
          source $progr_dir/barycenter_evt.csh
          chmod 766 $data_dir/$dir0/$ev2
          set modeM2=`ftlist $data_dir/$dir0/$ev2+0 K | grep "SUBMODE =" | sed -e "s/'/ /g"  | cut -d' ' -f4 | awk '{print $1}'`
          echo "ATTENTION: MOS2 event file $ev2 is in "$modeM2" mode"
          echo ""$ev2":"$modeM2"" >> $data_dir/$dir0/operating_mode_"$str".list
        else
         set ev2=`echo "$rev"_"$obsatt"_EMOS2_"$par"_ImagingEvts.ds`
         gunzip -c $ev > $data_dir/$dir0/$ev2
         chmod 766 $data_dir/$dir0/$ev2
         set modeM2=`ftlist $data_dir/$dir0/$ev2+0 K | grep "SUBMODE =" | sed -e "s/'/ /g"  | cut -d' ' -f4 | awk '{print $1}'`
         echo ""
         echo "Using the MOS2 $ev2 in $modeM2 mode"
         echo ""
         echo "the file $ev copied in $data_dir/$dir0/$ev2"
         source $progr_dir/barycenter_evt.csh
         set modeM2=`ftlist $data_dir/$dir0/$ev2+0 K | grep "SUBMODE =" | sed -e "s/'/ /g"  | cut -d' ' -f4 | awk '{print $1}'`
         echo "ATTENTION: MOS2 event file $ev2 is in "$modeM2" mode"
         echo ""$ev2":"$modeM2"" >> $data_dir/$dir0/operating_mode_"$str".list
        endif
       endif
      else
       echo "WARNING: (${dir0} - ${EXPMAP} does not exist) active EPIC cameras don't match respect cameras used to perform the source detection analysis "
       echo "WARNING: (${dir0} - ${EXPMAP} does not exist) active EPIC cameras don't match respect cameras used to perform the source detection analysis " >> $log_dir/messages.log
      endif
      endif
      if ( $str == "PN" ) then
      set EXPMAP=`echo P${dir0}${str}${par}EXPMAP8000.FTZ`
      if (-e ${pps_archive_dir}/${dir0}/pps/${EXPMAP}) then
        set ctrl_ciccio = `ls -1 $ev | wc -l`
        if ($ctrl_ciccio != 0) then
          echo "OK ! $ev EXISTS"
        endif
        set modePN=`ftlist $ev+0 K | grep "SUBMODE =" | sed -e "s/'/ /g"  | cut -d' ' -f4 | awk '{print $1}'`
        if ( $modePN == "PrimeSmallWindow" ) then
          echo ""
          echo "ATTENTION: pn event file is in "$modePN" mode"
          echo "ATTENTION: pn event file in "$modePN" mode will not be analysed"
          echo ""
        else
         set ev2=`echo "$rev"_"$obsatt"_EPN00_"$par"_ImagingEvts.ds`
         if ($ODF == "yes" && $ctrl_PN_exists == "yes") then
          set ev2_odf=`echo "$rev"_"$obsatt"_EPN_"$par"_ImagingEvts.ds`
          set ctrl_ciccio3 = `ls -1 $data_dir/$dir0/$ev2_odf | wc -l`
          if ($ctrl_ciccio3 != 0) then
           echo "OK ! $data_dir/$dir0/$ev2_odf EXISTS"
          endif
          cp $data_dir/$dir0/$ev2_odf $data_dir/$dir0/$ev2
          rm $data_dir/$dir0/$ev2_odf
          set ctrl_ciccio2 = `ls -1 $data_dir/$dir0/$ev2 | wc -l`
          if ($ctrl_ciccio2 != 0) then
           echo "OK ! $data_dir/$dir0/$ev2 EXISTS"
          endif
         else
          gunzip -c $ev > $data_dir/$dir0/$ev2
         endif
         chmod 766 $data_dir/$dir0/$ev2
         set modePN=`ftlist $data_dir/$dir0/$ev2+0 K | grep "SUBMODE =" | sed -e "s/'/ /g"  | cut -d' ' -f4 | awk '{print $1}'`
         echo ""
         echo "Using the PN $ev2 in $modePN mode"
         echo ""
         echo "the file $ev copied in $data_dir/$dir0/$ev2"
         source $progr_dir/barycenter_evt.csh
         set modePN=`ftlist $data_dir/$dir0/$ev2+0 K | grep "SUBMODE =" | sed -e "s/'/ /g"  | cut -d' ' -f4 | awk '{print $1}'`
         echo ""
         echo "ATTENTION: pn event file is $ev2 in "$modePN" mode"
         echo "ATTENTION: pn event file in "$modePN" mode will be analysed"
         echo ""
         echo ""$ev2":"$modePN"" >> $data_dir/$dir0/operating_mode_"$str".list
####CHECK COUNTING MODE
         source $progr_dir/check_counting_mode.csh
####
        endif
      else
       echo "WARNING: (${dir0} - ${EXPMAP} does not exist) active EPIC cameras don't match respect cameras used to perform the source detection analysis "
       echo "WARNING: (${dir0} - ${EXPMAP} does not exist) active EPIC cameras don't match respect cameras used to perform the source detection analysis " >> $log_dir/messages.log
      endif
      endif
     else
      echo "ERROR the file $pps_archive_dir/$ev does not exist" 
      echo "ERROR the file $pps_archive_dir/$ev does not exist" >> $log_dir/messages.log 
     endif
    end
   else
    set pps = "NO"
    echo "ERROR the file $pps_archive_dir/$ev does not exist" 
    echo "ERROR the file $pps_archive_dir/$ev does not exist" >> $log_dir/messages.log 
   endif
   endif #if barycentr = yes 
   cd ${pps_archive_dir}

##CATALOGUE
   echo "========="
   echo "CATALOGUE"
   echo "========="
   set cat3xmm=`ls $dir0/pps/*MSRLI*`
   set cat3xmm_ctrl=`ls $dir0/pps/*MSRLI* | wc -l | awk '{print $1}'`
   if ($cat3xmm_ctrl == 1 ) then
    set cat3xmm2=`echo ""$dir0"_3XMM_catalogue.fits"`
    gunzip -c $cat3xmm > $data_dir/$dir0/$cat3xmm2
    chmod 766 $data_dir/$dir0/$cat3xmm2
    echo "the file $cat3xmm copied in $data_dir/$dir0/$cat3xmm2"
    set ctrl_cat3xmm = "YES"
    set ctrl_cts = 0
#    if ($type_cat_noWP2 == "yes") then
     set list_eml_dir = $data_dir/$dir0
     set list_eml = $list_eml_dir/$cat3xmm2
     set cat3xmm3=`echo ""$dir0"_3XMM_catalogue_WP2.fits"`
     set cat3xmm4=`echo ""$dir0"_3XMM_catalogue_ORIG.fits"`
     set cat3xmm5=`echo ""$dir0"_3XMM_catalogue_NO_WP2.fits"`
     if ( ${cts_limit} != 0 ) then
      echo "Catalogue $cat3xmm2 : starting to filter sources with N cts < $cts_limit cts"
      source $progr_dir/check_cts.csh 
      if (-e $list_eml_dir/$cat3xmm2 && -e $list_eml_dir/$cat3xmm3) then
       rm ${list_eml_dir}/files1.txt
       rm ${list_eml_dir}/files2.txt
       rm ${list_eml_dir}/${cat3xmm3}~
       rm ${list_eml_dir}/${cat3xmm5}~
       cp $list_eml_dir/$cat3xmm2 $list_eml_dir/$cat3xmm4
       cp $list_eml_dir/$cat3xmm3 $list_eml_dir/$cat3xmm2
      else
       rm ${list_eml_dir}/${cat3xmm5}~
       if (-e $list_eml_dir/$cat3xmm2) then
        cp $list_eml_dir/$cat3xmm2 $list_eml_dir/test.fits 
        ftdelrow infile=${list_eml_dir}/test.fits+1 outfile=${list_eml_dir}/test.fits~ rows="-" clobber=yes
        rm $list_eml_dir/test.fits
        cp $list_eml_dir/$cat3xmm2 $list_eml_dir/$cat3xmm4
        cp $list_eml_dir/test.fits~ $list_eml_dir/$cat3xmm2
        cp $list_eml_dir/$cat3xmm2 $list_eml_dir/$cat3xmm3
        rm $list_eml_dir/test.fits~
       else
        echo "FATAL ERROR: $list_eml_dir/$cat3xmm2 does not exist !"
        break
       endif
      endif
     endif
     if ($cts_limit == 0 && -e $list_eml_dir/$cat3xmm5) then
      echo "FATAL ERROR: $list_eml_dir/$cat3xmm5 exists but the counts limit is "$cts_limit""
      echo "No sources should be filtered out"
      break
     endif
    #endif
    set ctrl_max_dist = 0
    if ($target_src == "yes") then
     echo "Catalogue $cat3xmm2 : starting to filter the target $target_name with coordinates RA: $target_RA Dec: $target_DEC" 
     set list_eml_dir = $data_dir/$dir0
     set list_eml = $list_eml_dir/$cat3xmm2
     set cat3xmm3=`echo ""$dir0"_3XMM_catalogue_"$target_name".fits"`
     source $progr_dir/check_src_position.csh 
    endif
   else
    set ctrl_cat3xmm = "NO"
    echo "ERROR the 3XMM catalogue file $pps_archive_dir/$cat3xmm does not exist" 
    echo "ERROR the 3XMM catalogue file $pps_archive_dir/$cat3xmm does not exist" >> $log_dir/messages.log 
   endif

##SP 
   echo "=================="
   echo "PROTON FLARE FILES"
   echo "=================="
    set SP=`ls $dir0/pps/*FBKTSR*.FTZ`
    ls -1 $dir0/pps/*FBKTSR*.FTZ > $data_dir/$dir0/list
    set N=`cat $data_dir/$dir0/list | wc -l | awk '{print $1}'`
    rm $data_dir/$dir0/list
    if ( $N != 0 ) then
    set ctrl_sp = "YES"
    foreach spg ($SP)
     if ( -e $spg ) then
      set obsevt="$dir0"
      set str=`echo $spg | cut -c 27-28`
      set par=`echo $spg | cut -c 29-32`
      if ( $str == "M1") then
       set spg2=`echo "$rev"_"$obsatt"_EMOS1_"$par"_FBKTSR.fits`
       gunzip -c $spg > $data_dir/$dir0/$spg2
       chmod 766 $data_dir/$dir0/$spg2
       echo "the file $spg copied in $data_dir/$dir0/$spg2"
      endif
      if ( $str == "M2") then
       set spg2=`echo "$rev"_"$obsatt"_EMOS2_"$par"_FBKTSR.fits`
       gunzip -c $spg > $data_dir/$dir0/$spg2
       chmod 766 $data_dir/$dir0/$spg2
       echo "the file $spg copied in $data_dir/$dir0/$spg2"
      endif
      if ( $str == "PN") then
       set spg2=`echo "$rev"_"$obsatt"_EPN00_"$par"_FBKTSR.fits`
       gunzip -c $spg > $data_dir/$dir0/$spg2
       chmod 766 $data_dir/$dir0/$spg2
       echo "the file $spg copied in $data_dir/$dir0/$spg2"
      endif
     else
      echo "WARNING the file $pps_archive_dir/$spg does not exist" 
      echo "WARNING the file $pps_archive_dir/$spg does not exist" >> $log_dir/messages.log 
     endif
    end
   else
    echo "WARNING the file $pps_archive_dir/$spg does not exist" 
    echo "WARNING the file $pps_archive_dir/$spg does not exist" >> $log_dir/messages.log 
    endif
    cd ${pps_archive_dir}
   else
    set pps = "NO"
    echo "ERROR the region file $pps_archive_dir/$region_file does not exist" 
    echo "ERROR the region file $pps_archive_dir/$region_file does not exist" >> $log_dir/messages.log 
   endif
  else
   set pps = "NO"
   echo "ERROR the attitude file $pps_archive_dir/$attitude does not exist" 
   echo "ERROR the attitude file $pps_archive_dir/$attitude does not exist" >> $log_dir/messages.log 
  endif
 else
  set pps = "NO"
  echo "ERROR the calibration file $pps_archive_dir/$cal does not exist" 
  echo "ERROR the calibration file $pps_archive_dir/$cal does not exist" >> $log_dir/messages.log 
 endif
else
 set pps = "NO"
 echo "the directory ${pps_archive_dir}/${dir0}/pps does not exist"
endif
cd $start_dir/

date
echo "stop crea_pps.csh"
