#!/local/bin/perl --  # -*-Perl-*-
'di';
'ig00';
# mp	
#
# Stephen Eglen
# COGS, University of Sussex.
#
# Created 24 Jun 96
# Latest version should be found in 
# http://www.cogs.susx.ac.uk/users/stephene/gnuplot/mp
#
# Comments at end of file
#
$RCSHEADER = '$Header: /rsunc/home/stephene/bin/perlbin/mp,v 1.3 1996/07/19 19:57:32 stephene Exp $';	#'
'$Revision: 1.3 $'  =~ /^\$\w+:\s+([.1234567890]+)\s+\$$/;	#'
$VERSION = $1;



# max screen coordinates - set the defaults
$maxscreenx=0.7;
$maxscreeny=0.7;

@copyargs = @ARGV;

$ncols=0; $nrows=0;


# Process the command line options
do 'newgetopt.pl'; do NGetOpt ('l:i', 'c', 's', 'x:f', 'y:f', 't:s');

if ($opt_s) {
    $square=1;
} else {
    $square=0;
}
if ($opt_l ) {
    $nminor=$opt_l;
}

if ($opt_c) {
    $majorcol=1;
} else {
    $majorcol=0;
}


if ($opt_x)  {
    $maxscreenx = $opt_x;
}

if ($opt_y)  {
    $maxscreeny = $opt_y;
}

# count the number of files

$numplots = $#ARGV+1;
@files =  @ARGV;

print "there are $numplots to plot\n";

# Create the output file
open(FP, ">mpout");
print FP "# generated by mp with @copyargs\n";
# pre row/col stuff
print FP "set size  $maxscreenx, $maxscreeny\n";
print FP "set origin 0.0, 0.0\n";
print FP "set multiplot\n";

# which is the minor and which the major axis?

# if ($majorcol) {
#     $nmajor = $ncols;
#     $nminor = $nrows;
# } else {
#     $nminor = $ncols;
#     $nmajor = $nrows;
# }



# work out other dimension. 
# set a default value of 4 plots in a line if no value is specified
if ($nminor == 0 ) {
    # set a default of 4 across.
    $nminor=4;
}


if ($nmajor == 0 ) {
    $nmajor = int($numplots/ $nminor);
    if ( $numplots% $nminor) {
	# there is an incomplete line.
	$nmajor++;
    }
}




# When creating the plots, we can go in two directions:

# 1. Fill up each column (default)
#
# 1 2 3 4
# 5 6 7 8

# or fill up each row (get with -c flag)
#
# 1 3 5 7
# 2 4 6 8


# dx,dy describes how much to move from one plot to the next. Eg for
# filling each column, it describes how to move the origin from plot 1
# to plot 2.

# majordx, majordy is the offset from the origin of the start of one
# line to the start of the next line.  So, when filling by columns, it
# describes how to move the origin from plot 1 to plot 5.


print "nminor $nminor nmajor $nmajor\n";


if ($majorcol) {
    # fill up a col at a time
    $minorinc = $maxscreeny/$nminor;
    $majorinc = $maxscreenx/$nmajor;

    $sx = $majorinc;
    $sy = $minorinc;

    $dx = 0;
    $dy = 0.0 - $sy;

    $majordx = $sx;
    $majordy = 0;

}
else {
    # fill up a row at a time
    $minorinc = $maxscreenx/$nminor;
    $majorinc = $maxscreeny/$nmajor;

    $sx = $minorinc;
    $sy = $majorinc;

    $dx = $sx;
    $dy =0;
    
    $majordx = 0;
    $majordy = 0.0 - $sy;
    

}
    

# ox, oy is the origin of the current plot.

# get ready for the first screen
$oy=$maxscreeny - $sy; # top of screen - first graph.
$ox=0;

$startlinex = $ox; $startliney = $oy;
$minor=0;



if ($square) {
    # make plots the same size
    $min = &min($sx, $sy);
    $sx = $min;
    $sy = $min;
}

# Now do all the hard work of plotting out the commands.
for($plot=0; $plot < $numplots; $plot++) {
    $thisplot = $files[$plot];
    print FP "set origin $ox,$oy\n";
    print FP "set size $sx,$sy\n";
    if ($opt_t) {
	$titlestr = $opt_t;
	$titlestr =~ s/%s/$plot/g;
	$titlestr =~ s/%d/$plot/g;
	$titlestr =~ s/%n/$thisplot/g;
	print FP "set title \"$titlestr\"\n";
    }
    if (-f $thisplot) {
	# this is a file to be plotted.
	print FP "plot '$thisplot\n";
    } else {
	# some arbitrary expression to plot
	print FP "plot $thisplot\n";
    }


    # move onto next plot
    $ox += $dx;
    $oy += $dy;
    $minor++;
    if ($minor == $nminor) {	
	# end of line: move onto next line
	$minor=0;
	
	$oy = $startliney + $majordy;
	$ox = $startlinex + $majordx;
	$startlinex = $ox; $startliney = $oy; # remember for next line
    }

}				# next plot



