#!/usr/bin/perl
# $Id: calc_leak,v 1.2 2008/07/23 12:55:17 skoch Exp $
if (scalar(@ARGV) < 3)
{
    print "calc_leak calculates leak current for the input and output fuel gas\n";
    print "as well as for the cell\n";
    print "for the \$ivnr\'th ivcurve for rig \$rig test \$test:\n";
    print "Usage: calc_leak \$rig \$test \$ivnr\n";
    exit;
}
use strict;
use AFM;
my $debug = 0;
if (scalar(@ARGV) > 3)
{
    for (my $x = 3; $x < scalar(@ARGV); $x++)
    {
	if ($ARGV[$x] =~ /--debug/){$debug = 1};
    }
} 


my $tmpname="/tmp/ASR". $$;
my $datafilename = $AFM::publicdir."rig".$ARGV[0]."/".$ARGV[0]."test".$ARGV[1]."/iv/ivdatadir-".$ARGV[2]."/ivdata_up";
open DATA, $datafilename or die "Could not open file $datafilename\n"; 
my $str = <DATA>;
my $count = 1;
my $po2i = &get_data($str,'O2_in');
my $poto = &get_data($str,'O2_out');
my $OCV = &get_data($str,'cell_voltage');
my $Tcell = &get_data($str,'T_center');
my $ti = &get_data($str,'T_in');
my $to = &get_data($str,'T_out');
my $H2 = &get_data($str,'h2');
my $O2 = &get_data($str,'o2');
my $O2C = &get_data($str,'o2_cathode');
my $air = &get_data($str,'air');
my $ch4 = &get_data($str,'ch4');
my $co = &get_data($str,'co');
my $co2 = &get_data($str,'co2');
my $water = &get_data($str,'bottle_temp');
my $current = &get_data($str,'current');
my $test = &get_data($str,'bottle_temp_enabled');
my $dry = 0;
$dry = 1 if (defined($test) && $test == 0);
my $curl = 0.02;
$str = <DATA>;
$current = &get_data($str,'current');
while (($current < $curl) && ($count < 10))
{
    $po2i += &get_data($str,'O2_in');
    $poto += &get_data($str,'O2_out');
    $OCV += &get_data($str,'cell_voltage');
    $Tcell += &get_data($str,'T_center');
    $ti += &get_data($str,'T_in');
    $to += &get_data($str,'T_out');
    $H2 += &get_data($str,'h2');
    $O2 += &get_data($str,'o2');
    $O2C += &get_data($str,'o2_cathode');
    $air += &get_data($str,'air');
    $ch4 += &get_data($str,'ch4');
    $co += &get_data($str,'co');
    $co2 += &get_data($str,'co2');
    $water += &get_data($str,'bottle_temp');
    $str = <DATA>;
    $current = &get_data($str,'current');
    $count++;
}
close DATA;
$po2i = $po2i / ($count*1000);
$poto = $poto / ($count*1000);
$OCV = -$OCV / ($count*1000);
$Tcell = $Tcell / $count;
$ti = $ti / $count;
$to = $to / $count;
$H2 = $H2 / $count;
$O2 = $O2 / $count;
$O2C = $O2C / $count;
$air = $air / $count;
$ch4 = $ch4 / $count;
$co = $co / $count;
$co2 = $co2 / $count;
$water = $water / $count;

my $leaki = 'NA';
my $leakc = 'NA';
my $leako = 'NA';

my $o2cutof = &AFM::get_usercv_test($ARGV[0],$ARGV[1],'gas_o2','cutoff_report')+0;
my $ch4cutof = &AFM::get_usercv_test($ARGV[0],$ARGV[1],'gas_ch4','cutoff_report')+0;
my $cocutof = &AFM::get_usercv_test($ARGV[0],$ARGV[1],'gas_co','cutoff_report')+0;
my $co2cutof = &AFM::get_usercv_test($ARGV[0],$ARGV[1],'gas_co2','cutoff_report')+0;
$o2cutof = &AFM::get_config_value_old($ARGV[0],'o2_cutoff',$ARGV[1])+0 unless ($o2cutof);
$ch4cutof = &AFM::get_config_value_old($ARGV[0],'ch4_cutoff',$ARGV[1])+0 unless ($ch4cutof);
if ($O2 < $o2cutof){$O2 = 0};
$co2 = 0 if ($co2 < $co2cutof);
$ch4 = 0 if ($ch4 < $ch4cutof);
$co = 0 if ($co < $cocutof);
my $command = '/usr/local/bin/OCV_corr --command leak --H2 '.$H2.' --O2_add '.$O2.' --water '.$water.' --Air '.$air.' --dry '.$dry.' --O2_cathode '.$O2C;
if ($ch4 || $co2 || $co )
{
    $command .= ' --use_ch4 YES --CO '.$co.' --CO2 '.$co2.' --CH4 '.$ch4
}
my $cmd = $command .' --cell '.$ti.' --emf '.$po2i;
if ($ch4 < $ch4cutof)
{
    print $cmd,"\n" if ($debug);
    $leaki = `$cmd`;
    chomp $leaki;
    $leaki = &rem_dig($leaki);
}

$cmd = $command .' --cell '.$Tcell.' --emf '.$OCV;
if ($ch4 < $ch4cutof)
{
    print $cmd,"\n" if ($debug);
    $leakc = `$cmd`;
    chomp $leakc;
    $leakc = &rem_dig($leakc);
}

$cmd = $command .' --cell '.$to.' --emf '.$poto;
if ($ch4 < $ch4cutof)
{
    print $cmd,"\n" if ($debug);
    $leako = `$cmd`;
    chomp $leako;
    $leako = &rem_dig($leako);
}

if ($count < 10)
{
    print "Leak_in: $leaki A    Leak_cell: $leakc A    Leak_out: $leako A\n";
}
else
{
    print "Leak_in: NA A    Leak_cell: NA A    Leak_out: NA A\n";
}
exit;

sub get_data($$)
{
    my $data = $_[0];
    my $id = $_[1];
    if ($data =~ /\s+$id\s+([\+\-\d\.eE]+)/)
    {
	return $1+0;
    }
    return undef;
}

sub rem_dig($)
{
    my $x = shift;
    return 'NA' if ($x =~ /nan/i);
    $x = sprintf("%.4f",$x);
    return $x;
}
