#!/bin/bash
BASEDIR="../../"
source $BASEDIR/Commons.sh


function DoShotHomepage()
{
    if [ ! -z "`cat ../../Production/Parameters/ScanDefinition`" ]; then  echo Doing Scan; MakeScan;fi
    $LogFunctionBegin
    php homepage.php > index_.html 2 #> ErrorLog
    cp index_.html $BASEDIR/index.html
    cp homepage.php homepage_php.txt
    $LogFunctionEnd
}


function MakeScan()
{

  start=$(for i in `cat ../../Production/Parameters/ScanDefinition|sed 's/,/ /g' `; do echo $i;done|head -1);
  end=$(cat ../../shot_no);
  mkdir Scan
  echo -n "set terminal postscript enhanced color; set datafile separator ',';set key left;set xlabel 'Time [s]';set ylabel 'U_{loop} [V]';set title 'U_{loop}' ;plot" > Scan/script.gp
  echo  "set terminal postscript enhanced color; set datafile separator ',';set xlabel 'ShotNo [#]';set bmargin 0;set tmargin 0;set lmargin 10;set rmargin 3;unset xlabel;set multiplot layout 4,1 columnsfirst title 'GOLEM Shots';set xrange [$((start-1)):$((end+1))];set yrange [*:*];set style data dots;"> Scan/ValuesScript.gp
    cat tmp|gnuplot 1>/dev/null 2>/dev/null
   	convert -resize 200x200 ScreenShotAll.png rawdata.jpg

  for i in `cat ../../Production/Parameters/ScanDefinition|sed 's/,/ /g' ` `cat ../../shot_no`; do 
    echo -n " '< wget -q -O - http://golem.fjfi.cvut.cz/shots/$i/Diagnostics/BasicDiagnostics/U_Loop.csv' u 1:2 w l title '$i'," >>Scan/script.gp
    for j in t_plasma_duration U_loop_mean Ip_max Bt_max;do
      echo -n "$i," >> Scan/$j; wget -q -O - http://golem.fjfi.cvut.cz/shots/$i/Diagnostics/BasicDiagnostics/Results/$j >> Scan/$j;echo "" >> Scan/$j 
    done
  done
  
  gnuplot -c Scan/script.gp > Scan/U_loop.eps
  gs -dNOPAUSE -r300 -sDEVICE=jpeg -dBATCH -sOutputFile=Scan/U_loop.jpg Scan/U_loop.eps
  mogrify -rotate 90 Scan/U_loop.jpg
  
  #echo -n "plot " >> Scan/ValuesScript.gp
  for j in t_plasma_duration Ip_max Bt_max;do
      echo -n "unset xtics;set ylabel '$j';plot 'Scan/$j' u 1:2 t '' w p ps 4;" >> Scan/ValuesScript.gp
  done
  for j in U_loop_mean;do
      echo -n "set xtics;set xtics rotate;set ylabel '$j';plot 'Scan/$j' u 1:2 t '' w p ps 4;" >> Scan/ValuesScript.gp
  done
  
  gnuplot -c Scan/ValuesScript.gp > Scan/Values.eps
  gs -dNOPAUSE -r300 -sDEVICE=jpeg -dBATCH -sOutputFile=Scan/Values.jpg Scan/Values.eps
  mogrify -rotate 90 Scan/Values.jpg
    
    echo "<br></br><br></br><table border=1>" >Scan/diags.html
   for i in `cat ../../Production/Parameters/ScanDefinition|sed 's/,/ /g' ` `cat ../../shot_no`; do 
       echo "<tr><td colspan=100%><a href=/shots/$i>#$i</a>&nbsp;&nbsp;&nbsp;&nbsp `cat /golem/database/operation/shots/$i/Production/Parameters/CommandLine`</td></tr><tr>" >>Scan/diags.html
      for j in `cat ../../Production/OnStage_wave`; do
	echo "<td><a href=/shots/$i/`dirname $j`/analysis.html><img src=/shots/$i/`dirname $j`/graph.png></img></a></td>" >> Scan/diags.html
      done
      echo "</tr>" >>Scan/diags.html
   done
   echo "</table>" >> Scan/diags.html

}


# Development tools:
#cd /dev/shm/golem/ActualShot/Analysis/Homepage/;cp /home/svoboda/Dirigent/Analysis/Homepage/homepage.php .;source Homepage.sh ;DoShotHomepage ;ll
# shotno=34306;cd /golem/shots/$shotno/analysis_wave_0/Homepage/;cp /home/svoboda/Dirigent/Analysis/Homepage/homepage.php .;source Homepage.sh ;PostDischargeFinals


#shotno=35763; cp -r /golem/svoboda/Dirigent/Analysis/Homepage/*.* /golem/database/operation/shots/$shotno/Analysis/Homepage/;cd /golem/database/operation/shots/$shotno/Analysis/Homepage;source Homepage.sh ;DoShotHomepage;