# print end of multiplot stuff, so reset now.
print FP "set nomultiplot\n";
print FP "set size $maxscreenx,$maxscreeny\n";
print FP "set origin 0.0,0.0\n";

# min(x,y) returns the minimum of x and y
sub min {
    local($x, $y) = @_;
    if ( $x <= $y ) {
	$x;
    } else {
	$y;
    }
}



# $Log: mp,v $
# Revision 1.3  1996/07/19  19:57:32  stephene
# Can now plot expressions as well as files
# ps bounding box problem solved (origin before multiplot entered must
# be 0,0)
#
# Revision 1.2  1996/06/25  20:42:15  stephene
# Initial version released
#
# Revision 1.1  1996/06/25  20:36:28  stephene
# Initial revision


########################################################

	# These next few lines are legal in both Perl and Nroff.

.00;		# finish .ig
 
'di		\" finish diversion--previous line must be blank
.nr nl 0-1	\" fake up transition to first page again
.nr % 0		\" start at page 1
'; __END__	#### From here on it's a standard manual page ####
.TH MP 1 "Mon Jun 24 1996" "COGS, University of Sussex."
.SH NAME
mp \- multiplot some plots in one gnuplot window
.SH SYNOPSIS
.B mp [-l n] [-c] [-s] [files/expressions]
.SH DESCRIPTION

Take a set of plots and creates a gnuplot script file to plot them
within one gnuplot window.  This uses the gnuplot multiplot option,
available in the 3.6 beta release.  For example, to plot the 6 files
f1..f6, we can do:

mp f1 f2 f3 f4 f5 f6

this program will then create a script file called mpout, which can be
loaded from within gnuplot.

gnuplot> load "mpout"
which produces plots like
f1 f2 f3 f4
f5 f6

By default, it will try to put 4 (entirely arbitrary) rows on one row
and then start on a new row.  For this example, it is probably neater
to put three plots on each row, so we can do

mp -l 3 f1 f2 f3 f4 f5 f6
which produces plots:
f1 f2 f3
f4 f5 f6

Alternatively, if you want to fill the columns instead of the rows,
specify the -c option:

mp -l 3 -c  f1 f2 f3 f4 f5 f6
which produces:
f1 f4
f2 f5
f3 f6

It can plot expressions as well as files.  It is very primitive in
deciding whether an argument is a file or an expression.  If the
argument exists as a file, it will be interpreted as a file, otherwise
it is assumed to be an expression.  For example:

mp -l 2 'sin(x)' 'cos(x)' f1 f2

Will print the sin and cos functions and then files f1 and f2,
assuming they exist.  The quotes around sin and cos are needed to
prevent the shell interpreting the brackets within the expressions.

.SH OPTIONS
-l n -- put n elements on one line.  
-c fill up columns instead of rows.
-s try to make plots square.
-x n set size in x direction to n (default 0.7)
-y n set size in y direction to n (default 0.7)
-t 'title str' produce a title str for each plot. 
    %s will be replaced with the plot number
    %d will be replaced with the plot number
    %n will be replaced with the name of the current plot

.SH ENVIRONMENT
.SH EXAMPLES
Simple example:

% gnuplot
gnuplot> ! mp -l 2 'sin(x)' 'cos(x)' 'sqrt(x)' 'x*x' 
there are 4 to plot
nminor 2 nmajor 2
!
gnuplot> load "mpout"

(In this example, the y extent of the plots may differ, due to the
ytics.  To make all of the plots the same size, take the ytics off
using "set noytics".)


.SH FILES
.SH AUTHOR
Stephen Eglen <stephene@cogs.susx.ac.uk>
.SH SEE ALSO
.SH DIAGNOSTICS
.SH BUGS

When creating a postscript file of the current gnuplot window, if the
bounding box in postscript is out of range, use the ps2epsi program to
fix the bounding box.  This should be fixed now. Note that the size of
the bounding box is calculated by the size and orign at the time 'set
multiplot' is executed.
