#!/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 1000,1000
    FType = '.png'

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

    FILEA(n) =  "../postProcessing/graphA/".TIMES(n)."/line.csv"
    FILEB(n) =  "../postProcessing/graphB/".TIMES(n)."/line.csv"

    #find extrema
    ALLCOLS =   "3 6 9"
    COLS(n) =   word(ALLCOLS, n)
    set autoscale
    stats  FILEB(1) using int(COLS(1)) nooutput name "BOUNDS_"
    ymin = BOUNDS_min
    ymax = BOUNDS_max
    do for [ COL in ALLCOLS] {
        do for [i=1:NUM] {
            stats FILEB(i) using int(COL) nooutput name "BOUNDS_"

            if (BOUNDS_min < ymin) {
                ymin = BOUNDS_min
            } else {}

            if (BOUNDS_max > ymax) {
                ymax = BOUNDS_max
            } else {}
        }
    }

    print 'ymin: ', ymin
    print 'ymax: ', ymax


    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 3,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.5] FILEA(i) u 1:(\$3*0) :3              w filledcu t "water", \
                     FILEA(i) u 1:3      :(\$3+\$4)       w filledcu t "oil", \
                     FILEA(i) u 1:(\$3+\$4):(\$3+\$4+\$5) w filledcu t "airD", \
                     FILEA(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 FILEA(i) u 1:3 w histeps lt 1 t "water", \
             FILEA(i) u 1:4 w histeps lt 2 t "oil", \
             FILEA(i) u 1:5 w histeps lt 3 t "airD"

        set xtics
        set xlabel  "y"
        set ylabel "U_{/Symbol a}"
        set yrange [-0.1:0.3] # ymin:ymax
        plot FILEB(i) u 1:3 w histeps lt 1 t "water", \
             FILEB(i) u 1:6 w histeps lt 2 t "oil", \
             FILEB(i) u 1:9 w histeps lt 3 t "airD"

        unset multiplot
    }

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

    FFILE(n) =  "../postProcessing/graph-blending/".TIMES(n)."/line.csv"

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

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

        set output  "blendingFactors".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 FILEA(i) u 1:3 w histeps lt 1 t "water", \
             FILEA(i) u 1:4 w histeps lt 2 t "oil", \
             FILEA(i) u 1:5 w histeps lt 3 t "airD"

        set ylabel  "f1D2.airD\\\_water"
        set yrange  [-0.05:1.05]
        plot [0:0.5] FFILE(i) u 1:2 w histeps lt 6 notitle

        set ylabel  "f1D2.airD\\\_oil"
        plot [0:0.5] FFILE(i) u 1:3 w histeps lt 6 notitle

        set ylabel  "fG.water\\\_oil"
        set xtics
        set xlabel  "y"
        plot [0:0.5] FFILE(i) u 1:4 w histeps lt 6 notitle

        unset multiplot
    }

EOF

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