#!/bin/bash

if ! which gnuplot > /dev/null 2>&1
then
    echo 'gnuplot not found - skipping graph creation' >&2
    exit 1
fi

gnuplot<<EOF
    set linetype 1 lc rgb "blue"   lw 3 dt 1
    set linetype 2 lc rgb "orange" lw 3 dt 1
    set linetype 3 lc rgb "red"    lw 3 dt 1

    set linetype 4 lc rgb "magenta"lw 5 dt 1
    set linetype 5 lc rgb "green"  lw 4 dt 1
    set linetype 6 lc rgb "black"  lw 2 dt 1

    set grid
    set key outside right

    #---PNG---
    set term pngcairo size 800,600
    FType = '.png'

    #---sizeDistribution---
    PATH =  "../postProcessing/probes/0/"

    set output 'sizeDistributionOutlet'.FType

    set xlabel 't'
    set ylabel 'f_i'

    plot 1                  t 'reference'   , \
         PATH.'f3.air1' w l t 'f3'          , \
         PATH.'f9.air2' w l t 'f9'

    #---PNG---
    set term pngcairo size 1000,1200
    FType = '.png'

    #---graph---
    LIST =      system("ls -1 ../postProcessing/graph/ | sort -g")
    NUM =       words(LIST)
    TIMES(n) =  word(LIST,n)

    FILE(n) =  "../postProcessing/graph/".TIMES(n)."/line.csv"

    set xrange [0:0.3]

    set tmargin 0
    set bmargin 0
    set lmargin 1
    set rmargin 1

    do for [i=1:NUM] {
        if ( (i-1)%10 == 0) {
            print sprintf("t = %1.2f s", TIMES(i)+0)
        }
        else {}

        set output  "alphaU".sprintf("%04i", i+0).FType

        set multiplot layout 4,1 margins 0.09,0.87,.07,.96 spacing 0,0 title "Bubble Column, t = ".sprintf("%1.3f", TIMES(i)+0)." s"

        unset xtics
        unset xlabel
        set ylabel "r_{/Symbol a}"
        set yrange [-0.05:1.05]
        plot [0:0.3] FILE(i) u 1:(\$4*0) :4              w filledcu t "water", \
                     FILE(i) u 1:4      :(\$4+\$2)       w filledcu t "air1" , \
                     FILE(i) u 1:(\$4+\$2):(\$4+\$2+\$3) w filledcu t "air2" , \
                     FILE(i) u (\$1+0.002):(1)           w impulse lc rgb "grey" lw 0.5 notitle

        set ylabel "r_{/Symbol a}"
        set yrange [-0.05:1.05]
        plot FILE(i) u 1:4 w histeps lt 1 t "water", \
             FILE(i) u 1:2 w histeps lt 2 t "air1" , \
             FILE(i) u 1:3 w histeps lt 3 t "air2"

        set ylabel "U_{/Symbol a}"
        set yrange [-0.1:0.3]
        plot FILE(i) u 1:12 w histeps lt 1 t "water", \
             FILE(i) u 1:6  w histeps lt 2 t "air1" , \
             FILE(i) u 1:9  w histeps lt 3 t "air2"

        set xtics
        set xlabel  "y"
        set ylabel "T_{/Symbol a}"
        set yrange [298:301]
        #set yrange [:]
        plot FILE(i) u 1:16 w histeps lt 1 t "water", \
             FILE(i) u 1:14 w histeps lt 2 t "air1" , \
             FILE(i) u 1:15 w histeps lt 3 t "air2"

        unset multiplot
    }
EOF

#------------------------------------------------------------------------------
