#! /bin/bash
# Last edited on 2014-03-14 19:16:51 by stolfilocal

# 05 pixels = 01:00

date="2014-03-14"
dinos=1

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} - 100 ))
    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

  # Decoration
  deco_cmds=( )
  while [[ "/$1" == "/-deco" ]]; do 
    shift;
    hpdeco="$1"; shift # H-coordinate of deco (unshifted).
    vpdeco="$1"; shift # V-coordinate of deco (absolute).
    hadeco="$1"; shift # H-alignment of deco.
    vadeco="$1"; shift # V-alignment of deco.
    imdeco="$1"; shift # Image name of deco.
    mgdeco="$1"; shift # Magnification factor of deco.
    
    hpdeco=$(( ${hpdeco} + ${hshift} ))
    
    deco_cmds+=( -I ${hpdeco} ${vpdeco} ${hadeco} ${vadeco} ${imdeco} ${mgdeco} ) 
  done
  echo "deco_cmds=( " "${deco_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="lignite-wp-edit-100.png"
      else
        cdot="#ff550099" 
        idino="ruby-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} )
      
      if [[ ${dinos} -ne 0 ]]; then
        # Dinosaur decoration:
        dot_cmds+=( -I ${hdot} ${vdino} 0.5 1.0 ${idino} 1.0 )
      fi
    done
    
  fi
  echo "dot_cmds=( " "${dot_cmds[@]}" " )" 1>&2

  set echo
  
  annotate-pic.sh \
      ${iimg}.png 16 Black Yellow \
      "${deco_cmds[@]}" \
      \
      "${labt_cmds[@]}" \
      \
      "${dot_cmds[@]}" \
      \
      "${rect_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=( 3400  552  4300  102 )
scale_BSTP=(  540  554   700  127 )

#   -deco 4088  88 0.5 0.5 germanodactylus-wp-edit-200.png 0.60 \
#   -deco 4088 368 0.5 0.5 ophthalmosaurus-wp-edit-200.png 0.60 \
#   -deco 4208 283 0.5 0.5 helix-wp-edit-200.png 0.60 \

annot HUBI check \
  ${scale_HUBI[@]} \
  -hshift -2344 \
  \
  -tpost "07:38" 4271 284 \
  \
  -rect "@"  368  4510 8 8 \
  -rect "@"  488  4450 8 8 \
  -rect "@"  733  4075 8 8 \
  -rect "@"  848  3970 8 8 \
  -rect "@"  968  3750 8 8 \
  -rect "@" 1088  3630 8 8 \
  -rect "@" 1208  3915 8 8 \
  -rect "@" 1328  3630 8 8 \
  -rect "@" 1448  3890 8 8 \
  -rect "@" 1568  3775 8 8 \
  -rect "@" 1688  3800 8 8 \
  -rect "@" 1808  3772 8 8 \
  -rect "@" 1928  3396 8 8 \
  -rect "@" 2048  3368 8 8 \
  -rect "@" 2168  3809 8 8 \
  -rect "@" 2288  3807 8 8 \
  -rect "@" 2408  3804 8 8 \
  -rect "@" 2528  3802 8 8 \
  -rect "@" 2528  2781 8 8 \
  -rect "@" 2648  3533 8 8 \
  -rect "@" 2768  3522 8 8 \
  -rect "@" 2888  3515 8 8 \
  -rect "@" 3008  3400 8 8 \
  -rect "@" 3128  3370 8 8 \
  -rect "@" 3248  4050 8 8 \
  -rect "@" 3368  3872 8 8 \
  -rect "@" 3488  3899 8 8 \
  -rect "@" 3608  3891 8 8 \
  -rect "@" 3728  3596 8 8 \
  -rect "@" 3848  3938 8 8 \
  -rect "@" 3968  4073 8 8 \
  -rect "@" 3968  3597 8 8 \
  -rect "@" 4088  3788 8 8 \
  -rect "@" 4208  3783 8 8 \
  -rect "+" 4328  3918 8 8 \
  \
  -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  4094   3128  4041 \
  -line  "@"  3128  4041   3188  3997 \
  -line  "@"  3188  3997   3248  3961 \
  -line  "@"  3248  3961   3308  3930 \
  -line  "@"  3308  3930   3368  3904 \
  -line  "@"  3368  3904   3428  3883 \
  -line  "@"  3428  3883   3488  3865 \
  -line  "@"  3488  3865   3548  3850 \
  -line  "@"  3548  3850   3608  3837 \
  -line  "@"  3608  3837   3668  3826 \
  -line  "@"  3668  3826   3728  3818 \
  -line  "@"  3728  3818   3788  3810 \
  -line  "@"  3788  3810   3848  3804 \
  -line  "@"  3848  3804   3908  3799 \
  -line  "@"  3908  3799   3968  3794 \
  -line  "@"  3968  3794   4028  3791 \
  \
  -dots 710 597 \
  "+" -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 \
  "+"  3728 3803 \
  "@"  3848 3784 \
  "+"  3968 3802 \
  "@"  4088 3876 \
  "+"  4208 3918 \
  "@"  4328 3821

