#! /bin/bash
# Last edited on 2014-02-15 19:24:48 by stolfilocal

# 05 pixels = 01:00

function pscale(){
  # Prints to {stdout} the V-coordinate corresponing to a given price.
  
  # Plot scaling parameters:
  pmin="$1"; shift # Low reference price.
  vbot="$1"; shift # V-coordinate of the low reference price.
  pmax="$1"; shift # High reference price.
  vtop="$1"; shift # V-coordinate of the high reference price.
  
  parg="$1"; shift # Given price.
  
  echo "enter pscale ${pmin} ${vbot} ${pmax} ${vtop} ${parg}" 1>&2 

  pdel=$(( ${pmax} - ${pmin} ));
  prnd=$(( ${pdel} / 2 ))
  
  varg=$(( ${vbot} - ( ( ( ${parg} - ${pmin} ) * ( ${vbot} - ${vtop} ) + ${prnd} ) / ${pdel} ) ))
  
  printf "%d\n" "${varg}"
  echo "exit pscale ${varg}" 1>&2 
}

function boxcom(){
  # Prints to {stdout} the draw commands for a prediction box with label.
  
  # Plot scaling parameters:
  pmin="$1"; shift # Low reference price.
  vbot="$1"; shift # V-coordinate of the low reference price.
  pmax="$1"; shift # High reference price.
  vtop="$1"; shift # V-coordinate of the high reference price.
  
  ttarg="$1"; shift # Label: UTC time when prediction was to be fulfilled, or "@" to omit the label.
  htarg="$1"; shift # H-coordinate corresponding to ${ttarg}.
  ptarg="$1"; shift # Predicted price at ${ttarg}.
  perr="$1"; shift  # V-radius of confidence interval.

  echo "enter boxcom ${pmin} ${vbot} ${pmax} ${vtop} ${ttarg} ${htarg} ${ptarg} ${perr}" 1>&2 
  
  vtarg_top=`pscale ${pmin} ${vbot} ${pmax} ${vtop} $(( ${ptarg} + ${perr} ))`
  vtarg_bot=`pscale ${pmin} ${vbot} ${pmax} ${vtop} $(( ${ptarg} - ${perr} ))`
  htarg_lft=$(( ${htarg} - 3 ))
  htarg_rht=$(( ${htarg} + 3 ))

  if [[ "/${ttarg}" != "/@" ]]; then 
    vtarg_arr=$(( ${vtarg_top} - 150 ))
    vtarg_lab=$(( ${vtarg_arr} -  60 ))

    printf "%s %s\n" "-color" "#33ddff"
    printf "%s %s\n" "-strokewidth" "2"
    printf "%s %s %s  %s %s %s\n" "-L" "${htarg}" "${vtarg_arr}"  "${htarg}" "${vtarg_lab}"  "${ttarg}"
    tcolor="#0088ff"
  else
    tcolor="#aabbcc"
  fi
  
  printf "%s %s\n" "-color" "${tcolor}"
  printf "%s %s\n" "-strokewidth" "2"
  printf "%s %s %s  %s %s  %s %s  %s %s\n" "-Q" \
    "${htarg_rht}" "${vtarg_top}" \
    "${htarg_rht}" "${vtarg_bot}" \
    "${htarg_lft}" "${vtarg_bot}" \
    "${htarg_lft}" "${vtarg_top}" 
  echo "exit boxcom" 1>&2 
}     

