#! /bin/bash
# Last edited on 2020-12-23 19:32:17 by jstolfi

# Plots formulas to compbine inhibitory and excitatory synapses.

func="$1"; shift;  # Formula name.

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

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

gfile="${tmp}_syn.pov"

gawk \
  '


tfile="${tmp}_syn.png"
pfile="out/syn_${func}.png"

export GDFONTPATH="."

gnuplot <<EOF
  tfile = "${tfile}"
  nnum = "${nnum}"
  vars = "total input {J}"
  zmin = -200
  zmax = +200

  set terminal png truecolor size 1200,1200 font "arial,18"
  set output "${tfile}"
  set xrange [-10:+110]
  set yrange [-10:+110]
  set xlabel "WP"
  set ylabel "WN"
  set view 60,120
  set zrange [(zmin-5):(zmax+5)]
  set title "Formula ${func}"
  
  VP = -20
  VB = -60
  VN = -80
  
  set style function pm3d
  set pm3d at s hidden3d
  unset hidden3d
  set cbrange [(zmin/2):(zmax/2)]
  set cbtics 20.0
  set palette defined ((zmin) 0 0 1, (VN) 0 0 1, (VB) 0 1 0, (VP) 1 0 0, (zmax) 1 0 0)
  set colorbox
  # set isosamples 10,10
  set samples 50,50

  inrange(z) = ((z < -0.001) || (z > 100.001) ? 0/0 : 1)
  ir(u,y) = inrange(u)*inrange(y)
  func1(u,v) = VB + u - v
  func2(u,v) = VB + (u**2 + v**2)/200
  func3(u,v) = VB + v
  
  splot \
    (ir(x,y)*${func}(x,y)) title "V" with pm3d lt 1 lw 1 lc rgb '#cc0000'
    
# , \
#     (ir(x,y)*VN) notitle with lines lt 1 lc rgb '#cc7744', \
#     (ir(x,y)*VP) notitle with lines lt 1 lc rgb '#cc7744'    
EOF

if [[ -s ${tfile} ]]; then
  convert ${tfile} -resize '50%' ${pfile}
  display ${pfile}
  rm ${tfile}
fi
