#! /bin/bash
# Last edited on 2011-04-15 18:59:33 by stolfi

# Plots water level and pressure for one #Fukushima reactor.

rix="$1"; shift # Index of reactor (1,2,3)

tmp="/tmp/$$"

tag="un${rix}"
  
export GDFONTPATH=..

gnuplot <<EOF

set term png font arial 18 size 2400,2600
set lmargin 7; set rmargin 7
set output "${tmp}.png" 
set xrange [0:886]
unset title
set xtics ( \
  "        3/11"   0 0, \
  "        3/12"  24 0, \
  "        3/13"  48 0, \
  "        3/14"  72 0, \
  "        3/15"  96 0, \
  "        3/16" 120 0, \
  "        3/17" 144 0, \
  "        3/18" 168 0, \
  "        3/19" 192 0, \
  "        3/20" 216 0, \
  "        3/21" 240 0, \
  "        3/22" 264 0, \
  "        3/23" 288 0, \
  "        3/24" 312 0, \
  "        3/25" 336 0, \
  "        3/26" 360 0, \
  "        3/27" 384 0, \
  "        3/28" 408 0, \
  "        3/29" 432 0, \
  "        3/30" 456 0, \
  "        3/31" 480 0, \
  "        4/01" 504 0, \
  "        4/02" 528 0, \
  "        4/03" 552 0, \
  "        4/04" 576 0, \
  "        4/05" 600 0, \
  "        4/06" 624 0, \
  "        4/07" 648 0, \
  "        4/08" 672 0, \
  "        4/09" 696 0, \
  "        4/10" 720 0, \
  "        4/11" 744 0, \
  "        4/12" 768 0, \
  "        4/13" 792 0, \
  "        4/14" 816 0, \
  "        4/15" 840 0, \
  "        4/16" 864 0, \
  "        4/17" 888 0, \
  "        4/18" 912 0, \
  "        4/19" 936 0, \
  "        4/20" 960 0 \
)
set grid xtics
set grid ytics
# set key left
keytime =   648 # Time coordinate when plot key starts.
norcorp =  6690 # Norml core operating pressure (kPa)
maxdryp =   485 # Max drywell design pressure (kPa)
maxtemp =   400 # Upper limit of temperature sensor scale (C).
atmosph =   100 # Atmospheric pressure (kPa).
boomord =     0 # Ordinate of explosion mark.
qty(z) = (z == 88888 ? 0/0 : (z == 99999 ? 0/0 : z))

# Explosion markers
bum(z) = ((z != 1) ? 0/0 : 0)       # Explosion mark for linear plots.
bup(z) = ((z != 1) ? 0/0 : atmosph) # Explosion mark for log plots (pressure).
buc(z) = ((z != 1) ? 0/0 : 1)       # Explosion mark for log plots (CAMS).

# Fire/smoke markers
fum(z) = ((z != 2) ? 0/0 : 0)       # Smoke mark for linear plots.
fup(z) = ((z != 2) ? 0/0 : atmosph) # Smoke mark for log plots (pressure).
fuc(z) = ((z != 2) ? 0/0 : 1)       # Smoke mark for log plots (CAMS).

set multiplot layout 1,1 title "Fukushima Dai-ichi Reactor #${rix}"
# ----------------------------------------------------------------------
# WATER INPUT
set origin 0.00, 0.00
set size 1.00,0.17
set yrange [-1:790]; set y2range [-1:790]
unset logscale y; unset logscale y2
set ytics 100; set y2tics 100
set mytics 2; set my2tics 2
set format y "%5.0f"
set format y2 "%-5.0f"
set grid mytics
set xzeroaxis linetype -1 linewidth 1.5
plot \
  "data-${tag}.txt" using 5:(bum(column(15))) notitle                              with linespoints pt 13 ps 4.0 lw 0.0 lc rgb '#ff5500', \
  "data-${tag}.txt" using 5:(fum(column(15))) notitle                              with linespoints pt  7 ps 0.5 lw  18 lc rgb '#777777', \
  "flow-${tag}.txt" using 5:(qty(column(7)))  title "Water injection rate (l/min)" with      points pt  7 ps 2.0 lw 1.5 lc rgb '#008888'
# ----------------------------------------------------------------------
# CORE WATER LEVEL
set origin 0.00, 0.16
set size 1.00,0.17
set yrange [-3500:4500]; set y2range [-3500:4500]
unset logscale y; unset logscale y2
set ytics 1000; set y2tics 1000
set mytics 2; set my2tics 2
set format y "%5.0f"
set format y2 "%-5.0f"
set grid mytics
set xzeroaxis linetype -1 linewidth 1.5
plot \
  "data-${tag}.txt" using 5:(bum(column(15)))   notitle                          with linespoints pt 13 ps 4.0 lw 0.0 lc rgb '#ff5500', \
  "data-${tag}.txt" using 5:(fum(column(15)))   notitle                          with linespoints pt  7 ps 0.5 lw  18 lc rgb '#777777', \
  "data-${tag}.txt" using 5:(qty(column(7)))    title "Water level (mm)"         with linespoints pt  7 ps 1.3 lw 1.5 lc rgb '#0044ff'
