#! /bin/bash
# Last edited on 2015-02-27 23:36:01 by stolfilocal

# Plots the local mean prices of one or more exchanges from a joint summary file.
# 
# Arguments: {RUNDATE} {DMIN_FULL} {DMAX_FULL} {DMIN_PART} {DMAX_PART} {HRAD} {JOINFILE}
# 
# Where 
#   
#   {DMIN_FULL,DMAX_FULL} is the range of dates to show in full to plot;
#   {DMIN_PART,DMAX_PART} is the range of dates to show in the detail plot;
#   {HRAD} is the half-radius of the smoothing window;
#   {JOINFILE} is the joint price file.
#  
# The input file must contain one line per day in the 
# format "{DATE} {TIME} | {VBT[i,k]} | {VCR[i,k]} | {PMD[i,k]}" where
# the fields "| {VBT[i,k]} | {VCR[i,k]} | {PMD[i,k]}" are repeated for each exchange {k}.
# See {join_smoothed_price_files.gawk} for details.
# 

rundate="$1"; shift;     # Nominal date of analysis
dmin_full="$1"; shift;   # Min date for full plot.
dmax_full="$1"; shift;   # Max date for full plot.
dmin_part="$1"; shift;   # Min date for partial plot.
dmax_part="$1"; shift;   # Max date for partial plot.
hrad="$1"; shift;        # Half-radius for PMED smoohing.
joinFile="$1"; shift;    # Joint price file to plot.

outDir="out"

outPref="${outDir}/${rundate}-prices-01d-sm${hrad}"

tmp="/tmp/$$"

indexFile="${rundate}-file-index.dir"

# Split out the files again for plotting:
ixLines=( \
  ` gawk '/^[ ]*20[01][0-9][-]/ { gsub(/^[ ]+/,"",$0); gsub(/[ ]+$/,"",$0); gsub(/[ ]+/,":",$0); print; }' ${indexFile} ` \
)
kf=0
for ixLine in "${ixLines[@]}" ; do 
  # Split fields from index line:
  ixf=( `echo "${ixLine}" | tr ':' ' '` )
  if [[ ${#ixf[@]} -ne 9 ]]; then echo "** bad index line \"${ixLine}\"" 1>&2; exit 1; fi
  iniDate="${ixf[0]}"
  finDate="${ixf[1]}"
  extag="${ixf[2]}"
  crtag="${ixf[3]}"
  exname="${ixf[4]}"
  rate="${ixf[5]}"

  # Temp single-exchange file extracted from joint file:
  tmpFile="${tmp}-${iniDate}--${finDate}-${extag}-${crtag}-01d-sm${hrad}.txt"
  
  # Extract the three fields of file with index {kf}:
  echo "extracting ${tmpFile} ..." 1>&2
  cat ${joinFile} \
    | gawk -v kf=${kf} 'BEGIN { jf = 4 + 6*kf; } /^20/{ printf "%s %s | %s | %s | %s\n", $1, $2, $(jf), $(jf+2), $(jf+4); }' \
    > ${tmpFile}
    
  kf=$(( $kf + 1 ))
done

# Plot all prices together:
echo "plotting extracted files ..." 1>&2
make_jpeg="YES"
show_jpeg="NO"

for prange in \
  "${dmin_full}--${dmax_full}:0.04--1600" \
  "${dmin_part}--${dmax_part}:25.0--1600" \
; do
  drange="${prange%%:*}"
  vrange="${prange##*:}"
  dmin="${drange%%--*}"
  dmax="${drange##*--}"
  vmin="${vrange%%--*}"
  vmax="${vrange##*--}"
  if [[ 10#${hrad} -eq 0 ]]; then
    wintit=""
  else
    hwid=$(( 2 * 10#${hrad} + 1 )); # Total width of smoothing window.
    wintit=" (smoothed with ${hwid}-day Hann window)"
  fi
  
  # File names for joint price plots:
  plotPref="${outPref}-${dmin}--${dmax}"; # Plot file prefix.
  pngFile="${plotPref}.png"
  jpgFile="${plotPref}.jpg"

  rm -f ${pngFile} ${jpgFile}
  plot_prices.sh \
      "Daily prices ${dmin} to ${dmax}${wintit}" \
      180 6  \
      "${dmin}" "${dmax}" \
      ${vmin} ${vmax} \
      "YES" \
      ` cat ${indexFile} \
         | gawk -v pref="${tmp}" -v hrad=${hrad} \
             ' /^20/{ printf "%s-%s--%s-%s-%s-01d-sm%s.txt 8 %s %s %s\n", pref,$1,$2,$3,$4,hrad,$5,$6,$9; }'
      ` \
    > ${pngFile}
  if [[ ( -s ${pngFile} ) && ( "/${make_jpeg}" == "/YES" ) ]]; then
    convert ${pngFile} -quality 85 -resize '600x' ${jpgFile}
    ls -l ${pngFile} ${jpgFile}
    if [[ "/${show_jpeg}" == "/YES" ]]; then display ${jpgFile} ; fi
  fi
done

rm -fv ${tmp}-${iniDate}--${finDate}*.{png,txt}
    
        
