#! /bin/bash
# Last edited on 2014-03-18 20:28:26 by stolfilocal

# Plots the slumber volume ratios {Vh/Vd} for each day
# 
# Arguments: {TITLE} {WH0} {WH1} {WH2} {REFS} {FNAME}
# 
# Where 
#   
#   {TITLE} is the plot's title;
#   {WH0,WH1,WH2} are weights for the mean slumber volume.
#   {REFS} is the soft threshold for {Vh/Vd}.
#   {FNAME} is a data file with one line per slumber time in format
#      "{DATE} {TIME} | {NDAY} | {QL} | {HUBI.VH0} | {HUBI.VH1} | {HUBI.VH2} | {HUBI.VD}"

title="$1"; shift  # Plot title.
wh0="$1"; shift    # Weight of {Vh0} in mean.
wh1="$1"; shift    # Weight of {Vh1} in mean.
wh2="$1"; shift    # Weight of {Vh2} in mean.
refS="$1"; shift   # Soft {Vd/Vd} threshold.  
fname="$1"; shift  # Input file name. 

show="SHOW"

tmp="/tmp/$$"

timg="${tmp}-full.png"

color=( '#0022ff' '#ff0000' '#008800' '#8800dd' '#dd4400' '#0066ff' )

export GDFONTPATH=.:..

gnuplot <<EOF
# ----------------------------------------------------------------------
# Common definitions:

set timefmt "%Y-%m-%d %H:%M:%S" # For {timecolumn}.
set lmargin 10
set rmargin 4
set bmargin 2.5
set tmargin 2.5

set xtics mirror
set xdata time
set grid xtics lt 1 lw 3 lc rgb '#ffddaa', lt 1 lw 1.5 lc rgb '#ffddaa'
set grid mxtics
set format x "%b/%d"

# Scaling factor for hourly volumes to match their means to that of {Vh1}:
rh0 = 2.05
rh1 = 1.00
rh2 = 0.78

# Weights to compute vhmed:
wh0 = ${wh0} # Scaling for {Vh0}. 
wh1 = ${wh1} # Scaling for {Vh1}. 
wh2 = ${wh2} # Scaling for {Vh2}.

refS = ${refS} # Soft threshold for {Vh/Vd}

load "plot_slumber_funcs.gnuplot"

# Mean slumber volume, excluding zero volumes (NaN if all weights*volumes are zero): 
vhmed(Vh0,Vh1,Vh2) = (wh0*Vh0 + wh1*Vh1 + wh2*Vh2)/((Vh0==0 ? 0 : wh0) + (Vh1==0 ? 0 : wh1) + (Vh2==0 ? 0 : wh2))

# Plottable volumes from column numbers: 
vhplot(ih,rh) = ((rh == 0) || (column(ih) == 0) ? 0/0 : column(ih)/rh)
vmplot(ih0,ih1,ih2) = vhmed(column(ih0),column(ih1),column(ih2))

# Plottable volumes relative to total daily volume, from column numbers:
rvhplot(ih,rh,id) = ((refS == 0) ? 0/0 : (vhplot(ih,rh)/column(id))/refS)
rvmplot(ih0,ih1,ih2,id) = ((refS == 0) ? 0/0 : (vmplot(ih0,ih1,ih2)/column(id))/refS)

set term png size 2400,600 font "courbd,18"
set output "${timg}"
set title "${title} - slumber volumes 02:00--04:59 CST"

set logscale y
set yrange [0.009:1001.0]
set grid ytics lt 1 lw 3 lc rgb '#ffddaa', lt 1 lw 1.5 lc rgb '#ffddaa'
set grid mytics

set key top right

plot \
  (1.00) notitle with lines lt 1 lw 2.0 lc rgb '#4466ff', \
  "< grep -e '^20' ${fname}" using (timecolumn(1)+20000):(vhplot(22,1.00)) title "Vd" \
    with impulses lt 1 lw 3.0 lc rgb '#44aa00', \
  ""  using (timecolumn(1)-10000):(rvhplot(16,rh0,22)) title "Vh(02:30)/Vd" \
    with impulses lt 1 lw 3.0 lc rgb '#aa0088', \
  ""  using (timecolumn(1)+00000):(rvhplot(18,rh1,22)) title "Vh(03:30)/Vd" \
    with impulses lt 1 lw 3.0 lc rgb '#ff0000', \
  ""  using (timecolumn(1)+10000):(rvhplot(20,rh2,22)) title "Vh(04:30)/Vd" \
    with impulses lt 1 lw 3.0 lc rgb '#aa6600', \
  ""  using (timecolumn(1)-20000):(rvmplot(16,18,20,22)) title "Vm/Vd" \
    with impulses lt 1 lw 3.0 lc rgb '#555555'

quit
EOF

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

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

cat ${oimg}

rm -fv ${tmp}{-*,}.*
    
        
