#! /bin/bash
# Last edited on 2014-05-06 18:18:16 by stolfilocal

date="2014-05-06"
hubi=1
bstp=1
icons=1
candlestep=5   # Pixels per hour.

tmp=/tmp/$$

function tscale(){
  # Prints to {stdout} the H-coordinate corresponing to a given hour number.

  # Scaling parameters:
  local tshift="$1"; shift # Reference hour.
  local hshift="$1"; shift # H-coordinate of reference hour.

  local targ="$1"; shift # Given hour.

  # echo "enter tscale ${tshift} ${hshift} ${targ}" 1>&2

  local harg=$(( ( ( ${targ} - ${tshift} ) * ${candlestep} ) + ${hshift} ))

  printf "%d\n" "${harg}"
  # echo "exit tscale ${harg}" 1>&2
}

function pscale(){
  # Prints to {stdout} the V-coordinate corresponing to a given price {parg/currf}

  # Scaling parameters:
  local pmin="$1"; shift # Low reference price.
  local vbot="$1"; shift # V-coordinate of the low reference price.
  local pmax="$1"; shift # High reference price.
  local vtop="$1"; shift # V-coordinate of the high reference price.

  local parg="$1"; shift # Given price.
  local currf="$1"; shift # Currency conversion factor.

  # echo "enter pscale ${pmin} ${vbot} ${pmax} ${vtop} ${parg} ${currf}" 1>&2

  local varg=`echo "p=${parg}/${currf}; a=${pmin}; b=${pmax}; r=${vbot}; s=${vtop}; v = r + (s-r)*(p-a)/(b-a); scale=0; v/1.0" | bc -lq`

  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.

  # Hour scaling parameters:
  local tshift="$1"; shift # Reference hour.
  local hshift="$1"; shift # H-coordinate of reference hour.

  # Price scaling parameters:
  local pmin="$1"; shift # Low reference price.
  local vbot="$1"; shift # V-coordinate of the low reference price.
  local pmax="$1"; shift # High reference price.
  local vtop="$1"; shift # V-coordinate of the high reference price.

  local krect="$1"; shift # "@" for old rectangle, "+" for new, "=" for predicted.
  local trect="$1"; shift # Hour of rectangle center.
  local prect="$1"; shift # Predicted price at ${trect}.
  local herr="$1"; shift  # Half-width of rectangle (pixels)
  local verr="$1"; shift  # Half-height of rectngle (pixels).

  # echo "enter make_rect_cmds ${pmin} ${vbot} ${pmax} ${vtop} ${krect} ${hrect} ${prect} ${herr} ${verr}" 1>&2

  local hrect=`tscale ${tshift} ${hshift} ${trect}`
  local vrect=`pscale ${pmin} ${vbot} ${pmax} ${vtop} ${prect} 1.00`
  local vrect_top=$(( ${vrect} + ${verr} ))
  local vrect_bot=$(( ${vrect} - ${verr} ))
  local hrect_lft=$(( ${hrect} - ${herr} ))
  local hrect_rht=$(( ${hrect} + ${herr} ))

  if [[ "/${krect}" == "/@" ]]; then
    local crect="#5566cc"
    local lwdt=2
  elif [[ "/${krect}" == "/+" ]]; then
    local crect="#0066ff"
    local lwdt=3
  elif [[ "/${krect}" == "/=" ]]; then
    local crect="#ff00aa"
    local lwdt=3
  else
    echo "** invalid rectange kind" 1>&2 ; exit 1
  fi

  printf "%s %s\n" "-color" "${crect}"
  printf "%s %s\n" "-strokewidth" "${lwdt}"
  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 make_dot_cmds(){
  # Prints to {stdout} the draw commands for a data point.

  # Hour scaling parameters:
  local tshift="$1"; shift # Reference hour.
  local hshift="$1"; shift # H-coordinate of reference hour.

  # Price scaling parameters:
  local pmin="$1"; shift # Low reference price.
  local vbot="$1"; shift # V-coordinate of the low reference price.
  local pmax="$1"; shift # High reference price.
  local vtop="$1"; shift # V-coordinate of the high reference price.

  # Dot parameters:
  local vvolb="$1"; shift # V-coordinate of the volume axis.
  local vicon="$1"; shift # V-coordinate of the icon baseline.
  local currf="$1"; shift # Currency conversion factor.
  local kdot="$1"; shift # Type of dot ("@", "+", etc.).

  local tdot="$1"; shift # Hour of dot.
  local pdot="$1"; shift # Price of dot.
  local srdot="$1"; shift # Slumber ratio {Vh/Vd} of dot.
  local wtdot="$1"; shift # Weight of dot.

  # Plot coordinates of dot:
  local hdot=`tscale ${tshift} ${hshift} ${tdot}`
  local vdot=`pscale ${pmin} ${vbot} ${pmax} ${vtop} ${pdot} ${currf}`

  # Diameter of dot (pt):
  local maxlw=32.0
  local minlw=10.0
  local lwdot=`echo 'w='"${wtdot}"';r='"${maxlw}"'*sqrt(w); m='"${minlw}"'; if(r<m){r=m}; scale=2; r/1.0' | bc -lq`
  # echo "wtdot = ${wtdot} dot diameter (pt) = ${lwdot}" 1>&2

  # Color of dot:
  if [[ "/${kdot}" == "/@" ]]; then
    # local cdot="#77885566" # dark style.
    local cdot="#77885566" # lite style. 
  elif [[ "/${kdot}" == "/+" ]]; then
    # local cdot="#ffff0099" # dark style.
    local cdot="#cc6600aa" # lite style. 
  else
    echo "** invalid {kdot} = \"${kdot}\"" 1>&2; exit 1
  fi

  if [[ ( ${icons} -ne 0 ) && ( ${vicon} -gt 0 ) ]]; then
    # Add iconic representation of night-time activity {srdot}
    # ../icons/albertosaurus-wp-edit-100.png
    local icon_img="../icons/nretnal-wp-edit-100.png"; # Icon image.
    local icon_img_vsz=108;  # Icon V-size in pixels.
    local icon_alh=0.50; # H-alignment of icon.
    local icon_alv=0.00; # V-alignment of icon.

    # Compute a relative icon area {icon_area = srdot/maxsr}, clipped at 1.0:
    local maxsr=0.015;  # Icon area is proportional to {srdot} up to this {srdot}, then constant.
    local icon_area=`echo 's='"${srdot}"'; u='"${maxsr}"'; a=s/u; if(a>1.0){a=1.0}; scale=3; a/1.0' | bc -lq`
    # echo "srdot = ${srdot} icon area = ${icon_area}" 1>&2

    # Convert {icon_area} to icon magnification factor:
    local maxmg=0.400; # Max icon magnification.
    local minmg=0.050; # Min icon magnification.
    local icon_mag=`echo 'a='"${icon_area}"'; v='"${maxmg}"'; g=v*sqrt(a); m='"${minmg}"'; if(g<m){g=m}; scale=2; g/1.0' | bc -lq`
    local icon_vsz=`echo 'a='"${icon_mag}"'; b='"${icon_img_vsz}"'; s=a*b; scale=0; s/1.0' | bc -lq`
    echo "icon magnification = ${icon_mag}" 1>&2

    printf "%s %s %s %s %s %s %s\n" "-I" "${hdot}" "${vicon}" "${icon_alh}" "${icon_alv}" "${icon_img}" "${icon_mag}"
  fi

  # Plot the dot at the slumber price:
  printf "%s %s\n" "-color" "${cdot}"
  printf "%s %s  -S %s %s\n" "-strokewidth" "${lwdot}" "${hdot}" "${vdot}"

  if [[ ( ${vvolb} -gt 0 ) && ( ${vicon} -gt 0 ) ]]; then
    # Plot a dot at the slumber time on the volume plot baseline:
    local vtick_bot=$(( ${vvolb} - 20 )) # V-coordinate of bottom of tick line.
    local vtick_top=$(( ${vicon} + ${icon_vsz} + 5 )) # V-coordinate of top of tick line.
    if [[ ${vtick_bot} -gt ${vtick_top} ]]; then 
      printf "%s %s -S %s %s %s %s\n" "-strokewidth" "1" "${hdot}" "${vtick_bot}" "${hdot}" "${vtick_top}"
    fi
  fi
}

function annot(){
  # Annotate one image.

  local ex="$1"; shift       # Exchange code
  local which="$1"; shift    # "check" or "make"

  # Input image:
  local iimg="${date}-prediction-check-${ex}"

  # Output image:
  local oimg="${date}-prediction-${which}-${ex}"

  # Dot data file:
  local dotfile="../00-DATA/2014-slumber-points-HUBI-BSTP.txt"

  # Horizontal scale information
  if [[ "/$1" == "/-hscale" ]]; then
    shift;
    local tshift="$1"; shift # Reference hour.
    local hshift="$1"; shift # H-coordinate of reference hour.
  else
    echo "** missing \"-hscale\"" 1>&2
    exit 1
  fi

  # Vertical scale information
  if [[ "/$1" == "/-vscale" ]]; then
    shift;
    local pmin="$1"; shift # Low reference price.
    local vbot="$1"; shift # V-coordinate of the low reference price.
    local pmax="$1"; shift # High reference price.
    local vtop="$1"; shift # V-coordinate of the high reference price.
  else
    echo "** missing \"-vscale\"" 1>&2
    exit 1
  fi

  # Decoration
  local deco_cmds=( )
  while [[ "/$1" == "/-deco" ]]; do
    shift;
    local hpdeco="$1"; shift # H-coordinate of deco (absolute, pixels).
    local vpdeco="$1"; shift # V-coordinate of deco (absolute, pixels).
    local hadeco="$1"; shift # H-alignment of deco.
    local vadeco="$1"; shift # V-alignment of deco.
    local imdeco="$1"; shift # Image name of deco.
    local mgdeco="$1"; shift # Magnification factor of deco.

    deco_cmds+=( -I ${hpdeco} ${vpdeco} ${hadeco} ${vadeco} ${imdeco} ${mgdeco} )
  done
  echo "deco_cmds=( " "${deco_cmds[@]}" " )" 1>&2

  # Balloon with time posted ("-tpost") or time valid ("-tpred")
  local labt_cmds=( )
  while [[ ( "/$1" == "/-tpost" ) || ( "/$1" == "/-tpred" ) ]]; do
    # if [[ "/$1" == "/-tpost" ]]; then clabt="#44aa88" ; else clabt="#33aadd" ; fi  # dark style.
    if [[ "/$1" == "/-tpost" ]]; then local clabt="#66cc88" ; else local clabt="#55ccdd" ; fi  # lite style.
    shift;

    # Label with time of last prediction:
    local mlabt="$1"; shift # Text (eg UTC time) to show in balloon.
    local tlabt="$1"; shift # Hour of balloon center.
    local vlabt="$1"; shift # V-coordinate of balloon dot.

    local hlabt=`tscale ${tshift} ${hshift} ${tlabt}`

    local vlabt_dot=${vlabt}
    local vlabt_lab=$(( ${vlabt_dot} - 160 ))
    labt_cmds+=( -color "${clabt}" -strokewidth 2 )
    labt_cmds+=( -L ${hlabt} ${vlabt_dot}  ${hlabt} ${vlabt_lab}  "${mlabt}" )
  done
  echo "labt_cmds=( " "${labt_cmds[@]}" " )" 1>&2

  # Prediction boxes:
  local rect_cmds=( ) # Commands to draw prediction boxes.
  while [[ "/$1" == "/-rect" ]]; do
    shift;
    local krect="$1"; shift # "@" for old, "+" for new, "=" for predicted.
    local trect="$1"; shift # Hour of rectangle center.
    local prect="$1"; shift # Price of rectangle center.
    local herr="$1"; shift  # Half-width of box (pixels).
    local verr="$1"; shift  # Half-height of box (pixels).

    local hrect=$(( ${hrect} + ${hshift} ))

    rect_cmds+=( `make_rect_cmds ${tshift} ${hshift} ${pmin} ${vbot} ${pmax} ${vtop} "${krect}" ${trect} ${prect} ${herr} ${verr}` )
  done
  echo "rect_cmds=( " "${rect_cmds[@]}" " )" 1>&2

  # Currency factor for trend lines:
  if [[ "/$1" == "/-linecurrf" ]]; then
    shift;
    local linecurrf="$1"; shift # Currency factor to be applied to prices of trend lines.
  else
    local linecurrf=1.00
  fi

  # Trend lines:
  local line_cmds=( )
  while [[ "/$1" == "/-line" ]]; do
    shift;
    local kline="$1"; shift # "@" for bad, "+" for new.
    local tline1="$1"; shift # Hour of line start.
    local pline1="$1"; shift # Price of line start.
    local tline2="$1"; shift # Hour of line end.
    local pline2="$1"; shift # Price of line end.

    if [[ "/${kline}" == "/@" ]]; then cline="#aa552299" ; else cline="#ff660099" ; fi

    local hline1=`tscale ${tshift} ${hshift} ${tline1}`
    local vline1=`pscale ${pmin} ${vbot} ${pmax} ${vtop} ${pline1} ${linecurrf}`

    local hline2=`tscale ${tshift} ${hshift} ${tline2}`
    local vline2=`pscale ${pmin} ${vbot} ${pmax} ${vtop} ${pline2} ${linecurrf}`

    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:
  local dot_cmds=( ) # Commands to draw dots at the slumber points and slumber times.
  while [[ "/$1" == "/-dots" ]]; do
    shift;

    local kdots="$1"; shift    # Dot type ("@", "+", etc.).
    local tmin="$1"; shift     # Hour of first dot to show.
    local pcol="$1"; shift     # Column of dot data file with the price to show.
    local dotcurrf="$1"; shift # Currency conversion factor for dot prices.
    local vvolb="$1"; shift    # V-coordinate of volume plot baseline (absolute, pixels).
    local vicon="$1"; shift    # V-coordinate of icon baseline (absolute, pixels).

    # Extract from file ${dotfile} the arguments to {make_dot_cmds}:
    local tmpfile="${tmp}.args"
    extract_slumber_data_file_columns.gawk \
        -v tmin=${tmin} \
        -v tcol=24 \
        -v csep="/" \
        -v cols="24,${pcol},26,28" \
        ${dotfile} \
      | sed -e 's: *$::' \
      > ${tmpfile}
    for lin in `cat ${tmpfile}` ; do
      args=( `echo ${lin} | tr '/' ' '` )
      # echo "args = ( ${args[@]} )" 1>&2
      dot_cmds+=(
        ` make_dot_cmds \
            ${tshift} ${hshift} \
            ${pmin} ${vbot} ${pmax} ${vtop} \
            ${vvolb} ${vicon} \
            ${dotcurrf} "${kdots}" \
            ${args[@]}
        `
      )
    done
  done
  echo "dot_cmds=( " "${dot_cmds[@]}" " )" 1>&2

  set echo

  annotate-pic.sh \
      ${iimg}.png 16 Black Yellow \
      "${deco_cmds[@]}" \
      \
      "${labt_cmds[@]}" \
      \
      "${line_cmds[@]}" \
      \
      "${dot_cmds[@]}" \
      \
      "${rect_cmds[@]}" \
      \
    > ${oimg}-annotated.png
  convert ${oimg}-annotated.png -resize '600x' -quality 98% ${oimg}-annotated.jpg
  gimp ${oimg}-annotated.png ${oimg}-annotated.png 
}

function fix_HUBI_plot(){
  # Fixes the Huobi plot to account for the missing hours 07:00 and 08:00 on Sunday 2014-03-23 (DDOS attack).
  
  # Hour scaling parameters:
  local tshift="$1"; shift # Reference hour.
  local hshift="$1"; shift # H-coordinate of reference hour.

  # Hour gap parameters:  
  local tkeep="$1"; shift   # Number of first hour after gap.
  local tgap="$1"; shift    # Number of hours in gap.
  
  # Margin parameters:
  local hleft="$1"; shift   # H-coordinate of left of rectangle to be shifted.
  local vtop="$1"; shift    # V-coordinate of top of rectangle to be shifted.
  local vsize="$1"; shift   # V-size of rectangle to be moved.

  local hkeep=`tscale ${tshift} ${hshift} ${tkeep}`      # First H-coordinate outside rect to shift.
  hkeep=$(( ${hkeep} - ( ${candlestep} / 2 ) ))          # Correction for candle half-width.
  local hgap=$(( ${tgap} * ${candlestep} ))              # Amount to shift (pixels).
  local hcrop=$(( ${hleft} + ${hgap} ))                  # Left H-coordinate of rectangle to crop.
  local hclear=$(( ${hkeep} - ${hgap} ))                 # Left H-coordinate of slice to be cleared.
  local hsize=$(( ${hkeep} - ${hcrop} ))                 # Width of rect to crop.
  
  local rawimg="${date}-prediction-check-HUBI-raw.png"
  local kukimg="${date}-prediction-check-HUBI.png"
  local tmpimg="${tmp}-aux.png"
  
  # Extract the rectangle to move to ${tmpimg}:
  convert ${rawimg} -crop "${hsize}x${vsize}+${hcrop}+${vtop}" +repage ${tmpimg}
  # gimp ${tmpimg}
  
  # Clear the rectangle and paste the shifted rectangle.
  convert ${rawimg} \
    -fill 'rgb(90,90,70)' -stroke none -draw "rectangle ${hclear},${vtop} $(( ${hkeep} - 1 )),$(( ${vtop} + ${vsize} - 1 ))" \
    \( ${tmpimg} -geometry "+${hleft}+${vtop}" \) \
    -composite \
    ${kukimg}
  
  gimp ${kukimg}
  rm -fv ${tmpimg}
}

# 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.

hscale_HUBI=( 3019 1346 )
hscale_BSTP=( 3019 1346 )

vscale_HUBI=( 2550  523  2850  139 )
vscale_BSTP=(  410  516   475   84 )

# Fix the Huobi plot to account for outage on 2014-03-23:
# fix_HUBI_plot ${hscale_HUBI[@]} 1953 2  0  60 608
# exit 0

# When there is no need to fix the Huobi plot:
# mv -v ${date}-prediction-check-HUBI{-raw,}.png
# exit 0

trend_lines=(
    -line  "@"   871  4873    883  4780 \
    -line  "@"   883  4780    895  4704 \
    -line  "@"   895  4704    907  4642 \
    -line  "@"   907  4642    919  4592 \
    -line  "@"   919  4592    931  4552 \
    -line  "@"   931  4552    943  4519 \
    -line  "@"   943  4519    955  4492 \
    -line  "@"   955  4492    967  4470 \
    \
    -line  "@"   967  4077    979  4072 \
    -line  "@"   979  4072    991  4066 \
    -line  "@"   991  4066   1003  4060 \
    -line  "@"  1003  4060   1015  4053 \
    -line  "@"  1015  4053   1027  4044 \
    -line  "@"  1027  4044   1039  4035 \
    -line  "@"  1039  4035   1051  4025 \
    -line  "@"  1051  4025   1063  4013 \
    -line  "@"  1063  4013   1075  3999 \
    -line  "@"  1075  3999   1087  3983 \
    -line  "@"  1087  3983   1099  3966 \
    -line  "@"  1099  3966   1111  3946 \
    -line  "@"  1111  3946   1123  3923 \
    -line  "@"  1123  3923   1135  3897 \
    -line  "@"  1135  3897   1147  3868 \
    -line  "@"  1147  3868   1159  3835 \
    -line  "@"  1159  3835   1171  3797 \
    -line  "@"  1171  3797   1183  3754 \
    -line  "@"  1183  3754   1195  3705 \
    -line  "@"  1195  3705   1207  3650 \
    -line  "@"  1207  3650   1219  3587 \
    -line  "@"  1219  3587   1231  3516 \
    -line  "@"  1231  3516   1243  3434 \
    -line  "@"  1243  3434   1255  3342 \
    \
    -line  "@"  1279  3964   1351  3175 \
    \
    -line  "@"  1351  3567   1471  3415 \
    \
    -line  "@"  1495  4045   1507  4026 \
    -line  "@"  1507  4026   1519  4004 \
    -line  "@"  1519  4004   1531  3977 \
    -line  "@"  1531  3977   1543  3945 \
    -line  "@"  1543  3945   1555  3907 \
    -line  "@"  1555  3907   1567  3862 \
    -line  "@"  1567  3862   1579  3808 \
    -line  "@"  1579  3808   1591  3744 \
    -line  "@"  1591  3744   1603  3667 \
    -line  "@"  1603  3667   1615  3576 \
    \
    -line  "@"  1615  3788   1627  3791 \
    -line  "@"  1627  3791   1639  3794 \
    -line  "@"  1639  3794   1651  3799 \
    -line  "@"  1651  3799   1663  3807 \
    -line  "@"  1663  3807   1675  3817 \
    -line  "@"  1675  3817   1687  3831 \
    -line  "@"  1687  3831   1699  3852 \
    -line  "@"  1699  3852   1711  3880 \
    -line  "@"  1711  3880   1723  3921 \
    -line  "@"  1723  3921   1735  3979 \
    \
    -line  "@"  1783  3865   1855  3674 \
    \
    -line  "@"  1855  3804   1867  3748 \
    -line  "@"  1867  3748   1879  3694 \
    -line  "@"  1879  3694   1891  3642 \
    -line  "@"  1891  3642   1903  3594 \
    -line  "@"  1903  3594   1915  3548 \
    -line  "@"  1915  3548   1927  3504 \
    -line  "@"  1927  3504   1939  3462 \
    -line  "@"  1939  3462   1951  3423 \
    \
    -line  "@"  1951  3449   2047  3608 \
    \
    -line  "@"  2047  3419   2053  3230 \
    -line  "@"  2053  3230   2059  3124 \
    -line  "@"  2059  3124   2065  3064 \
    -line  "@"  2065  3064   2071  3030 \
    -line  "@"  2071  3030   2077  3012 \
    -line  "@"  2077  3012   2083  3001 \
    -line  "@"  2083  3001   2089  2995 \
    -line  "@"  2089  2995   2095  2992 \
    -line  "@"  2095  2992   2101  2990 \
    -line  "@"  2101  2990   2107  2989 \
    -line  "@"  2107  2989   2113  2988 \
    -line  "@"  2113  2988   2119  2988 \
    \
    -line  "@"  2143  2708   2191  2912 \
    \
    -line  "@"  2215  2639   2311  2896 \
    \
    -line  "@"  2311  2813   2479  2591 \
    \
    -line  "@"  2527  3238   2599  2938 \
    \
    -line  "@"  2599  3145   2743  3073 \
    \
    -line  "@"  2815  2691   2821  2704 \
    -line  "@"  2821  2704   2827  2716 \
    -line  "@"  2827  2716   2833  2727 \
    -line  "@"  2833  2727   2839  2738 \
    -line  "@"  2839  2738   2845  2748 \
    -line  "@"  2845  2748   2851  2758 \
    -line  "@"  2851  2758   2857  2767 \
    -line  "@"  2857  2767   2863  2775 \
    -line  "@"  2863  2775   2869  2783 \
    -line  "@"  2869  2783   2875  2791 \
    -line  "@"  2875  2791   2881  2798 \
    -line  "@"  2881  2798   2887  2804 \
    -line  "@"  2887  2804   2893  2811 \
    -line  "@"  2893  2811   2899  2817 \
    -line  "@"  2899  2817   2905  2822 \
    -line  "@"  2905  2822   2911  2827 \
     \
    -line  "@"  2911  2761   3007  2649 \
     \
    -line  "+"  3007  2672   3049  2672 \
  )

#   -deco 4088   88 0.5 0.5 ../icons/helix-wp-edit-200.png 0.60 \
#   -deco 4088  368 0.5 0.5 ../icons/ophthalmosaurus-wp-edit-200.png 0.60 \
#   -deco  740  280 0.5 0.5 ../icons/germanodactylus-wp-edit-200.png 0.60 \
#   -deco  740  485 0.5 0.5 ../icons/raphus-wp-edit-200.png 0.60 \
#   -deco 1172  515 0.5 1.0 ../icons/struthio-niloticus-wp-edit-200.png 0.65 \
#   -deco 1000  531 0.5 1.0 ../icons/milnesium-wp-edit-400.png 0.333333 \
#   -deco  737  517 0.5 1.0 ../icons/bufoceratias-wp-edit-400.png 0.666667 \
#   -deco 1865  365 0.5 1.0 ../icons/panda-wp-edit-400.png 0.50 \
#   -deco 1805  387 0.5 1.0 ../icons/panda2-wp-edit-200.png 0.667 \
#   -deco 1805  387 0.5 1.0 ../icons/neotragus-tr-edit-200.png 0.667 \
      
if [[ ${hubi} -ne 0 ]]; then
  currf=1.00
  annot HUBI check \
    -hscale ${hscale_HUBI[@]} \
    -vscale ${vscale_HUBI[@]} \
    \
    -deco 1435  335 0.5 1.0 ../icons/helix-wp-edit-200.png 0.800 \
    \
    -tpost "21:33" 2998 337 \
    \
    -rect "@"   955  4510 9 9 \
    -rect "@"   979  4450 9 9 \
    -rect "@"  1028  4075 9 9 \
    -rect "@"  1051  3970 9 9 \
    -rect "@"  1075  3750 9 9 \
    -rect "@"  1099  3630 9 9 \
    -rect "@"  1123  3915 9 9 \
    -rect "@"  1147  3630 9 9 \
    -rect "@"  1171  3890 9 9 \
    -rect "@"  1195  3775 9 9 \
    -rect "@"  1219  3800 9 9 \
    -rect "@"  1243  3772 9 9 \
    -rect "@"  1267  3396 9 9 \
    -rect "@"  1291  3368 9 9 \
    -rect "@"  1315  3809 9 9 \
    -rect "@"  1339  3807 9 9 \
    -rect "@"  1363  3804 9 9 \
    -rect "@"  1387  3802 9 9 \
    -rect "@"  1387  2781 9 9 \
    -rect "@"  1411  3533 9 9 \
    -rect "@"  1435  3522 9 9 \
    -rect "@"  1459  3515 9 9 \
    -rect "@"  1483  3400 9 9 \
    -rect "@"  1507  3370 9 9 \
    -rect "@"  1531  4050 9 9 \
    -rect "@"  1555  3872 9 9 \
    -rect "@"  1579  3899 9 9 \
    -rect "@"  1603  3891 9 9 \
    -rect "@"  1627  3596 9 9 \
    -rect "@"  1651  3938 9 9 \
    -rect "@"  1675  4073 9 9 \
    -rect "@"  1675  3597 9 9 \
    -rect "@"  1699  3788 9 9 \
    -rect "@"  1723  3783 9 9 \
    -rect "@"  1747  3918 9 9 \
    -rect "@"  1843  3674 9 9 \
    -rect "@"  1867  3699 9 9 \
    -rect "@"  1891  3725 9 9 \
    -rect "@"  1915  3630 9 9 \
    -rect "@"  1939  3447 9 9 \
    -rect "@"  1963  3386 9 9 \
    -rect "@"  1987  3316 9 9 \
    -rect "@"  2011  3525 9 9 \
    -rect "@"  2035  3596 9 9 \
    -rect "@"  2059  3628 9 9 \
    -rect "@"  2083  3667 9 9 \
    -rect "@"  2107  2858 9 9 \
    -rect "@"  2131  2987 9 9 \
    -rect "@"  2179  2759 9 9 \
    -rect "@"  2203  2963 9 9 \
    -rect "@"  2275  2796 9 9 \
    -rect "@"  2299  2874 9 9 \
    -rect "@"  2323  2929 9 9 \
    -rect "@"  2347  2782 9 9 \
    -rect "@"  2371  2808 9 9 \
    -rect "@"  2395  2717 9 9 \
    -rect "@"  2443  2634 9 9 \
    -rect "@"  2467  2609 9 9 \
    -rect "@"  2491  2575 9 9 \
    -rect "@"  2515  2919 9 9 \
    -rect "@"  2539  3345 9 9 \
    -rect "@"  2563  3198 9 9 \
    -rect "@"  2587  3001 9 9 \
    -rect "@"  2611  2888 9 9 \
    -rect "@"  2635  2788 9 9 \
    -rect "@"  2659  3097 9 9 \
    -rect "@"  2683  3091 9 9 \
    -rect "@"  2707  3112 9 9 \
    -rect "@"  2731  3059 9 9 \
    -rect "@"  2755  3067 9 9 \
    -rect "@"  2779  2814 9 9 \
    -rect "@"  2803  2876 9 9 \
    -rect "@"  2851  2684 9 9 \
    -rect "@"  2875  2720 9 9 \
    -rect "@"  2899  2802 9 9 \
    -rect "@"  2923  2837 9 9 \
    -rect "@"  2947  2744 9 9 \
    -rect "@"  2971  2600 9 9 \
    -rect "@"  2995  2676 9 9 \
    \
    -rect "+"  3019  2635 9 9 \
    \
    -rect "="  3043  2672 9 9 \
    \
    -linecurrf 1.00 \
    \
    ${trend_lines[@]} \
    \
    -dots "+" 475 8 1.00 672 565
fi

if [[ ${bstp} -ne 0 ]]; then
  annot BSTP check \
    -hscale ${hscale_BSTP[@]} \
    -vscale ${vscale_BSTP[@]} \
    \
    -tpost "21:33" 2998 334 \
    \
    -rect "@"   955  700 9 9 \
    -rect "@"   979  690 9 9 \
    -rect "@"  1028  640 9 9 \
    -rect "@"  1051  650 9 9 \
    -rect "@"  1075  615 9 9 \
    -rect "@"  1099  600 9 9 \
    -rect "@"  1123  640 9 9 \
    -rect "@"  1147  590 9 9 \
    -rect "@"  1171  635 9 9 \
    -rect "@"  1195  615 9 9 \
    -rect "@"  1219  621 9 9 \
    -rect "@"  1243  616 9 9 \
    -rect "@"  1267  555 9 9 \
    -rect "@"  1291  550 9 9 \
    -rect "@"  1315  622 9 9 \
    -rect "@"  1339  622 9 9 \
    -rect "@"  1363  622 9 9 \
    -rect "@"  1387  621 9 9 \
    -rect "@"  1387  454 9 9 \
    -rect "@"  1411  577 9 9 \
    -rect "@"  1435  575 9 9 \
    -rect "@"  1459  572 9 9 \
    -rect "@"  1483  556 9 9 \
    -rect "@"  1507  551 9 9 \
    -rect "@"  1531  663 9 9 \
    -rect "@"  1555  633 9 9 \
    -rect "@"  1579  637 9 9 \
    -rect "@"  1603  636 9 9 \
    -rect "@"  1627  588 9 9 \
    -rect "@"  1651  656 9 9 \
    -rect "@"  1675  679 9 9 \
    -rect "@"  1675  600 9 9 \
    -rect "@"  1699  631 9 9 \
    -rect "@"  1723  631 9 9 \
    -rect "@"  1747  640 9 9 \
    -rect "@"  1843  606 9 9 \
    -rect "@"  1867  610 9 9 \
    -rect "@"  1891  615 9 9 \
    -rect "@"  1915  591 9 9 \
    -rect "@"  1939  566 9 9 \
    -rect "@"  1963  551 9 9 \
    -rect "@"  1987  541 9 9 \
    -rect "@"  2011  576 9 9 \
    -rect "@"  2035  589 9 9 \
    -rect "@"  2059  593 9 9 \
    -rect "@"  2083  600 9 9 \
    -rect "@"  2107  470 9 9 \
    -rect "@"  2131  493 9 9 \
    -rect "@"  2179  452 9 9 \
    -rect "@"  2203  490 9 9 \
    -rect "@"  2275  461 9 9 \
    -rect "@"  2299  466 9 9 \
    -rect "@"  2323  472 9 9 \
    -rect "@"  2347  452 9 9 \
    -rect "@"  2371  456 9 9 \
    -rect "@"  2395  441 9 9 \
    -rect "@"  2443  422 9 9 \
    -rect "@"  2467  417 9 9 \
    -rect "@"  2491  414 9 9 \
    -rect "@"  2515  469 9 9 \
    -rect "@"  2539  529 9 9 \
    -rect "@"  2563  513 9 9 \
    -rect "@"  2587  483 9 9 \
    -rect "@"  2611  467 9 9 \
    -rect "@"  2635  450 9 9 \
    -rect "@"  2659  496 9 9 \
    -rect "@"  2683  494 9 9 \
    -rect "@"  2707  494 9 9 \
    -rect "@"  2731  487 9 9 \
    -rect "@"  2755  488 9 9 \
    -rect "@"  2779  447 9 9 \
    -rect "@"  2803  471 9 9 \
    -rect "@"  2851  443 9 9 \
    -rect "@"  2875  439 9 9 \
    -rect "@"  2899  450 9 9 \
    -rect "@"  2923  461 9 9 \
    -rect "@"  2947  451 9 9 \
    -rect "@"  2971  427 9 9 \
    -rect "@"  2995  434 9 9 \
    \
    -rect "+"  3019  427 9 9 \
    \
    -rect "="  3043  430 9 9 \
    \
    -dots "+" 475  14 1.00 672 -1

# From BSTP:
#     -linecurrf 6.10 \
#     ${trend_lines[@]} \
     
fi
