#!/bin/bash

if ! which gnuplot > /dev/null 2>&1
then
    echo 'gnuplot not found - skipping graph creation' >&2
    exit 1
fi

gnuplot<<EOF

    set terminal pngcairo
    set grid
    set key outside below

    set style line 1 dt 2 lw 2 lc "black"
    set style line 2      lw 2 lc "green"

    TIME = system("foamListTimes -latestTime -case ..")

#---graph---
    FILE = "../postProcessing/graph/".TIME."/line.csv"
    RFILE = "reference/line.csv"

    set xlabel 'x [m]'
    set xrange [0:0.15]
    set yrange [0:]

    set output 'alphaAir_horizontal.png'
    set ylabel 'r_{air} [-]'
    plot RFILE u 1:(column("alpha.air")) w l ls 2 t "reference" , \
          FILE u 1:(column("alpha.air")) w l ls 1 t "current"

    set output 'UAir_horizontal.png'
    set ylabel 'U_{air} [m s^{-1}]'
    plot RFILE u 1:(column("U.air_y")) w l ls 2 t "reference"   , \
          FILE u 1:(column("U.air_y")) w l ls 1 t "current"

    set output 'kAir_horizontal.png'
    set ylabel 'k_{air} [m^2 s^{-2}]'
    plot RFILE u 1:(column("k.air")) w l ls 2 t "reference"     , \
          FILE u 1:(column("k.air")) w l ls 1 t "current"

    set autoscale xy


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

    set xlabel "t [s]"
    set logscale y
    set format y "10^{%.0T}"

    set output "residuals.png"
    set ylabel "initial residuals [-]"
    plot FILE u 1:2 w l t "p_{rgh}"             , \
         FILE u 1:3 w l t "k_{water}"           , \
         FILE u 1:4 w l t "{/Symbol w}_{water}"

EOF

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