#! /bin/bash
# Last edited on 2021-03-05 20:43:45 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.
rsmoothd="$1"; shift # Smoothing window radius: "0300", "0600", etc.
show=$1; shift       # Display plot if 1, silent if 0

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

anfile="out/${imname}-sm${rsmoothd}-cvt.txt"

tag="cvt-by-length"

# 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 (pixels)"
  set ylabel "Y (1/pixels)"

  plot \
    "${anfile}" using 1: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${rsmoothd}-${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
