#!/bin/bash

if ! command -v gnuplot > /dev/null 2>&1
then
    echo 'gnuplot not found - skipping graph creation' >&2
    exit 1
else
    mkdir -p ../postProcessing/report
fi

gnuplot<<EOF

r1    = 0.012115
Y0    = 1.6

set linetype 1 lc rgb "navy"       lw 2 pt 5 ps 1.5 pi 2      # multiMorph - airC
set linetype 2 lc rgb "orange-red" lw 2 pt 4 ps 1.5 pi 2      # multiMorph - airD
set linetype 3 lc rgb "black"      lw 3 dt 2                  # reference
set linetype 4 lc rgb "white"      lw 1 pt 7 ps 0.8           # experiment
set linetype 5 lc rgb "black"      lw 1 pt 7 ps 0.5           # experiment

MCLABEL = "Continuous air"
MDLABEL = "Disperse air"
ELABEL  = "Chanson et al. (2004)"

PATH    = "../postProcessing/"
RPATH   = "reference/"

set grid

#---PNG---
set term pngcairo
FType = '.png'


#---volumeAirD---
FILE = "volumeAirD/0/volFieldValue.dat"

set output "../postProcessing/report/volumeAirD".FType

set xlabel "t [s]"
set ylabel "{/Symbol S} r_D [-]"

plot  PATH.FILE u 1:2 w l ls 2 t "current"   , \
     RPATH.FILE u 1:2 w l ls 3 t "reference"


#---volumeUnderwaterAirCAirD---
FILE = "volumeUnderwaterAirCAirD/0/volFieldValue.dat"

set output "../postProcessing/report/volumeUnderwaterAirCAirD".FType
set key top left

set title "Underwater: r < 0.215, y < 1.45"
set ylabel "{/Symbol S} r_{/Symbol a} [-]" offset 1

plot PATH.FILE u 1:(0*\$2):2         w filledcu ls 1 title MCLABEL, \
     PATH.FILE u 1:2      :(\$2+\$3) w filledcu ls 2 title MDLABEL

unset title


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

AFILE(a, n) = "graph".a."/".TIMES(n)."/line.csv"
EFILE(a)    = "2004_Chanson_et_al/Model1/voidFraction_line".a.".dat"

#---verticalUMean.water---
set output "../postProcessing/report/UMean".FType

set xlabel "y [m]"
set ylabel "U_{water,z} [m/s]"

set autoscale xy
set key top right

plot  PATH.AFILE("Vertical", NUM) u (Y0 - \$1):4 w l ls 1 t "current"  , \
     RPATH.AFILE("Vertical", NUM) u (Y0 - \$1):4 w l ls 3 t "reference"


#---PNG---
set term pngcairo size 1000,400

#---horizontalAirCAirD---

set xrange [0:0.125]
set yrange [0:0.4]

set xtics 0.05
set ytics 0.1


#---Mean---
set output "../postProcessing/report/alphaAirMean".FType
set multiplot layout 1,3 margins 0.07,0.99,0.15,0.9 spacing 0.015

set xlabel "r [m]"
set format x "%.1g"
set ylabel "r_{/Symbol a} [-]"
set format y "%.1f"
unset key

set title "Line A"
plot  PATH.AFILE("A",NUM) u (column("x")):(column("alphaMean.airC")*0):(column("alphaMean.airC"))                            w filledcu ls 1 title MCLABEL, \
      PATH.AFILE("A",NUM) u (column("x")):(column("alphaMean.airC"))  :(column("alphaMean.airC") + column("alphaMean.airD")) w filledcu ls 2 title MDLABEL, \
     RPATH.AFILE("A",NUM) u (column("x")):(column("alphaMean.airC"))                            w l ls 3 title "reference"   , \
     RPATH.AFILE("A",NUM) u (column("x")):(column("alphaMean.airC") + column("alphaMean.airD")) w l ls 3 notitle             , \
     EFILE("A")     u (\$1*r1):2     w p ls 4 notitle, \
     EFILE("A")     u (\$1*r1):2     w p ls 5 title ELABEL

unset ylabel
set format y ""

set title "Line B"
plot  PATH.AFILE("B",NUM) u (column("x")):(column("alphaMean.airC")*0):(column("alphaMean.airC"))                            w filledcu ls 1 title MCLABEL, \
      PATH.AFILE("B",NUM) u (column("x")):(column("alphaMean.airC"))  :(column("alphaMean.airC") + column("alphaMean.airD")) w filledcu ls 2 title MDLABEL, \
     RPATH.AFILE("B",NUM) u (column("x")):(column("alphaMean.airC"))                            w l ls 3 title "reference"   , \
     RPATH.AFILE("B",NUM) u (column("x")):(column("alphaMean.airC") + column("alphaMean.airD")) w l ls 3 notitle             , \
     EFILE("B")     u (\$1*r1):2     w p ls 4 notitle, \
     EFILE("B")     u (\$1*r1):2     w p ls 5 title ELABEL

set key

set title "LineC"
plot  PATH.AFILE("C",NUM) u (column("x")):(column("alphaMean.airC")*0):(column("alphaMean.airC"))                            w filledcu ls 1 title MCLABEL, \
      PATH.AFILE("C",NUM) u (column("x")):(column("alphaMean.airC"))  :(column("alphaMean.airC") + column("alphaMean.airD")) w filledcu ls 2 title MDLABEL, \
     RPATH.AFILE("C",NUM) u (column("x")):(column("alphaMean.airC"))                            w l ls 3 title "reference"   , \
     RPATH.AFILE("C",NUM) u (column("x")):(column("alphaMean.airC") + column("alphaMean.airD")) w l ls 3 notitle             , \
     EFILE("C")     u (\$1*r1):2     w p ls 4 notitle, \
     EFILE("C")     u (\$1*r1):2     w p ls 5 title ELABEL

unset multiplot

EOF

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