+#
+# Tero Salminen 2013 for project Log-plotter
+# Timo Kokkonen http://git.itanic.dy.fi/?p=log-plotter
+#
+# This program is free software: you can redistribute it and/or modify
+# it under the terms of the GNU General Public License as published by
+# the Free Software Foundation, either version 3 of the License, or
+# (at your option) any later version.
+#
+# This program is distributed in the hope that it will be useful,
+# but WITHOUT ANY WARRANTY; without even the implied warranty of
+# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+# GNU General Public License for more details.
+#
+# You should have received a copy of the GNU General Public License
+# along with this program. If not, see <http://www.gnu.org/licenses/>.
+#
+# Commandline usage with example values unless ran from log-plotter
+# directly:
+# gnuplot -e "datafile='log.log' ; chg_name='iCharger 3010B' ; cell_cnt='6' ; sizex='800' ; sizey='480'" bal.plot
+
+set output "cell_bal.png"
+
+# Build title
+ttext1=" - Cell balance "
+ttext2="S battery"
+set title chg_name.ttext1.cell_cnt.ttext2
+
+# Setting the graphics size from log-plotter variables
+gsizex=sizex
+gsizey=sizey
+set term png size gsizex,gsizey
+
+# Setting layout and label & legends etc
+set lmargin 8
+set rmargin 8
+set tmargin 3
+set datafile separator ";"
+set key out bot hor center samplen 2
+set timestamp "%d.%m.%Y - %H:%M" top offset -1,-1 font ",8"
+set ylabel "Voltage [mV]"
+set y2label "Voltage [mV]"
+set y2tics border
+set grid y
+set xlabel "Time [minutes]"
+set format x "%.0f"
+
+# extract the 'time' cell for xtics based on tital time
+stats datafile every ::::0 using 3 nooutput
+time = int(STATS_min)
+
+if (time <= 120) set xtics 0.5 nomirror;
+if (time <= 300) set xtics 1 nomirror;
+if (time <= 900) set xtics 2 nomirror;
+if (time <= 1200) set xtics 3 nomirror;
+if (time <= 1800) set xtics 5 nomirror;
+if (time > 1800) set xtics 10 nomirror;
+
+# Count average-value for balance sero-line
+samples(x) = $0 > 4 ? 5 : ($0+1)
+avg5(x) = (shift5(x), (back1 + back2 + back3 + back4 + back5) / samples($0))
+shift5(x) = (back5 = back4, \
+ back4 = back3, \
+ back3 = back2, \
+ back2 = back1, \
+ back1 = x)
+init(x) = (back1 = back2 = back3 = back4 = back5 = sum = 0)
+
+# Draw the plot based on log-plotter's variable cell-count
+if (cell_cnt==1) plot sum=init(0), \
+ datafile using ($3/60):(avg5($7 - ($7 + $8) / cell_cnt)*100) with lines title "Cell 1";
+
+if (cell_cnt==2) plot sum=init(0), \
+ datafile using ($3/60):(avg5($7 - ($7 + $8) / cell_cnt)*100) with lines title "Cell 1", \
+ datafile using ($3/60):(avg5($8 - ($7 + $8) / cell_cnt)*100) with lines title "Cell 2";
+
+if (cell_cnt==3) plot sum=init(0), \
+ datafile using ($3/60):(avg5($7 - ($7 + $8 + $9) / cell_cnt)*100) with lines title "Cell 1", \
+ datafile using ($3/60):(avg5($8 - ($7 + $8 + $9) / cell_cnt)*100) with lines title "Cell 2", \
+ datafile using ($3/60):(avg5($9 - ($7 + $8 + $9) / cell_cnt)*100) with lines title "Cell 3";
+
+if (cell_cnt==4) plot sum=init(0), \
+ datafile using ($3/60):(avg5($7 - ($7 + $8 + $9 + $10) / cell_cnt)*100) with lines title "Cell 1", \
+ datafile using ($3/60):(avg5($8 - ($7 + $8 + $9 + $10) / cell_cnt)*100) with lines title "Cell 2", \
+ datafile using ($3/60):(avg5($9 - ($7 + $8 + $9 + $10) / cell_cnt)*100) with lines title "Cell 3", \
+ datafile using ($3/60):(avg5($10 - ($7 + $8 + $9 + $10) / cell_cnt)*100) with lines title "Cell 4";
+
+if (cell_cnt==5) plot sum=init(0), \
+ datafile using ($3/60):(avg5($7 - ($7 + $8 + $9 + $10 + $11) / cell_cnt)*100) with lines title "Cell 1", \
+ datafile using ($3/60):(avg5($8 - ($7 + $8 + $9 + $10 + $11) / cell_cnt)*100) with lines title "Cell 2", \
+ datafile using ($3/60):(avg5($9 - ($7 + $8 + $9 + $10 + $11) / cell_cnt)*100) with lines title "Cell 3", \
+ datafile using ($3/60):(avg5($10 - ($7 + $8 + $9 + $10 + $11) / cell_cnt)*100) with lines title "Cell 4", \
+ datafile using ($3/60):(avg5($11 - ($7 + $8 + $9 + $10 + $11) / cell_cnt)*100) with lines title "Cell 5";
+
+if (cell_cnt==6) plot sum=init(0), \
+ datafile using ($3/60):(avg5($7 - ($7 + $8 + $9 + $10 + $11 + $12) / cell_cnt)*100) with lines title "Cell 1", \
+ datafile using ($3/60):(avg5($8 - ($7 + $8 + $9 + $10 + $11 + $12) / cell_cnt)*100) with lines title "Cell 2", \
+ datafile using ($3/60):(avg5($9 - ($7 + $8 + $9 + $10 + $11 + $12) / cell_cnt)*100) with lines title "Cell 3", \
+ datafile using ($3/60):(avg5($10 - ($7 + $8 + $9 + $10 + $11 + $12) / cell_cnt)*100) with lines title "Cell 4", \
+ datafile using ($3/60):(avg5($11 - ($7 + $8 + $9 + $10 + $11 + $12) / cell_cnt)*100) with lines title "Cell 5", \
+ datafile using ($3/60):(avg5($12 - ($7 + $8 + $9 + $10 + $11 + $12) / cell_cnt)*100) with lines title "Cell 6";
+
+if (cell_cnt==7) plot sum=init(0), \
+ datafile using ($3/60):(avg5($7 - ($7 + $8 + $9 + $10 + $11 + $12 + $13) / cell_cnt)*100) with lines title "Cell 1", \
+ datafile using ($3/60):(avg5($8 - ($7 + $8 + $9 + $10 + $11 + $12 + $13) / cell_cnt)*100) with lines title "Cell 2", \
+ datafile using ($3/60):(avg5($9 - ($7 + $8 + $9 + $10 + $11 + $12 + $13) / cell_cnt)*100) with lines title "Cell 3", \
+ datafile using ($3/60):(avg5($10 - ($7 + $8 + $9 + $10 + $11 + $12 + $13) / cell_cnt)*100) with lines title "Cell 4", \
+ datafile using ($3/60):(avg5($11 - ($7 + $8 + $9 + $10 + $11 + $12 + $13) / cell_cnt)*100) with lines title "Cell 5", \
+ datafile using ($3/60):(avg5($12 - ($7 + $8 + $9 + $10 + $11 + $12 + $13) / cell_cnt)*100) with lines title "Cell 6", \
+ datafile using ($3/60):(avg5($13 - ($7 + $8 + $9 + $10 + $11 + $12 + $13) / cell_cnt)*100) with lines title "Cell 7";
+
+if (cell_cnt==8) plot sum=init(0), \
+ datafile using ($3/60):(avg5($7 - ($7 + $8 + $9 + $10 + $11 + $12 + $13 + $14) / cell_cnt)*100) with lines title "Cell 1", \
+ datafile using ($3/60):(avg5($8 - ($7 + $8 + $9 + $10 + $11 + $12 + $13 + $14) / cell_cnt)*100) with lines title "Cell 2", \
+ datafile using ($3/60):(avg5($9 - ($7 + $8 + $9 + $10 + $11 + $12 + $13 + $14) / cell_cnt)*100) with lines title "Cell 3", \
+ datafile using ($3/60):(avg5($10 - ($7 + $8 + $9 + $10 + $11 + $12 + $13 + $14) / cell_cnt)*100) with lines title "Cell 4", \
+ datafile using ($3/60):(avg5($11 - ($7 + $8 + $9 + $10 + $11 + $12 + $13 + $14) / cell_cnt)*100) with lines title "Cell 5", \
+ datafile using ($3/60):(avg5($12 - ($7 + $8 + $9 + $10 + $11 + $12 + $13 + $14) / cell_cnt)*100) with lines title "Cell 6", \
+ datafile using ($3/60):(avg5($13 - ($7 + $8 + $9 + $10 + $11 + $12 + $13 + $14) / cell_cnt)*100) with lines title "Cell 7", \
+ datafile using ($3/60):(avg5($14 - ($7 + $8 + $9 + $10 + $11 + $12 + $13 + $14) / cell_cnt)*100) with lines title "Cell 8";
+
+if (cell_cnt==9) plot sum=init(0), \
+ datafile using ($3/60):(avg5($7 - ($7 + $8 + $9 + $10 + $11 + $12 + $13 + $14 + $15) / cell_cnt)*100) with lines title "Cell 1", \
+ datafile using ($3/60):(avg5($8 - ($7 + $8 + $9 + $10 + $11 + $12 + $13 + $14 + $15) / cell_cnt)*100) with lines title "Cell 2", \
+ datafile using ($3/60):(avg5($9 - ($7 + $8 + $9 + $10 + $11 + $12 + $13 + $14 + $15) / cell_cnt)*100) with lines title "Cell 3", \
+ datafile using ($3/60):(avg5($10 - ($7 + $8 + $9 + $10 + $11 + $12 + $13 + $14 + $15) / cell_cnt)*100) with lines title "Cell 4", \
+ datafile using ($3/60):(avg5($11 - ($7 + $8 + $9 + $10 + $11 + $12 + $13 + $14 + $15) / cell_cnt)*100) with lines title "Cell 5", \
+ datafile using ($3/60):(avg5($12 - ($7 + $8 + $9 + $10 + $11 + $12 + $13 + $14 + $15) / cell_cnt)*100) with lines title "Cell 6", \
+ datafile using ($3/60):(avg5($13 - ($7 + $8 + $9 + $10 + $11 + $12 + $13 + $14 + $15) / cell_cnt)*100) with lines title "Cell 7", \
+ datafile using ($3/60):(avg5($14 - ($7 + $8 + $9 + $10 + $11 + $12 + $13 + $14 + $15) / cell_cnt)*100) with lines title "Cell 8", \
+ datafile using ($3/60):(avg5($15 - ($7 + $8 + $9 + $10 + $11 + $12 + $13 + $14 + $15) / cell_cnt)*100) with lines title "Cell 9";
+
+if (cell_cnt==10) plot sum=init(0), \
+ datafile using ($3/60):(avg5($7 - ($7 + $8 + $9 + $10 + $11 + $12 + $13 + $14 + $15 + $16) / cell_cnt)*100) with lines title "Cell 1", \
+ datafile using ($3/60):(avg5($8 - ($7 + $8 + $9 + $10 + $11 + $12 + $13 + $14 + $15 + $16) / cell_cnt)*100) with lines title "Cell 2", \
+ datafile using ($3/60):(avg5($9 - ($7 + $8 + $9 + $10 + $11 + $12 + $13 + $14 + $15 + $16) / cell_cnt)*100) with lines title "Cell 3", \
+ datafile using ($3/60):(avg5($10 - ($7 + $8 + $9 + $10 + $11 + $12 + $13 + $14 + $15 + $16) / cell_cnt)*100) with lines title "Cell 4", \
+ datafile using ($3/60):(avg5($11 - ($7 + $8 + $9 + $10 + $11 + $12 + $13 + $14 + $15 + $16) / cell_cnt)*100) with lines title "Cell 5", \
+ datafile using ($3/60):(avg5($12 - ($7 + $8 + $9 + $10 + $11 + $12 + $13 + $14 + $15 + $16) / cell_cnt)*100) with lines title "Cell 6", \
+ datafile using ($3/60):(avg5($13 - ($7 + $8 + $9 + $10 + $11 + $12 + $13 + $14 + $15 + $16) / cell_cnt)*100) with lines title "Cell 7", \
+ datafile using ($3/60):(avg5($14 - ($7 + $8 + $9 + $10 + $11 + $12 + $13 + $14 + $15 + $16) / cell_cnt)*100) with lines title "Cell 8", \
+ datafile using ($3/60):(avg5($15 - ($7 + $8 + $9 + $10 + $11 + $12 + $13 + $14 + $15 + $16) / cell_cnt)*100) with lines title "Cell 9", \
+ datafile using ($3/60):(avg5($16 - ($7 + $8 + $9 + $10 + $11 + $12 + $13 + $14 + $15 + $16) / cell_cnt)*100) with lines title "Cell 10";