#!/usr/bin/perl
# $Id: get_ASR_750,v 1.4 2009/11/04 09:41:33 skoch Exp $
use strict;
use warnings;
use AFM;

if (scalar(@ARGV) < 2)
{
    print "USAGE:  get_ASR_750 \$rig \$test [optional --flow\$temp] [optional --volt\$voltage]\n";
    print "If the --flow[\$temp] option is given, the minimum ASR750 for the given\n"; 
    print "temperature and a dry hydrogen flow of 24\+\-1 L/hour and a air flow \nof 140\+\-5 L/hour\ is given.";
    print " If no temperature is given default is 850C\n";
    print "If the --volt[\$temp] option is given, the minimum ASR for the specified voltage is given insteaad of the default 750 mV\n"; 
    exit
}
my $sortflow = 0;
my $temp = '';
my $volt = 750;
if (scalar(@ARGV) > 2)
{
    for (my $x = 2; $x < scalar(@ARGV); $x++)
    {
	if ($ARGV[$x] =~ /--flow(\d*)/)
	{
	    $sortflow = 1;
	    $temp = $1;
	}
	if ($ARGV[$x] =~ /--volt(\d*)/)
	{
	    $volt = $1;
	}
    }
}
$temp = 850 unless ($temp);
my $rig = $ARGV[0];
$rig =~ /(\d+)/;
$rig = $1+0;
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 @gaslist = split(/\s*\,\s*/,&AFM::get_usercv_test($rig,$test,'report','gas_names_ivtable'));
@gaslist = split(/\s*\,\s*/,&get_usercv($rig,'report','gas_names_ivtable')) unless (@gaslist);
@gaslist = ('h2','ch4','o2','air','backup') unless (@gaslist);
@gaslist = ('h2','air','o2','ch4') if ($sortflow); ### Special case for sortflow ###
my @lines = ();
for (my $x = 1; $x <= $max ; $x++)
{
    my $cmd = "/usr/local/bin/ASR750 $rig $test $x $volt";
    my $ASR = `$cmd`;
    $ASR =~ /([\d\.\-]+)\s+([\d\.\-]+)/;
    $ASR = $1+0;
    my $SEC = $2+0;
    if ($ASR =~ /\d+\.\d\d\d+/)
    {
	$ASR =~ /(\-*\d+\.\d\d\d)/;
	$ASR = $1;
    } 
    if ($SEC =~ /\d+\.\d\d\d+/)
    {
	$SEC =~ /(\-*\d+\.\d\d\d)/;
	$SEC = $1;
    } 
    $cmd = "/usr/bin/head -1 $dir".'ivdatadir-'.$x.'/ivdata';
    my $line = `$cmd`;
    my %flow = ();
    for my $gas (@gaslist)
    {
	$line =~ /$gas\s+([\-\+\d\.]+)/;
	$flow{$gas} = sprintf("%.1f",$1);
    }
    $line =~ /\sT_center\s+([\-\d]+)/;
    my $temp = $1+0;
    $line =~ /^\s*([\d\.]+)/;
    my $time = $1+0;
    $time = sprintf("%.1f",$time);
    $line =~ /\scell_voltage\s+([\-\d\.]+)/;
    my $volt = sprintf("%.3f",($1+0)/1000);
    if ($volt == -32.768)
    {
	$volt = -32768;
    }
    my $gstr = '';
    for my $gas (@gaslist)
    {
	$gstr .= '     '.$flow{$gas};
    }
    push @lines ,"$x    $time  $SEC   $ASR   $volt   $temp$gstr\n";
}
my $print = 0;
$print = $sortflow;
my $gstr = '';
for my $gas (@gaslist)
{
    $gstr .= '      '.$gas;
}
print "iv   time R_SEC$volt ASR$volt  OCV  T_center$gstr\n" unless ($print);
print @lines unless ($print);
exit unless ($print);

if ($sortflow)
{
    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 > $temp - 8) && ($T < $temp + 8) && ($H > 23) && ($H < 25) && ($air < 145) && ($air > 135) && ($O < 1) && ($C < 1))
	{
	    push @flowok, $line;
	}
    }
    my $ASRmin = 1000000;
    for my $line (@flowok)
    {
	$line =~ /^\s*\d+\s+[\d\.]+\s+[\d\.\-]+\s+([\d\.\-]+)/;
	my $ASR = $1+0;
	if (($ASR > 0) && ($ASR < $ASRmin)){$ASRmin = $ASR};
    }
    if ($ASRmin == 1000000)
    {
	$ASRmin = -32768;
    }
    print $ASRmin,"\n";
}
exit;

