#! /bin/tcsh -f

echo "******************************"
echo "*  start collect_results.csh *"
echo "******************************"
date

#WP4PRODUCTS
if ( -r $prod_start_dir ) then
 echo ""$prod_start_dir" already exists"
else
 mkdir $prod_start_dir
 echo ""$prod_start_dir" done"
endif
#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

set type_stringa_new = "$1"

if ( $type_stringa_new == "bayesian" ) then
 if ( $type_baye_cat1 == "full" ) then
  set results_analysis_dir = $result_dir/$dir0/WP4products/BAYESIAN
 endif
 if ( $type_baye_cat2 == "3xmm" ) then
  set results_analysis_dir = $result_dir/$dir0/WP4products/BAYESIAN/3XMM/
 endif
endif
if ( $type_stringa_new == "binfixed" ) then
 if ( $type_bin_cat1 == "full" ) then
  set results_analysis_dir = $result_dir/$dir0/WP4products/BIN
 endif
 if ( $type_bin_cat2 == "3xmm" ) then
  set results_analysis_dir = $result_dir/$dir0/WP4products/BIN/3XMM
 endif
endif

if ($ctrl_no_trans == 1) then
 echo "WARNING: there are not transient sources in the $an_dir2/"$dir0"_1trans.txt"
 echo "WARNING: there are not transient sources in the $an_dir2/"$dir0"_1trans.txt" > $prod_dir_final/NO_TRANSIENT.txt
 echo "WARNING: no transients are detected for the observation $dir0" >> $prod_dir_final/NO_TRANSIENT.txt
else

cd $results_analysis_dir
set tcat=`ls *emllist*`
if ( -e $tcat ) then
 cp $tcat $prod_dir_final/${dir0}_tcat.fits
endif

ls -1 *trans* > $prod_dir_final/all_file.txt
foreach all_file (`cat $prod_dir_final/all_file.txt`)
 if ( -e $all_file ) then
  set trans_file=`echo $all_file | sed -e "s/trans/t/g"`
  cp $all_file $prod_dir_final/$trans_file
 endif
end

