#! /bin/bash
# Last edited on 2014-02-23 18:35:56 by stolfilocal

# 05 pixels = 01:00

date="2014-02-23"

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 make_rect_cmds(){
  # 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.
  
  krect="$1"; shift # "@" for old rectangle, "+" for new.
  hrect="$1"; shift # H-coordinate of rectangle center (already shifted by ${hshift}).
  prect="$1"; shift # Predicted price at ${trect}.
  perr="$1"; shift  # V-radius of confidence interval.

  echo "enter make_rect_cmds ${pmin} ${vbot} ${pmax} ${vtop} ${krect} ${hrect} ${prect} ${perr}" 1>&2 
  
  vrect_top=`pscale ${pmin} ${vbot} ${pmax} ${vtop} $(( ${prect} + ${perr} ))`
  vrect_bot=`pscale ${pmin} ${vbot} ${pmax} ${vtop} $(( ${prect} - ${perr} ))`
  hrect_lft=$(( ${hrect} - 3 ))
  hrect_rht=$(( ${hrect} + 3 ))

  if [[ "/${krect}" != "/@" ]]; then crect="#0088ff" ; else crect="#aabbcc" ; fi
  
  printf "%s %s\n" "-color" "${crect}"
  printf "%s %s\n" "-strokewidth" "3"
  printf "%s %s %s  %s %s  %s %s  %s %s\n" "-Q" \
    "${hrect_rht}" "${vrect_top}" \
    "${hrect_rht}" "${vrect_bot}" \
    "${hrect_lft}" "${vrect_bot}" \
    "${hrect_lft}" "${vrect_top}" 
  echo "exit make_rect_cmds" 1>&2 
}     

