#!/usr/bin/perl
# $Id: degradation_300_1500,v 1.1 2006/03/28 13:18:50 skoch Exp $
use AFM;
use strict;
use warnings;
my $PROGRAM = 'degradation_300_1500';

my $deltat = 2;

if (scalar(@ARGV) < 3)
{
    warn "Usage: $PROGRAM \$rig \$test \$start_time\n";
    warn "Where \$start_time is in hours after the test began\n";
    warn "The resultant degradation rates are given after 300\nand 1500 hours after the specified time\n";
    die "\n";
}

my $rig = $ARGV[0]+0;
if (!&AFM::check_rig_nr($rig))
{
    die "Wrong rig $rig !!\n";
}

my $usernumber = $<;
my $username = &AFM::get_user_name($usernumber);
my $testusername = 'rig'.$ARGV[0];
if ($username ne $testusername)
{
    warn "wrong user: $username !\n";
    warn "To access data for rig $ARGV[0] you must be user rig$ARGV[0]!\n";
    exit 1;
}
if ( !&AFM::check_rig_nr($rig) )
{
    print "Rig $rig is not valid!\n";
    exit 1;
}

my $test = $ARGV[1];
$test =~ /(\d+)/;
$test = $1+0;
if (($test < 1) || ($test > &AFM::get_test_nr($rig)))
{
    die "Illegal test nr: $test!!\n";
}
my $startt = $ARGV[2];
$startt =~ /([\d.]+)/; 
$startt= $1+0;
my $endt1 = $startt + 300;
my $endt2 = $startt + 1500;
my $jdata = $AFM::publicdir.'rig'.$rig.'/'.$rig.'test'.$test.'/jdata';

my $trueend = `/usr/bin/tail -1 $jdata`;
$trueend =~ /^\s*([\d\.]+)/;
$trueend = $1+0;

if ($endt1 > $trueend)
{
    die "Wrong times, time +300 hours is larger than the end time of the test as found in\n$jdata!\n";
}
my $header = `/usr/bin/head -1 $jdata`;
chomp $header;
my @list = split(/\s+/,$header);
my $id = 0;
my $idi = 0;
for (my $x = 0; $x < scalar(@list); $x++)
{
    if ($list[$x] =~ /cell_voltage/)
    {
	$id = $x+2;
    }
    if ($list[$x] =~ /current/)
    {
	$idi = $x+2;
    }
}
my $cmd = "/bin/awk \'\{print \$1 \"  \"  \$$id \" \" \$$idi;}\' $jdata";
@list = `$cmd`;
my $sumUs = 0;
my $nxs = 0;
my $sumUe1 = 0;
my $nxe1 = 0;
my $sumUe2 = 0;
my $nxe2 = 0;
my $is = 0;
my $ie1 = 0;
my $ie2 = 0;
for my $str (@list)
{
    my ($t,$u,$i) = split(/\s+/,$str);
    if (($t > ($startt-$deltat)) && ($t < ($startt+$deltat)))
    {
	$sumUs += $u;
	$is += $i;
	$nxs++;
    }
    if (($t > ($endt1-$deltat)) && ($t < ($endt1+$deltat)))
    {
	$sumUe1 += $u;
	$ie1 += $i;
	$nxe1++;
    }
    if (($t > ($endt2-$deltat)) && ($t < ($endt2+$deltat)))
    {
	$sumUe2 += $u;
	$ie2 += $i;
	$nxe2++;
    }
}
my $deltai = 0.2;
my $Us = $sumUs / $nxs;
$is = $is / $nxs;
my $Ue1 = 'NA';
$ie1 = $ie1 / $nxe1;
$Ue1 = $sumUe1 / $nxe1;
my $Ue2 = 'NA';
if ($nxe2)
{
    $ie2 = $ie2 / $nxe2;
    $Ue2 = $sumUe2 / $nxe2;
}
my $dU1 = 'NA';
if (($ie1 > $is - $deltai) && ($ie1 < $is + $deltai))
{
    $dU1 = ($Us - $Ue1) * 1000/($endt1 - $startt);
}
my $dU2 = 'NA';
if (($ie2 > $is - $deltai) && ($ie2 < $is + $deltai))
{
    $dU2 = ($Us - $Ue2) * 1000/($endt2 - $startt);
}
$Us = &chop($Us);
$Ue1 = &chop($Ue1);
$Ue2 = &chop($Ue2);
$dU1 = &chop($dU1);
$dU2 = &chop($dU2);
print "300 hours ($startt h - $endt1 h): $Us mV  End: $Ue1 mV  Degradation: $dU1 myV/h\n";
print "1500 hours ($startt h - $endt2 h): $Us mV  End: $Ue2 mV  Degradation: $dU2 myV/h\n";
my $outfile = $AFM::publicdir.'rig'.$rig.'/'.$rig.'test'.$test.'/degradation_300_1500.txt';
open OUT ,"> $outfile" or die "Could not open file $outfile";
print OUT "300 hours ($startt h - $endt1 h): $Us mV  End: $Ue1 mV  Degradation: $dU1 myV/h\n";
print OUT "1500 hours ($startt h - $endt2 h): $Us mV  End: $Ue2 mV  Degradation: $dU2 myV/h\n";
close OUT;
exit;

sub chop($)
{
    my $x = $_[0];
    return $x if ($x =~ /NA/); 
    my $sign = 1;
    if ($x < 0){$sign = -1}; 
    my $res = $x;
    if ($x > 1)
    {
	if ($x =~ /(\d+\.\d\d)/)
	{
	    $res = $1+0;
	}
    }
    else
    {
	if ($x =~ /(\d+\.\d\d\d)/)
	{
	    $res = $1+0;
	}
    }
    $res = $res * $sign;
    return $res;
}
