#!/bin/bash
 
ifolder=$1   ## contain all snapshots (gw format)
netName=$2 ##network name
ofolder=$3  ## output folder that stores all results
dynGDVS=$4 ## dynamic gdvs file

stattmpGDV=$ofolder/$netName"StatTmpGDV.txt"  ## output file that contains all nodes' static tmp gdv values.
weight_file=$ofolder/$netName"-w.txt" ## output file that contains all relevant orbit weights

if [ -d $ofolder ]; then
	rm -r $ofolder
fi
mkdir $ofolder

## calculate GDV of nodes in each snapshot
gdv_folder=$ifolder-"StatTmpGDV"
if [ -d $gdv_folder ]; then
	rm -r $gdv_folder
fi
mkdir $gdv_folder

for item in `ls $ifolder`
do
   echo $item
   gdvName=`echo $item | cut -d'.' -f1`
  ./ncount $ifolder/$item $gdvName"-gdv5"
  mv $gdvName"-gdv5.ndump2" $gdv_folder
  rm $gdvName"-gdv5"
done


counter=0
for item in `ls $gdv_folder`
do
  echo "counter: "$counter
  if [ $counter -eq "0" ];then
    cat $gdv_folder/$item > $stattmpGDV"-"$counter
  else
    
   
    #awk 'FNR==NR{$1=""; a[FNR]=$0; next}{print $0, a[FNR]}'  $stattmpGDV $gdv_folder/$item > $stattmpGDV"-tmp"
    prev=`expr $counter - 1`
    python join_gdv.py -b $stattmpGDV"-"$prev -a $gdv_folder/$item -o $stattmpGDV"-"$counter
    rm $stattmpGDV"-"$prev
    awk '{if (NR==4) print NF }' $stattmpGDV"-"$counter
    #sort -k1 $gdv_folder/$item | join -j 1 $stattmpGDV > $stattmpGDV
  fi
  counter=`expr $counter + 1`
  
done
counter=`expr $counter - 1` 
mv $stattmpGDV"-"$counter  $stattmpGDV

##basic weights
weights=( 1 0.838444533 0.838444533 0.838444533 0.743940642 0.676889065 0.743940642 0.743940642 0.676889065 0.743940642 0.676889065 0.676889065 0.676889065 0.676889065 0.743940642 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 )
if [ -e $weight_file ];then
   rm $weight_file
fi

touch $weight_file
counter=`expr $counter + 1`
## generate a weight file
for ic in $(seq 1 1 $counter)
do
  for iw in "${weights[@]}"
  do
    echo -e 'xx \t xx \t'$iw >> $weight_file
  done
done

## step 1: calculate static tmp gdvs using gdvs_dynamics
num=`wc -l $weight_file | cut -d' ' -f 1`
StatTmpGDVS=$netName"-StatTmpGDVS.txt"
echo $num
echo "calculating stat-tmp gdvs"
./gdvs_dynamics $stattmpGDV $stattmpGDV $ofolder/$StatTmpGDVS $weight_file $num

## step2: compare static tmp gdv-s and dynamic gdv-s
echo "running staticTmp-vs-dynamic-gdvs now"
StaDynOutput=$netName"-StatTmpDyn-output.txt"
python static-vs-dynamic-gdvs.py -s $ofolder/$StatTmpGDVS -d $dynGDVS -o $ofolder/$StaDynOutput

## step3: plot the results of static-vs-dynamic-gdvs
gnufile=$netName"-StatTmpDyn.gnuplot"
gnuOutput=$netName"-StatTmpDyn.eps"

sed 's/testtest/'"$ofolder\/$StaDynOutput"'/g; s/XYZ/'"$ofolder\/$gnuOutput"'/g; s/networkname/'"$netName"'/g; s/Static/Static Temporal/g' static-dynamic-gdvs.gnuplot > $ofolder/$gnufile
gnuplot $ofolder/$gnufile

## step4: principal component analysis
#echo "running pca"
#pcaF=$netName"-StatTmpDyn-pca.txt"
#pcaOutput=$netName"-StatTmpDyn-pca.eps"
#pcaGNU=$netName"-StatTmpDyn-pca.gnuplot"
#Rscript pca.r $statGDV $dynGDV > $ofolder/$pcaF

#pcatmp=$pcaF"tmp"
## step5: plot the results of the principal component analysis
#awk '{if(NR!=1) print $3 "\t" $4}' $ofolder/$pcaF > $ofolder/$pcaF"tmp"

#sed 's/testtest/'"$ofolder\/$pcatmp"'/g; s/XYZ/'"$ofolder\/$pcaOutput"'/g; s/networkname/'"$netName"'/g' static-dynamic-pca.gnuplot > $ofolder/$pcaGNU
#gnuplot $ofolder/$pcaGNU
#rm $ofolder/$pcaF"tmp"

## step6: Pearson correlation
## step7: Spearman's rank correlation
echo "running correlation analysis"
corrF=$netName"-StatTmpDyn-corrSummary.txt"
Rscript correlation.r $ofolder/$StaDynOutput > $ofolder/$corrF






