#! /bin/bash
# Last edited on 2014-03-01 20:20:13 by stolfilocal

# 05 pixels = 01:00

date="2014-03-01"

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.

check=0
make=1

scale_HUBI=( 3200  450  4800  122 )
scale_BSTP=(  400  486   800  102 )

if [[ ${make} -gt 0 ]]; then 

  annot HUBI make \
    ${scale_HUBI[@]} \
    -hshift 420 \
    \
    -rect "+" 2888  3515 25 \
    \
    -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  "@"  1628  3905   1628  3515 \
    \
    -line  "+"  1628  3515   2948  3515 \
    \
    -dots 599 \
    "+" -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

fi

if [[ ${check} -gt 0 ]]; then 

  annot HUBI check \
    ${scale_HUBI[@]} \
    -hshift 420 \
    \
    -tpost "22:19" 2664 360 \
    -tpred "19:00" 2768 360 \
    \
    -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 \
    -rect "@" 2168  3809 25 \
    -rect "@" 2288  3807 25 \
    -rect "@" 2408  3804 25 \
    -rect "@" 2528  3802 25 \
    -rect "@" 2528  2781 25 \
    -rect "@" 2648  3533 25 \
    -rect "+" 2768  3522 25 \
    \
    -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  "@"  1628  3905   1628  3522 \
    \
    -line  "+"  1628  3522   2828  3522 \
    \
    -dots 599 \
    "+" -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

  annot BSTP check \
    ${scale_BSTP[@]} \
    -hshift 420 \
    \
    -tpost "22:19" 2664  300 \
    -tpred "19:00" 2768  300 \
    \
    -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 \
    -rect "@" 2168  622  5 \
    -rect "@" 2288  622  5 \
    -rect "@" 2408  622  5 \
    -rect "@" 2528  621  5 \
    -rect "@" 2528  454  5 \
    -rect "@" 2648  577  5 \
    -rect "+" 2768  575  5 \
    \
    -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  "@"  1628   638   1628   575 \
    \
    -line  "+"  1628   575   2828   575 \
    \
    -dots 599 \
    "+" -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

fi
