#! /bin/bash
# Last edited on 2021-03-04 18:22:49 by jstolfi

# Reads a eight-colum data file, plots curvature as function of arc length.

cmd="$0"; cmd="${cmd##*/}"

imname="$1"; shift  # Image name without smoothing radius, directory, tag, extension.
rsmooth="$1"; shift # Smoothing window radius.
show=$1; shift      # Display plot if 1, silent if 0

# END COMMAND LINE PARSING
# ----------------------------------------------------------------------

rsmooth=`printf "%04d" "${rsmooth}"`
anfile="out/${imname}-sm${rsmooth}-cvt.txt"

tag="cvt-by-theta"

# Prefix for temporary file names
tmp="/tmp/$$"

hPix=1200
vPix=1200

tfile="${tmp}.png"
export GDFONTPATH="tt-fonts"

gnuplot <<EOF
  set terminal png truecolor size ${hPix},${vPix} font "arial,18"
  set output "${tfile}"
  set nokey
  
  set yrange [0:]
  
  set xlabel "X (radians)"
  set ylabel "Y (1/pixels)"

  plot \
    "${anfile}" using 9:8 title "raw" with linespoints lt 1 lw 1 pt 7 ps 0.75 lc rgb '#0022cc'

EOF

if [[ -s ${tfile} ]]; then
  pfile="out/${imname}-sm${rsmooth}-${tag}.png"
  convert ${tfile} -resize '50%' ${pfile}
  if [[ ${show} -ne 0 ]]; then display ${pfile}; fi
  rm ${tfile}
else
  echo "** plot failed" 1>&2 ; exit 1
fi
