#! /bin/tcsh -f

echo "*******************************"
echo "*  start analysis_extras.csh  *"
echo "*******************************"
date

if ( -e ctrl_setup.txt) then
 echo "========================"
 echo "setup.csh file ran..."
 echo "========================"
else
 echo "================================="
 echo "Please, run first setup.csh file!"
 echo "source analisi/setup.csh"
 echo "================================="
 break
endif

if ($type_det == "no" && $type_anal_fixed == "no" && $type_anal_bayesian == "no" && $target_spectra == "no" && $total_expo == "no") then
 echo "FATAL ERROR: ALL OPTIONS ARE SET ON NO"
 echo "Please, check the $progr_dir/setup.csh file and select an analysis option"
 break
endif

set O = 1
foreach dir0 (`cat $start_dir/$obs_list`)
echo "**********************************"
echo "                                  "
echo "  START ANALYSIS: OBS $dir0       "
echo "                                  "
echo "**********************************"

setenv SAS_CCF $data_dir/$dir0/ccf.cif
setenv SAS_ODF $data_dir/$dir0/pps

#final products directories
 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

#internal products directories
 if ( -r $result_dir/$dir0) then
  echo ""$result_dir/$dir0" already exists"
 else
  mkdir $result_dir/$dir0
  echo ""$result_dir/$dir0" done"
 endif

 set prod_dir=$result_dir/$dir0/WP4products
 if ( -r $prod_dir ) then
  echo ""$prod_dir" already exists"
 else
  mkdir $prod_dir
  echo ""$prod_dir" done"
 endif

#PFILES
if ($ambient == "cluster") then
  setenv PFILES  "${PWD}/PFILE_${dir0}_$$"
  cp -r ~/pfiles $PFILES
endif

#LOG DIR
if ( -r $log_dir_start ) then
 echo "OK LOG DIR"
 if ( -r $log_dir_start/$dir0 ) then
  echo "$log_dir_start/$dir0 dir exists"
  set log_dir = $log_dir_start/$dir0
 else
  mkdir $log_dir_start/$dir0
  set log_dir = $log_dir_start/$dir0
 endif
else
 echo "MAKE LOG DIR"
 mkdir $log_dir_start
 mkdir $log_dir_start/$dir0
 set log_dir = $log_dir_start/$dir0
endif

#ANALYSIS DATA DIR
if ( -r $data_dir ) then
 echo "OK ANALYSIS DATA DIR"
 if ( -r $data_dir/$dir0 ) then
  echo "$data_dir/$dir0 dir exists"
 else
  mkdir $data_dir/$dir0
 endif
else
 echo "MAKE ANALYSIS DATA DIR"
 mkdir $data_dir
 mkdir $data_dir/$dir0
endif

#PPS
#For ODF files
#source $progr_dir/crea_pps_test.csh >>& $log_dir/analysis.log
source $progr_dir/crea_pps.csh >>& $log_dir/analysis.log
if ( $pps == "YES" ) then

#CHECK OBS
source $progr_dir/check_epic.csh >>& $log_dir/analysis.log
if ($check_epic == "OK") then
 cd $start_dir/
 if ( -r $result_dir/$dir0) then
  echo "$result_dir/$dir0 already exists"
 else
  mkdir $result_dir/$dir0
 endif

if ($band_analysis == 0) then
 echo $b1 > $result_dir/$dir0/energy_band.txt
 echo $b2 >> $result_dir/$dir0/energy_band.txt
 echo $b3 >> $result_dir/$dir0/energy_band.txt
 echo $b4 >> $result_dir/$dir0/energy_band.txt
 echo $b5 >> $result_dir/$dir0/energy_band.txt
 echo $b6 >> $result_dir/$dir0/energy_band.txt

 echo $d1 > $result_dir/$dir0/energy_band_d.txt
 echo $d2 >> $result_dir/$dir0/energy_band_d.txt
 echo $d3 >> $result_dir/$dir0/energy_band_d.txt
 echo $d4 >> $result_dir/$dir0/energy_band_d.txt
 echo $d5 >> $result_dir/$dir0/energy_band_d.txt
 echo $d6 >> $result_dir/$dir0/energy_band_d.txt

 set N_EN_BAND=`cat $result_dir/$dir0/energy_band.txt | perl -ne 'print unless $.<2 ' | wc -l | awk '{print $1}'`
