#!/usr/bin/perl
#$Id: ASR750,v 1.4 2015/03/11 10:11:58 sqko Exp $
use strict;
if (scalar(@ARGV) < 3)
{
    print "ASR750 calculates the Fuel utilisation corrected cell resistance\n";
    print "and secant resistance\n";
    print "at 750 mV for the \$ivnr\'th ivcurve for rig \$rig test \$test:\n";
    print "Usage: ASR750 \$rig \$test \$ivnr\n [opt voltage]";
    print "If the voltage option is specified this is used instead of the default 750 mV\n";
    exit;
}
my $rig = shift;
my $test = shift;
my $iv = shift;
my $tmpname='/tmp/ASR'. $$;
my $datafilename='/home/http/html/rig'.$rig.'/'.$rig.'test'.$test.'/iv/ivdatadir-'.$iv.'/ivdata_up';
my $cstr="indhold $datafilename | grep -P '\\bcell_voltage\\b' | awk '{print \$1+1}'";
my $voltcol=`$cstr`;
chomp($voltcol);
$cstr="indhold $datafilename | grep -P '\\bASR_corr\\b' | awk '{print \$1+1}'";
my $ASRcol=`$cstr`;
chomp($ASRcol);
$cstr="indhold $datafilename | grep -P '\\bR_secant\\b' | awk '{print \$1+1}'";
my $seccol=`$cstr`;
chomp($seccol);
my $volt = 750; 
$volt = shift if (@ARGV);
$cstr="awk '{print \$".$voltcol.",\$".$ASRcol."}' $datafilename > $tmpname";
system($cstr);
$cstr="perl /usr/local/bin/lineinterpol.pl $tmpname $volt | awk '{print \$2}'";
my $rep=`$cstr`;
chomp($rep);
$rep.=" ";
$cstr="awk '{print \$".$voltcol.",\$".$seccol."}' $datafilename > $tmpname";
system($cstr);
$cstr="perl /usr/local/bin/lineinterpol.pl $tmpname $volt | awk '{print \$2}'";
$rep .=`$cstr`;
print $rep;
unlink($tmpname);
exit;