function annot(){
  # Annotate one image.
  
  ex="$1"; shift       # Exchange code
  which="$1"; shift    # "check" or "make"

  # Input image:
  iimg="${date}-prediction-check-${ex}"
  
  # Output image:
  oimg="${date}-prediction-${which}-${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
  
  # Balloon with time posted ("-tpost") or time valid ("-tpred")
  labt_cmds=( )
  while [[ ( "/$1" == "/-tpost" ) || ( "/$1" == "/-tpred" ) ]]; do
    if [[ "/$1" == "/-tpost" ]]; then clabt="#ff44aa" ; else clabt="#33ddff" ; fi
    shift;
  
    # Label with time of last prediction:
    tlabt="$1"; shift # UTC time to show in balloon.
    hlabt="$1"; shift # H-coordinate of balloon center (unshifted).
    vlabt_dot="$1"; shift # V-coordinate of balloon dot.

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

    vlabt_lab=$(( ${vlabt_dot} - 80 ))
    labt_cmds+=( -color "${clabt}" -strokewidth 2 )
    labt_cmds+=( -L ${hlabt} ${vlabt_dot}  ${hlabt} ${vlabt_lab}  "${tlabt}" )
  done
  echo "labt_cmds=( " "${labt_cmds[@]}" " )" 1>&2
  
  # Prediction boxes:
  rect_cmds=( ) # Commands to draw prediction boxes.
  while [[ "/$1" == "/-rect" ]]; do 
    shift;
    krect="$1"; shift # "@" for old, "+" for new.
    hrect="$1"; shift # H-coordinate of rectangle center (unshifted).
    prect="$1"; shift # Price of rectangle center.
    perr="$1"; shift  # Price variation from center.
    
    hrect=$(( ${hrect} + ${hshift} ))

    rect_cmds+=( `make_rect_cmds ${pmin} ${vbot} ${pmax} ${vtop} "${krect}" ${hrect} ${prect} ${perr}` )
  done
  echo "rect_cmds=( " "${rect_cmds[@]}" " )" 1>&2
  
  # Trend lines:
  line_cmds=( )
  while [[ "/$1" == "/-line" ]]; do 
    shift;
    kline="$1"; shift # "@" for bad, "+" for new.
    hline1="$1"; shift # H-coordinate of line start (unshifted).
    pline1="$1"; shift # Price of line start.
    hline2="$1"; shift # H-coordinate of lineend (unshifted).
    pline2="$1"; shift # Price of line end.
    
    if [[ "/${kline}" == "/@" ]]; then cline="#88664466" ; else cline="#ff550099" ; fi
    
    hline1=$(( ${hline1} + ${hshift} ))
    vline1=`pscale ${pmin} ${vbot} ${pmax} ${vtop} ${pline1}`
      
    hline2=$(( ${hline2} + ${hshift} ))
    vline2=`pscale ${pmin} ${vbot} ${pmax} ${vtop} ${pline2}`

    line_cmds+=( -strokewidth 3 -color "${cline}" )
    line_cmds+=( -S ${hline1} ${vline1} ${hline2} ${vline2} ) 
  done
  echo "line_cmds=( " "${line_cmds[@]}" " )" 1>&2

  # Dots for Slumber Points:
  dot_cmds=( ) # 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".

    dot_cmds+=( -strokewidth 20 )
    i=0
    while [[ ${i} -lt ${#dots[@]} ]]; do 
      kdot="${dots[${i}]}"; # "@" for bad, "+" for good.
      if [[ "/${kdot}" == "/@" ]]; then cdot="#88664466" ; else cdot="#ff550099" ; fi
      i=$(( ${i} + 1 ))
      
      hdot="${dots[${i}]}"; # H-coordinate of dot (unshifted).
      hdot=$(( ${hdot} + ${hshift} ))
      i=$(( ${i} + 1 ))
      
      pdot="${dots[${i}]}"; # Price at dot.
      vdot=`pscale ${pmin} ${vbot} ${pmax} ${vtop} ${pdot}`
      i=$(( ${i} + 1 ))
  
      dot_cmds+=( -strokewidth 24 -color "${cdot}" )
      dot_cmds+=( -S ${hdot} ${vdot} ) 
      dot_cmds+=( -S ${hdot} ${vvolb_dot} )
    done
    
  fi
  echo "dot_cmds=( " "${dot_cmds[@]}" " )" 1>&2

  set echo
  
  annotate-pic.sh \
      ${iimg}.png 16 Black Yellow \
      "${labt_cmds[@]}" \
      \
      "${rect_cmds[@]}" \
      \
      "${dot_cmds[@]}" \
      \
      "${line_cmds[@]}" \
      \
    > ${oimg}-annotated.png
  gimp ${oimg}-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 HUBI make \
  3300 632  4800  94 \
  -hshift -10 \
  \
  -rect "+" 2168  3809 25 \
  \
  -line "@"  458 4095  1478 3849 \
  -line "+" 1418 3825  2198 3809 \
  \
  -dots 800 \
  "+"    8 4782 \
  "+"  128 4637 \
  "+"  248 4555 \
  "+"  368 4492 \
  "+"  488 4080 \
  "@"  608 4139 \
  "+"  728 4006 \
  "@"  848 3876 \
  "+"  968 4008 \
  "+" 1088 3961 \
  "@" 1208 3733 \
  "+" 1328 3905 \
  "+" 1448 3817 \
  "+" 1568 3832 \
  "@" 1688 3530 \
  "+" 1808 3425 \
  "@" 1928 3621 \
  "+" 2048 3810

annot HUBI check \
  3300 632  4800  94 \
  -hshift -10 \
  \
  -tpost "22:05" 1943 400 \
  -tpred "19:00" 2048 400 \
  \
  -rect "@"  368  4510 25 \
  -rect "@"  488  4450 25 \
  -rect "@"  733  4075 25 \
  -rect "@"  848  3970 25 \
  -rect "@"  968  3750 25 \
  -rect "@" 1088  3630 25 \
  -rect "@" 1208  3915 25 \
  -rect "@" 1328  3630 25 \
  -rect "@" 1448  3890 25 \
  -rect "@" 1568  3775 25 \
  -rect "@" 1688  3800 25 \
  -rect "@" 1808  3772 25 \
  -rect "@" 1928  3396 25 \
  -rect "+" 2048  3368 25 \
  \
  -line "@"  428 4101  2108 3700 \
  -line "+" 1628 3467  2108 3353 \
  \
  -dots 800 \
  "+"    8 4782 \
  "+"  128 4637 \
  "+"  248 4555 \
  "+"  368 4492 \
  "+"  488 4080 \
  "@"  608 4139 \
  "+"  728 4006 \
  "@"  848 3876 \
  "+"  968 4008 \
  "+" 1088 3961 \
  "@" 1208 3733 \
  "+" 1328 3905 \
  "+" 1448 3817 \
  "+" 1568 3832 \
  "@" 1688 3530 \
  "+" 1808 3425 \
  "@" 1928 3621 \
  "+" 2048 3810

annot BSTP check \
  540 622  780  92 \
  -hshift -10 \
  \
  -tpost "22:05" 1943  370 \
  -tpred "19:00" 2048  370 \
  \
  -rect "@"  368  700  5 \
  -rect "@"  488  690  5 \
  -rect "@"  733  640  5 \
  -rect "@"  848  650  5 \
  -rect "@"  968  615  5 \
  -rect "@" 1088  600  5 \
  -rect "@" 1208  640  5 \
  -rect "@" 1328  590  5 \
  -rect "@" 1448  635  5 \
  -rect "@" 1568  615  5 \
  -rect "@" 1688  621  5 \
  -rect "@" 1808  616  5 \
  -rect "@" 1928  555  5 \
  -rect "+" 2048  550  5 \
  \
  -line "@"  428  670  2108  605 \
  -line "+" 1628  567  2108  548 \
  \
  -dots 800 \
  "+"    8  781 \
  "+"  128  724 \
  "+"  248  711 \
  "+"  368  701 \
  "+"  488  666 \
  "@"  608  676 \
  "+"  728  654 \
  "@"  848  633 \
  "+"  968  655 \
  "+" 1088  647 \
  "@" 1208  610 \
  "+" 1328  638 \
  "+" 1448  624 \
  "+" 1568  626 \
  "@" 1688  577 \
  "+" 1808  560 \
  "@" 1928  592 \
  "+" 2048  623