function annot(){
  # Annotate one image.
  
  ex="$1"; shift    # Exchange code
  
  pmin="$1"; shift # Low reference price.
  vbot="$1"; shift # V-coordinate of the low reference price.
  pmax="$1"; shift # High reference price.
  vtop="$1"; shift # V-coordinate of the high reference price.
  
  tpred="$1"; shift # UTC time when last prediction was issued.
  hpred="$1"; shift # H-coordinate corresponding to ${tpred}.
  vpred="$1"; shift # V-coordinate just above plot at ${tprev}.
  
  ttarg1="$1"; shift # UTC time when prediction 1 was to be fulfilled, or "@".
  htarg1="$1"; shift # H-coordinate corresponding to ${ttarg}.
  ptarg1="$1"; shift # V-coordinate for predicted price at ${ttarg}.
  perr1="$1"; shift  # V-radius of confidence interval.
  
  ttarg2="$1"; shift # UTC time when prediction 2 was to be fulfilled, or "@".
  htarg2="$1"; shift # H-coordinate corresponding to ${ttarg}.
  ptarg2="$1"; shift # V-coordinate for predicted price at ${ttarg}.
  perr2="$1"; shift  # V-radius of confidence interval.
  
  ttarg3="$1"; shift # UTC time when prediction 3 was to be fulfilled, or "@".
  htarg3="$1"; shift # H-coordinate corresponding to ${ttarg}.
  ptarg3="$1"; shift # V-coordinate for predicted price at ${ttarg}.
  perr3="$1"; shift  # V-radius of confidence interval.
  
  ttarg4="$1"; shift # UTC time when prediction 4 was to be fulfilled, or "@".
  htarg4="$1"; shift # H-coordinate corresponding to ${ttarg}.
  ptarg4="$1"; shift # V-coordinate for predicted price at ${ttarg}.
  perr4="$1"; shift  # V-radius of confidence interval.
  
  ttarg5="$1"; shift # UTC time when prediction 5 was to be fulfilled, or "@".
  htarg5="$1"; shift # H-coordinate corresponding to ${ttarg}.
  ptarg5="$1"; shift # V-coordinate for predicted price at ${ttarg}.
  perr5="$1"; shift  # V-radius of confidence interval.
  
  vvolb="$1"; shift  # V-coordinate of volume plot baseline.

  dots=( "$@" ) # H- an V-cordinates of "slumber points".

  img="2014-02-14-prediction-check-${ex}"
  
  vpred_lab=$(( ${vpred} - 160 ))
  
  boxcmds=( ) # Commands to draw prediction boxes.
  boxcmds+=( `boxcom ${pmin} ${vbot} ${pmax} ${vtop} "${ttarg1}" ${htarg1} ${ptarg1} ${perr1}` )
  boxcmds+=( `boxcom ${pmin} ${vbot} ${pmax} ${vtop} "${ttarg2}" ${htarg2} ${ptarg2} ${perr2}` )
  boxcmds+=( `boxcom ${pmin} ${vbot} ${pmax} ${vtop} "${ttarg3}" ${htarg3} ${ptarg3} ${perr3}` )
  boxcmds+=( `boxcom ${pmin} ${vbot} ${pmax} ${vtop} "${ttarg4}" ${htarg4} ${ptarg4} ${perr4}` )
  boxcmds+=( `boxcom ${pmin} ${vbot} ${pmax} ${vtop} "${ttarg5}" ${htarg5} ${ptarg5} ${perr5}` )
  
  echo "boxcmds=( " "${boxcmds[@]}" " )" 1>&2
  
  dotcmds=( "-color" "#ffaa0099" "-strokewidth" "20" ) # Commands to draw dots at the slumber points.
  
  echo "dotcmds=( " "${boxcmds[@]}" " )" 1>&2

  i=0
  while [[ ${i} -lt ${#dots[@]} ]]; do 
    hdot="${dots[${i}]}";
    i=$(( ${i} + 1 ))
    pdot="${dots[${i}]}";
    vdot=`pscale ${pmin} ${vbot} ${pmax} ${vtop} ${pdot}`
    i=$(( ${i} + 1 ))
    dotcmds+=( -S ${hdot} ${vdot}  -S ${hdot} ${vvolb} )
  done
  
  set echo
  
  annotate-pic.sh \
      ${img}.png 16 Black Yellow \
      -color '#ff44aa' -strokewidth 2 \
      -L ${hpred} ${vpred}  ${hpred} ${vpred_lab}  "${tpred}" \
      \
      "${boxcmds[@]}" \
      \
      "${dotcmds[@]}" \
      \
    > ${img}-annotated.png
  gimp ${img}-annotated.png
}

# echo `pscale 3700 573  4800 111  4400` 

# Bitstamp slumber dots are computed from Huobi's by the ratios
#   1 USD = 6.40 CNY for 2014-02-07 -- 2014-02-09
#   1 USD = 6.12 CNY for other days.
annot BSTP \
  540 569  780 104 \
  "23:07" 868  370 \
  "@"     368  700  10 \
  "@"     488  690  10 \
  "@"     733  640  15 \
  "@"     848  650  10 \
  "19:00" 968  615 10 \
  721 \
     8   781 \
   128   724 \
   248   711 \
   368   701 \
   488   666 \
   608   676 \
   728   654 \
   848   633 \
   968   655

annot HUBI \
  3300 582  4800  93 \
  "23:07" 868 370 \
  "@"     368  4510 20 \
  "@"     488  4450 25 \
  "@"     733  4075 30 \
  "@"     848  3970 30 \
  "19:00" 968  3750 30 \
   721 \
     8  4782 \
   128  4637 \
   248  4555 \
   368  4492 \
   488  4080 \
   608  4139 \
   728  4006 \
   848  3876 \
   968  4008

# Dot positions:

#   X-2160 
#   X-2040 
#   X-1920 
#   X-1800 
#   X-1680 
#   X-1560 
#   X-1440 
#   X-1320 
#   X-1200 
#   X-1080 
#   X-960  
#   X-840  
#   X-720  
#   X-600  
#   X-480  
#   X-360  
#   X-240  
#   X-120  
#   X      
#   X+120
