#! /bin/bash -eu
# Last edited on 2026-03-14 11:39:43 by stolfi

# Reads the join files "

kind="$1"; shift # Kind of string: either 'words' or 'roots'

echo "plotting ${kind} frequencies hea x heb ..." 1>&2

ct_dir="st_${kind}"
ex_dir="work/VoynichNinja/096"
mkdir -p ${ex_dir}

oct_file="${ct_dir}/hea-0-heb-0-parags-${kind}-paired-by-item.oct"

temp="/tmp/$$"

export GDFONTPATH="${HOME}/ttf"
temp_plot_file="${temp}_plot.png"

if [[ "/${kind}" == "/words" ]]; then
  color='#0077bb'; fr_max=0.12003
elif [[ "/${kind}" == "/roots" ]]; then
  color='#ff2200'; fr_max=0.55003
fi

plot_size=1200
tsize=$(( ${plot_size} * 2 ))

ticvals=(
    "0.0002" 0.0002 , 
    "0.0005" 0.0005 , 
    "0.002" 0.002 , 
    "0.005" 0.005 ,  
    "0.02" 0.02 , 
    "0.05" 0.05 , 
    "0.2" 0.2 , 
    "0.5" 0.5
  )

gnuplot <<EOF
  set term pngcairo size ${tsize},${tsize} linewidth 2.0 font "Arial,30" noenhanced
  set size ratio -1
  set output "${temp_plot_file}"
  set xrange [0.00025:${fr_max}]
  set yrange [0.00025:${fr_max}]
  set logscale 
  set xtics 10
  set mxtics 10
  set xtics add ( ${ticvals[@]} )
  set ytics 10 rotate by 90 center
  set mytics 10
  set ytics add ( ${ticvals[@]} )
  set grid xtics mxtics lt 1 lc rgb "#44aaff", lt 1 lc rgb "#aaddff"
  set grid ytics mytics lt 1 lc rgb "#44aaff", lt 1 lc rgb "#aaddff"
  set lmargin 6.5
  fre(k) = column(k) + 0.0000001 # To avoid log of zero.
  set xlabel  "${kind/s} frequencies in Herbal-A" font "Arial,40"
  set ylabel  "${kind/s} frequencies in Herbal-B" font "Arial,40" offset 2,0
  plot \
    (x) notitle with lines lw 4 lc rgb "#77aa33", \
    "${oct_file}" using (fre(2)):(fre(5)) notitle \
      with points pt 7 ps 3.0 \
      lc rgb "${color}", \
    "" using (fre(2)):(fre(5)):6 notitle \
      with labels rotate by 45 offset character 1,0 left \
      lc rgb "#000000"
  quit
EOF

plot_file="${ct_dir}/hea_heb_freqs_plot.png"
export_plot_file="${ex_dir}/hea_heb_${kind}_freqs_plot.jpg"
rm -f ${export_plot_file}
if [[ -s ${temp_plot_file} ]]; then
  convert ${temp_plot_file} -resize '50%' ${plot_file}
  convert ${plot_file} ${export_plot_file}
  display -title '%f' ${plot_file}
else
  echo "** ${temp_plot_file} not created" 1>&2; exit 1
fi
rm -f ${temp}-*
 
