#! /bin/bash
# Last edited on 2014-03-08 18:27:36 by stolfilocal

# 05 pixels = 01:00

date="2014-03-08"

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, "=" for predicted.
  hrect="$1"; shift # H-coordinate of rectangle center (already shifted by ${hshift}).
  prect="$1"; shift # Predicted price at ${trect}.
  herr="$1"; shift  # Half-width of rectangle (pixels)
  verr="$1"; shift  # Half-height of rectngle (pixels).

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

  if [[ "/${krect}" == "/@" ]]; then
    crect="#aabbcc"
  elif [[ "/${krect}" == "/=" ]]; then
    crect="#ff0088" 
  elif [[ "/${krect}" == "/+" ]]; then
    crect="#0088ff" 
  else 
    echo "** invalid rectange kind" 1>&2 ; exit 1
  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} - 60 ))
    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, "=" for predicted.
    hrect="$1"; shift # H-coordinate of rectangle center (unshifted).
    prect="$1"; shift # Price of rectangle center.
    herr="$1"; shift  # Half-width of box (pixels).
    verr="$1"; shift  # Half-height of box (pixels).
    
    hrect=$(( ${hrect} + ${hshift} ))

    rect_cmds+=( `make_rect_cmds ${pmin} ${vbot} ${pmax} ${vtop} "${krect}" ${hrect} ${prect} ${herr} ${verr}` )
  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.
    vdino="$1"; shift  # V-coordinate of dinosaur 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 False Slumber Point, "+" for True.
      if [[ "/${kdot}" == "/@" ]]; then
        cdot="#88664466" 
        idino="albertosaurus-wp-edit-100.png"
      else
        cdot="#ff550099" 
        idino="glyptodon-wp-edit-100.png"
      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} )
      
      # Dinosaur decoration:
      dot_cmds+=( -I ${hdot} ${vdino} 0.5 1.0 ${idino} 1.0 )
    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.

scale_HUBI=( 3100  519  4300  114 )
scale_BSTP=(  400  542   700  110 )

annot HUBI check \
  ${scale_HUBI[@]} \
  -hshift -578 \
  \
  -tpost "01:00" 3518 205 \
  \
  -rect "@"  368  4510 5 5 \
  -rect "@"  488  4450 5 5 \
  -rect "@"  733  4075 5 5 \
  -rect "@"  848  3970 5 5 \
  -rect "@"  968  3750 5 5 \
  -rect "@" 1088  3630 5 5 \
  -rect "@" 1208  3915 5 5 \
  -rect "@" 1328  3630 5 5 \
  -rect "@" 1448  3890 5 5 \
  -rect "@" 1568  3775 5 5 \
  -rect "@" 1688  3800 5 5 \
  -rect "@" 1808  3772 5 5 \
  -rect "@" 1928  3396 5 5 \
  -rect "@" 2048  3368 5 5 \
  -rect "@" 2168  3809 5 5 \
  -rect "@" 2288  3807 5 5 \
  -rect "@" 2408  3804 5 5 \
  -rect "@" 2528  3802 5 5 \
  -rect "@" 2528  2781 5 5 \
  -rect "@" 2648  3533 5 5 \
  -rect "@" 2768  3522 5 5 \
  -rect "@" 2888  3515 5 5 \
  -rect "@" 3008  3400 5 5 \
  -rect "@" 3128  3370 5 5 \
  -rect "@" 3248  4050 5 5 \
  -rect "@" 3368  3872 5 5 \
  -rect "@" 3488  3899 5 5 \
  -rect "+" 3608  3891 5 5 \
  \
  -rect "=" 3728  3596 7 7 \
  \
  -line  "@"   -51  4873      8  4780 \
  -line  "@"     8  4780     68  4704 \
  -line  "@"    68  4704    128  4642 \
  -line  "@"   128  4642    188  4592 \
  -line  "@"   188  4592    248  4552 \
  -line  "@"   248  4552    308  4519 \
  -line  "@"   308  4519    368  4492 \
  -line  "@"   368  4492    428  4470 \
  \
  -line  "@"   428  4470    428  4147 \
  \
  -line  "@"   428  4147    488  4101 \
  -line  "@"   488  4101    548  4063 \
  -line  "@"   548  4063    608  4033 \
  -line  "@"   608  4033    668  4008 \
  -line  "@"   668  4008    728  3988 \
  -line  "@"   728  3988    788  3972 \
  -line  "@"   788  3972    848  3958 \
  -line  "@"   848  3958    908  3947 \
  -line  "@"   908  3947    968  3939 \
  -line  "@"   968  3939   1028  3932 \
  -line  "@"  1028  3932   1088  3926 \
  -line  "@"  1088  3926   1148  3921 \
  -line  "@"  1148  3921   1208  3917 \
  -line  "@"  1208  3917   1268  3914 \
  -line  "@"  1268  3914   1328  3912 \
  -line  "@"  1328  3912   1388  3910 \
  -line  "@"  1388  3910   1448  3908 \
  -line  "@"  1448  3908   1508  3907 \
  -line  "@"  1508  3907   1568  3905 \
  -line  "@"  1568  3905   1628  3905 \
  \
  -line  "@"  1988  3964   2348  3175 \
  \
  -line  "@"  2348  3567   2948  3415 \
  \
  -line  "+"  3068  4102   3788  3550 \
  \
  -dots 670 564 \
  "+" -2032 5014 \
  "@" -1912 5015 \
  "@" -1792 4966 \
  "+" -1672 4927 \
  "+" -1552 4730 \
  "+" -1432 4839 \
  "@" -1312 5041 \
  "@" -1192 4856 \
  "+" -1072 4829 \
  "@"  -952 4809 \
  "@"  -832 4851 \
  "+"  -712 4873 \
  "@"  -592 4939 \
  "@"  -472 4973 \
  "@"  -352 4898 \
  "@"  -232 4915 \
  "@"  -112 4892 \
  "+"     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 \
  "+"  2168 3615 \
  "+"  2288 3284 \
  "@"  2408 3543 \
  "+"  2528 3533 \
  "+"  2648 3464 \
  "+"  2768 3479 \
  "+"  2888 3427 \
  "@"  3008 3825 \
  "+"  3128 4040 \
  "+"  3248 3956 \
  "+"  3368 3917 \
  "@"  3488 3802 \
  "+"  3608 3668

