#!/bin/bash

diagn=v_r
shotnobase=177
referenceshot=17743 # for Uloop graph
R=50
#xrange=[-45:15]
#yrange=[-1.5*1000/$R:10*1000/$R]
xrange=[-45:45]
yrange=[-1.5*1000/$R:22.5*1000/$R]
param=param_2
paramunits=V
ylabel=\$U_R\$
seq1="`seq 39 46`" # positive part of the VA char
seq2="`seq 48 62`" # negative part of the VA char
timewindow=10
initframe=14000
finalframe=28000
shotstart=17100
shotend=20800
ysize=0.56;xsizecom=0.44;ysizesm=0.5


j=18000
PTH=""

#Is(1-exp([V-Vf]/Te))

gnuplot -e ";set terminal postscript enhanced color portrait size 30,18  font 'Helvetica,30';set out 'image.eps';\
set size 1,1;set title \"$title avg <$j,$((j+$timewindow))> us\";set multiplot;\
set size $ysize,0.9;set origin 0,0.1;set xrange "$xrange";set yrange "$yrange";set xlabel 'V_{bias} [V]'; set ylabel 'I_R [mA]';set xzeroaxis;set yzeroaxis;
set label 1 '#17739'  at -40,-135 rotate left;
set label 2 '#17740'  at -35,-135 rotate left;
set label 3 '#17741'  at -30,-135 rotate left;
set label 4 '#17742'  at -25,-135 rotate left;
set label 5 '#17743'  at -20,-135 rotate left;
set label 6 '#17744'  at -15,-135 rotate left;
set label 7 '#17745'  at -10,-135 rotate left;
set label 8 '#17746'  at -5,-135 rotate left;
set label 9 '#17747'  at 0,-135 rotate left;
set label 10 ':'  at 4,-70 rotate left;
set label 11 ':'  at 8,-70 rotate left;
set label 12 '#17762'  at 14,-135 rotate left;
if ( $j > $shotstart && $j < $shotend ) { stats '"$PTH"IndData/"$j"fit.dat' using 2;plot '"$PTH"IndData/"$j"raw.dat' using 1:(\$2*1000) title 'raw exp. data' ps 2,`grep "$j" "$PTH"Dpndcs/langmuirfit|awk '{print "STATS_max*1000*(exp("$2"*(tanh((x+"$3")/abs("$4"))))-"$5")"}'` title \"Aazoz fit\" w l,-0.0142*1000*(1-exp((x-(-18.466))/12.744)) title \"3 parameter fit\"} else {plot '"$PTH"IndData/"$j"raw.dat' using 1:(\$2*1000) title 'raw exp. data'};\
set size $xsizecom*7/12,$ysizesm*4/6;set origin 0.05,0.5;set title '#"$referenceshot" U_{loop}';set xrange [12:38];set yrange [0:20];set xtics; set ytics 0,5,20;set xlabel 't [ms]';set ylabel '';unset arrow;set grid;set arrow from $j/1000.0,0 to $j/1000.0,20 nohead lc rgb 'brown';plot '"$PTH"Data/"$referenceshot"Uloop' using (\$1*1000):(\$2*5.5) w l t '';\
if ( $j > $shotstart && $j < $shotend ) { set size $xsizecom,$ysizesm;set origin $ysize,0;set title '';unset arrow;set xrange [0:40];set yrange [0:13];set xtics;set ytics 0,3,13;set xlabel 'Electron Energy [eV]';set ylabel 'F(E) [x10^{14} eV^{-1}m^{-3}]';unset label;plot '"$PTH"IndData/"$j"eedf.dat' using 1:(\$2/1e14) w l t 'Experimental EEDF','"$PTH"IndData/"$j"fm.dat' using 1:(\$2/1e14) w l t 'Fit to Maxwell-Boltzman','"$PTH"IndData/"$j"fd.dat' using 1:(\$2/1e14) w l t 'Fit to Druyvesteyn';}\
set size $xsizecom,$ysizesm;set origin $ysize,$ysizesm;set title '';set xrange [17:21];set yrange [0:25];set y2range [0:25];set xtics 17,1,21; set ytics 0,5,25;set y2tics;set xlabel 't [ms]';set ylabel 'T_e [eV]';set y2label 'n [x10^{17}]';unset arrow;set grid;if ( $j > $shotstart && $j < $shotend ) {set arrow from $j/1000.0,0 to $j/1000.0,25 nohead lc rgb 'brown'};plot '"$PTH"Dpndcs/Te' using (\$1/1000):2 w l t 'T_e','"$PTH"Dpndcs/Ni' using (\$1/1000):(\$2/1e17) w l t 'n_i' axes x1y2,'"$PTH"Dpndcs/Ne' using (\$1/1000):(\$2/1e17) w l t 'n_e' axes x1y2;\
unset multiplot;set out"
convert image.eps $j.jpg