annot BSTP check \
  ${scale_BSTP[@]} \
  -hshift -2344 \
  \
  -tpost "07:38" 4271  260 \
  \
  -rect "@"  368  700 8 8 \
  -rect "@"  488  690 8 8 \
  -rect "@"  733  640 8 8 \
  -rect "@"  848  650 8 8 \
  -rect "@"  968  615 8 8 \
  -rect "@" 1088  600 8 8 \
  -rect "@" 1208  640 8 8 \
  -rect "@" 1328  590 8 8 \
  -rect "@" 1448  635 8 8 \
  -rect "@" 1568  615 8 8 \
  -rect "@" 1688  621 8 8 \
  -rect "@" 1808  616 8 8 \
  -rect "@" 1928  555 8 8 \
  -rect "@" 2048  550 8 8 \
  -rect "@" 2168  622 8 8 \
  -rect "@" 2288  622 8 8 \
  -rect "@" 2408  622 8 8 \
  -rect "@" 2528  621 8 8 \
  -rect "@" 2528  454 8 8 \
  -rect "@" 2648  577 8 8 \
  -rect "@" 2768  575 8 8 \
  -rect "@" 2888  572 8 8 \
  -rect "@" 3008  556 8 8 \
  -rect "@" 3128  551 8 8 \
  -rect "@" 3248  663 8 8 \
  -rect "@" 3368  633 8 8 \
  -rect "@" 3488  637 8 8 \
  -rect "@" 3608  636 8 8 \
  -rect "@" 3728  588 8 8 \
  -rect "@" 3848  656 8 8 \
  -rect "@" 3968  679 8 8 \
  -rect "@" 3968  600 8 8 \
  -rect "@" 4088  631 8 8 \
  -rect "@" 4208  631 8 8 \
  -rect "+" 4328  640 8 8 \
  \
  -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   682   3128   674 \
  -line  "@"  3128   674   3188   666 \
  -line  "@"  3188   666   3248   660 \
  -line  "@"  3248   660   3308   655 \
  -line  "@"  3308   655   3368   651 \
  -line  "@"  3368   651   3428   647 \
  -line  "@"  3428   647   3488   644 \
  -line  "@"  3488   644   3548   642 \
  -line  "@"  3548   642   3608   639 \
  -line  "@"  3608   639   3668   638 \
  -line  "@"  3668   638   3728   636 \
  -line  "@"  3728   636   3788   635 \
  -line  "@"  3788   635   3848   634 \
  -line  "@"  3848   634   3908   633 \
   \
  -line  "@"  3908   633   3908   621 \
   \
  -line  "@"  3908   621   3968   620 \
  -line  "@"  3968   620   4028   619 \
  \
  -dots 710 597 \
  "+" -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  673 \
  "+"  3248  659 \
  "+"  3368  653 \
  "@"  3488  634 \
  "+"  3608  611 \
  "+"  3728  634 \
  "@"  3848  631 \
  "+"  3968  621 \
  "@"  4088  633 \
  "+"  4208  640 \
  "@"  4328  624