endif

if ( $band_analysis == 1) then
 echo $b1 > $result_dir/$dir0/energy_band.txt
 echo $b2 >> $result_dir/$dir0/energy_band.txt
 echo $b3 >> $result_dir/$dir0/energy_band.txt
 echo $b4 >> $result_dir/$dir0/energy_band.txt
 echo $b5 >> $result_dir/$dir0/energy_band.txt
 echo $b6 >> $result_dir/$dir0/energy_band.txt

 echo $d1 > $result_dir/$dir0/energy_band_d.txt
 echo $d2 >> $result_dir/$dir0/energy_band_d.txt
 echo $d3 >> $result_dir/$dir0/energy_band_d.txt
 echo $d4 >> $result_dir/$dir0/energy_band_d.txt
 echo $d5 >> $result_dir/$dir0/energy_band_d.txt
 echo $d6 >> $result_dir/$dir0/energy_band_d.txt

 set N_EN_BAND=`cat $result_dir/$dir0/energy_band.txt | perl -ne 'print unless $.<2 ' | wc -l | awk '{print $1}'`
endif
echo $model_spect > $result_dir/$dir0/model.txt

echo $ba1 > $result_dir/$dir0/energy_band_bayesian.txt
echo $ba2 >> $result_dir/$dir0/energy_band_bayesian.txt
echo $ba3 >> $result_dir/$dir0/energy_band_bayesian.txt

#GLOBAL HK
source $progr_dir/global_hk_3XMM.csh >>& $log_dir/analysis.log 

#DIRECTORY PREPARATION
source $progr_dir/clean_ds_3XMM.csh >>& $log_dir/analysis.log

#GTI PROTON FLARE
source $progr_dir/GTI_flare_3XMM.csh >>& $log_dir/analysis.log

#SPECTRA
if ($target_spectra == "yes") then
 if ($target_src == "yes") then
  echo ""
  echo "START TO PERFORM SPECTRA OF TARGET ${target_name}: ${dir0}"
  echo ""
  source $progr_dir/spectra_target.csh >>& $log_dir/analysis.log
 else
  echo "ERROR: no target is selected"
  echo "Please select a target source and its RA, Dec coordinates"
  break
 endif
endif

#DETECTION FULLDET
if ( $type_det == "fulldet") then
 echo "**************************"
 echo "*                        *"
 echo "*  THE FULLDET ANALYSIS  *"
 echo "*                        *"
 echo "**************************"
 echo "Analysis source detection fulldet"
 source $progr_dir/det_src_det_fulldet.csh
 set ctrl_fulldet = 1
endif

#FILTERING 3XMM CATALOGUE
echo "BEGIN FILTER_3XMM_CATALOG.CSH"
echo start: `date`
 source $progr_dir/filter_3xmm_catalog.csh >>& $log_dir/analysis.log 
echo end: `date`
echo "END FILTER_3XMM_CATALOG.CSH"

