#! /bin/bash
# Last edited on 2014-04-06 19:46:44 by stolfilocal

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

tmp=/tmp/$$

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

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

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

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

  harg=$(( ( ( ${targ} - ${tshift} ) * ${candlewidth} ) + ${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:
  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.
  currf="$1"; shift # Currency conversion factor.

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

  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:
  tshift="$1"; shift # Reference hour.
  hshift="$1"; shift # H-coordinate of reference hour.

  # Price 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.
  trect="$1"; shift # Hour of rectangle center.
  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

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

  if [[ "/${krect}" == "/@" ]]; then
    crect="#5566cc"
    lwdt=2
  elif [[ "/${krect}" == "/+" ]]; then
    crect="#0066ff"
    lwdt=3
  elif [[ "/${krect}" == "/=" ]]; then
    crect="#ff00aa"
    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:
  tshift="$1"; shift # Reference hour.
  hshift="$1"; shift # H-coordinate of reference hour.

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

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

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

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

  # Diameter of dot (pt):
  minlw=8.0
  lwdot=`echo 'w='"${wtdot}"';r=32.0*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
    cdot="#77885566"
  elif [[ "/${kdot}" == "/+" ]]; then
    cdot="#ffff0099"
  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}
    # albertosaurus-wp-edit-100.png
    icon_img="nretnal-wp-edit-100.png"; # Icon image.
    icon_img_vsz=108;  # Icon V-size in pixels.
    icon_alh=0.50; # H-alignment of icon.
    icon_alv=0.00; # V-alignment of icon.

    # Compute a relative icon area {icon_area = srdot/maxsr}, clipped at 1.0:
    maxsr=0.015;  # Icon area is proportional to {srdot} up to this {srdot}, then constant.
    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:
    maxmg=0.400; # Max icon magnification.
    minmg=0.050; # Min icon magnification.
    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`
    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:
    vtick_bot=$(( ${vvolb} - 20 )) # V-coordinate of bottom of tick line.
    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.

  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}"

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

  # Horizontal scale information
  if [[ "/$1" == "/-hscale" ]]; then
    shift;
    tshift="$1"; shift # Reference hour.
    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;
    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.
  else
    echo "** missing \"-vscale\"" 1>&2
    exit 1
  fi

  # Decoration
  deco_cmds=( )
  while [[ "/$1" == "/-deco" ]]; do
    shift;
    hpdeco="$1"; shift # H-coordinate of deco (absolute, pixels).
    vpdeco="$1"; shift # V-coordinate of deco (absolute, pixels).
    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.

    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")
  labt_cmds=( )
  while [[ ( "/$1" == "/-tpost" ) || ( "/$1" == "/-tpred" ) ]]; do
    if [[ "/$1" == "/-tpost" ]]; then clabt="#44ffaa" ; else clabt="#33ddff" ; fi
    shift;

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

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

    vlabt_dot=${vlabt}
    vlabt_lab=$(( ${vlabt_dot} - 80 ))
    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:
  rect_cmds=( ) # Commands to draw prediction boxes.
  while [[ "/$1" == "/-rect" ]]; do
    shift;
    krect="$1"; shift # "@" for old, "+" for new, "=" for predicted.
    trect="$1"; shift # Hour of rectangle center.
    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 ${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;
    linecurrf="$1"; shift # Currency factor to be applied to prices of trend lines.
  else
    linecurrf=1.00
  fi

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

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

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

    hline2=`tscale ${tshift} ${hshift} ${tline2}`
    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:
  dot_cmds=( ) # Commands to draw dots at the slumber points and slumber times.
  while [[ "/$1" == "/-dots" ]]; do
    shift;

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

    # Extract from file ${dotfile} the arguments to {make_dot_cmds}:
    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
  gimp ${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:
  tshift="$1"; shift # Reference hour.
  hshift="$1"; shift # H-coordinate of reference hour.

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

  hkeep=`tscale ${tshift} ${hshift} ${tkeep}`      # First H-coordinate outside rect to shift.
  hkeep=$(( ${hkeep} - ( ${candlewidth} / 2 ) ))   # Correction for candle half-width.
  hgap=$(( ${tgap} * ${candlewidth} ))             # Amount to shift (pixels).
  hcrop=$(( ${hleft} + ${hgap} ))                  # Left H-coordinate of rectangle to crop.
  hclear=$(( ${hkeep} - ${hgap} ))                 # Left H-coordinate of slice to be cleared.
  hsize=$(( ${hkeep} - ${hcrop} ))                 # Width of rect to crop.
  
  rawimg="${date}-prediction-check-HUBI-raw.png"
  kukimg="${date}-prediction-check-HUBI.png"
  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=( 2299  656 )
hscale_BSTP=( 2299  656 )

vscale_HUBI=( 2400  534  3000  104 )
vscale_BSTP=(  420  502   490  134 )

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

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

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

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   2335  2961 \
  )
    
if [[ ${hubi} -ne 0 ]]; then
  currf=1.00
  annot HUBI check \
    -hscale ${hscale_HUBI[@]} \
    -vscale ${vscale_HUBI[@]} \
    \
    -deco  740 280 0.5 0.5 germanodactylus-wp-edit-200.png 0.60 \
    \
    -tpost "23:41" 2280 170 \
    \
    -rect "@"   955  4510 11 11 \
    -rect "@"   979  4450 11 11 \
    -rect "@"  1028  4075 11 11 \
    -rect "@"  1051  3970 11 11 \
    -rect "@"  1075  3750 11 11 \
    -rect "@"  1099  3630 11 11 \
    -rect "@"  1123  3915 11 11 \
    -rect "@"  1147  3630 11 11 \
    -rect "@"  1171  3890 11 11 \
    -rect "@"  1195  3775 11 11 \
    -rect "@"  1219  3800 11 11 \
    -rect "@"  1243  3772 11 11 \
    -rect "@"  1267  3396 11 11 \
    -rect "@"  1291  3368 11 11 \
    -rect "@"  1315  3809 11 11 \
    -rect "@"  1339  3807 11 11 \
    -rect "@"  1363  3804 11 11 \
    -rect "@"  1387  3802 11 11 \
    -rect "@"  1387  2781 11 11 \
    -rect "@"  1411  3533 11 11 \
    -rect "@"  1435  3522 11 11 \
    -rect "@"  1459  3515 11 11 \
    -rect "@"  1483  3400 11 11 \
    -rect "@"  1507  3370 11 11 \
    -rect "@"  1531  4050 11 11 \
    -rect "@"  1555  3872 11 11 \
    -rect "@"  1579  3899 11 11 \
    -rect "@"  1603  3891 11 11 \
    -rect "@"  1627  3596 11 11 \
    -rect "@"  1651  3938 11 11 \
    -rect "@"  1675  4073 11 11 \
    -rect "@"  1675  3597 11 11 \
    -rect "@"  1699  3788 11 11 \
    -rect "@"  1723  3783 11 11 \
    -rect "@"  1747  3918 11 11 \
    -rect "@"  1843  3674 11 11 \
    -rect "@"  1867  3699 11 11 \
    -rect "@"  1891  3725 11 11 \
    -rect "@"  1915  3630 11 11 \
    -rect "@"  1939  3447 11 11 \
    -rect "@"  1963  3386 11 11 \
    -rect "@"  1987  3316 11 11 \
    -rect "@"  2011  3525 11 11 \
    -rect "@"  2035  3596 11 11 \
    -rect "@"  2059  3628 11 11 \
    -rect "@"  2083  3667 11 11 \
    -rect "@"  2107  2858 11 11 \
    -rect "@"  2131  2987 11 11 \
    -rect "@"  2179  2759 11 11 \
    -rect "@"  2203  2963 11 11 \
    -rect "@"  2275  2796 11 11 \
    \
    -rect "+"  2299  2874 11 11 \
    \
    -rect "="  2323  2929 11 11 \
    \
    -linecurrf 1.00 \
    \
    ${trend_lines[@]} \
    \
    -dots "+" 475 8 1.00 666 560
fi

if [[ ${bstp} -ne 0 ]]; then
  annot BSTP check \
    -hscale ${hscale_BSTP[@]} \
    -vscale ${vscale_BSTP[@]} \
    \
    -tpost "23:41" 2280 250 \
    \
    -rect "@"   955  700 11 11 \
    -rect "@"   979  690 11 11 \
    -rect "@"  1028  640 11 11 \
    -rect "@"  1051  650 11 11 \
    -rect "@"  1075  615 11 11 \
    -rect "@"  1099  600 11 11 \
    -rect "@"  1123  640 11 11 \
    -rect "@"  1147  590 11 11 \
    -rect "@"  1171  635 11 11 \
    -rect "@"  1195  615 11 11 \
    -rect "@"  1219  621 11 11 \
    -rect "@"  1243  616 11 11 \
    -rect "@"  1267  555 11 11 \
    -rect "@"  1291  550 11 11 \
    -rect "@"  1315  622 11 11 \
    -rect "@"  1339  622 11 11 \
    -rect "@"  1363  622 11 11 \
    -rect "@"  1387  621 11 11 \
    -rect "@"  1387  454 11 11 \
    -rect "@"  1411  577 11 11 \
    -rect "@"  1435  575 11 11 \
    -rect "@"  1459  572 11 11 \
    -rect "@"  1483  556 11 11 \
    -rect "@"  1507  551 11 11 \
    -rect "@"  1531  663 11 11 \
    -rect "@"  1555  633 11 11 \
    -rect "@"  1579  637 11 11 \
    -rect "@"  1603  636 11 11 \
    -rect "@"  1627  588 11 11 \
    -rect "@"  1651  656 11 11 \
    -rect "@"  1675  679 11 11 \
    -rect "@"  1675  600 11 11 \
    -rect "@"  1699  631 11 11 \
    -rect "@"  1723  631 11 11 \
    -rect "@"  1747  640 11 11 \
    -rect "@"  1843  606 11 11 \
    -rect "@"  1867  610 11 11 \
    -rect "@"  1891  615 11 11 \
    -rect "@"  1915  591 11 11 \
    -rect "@"  1939  566 11 11 \
    -rect "@"  1963  551 11 11 \
    -rect "@"  1987  541 11 11 \
    -rect "@"  2011  576 11 11 \
    -rect "@"  2035  589 11 11 \
    -rect "@"  2059  593 11 11 \
    -rect "@"  2083  600 11 11 \
    -rect "@"  2107  470 11 11 \
    -rect "@"  2131  493 11 11 \
    -rect "@"  2179  452 11 11 \
    -rect "@"  2203  490 11 11 \
    -rect "@"  2275  461 11 11 \
    \
    -rect "+"  2299  466 11 11 \
    \
    -rect "="  2323  472 11 11 \
    \
    -dots "+" 475  14 1.00 666 -1

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