#! /bin/bash 
# Last edited on 2014-01-18 23:43:05 by stolfilocal

# Reads from {stdin} a file with lines "{X[I]} {YLO[I]} {YHI[I]}",
# plots them with appropriate scale.

show="$1"; shift;   # "SHOW" or "NOSHOW".
which="$1"; shift;  # Which value to plot ("lo", "hi", "mid", or "both").
xiorg="$1"; shift;  # Value to subtract from each {X[I]}.
xunit="$1"; shift;  # Plot units for each unit increment in input {X[i]}. 
xoorg="$1"; shift;  # Plot {X} value that corresponds to input {X==xiorg}. 
yiorg="$1"; shift;  # Value to subtract from each {YLO[I],YHI[I]}.
yunit="$1"; shift;  # Plot units for each unit increment in {YLO[I],YHI[I]}.
yoorg="$1"; shift;  # Plot {Y} value that corresponds to input {Y=yiorg}. 
ymin="$1"; shift;   # Min {Y} for plotting range.
ymax="$1"; shift;   # Max {Y} for plotting range.
title="$1"; shift;  # Plot title.

tmp="/tmp/$$"

# Copy the input data to a scratch file:
datafile="${tmp}.dat";  # Input data file.
cat \
  | egrep -v -e '^[ ]*([#]|$)' \
  > ${datafile}

export GDFONTPATH=.:..

gnuplot <<EOF
set term png size 2400,1600 font "courbd,24"
set output "${tmp}-full.png"
xiorg=${xiorg}
xunit=${xunit}
xoorg=${xoorg}
yiorg=${yiorg}
yunit=${yunit}
yoorg=${yoorg}
ymin=${ymin}
ymax=${ymax}
set title "${title}"

xval(dummy) = ((column(1)-xiorg)*xunit + xoorg)

ylo(dummy) = ((column(2)-yiorg)*yunit + yoorg)
yhi(dummy) = ((column(3)-yiorg)*yunit + yoorg)
ymid(dummy) = (0.5*(ylo(dummy)+yhi(dummy)))
 
set yrange [(ymin):(ymax)]
set logscale y
if (ymax/ymin <= 10) {
  set ytics ( \
    0.10, 0.11, 0.12, 0.15, 0.20, 0.25, 0.30, 0.35, 0.40, 0.45  0.50, 0.55, 0.60, 0.65, 0.70, 0.75, 0.80, 0.85, 0.90, 0.95, \
    1.0,  1.1,  1.2,  1.5,  2.0,  2.5,  3.0,  3.5,  4.0,  4.5,  5.0,  5.5,  6.0,  6.5,  7.0,  7.5,  8.0,  8.5,  9.0,  9.5,  \
    10,   11,   12,   15,   20,   25,   30,   35,   40,   45,   50,   55,   60,   65,   70,   75,   80,   85,   90,   95,   \
    100,  110,  120,  150,  200,  250,  300,  350,  400,  450,  500,  550,  600,  650,  700,  750,  800,  850,  900,  950,  \
    1000, 1100, 1200, 1500, 2000, 2500, 3000, 3500, 4000, 4500, 5000, 5500, 6000, 6500, 7000, 7500, 8000, 8500, 9000, 9500  \
  )
}
if (ymax/ymin > 10) {
  set ytics ( \
    0.10, 0.12, 0.15, 0.20, 0.25 1, 0.30, 0.40, 0.50 1, 0.60, 0.70 1, 0.80, 0.90 1, \
    1.0,  1.2,  1.5,  2.0,  2.5 1,  3.0,  4.0,  5.0 1,  6.0,  7.0 1,  8.0,  9.0 1,  \
    10,   12,   15,   20,   25 1,   30,   40,   50 1,   60,   70 1,   80,   90 1,   \
    100,  120,  150,  200,  250 1,  300,  400,  500 1,  600,  700 1,  800,  900 1,  \
    1000, 1200, 1500, 2000, 2500 1, 3000, 4000, 5000 1, 6000, 7000 1, 8000, 9000 1  \
  )
}
set grid ytics lt 1 lw 3 lc rgb '#ffddaa', lt 1 lw 1.5 lc rgb '#ffddaa'
set grid mytics

set xtics 1.0
set mxtics 12
set grid xtics lt 1 lw 3 lc rgb '#ffddaa', lt 1 lw 1.5 lc rgb '#ffddaa'
set grid mxtics
set xzeroaxis lt 1 lw 3 lc rgb '#ffddaa'
set key center top

if ("${which}" eq "lo") {
  plot "${datafile}" using (xval(0)):(ylo(0)) notitle with linespoints lt 1 lw 1.0 pt 7 ps 1.0 lc rgb '#002288' 
}
if ("${which}" eq "hi") {
  plot "${datafile}" using (xval(0)):(yhi(0)) notitle with linespoints lt 1 lw 1.0 pt 7 ps 1.0 lc rgb '#002288' 
}
if ("${which}" eq "mid") {
  plot "${datafile}" using (xval(0)):(ymid(0)) notitle with linespoints  lt 1 lw 1.0 pt 7 ps 1.0 lc rgb '#002288'
}
if ("${which}" eq "both") {
  set style fill solid 0.5 
  set boxwidth 0.75 relative
  plot "${datafile}" using (xval(0)):(ylo(0)):(ylo(0)):(yhi(0)):(yhi(0)) \
    notitle with candlesticks lt 1 lw 1.0 lc rgb '#3344ff'
}
quit
EOF

if [[ -s ${tmp}-full.png ]]; then
  plotfile="${tmp}.png"
  convert ${tmp}-full.png -resize '50%' ${plotfile}

  if [[ "/${show}" == "/SHOW" ]]; then
    display ${plotfile}
  fi

  cat ${plotfile}
else
  echo "** plot not generated" 1>&2
fi
rm -fv ${tmp}.*
    
        