#TOTAL EXPOSURE
if ($total_expo == "yes") then
 echo "BEGIN CLEAN_MAKE_DETECTION_DIR.CSH"
 echo start: `date`
 source $progr_dir/clean_make_detection_dir_3XMM.csh >>& $log_dir/analysis.log
 echo end: `date`
 echo "END CLEAN_MAKE_DETECTION_DIR.CSH"
 echo "BEGIN TOTAL_EXPOSUREIMAGE.CSH"
 echo start: `date`
  source $progr_dir/total_exposureIMAGE.csh >>& $log_dir/analysis.log 
 echo end: `date`
 echo "END TOTAL_EXPOSUREIMAGE.CSH"
 echo "BEGIN TOTAL_EXPOSUREEXPO.CSH"
 echo start: `date`
  source $progr_dir/total_exposureEXPO.csh >>& $log_dir/analysis.log 
 echo end: `date`
 echo "END TOTAL_EXPOSUREIMAGEEXPO.CSH"
 echo "BEGIN TOTAL_EXPOSURE_EXPDETECTPREP_WEIGHT_IMAGE.CSH"
 echo start: `date`
  source $progr_dir/total_exposure_ExpDetectPrep_weight_image.csh >>& $log_dir/analysis.log 
 echo end: `date`
 echo "END TOTAL_EXPOSURE_EXPDETECTPREP_WEIGHT_IMAGE.CSH"
 echo "BEGIN TOTAL_EXPOSURE_EXPDETECTPREP_MERGE_EVT.CSH"
 echo start: `date`
  source $progr_dir/total_exposure_ExpDetectPrep_merge_evt.csh >>& $log_dir/analysis.log 
 echo end: `date`
 echo "END TOTAL_EXPOSURE_EXPDETECTPREP_MERGE_EVT.CSH"
 echo "BEGIN TOTAL_EXPOSURE_EXPDETECTPREP_DETMASK.CSH"
 echo start: `date`
  source $progr_dir/total_exposure_ExpDetect_detmask.csh >>& $log_dir/analysis.log 
 echo end: `date`
 echo "END TOTAL_EXPOSURE_EXPDETECTPREP_DETMASK.CSH"
 echo "BEGIN TOTAL_EXPOSURE_FILTER.CSH"
 echo start: `date`
  source $progr_dir/total_exposure_filter.csh >>& $log_dir/analysis.log 
 echo end: `date`
 echo "END TOTAL_EXPOSURE_FILTER.CSH"
 echo "BEGIN TOTAL_EXPOSURE_COUNT.CSH"
 echo start: `date`
  source $progr_dir/total_exposure_count.csh >>& $log_dir/analysis.log 
 echo end: `date`
 echo "END TOTAL_EXPOSURE_COUNT.CSH"
 echo "BEGIN TOTAL_EXPOSURE_STAT.CSH"
 echo start: `date`
  source $progr_dir/total_exposure_stat.csh >>& $log_dir/analysis.log 
 echo end: `date`
 echo "END TOTAL_EXPOSURE_STAT.CSH"
endif

#DETECTION BIN FIXED
if ( $type_anal_fixed == "binfixed" ) then
   echo "***************************"
   echo "*                         *"
   echo "*  THE BINFIXED ANALYSIS  *"
   echo "*                         *"
   echo "***************************"
  echo "TIME BIN FIXED ANALYSIS"
  if ( $type_det == "fulldet" && $type_bin_cat1 == "full" && $type_bin_cat2 == "3xmm" ) then
   echo "FULLDET analysis already done"
   source $progr_dir/master_bin.csh  
   source $progr_dir/master_bin_3xmm.csh
  endif
  if ( $type_det == "fulldet" && $type_bin_cat1 == "full" && $type_bin_cat2 == "no" ) then
   echo "FULLDET analysis already done"
   source $progr_dir/master_bin.csh  
  endif
  if ( $type_det == "fulldet" && $type_bin_cat1 == "no" && $type_bin_cat2 == "3xmm" ) then
   echo "FULLDET analysis already done"
   source $progr_dir/master_bin_3xmm.csh
  endif
  if ( $type_det == "no" && $type_bin_cat1 == "full" && $type_bin_cat2 == "3xmm" ) then
   source $progr_dir/master_start_bin.csh
   source $progr_dir/master_bin.csh  
   source $progr_dir/master_bin_3xmm.csh
  endif
  if ( $type_det == "no" && $type_bin_cat1 == "full" && $type_bin_cat2 == "no" ) then
   source $progr_dir/master_start_bin.csh
   source $progr_dir/master_bin.csh  
  endif
  if ( $type_det == "no" && $type_bin_cat1 == "no" && $type_bin_cat2 == "3xmm" ) then
   source $progr_dir/master_start_bin.csh
   source $progr_dir/master_bin_3xmm.csh
  endif
   if ($type_det == "no" && $type_bin_cat1 == "no" && $type_bin_cat2 == "no" && $target_spectra == "no" ) then
    echo "ATTENTION: ALL OPTIONS ARE SETTED ON NO"
    echo "Please, check the $progr_dir/setup.csh file and select an option"
    break
   endif
