#! /bin/tcsh -f

echo "***************************"
echo "*  start cat_var_3xmm.csh *"
echo "***************************"
date

cd $result_dir/
set det=$result_dir/$dir0/detection
set prod_dir=$result_dir/$dir0/WP4products
if ($type_stringa == "binfixed") then
 set prod_dir3xmm = $prod_dir/BIN/3XMM
endif
if ($type_stringa == "bayesian") then
 set prod_dir3xmm = $prod_dir/BAYESIAN/3XMM
endif
#set an_dir = $bin_dir/$dir0
set an_dir2 = $xmmcat_dir
cd $an_dir2

if (-e emllist_var.txt) then
 rm emllist_var.txt
endif
if (-e "$an_dir2"/"$dir0"_1var.txt) then
   echo ""$an_dir2"/"$dir0"_1var.txt exists"
   set ctrl_var=`wc -l "$an_dir2"/"$dir0"_1var.txt | awk '{print $1}'`
   if ($ctrl_var == 2) then
     echo "WARNING: there are not variable sources in the $an_dir2/"$dir0"_1var.txt"
     echo "WARNING: there are not variable sources in the $an_dir2/"$dir0"_1var.txt BINFIXED" >> $log_dir/messages.log
   else
    set nvar = 1 
    foreach var_list (`grep "$dir0" "$an_dir2"/"$dir0"_1var.txt | awk '{print $1}'`)
     set n_tim_int=`grep "$dir0" "$an_dir2"/"$dir0"_1var.txt | awk -v nr=$nvar '{ if (NR == nr) print $30}' | wc | awk '{print $3}'`
     echo $n_tim_int >> $an_dir2/n_tim_int.txt 
    set nvar = ` expr $nvar + 1`
    end
   set max_n_tim_int=`cat $an_dir2/n_tim_int.txt | sort -nr | awk 'NR == 1 {print $1}'`  
   rm $an_dir2/n_tim_int.txt
   endif
endif

