#! /bin/bash
# Last edited on 2014-02-16 19:42:02 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} -  60 ))
    vtarg_lab=$(( ${vtarg_arr} -  80 ))

    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

  img="2014-02-16-prediction-check-${ex}"
  
  # Vertical scale information
  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.
  
  hshift=0;  # Value to add or subtract from all h-coordinates.
  if [[ "/$1" == "/-hshift" ]]; then
    shift;

    hshift="$1"; shift
  fi
  
  # Label with time of last prediction:
  postcmds=( )
  if [[ "/$1" == "/-post" ]]; then
    shift;
  
    # Label with time of last prediction:
    tpost="$1"; shift # UTC time when last prediction was issued.
    hpost="$1"; shift # H-coordinate corresponding to ${tpost}.
    vpost="$1"; shift # V-coordinate just above plot at ${tpost}.

    hpost=$(( ${hpost} + ${hshift} ))

    vpost_lab=$(( ${vpost} - 80 ))
    postcmds+=( -color '#ff44aa' -strokewidth 2 )
    postcmds+=( -L ${hpost} ${vpost}  ${hpost} ${vpost_lab}  "${tpost}" )
  fi
  
  # Prediction boxes:
  boxcmds=( ) # Commands to draw prediction boxes.
  while [[ "/$1" == "/-pred" ]]; do 
    shift;
    ttarg="$1"; shift # UTC time when prediction  was to be fulfilled, or "@".
    htarg="$1"; shift # H-coordinate corresponding to ${ttarg}.
    ptarg="$1"; shift # V-coordinate for predicted price at ${ttarg}.
    perr="$1"; shift  # V-radius of confidence interval.
    
    htarg=$(( ${htarg} + ${hshift} ))

    boxcmds+=( `boxcom ${pmin} ${vbot} ${pmax} ${vtop} "${ttarg}" ${htarg} ${ptarg} ${perr}` )
  done
  echo "boxcmds=( " "${boxcmds[@]}" " )" 1>&2
  
  # Dots for Slumber Points:
  dotcmds=( ) # Commands to draw dots at the slumber points and slumber times.
  if [[ "/$1" == "/-dots" ]]; then 
    shift;

    vvolb="$1"; shift  # V-coordinate of volume plot baseline.
    vvolb_dot=${vvolb} # V-coordinate of Slumber Times dots.
    
    dots=( "$@" ) # H- an V-cordinates of "slumber points".

    dotcmds+=( -strokewidth 20 -color "#ffbb0099" )
    i=0
    while [[ ${i} -lt ${#dots[@]} ]]; do 
      hdot="${dots[${i}]}";
      hdot=$(( ${hdot} + ${hshift} ))
      i=$(( ${i} + 1 ))
      pdot="${dots[${i}]}";
      vdot=`pscale ${pmin} ${vbot} ${pmax} ${vtop} ${pdot}`
      i=$(( ${i} + 1 ))
      dotcmds+=( -S ${hdot} ${vdot} ) 
      dotcmds+=( -S ${hdot} ${vvolb_dot} )
    done
    
  fi
  echo "dotcmds=( " "${dotcmds[@]}" " )" 1>&2

  set echo
  
  annotate-pic.sh \
      ${img}.png 16 Black Yellow \
      "${postcmds[@]}" \
      \
      "${boxcmds[@]}" \
      \
      "${dotcmds[@]}" \
      \
    > ${img}-annotated.png
  gimp ${img}-annotated.png
}

# echo `pscale 3700 573  4800 111  4400` 

annot HUBI \
  3400 463  4600  85 \
  -hshift -240 \
  -post "03:01" 1128 231 \
  -pred "@"      368  4510 20 \
  -pred "@"      488  4450 25 \
  -pred "@"      733  4075 30 \
  -pred "@"      848  3970 30 \
  -pred "@"      968  3750 30 \
  -pred "@"     1088  3630 30 \
  -pred "19:00" 1208  3915 30 \
  -dots 616 \
     8  4782 \
   128  4637 \
   248  4555 \
   368  4492 \
   488  4080 \
   608  4139 \
   728  4006 \
   848  3876 \
   968  4008 \
  1088  3961 \
  1208  3733

# 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 483  760 86 \
  -hshift -240 \
  -post "03:01" 1128  225 \
  -pred "@"      368  700  10 \
  -pred "@"      488  690  10 \
  -pred "@"      733  640  15 \
  -pred "@"      848  650  10 \
  -pred "@"      968  615  10 \
  -pred "@"     1088  600  25 \
  -pred "19:00" 1208  640  10 \
  -dots 726 \
     8   781 \
   128   724 \
   248   711 \
   368   701 \
   488   666 \
   608   676 \
   728   654 \
   848   633 \
   968   655 \
  1088   647 \
  1208   610
