#! /bin/bash
# Last edited on 2011-06-01 17:06:02 by stolfilocal

# Plots the NISA for one Fukushima reactor, comparing the NISA and TEPCO data.

rix="$1"; shift;     # Index of reactor (1,2,3).
tag="$1"; shift;     # Tag for plot file name ("I" for first month, "T" for full history).
vix="$1"; shift;     # Index of variable to plot (1,2,...).
vtitle"$1"; shift;   # Name of variable {vix}.
sourceA="$1"; shift; # Data source A suffix ('t', 'n', etc.).
sourceB="$1"; shift; # Data source B suffix ('t', 'n', etc.).
width="$1"; shift;   # Width in pixels before downscaling.
xmin="$1"; shift;    # Initial time coordinate (better if midnight).
xmax="$1"; shift;    # Final time coordinate (better if just before midnight).
xtics="$1"; shift;   # X tics option ("daily" for now).

tmp="/tmp/$$"

unk="un${rix}"
unka="${unk}-${sourceA}"
unkb="${unk}-${sourceB}"

titleA="${title (by ${sourceA})}"
titleB="${title (by ${sourceB})}"

height=702

pngfile="out/plot-pres-${unk}-${sourceA}-${sourceB}-${tag}-full.png"
  
export GDFONTPATH=..

gnuplot <<EOF

set term png font arial 14 size ${width},${height}
set xrange [${xmin}:${xmax}]
set output "${tmp}.png" 

# Special pressures:
norcorp =  6690 # Norml core operating pressure (kPa).
maxcorp =  8700 # Max core operating pressure (kPa).
maxdryp =   485 # Max drywell design pressure (kPa).
atmosph =   100 # Atmospheric pressure (kPa).

ymin = 3.16     # Min plot pressure (kPa).
ymax = 75000.0  # Max plot pressure (kPa).

yelo = ymin*1.05  # Min ordinate of event bar (kPa).
yehi = ymax/1.05  # Max ordinate of event bar (kPa).

ytopscale = 100000.0   # Assumed top of instrument's scale (kPa).
ybotscale =      1.0   # Assumed bottom of instrument's scale (kPa).

# Ordinate of explosion and smoke mark:
boomord = atmosph 

load "plot-common.gnuplot"
load "xtics-${xtics}.gnuplot"

set logscale y; set logscale y2
set ytics ( \
  0.5, \
  1, \
  2, \
  5, \
  10, \
  20, \
  50, \
  100, \
  200, \
  500, \
  1000, \
  2000, \
  5000, \
  10000, \
  20000, \
  50000, \
  100000, \
  200000 \
)
set y2tics ( \
  0.5, \
  1, \
  2, \
  5, \
  10, \
  20, \
  50, \
  100, \
  200, \
  500, \
  1000, \
  2000, \
  5000, \
  10000, \
  20000, \
  50000, \
  100000, \
  200000 \
)
set mytics 1; set my2tics 1
set format y "%7.0f"
set format y2 "%-7.0f"
set grid nomytics; set grid ytics
set xzeroaxis linetype -1 linewidth 1.5
plot \
  "boom-${unk}.txt" using 5:(bum(column(7))):(yelo):(yehi)   title "Explosion"   with yerrorbars pt 13 ps 2.0 lw  6.0 lc rgb '#ff8844', \
  "boom-${unk}.txt" using 5:(fum(column(7))):(yelo):(yehi)   title "Dark smoke"  with yerrorbars pt  7 ps 0.0 lw  8.0 lc rgb '#ccbbaa', \
  (atmosph)                                    title "Atmospheric pressure"      with lines                    lw 3.0 lc rgb '#aaccff', \
  (maxdryp)                                    title "Max drywell pressure"      with lines                    lw 3.0 lc rgb '#99cc99', \
  (norcorp)                                    title "Operating core pressure"   with lines                    lw 3.0 lc rgb '#ddaaaa', \
  (maxcorp)                                    title "Max core pressure"         with lines                    lw 3.0 lc rgb '#ffaaaa', \
  "pres-${unka}.txt" using 5:(qty(column(7)))  title "${titleA} (kPa)"     with linespoints pt 6 ps 3.5 lw 1.8 lc rgb  '#aa0066', \
  "pres-${unkb}.txt" using 5:(qty(column(9)))  title "${titleB} (kPa)"     with linespoints pt 7 ps 2.8 lw 1.8 lc rgb  '#ff0088'
quit
EOF

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

display ${pngfile}

rm -f ${tmp}.png