if (-e "$an_dir2"/"$dir0"_1var.txt) then
   echo ""$an_dir2"/"$dir0"_1var.txt exists"
   set ctrl_var=`wc -l "$an_dir2"/"$dir0"_1var.txt | awk '{print $1}'`
   if ($ctrl_var == 2) then
     echo "WARNING: there are not variable sources in the $an_dir2/"$dir0"_1var.txt"
     echo "WARNING: there are not variable sources in the $an_dir2/"$dir0"_1var.txt BINFIXED" >> $log_dir/messages.log
   else
    set nvar = 1 
    foreach var_list (`grep "$dir0" "$an_dir2"/"$dir0"_1var.txt | awk '{print $1}'`)
     if ($type_stringa == "binfixed") then
      set tsts=`echo $trans_list | cut -d'.' -f1 | cut -d'_' -f3 | awk '{print $1}'`
     endif
     if ($type_stringa == "bayesian") then
     # set tsts=`echo $trans_list | sed -e "s/\./ /g" | sed -e "s/\_/ /g" | awk '{print $3,$4,$5}' | sed -e "s/ /\./g"`
      set tsts=`echo $trans_list | cut -d'_' -f3 | cut -d'.' -f-3 | awk '{print $1}'`
     endif
     set n_timbin=`grep "$dir0" "$an_dir2"/"$dir0"_1var.txt | awk -v nr=$nvar '{ if (NR == nr) print int($29)}'`
     set id_var=`grep "$dir0" "$an_dir2"/"$dir0"_1var.txt | awk -v nr=$nvar '{ if (NR == nr) print int($25)}'`
     set id_var_3xmm=`grep "$dir0" "$an_dir2"/"$dir0"_1var.txt | awk -v nr=$nvar '{ if (NR == nr) print int($26)}'`
     set tim_int=`grep "$dir0" "$an_dir2"/"$dir0"_1var.txt | awk -v nr=$nvar '{ if (NR == nr) print $30}'`
     echo "variable: $nvar"

     if ($DET == 01) then
       set inst=2
     endif
     if ($DET == 02) then
       set inst=3
     endif
     if ($DET == 03) then
       set inst=1
     endif
     ##catalogo del singolo var
     if (-e $an_dir/emllist_"$tsts".fits) then
      fselect $an_dir/emllist_"$tsts".fits+1 $an_dir2/emllist_var"$nvar"_partial.fits ML_ID_SRC.eq."$id_var".and.ID_INST.ne.0 CLOBBER=yes 
      echo "emllist_var"$nvar"_partial.fits+1" >> $an_dir2/emllist_var.txt
      fkeypar $an_dir2/emllist_var"$nvar"_partial.fits+1 NAXIS2
      set var_rows = `pget fkeypar.par value`

      if ($type_bin_cat2 == "3xmm") then
       if (-e $data_dir/$dir0/"$dir0"_3XMM_catalogue.fits) then
        echo "Start to select the variable src in the 3XMM catalogue "
       # fselect $data_dir/$dir0/"$dir0"_3XMM_catalogue.fits+1 $an_dir2/emllist_3XMM_1_partial_var"$nvar".fits ML_ID_SRC.eq."$id_var_3xmm".and.ID_INST.eq."$inst".and.ID_INST.eq.0 CLOBBER=yes         
        fselect $data_dir/$dir0/"$dir0"_3XMM_catalogue.fits+1 $an_dir2/emllist_3XMM_1_partial_var"$nvar".fits ML_ID_SRC.eq."$id_var_3xmm".and.ID_INST.eq."$inst" CLOBBER=yes         
        fkeypar $an_dir2/emllist_3XMM_1_partial_var"$nvar".fits+1 NAXIS2
        set rows_3xmm = `pget fkeypar.par value`
       endif
      endif
     # cp "$dir0"_1_"$tsts"_var"$nvar".ds "$dir0"_1_"$tsts"_var"$nvar".reg "$dir0"_1_"$tsts"_var"$nvar"_bkg.ds "$dir0"_1_"$tsts"_var"$nvar"_bkg.reg $prod_dir3xmm
      cp "$dir0"_1_"$tsts"_var"$nvar".reg "$dir0"_1_"$tsts"_var"$nvar"_bkg.reg $prod_dir3xmm

      if ($plot0 == 1) then
       if (-e "$an_dir2"/emllist_var"$nvar"_partial.fits) then
        echo "#! /bin/tcsh -f" > $an_dir2/implot.csh
        echo "implot device="$an_dir2"/"$dir0"_"$tsts"_var"$nvar".PNG/PNG expression='' thickness=3 set="$an_dir"/"$dir0"_1_"$tsts".img srclisttab="$an_dir2"/emllist_var"$nvar"_partial.fits:SRCLIST withsrclisttab=yes -w "$warn" -V "$verb"" >> $an_dir2/implot.csh
        source $an_dir2/implot.csh
        cp "$an_dir2"/"$dir0"_"$tsts"_var"$nvar".PNG $prod_dir3xmm
        rm $an_dir2/implot.csh
       else
        echo ""$an_dir2"/emllist_var"$nvar"_partial.fits does not exist"
       endif
      endif

     ##aggiunta nel catalogo di singolo var della colonna var_id e tstart-tstop
      if (-e "$an_dir2"/emllist_var"$nvar"_partial.fits) then
       fkeypar $an_dir2/emllist_var"$nvar"_partial.fits+1 NAXIS2
       set var_rows = `pget fkeypar.par value`
       echo $var_rows
       rm $an_dir2/var.txt
       set col = 1
       while($col <= $var_rows)
        echo "$dir0 $nvar $n_timbin $tim_int" >> $an_dir2/var.txt
       set col = ` expr $col + 1`
       end
       echo "OBS_ID 10A" > $an_dir2/column.txt
       echo "VAR_ID I" >> $an_dir2/column.txt
       echo "N_TIME_BIN I" >> $an_dir2/column.txt
       echo "TIME_BINS "$max_n_tim_int"A" >> $an_dir2/column.txt
       echo "fcreate $an_dir2/column.txt $an_dir2/var.txt $an_dir2/var.fits clobber=yes" > $an_dir2/fcreate.csh
       source $an_dir2/fcreate.csh
       rm $an_dir2/fcreate.csh
       echo "ftpaste $an_dir2/var.fits+1 $an_dir2/emllist_var"$nvar"_partial.fits+1 $an_dir2/emllist_var"$nvar"_partial.fits clobber=yes" > $an_dir2/ftpaste.csh
       source $an_dir2/ftpaste.csh
       rm $an_dir2/ftpaste.csh
       rm $an_dir2/var.fits
       rm $an_dir2/var.txt
       rm $an_dir2/column.txt
       
       if ($type_bin_cat2 == "3xmm") then
        if ($var_rows == $rows_3xmm) then
         echo "ftpaste '"$an_dir2"/emllist_var"$nvar"_partial.fits+1' '"$an_dir2"/emllist_3XMM_1_partial_var"$nvar".fits+1[col ML_ID_SRC_3XMM==ML_ID_SRC;BOX_ID_SRC_3XMM==BOX_ID_SRC;ID_INST_3XMM==ID_INST;ID_BAND_3XMM==ID_BAND;ID_CLUSTER_3XMM==ID_CLUSTER;SCTS_3XMM==SCTS;SCTS_ERR_3XMM==SCTS_ERR;X_IMA_3XMM==X_IMA;X_IMA_ERR_3XMM==X_IMA_ERR;Y_IMA_3XMM==Y_IMA;Y_IMA_ERR_3XMM==Y_IMA_ERR;EXT_3XMM==EXT;EXT_ERR_3XMM==EXT_ERR;DET_ML_3XMM==DET_ML;EXT_ML_3XMM==EXT_ML;BG_MAP_3XMM==BG_MAP;EXP_MAP_3XMM==EXP_MAP;FLUX_3XMM==FLUX;FLUX_ERR_3XMM==FLUX_ERR;RATE_3XMM==RATE;RATE_ERR_3XMM==RATE_ERR;RA_3XMM==RA;DEC_3XMM==DEC;RADEC_ERR_3XMM==RADEC_ERR;LII_3XMM==LII;BII_3XMM==BII;RAWX_3XMM==RAWX;RAWY_3XMM==RAWY;OFFAX_3XMM==OFFAX;CCDNR_3XMM==CCDNR;HR1_3XMM==HR1;HR1_ERR_3XMM==HR1_ERR;HR2_3XMM==HR2;HR2_ERR_3XMM==HR2_ERR;HR3_3XMM==HR3;HR3_ERR_3XMM==HR3_ERR;HR4_3XMM==HR4;HR4_ERR_3XMM==HR4_ERR;CUTRAD_3XMM==CUTRAD;MASKFRAC_3XMM==MASKFRAC;EEF_3XMM==EEF;VIGNETTING_3XMM==VIGNETTING;ONTIME_3XMM==ONTIME;DIST_NN_3XMM==DIST_NN;FLAG_3XMM==FLAG]' $an_dir2/emllist_var"$nvar"_partial.fits clobber=yes" > $an_dir2/ftpaste.csh
         source $an_dir2/ftpaste.csh
         rm $an_dir2/ftpaste.csh
        else
         echo "catalogue emllist_var"$nvar"_partial.fits: $var_rows rows"
         echo "catalogue emllist_3XMM_1_partial_var"$nvar".fits: $rows_3xmm rows"
         echo "catalogues have different number of rows"
        endif
       endif
      endif
      if (-e "$an_dir"/"$dir0"_1_"$tsts".ds) then
     # cp "$an_dir"/"$dir0"_1_"$tsts".ds "$an_dir"/"$dir0"_1_"$tsts".PNG "$an_dir"/"$dir0"_1_"$tsts".bkgimg "$an_dir"/"$dir0"_1_"$tsts".expimg "$an_dir"/"$dir0"_1_"$tsts".img "$an_dir2"/"$dir0"_1_"$tsts".list "$an_dir2"/"$dir0"_1_"$tsts".out "$an_dir"/"$dir0"_1_"$tsts".reg $prod_dir3xmm
       cp "$an_dir"/"$dir0"_1_"$tsts".PNG "$an_dir2"/"$dir0"_1_"$tsts".list "$an_dir2"/"$dir0"_1_"$tsts".out "$an_dir"/"$dir0"_1_"$tsts".reg $prod_dir3xmm
       gzip -c "$an_dir"/"$dir0"_1_"$tsts".bkgimg > $prod_dir3xmm/"$dir0"_1_"$tsts".bkgimg.gz
       gzip -c "$an_dir"/"$dir0"_1_"$tsts".expimg > $prod_dir3xmm/"$dir0"_1_"$tsts".expimg.gz
       gzip -c "$an_dir"/"$dir0"_1_"$tsts".img > $prod_dir3xmm/"$dir0"_1_"$tsts".img.gz
      else
     # cp "$an_dir"/"$dir0"_1_"$tsts".PNG "$an_dir"/"$dir0"_1_"$tsts".bkgimg "$an_dir"/"$dir0"_1_"$tsts".expimg "$an_dir"/"$dir0"_1_"$tsts".img "$an_dir2"/"$dir0"_1_"$tsts".list "$an_dir2"/"$dir0"_1_"$tsts".out "$an_dir"/"$dir0"_1_"$tsts".reg $prod_dir3xmm
       cp "$an_dir"/"$dir0"_1_"$tsts".PNG "$an_dir2"/"$dir0"_1_"$tsts".list "$an_dir2"/"$dir0"_1_"$tsts".out "$an_dir"/"$dir0"_1_"$tsts".reg $prod_dir3xmm
       gzip -c "$an_dir"/"$dir0"_1_"$tsts".bkgimg > $prod_dir3xmm/"$dir0"_1_"$tsts".bkgimg.gz
       gzip -c "$an_dir"/"$dir0"_1_"$tsts".expimg > $prod_dir3xmm/"$dir0"_1_"$tsts".expimg.gz
       gzip -c "$an_dir"/"$dir0"_1_"$tsts".img > $prod_dir3xmm/"$dir0"_1_"$tsts".img.gz
      endif
     endif
    set nvar = ` expr $nvar + 1`
    # endif
    end

    ##catalogo del generale dei var
    echo "fmerge infiles=""$etm""$an_dir2"/emllist_var.txt" outfile="$an_dir2/"$dir0"_emllist_variable.fits" columns="-" clobber=yes" > $an_dir2/fmerge.csh
    source $an_dir2/fmerge.csh
    cp "$an_dir2"/"$dir0"_emllist_variable.fits $prod_dir3xmm
    rm $an_dir2/fmerge.csh
    rm $an_dir2/emllist_var*_partial.fits
    rm $an_dir2/emllist_3XMM_1_partial_var*.fits
    rm $an_dir2/emllist_var.txt
#    rm $an_dir2/emllist_singsrc_1_partial_var*_new_last.fits
#    rm $an_dir2/emllist_singsrc_1_partial_var*_new.fits
   endif
endif
cd $start_dir/
date
echo "stop cat_var_3xmm.csh"