rm $prod_dir_final/all_file.txt
rm $prod_dir_final/bayesian*.reg
rm $prod_dir_final/*_emllist_tient.fits
rm $prod_dir_final/*_t*_bkg.reg

#SUMMARY FILE
 echo "Making the SUMMARY FILE..."
 cd $prod_dir_final/
 set N_t=`ls -1 *$dir0* | grep "expimg.gz" | wc -l | awk '{print $1}'`
 ls -1 *$dir0* | grep "expimg.gz" > expo_list.txt
 set N_t_ctrl = 1
 foreach expo_list (`cat expo_list.txt`)
  if ( $type_stringa_new == "binfixed" ) then
   set tsts_expo=`echo $expo_list | cut -d'.' -f1 | cut -d'_' -f3`
   set t_expo=`echo $expo_list | cut -d'.' -f1 | cut -d'_' -f4`
   set tid_expo=`echo $expo_list | cut -d'.' -f1 | cut -d't' -f2`
  endif
  if ( $type_stringa_new == "bayesian" ) then
   set tsts_expo=`echo $expo_list | cut -d'_' -f3 | cut -d'.' -f-3 | awk '{print $1}'`
   set t_expo=`echo $expo_list | cut -d'_' -f4 | cut -d'.' -f1 | awk '{print $1}'`
   set tid_expo=`echo $expo_list | cut -d'_' -f4 | cut -d't' -f2 | cut -d'.' -f-1 | awk '{print $1}'`
  endif
  set t_file=`ls -1 *$dir0* | grep "t${tid_expo}\${po}"`
  echo "$dir0 $tid_expo $t_file" >> $prod_dir_final/t_row.txt 
 set N_t_ctrl=`expr $N_t_ctrl + 1`
 end
 rm expo_list.txt
 if ( $type_stringa_new == "bayesian" ) then
  echo "OBS_ID 10A" > $prod_dir_final/t_column.txt
  echo "T_ID I" >> $prod_dir_final/t_column.txt
  echo "T_PNG 54A" >> $prod_dir_final/t_column.txt
  echo "ALL_SRC_PNG 54A" >> $prod_dir_final/t_column.txt
  echo "ALL_SRC_REG 54A" >> $prod_dir_final/t_column.txt
  echo "T_BKG_MAP 54A" >> $prod_dir_final/t_column.txt
  echo "T_EXP_MAP 54A" >> $prod_dir_final/t_column.txt
  echo "T_IMG_MAP 54A" >> $prod_dir_final/t_column.txt
  echo "T_REG 54A" >> $prod_dir_final/t_column.txt
  echo "TTYPE1 OBS_ID / transient observation ID" > $prod_dir_final/SUMMARY.lis
  echo "TTYPE2 T_ID / transient ID" >> $prod_dir_final/SUMMARY.lis
  echo "TTYPE3 T_PNG / transient image in png format" >> $prod_dir_final/SUMMARY.lis
  echo "TTYPE4 ALL_SRC_PNG / all sources detected image in png format" >> $prod_dir_final/SUMMARY.lis
  echo "TTYPE5 ALL_SRC_REG / all sources detected region file" >> $prod_dir_final/SUMMARY.lis
  echo "TTYPE6 T_BKG_MAP / transient background map" >> $prod_dir_final/SUMMARY.lis
  echo "TTYPE7 T_EXP_MAP / transient exposure map" >> $prod_dir_final/SUMMARY.lis
  echo "TTYPE8 T_IMG_MAP / transient image" >> $prod_dir_final/SUMMARY.lis
  echo "TTYPE9 T_REG / transient region file" >> $prod_dir_final/SUMMARY.lis
 endif
 if ( $type_stringa_new == "binfixed" ) then
  echo "OBS_ID 10A" > $prod_dir_final/t_column.txt
  echo "T_ID I" >> $prod_dir_final/t_column.txt
  echo "T_PNG 54A" >> $prod_dir_final/t_column.txt
  echo "ALL_SRC_PNG 54A" >> $prod_dir_final/t_column.txt
  echo "ALL_SRC_REG 54A" >> $prod_dir_final/t_column.txt
  echo "T_BKG_MAP 54A" >> $prod_dir_final/t_column.txt
  echo "T_EXP_MAP 54A" >> $prod_dir_final/t_column.txt
  echo "T_IMG_MAP 54A" >> $prod_dir_final/t_column.txt
  echo "T_REG 54A" >> $prod_dir_final/t_column.txt
  echo "TTYPE1 OBS_ID / transient observation ID" > $prod_dir_final/SUMMARY.lis
  echo "TTYPE2 T_ID / transient ID" >> $prod_dir_final/SUMMARY.lis
  echo "TTYPE3 T_PNG / transient image in png format" >> $prod_dir_final/SUMMARY.lis
  echo "TTYPE4 ALL_SRC_PNG / all sources detected image in png format" >> $prod_dir_final/SUMMARY.lis
  echo "TTYPE5 ALL_SRC_REG / all sources detected region file" >> $prod_dir_final/SUMMARY.lis
  echo "TTYPE6 T_BKG_MAP / transient background map" >> $prod_dir_final/SUMMARY.lis
  echo "TTYPE7 T_EXP_MAP / transient exposure map" >> $prod_dir_final/SUMMARY.lis
  echo "TTYPE8 T_IMG_MAP / transient image" >> $prod_dir_final/SUMMARY.lis
  echo "TTYPE9 T_REG / transient region file" >> $prod_dir_final/SUMMARY.lis
 endif

 echo "fcreate $prod_dir_final/t_column.txt $prod_dir_final/t_row.txt $prod_dir_final/"$dir0"_t_SUMMARY_FILE.fits headfile=SUMMARY.lis clobber=yes" > $prod_dir_final/fcreate.csh
 source $prod_dir_final/fcreate.csh
 rm $prod_dir_final/SUMMARY.lis 

 echo "The SUMMARY FILE done"
 rm $prod_dir_final/fcreate.csh
 rm $prod_dir_final/t_column.txt
 rm $prod_dir_final/t_row.txt

#3XMM CATALOGUE
 echo "Making the transient 3XMM catalogue..."
 if (-e ${dir0}_tcat.fits) then
  source $progr_dir/make_cat_3xmm.csh
 else
  echo "WARNING: the transient 3XMM catalogue ${prod_dir_final}/${dir0}_tcat.fits does not exist"
 endif
 if (-e ${prod_dir_final}/${dir0}_tcat_3xmm.fits) then
  if ($barycentr == "yes") then
    echo "TIMEREF = 'SOLARSYSTEM'        / Reference location of photon arrival times" > ${prod_dir_final}/editfile.txt
    echo "fthedit ${prod_dir_final}/${dir0}_tcat_3xmm.fits @"${prod_dir_final}/editfile.txt"" > ${prod_dir_final}/editfile.csh
    source ${prod_dir_final}/editfile.csh
    rm ${prod_dir_final}/editfile.csh
    rm ${prod_dir_final}/editfile.txt
  else  
    echo "TIMEREF = 'SATELLITE'        / Reference location of photon arrival times" > ${prod_dir_final}/editfile.txt
    echo "fthedit ${prod_dir_final}/${dir0}_tcat_3xmm.fits @"${prod_dir_final}/editfile.txt"" > ${prod_dir_final}/editfile.csh
    source ${prod_dir_final}/editfile.csh
    rm ${prod_dir_final}/editfile.csh
    rm ${prod_dir_final}/editfile.txt
  endif
 else
  echo "WARNING: the transient 3XMM catalogue ${prod_dir_final}/${dir0}_tcat_3xmm.fits does not exist"
 endif
 echo "The transient 3XMM catalogue done"
 endif
 cd $start_dir/

date
echo "stop collect_results.csh"
