#!/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 2
    set linetype 2 lc rgb "blue"   lw 2
    set linetype 3 lc rgb "green"  lw 2
    set linetype 4 lc rgb "black"  lw 2
    set linetype 5 lc rgb "orange" lw 2
    set linetype 6 lc rgb "gray"   lw 2
    set linetype 7 lc rgb "pink"   lw 2

    set grid
    set key outside below noenhanced
    set terminal pngcairo enhanced


    #---RESIDUALS---
    FLFILE= "../postProcessing/fluid/residualsFluid/0/residuals.dat"
    FIFILE= "../postProcessing/film/residualsFilm/0/residuals.dat"

    set     output "residuals.png"
    set     xlabel "t [s]"
    set     ylabel "initial residuals [-]"
    set     logscale y
    set     format y "10^{%.0T}"
    FLheaderline = system('head -2 '.FLFILE.' | tail -1')
    FIheaderline = system('head -2 '.FIFILE.' | tail -1')
    FLnumcols = words(FLheaderline) - 1
    FInumcols = words(FIheaderline) - 1
    plot for [col=2:FLnumcols] FLFILE u 1:col w l lt col dt 1 t "fluid - ".word(FLheaderline,col+1), \
         for [col=2:FInumcols] FIFILE u 1:col w l lt col dt 2 t "film - ".word(FIheaderline,col+1)
    set key outside below
    unset   logscale y
    set     format y "%g"


    #---CONDENSATION-RATE---
    FILE =      "../postProcessing/fluid/condensationRate/0/volFieldValue.dat"

    set output  "condensationRate.png"
    set ylabel  "dm/dt [g/s]"
    plot FILE u 1:(1e3*\$2) w l t "current" , \
                   2.06     w l ls 4 dt 2 t "Bucci (2009)"


    #---IN-OUT-FLOW---
    FLINFILE =  "../postProcessing/fluid/inflow/0/surfaceFieldValue.dat"
    FLOUFILE =  "../postProcessing/fluid/outflow/0/surfaceFieldValue.dat"
    FIOUFILE =  "../postProcessing/film/outflowFilm/0/surfaceFieldValue.dat"

    set output  "massFlowRate.png"
    set ylabel  "dm/dt [kg/s]"
    plot FLINFILE u 1:(-\$2) w l t "gas inlet", \
         FLOUFILE u 1:2      w l t "gas outlet" , \
         FIOUFILE u 1:2      w l t "film outlet"


    #---FILM-THICKNESS---
    PATH =      "../postProcessing/film/filmThickness/"

    LIST = system("ls -1 ".PATH." | sort -g")
    NUM = words(LIST)
    TIMES(n) = word(LIST,n)

    FILES(n) = PATH.TIMES(n)."/line.csv"

    set output  "filmThickness.png"
    set xlabel  "{/Symbol d} [{/Symbol m}m]"
    set ylabel  "y [m]"
    plot FILES(NUM) u (1e6*\$2):1 w l notitle

EOF

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