#! /bin/tcsh -f

echo "**********************"
echo "*  start cat_var.csh *"
echo "**********************"
date

cd $result_dir/
set prod_dir=$result_dir/$dir0/WP4products
if ($type_stringa == "binfixed") then
 set prod_dirbin = $prod_dir/BIN
endif
if ($type_stringa == "bayesian") then
 set prod_dirbin = $prod_dir/BAYESIAN
endif
set det=$result_dir/$dir0/detection
cd $an_dir

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

if (-e "$an_dir"/"$dir0"_1var.txt) then
   echo ""$an_dir"/"$dir0"_1var.txt exists"
   set ctrl_var=`wc -l "$an_dir"/"$dir0"_1var.txt | awk '{print $1}'`
   if ($ctrl_var == 2) then
     echo "WARNING: there are not variable sources in the $an_dir/"$dir0"_1var.txt"
     echo "WARNING: there are not variable sources in the $an_dir/"$dir0"_1var.txt BINFIXED" >> $log_dir/messages.log
   else
    set nvar = 1 
    foreach var_list (`grep "$dir0" "$an_dir"/"$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_dir"/"$dir0"_1var.txt | awk -v nr=$nvar '{ if (NR == nr) print int($29)}'`
     set id_var=`grep "$dir0" "$an_dir"/"$dir0"_1var.txt | awk -v nr=$nvar '{ if (NR == nr) print int($25)}'`
     set id_var_full=`grep "$dir0" "$an_dir"/"$dir0"_1var.txt | awk -v nr=$nvar '{ if (NR == nr) print int($26)}'`
     set tim_int=`grep "$dir0" "$an_dir"/"$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_dir/emllist_var"$nvar"_partial.fits ML_ID_SRC.eq."$id_var" CLOBBER=yes 
      echo "emllist_var"$nvar"_partial.fits+1" >> $an_dir/emllist_var.txt
      fkeypar $an_dir/emllist_var"$nvar"_partial.fits+1 NAXIS2
      set var_rows = `pget fkeypar.par value`
      if ($type_bin_cat1 == "full") then
       if (-e $det/emllist.fits) then
         echo "Start to select the variable src in the full catalogue "
         fselect $det/emllist.fits+1 $an_dir/emllist_singsrc_1_partial_var"$nvar".fits ML_ID_SRC.eq."$id_var_full" CLOBBER=yes         
        # fselect $det/emllist.fits+1 $an_dir/emllist_singsrc_1_partial_var"$nvar".fits ML_ID_SRC.eq."$id_var_full".and.ID_INST.eq."$inst" CLOBBER=yes         
         fkeypar $an_dir/emllist_singsrc_1_partial_var"$nvar".fits+1 NAXIS2
         set full_rows = `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_dirbin
      cp "$dir0"_1_"$tsts"_var"$nvar".reg "$dir0"_1_"$tsts"_var"$nvar"_bkg.reg $prod_dirbin

      if ($plot0 == 1) then
       if (-e "$an_dir"/emllist_var"$nvar"_partial.fits) then
        echo "#! /bin/tcsh -f" > $an_dir/implot.csh
        echo "implot device="$an_dir"/"$dir0"_"$tsts"_var"$nvar".PNG/PNG expression='' thickness=3 set="$an_dir"/"$dir0"_1_"$tsts".img srclisttab="$an_dir"/emllist_var"$nvar"_partial.fits:SRCLIST withsrclisttab=yes -w "$warn" -V "$verb"" >> $an_dir/implot.csh
        source $an_dir/implot.csh
        cp "$an_dir"/"$dir0"_"$tsts"_var"$nvar".PNG $prod_dirbin
        rm $an_dir/implot.csh
       else
        echo ""$an_dir"/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_dir"/emllist_var"$nvar"_partial.fits) then
       fkeypar $an_dir/emllist_var"$nvar"_partial.fits+1 NAXIS2
       set var_rows = `pget fkeypar.par value`
       echo $var_rows
       rm $an_dir/var.txt
       set col = 1
       while($col <= $var_rows)
       # echo "$dir0 $nvar $tsts $n_timbin $tim_int" >> $an_dir/var.txt
        echo "$dir0 $nvar $n_timbin $tim_int" >> $an_dir/var.txt
       set col = ` expr $col + 1`
       end
       echo "OBS_ID 10A" > $an_dir/column.txt
       echo "VAR_ID I" >> $an_dir/column.txt
       echo "N_TIME_BIN I" >> $an_dir/column.txt
       echo "TIME_BINS "$max_n_tim_int"A" >> $an_dir/column.txt
       echo "fcreate $an_dir/column.txt $an_dir/var.txt $an_dir/var.fits clobber=yes" > $an_dir/fcreate.csh
       source $an_dir/fcreate.csh
       rm $an_dir/fcreate.csh
       echo "ftpaste $an_dir/var.fits+1 $an_dir/emllist_var"$nvar"_partial.fits+1 $an_dir/emllist_var"$nvar"_partial.fits clobber=yes" > $an_dir/ftpaste.csh
       source $an_dir/ftpaste.csh
       rm $an_dir/ftpaste.csh
       rm $an_dir/var.fits
       rm $an_dir/tran.txt
       rm $an_dir/column.txt
       
       if ($type_bin_cat1 == "full") then
        if ($var_rows == $full_rows) then
         echo "ftpaste '"$an_dir"/emllist_var"$nvar"_partial.fits+1' '"$an_dir"/emllist_singsrc_1_partial_var"$nvar".fits+1[col ML_ID_SRC_FULL==ML_ID_SRC;BOX_ID_SRC_FULL==BOX_ID_SRC;ID_INST_FULL==ID_INST;ID_BAND_FULL==ID_BAND;ID_CLUSTER_FULL==ID_CLUSTER;SCTS_FULL==SCTS;SCTS_ERR_FULL==SCTS_ERR;X_IMA_FULL==X_IMA;X_IMA_ERR_FULL==X_IMA_ERR;Y_IMA_FULL==Y_IMA;Y_IMA_ERR_FULL==Y_IMA_ERR;EXT_FULL==EXT;EXT_ERR_FULL==EXT_ERR;DET_ML_FULL==DET_ML;EXT_ML_FULL==EXT_ML;BG_MAP_FULL==BG_MAP;EXP_MAP_FULL==EXP_MAP;FLUX_FULL==FLUX;FLUX_ERR_FULL==FLUX_ERR;RATE_FULL==RATE;RATE_ERR_FULL==RATE_ERR;RA_FULL==RA;DEC_FULL==DEC;RADEC_ERR_FULL==RADEC_ERR;LII_FULL==LII;BII_FULL==BII;RAWX_FULL==RAWX;RAWY_FULL==RAWY;OFFAX_FULL==OFFAX;CCDNR_FULL==CCDNR;HR1_FULL==HR1;HR1_ERR_FULL==HR1_ERR;HR2_FULL==HR2;HR2_ERR_FULL==HR2_ERR;HR3_FULL==HR3;HR3_ERR_FULL==HR3_ERR;HR4_FULL==HR4;HR4_ERR_FULL==HR4_ERR;CUTRAD_FULL==CUTRAD;MASKFRAC_FULL==MASKFRAC;EEF_FULL==EEF;VIGNETTING_FULL==VIGNETTING;ONTIME_FULL==ONTIME;DIST_NN_FULL==DIST_NN;FLAG_FULL==FLAG]' $an_dir/emllist_var"$nvar"_partial.fits clobber=yes" > $an_dir/ftpaste.csh
         source $an_dir/ftpaste.csh
         rm $an_dir/ftpaste.csh
        else
         echo "catalogue emllist_var"$nvar"_partial.fits: $var_rows rows"
         echo "catalogue emllist_singsrc_1_partial_var"$nvar".fits: $full_rows rows"
         echo "catalogues have different number of rows"
        endif
       endif
      endif
      if (-e "$dir0"_1_"$tsts".ds) then
     # cp "$dir0"_1_"$tsts".PNG "$dir0"_1_"$tsts".bkgimg "$dir0"_1_"$tsts".ds "$dir0"_1_"$tsts".expimg "$dir0"_1_"$tsts".img "$dir0"_1_"$tsts".list "$dir0"_1_"$tsts".out "$dir0"_1_"$tsts".reg $prod_dirbin
       cp "$dir0"_1_"$tsts".PNG "$dir0"_1_"$tsts".list "$dir0"_1_"$tsts".out "$dir0"_1_"$tsts".reg $prod_dirbin
       gzip -c "$dir0"_1_"$tsts".bkgimg > $prod_dirbin/"$dir0"_1_"$tsts".bkgimg.gz
       gzip -c "$dir0"_1_"$tsts".expimg > $prod_dirbin/"$dir0"_1_"$tsts".expimg.gz
       gzip -c "$dir0"_1_"$tsts".img > $prod_dirbin/"$dir0"_1_"$tsts".img.gz
      else
     # cp "$dir0"_1_"$tsts".PNG "$dir0"_1_"$tsts".bkgimg "$dir0"_1_"$tsts".expimg "$dir0"_1_"$tsts".img "$dir0"_1_"$tsts".list "$dir0"_1_"$tsts".out "$dir0"_1_"$tsts".reg $prod_dirbin
       cp "$dir0"_1_"$tsts".PNG "$dir0"_1_"$tsts".list "$dir0"_1_"$tsts".out "$dir0"_1_"$tsts".reg $prod_dirbin
       gzip -c "$dir0"_1_"$tsts".bkgimg > $prod_dirbin/"$dir0"_1_"$tsts".bkgimg.gz
       gzip -c "$dir0"_1_"$tsts".expimg > $prod_dirbin/"$dir0"_1_"$tsts".expimg.gz
       gzip -c "$dir0"_1_"$tsts".img > $prod_dirbin/"$dir0"_1_"$tsts".img.gz
      endif
     endif
    set nvar = ` expr $nvar + 1`
  #  endif
    end

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