#!bin/perl

do "parameters.pl";

# Open the file for reading.
open DATA1, "Iplasma_dp.csv" or die "can't open file $!";
my @data1 = <DATA1>;
close (DATA1);

# Open the file for reading.
open DATA2, "disR.txt" or die "can't open file $!";
my @data2 = <DATA2>;
close (DATA2);

# Open the file for reading.
open DATA3, "disZ.txt" or die "can't open file $!";
my @data3 = <DATA3>;
close (DATA3);

open DATA4, "PlasmaStartRow" or die "can't open file $!";
my $PlasmaStartRow = <DATA4>;
close (DATA4);

open DATA5, "PlasmaEndRow" or die "can't open file $!";
my $PlasmaEndRow = <DATA5>;
close (DATA5);

for ($i=0;$i<$LineCount;++$i)
{
	@row1=split(';',$data1[$i]);
	@row2=split(' ',$data2[$i]);
	@row3=split(' ',$data3[$i]);
	$Time[$i]=$row1[0];
	$GreenwaldDensityTimeMirnov[$i]= abs(($row1[1]/100000)/(3.14159265*($LimiterPosition-(($row2[3]/1000)**2+($row3[3]/1000)**2)**0.5)**2));
}

$maxGreenwaldDensityMirnov=0;
for ($i=$PlasmaStartRow+200;$i<$PlasmaEndRow-200;++$i)
{
	$maxGreenwaldDensityMirnov= $maxGreenwaldDensityMirnov < $GreenwaldDensityTimeMirnov[$i] ? $GreenwaldDensityTimeMirnov[$i] : $maxGreenwaldDensityMirnov;
}

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

open CIL2, ">maxGreenwaldDensityMirnov";
print CIL2 "$maxGreenwaldDensityMirnov";
close (CIL2);
