#!/bin/tcsh -f

# Mimic minimal plot_data-ish functionality using gnuplot.
#
# Options:	-c i j k ...	columns to plot (x y y...) [1 2]
#		-f		linear fit to first plot [no]
#		-F x1 x2	x limits to fit [plot limits]
#		-g <geometry>	specify X-window geometry [xdefault]
#		-G		toggle PNG graphics output [off]
#		-h header	specify an overall header [none]
#		-k		suppress "column x" key [no]
#		-l x1 x2 y1 y2	specify plot limits [automatic]
#		-L size		specify plot limits [automatic]
#		-p		toggle points [on]
#		-P		toggle PostScript output [off]
#		-s scriptname	save the script in the named file [don't save]
#               -S              request a square plot [yes]
#		-x		specify x-label [none]
#		-X		toggle explicit X-window output
#		-y		specify y-label [none]
#		-Y a b ...	specify plot labels ["column j", ...]
#
# PNG/Postscript output, if any, is sent to stdout.

#-----------------------------------------------------------------------
# **** Potentially problematic parameters ****
#
# These variables control the margins around the box in the output
# window.  The optimal choices seem to change with new releases of
# gnuplot.  If you don't like the appearance of the plot, try
# experimenting with these.  Making a number smaller makes the margin
# smaller and hence increases the size of the box, but leaves less
# room for labels.

set tmargin = 2
set bmargin = 2
set lmargin = 8
set rmargin = 3

# Old values: 2, 4, 16, 7

#-----------------------------------------------------------------------

#set echo

