#!/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[2];
}
$aver=$aver/$TriggerLine;




$EFieldTrigger  =`cat ../Te_aktual`;
$EFieldTrigger = $EFieldTrigger/1000; # [ms]	


#Data integration
$integr=0;
$Time=0;
$IrogMax = 0;	
$dIdt_rogMax = 0;
open FH, ">Irogowski_dp.csv";
 #remove bad data points

$start = ($TororoidalMagneticFieldTrigger+$EFieldTrigger)/$DeltaT-10;


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

for ($i = $start; $i < $LineCount; ++$i)  
{
    @row = split(' ',$data[$i]);
    $integr+=($row[2]-$aver)*$RogowskiCalibration;
    $Time+=$DeltaT;
    if ( abs($integr) > abs($IrogMax)  ) {$IrogMax =$integr };
    if ( abs($row[2]-$aver) > abs($dIdt_rogMax)  ) {$dIdt_rogMax =$row[2] ; };
    printf FH "%3.2f;%4.3f\n",$Time,$integr;

}


close (FH);

open RESULT, ">IrogowskiMax";
    printf RESULT "%4.3f", $IrogMax;
close RESULT;
open RESULT, ">dIdt_rogMax";
    printf RESULT "%4.3f", $dIdt_rogMax;
close RESULT;