#!/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 "black"  lw 3 dt 2
    set linetype 5 lc rgb "black"  lw 3 dt 3
    set linetype 6 lc rgb "black"  lw 3 dt 4

    set grid
    set key outside right

    set term pngcairo size 1000,1000

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

    do for [i=1:NUM] {

        print(FILE(i))

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

        set multiplot layout 3,1 margins 0.09,0.82,.07,.96 spacing 0,0.04 title "t = ".sprintf("%1.3f", TIMES(i)+0)." s"

        unset xlabel

        set ylabel "r_{/Symbol a} [-]"
        set yrange [-0.05:1.05]
        r1 = "alpha.airC"
        r2 = "alpha.waterD"
        plot FILE(i) u 1:(column(r1)*0         ):(column(r1)                      ) w filledcu lt 2 t "air"      , \
             FILE(i) u 1:(column(r1)           ):(column(r1)+column(r2)           ) w filledcu lt 3 t "droplets"

        set ylabel "r_{/Symbol a} [-]"
        set yrange [-0.05:1.05]
        plot FILE(i) u 1:(column("alpha.airC"  )) w histeps lt 2 t "air"      , \
             FILE(i) u 1:(column("alpha.waterD")) w histeps lt 3 t "droplets"

        set xlabel  "y [m]"
        set ylabel "U_{/Symbol a} [m/s]"
        set yrange [-0.05:1.05]
        plot FILE(i) u 1:(column("U.airC_x"  )) w histeps lt 2 t "air"      , \
             FILE(i) u 1:(column("U.waterD_x")) w histeps lt 3 t "droplets"

        unset multiplot
    }

    #---comparison to reference---
    FILERES=FILE(NUM)
    FILEREF="./line.csv"

    set output  "alphaUReference.png"

    set multiplot layout 2,1 margins 0.09,0.82,.07,.96 spacing 0,0.04 title "last vs. reference"

    unset xlabel

    set ylabel "r_{/Symbol a} [-]"
    set yrange [-0.05:1.05]
    plot FILERES u 1:(column("alpha.airC"  )) w histeps lt 2 t "air"          , \
         FILERES u 1:(column("alpha.waterD")) w histeps lt 3 t "droplets"     , \
         FILEREF u 1:(column("alpha.airC"  )) w histeps lt 5 t "ref air"      , \
         FILEREF u 1:(column("alpha.waterD")) w histeps lt 6 t "ref droplets"

    set xlabel  "y [m]"
    set ylabel "U_{/Symbol a} [m/s]"
    set yrange [-0.05:1.05]
    plot FILERES u 1:(column("U.airC_x"  )) w histeps lt 2 t "air"          , \
         FILERES u 1:(column("U.waterD_x")) w histeps lt 3 t "droplets"     , \
         FILEREF u 1:(column("U.airC_x"  )) w histeps lt 5 t "ref air"      , \
         FILEREF u 1:(column("U.waterD_x")) w histeps lt 6 t "ref droplets"

    unset multiplot
    set key inside auto
    set autoscale xy

    #---water-volume---
    FILE="../postProcessing/bulk/volumeWater/0/volFieldValue.dat"

    system("sed -i 's/^# Time/Time/' ".FILE)

    set output  "volumeDroplets.png"

    set term pngcairo size 800,600
    set grid

    epsilon = 50
    rho = 997
    Lx = 0.01
    Lz = 0.0005

    entrainmentVolumeRate = epsilon*Lx*Lz/rho

    set xlabel "time [s]"
    set ylabel "volume [m^3]"

    plot FILE u 1:(column('volIntegrate(alpha.waterD)')) w l lt 3 notitle

    print 'Finished'

EOF

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