if ($#argv > 0 && x$1 == x--help) then

    echo ""
    echo 'gpl:       minimal subset of gnuplot commands, with data from stdin'
    echo ""
    echo 'Options:   -c i j k...     columns to plot (x y1 y2...)       [1 2]'
    echo '           -f              linear fit to first plot            [no]'
    echo '           -F x1 x2        x limits to fit            [plot limits]'
    echo '           -g <geometry>   specify X-window geometry     [xdefault]'
    echo '           -G              toggle PNG graphics output         [off]'
    echo '           -h header       specify an overall header         [none]'
    echo '           -k              suppress key giving column info     [no]'
    echo '           -l x1 x2 y1 y2  specify plot limits          [automatic]'
    echo '           -L size         specify plot limits (+/- L)  [automatic]'
    echo '           -p              toggle points/lines             [points]'
    echo '           -P              toggle PostScript output           [off]'
    echo '           -s script       save the script in the named file   [no]'
    echo '           -S              request a square plot              [yes]'
    echo '           -x              specify x-label                   [none]'
    echo '           -X              toggle explicit X-window output    [yes]'
    echo '           -y              specify y-label                   [none]'
    echo '           -Y a b ...      specify plot labels    ["column j", ...]'
    echo ""

    exit
endif

set infile = /tmp/gpl$$.in
set datafile = /tmp/gpl$$.dat
onintr cleanup

#-----------------------------------------------------------------------

# Create the datafile.

cat >! $datafile

#-----------------------------------------------------------------------

# Parse the argument list.

set col = (1 2)
@ l = 1
set nokey = 0
set png = 0
set post = 0
set X = 1
set script = ""
set square = 1
set xlabel = ""
set title = ""
set ylabel = ""
set Ylabel = ()
set xlims = ""
set ylims = ""

set geom = "400x400+0+0"

set fit = 0
set xflims = ""

@ i = 0
while ($i < $#argv)
    @ i++
    set arg = $argv[$i]
    if (x$arg == x-c) then
	set col = ()
	@ j = $i
	while ($j < $#argv)
	    @ j++
	    set tmp = (`echo $argv[$j] | grep -e -`)
	    if ($#tmp > 0) then
		@ i = $j - 1
		break
	    else
		set col = ($col $argv[$j])
		@ i = $j
	    endif
	end
    else if (x$arg == x-g) then
	@ i++
	set geom = "$argv[$i]"
    else if (x$arg == x-G) then
	@ png = 1 - $png
    else if (x$arg == x-h) then
	@ i++
	set title = "$title$argv[$i]"
    else if (x$arg == x-k) then
	set nokey = 1
    else if (x$arg == x-l) then
	@ i++ ; set x1 = $argv[$i]
	@ i++ ; set x2 = $argv[$i]
	@ i++ ; set y1 = $argv[$i]
	@ i++ ; set y2 = $argv[$i]
	set xlims = "[$x1 : $x2]"
	set ylims = "[$y1 : $y2]"
    else if (x$arg == x-L) then
	@ i++ ; set size = $argv[$i]
	set xlims = "[-$size : $size]"
	set ylims = "[-$size : $size]"
    else if (x$arg == x-p) then
	@ l = 1 - $l
    else if (x$arg == x-P) then
	@ post = 1 - $post
	@ square = 1 - $square
    else if (x$arg == x-s) then
	@ i++
	set script = "$argv[$i]"
	set infile = "$script"
    else if (x$arg == x-S) then
	@ square = 1 - $square
    else if (x$arg == x-x) then
	@ i++
	set xlabel = "$argv[$i]"
    else if (x$arg == x-X) then
	@ X = 1 - $X
    else if (x$arg == x-y) then
	@ i++
	set ylabel = "$argv[$i]"
    else if (x$arg == x-Y) then
	@ j = $i
	while ($j < $#argv)
	    @ j++
	    set tmp = (`echo $argv[$j] | grep -e '^-'`)
	    if ($#tmp > 0) then
		@ i = $j - 1
		break
	    else
		set Ylabel = ($Ylabel "$argv[$j]")
		@ i = $j
	    endif
	end
    else if (x$arg == x-f) then
	@ fit = 1 - $fit
    else if (x$arg == x-F) then
	@ i++ ; set x1 = $argv[$i]
	@ i++ ; set x2 = $argv[$i]
	set xflims = "[$x1 : $x2]"
    endif
end

if ($#col < 2) goto cleanup

if  ("x$xlabel" == x) then
    set xlabel = column_$col[1]
endif

# Want Ylabel to contain 1 less entries than col.  Pad if necessary,
# with generic labels.

if ($#Ylabel < $#col - 1) then
    @ j = $#Ylabel + 1
    while ($j < $#col)
	@ j++
	set Ylabel = ($Ylabel column_$col[$j])
    end
endif

#echo xlabel = $xlabel
#echo ylabel = $ylabel
#echo title = $title
#echo col = $col, \# = $#col
#echo Ylabel = $Ylabel, \# = $#Ylabel

#-----------------------------------------------------------------------

# Create and run the input file.

if ($#col > 1) then

    echo set tmargin $tmargin				>! $infile
    echo set bmargin $bmargin				>> $infile
    echo set lmargin $lmargin				>> $infile
    echo set rmargin $rmargin				>> $infile

    if ($square == 1) then
	echo set size square				>> $infile
    endif

    if ($nokey == 1) then
	echo set nokey					>> $infile
    endif

    if ($post == 1) then
	echo set term post color 18			>> $infile
    else if ($png == 1) then
	echo set term png				>> $infile
    else if ($X == 1) then
	echo set term x11				>> $infile
    endif
#   echo show term					>> $infile

#   Label convention: replace "_" by space.

    set xl = `echo "$xlabel" | tr '_' ' '`
    echo set xlabel \"$xl\"				>> $infile
    set yl = `echo "$ylabel" | tr '_' ' '`
    echo set ylabel \"$yl\"				>> $infile

    set x2l = `echo "$title" | tr '_' ' '`
    echo set title \"$x2l\"				>> $infile

    if ($l == 1) then
	set lines = "lines"
    else
	set lines = "points"
    endif

    set x = $col[1]

    if ($fit == 1) then
	echo "f(x) = a*x+b"				>> $infile
	echo "fit $xflims f(x) '$datafile' "\\		>> $infile
 	echo using {$x}:$col[2] via a,b			>> $infile
    endif

    echo plot "$xlims" "$ylims" \\			>> $infile

    set x = $col[1]
    @ i = 1
    @ j = 0
    while ($i < $#col)
	@ i++
	@ j++
	echo -n "    " \'$datafile\' using {$x}:$col[$i]  >> $infile

#	Label convention: replace "_" by space.

	set yl = "`echo "$Ylabel[$j]" | tr '_' ' '`"
	echo -n \ title \"$yl\"				>> $infile

	echo -n \ with $lines				>> $infile
	if ($i < $#col) then
	    echo ", \"					>> $infile
	else
	    if ($fit == 0) then
		echo ""					>> $infile
	    else
		echo ", f(x) with lines"		>> $infile
	    endif
	endif
    end

#   cat $infile

    setenv FIT_LOG /tmp/gpl$$.fit
    gnuplot -geometry $geom -persist $infile	# X11 output stays visible

endif

#-----------------------------------------------------------------------

cleanup:
rm -f /tmp/gpl$$.*
