#! /bin/tcsh -f

echo "*************************************"
echo "*  start collect_results_header.csh *"
echo "*************************************"
date

set type_stringa_new = "$1"

#WP4PRODUCTS
if ( -r $prod_start_dir ) then
 echo ""$prod_start_dir" already exists"
else
 mkdir $prod_start_dir
 echo ""$prod_start_dir" done"
endif

echo "OBSERVATION ${dir0}"
#final products
set prod_dir_fin=$prod_start_dir/$dir0
if ( -r $prod_dir_fin ) then
 echo ""$prod_dir_fin" already exists"
else
 mkdir $prod_dir_fin
 echo ""$prod_dir_fin" done"
endif
set prod_dir_final=$prod_dir_fin/WP4products
if ( -r $prod_dir_final ) then
 echo ""$prod_dir_final" already exists"
else
 mkdir $prod_dir_final
 echo ""$prod_dir_final" done"
endif

cd ${prod_dir_final}
set tcat3xmmN=`ls *tcat_3xmm.fits | wc -l`
if ($tcat3xmmN == 1) then
 set tcat3xmm=`ls *tcat_3xmm.fits`
 fkeypar ${tcat3xmm}+1 NAXIS2
 set cat_rows = `pget fkeypar.par value`
 if (${cat_rows} != 0) then
  set event_file_all=`ls $data_dir/$dir0/*ImagingEvts*.ds | wc -l | awk '{print $1}'`   
  if ($event_file_all != 0) then
   set event_file_sing=`ls $data_dir/$dir0/*ImagingEvts*.ds | awk -v nr=1 '{ if (NR == nr) print $1}'`  
   set obs_id=`fkeyprint ${event_file_sing}+1 'OBS_ID' | grep "OBS_ID" | awk -v nr=2 '{ if (NR == nr) print $0}'`
   set date_obs=`fkeyprint ${event_file_sing}+1 'DATE-OBS' | grep "DATE-OBS" | awk -v nr=2 '{ if (NR == nr) print $0}'`
   set date_end=`fkeyprint ${event_file_sing}+1 'DATE-END' | grep "DATE-END" | awk -v nr=2 '{ if (NR == nr) print $0}'`
   set filter=`fkeyprint ${event_file_sing}+1 'FILTER' | grep "FILTER" | awk -v nr=2 '{ if (NR == nr) print $0}'`
   set observer=`fkeyprint ${event_file_sing}+1 'OBSERVER' | grep "OBSERVER" | awk -v nr=2 '{ if (NR == nr) print $0}'`
   set revolution=`fkeyprint ${event_file_sing}+1 'REVOLUT' | grep "REVOLUT" | awk -v nr=2 '{ if (NR == nr) print $0}'`
   set tstart_ev=`fkeyprint ${event_file_sing}+1 'TSTART' | grep "TSTART" | awk -v nr=2 '{ if (NR == nr) print $0}'`
   set tstop_ev=`fkeyprint ${event_file_sing}+1 'TSTOP' | grep "TSTOP" | awk -v nr=2 '{ if (NR == nr) print $0}'`
   set ra_pnt=`fkeyprint ${event_file_sing}+1 'RA_PNT' | grep "RA_PNT" | awk -v nr=2 '{ if (NR == nr) print $0}'`
   set dec_pnt=`fkeyprint ${event_file_sing}+1 'DEC_PNT' | grep "DEC_PNT" | awk -v nr=2 '{ if (NR == nr) print $0}'`
   set pa_pnt=`fkeyprint ${event_file_sing}+1 'PA_PNT' | grep "PA_PNT" | awk -v nr=2 '{ if (NR == nr) print $0}'`
   echo "${obs_id}" > editfile_fits.txt
   echo "${revolution}" >> editfile_fits.txt
   echo "${date_obs}" >> editfile_fits.txt
   echo "${date_end}" >> editfile_fits.txt
   echo "${tstart_ev}" >> editfile_fits.txt
   echo "${tstop_ev}" >> editfile_fits.txt
   echo "${filter}" >> editfile_fits.txt
   echo "${ra_pnt}" >> editfile_fits.txt
   echo "${dec_pnt}" >> editfile_fits.txt
   echo "${pa_pnt}" >> editfile_fits.txt
   echo "${observer}" >> editfile_fits.txt
   #DURATION and/or LIVETIME/EXPOSURE
   gunzip *.gz 
   ls *.img > list_results_tgz.txt
   ls *.expimg >> list_results_tgz.txt
   ls *.bkgimg >> list_results_tgz.txt
   ls *.fits > list_results_fits.txt
   set N_results_fits=`cat list_results_fits.txt | wc -l | awk '{print $1}'`
   if ($N_results_fits > 0) then
    foreach file_results_fits (`cat list_results_fits.txt`)
     echo "Changing the header of the ${file_results_fits} file..."
     echo "fthedit ${file_results_fits} @editfile_fits.txt" > editfile.csh
     source editfile.csh
     rm editfile.csh
    end
   endif
   set N_results_tgz=`cat list_results_tgz.txt | wc -l | awk '{print $1}'`
   if ($N_results_tgz > 0) then
    foreach file_results_tgz (`cat list_results_tgz.txt`)
     if ( $type_stringa_new == "binfixed" ) then
      set tid=`echo ${file_results_tgz} | cut -d'.' -f1 | cut -d't' -f2`
     endif
     if ( $type_stringa_new == "bayesian" ) then
      set tid=`echo ${file_results_tgz} | cut -d'_' -f4 | cut -d't' -f2 | cut -d'.' -f-1 | awk '{print $1}'`
     endif
     echo "Transient N.$tid"
     echo "Changing the header of the ${file_results_tgz} file..."
     echo "fthedit ${file_results_tgz} @editfile_fits.txt" > editfile.csh
     source editfile.csh
     rm editfile.csh
     echo "fselect infile=${tcat3xmm}+1 outfile=tmp.fits expr="T_ID.eq.${tid}" clobber=yes" > fselect.csh
     source fselect.csh
     rm fselect.csh
     fdump tmp.fits transient_ID.txt columns='T_ID' rows=- pagewidth=256 prhead=no showcol=no showunit=no showrow=no showscale=no clobber=yes
     set transient_ID=`cat transient_ID.txt`
    # echo "$transient_ID"
     fdump tmp.fits transient_RA.txt columns='RA' rows=- pagewidth=256 prhead=no showcol=no showunit=no showrow=no showscale=no clobber=yes
     set transient_RA=`cat transient_RA.txt`
     echo "Transient RA: $transient_RA"
     fdump tmp.fits transient_DEC.txt columns='DEC' rows=- pagewidth=256 prhead=no showcol=no showunit=no showrow=no showscale=no clobber=yes
     set transient_DEC=`cat transient_DEC.txt`
     echo "Transient Dec: $transient_DEC"
     rm tmp.fits
     echo "SRC_ID = ${transient_ID}        / Transient source identification number" > editfile_tgz.txt
     echo "SRC_RA = ${transient_RA}        / Transient source RA" >> editfile_tgz.txt
     echo "SRC_DEC = ${transient_DEC}      / Transient source Dec" >> editfile_tgz.txt
     echo "fthedit ${file_results_tgz} @editfile_tgz.txt" > editfile.csh
     source editfile.csh
     rm editfile.csh
     gzip ${file_results_tgz}
    end
   endif
  endif
 endif
 rm transient_ID.txt
 rm transient_RA.txt
 rm transient_DEC.txt
 rm editfile_tgz.txt
 rm editfile_fits.txt
 rm list_results_tgz.txt
 rm list_results_fits.txt
else
 echo "WARNING: no transient sources are detected in the observation ${dir0}"
endif
cd $start_dir/

date
echo "stop collect_results_header.csh"
