#! /bin/bash
# Last edited on 2021-11-25 21:48:38 by stolfi

# Plots a three-component phase diagram, with up to two highlighted regions.
# See {ternary_phase_diagram.py} for procedures that generate the 
# necessary data files.  See {phase_diagram.py} in
# {projects/wikipedia/chemistry/magnesium_oxychloride} for an
# example of how to use it.

# Arguments: 
#   {ptfile}   Name of ".txt" file with point coordinates.
#   {lnfile}   Name of ".txt" file with line pairs.
#   {rgfile1}  Name of ".txt" file with filled region 1.
#   {rgfile2}  Name of ".txt" file with filled region 2.
#   {color}    Dot color, as a hex RGB triple WITHOUT a leading '#'.
#   {pngname}  Name of the output ".png" file, minus extension.

# Creates files {pngname}-f.png and {pngname}-s.png

# Each line of {ptfile} (minus blank lines and #-comments) must contain:
#   {GMASS}     Molar mass of compound (grams).
#   {XC} {YC}   Coordinates of point [0_1].
#   {PTSIZE}    Relative point size (1.0 very small).
#   {XL} {YL}   Coordiantes for label [0_1].
#   {LABEL} label of compound (with extended text conventions).

# Each line of {lnfile} (minus blank lines and #-comments) must contain:

# Each line of {rgfile} (minus blank lines and #-comments) must contain:

export LANG=en_US.ISO-8859-1

ptfile="$1"; shift
lnfile="$1"; shift
rgfile1="$1"; shift
rgfile2="$1"; shift

color="$1"; shift
pngname="$1"; shift

echo "${0##*/} ${ptfile} ps ${ptsize} rgb '${color}' > ${pngname}.png" 1>&2

tmp="/tmp/$$"

# # Compute the total of all {ptfile} records, save in temp file:
# totfile="${tmp}.tot"
# 
# cat ${ptfile} \
#   | gawk \
#       ' BEGIN{ split("", fld); nf = -1; }
#         /^[ ]*([\#]|$)/ { next; }
#         // { 
#           stcd=$1; ofnum=$6;
#           if (nf < 0) { nf = NF; }
#           for (k=7; k<=nf; k++) { fld[k] += $(k); }
#         }
#         END{
#           printf "%s 000 0000 00000 00000 %d", stcd, ofnum;
#           for (k=7; k<=nf; k++) { printf " %4d", fld[k]; }
#           printf "\n";
#         }
#       ' \
#   > ${totfile}

export GDFONTPATH="." 
gnuplot <<EOF

# PNG output:
# set output "${tmp}-tri.png" 
# set term png enhanced font arialbd 60 size 4800,4800
# set size ratio -1
# set pointsize 1.0

set output "${tmp}-tri.pdf" 
set term pdf enhanced font "Helvetica,11" rounded linewidth 0.333 size 14 cm, 12.5 cm background rgb '#ffffff'
set size ratio -1
set pointsize 0.1

set notitle
# Height of unit-side triangle:
htri = sqrt(3)/2;
# Plot ranges:
xtad = 0.05;
ytad = xtad;
set xrange [(-xtad):(1.0+xtad)]
set yrange [(-2*xtad):(htri+xtad)]
set noborder
set noxlabel
set noylabel

set lmargin at screen 0.03
set rmargin at screen 0.97
set tmargin at screen 0.97
set bmargin at screen 0.03

# Point data: 
xcoord(dum) = column(1) # X coordinate of point [0_1].                         
ycoord(dum) = column(2) # Y coordinate of point [0_1].                         
ptsize(dum) = column(3) # Relative point size.                                       
xlab(dum) = (ptsize(dum) < 0.001 ? 0/0 : column(4))   # X coord of label (mm)                        
ylab(dum) = (ptsize(dum) < 0.001 ? 0/0 : column(5))   # Y coord of label (mm)                      
label(dum) = strcol(6)  # Label of compound (with extended text conventions).

# Grid lines and tic label coordinates:
set samples 2000
gtic = 0.008 # Tic length.
nit = 0.001

# Descending lines:
grid1(v,x) = ((x < 0.5*(1-v-gtic)-nit) || (x > 1-v+nit) ? 0/0 : 2*htri*(1-x-v));
xticlab1(v) = 0.5*(1-v-2*gtic)
yticlab1(v) = 2*htri*(0.5-0.5*v+gtic)

grid2(v,x) = ((x < 0.5*v-nit) || (x > 1-0.5*v+gtic+nit) ? 0/0 : htri*v); # Horizontal.
xticlab2(v) = 1-0.5*v+2*gtic
yticlab2(v) = htri*v

