#!/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 "cyan"   lw 3 dt 1
    set linetype 3 lc rgb "red"    lw 3 dt 1
    set linetype 4 lc rgb "orange" 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] {
        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 3,1 margins 0.09,0.82,.07,.96 spacing 0,0 title "Bubble Column, t = ".sprintf("%1.3f", TIMES(i)+0)." s"

        y   = "y"
        rAC = "alpha.airC"
        rAD = "alpha.airD"
        rWC = "alpha.waterC"
        rWD = "alpha.waterD"
        UAC = "U.airC_y"
        UAD = "U.airD_y"
        UWC = "U.waterC_y"
        UWD = "U.waterD_y"

        unset xtics
        unset xlabel
        set ylabel "r_{/Symbol a}"
        set yrange [-0.05:1.05]
        plot FILE(i) u 1:(0                                  ):(column(rWC)                                    ) w filledcu t "waterC"  , \
             FILE(i) u 1:(column(rWC)                        ):(column(rWC)+column(rWD)                        ) w filledcu t "waterD"  , \
             FILE(i) u 1:(column(rWC)+column(rWD)            ):(column(rWC)+column(rWD)+column(rAD)            ) w filledcu t "airD"    , \
             FILE(i) u 1:(column(rWC)+column(rWD)+column(rAD)):(column(rWC)+column(rWD)+column(rAD)+column(rAC)) w filledcu t "airC"    , \
             FILE(i) u   (column(y)+0.01                     ):(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:(column(rWC)) w histeps t "waterC" , \
             FILE(i) u 1:(column(rWD)) w histeps t "waterD" , \
             FILE(i) u 1:(column(rAD)) w histeps t "airD"   , \
             FILE(i) u 1:(column(rAC)) w histeps t "airC"
        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:(column(UWC)) w histeps t "waterC" , \
             FILE(i) u 1:(column(UWD)) w histeps t "waterD" , \
             FILE(i) u 1:(column(UAD)) w histeps t "airD"   , \
             FILE(i) u 1:(column(UAC)) w histeps t "airC"

        unset multiplot
    }

    print 'Finished'

EOF

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