#! /bin/bash -f
# Last edited on 2026-02-18 03:10:29 by stolfi

# Plots the mean and principal component of a set of spectra.
#
# If ${hp1} and ${hp2} are not "NONE", considers only
# the rectangle of the image with corners ${hp1} and ${hp2}.
#

page="$1"; shift
clip="$1"; shift
masks=( "$@" ); shift

nm=${#masks[@]}
echo ${nm} '[' ${masks[@]} ']' 1>&2

temp="/tmp/$$"

masktag="`echo ${masks[@]} | tr ' ' '-'`"
cdir="pages/${page}/clips/${clip}"

cd ${cdir}

ofile="plot-${masktag}.png"

colors=( \
  ff2200 0033ff 006633 ee8800 aa5500 \
  55aa00 008877 8800ff dd0044 ff55ff \
  777777 \
)
if [[ ${nm} -gt ${#colors[@]} ]]; then 
  echo "** ran out of line colors" 1>&2; exit 1;
fi 

export GDFONTPATH="${HOME}/ttf"

ndev=3  # Multiplier for principal components

echo "  creating plots ..." 1>&2
for ip in 0 1 2 3 4 5 ; do

  tfile_ip="${temp}-plot-${ip}.png"
  
  # Create the plot commands:
  gfile="${temp}.gpl"

  sep=""
  printf "plot" > ${gfile}
  im=0
  while [[ $im -lt $nm ]]; do
    mask=${masks[${im}]}
    ifile="masks/${mask}/analysis.txt"
    printf "%s "'\\'"\n" "${sep}" >> ${gfile}
    printf "  '${ifile}' using (xplot(3,2)):(yplot(0)) title '${masks[im]}'" >> ${gfile}
    printf " with linespoints pt 7 ps 1.0 lw 2 lc rgb '#%s'" "${colors[im]}"  >> ${gfile}
    sep=","
    im=$(( ${im} + 1 ))
  done
  printf "\n" >> ${gfile}
  cat ${gfile} 1>&2
  
  gnuplot <<EOF
    set term png size 1800,600 linewidth 2.0 font "Arial" 21 noenhanced
    set output "${tfile_ip}"
    ip = ${ip}
    set xrange [350:1030]
    set xzeroaxis lt 0 lw 1 lc rgb '#000000' 
    # Choose major tics:
    set xtics ( \
      "300" 300 0, \
      "365\nUV"  365 0, \
      "450\nRB"  450 0, \
      "470\nLB"  470 0, \
      "505\nCN"  505 0, \
      "535\nGN"  535 0, \
      "570\nAM"  570 0, \
      "625\nRD"  625 0, \
      "700\nIR1" 700 0, \
      "735\nIR2" 735 0, \
      "780\nIR3" 780 0, \
      "870\nIR4" 870 0, \
      "940\nIR5" 940 0, \
      "1200" 1200 0 \
    )
    set lmargin 7
    set bmargin 3
    ndev = ${ndev}
    xplot(kw,ki) = column(kw) + 5*column(ki) # {kw} = wlen, {ki} = illum
    savg(im) = column(4 + 0)
    smin(im) = column(4 + 1)
    smax(im) = column(4 + 2)
    rdev(im) = column(4 + 3)*ndev
    tdev(im) = column(4 + 4)*ndev
    spc0(im) = column(4 + 5)*ndev
    
    samp(im) = savg(im) - spc0(im)
    
    if (ip == 0) {
      yplot(im) = savg(im)
      set title "Mean spectra"
      set ytics 0.1
      set yrange [-0.1:+1.1]
    } else if (ip == 1) {
      yplot(im) = smin(im)
      set title "Darkest spectra"
      set ytics 0.1
      set yrange [-0.1:+1.1];
    } else if (ip == 2) {
      yplot(im) = smax(im)
      set title "Lightest spectra"
      set ytics 0.1
      set yrange [-0.1:+1.1];
    } else if (ip == 3) {
      yplot(im) = spc0(im)
      set title "Principal component (×${ndev})"
      set ytics 0.1
      set yrange [-0.1:+0.8];
    } else if (ip == 4) {
      yplot(im) = samp(im)
      set title "Mean spectrum minus princ comp (×${ndev})"
      set ytics 0.1
      set yrange [-0.3:+1.2];
    } else if (ip == 5) {
      yplot(im) = tdev(im)
      set title "Component residual (×${ndev})"
      set ytics 0.02
      set yrange [-0.04:+0.20];
    } else {
      kaboom(418)
    }
    set grid xtics lc rgb "#88ccff", lc rgb "#aaddff"
    set grid ytics lc rgb "#88ccff", lc rgb "#aaddff"
    
    load "${gfile}"
    quit
EOF
done

echo "  joining plots ..." 1>&2
tpfile_L=${temp}-012.png
tpfile_R=${temp}-345.png
convert -append ${temp}-plot-0.png ${temp}-plot-1.png ${temp}-plot-2.png -resize '50%' ${tpfile_L}
convert -append ${temp}-plot-3.png ${temp}-plot-4.png ${temp}-plot-5.png -resize '50%' ${tpfile_R}
convert +append ${tpfile_L} ${tpfile_R} ${ofile}

rm -f ${temp}*.*

if [[ -s ${ofile} ]]; then
  display -title '%f' ${ofile}
else
  echo "** file ${ofile} not created" 1>&2; exit 1
fi


