#!/bin/perl

#====podprogramy============

sub integrate (@)
{
  my $aver=0;
  my $integr=0;

  for ($i=0;$i<$TriggerLine;$i++)
  {
    $aver+=$_[$i];
  }
  $aver=$aver/$TriggerLine;

  for ($i=0;$i<$LineCount;$i++)
  {
    $integr+=($_[$i]-$aver);
    $_[$i]=$integr*$DeltaT;
  }
}

#===========================

#chomp ($Ublimit= <>);
$LineCount=4000;
$TriggerLine=350;
$DeltaT=1e-5;
$Divider=3.75;
$rogowsky_calibration=2000;
$rog_cal=2000.0;

# Open the data-file for reading.
open DATA, "Nidatap.lvm" or die "cannot open Nidatap.lvm";
@data=<DATA>;
close (DATA);

for ($i=0;$i<$LineCount;$i++) {
  @row=split('	',$data[$i]);
  $uloop[$i]=$row[1];
  $current_prim[$i]=$row[9];
}


integrate (@uloop);
for ($i=0;$i<$LineCount;$i++) {
  $uloop[$i]=$Divider*$uloop[$i];
  $current_prim[$i]=$rogowsky_calibration*$current_prim[$i];
}

open CIL1, ">hysteresis.txt";
for ($i=0;$i<$LineCount;$i++)
{
	print CIL1 $current_prim[$i]."\t".$uloop[$i]."\n";
}
close (CIL1);
