#!/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 = 0; $i < $TriggerLine; ++$i)  
{
    @row = split(' ',$data[$i]);
    $aver+=$row[1];
}
$aver=$aver/$TriggerLine;

#Data integration
$integr=0;
$Time=0;
$BtMax=0;
$Bt=0;
$Total=0;
open FH, ">Btoroidal_dp.csv";
for ($i = 0; $i < $LineCount; ++$i)  
{
    @row = split(' ',$data[$i]);
    $integr+=$row[1]-$aver;
    $Bt=$integr*$Bt_calibration;
    $Time+=$DeltaT;
    $Total+=$Bt*$DeltaTs;    
    printf FH "%3.2f;%4.3f\n",$Time,$Bt;
    if ( abs($Bt) > abs($BtMax)  ) {$BtMax=$Bt};
}
close (FH);

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

open RESULT, ">MeanBt";
    printf RESULT "%4.3f", $Total/($LineCount*$DeltaTs);
close RESULT;
