#! /bin/tcsh -f

echo "****************************"
echo "*  start check_target.csh  *"
echo "****************************"
date

set type_stringa_new = "$1"

set start_dir = /Users/Gio/Work/EXTRAS/EXTRAS_analysis3XMM  
set progr_dir = $start_dir/WP4scripts
set result_dir = $start_dir/results.v12
set bin_dir = $result_dir/BIN

set par_ext = 0

#TARGET SRC
set target_name = "PSRB0943+10"
set target_RA = 146.530458
set target_DEC = 9.865917
set max_dist = 20

set O = 1
foreach dir0 (`cat $start_dir/$obs_list`)
 echo "*************************************"
 echo "  START ANALYSIS: OBS $dir0          "
 echo "*************************************"
 set out_dir = $bin_dir/$dir0
 cd $out_dir/
 set N_cat_eml = 1
 foreach list_eml (`ls -1 emllist_1_* | sed -e "s/_1_/_/g"`)
  if ( -e $list_eml) then
   echo "Checking $target_name the catalogue "$list_eml"..."
   if ($type_stringa_new == "binfixed") then
    set outT=`echo $list_eml | awk '{print $1}' | cut -d'.' -f1 | cut -d'_' -f2`
   endif
   if ($type_stringa_new == "bayesian") then
    set outT=`echo $list_eml | awk '{print $1}' | cut -d'_' -f2 | cut -d'.' -f-3`
   endif

   set outTstart=`echo $outT | sed -e "s/-/ /g"| awk '{print $1}'`
   set outTstop=`echo $outT | sed -e "s/-/ /g"| awk '{print $2}'`
   set list_eml_new="emllist_"$outTstart"-"$outTstop"_NEW.fits" 
   set list_eml_target=""$dir0"_emllist_"$outTstart"-"$outTstop"_"$N_cat_eml"_TARGET.fits" 
   
   fkeypar $list_eml+1  NAXIS2 
   set n_eml = `pget fkeypar.par value`
   echo "Catalogue rows: $n_eml"
   set n_src_eml=`echo "$n_eml" | awk '{printf("%i",($1/19))}'`
   echo "N. of sources: $n_src_eml"
   if ($n_eml != 0) then
    set N_t_ctrl = 1
    while($N_t_ctrl <= $n_eml)
      echo "$outTstart $outTstop" >> tstart_tstop.txt
      set N_t_ctrl=`expr $N_t_ctrl + 1`
    end
    echo "T_START D" > t_column.txt
    echo "T_STOP D" >> t_column.txt
    echo "fcreate t_column.txt tstart_tstop.txt "$outTstart"-"$outTstop".fits clobber=yes" > fcreate.csh
    source fcreate.csh
    rm fcreate.csh
    rm tstart_tstop.txt 
    rm t_column.txt 
    echo "ftpaste "$outTstart"-"$outTstop".fits $list_eml $list_eml_new clobber=yes" > ftpaste.csh
    source ftpaste.csh
    rm ftpaste.csh
    rm "$outTstart"-"$outTstop".fits
    set ctrl_max_dist = 0
    set n = 1
    while($n <= $n_eml)       
     ftabpar $list_eml_new+1 column=RA row=$n
     set ra2 = `pget ftabpar.par value`
     ftabpar $list_eml_new+1 column=DEC row=$n
     set dec2 = `pget ftabpar.par value`
     ftabpar $list_eml_new+1 column=BOX_ID_SRC row=$n
     set  box_id = `pget ftabpar.par value`        
     ftabpar $list_eml+1 column=ML_ID_SRC row=$n
     set  ml_id = `pget ftabpar.par value`        
 
     set dist=`echo "$target_RA" "$target_DEC" "$ra2" "$dec2" | awk -f $progr_dir/angsep.awk`
     set dist1=`echo "$dist" | awk '{printf("%i",($1*1000))}'`
     set max_dist_int=`echo "$max_dist" | awk '{printf("%i",($1*1000))}'`
     echo "Target: $target_RA $target_DEC"
     echo "Catalogue src: $ra2 $dec2"
     if ($dist1 <= $max_dist_int) then
       set ctrl_max_dist = 1
       echo "OK: "$dir0" the src "$ml_id" (box id: "$box_id") at the row "$n" is to a distance of $dist arcsec from the target $target_name"
       echo "fselect infile="$list_eml_new"+1 outfile="$result_dir"/"$list_eml_target" expr="BOX_ID_SRC.eq."$box_id".and.EXT.le."$par_ext"" clobber=yes" > fselect.csh
       source fselect.csh
       rm fselect.csh
       set box_id_reg = $box_id
       echo ""$list_eml_target"+1" >> $result_dir/target_list.txt
     else
       echo "the src "$ml_id" is too far from target $target_name at the row n. "$n" : $dist arcsec"
     endif
    set n = `expr $n + 19`
    end
    rm "$list_eml_new"
   endif
  else
    echo "ERROR: there are not the src lists"
  endif
 set N_cat_eml = `expr $N_cat_eml + 1`
 end
 cd $start_dir/
set O = ` expr $O + 1`
end

cd $result_dir/
echo "fmerge @target_list.txt "$target_name".fits columns="-" clobber=yes" > fmerge.csh
source fmerge.csh
rm fmerge.csh
rm target_list.txt
source $progr_dir/make_cat_target.csh "$target_name".fits 
cd $start_dir/

date
echo "stop check_target.csh"
