#! /bin/csh -f 
# Last edited on 2008-02-04 20:51:22 by stolfi

set usage = "$0 DIR HAXIS VAXIS > PLOT.gif"

# Reads two files DIR/HAXIS.projs and DIR/VAXIS.projs, each with entries
# in the format
#
#    1     2   
#    COORD FNUM 
#
# where COORD is a real number and FNUM is a page's f-number.
# Ouputs a plot (GIF image) of the (H.COORD,V.COORD) pairs,
# with consecutive pages connected by lines.

if ( $#argv != 3 ) then
  echo "usage: ${usage}"; exit 1
endif

set dir = "$1"; shift;
set haxis = "$1"; shift;
set vaxis = "$1"; shift;

set tmp = "/tmp/$$"
set dfile = "${tmp}.datsp"
set pfile = "${tmp}.plt"
set mfile = "${tmp}.ppm"

join \
    -a 1 -a 2 -e '+0.0000' \
    -j1 2 -j2 2 \
    -o 1.1,2.1,0 \
    ${dir}/${haxis}.projs \
    ${dir}/${vaxis}.projs \
  | map-field \
      -v inField=3 \
      -v outField=4 \
      -v table=fnum-to-plot.tbl \
      -v default='?' \
  | map-field \
      -v inField=3 \
      -v outField=5 \
      -v table=fnum-to-pnum.tbl \
      -v default='000' \
  > ${dfile}

# Sort by section and p-number.
# Then insert single blank line at gaps, 
# write separate file by subsection.

cat ${dfile} \
  | sort +3 -4 +4 -5n \
  | gawk -v tmp="${tmp}" \
       ' BEGIN{s="";sf="";} \
         //{ if($4!=s) \
               { if(s!=""){ close(sf);} \
                 s=$4; sf=(tmp "." s); p=-1; \
               } \
             if(($5!=p)&&(p!=-1)) { printf "\n" > sf; } \
             print > sf; p = $5+1; \
           } \
       ' \
  > ${pfile}

/bin/rm -f rm ${mfile}
gnuplot <<EOF
set terminal pbm color small
set output "${mfile}"
set lmargin 6
# set terminal x11
set size ratio -1 0.90,0.90
set xlabel "${haxis}"
set ylabel "${vaxis}"
set xrange [-0.12:+0.12]
set yrange [-0.12:+0.12] 
set key outside Left reverse samplen 2
plot \
  "${tmp}.pharma"   using 1:2 title  "pharma"   with linespoints lt  5 pt 3, \
  "${tmp}.her-a-1"  using 1:2 title  "her A.1"  with linespoints lt  1 pt 3, \
  "${tmp}.her-a-2"  using 1:2 title  "her A.2"  with linespoints lt  1 pt 1, \
  "${tmp}.zodiac"   using 1:2 title  "zodiac"   with linespoints lt  2 pt 3, \
  "${tmp}.cosmo-1"  using 1:2 title  "cosmo 1"  with linespoints lt  8 pt 3, \
  "${tmp}.cosmo-2"  using 1:2 title  "cosmo 2"  with linespoints lt  8 pt 1, \
  "${tmp}.stars"    using 1:2 title  "stars"    with linespoints lt  4 pt 3, \
  "${tmp}.her-b-1"  using 1:2 title  "her B.1"  with linespoints lt  9 pt 3, \
  "${tmp}.her-b-2"  using 1:2 title  "her B.2"  with linespoints lt  9 pt 1, \
  "${tmp}.bio"      using 1:2 title  "bio"      with linespoints lt  3 pt 3, \
  "${tmp}.f1r"      using 1:2 title  "f1r"      with      points lt  6 pt 3, \
  "${tmp}.f57v"     using 1:2 title  "f57v"     with      points lt  6 pt 1, \
  "${tmp}.f49v"     using 1:2 title  "f49v"     with      points lt  1 pt 1 ps 2, \
  "${tmp}.f58rv"    using 1:2 title  "f58rv"    with linespoints lt  4 pt 2 ps 2, \
  "${tmp}.f65rv"    using 1:2 title  "f65rv"    with linespoints lt  4 pt 3 ps 2, \
  "${tmp}.f66r"     using 1:2 title  "f66r"     with      points lt  8 pt 4 ps 2, \
  "${tmp}.f85r1"    using 1:2 title  "f85r1"    with      points lt  9 pt 5 ps 2, \
  "${tmp}.f86v6"    using 1:2 title  "f86v6"    with      points lt  9 pt 6 ps 2, \
  "${tmp}.f86v5"    using 1:2 title  "f86v5"    with      points lt  9 pt 7 ps 2, \
  "${tmp}.f116v"    using 1:2 title  "f116v"    with      points lt  4 pt 8 ps 2
EOF

if ( ( ! ( -r ${mfile} ) ) || ( -z ${mfile} ) ) then 
  /bin/rm -f rm ${mfile}; exit 1
endif

set path = ( ${STOLFIHOME}/pkg/netpbm-1mar1994-1/PUB/${PLATFORM}/bin $path )

cat ${mfile}  \
  | pnminvert \
  | pnmscale 1.33 \
  | pnmgamma 3.0 \
  | pnmdepth 255 \
  | ppmquant 256 \
  | ppmtogif 

/bin/rm -f ${dfile} ${pfile} ${mfile} ${tmp}.???.?

exit 0