endif

#DETECTION BIN BAYESIAN
if ( $type_anal_bayesian == "bayesian" ) then
  echo "******************************************"
  echo "*                                        *"
  echo "*  OUT TO SRC AND CLOSE TO SRC ANALYSIS  *"
  echo "*                                        *"
  echo "******************************************"
  echo "TIME BIN BAYESIAN ANALYSIS"
  if ( $type_det == "fulldet" && $type_baye_cat1 == "full" && $type_baye_cat2 == "3xmm" ) then
   echo "FULLDET analysis already done"
   source $progr_dir/master_bayesian.csh
   source $progr_dir/master_bayesian_3xmm.csh
  endif
  if ( $type_det == "fulldet" && $type_baye_cat1 == "full" && $type_baye_cat2 == "no" ) then
   echo "FULLDET analysis already done"
   source $progr_dir/master_bayesian.csh
  endif
  if ( $type_det == "fulldet" && $type_baye_cat1 == "no" && $type_baye_cat2 == "3xmm" ) then
   echo "FULLDET analysis already done"
   source $progr_dir/master_bayesian_3xmm.csh
  endif
  if ( $type_det == "no" && $type_baye_cat1 == "full" && $type_baye_cat2 == "3xmm" ) then
   source $progr_dir/master_start_bin.csh
   source $progr_dir/master_bayesian.csh
   source $progr_dir/master_bayesian_3xmm.csh
  endif
  if ( $type_det == "no" && $type_baye_cat1 == "full" && $type_baye_cat2 == "no" ) then
   source $progr_dir/master_start_bin.csh
   source $progr_dir/master_bayesian.csh
  endif
  if ( $type_det == "no" && $type_baye_cat1 == "no" && $type_baye_cat2 == "3xmm" ) then
   source $progr_dir/master_start_bin.csh
   source $progr_dir/master_bayesian_3xmm.csh
  endif
  if ( $type_det == "no" && $type_baye_cat1 == "no" && $type_baye_cat2 == "no" && $target_spectra == "no" ) then
    echo "ATTENTION: ALL OPTIONS ARE SETTED ON NO"
    break
  endif
endif

#clean of $result_dir/$dir0/
if ($ctrl_clean == "yes") then
 echo "Cleaning directories..."
 source $progr_dir/clean.csh
endif

##remove PFILES
if ( $ambient == "cluster") then
 if ( -e $PFILES) then
  echo "Cleaning the PFILES directory"
  rm -r $PFILES
 endif
endif

endif
endif

if ( -r $log_dir_start ) then
 if ( -r $log_dir_start/$dir0 ) then
  set log_dir = $log_dir_start/$dir0
  if (-e $log_dir/analysis.log) then
   mv $log_dir/analysis.log $log_dir/analysis_v"$version_analysis".log
  endif
 endif
endif

set DET=`grep " DET " ${progr_dir}/setup.csh | awk -v nr=1 '{ if (NR==nr) print $4}'`
set bin = 0
set baye = 0
set ctrl_fulldet = 0
set O = ` expr $O + 1`

if ($type_anal_bayesian == "yes") then
 if ($ctrl_closetosrc == "yes") then
  set closetosrc = "yes"
  set ctrl_closetosrc = "no"
 endif
endif
end

date
echo "stop analysis_extras.csh"
