#!/bin/csh
#
## $Id$
## $Log$
## Revision 1.2  2005/10/05 06:18:35  nadya
## use full path for "rm". Asssume everybody has /bin/rm.
##
## Revision 1.1.1.1  2005/07/29 00:00:42  nadya
## Importing from meme-3.0.14, and adding configure/make
##
#

set pgm = $0; set pgm = $pgm:t
if ($#argv < 1) then
  error:
  more << "USAGE"
  USAGE:
	$pgm [<label> <data>] [-ps|-png <name>] 
	  [-xlabel <xlab>] [-ylabel <ylab>] 
	  [-xlaboff <xlaboff>] [-ylaboff <ylaboff>]
          [-xrange [<x1>] : [<x2>] [-yrange [<y1>] : [<y2>]
	  [-key <x> <y>] [-misc <whatever>] [-file <file>]
	  [-size <size>] [-point <psize>] [-rm_] [-big] [-f] [-nocat]

	<label>			label for curve  
	<data>			file of points to plot;
				if the file does not exist, then <data>
				is assumed to be a function defined in
				a -misc or -file command
	[-ps|-png <name>]	plot to .ps or .png file <name>.ps 
				or <name>.png, tex file <name>.tex
				plot to screen if <name> is "_screen_"
				default is screen
	[-xlabel <xlab>]	label for x axis
	[-ylabel <ylab>]	label for y axis
	[-xlaboff [<xoff>][,<yoff>]]	offset for label for x axis
	[-ylaboff [<xoff>][,<yoff>]	offset for label for y axis
	[-xrange [<x1>] : [<x2>]range for x axis
	[-yrange [<y1>] : [<y2>]range for y axis
	[-key <x> <y>]  	position key at <x,y>
	[-title <title>]	title for plot; underscores are blanked
	[-misc <whatever>]+	any gnuplot command(s) separated by ';'
	[-file <file>]		file containing gnuplot commands;
	[-size <xsize> <ysize>]	scale-factor for size of plot
	[-point <psize>]	point size for text on plot; default = 11
	[-rm_]			replace underscores with blanks in
				<label>, <xlab> and <ylab>
	[-big]			make 65in, two column figure
	[-[no]color]		make a [non-]color figure
	[-f]			force: overwrite files if they exist
	[-nocat]		don't print the plot script; default: cat the script to STDOUT

	Plots one or more curves from files containing <x, y> pairs.
	Default is to screen.
	The -misc commands followed by the -file are done just before
	any plot commands generated by the <label> <data> pairs.

	Underscores in <title> are replaced by blanks.

 	Creates files:
		<name>.[ps|png]
		<name>.tex
		tmp.tex
"USAGE"
  exit 1
endif

set command = ($0 "$*")

onintr cleanup

set ncurves = 0
set label = (1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1)
set data = ""
set misc = ""; set point = 11

set miscfile = $pgm.miscfile.$$.tmp

set xlaboff = ""
set ylaboff = ""

set color = ""

set force = 0

# tex figure parameters
set col = "" 
set width = "\widtha"

# get input switches
while  ("$1" != "")
  switch ("$1")
  case -ps:
  case -png:
    if ($2 == "_screen_") break
    set name = $2:t; 
    if ("$1" == "-ps") then
      set otype = "ps"
      set out = $2.ps; set latex = $2.tex; set latex2 = $latex
    else 
      set otype = "png"
      set out = $2.png;
    endif
    shift
    breaksw
  case -key:
    set key = "set key ${2},${3}"; shift; shift; breaksw
  case -xlabel:
    set xlab = "$2"; shift; breaksw
  case -ylabel:
    set ylab = "$2"; shift; breaksw
  case -xlaboff:
    set xlaboff = $2; shift; breaksw
  case -ylaboff:
    set ylaboff = $2; shift; breaksw
  case -xrange:
    shift; 
    if ($1 != ":") then
      set xrange = "[$1 : $3]"; shift; shift;
    else 
      set xrange = "[ : $2]"; shift
    endif
    breaksw
  case -yrange:
    shift; 
    if ($1 != ":") then
      set yrange = "[$1 : $3]"; shift; shift;
    else 
      set yrange = "[ : $2]"; shift
    endif
    breaksw
  case -title:
    set title = `echo $2 | sed s/_/" "/g`; shift; breaksw
  case -misc:
    if !(-e $miscfile) touch $miscfile
    shift; echo "$1" >> $miscfile; breaksw
    #set misc = ("$misc" "$2" ;); shift; breaksw
  case -file:
    set gnufile = "$2"; shift; breaksw
  case -size:
    set xsize = $2; shift; set ysize = $2; shift; breaksw
  case -point:
    shift; set point = $1; breaksw
  case -rm_:
    set rm_ = 1; breaksw
  case -big:
    set col = '*'; set width = "6.5in"; breaksw
  case -color:
    set color = "color"; breaksw
  case -nocolor:
    set color = ""; breaksw
  case -f
    set force = 1; breaksw
  case -nocat
    set nocat = 1; breaksw
  default:
    @ ncurves++
    if ($ncurves > $#label) then
      echo Too many curves specified | perl -n -e 'print stderr $_'
      exit 1
    endif
    set label[$ncurves] = "$1"
    set data = ($data $2)
    shift; breaksw
  endsw
  shift
#  echo $1
end

# check that at least one curve to be printed
#if ($ncurves < 1) goto error

# set up the file which will receive the plot 
set gnuplot = $pgm.plotscript.$$.tmp
if ($?out) then
  if ((-e $out) && $force == 0) then
    echo "File $out already exists.  Overwrite? (n)" | perl -n -e 'print stderr $_'
    set ans = $<
    if (!($ans == "y" || $ans == "Y")) then
      exit
    endif
    /bin/rm $out
  endif
  echo Output will be to $otype file $out | perl -n -e 'print stderr $_'
  if ("$otype" == "ps") then
    echo "set terminal postscript eps $color $point" > $gnuplot
  else
    if ($point < 20) then
      set psize = "small"
    else if ($point < 30) then
      set psize = "medium"
    else 
      set psize = "large"
    endif
    echo "set terminal png $psize" > $gnuplot
  endif
  echo "set output '/dev/null'" >> $gnuplot
else
  echo Output will be to screen | perl -n -e 'print stderr $_'
  echo -n "" > $gnuplot
endif

# set up the file which will receive the latex figures (.tex file)
if ($?latex) then
  if ((-e $latex) && $force == 0) then
    echo "File $latex already exists.  Overwrite? (n)" | perl -n -e 'print stderr $_'
    set ans = $<
    if ($ans == "y" || $ans == "Y") then
      /bin/rm $latex
    else
      set latex = /dev/null
    endif
  endif
  echo -n "" >! $latex

# print the header for a latex document
  echo "\documentclass[12pt]{article}" >! tmp.tex
  echo "\usepackage{times,a4,graphicx}" >> tmp.tex
endif

if ($?title) then
  if ($?rm_) set title = `echo $title | sed s/_/" "/g`
  echo "set title '$title'" >> $gnuplot
endif
echo "set style data linespoints" >> $gnuplot
if ($?xlab) then
  if ($?rm_) set xlab = `echo $xlab | sed s/_/" "/g`
  echo "set xlabel '$xlab' $xlaboff" >> $gnuplot
endif
if ($?ylab) then
  if ($?rm_) set ylab = `echo $ylab | sed s/_/" "/g`
  echo "set ylabel '$ylab' $ylaboff" >> $gnuplot
endif
if ($?xrange) then 
  echo set xrange "$xrange" >> $gnuplot
endif
if ($?yrange) then
  echo set yrange "$yrange" >> $gnuplot
endif
if ($?key) then
  echo "$key" >> $gnuplot
endif
if ($?xsize) then
  echo set size $xsize,$ysize >> $gnuplot
endif
#foreach com ($misc)
#  if ($com != ";") then
#    echo -n "$com " >> $gnuplot
#  else
#    echo "" >> $gnuplot
#  endif
#end
if (-e $miscfile) cat $miscfile >> $gnuplot
if ($?gnufile) then
  cat $gnufile >> $gnuplot
endif

set plotcmd = plot
set i=1
while ($i <= $ncurves)
# plot the points
  if ($?rm_) then 
    set curve_title = `echo $label[$i] | sed s/_/" "/g`
  else
    set curve_title = $label[$i]
  endif
  echo "curve ${i}: $curve_title" | perl -n -e 'print stderr $_'
  if (-e "$data[$i]") then
    echo "$plotcmd '$data[$i]' title '$curve_title'" >> $gnuplot
  else
    echo "$plotcmd $data[$i] title '$curve_title'" >> $gnuplot
  endif

  set plotcmd = replot
  @ i++
end
#echo "replot" >> $gnuplot
#echo "replot" >> $gnuplot

if (!($?out)) then
  echo "pause 1e6 'Hit ^C to exit: '" >> $gnuplot
else
  echo "set output '$out'" >> $gnuplot
  echo replot >> $gnuplot
  # make a latex file if required
  if ($?latex) then
    echo "\begin{figure$col}" >> $latex
    echo "\centerline{" >> $latex
    echo "  \includegraphics[width=$width]{$out}" >> $latex
    echo "}" >> $latex
    echo "\caption{$name}" >> $latex
    echo "\label{fig:$name}"  >> $latex
    echo "% $command" >> $latex
    echo "\end{figure$col}" >> $latex
    echo "\markfigure{\ref{fig:$name}}" >> $latex
  endif
endif

# end the latex document
if ($?latex) then
  echo  "\begin{document}" >> tmp.tex
  echo  "\input{$latex2}" >> tmp.tex
  echo  "\end{document}" >> tmp.tex
endif

# execute the gnuplot script
if (!($?nocat)) then
  cat $gnuplot | perl -n -e 'print stderr $_'
endif
gnuplot $gnuplot

cleanup:
/bin/rm $pgm.*.$$.tmp
