#! /bin/bash
# Last edited on DATE TIME by USER

# Plots pressure of drywell against core pressure, colored by time interval

# Arguments:
rix="$1"; shift # Reactor index (1, 2, or 3)
xpr="$1"; shift # Which pressure to use on X axis (0 = core, 1 = drywell, 2 = torus)
ypr="$1"; shift # Which pressure to use on Y axis (0 = core, 1 = drywell, 2 = torus)

fpr="$1"; shift # Formula F for Y as a function of X, e.g. "1.2*(X+70)"
gpr="$1"; shift # Formula G for Y as a function of X, e.g. "1.2*(X+70)"

tma="$1"; shift # Tansition time A (hours since mar/11 00:00)
tmb="$1"; shift # Tansition time B (hours since mar/11 00:00)
tmc="$1"; shift # Tansition time C (hours since mar/11 00:00)
tmd="$1"; shift # Tansition time D (hours since mar/11 00:00)

# Internal variables:

utag="un${rix}"

pr_to_tag=( "PC" "PD" "PS" )
pr_to_tit=( "reactor core" "drywell" "suppression chamber" )

xtag="${pr_to_tag[${xpr}]}"
xtit="${pr_to_tit[${xpr}]}"
echo "${xtag}" "${xtit}"  1>&2

ytag="${pr_to_tag[${ypr}]}"
ytit="${pr_to_tit[${ypr}]}"
fteo="${ytag} = ${fpr/X/${xtag}}"
gteo="${ytag} = ${gpr/X/${xtag}}"
echo "${ytag}" "${ytit}" 1>&2
echo "${fteo}" 1>&2
echo "${gteo}" 1>&2

# Label style:

showlabs=0
labsty="left rotate by (-45) font \"arial,12\" offset 1,-0.7 tc rgb '#333333'"

if [[ showlabs -ne 0 ]]; then
  labplot="\"data-${utag}.txt\" using (dat(xc)):(dat(yc)):5 notitle with labels ${labsty},"
else
  labplot=""
fi

tmp="/tmp/$$"

pngfile="pcor-${xpr}-${ypr}-${utag}-full.png"
  
export GDFONTPATH=..

echo "plotting..." 1>&2

gnuplot <<EOF
  set term png font arial 18 size 1440,1440
  set size ratio -1
  set output "${tmp}.png" 
  set xrange [0:1013]; set xtics 100; set mxtics 10; set grid xtics
  set yrange [0:1013]; set ytics 100; set mytics 10; set grid ytics
  set title "Absolute pressures"
  set xlabel "${xtit} pressure ${xtag} (kPa)"
  set ylabel "${ytit} pressure ${ytag} (kPa)"
  set key left Left reverse
  xc = 9 + 2*${xpr};
  yc = 9 + 2*${ypr};
  tm0 = 0;
  tma = ${tma};
  tmb = ${tmb};
  tmc = ${tmc};
  tmd = ${tmd};
  tm9 = 100000;
  fpr(X) = ${fpr};
  gpr(X) = ${gpr};
  col(k) = column(k)
  dat(k) = ((col(k) == 99999) || (col(k) == 88888) ? 0/0 : col(k))
  din(k,t,a,b) = ((col(t) < a) || (col(t) > b) ? 0/0 : dat(k))
  plot \
    fpr(x)                                                 title "${fteo}"        with lines                   lc rgb '#4477aa', \
    gpr(x)                                                 title "${gteo}"        with lines                   lc rgb '#aa7744', \
    "data-${utag}.txt" using (dat(xc)):(dat(yc))           notitle                with lines                   lc rgb '#444444', \
    ${labplot} \
    "data-${utag}.txt" using (dat(xc)):(din(yc,5,tm0,tma)) title "before ${tma}"  with 	    points pt 7 ps 1.5 lc rgb '#cc0000', \
    "data-${utag}.txt" using (dat(xc)):(din(yc,5,tma,tmb)) title "${tma}--${tmb}" with 	    points pt 7 ps 1.5 lc rgb '#aa8800', \
    "data-${utag}.txt" using (dat(xc)):(din(yc,5,tmb,tmc)) title "${tmb}--${tmc}" with 	    points pt 7 ps 1.5 lc rgb '#339900', \
    "data-${utag}.txt" using (dat(xc)):(din(yc,5,tmc,tmd)) title "${tmc}--${tmd}" with 	    points pt 7 ps 1.5 lc rgb '#0088ff', \
    "data-${utag}.txt" using (dat(xc)):(din(yc,5,tmd,tm9)) title "after ${tmd}"   with 	    points pt 7 ps 1.5 lc rgb '#4400ff'
quit
EOF

echo "reducing..." 1>&2

convert ${tmp}.png -resize '50%' ${pngfile}

display ${pngfile}

rm -f ${tmp}.png

echo "done." 1>&2