#!/bin/perl

do "parameters.pl";


# Open the file for reading.
open DATA, "usbscopes" or die "can't open usbscopes $!";
my @data = <DATA>;
close (DATA);




# We have to get rid of DAS shift
$aver=0;
for ($i = 1; $i <= $TriggerLine; ++$i)  
{
    @row = split(' ',$data[$i]);
    $aver+=$row[0];
}
$aver=$aver/$TriggerLine;




$Time=0;
$UloopMax=0;
$Uloop=0;
$Total=0;
open FH, ">Uloop_dp.csv";
$start = ($TororoidalMagneticFieldTrigger)/$DeltaT+20; #remove influence of tyristors

for ($i = 0; $i < $start; ++$i) 
{
    $Time+=$DeltaT;
    printf FH "%3.2f;%4.3f\n",$Time,0;
}

for ($i = $start; $i < $LineCount; ++$i)  
{
    @row = split(' ',$data[$i]);
    $Time+=$DeltaT;
    $I = $row[0]-$aver;
    $Total+=abs($I)*$DeltaTs;    #Bereme jen kladnou cast, protoze tyristor to nezasekne v zaporu a prumer by mohl byt spatny ...
    
    #remove noise from toroidal field
    if(abs($TororoidalMagneticFieldTrigger-$DeltaT*$i) < 0.1)
     { $I = $aver;}

    printf FH "%3.2f;%4.2f\n",$Time,$I;
    if ( abs($row[0]) > abs($UloopMax)  ) {$UloopMax=$I;}
}
close (FH);

open RESULT, ">UloopMax";
    printf RESULT "%4.3f", $UloopMax;
close RESULT;

open RESULT, ">MeanUloop"; #Slouzi jen k detekci "failures"
    printf RESULT "%3.2f", $Total/($LineCount*$DeltaTs);
close RESULT;
