#! /bin/bash
# Last edited on 2014-10-21 18:17:52 by stolfilocal

outDir="$1"; shift;
ycol="$1"; shift;  # Which column from data file to print.
ymin="$1"; shift;  # Min value for Y scale.
ymax="$1"; shift;  # Max value for Y scale.
title="$1"; shift; # Which column from data file to print.
dtdts=() # Date intervals to plot.
nf=0
while [[ $# -ge 1 ]]; do
  dtdts[${nf}]="$1"; shift;
  nf=$(( ${nf} + 1 ))
done
printf "%d dtdts\n" "${nf}" 1>&2
if [[ $# -gt 0 ]]; then 
  echo "** unpaired arg \"$1\"" 1>&2 ; exit 1
fi

show=SHOW
color=( '#ff0000' '#dd4400' '#886600' '#668800' '#008800' '#0066ff' '#0022ff' '#4400ff' '#8800dd' '#dd0077' '#ff0077' )

# Compute color increment:
nc=${#color[@]}
dc=$(( ${nc} / ${nf} ))
if [[ ${dc} -lt 1 ]]; then dc=1; fi

tmp="/tmp/$$"
 
pngfull="${tmp}-full.png"
pngout="${tmp}.png"
rm -f ${pngfull} ${pngout} 

# Create the gnuplots command file:
rm -f ${tmp}.gpl
sep="plot (1) notitle with lines lc rgb '#777777',"
km=0; # Month index
kc=0; # Color index
while [[ ${km} -lt ${nf} ]]; do
  dtdt="${dtdts[${km}]}";
  file="${outDir}/${dtdt}-hourly.txt"
  clr="${color[${kc}]}"
  printf "%s \\\\\n" "${sep}" >> ${tmp}.gpl
  printf "  \"%s\" using 1:%d \\\\\n" "${file}" "${ycol}" >> ${tmp}.gpl
  printf "    title '%s' with linespoints pt 7 ps 2.0 lt 1 lw 2.0 lc rgb '%s'" "${dtdt}" "${clr}" >> ${tmp}.gpl
  sep=','
  km=$(( ${km} + 1 ))
  # Get the next color:
  kc=$(( ${kc} + ${dc} ))
  if [[ ${kc} -ge ${#color[@]} ]]; then kc=0; fi
done
printf "\n" >> ${tmp}.gpl

export GDFONTPATH=.
 
gnuplot <<EOF
set term png size 1200,1200 font "courbd,20"
set output "${pngfull}"
set title "${title}" 

set xrange [-0.6:+23.6]
set xtics 1
unset mxtics
set grid xtics lt 1 lw 3 lc rgb '#ffddaa', lt 1 lw 2.0 lc rgb '#ffddaa'
set xlabel "Hour of day (UTC)"
ymin=${ymin}
ymax=${ymax}

set yrange [(ymin):(ymax)]
set logscale y
if ((ymin > 0.90) && (ymax < 1.1)) {
  set ytics ( \
    0.900, 0.905, 0.910, 0.915, 0.920, 0.925, 0.930, 0.935, 0.940, 0.945, \
    0.950, 0.955, 0.960, 0.965, 0.970, 0.975, 0.980, 0.985, 0.990, 0.995, \
    1.000, 1.005, 1.010, 1.015, 1.020, 1.025, 1.030, 1.035, 1.040, 1.045, \
    1.050, 1.055, 1.060, 1.065, 1.070, 1.075, 1.080, 1.085, 1.090, 1.095 \
  )
  set format y "%.3f"
} else {
  set ytics ( \
    0.1, 0.15, 0.2, 0.25, 0.3, 0.4, 0.5, 0.6, 0.7, 0.8, 0.9, \
    1.0, 1.5, 2.0, 2.5, 3.0, 4.0, 5.0, 6.0, 7.0, 8.0, 9.0, \
    10.0, 15.0, 20.0, 25.0, 30.0, 40.0, 50.0, 60.0, 70.0, 80.0, 90.0, \
    100.0, 150.0, 200.0, 250.0, 300.0, 400.0, 500.0, 600.0, 700.0, 800.0, 900.0, \
    1000.0, 1500.0, 2000.0, 2500.0, 3000.0, 4000.0, 5000.0, 6000.0, 7000.0, 8000.0, 9000.0, \
    10000.0, 15000.0, 20000.0, 25000.0, 30000.0, 40000.0, 50000.0, 60000.0, 70000.0, 80000.0, 90000.0, \
    100000.0, 150000.0, 200000.0, 250000.0, 300000.0, 400000.0, 500000.0, 600000.0, 700000.0, 800000.0, 900000.0, \
    1000000.0 \
  )
}
set grid ytics lt 1 lw 3 lc rgb '#ffddaa', lt 1 lw 2.0 lc rgb '#ffddaa'
set grid mytics

set key left

load "${tmp}.gpl"

EOF

if [[ -s ${pngfull} ]]; then 
  convert ${pngfull} -resize '50%' ${pngout}
  if [[ "/${show}" == "/SHOW" ]]; then
    display ${pngout}
  fi
  cat ${pngout}
fi

rm -f ${tmp}-* ${tmp}.*
