#!/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 "red"    lw 3 dt 1
    set linetype 2 lc rgb "blue"   lw 3 dt 1
    set linetype 3 lc rgb "orange" lw 3 dt 1
    set linetype 4 lc rgb "grey"   lw 2 dt 3    # reference
    set linetype 5 lc rgb "grey"   lw 2 dt 4
    set linetype 6 lc rgb "black"  lw 2 dt 2    # analytical

    set grid
    set key outside below noenhanced
    set terminal pngcairo enhanced

#---residuals---
    FILE = "../postProcessing/bulk/residuals/0/residuals.dat"

    set output "residuals.png"
    set xlabel "t [s]"
    set ylabel "Initial residuals [-]"
    set logscale y
    set format y "10^{%.0T}"
    plot FILE u 1:2 w l t "p_rgh"

    unset logscale y
    set format y "%g"

#---layer-average---
    BFILE = "../postProcessing/bulk/layerAverage/1/layerAverage.csv"
    FFILE = "../postProcessing/film/graph/1/line.csv"
    RBFILE= "reference/layerAverage.csv"
    RFFILE= "reference/line.csv"
    AFILE = "reference/analyticalFilmProfile.csv"

    set terminal pngcairo enhanced size 1200,500
    set output "filmThickness.png"

    set xlabel "y [m]"
    set ylabel "Film Thickness [mm]"

    set datafile separator whitespace
    plot BFILE                      u (column("x")):(1e3*column("alpha.liquid")*1e-3)                     w l lt 1 t "Bulk"             , \
         FFILE                      u (column("x")):(1e3*column("delta"))                                 w l lt 2 t "Film"             , \
         RBFILE                     u (column("x")):(1e3*column("alpha.liquid")*1e-3)                     w l lt 4 t "Reference - bulk" , \
         RFFILE                     u (column("x")):(1e3*column("delta"))                                 w l lt 5 t "Reference - film" , \
         AFILE                      u (column("x")):(1e3*column("delta"))                                 w l lt 6 t "Analytical"       , \
         "< paste ".BFILE." ".FFILE u (column("x")):(1e3*(column("alpha.liquid")*1e-3 + column("delta"))) w l lt 3 t "Total"

EOF

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