#!/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 grid
    set key outside right

    #---PNG---
    set term pngcairo size 1000,1000
    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"

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

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

        set multiplot layout 3,1 margins 0.09,0.82,.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 FILE(i) u 1:(\$3*0) :3              w filledcu t "water", \
             FILE(i) u 1:3      :(\$3+\$4)       w filledcu t "airC", \
             FILE(i) u 1:(\$3+\$4):(\$3+\$4+\$5) w filledcu t "airD", \
             FILE(i) u (\$1+0.0025):(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:3 w histeps lt 1 t "water", \
             FILE(i) u 1:4 w histeps lt 2 t "airC", \
             FILE(i) u 1:5 w histeps lt 3 t "airD"
        set ytics
        unset y2tics
        unset y2label

        set xtics
        set xlabel  "y"
        set ylabel "U_{/Symbol a}"
        set yrange [-0.1:0.3]
        plot FILE(i) u 1:7  w histeps lt 1 t "water", \
             FILE(i) u 1:10 w histeps lt 2 t "airC", \
             FILE(i) u 1:13 w histeps lt 3 t "airD"

        unset multiplot
    }

    print 'Finished'

EOF

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