annot BSTP check \
  ${scale_BSTP[@]} \
  -hshift -578 \
  \
  -tpost "01:00" 3518  165 \
  \
  -rect "@"  368  700 5 5 \
  -rect "@"  488  690 5 5 \
  -rect "@"  733  640 5 5 \
  -rect "@"  848  650 5 5 \
  -rect "@"  968  615 5 5 \
  -rect "@" 1088  600 5 5 \
  -rect "@" 1208  640 5 5 \
  -rect "@" 1328  590 5 5 \
  -rect "@" 1448  635 5 5 \
  -rect "@" 1568  615 5 5 \
  -rect "@" 1688  621 5 5 \
  -rect "@" 1808  616 5 5 \
  -rect "@" 1928  555 5 5 \
  -rect "@" 2048  550 5 5 \
  -rect "@" 2168  622 5 5 \
  -rect "@" 2288  622 5 5 \
  -rect "@" 2408  622 5 5 \
  -rect "@" 2528  621 5 5 \
  -rect "@" 2528  454 5 5 \
  -rect "@" 2648  577 5 5 \
  -rect "@" 2768  575 5 5 \
  -rect "@" 2888  572 5 5 \
  -rect "@" 3008  556 5 5 \
  -rect "@" 3128  551 5 5 \
  -rect "@" 3248  663 5 5 \
  -rect "@" 3368  633 5 5 \
  -rect "@" 3488  637 5 5 \
  -rect "+" 3608  636 5 5 \
  \
  -rect "=" 3728  588 7 7 \
  \
  -line  "@"   -51   796      8   781 \
  -line  "@"     8   781     68   768 \
  \
  -line  "@"    68   768     68   735 \
  \
  -line  "@"    68   735    128   725 \
  -line  "@"   128   725    188   717 \
  -line  "@"   188   717    248   711 \
  -line  "@"   248   711    308   706 \
  -line  "@"   308   706    368   701 \
  -line  "@"   368   701    428   698 \
  \
  -line  "@"   428   698    428   677 \
  \
  -line  "@"   428   677    488   670 \
  -line  "@"   488   670    548   663 \
  -line  "@"   548   663    608   658 \
  -line  "@"   608   658    668   654 \
  -line  "@"   668   654    728   651 \
  -line  "@"   728   651    788   649 \
  -line  "@"   788   649    848   646 \
  -line  "@"   848   646    908   644 \
  -line  "@"   908   644    968   643 \
  -line  "@"   968   643   1028   642 \
  -line  "@"  1028   642   1088   641 \
  -line  "@"  1088   641   1148   640 \
  -line  "@"  1148   640   1208   640 \
  -line  "@"  1208   640   1268   639 \
  -line  "@"  1268   639   1328   639 \
  -line  "@"  1328   639   1388   638 \
  -line  "@"  1388   638   1448   638 \
  -line  "@"  1448   638   1508   638 \
  -line  "@"  1508   638   1568   638 \
  -line  "@"  1568   638   1628   638 \
  \
  -line  "@"  1988   648   2348   519 \
  \
  -line  "@"  2348   583   2948   558 \
  \
  -line  "+"  3068   670   3788   580 \
  \
  -dots 670 564 \
  "+" -2032  819 \
  "@" -1912  819 \
  "@" -1792  811 \
  "+" -1672  805 \
  "+" -1552  773 \
  "+" -1432  791 \
  "@" -1312  824 \
  "@" -1192  793 \
  "+" -1072  789 \
  "@"  -952  786 \
  "@"  -832  793 \
  "+"  -712  796 \
  "@"  -592  807 \
  "@"  -472  813 \
  "@"  -352  800 \
  "@"  -232  803 \
  "@"  -112  799 \
  "+"     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 \
  "+"  2168  591 \
  "+"  2288  537 \
  "@"  2408  579 \
  "+"  2528  577 \
  "+"  2648  566 \
  "+"  2768  568 \
  "+"  2888  560 \
  "@"  3008  625 \
  "+"  3128  660 \
  "+"  3248  646 \
  "+"  3368  640 \
  "@"  3488  621 \
  "+"  3608  599

