#!/bin/perl

do "parameters.pl";


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

$EFieldTriggerLine  =`cat /srv/www/svoboda/golem/operation/currentshot/Tcd_aktual`;
$EFieldTriggerLine = ($EFieldTriggerLine/1000)/$DeltaT; # [ms]	
$TorMagFieTrigLine = $TororoidalMagneticFieldTrigger/$DeltaT;



#linear correction   
$m=0;
 for ($i = 1; $i <  $TorMagFieTrigLine-100; ++$i) 
{
    @row = split(' ',$data[$i]);
    $m = $m+ $row[3];
}
$averM= $m/($TorMagFieTrigLine-100);



$m1=0;
for ($i = $TorMagFieTrigLine+50; $i < $TorMagFieTrigLine+$EFieldTriggerLine; ++$i)  
{
    @row = split(' ',$data[$i]);
    $m1 = $m1+ $row[3];
}
#$averM1= $m1/($EFieldTriggerLine-50);


$m2=0;
for ($i = $LineCount-100; $i < $LineCount; ++$i)  
{
    @row = split(' ',$data[$i]);
     $m2 = $m2+ $row[3];
}
$averM2= $m2/100;

#y = $slope*x+$inter

#$slope = ($averM2-$averM1)/($LineCount+100/2-($TorMagFieTrigLine+50+$EFieldTriggerLine/2));
#$inter = -$slope*($TorMagFieTrigLine+50+$EFieldTriggerLine/2)+$averM1;




$Time=0;
open FH, ">PhotodHalfa_dp.csv";
for ($i = 0; $i < $TorMagFieTrigLine+100; ++$i)  
{
    @row = split(' ',$data[$i]);
    $Time+=$DeltaT;
    printf FH "%3.2f;%4.3f\n",$Time,0;
}
;
for ($i = $TorMagFieTrigLine+100; $i <$LineCount ; ++$i)  
{
    @row = split(' ',$data[$i]);
    $Time+=$DeltaT;
    #printf FH "%3.2f;%4.3f\n",$Time,($row[3]-($slope*$i+$inter));
	printf FH "%3.2f;%4.3f\n",$Time,-1*$row[4];				
}




close (FH);

