#!/usr/bin/perl
# $Id: iv1.pl,v 1.2 2015/11/11 10:17:12 sqko Exp $
use Time::Local;
use AFM;
$rigno = $ARGV[2];
$testno = $ARGV[3];
$currentlabel = &AFM::get_usercv_test($rigno,$testno,'IV_control','current_label');
$voltlabel = &AFM::get_usercv_test($rigno,$testno,'IV_control','voltage_label');
$templabel = &AFM::get_usercv_test($rigno,$testno,'IV_control','temp_label');
$currentlabel = 'current' unless ($currentlabel); ## Default value for backwards compatibility ###
$voltlabel = 'cell_voltage' unless ($voltlabel);  ## Default value for backwards compatibility ###
$templabel = 'T_center' unless ($templabel); ## Default value for backwards compatibility ###
#### Get list of gasses ###
@gaslist = split(/\s*\,\s*/,&AFM::get_usercv_test($rigno,$testno,'main','gas_names'));
@gaslist = split(/\:/,&AFM::get_config_value_old($rigno,'gas_name',$testno)) unless (@gaslist);
@gaslist = ('h2','o2','air','ch4','backup','sweep') unless (@gaslist);


open IVT,  '<', $ARGV[0] or die "Could not open $ARGV[0] $!\n";
open DATA, '<', $ARGV[1] or die "Could not open $ARGV[1] $!\n";
my $str = <DATA>;
my @l = split(/\s+/,$str);
my $utimecol = 0;
for (my $x = 0; $x < @l; $x++)
{
    next unless ($l[$x] =~ /utime/);
    $utimecol = $x+1;
}
$nextline = undef;
if ($ARGV[2])
{
    $tshiftfile .= '/home/http/html/rig'.$ARGV[2].'/'.$ARGV[2].'test'.$ARGV[3].'/timeshift.txt';
    if (-e $tshiftfile)
    {
        open MYIN,'<',$tshiftfile or die;
        $tshift = <MYIN>;
        chomp $tshift;
        $tshift = -3600 * $tshift;
        $nextline = <MYIN>;
    }
}
$ivcount = 0;
$tbuf=1;
while (<IVT>) {
    $ivcount++;
    if ($nextline =~ /^$ivcount\s+([\d\.]+)/)
    {
        $tshift = -$1*3600;
        $nextline = <MYIN>;
    }
    @a  = split ( " ", $_ );
    @t1 = split ( ":", $a[0] );
    $t1[0] -= 1900;
    $t1[1] -= 1;

    @t2 = split ( ":", $a[1] );
    $t2[0] -= 1900;
    $t2[1] -= 1;

    $tl1 = timelocal( reverse(@t1) ) - $tbuf + $tshift;
    $tl2 = timelocal( reverse(@t2) ) + $tbuf + $tshift;
    $d[$utimecol] = 0;
    $cmax=0;
    $cmaxa = 0;
    while ( ( $line = <DATA> ) && ( $d[$utimecol] < $tl1 ) ) {

        @d = split ( " ", $line );

    }
    $nstart=$.-1;
    $dstart=$d[1];
    $tstart=$d[0];
    my %gasmax = ();
    my %gasses = ();
    while ( ( $line = <DATA> ) && ( $d[$utimecol] < $tl2 ) ) {
        @d = split ( " ", $line );
	
	if ($d[$utimecol] < $tl2 )  {$dstop=$d[1];$tstop=$d[0];};
        if ($#d) {
            for ( $i = 2 ; $i < $#d ; $i += 2 ) 
	    {
                $data{ $d[$i] } = $d[ $i + 1 ]; 
	    }
	    $Time = $d[0];
	    $date = $d[1];
	    foreach (@gaslist)
	    {
		$gasses{$_} = $data{$_};
	    }
            $current = $data{$currentlabel};
            $voltage = $data{$voltlabel};
	    
	    if (abs($current) > $cmaxa) {	#???
		$cmaxa = abs($current);
		$cmax = $current;
		$ncmax = $.;
		$vcmax = $voltage;
		$tmax = $Time;
		foreach (@gaslist)
		{
		    $gasmax{$_} = $gasses{$_};
		}
		
	    }
	}
	
    }
    $h2max = 0;
    $o2max = 0;
    $airmax = 0;
    $ch4max = 0;
    $backupmax = 0;
    $h2max = $gasmax{'h2'} if  (exists($gasmax{'h2'}));
    $o2max = $gasmax{'o2'} if  (exists($gasmax{'02'}));
    $airmax = $gasmax{'air'} if  (exists($gasmax{'air'}));
    $ch4max = $gasmax{'ch4'} if  (exists($gasmax{'ch4'}));
    $backupmax = $gasmax{'backup'} if  (exists($gasmax{'backup'}));
    
    print 'dstart'," ", $dstart," ", 'dstop'," ", $dstop," ", 'nstart'," ", $nstart," ", 'ncmax'," ",
    $ncmax," ", 'nstop'," ", $.-2 ," ", 'tstart'," ", $tstart," ", 'tstop'," ", $tstop," ", 'cmax'," ",
    $cmax," ", 'Voltage_at_max_current'," ", $vcmax," ", 'H_2'," ", $h2max," ", 'O_2'," ",
    $o2max," ", 'Air'," ", $airmax," ", 'CH_4'," ", $ch4max," ", 'backup'," ", $backupmax,"\n";
    $cmax = 0;

}
