#!/usr/local/bin/perl -w --



my @f = ();
my $f = 'amoeba.tmp';
my $start = 10;
my $avgvar = 1;

while($ARGV[0] =~ m'^-')	{
	my $arg = shift(@ARGV);
	if($arg eq '-f')	{
		push(@f, shift(@ARGV));
		}
	elsif($arg eq '-s')	{
		$start = shift(@ARGV);
		}
	elsif($arg eq '-n')	{
		$avgvar = 0;
		}
	elsif($arg eq '--')	{ last; }
	else	{
		die "ERR: show_prms.sh: unregognized flag: $arg\n";
		}
	}

my ($n1, $n2, $trash) = @ARGV;

if($#f < 0)	{ push(@f, $f); }
@ARGV = @f;

$| = 1;

my $td = "/tmp/wp$$";
mkdir($td, 0700);

if($avgvar)	{
	open(A1, "| col_average -s -Cpre p$n1: -Cmid +-");
	open(A2, "| col_average -s -Cpre p$n2: -Cmid +-");
	}


my $lav = '';
my $nf = 0;
my $nr = 0;
while(<>)	{
	if($lav ne $ARGV)	{
		$lav = $ARGV;
		open(T, ">$td/$nf") or die "Can't open $td/$nf";
		$nf++;
		$nr = 0;
		}
	next if(m/^#/);
	s/\s#.*//;
	next if(m/^$/);
	if($nr++ > $start)	{
		my @a = split(' ', $_);
		next if($#a<$n2 || $#a<$n1);
		print T $a[$n1] . " " . $a[$n2] . "\n";
		if($avgvar)	{
			print A1 $a[$n1] . "\n";
			print A2 $a[$n2] . "\n";
			}
		}
	if($. % 200 == 199)	{
		print ".";
		}
	}
print "\n";

close(T);
if($avgvar)	{
	close(A1);
	close(A2);
	}
print "type redraw to redraw graph.\n";
open(SM, "| tee foo | sm -S");
select(SM);
$| = 1;
print SM "macro redraw	{\n";
print SM "	erase box\n";
my $k;
for($k=0; $k<$nf; $k++)	{
	my $color = $k+2;
	print SM "	ptype 3 1 expand 1.5 ctype $color points x$k y$k\n";
	print SM "	ptype 1 1 expand 1 ctype 1 points x$k y$k\n";
	print SM "	ctype default\n";
	my $z = "[0]";
	my $e = "[DIMEN(x$k)-1]";
	print SM "	draw_arrow \$(x$k$z) \$(y$k$z) \$(x$k$e) \$(y$k$e)\n";
	}
print SM "	ctype default\n";
print SM "	xlabel p$n1\n";
print SM "	ylabel p$n2\n";
print SM "	}\n";
for($k=0; $k<$nf; $k++)	{
	print SM "	data \"$td/$k\"\n";
	print SM "	read {x$k 1 y$k 2}\n";
	}
print SM "terminal x11\n";
print SM "limits x0 y0\n";
print SM "redraw\n";
while(<STDIN>)	{
	print SM $_;
	}
print SM "quit\n";
close(SM);
unlink($td);