# ----------------------------------------------------------------------
# CAMS RADIATION READINGS
set origin 0.00, 0.32
set size 1.00,0.17
set yrange [0.24:2400]; set y2range [0.24:2400]
set logscale y; set logscale y2
set ytics ( \
  0.5, \
  1, \
  2.5, \
  5, \
  10, \
  25, \
  50, \
  100, \
  250, \
  500, \
  1000, \
  2500, \
  5000 \
)
set y2tics ( \
  0.5, \
  1, \
  2.5, \
  5, \
  10, \
  25, \
  50, \
  100, \
  250, \
  500, \
  1000, \
  2500, \
  5000 \
)
set mytics 2; set my2tics 2
set format y "%5.1f"
set format y2 "%-5.1f"
set grid mytics
# set xzeroaxis linetype -1 linewidth 1.5
plot \
  "data-${tag}.txt" using 5:(buc(column(15)))   notitle                           with linespoints pt 13 ps 4.0 lw 0.0 lc rgb '#ff5500', \
  "data-${tag}.txt" using 5:(fuc(column(15)))   notitle                           with linespoints pt  7 ps 0.5 lw  18 lc rgb '#777777', \
  "cams-${tag}.txt" using 5:(qty(column(7))/100) title "Drywell radiation (Sv/h)" with linespoints pt  7 ps 1.3 lw 1.5 lc rgb '#ff8800', \
  "cams-${tag}.txt" using 5:(qty(column(9))/100) title "Torus radiation (Sv/h)"   with linespoints pt  7 ps 1.3 lw 1.5 lc rgb '#ff0088'
# ----------------------------------------------------------------------
# TEMPERATURES
set origin 0.00, 0.49
set size 1.00,0.17
set yrange [-49.0:590.0]; set y2range [-49.0:590.0]
unset logscale y; unset logscale y2
set format x ""
set ytics 100; set y2tics 100
set mytics 2; set my2tics 2
set format y "%5.0f"
set format y2 "%-5.0f"
set grid mytics
set xzeroaxis linetype -1 linewidth 1.5
# set key left Left reverse
plot \
  (x < keytime ? maxtemp : 0/0)                 title "Topscale temp. (C)"   with lines                    lw 3.5 lc rgb '#ff7755', \
  "data-${tag}.txt" using 5:(bum(column(15)))   notitle                      with linespoints pt 13 ps 4.0 lw 0.0 lc rgb '#ff5500', \
  "data-${tag}.txt" using 5:(fum(column(15)))   notitle                      with linespoints pt  7 ps 0.5 lw  18 lc rgb '#777777', \
  "heat-${tag}.txt" using 5:(qty(column(7)))    title "Core noz. temp. (C)"  with linespoints pt  7 ps 1.3 lw 1.5 lc rgb '#aa1100', \
  "heat-${tag}.txt" using 5:(qty(column(9)))    title "Core bot. temp. (C)"  with linespoints pt  7 ps 1.3 lw 1.5 lc rgb '#ff1100'
# ----------------------------------------------------------------------
# PRESSURES
set origin 0.00, 0.66
set size 1.00,0.32
set yrange [3.16:31600.0]; set y2range [3.16:31600.0]
set logscale y; set logscale y2
set ytics ( \
  0.5, \
  1, \
  2.5, \
  5, \
  10, \
  25, \
  50, \
  100, \
  250, \
  500, \
  1000, \
  2500, \
  5000, \
  10000, \
  25000, \
  50000 \
)
set y2tics ( \
  0.5, \
  1, \
  2.5, \
  5, \
  10, \
  25, \
  50, \
  100, \
  250, \
  500, \
  1000, \
  2500, \
  5000, \
  10000, \
  25000, \
  50000 \
)
set mytics 1; set my2tics 1
set format y "%6.0f"
set format y2 "%-6.0f"
set grid nomytics; set grid ytics
set xzeroaxis linetype -1 linewidth 1.5
plot \
  (atmosph)                                    title "Atmospheric pressure"      with lines                    lw 3.5 lc rgb '#77aaff', \
  (maxdryp)                                    title "Max drywell pressure"      with lines                    lw 3.5 lc rgb '#cc9977', \
  (x < keytime ? norcorp : 0/0)                title "Operating core pressure"   with lines                    lw 3.5 lc rgb '#55aa55', \
  "data-${tag}.txt" using 5:(bup(column(15)))  title "Explosion"                 with linespoints pt 13 ps 4.0 lw 0.0 lc rgb '#ff5500', \
  "data-${tag}.txt" using 5:(fup(column(15)))  title "Dark smoke"                with linespoints pt  7 ps 0.5 lw  18 lc rgb '#777777', \
  "data-${tag}.txt" using 5:(qty(column(13)))  title "Torus pressure (kPa)"      with linespoints pt  6 ps 2.5 lw 1.5 lc rgb '#6688ff', \
  "data-${tag}.txt" using 5:(qty(column(11)))  title "Drywell pressure (kPa)"    with linespoints pt  7 ps 1.5 lw 1.5 lc rgb '#993300', \
  "data-${tag}.txt" using 5:(qty(column(9)))   title "Core pressure (kPa)"       with linespoints pt  7 ps 1.3 lw 1.5 lc rgb '#008800'
# ----------------------------------------------------------------------
unset multiplot
quit
EOF

convert ${tmp}.png -resize '50%' plot-${tag}-full.png

display plot-${tag}-full.png

rm -f ${tmp}.png
