#! /bin/bash
# Last edited on 2022-04-14 18:04:05 by stolfi

# Plots Tcpu as a function of the max band width mu

# Usage: plot_tcpus.sh {DIRECTORY} {DELTA} {MUMAX} {DATASET_NAME}...

# Expect to find a file {DIRECTORY}/{DS}/raw.txt
# for each dataset {DS}, with one {mu} and one {Tcpu} per line.

plotdir="$1"; shift    # Directory for input data and plots.
Delta="$1"; shift  # Delta for which the data was collected.
mumax="$1"; shift  # Largest {mu} value in plot.
datasets=( "$@" )  # Datasets in the order of Table 1.

echo "datasets = ${datasets[@]}" 1>&2 

nsets=${#datasets[@]}

pngname="tcpus-delta${Delta}.png"
pngfile="${plotdir}/${pngname}"

pdfname="tcpus-delta${Delta}.pdf"
pdffile="${plotdir}/${pdfname}"

tmp="/tmp/$$"

datafile="${tmp}-data.txt"
# Create a file ${datafile} with the data points.
# Each line has "{id} {dataset} {mu} {Tcpu}"
# Where {id} is the curve index from 1, as in Table 1.
# There is a blank line between curves.

echo "datafile = ${datafile}" 1>&2
rm -f ${datafile}
ip=0 # Curve index, from 0.
while [[ ip -lt ${#datasets[@]} ]]; do
  ds=${datasets[${ip}]}
  dsdir=${plotdir}/${ds}_delta${Delta}
  rawfile=${dsdir}/raw.txt
  cat ${rawfile} \
    | sort -k1,1n \
    | gawk \
        -v ds="${ds}" \
        -v ip="${ip}" \
        ' /[0-9]/ {
            mu = $1; Tcpu = $2; id = ip+1; 
            printf "%2d %-10s %3d %10s\n", id, ds, mu, Tcpu;
          }
        ' \
    >> ${datafile}
  printf "\n" >> ${datafile}
  ip=$(( $ip + 1 ))
done

segmfile="${tmp}-segm.txt"
# Create a file ${segmfile} with endpoints of the dotted line for each curve.
# Two lines for each dataset.
# The first line line has "{ìd} {dataset} 0 {Tlab}" where {id} is the curve index in Table 1, from 1
# and {Tlab} is the {Tcpu} value of the position where the curve label should be printed.
# The second line line has "{ìd} {dataset} {mu0} {Tcpu0}" where
# {(mu0,Tcpu0)} is the last (rightmost) data point of the curve.
# There will be one blank line betwen datasets.

echo "segmfile = ${segmfile}" 1>&2
rm -f ${segmfile}
cat ${datafile} \
  | sort -k4,4gr \
  | gawk \
      -v ip="${ip}" \
      -v mumax="${mumax}" \
      ' BEGIN { Tlab = 1250; rlab = 0.70; }
        /[0-9]/ {
          id = $1; ds = $2; mu = $3; Tcpu = $4;
          if (mu+0 == mumax+0) {
            printf "%2d %-10s %3d %10s\n", id, ds, 0, Tlab;
            printf "%2d %-10s %3d %10s\n", id, ds, mu, Tcpu;
            printf "\n"
            Tlab = Tlab*rlab
          }
        }
      ' \
  > ${segmfile}

# alf=`echo "l(1031.0/9.0)/l(8.0)"  | bc -lq`
alf=3.0
coefa=0.00370
coefb=0.000481
echo "exponent = ${alf}"

pngtemp="${tmp}.png"

export GDFONTPATH=".:${HOME}/ttf" 
gnuplot <<EOF
# set term pngcairo color enhanced font "arialb,28" size 1600,1200
# set output "${pngtemp}" 

set term pdfcairo enhanced color font "arialb,28" rounded background "#ffffff" size 11.1,8.3
set output "${pdffile}"

unset title

# Number of datasets in the input
nsets = ${nsets}

set cbrange [0:(nsets)]
set palette defined ( 0 "#ff0000", 0.200 "#dd5500", 0.400 "#008800", 0.600 "#0077ff", 0.800 "#2200ff", 1.000 "#cc0088" )
unset colorbox
# set key top right  
unset key

# set ytics 100
# set mytics 5

set logscale x
set xrange [8:120]; set xlabel "max band width"
set xtics (10 0, 20 0, 30 0, 40 0, 50 0, 60 0, 70 0, 80 0, 100 0 )
set grid xtics,mxtics  lt 1 lw 2 lc rgb '#eecc99', lt 1 lw 2 lc rgb '#eecc99'

set logscale y
set yrange [0.8:2100]; set ylabel "CPU time (seconds)"
set ytics ( \
  1 0, 2 0, 3 0, 5 0, \
  10 0, 20 0, 30 0, 50 0, \
  100 0, 200 0, 300 0, 500 0, \
  1000 0, 2000 0, 3000 0, 5000 0, \
)
set grid ytics,mytics  lt 1 lw 2 lc rgb '#eecc99', lt 1 lw 2 lc rgb '#eecc99'

alf = ${alf}
coefa=${coefa}
coefb=${coefb}
modela(x) = coefa*exp(log(x)*alf)
modelb(x) = coefb*exp(log(x)*alf)

# Position of label from columns {i} and {j} of ${segmfile}:
labx(i) = (column(i) == 0 ? 105 : 0/0)
laby(j) = column(j)

# Color of curve from column {k} of ${segmfile}:
clr(k) = column(k) - 1

# Endpoint of dotted line from columns {i} and {j} of ${segmfile}:
segx(i) = (column(i) == 0 ? 102.0 : column(i))
segy(j) = column(j)

set linetype 1 lw 3 dashtype (10,10)

plot \
  (modela(x)) notitle with lines lt 4 lw 8 lc rgb '#eadbbb', \
  (modelb(x)) notitle with lines lt 4 lw 8 lc rgb '#eadbbb', \
  "${segmfile}" using (labx(3)):(laby(4)):1 with labels left, \
  "${segmfile}" using (segx(3)):(segy(4)):(clr(1)) with lines lt 1 palette, \
  "${datafile}" using 3:4 notitle with linespoints pt 7 ps 2.5 lw 5 lc rgb '#fefefe', \
  "${datafile}" using 3:4:(clr(1)) notitle with linespoints pt 7 ps 0.8 lw 4 palette
quit
EOF

if [[ -s ${pngtemp} ]] ; then 
  convert ${pngtemp} -resize '50%' ${pngfile}
  eom ${pngfile}
fi

if [[ -s ${pdffile} ]] ; then 
  evince ${pdffile}
fi

# rm -f ${datafile} ${pngtemp}

