#!/usr/bin/perl
# $Id: voltage_vs_flow_plot.pl,v 1.2 2006/04/11 10:48:09 skoch Exp $


$flow=$ARGV[0];
$infile=$ARGV[1];


@values = (
"emf0_3" ,
"emf0_5" ,
"emf0_8" ,
	   );

$line=`head -1 $infile`;
    @Fld = split ( ' ', $line, 9999 );

    for ( $i = 1 ; $i < $#Fld ; $i += 2 ) {

        $data{ $Fld[$i] } = $Fld[ $i + 1 ];
        $col{ $Fld[$i] } = $i+2
	
	}

	
$cstr="sort -k $col{$flow} $infile | head -1";
#print $cstr,"\n";

$line=`$cstr`;
    @Fld = split ( ' ', $line, 9999 );

    for ( $i = 1 ; $i < $#Fld ; $i += 2 ) {

        $data{ $Fld[$i] } = $Fld[ $i + 1 ];
        $col{ $Fld[$i] } = $i+2
	
	}

foreach (@values) 
{
    $ila++;
    print "set la ",$ila," \"", substr($_,5,1)/10.0," A/cm^2  \"" , " at " , $data{$flow},",", $data{$_ }," right ","\n";
}

print <<END
set nokey
set tmargin 2
set lmargin 10
set rmargin 10
set bmargin 4
set size 1,0.5
set grid  linetype 5
set border 3
set xtics nomirror
set ytics nomirror
set ylabel "Cell voltage (mV)"
set xlabel "$flow flow (l/h)"
#set nok
set xr[0:]
set yr[300:1100]
set ytics 300,200,1100
set term post portrait enhanced color
set out \'$infile.ps\'
END
    ;
$i=0;
    foreach (@values) { 
$i++;
if ($i==1) {  print "plot \'$infile\' u  ",$col{$flow},":",$col{$_}; } else {  print ",\'\' u  ",$col{$flow},":",$col{$_}; }}
print "\nset out \n";
