#! /bin/bash
# Last edited on 2021-03-30 03:40:45 by jstolfi

# Reads a file of points of a curve.  Plots the {X} and {Y} coords as a
# function of index.

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

dfile="$1"; shift    # Name of file with data points.
oprefix="$1"; shift  # Prefix for output file names, with "out/", sans extension.
show=$1; shift       # Display plot if 1, silent if 0

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

export PATH=".:..:../..:${PATH}"

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

hPix=1800
vPix=800

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

for mag in 0 1 ; do

  # Plot the coordinates
  # If {mag} is 1, plot enlarged version of some points.

  if [[ ${mag} -ne 0 ]]; then
    xmin="-5"; xmax="100" 
  else 
    xmin="-100"; xmax=""
  fi

  gnuplot <<EOF
    set terminal png truecolor size ${hPix},${vPix} font "arial,18"
    set output "${tfile}"

    set xrange[${xmin}:${xmax}]

    set grid ytics lt 1 lw 2 lc rgb '#cccccc'

    mag = ${mag}

    lwd = 1
    psz = 0 + 0.5*mag

    plot \
      "${dfile}" using 1:2 title "X" with linespoints lt 1 lw (lwd) pt 7 ps (psz) lc rgb '#dd2200', \
      ""         using 1:3 title "Y" with linespoints lt 1 lw (lwd) pt 7 ps (psz) lc rgb '#2277a00'

EOF

  if [[ -s ${tfile} ]]; then
    pfile="${oprefix}-xy-mag${mag}.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
done