grid3(v,x) = ((1-x < 0.5*(1-v)-nit) || (1-x > 1-v+0.5*gtic+nit) ? 0/0 : 2*htri*(x-v)); # Ascending.
xticlab3(v) = v-gtic
yticlab3(v) = -htri*2*gtic

set nokey
unset xtics
unset ytics
set style textbox opaque noborder

set label   "0" at (xticlab1(0.00)),(yticlab1(0.00)) font "Helvetica,10" right  rotate by -60 offset character 0,0 textcolor rgb '#aa8866'
set label  "10" at (xticlab1(0.10)),(yticlab1(0.10)) font "Helvetica,10" right  rotate by -60 offset character 0,0 textcolor rgb '#aa8866'
set label  "20" at (xticlab1(0.20)),(yticlab1(0.20)) font "Helvetica,10" right  rotate by -60 offset character 0,0 textcolor rgb '#aa8866'
set label  "30" at (xticlab1(0.30)),(yticlab1(0.30)) font "Helvetica,10" right  rotate by -60 offset character 0,0 textcolor rgb '#aa8866'
set label  "40" at (xticlab1(0.40)),(yticlab1(0.40)) font "Helvetica,10" right  rotate by -60 offset character 0,0 textcolor rgb '#aa8866'
set label  "50" at (xticlab1(0.50)),(yticlab1(0.50)) font "Helvetica,10" right  rotate by -60 offset character 0,0 textcolor rgb '#aa8866'
set label  "60" at (xticlab1(0.60)),(yticlab1(0.60)) font "Helvetica,10" right  rotate by -60 offset character 0,0 textcolor rgb '#aa8866'
set label  "70" at (xticlab1(0.70)),(yticlab1(0.70)) font "Helvetica,10" right  rotate by -60 offset character 0,0 textcolor rgb '#aa8866'
set label  "80" at (xticlab1(0.80)),(yticlab1(0.80)) font "Helvetica,10" right  rotate by -60 offset character 0,0 textcolor rgb '#aa8866'
set label  "90" at (xticlab1(0.90)),(yticlab1(0.90)) font "Helvetica,10" right  rotate by -60 offset character 0,0 textcolor rgb '#aa8866'
set label "100" at (xticlab1(1.00)),(yticlab1(1.00)) font "Helvetica,10" right  rotate by -60 offset character 0,0 textcolor rgb '#aa8866'

set label   "0" at (xticlab2(0.00)),(yticlab2(0.00)) font "Helvetica,10" left   rotate by   0 offset character 0,0 textcolor rgb '#aa8866'
set label  "10" at (xticlab2(0.10)),(yticlab2(0.10)) font "Helvetica,10" left   rotate by   0 offset character 0,0 textcolor rgb '#aa8866'
set label  "20" at (xticlab2(0.20)),(yticlab2(0.20)) font "Helvetica,10" left   rotate by   0 offset character 0,0 textcolor rgb '#aa8866'
set label  "30" at (xticlab2(0.30)),(yticlab2(0.30)) font "Helvetica,10" left   rotate by   0 offset character 0,0 textcolor rgb '#aa8866'
set label  "40" at (xticlab2(0.40)),(yticlab2(0.40)) font "Helvetica,10" left   rotate by   0 offset character 0,0 textcolor rgb '#aa8866'
set label  "50" at (xticlab2(0.50)),(yticlab2(0.50)) font "Helvetica,10" left   rotate by   0 offset character 0,0 textcolor rgb '#aa8866'
set label  "60" at (xticlab2(0.60)),(yticlab2(0.60)) font "Helvetica,10" left   rotate by   0 offset character 0,0 textcolor rgb '#aa8866'
set label  "70" at (xticlab2(0.70)),(yticlab2(0.70)) font "Helvetica,10" left   rotate by   0 offset character 0,0 textcolor rgb '#aa8866'
set label  "80" at (xticlab2(0.80)),(yticlab2(0.80)) font "Helvetica,10" left   rotate by   0 offset character 0,0 textcolor rgb '#aa8866'
set label  "90" at (xticlab2(0.90)),(yticlab2(0.90)) font "Helvetica,10" left   rotate by   0 offset character 0,0 textcolor rgb '#aa8866'
set label "100" at (xticlab2(1.00)),(yticlab2(1.00)) font "Helvetica,10" left   rotate by   0 offset character 0,0 textcolor rgb '#aa8866'

