#!/usr/bin/perl
# $Id: get_leak_iv,v 1.1 2006/03/28 13:30:44 skoch Exp $
use strict;
use warnings;
use AFM;

if (scalar(@ARGV) < 2)
{
    print "USAGE:  get_leak_iv \$rig \$test [--flow]\n";
    print "If the --flow option is given, the minimum leak for a cell temperature\n";
    print "of 850\+\-7 C  and a dry hydrogen flow of 24\+\-1 L/hour and a air flow \nof 140\+\-5 L/hour\ is given\n";
    exit
}
my $sortflow = 0;
if (scalar(@ARGV) > 2)
{
    for (my $x = 2; $x < scalar(@ARGV); $x++)
    {
	if ($ARGV[$x] =~ /--flow/)
	{
	    $sortflow = 1;
	}
    }
}
my $rig = $ARGV[0];
$rig =~ /(\d+)/;
$rig = $1+0;
&check_run($rig);
my $test = $ARGV[1];
$test =~ /(\d+)/;
$test = $1+0;
if (($test < 1) || ($rig < 1))
{
    die "ERROR::  invalid rig or test number!!\n";
} 
my $dir = $AFM::publicdir.'rig'.$rig.'/'.$rig.'test'.$test.'/iv/';
my $cmd = '/bin/ls -1d '.$dir.'ivdata* 2>/dev/null';
my @list = `$cmd`;
my $max = 0;
for my $str (@list)
{
    $str =~ /ivdatadir-(\d+)/;
    my $d = $1+0;
    if ($d > $max){$max = $d};
}
if ($max < 2)
{
    my $file = $dir.'ivdatadir-1/ivdata';
    if (!-e($file))
    {
	print "No i-V curves found!!\n";
	exit;
    }
    my @l = `/bin/cat $file`;
    if (scalar(@l) < 5)
    {
	print "No i-V curves found!!\n";
	exit;
    }
}
my @lines = ();
for (my $x = 1; $x <= $max ; $x++)
{
    my $cmd = "/usr/local/bin/calc_leak $rig $test $x";
    my $res = `$cmd`;
    $res =~ /Leak_in:\s+([\w\.\-]+)/;
    my $leaki = $1;
    $res =~ /Leak_cell:\s+([\w\.\-]+)/;
    my $leakc = $1;
    $res =~ /Leak_out:\s+([\w\.\-]+)/;
    my $leako = $1;
    $cmd = "/usr/bin/head -1 $dir".'ivdatadir-'.$x.'/ivdata';
    my $line = `$cmd`;
    $line =~ /\sh2\s+([\-\d\.]+)/;
    my $h2 = $1+0;
    $line =~ /\sair\s+([\-\d\.]+)/;
    my $air = $1+0;
    $line =~ /\so2\s+([\-\d\.]+)/;
    my $o2 = $1+0;
    $line =~ /\sch4\s+([\-\d\.]+)/;
    my $ch4 = $1+0;
    $line =~ /\sT_center\s+([\-\d]+)/;
    my $temp = $1+0;
    if ($h2 =~ /\d+\.\d+/)
    {
	$h2 =~ /(\-*\d+\.\d)/;
	$h2 = $1;
    } 
    if ($o2 =~ /\d+\.\d+/)
    {
	$o2 =~ /(\-*\d+\.\d)/;
	$o2 = $1;
    } 
    if ($ch4 =~ /\d+\.\d+/)
    {
	$ch4 =~ /(\-*\d+\.\d)/;
	$ch4 = $1;
    } 
    if ($air =~ /\d+\.\d+/)
    {
	$air =~ /(\-*\d+\.\d)/;
	$air = $1;
    }
    $line =~ /^\s*([\d\.]+)/;
    my $time = $1+0;
    if ($time =~ /\d+\.\d+/)
    {
	$time =~ /(\-*\d+\.\d)/;
	$time = $1;
    } 
    $line =~ /\scell_voltage\s+([\-\d\.]+)/;
    my $volt = ($1+0)/1000;
    if ($volt =~ /\d+\.\d\d\d+/)
    {
	$volt =~ /(\-*\d+\.\d\d\d)/;
	$volt = $1;
    } 
    if ($volt == -32.768)
    {
	$volt = -32768;
    }
    warn "#### Getting leak current from iv curve nr $x ####\n";
    push @lines ,"$x    $time  $leaki    $leakc    $leako     $temp     $h2     $air     $o2     $ch4\n";
}

my $print = 0;
$print = $sortflow;

print "iv   time Leak_in Leak_cell Leak_out T_center H2flow airflow O2flow CH4flow\n" unless ($print);
print @lines unless ($print);
exit unless ($print);

if ($sortflow)
{
    my $o2cutoff = &AFM::get_usercv_test($rig,$test,'gas_o2','cutoff_report')+0;
    my $ch4cutoff = &AFM::get_usercv_test($rig,$test,'gas_ch4','cutoff_report')+0;
    $o2cutoff = &AFM::get_config_value_old($rig,'o2_cutoff',$test)+0 unless ($o2cutoff);
    $ch4cutoff = &AFM::get_config_value_old($rig,'ch4_cutoff',$test)+0 unless ($ch4cutoff);
    my $leaki = 100000;
    my $leako = 100000;
    my @flowok = ();
    for my $line (@lines)
    {
	$line =~ /.*\s+([\d\.\-]+)\s+([\d\-\.]+)\s+([\d\.\-]+)\s+([\d\.\-]+)\s+([\d\.\-]+)\s*\n/;
	my $T = $1+0;
	my $H = $2+0;
	my $air = $3+0;
	my $O = $4+0;
	my $C= $5+0;
	if (($T > 842) && ($T < 858) && ($H > 23) && ($H < 25) && ($air < 145) && ($air > 135) && ($O < $o2cutoff) && ($C < $ch4cutoff))
	{
	    push @flowok, $line;
	}
    }
    my $ASRmin = 1000000;
    my $ASR = 1000000;
    my $tin = 0;
    my $tou = 0;
    for my $line (@flowok)
    {
	if ($line =~ /^\s*\d+\s+[\d\.]+\s+([\d\.\-]+)\s+([\d\.\-]+)\s+([\d\.\-]+)/)
	{
	    $ASR = $2+0;
	    $tin = $1+0;
	    $tou = $3+0;
	}
	if ($ASR < $ASRmin)
	{
	    $ASRmin = $ASR;
	    $leaki = $tin;
	    $leako = $tou;
	}
    }
    if ($ASRmin == 1000000)
    {
	$ASRmin = -32768;
	$leaki = -32768;
	$leako = -32768;
    }
    print "$leaki   $ASRmin  $leako\n";
}
exit;
