#!/usr/bin/perl
# $Id: get_impedance_ASRFU.pl,v 1.2 2006/07/14 08:31:40 skoch Exp $
use strict;
use warnings;
use AFM;
my $rig = undef;
my $tmpstr = shift;
if ( $tmpstr =~ /^(\d+)$/ ) { $rig = $1 };
&check_run($rig);
my $test = undef;
$tmpstr = shift;
if ( $tmpstr =~ /^(\d+)$/ ) { $test = $1 };
my $dir = $AFM::publicdir.'rig'.$rig.'/'.$rig.'test'.$test.'/impedance/';
my $impd = $dir.'imp_data';
my $outfile = $impd.'_corr';
my $jdat = $dir.'imp_jdata';
exit unless ((-e $impd) && (-e $jdat));
open IN,'<',$impd or die "Could not open file $impd for reading!\n";
my @impdata = <IN>;
close IN;
open IN,'<',$jdat or die "Could not open file $jdat for reading!\n";
my @jdat = <IN>;
close IN;

my $o2cutoff = &AFM::get_usercv_test( $rig,$test, 'gas_o2','cutoff_report',) + 0;
my $ch4cutoff = &AFM::get_config_value_old( $rig, '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);

###################################
### Default is 80 ma Amplitude! ###
###################################
my $i = 0.08;
$i = shift if (@ARGV);
my $area = &AFM::get_usercv_test($rig,$test,'main','cell_area');
$area = &AFM::get_config_value_old($rig,'area',$test) unless ($area);
open OUT,'>',$outfile or die "Could not open file $outfile for writing!\n";
for (my $x = 0; $x < @impdata; $x++)
{
    my %d = &get_data($jdat[$x]);
    my $st = $impdata[$x];
    $st =~ /Rs\:([\.\d\-\+eE]+)/;
    my $rs = $1+0;
    $st =~ /Rp\:([\.\d\-\+eE]+)/;
    my $rp = $1+0;
    my $r = $rs+$rp;
    my $du = $r * $i;
    my $emf = -$d{'cell_voltage'}/1000;
    my $u = $emf + $du;
    my $o2 = 0;
    $o2 = $d{'o2'} if ($d{'o2'} > $o2cutoff);
    my $ch4 = 0;
    $ch4 = $d{'ch4'} if ($d{'ch4'} > $ch4cutoff);
    my $h2 = $d{'h2'} + $deltaH2;
    my $cmd = '/usr/local/bin/OCV_corr --command par_all --A '.$area. " --H2flow $h2 --O2flow $d{'air'} --O2_add $o2 --I $i --water $d{'bottle_temp'} --emf $emf --cell $d{'T_center'} --U $u";
    my @l = `$cmd`;
    $l[0]=~ /[\t\s]+([\d\.\+\-eE]+)/;
    my $sek = $1+0;
    $l[1]=~ /[\t\s]+([\d\.\+\-eE]+)/;
    my $asr = $1+0;
    my $rc = ($sek - $asr);
    $cmd = '/usr/local/bin/OCV_corr --command demf --A '.$area. " --H2flow $h2 --O2flow $d{'air'} --O2_add $o2 --I $i --water $d{'bottle_temp'} --emf $emf --cell $d{'T_center'} --U $u";
    my $res = `$cmd`;
    $res = 0 unless($res =~ /^\s*[\-\d\.]+/);
    chomp $res;
    my $remf = $emf - $res;
    my $dv = $u - $remf;
    my $rcorr = $dv/$i;
    my $rpc = $rcorr-$rs;
    print OUT sprintf(" $d{'id'} Time %.0f T_center %.0f h2 %.1f o2 %.1f air %.0f ch4 %.1f backup %.0f Current %.2f Rs %.4f Rp %.4f Rtot_corr %.4f Rp_corr %.4f emf %.4f r_corr %.4f\n",$d{'time'},$d{'T_center'},$d{'h2'},$o2,$d{'air'},$ch4,$d{'backup'},$d{'current'},$rs*$area,$rp*$area,$rcorr*$area,$rpc*$area,$emf,$rc);
}
close OUT;


sub get_data($)
{
    my $line = shift;
    my @l = split(/\s+/,$line);
    my %h = ();
    for (my $x = 4; $x < @l; $x+=2)
    {
	$h{$l[$x]} = $l[$x+1];
    }
    $h{'id'} = $l[1];
    $h{'time'} = $l[2];
    return %h;
}