set label   "0" at (xticlab3(0.00)),(yticlab3(0.00)) font "Helvetica,10" right  rotate by +60 offset character 0,0 textcolor rgb '#aa8866'
set label  "10" at (xticlab3(0.10)),(yticlab3(0.10)) font "Helvetica,10" right  rotate by +60 offset character 0,0 textcolor rgb '#aa8866'
set label  "20" at (xticlab3(0.20)),(yticlab3(0.20)) font "Helvetica,10" right  rotate by +60 offset character 0,0 textcolor rgb '#aa8866'
set label  "30" at (xticlab3(0.30)),(yticlab3(0.30)) font "Helvetica,10" right  rotate by +60 offset character 0,0 textcolor rgb '#aa8866'
set label  "40" at (xticlab3(0.40)),(yticlab3(0.40)) font "Helvetica,10" right  rotate by +60 offset character 0,0 textcolor rgb '#aa8866'
set label  "50" at (xticlab3(0.50)),(yticlab3(0.50)) font "Helvetica,10" right  rotate by +60 offset character 0,0 textcolor rgb '#aa8866'
set label  "60" at (xticlab3(0.60)),(yticlab3(0.60)) font "Helvetica,10" right  rotate by +60 offset character 0,0 textcolor rgb '#aa8866'
set label  "70" at (xticlab3(0.70)),(yticlab3(0.70)) font "Helvetica,10" right  rotate by +60 offset character 0,0 textcolor rgb '#aa8866'
set label  "80" at (xticlab3(0.80)),(yticlab3(0.80)) font "Helvetica,10" right  rotate by +60 offset character 0,0 textcolor rgb '#aa8866'
set label  "90" at (xticlab3(0.90)),(yticlab3(0.90)) font "Helvetica,10" right  rotate by +60 offset character 0,0 textcolor rgb '#aa8866'
set label "100" at (xticlab3(1.00)),(yticlab3(1.00)) font "Helvetica,10" right  rotate by +60 offset character 0,0 textcolor rgb '#aa8866'

plot \
  "${rgfile1}" using (xcoord(0)):(ycoord(0)) \
    with filledcurves closed fs solid lw 0.0 lc rgb '#5588ff', \
  "${rgfile2}" using (xcoord(0)):(ycoord(0)) \
    with filledcurves closed fs solid lw 0.0 lc rgb '#aaddff', \
  (grid1(0.00,x)) notitle with lines lw 5.0 lc rgb '#aa8866', \
  (grid1(0.05,x)) notitle with lines lw 1.5 lc rgb '#ffcc88', \
  (grid1(0.10,x)) notitle with lines lw 3.0 lc rgb '#eebb44', \
  (grid1(0.15,x)) notitle with lines lw 1.5 lc rgb '#ffcc88', \
  (grid1(0.20,x)) notitle with lines lw 3.0 lc rgb '#eebb44', \
  (grid1(0.25,x)) notitle with lines lw 1.5 lc rgb '#ffcc88', \
  (grid1(0.30,x)) notitle with lines lw 3.0 lc rgb '#eebb44', \
  (grid1(0.35,x)) notitle with lines lw 1.5 lc rgb '#ffcc88', \
  (grid1(0.40,x)) notitle with lines lw 3.0 lc rgb '#eebb44', \
  (grid1(0.45,x)) notitle with lines lw 1.5 lc rgb '#ffcc88', \
  (grid1(0.50,x)) notitle with lines lw 5.0 lc rgb '#eebb44', \
  (grid1(0.55,x)) notitle with lines lw 1.5 lc rgb '#ffcc88', \
  (grid1(0.60,x)) notitle with lines lw 3.0 lc rgb '#eebb44', \
  (grid1(0.65,x)) notitle with lines lw 1.5 lc rgb '#ffcc88', \
  (grid1(0.70,x)) notitle with lines lw 3.0 lc rgb '#eebb44', \
  (grid1(0.75,x)) notitle with lines lw 1.5 lc rgb '#ffcc88', \
  (grid1(0.80,x)) notitle with lines lw 3.0 lc rgb '#eebb44', \
  (grid1(0.85,x)) notitle with lines lw 1.5 lc rgb '#ffcc88', \
  (grid1(0.90,x)) notitle with lines lw 3.0 lc rgb '#eebb44', \
  (grid1(0.95,x)) notitle with lines lw 1.5 lc rgb '#ffcc88', \
  (grid1(1.00,x)) notitle with lines lw 5.0 lc rgb '#aa8866', \
  \
  (grid2(0.00,x)) notitle with lines lw 5.0 lc rgb '#aa8866', \
  (grid2(0.05,x)) notitle with lines lw 1.5 lc rgb '#ffcc88', \
  (grid2(0.10,x)) notitle with lines lw 3.0 lc rgb '#eebb44', \
  (grid2(0.15,x)) notitle with lines lw 1.5 lc rgb '#ffcc88', \
  (grid2(0.20,x)) notitle with lines lw 3.0 lc rgb '#eebb44', \
  (grid2(0.25,x)) notitle with lines lw 1.5 lc rgb '#ffcc88', \
  (grid2(0.30,x)) notitle with lines lw 3.0 lc rgb '#eebb44', \
  (grid2(0.35,x)) notitle with lines lw 1.5 lc rgb '#ffcc88', \
  (grid2(0.40,x)) notitle with lines lw 3.0 lc rgb '#eebb44', \
  (grid2(0.45,x)) notitle with lines lw 1.5 lc rgb '#ffcc88', \
  (grid2(0.50,x)) notitle with lines lw 5.0 lc rgb '#eebb44', \
  (grid2(0.55,x)) notitle with lines lw 1.5 lc rgb '#ffcc88', \
  (grid2(0.60,x)) notitle with lines lw 3.0 lc rgb '#eebb44', \
  (grid2(0.65,x)) notitle with lines lw 1.5 lc rgb '#ffcc88', \
  (grid2(0.70,x)) notitle with lines lw 3.0 lc rgb '#eebb44', \
  (grid2(0.75,x)) notitle with lines lw 1.5 lc rgb '#ffcc88', \
  (grid2(0.80,x)) notitle with lines lw 3.0 lc rgb '#eebb44', \
  (grid2(0.85,x)) notitle with lines lw 1.5 lc rgb '#ffcc88', \
  (grid2(0.90,x)) notitle with lines lw 3.0 lc rgb '#eebb44', \
  (grid2(0.95,x)) notitle with lines lw 1.5 lc rgb '#ffcc88', \
  (grid2(1.00,x)) notitle with lines lw 5.0 lc rgb '#aa8866', \
  \
  (grid3(0.00,x)) notitle with lines lw 5.0 lc rgb '#aa8866', \
  (grid3(0.05,x)) notitle with lines lw 1.5 lc rgb '#ffcc88', \
  (grid3(0.10,x)) notitle with lines lw 3.0 lc rgb '#eebb44', \
  (grid3(0.15,x)) notitle with lines lw 1.5 lc rgb '#ffcc88', \
  (grid3(0.20,x)) notitle with lines lw 3.0 lc rgb '#eebb44', \
  (grid3(0.25,x)) notitle with lines lw 1.5 lc rgb '#ffcc88', \
  (grid3(0.30,x)) notitle with lines lw 3.0 lc rgb '#eebb44', \
  (grid3(0.35,x)) notitle with lines lw 1.5 lc rgb '#ffcc88', \
  (grid3(0.40,x)) notitle with lines lw 3.0 lc rgb '#eebb44', \
  (grid3(0.45,x)) notitle with lines lw 1.5 lc rgb '#ffcc88', \
  (grid3(0.50,x)) notitle with lines lw 5.0 lc rgb '#eebb44', \
  (grid3(0.55,x)) notitle with lines lw 1.5 lc rgb '#ffcc88', \
  (grid3(0.60,x)) notitle with lines lw 3.0 lc rgb '#eebb44', \
  (grid3(0.65,x)) notitle with lines lw 1.5 lc rgb '#ffcc88', \
  (grid3(0.70,x)) notitle with lines lw 3.0 lc rgb '#eebb44', \
  (grid3(0.75,x)) notitle with lines lw 1.5 lc rgb '#ffcc88', \
  (grid3(0.80,x)) notitle with lines lw 3.0 lc rgb '#eebb44', \
  (grid3(0.85,x)) notitle with lines lw 1.5 lc rgb '#ffcc88', \
  (grid3(0.90,x)) notitle with lines lw 3.0 lc rgb '#eebb44', \
  (grid3(0.95,x)) notitle with lines lw 1.5 lc rgb '#ffcc88', \
  (grid3(1.00,x)) notitle with lines lw 5.0 lc rgb '#aa8866', \
  "${ptfile}" using (xcoord(0)):(ycoord(0)):(ptsize(0)) \
    with points pt 7 ps variable lw 3.0 lc rgb '#${color}', \
  "${lnfile}" using (xcoord(0)):(ycoord(0)) \
    with lines lw 4.0 lc rgb '#${color}', \
  "${ptfile}" using (xlab(0)):(ylab(0)):(label(0)) \
    with labels tc rgb '#000000'
 
EOF

if [[ -s ${tmp}-tri.pdf ]] ; then 
  cp ${tmp}-tri.pdf ${pngname}-f.pdf
  atril ${pngname}-f.pdf
  convert -density 1200 ${tmp}-tri.pdf +repage -resize 'x800' -flatten ${pngname}-f.png
  convert ${pngname}-f.png -resize '100x' ${pngname}-s.png
  display -title '%d' ${pngname}-f.png
fi

rm -f ${tmp}.png ${tmp}.tot
