InterFiles.pl Documentation

This lists the script interFiles.pl, used to parse and resample the various satellite files.

It reads in 4 satellites' files, each with the fields time index, x, y, z columns, and finds the min and max time of the files, and interpolates all data of those files into the directories 12 and 34, sub-directoriees Edot, E, and B, each with files time, 1x, 1y, 1z, 2x, 2y, 2z, 3x, 3y, 3z, 4x, 4y, 4z.

We put zeroes in for missing data.

In the case of the e-fields, we copied satellite 1's data to the satellite 2 slot, so expect large cross-sembalnaces between those two channels.

#!/perl 
# This is a one-off program. Be kind.

use FileHandle;

print "We interpolate the given files. \n";

# Get the file names for up to 4 files.
$nFiles = $#ARGV;
mkdir "tmp";
if ( ($nFiles+1) >= 1 ) {
	$f1 = $ARGV[0];
}
if ( ($nFiles+1) >= 2 ) {
	$f2 = $ARGV[1];
}
if ( ($nFiles+1) >= 3 ) {
	$f3 = $ARGV[2];
}
if ( ($nFiles+1) >= 4 ) {
	$f4 = $ARGV[3];
}

open $fi1, "<$f1" || die "Cannot open file $f1 for input.";
open $fi2, "<$f2" || die "Cannot open file $f2 for input.";
open $fi3, "<$f3" || die "Cannot open file $f3 for input.";
open $fi4, "<$f4" || die "Cannot open file $f4 for input.";

open $foSr, ">tmp/SampleRate" || die "Cannot open file tmp/SampleRate for input.";
open $foMax, ">tmp/maxTime" || die "Cannot open file tmp/maxTime for input.";
open $foMin, ">tmp/minTime" || die "Cannot open file tmp/minTime for input.";

open $foT, ">tmp/time" || die "Cannot open file tmp/time for output.";
open $fox1, ">tmp/x1" || die "Cannot open file tmp/x1 for output.";
open $fox2, ">tmp/x2" || die "Cannot open file tmp/x2 for output.";
open $fox3, ">tmp/x3" || die "Cannot open file tmp/x3 for output.";
open $fox4, ">tmp/x4" || die "Cannot open file tmp/x4 for output.";
open $foy1, ">tmp/y1" || die "Cannot open file tmp/y1 for output.";
open $foy2, ">tmp/y2" || die "Cannot open file tmp/y2 for output.";
open $foy3, ">tmp/y3" || die "Cannot open file tmp/y3 for output.";
open $foy4, ">tmp/y4" || die "Cannot open file tmp/y4 for output.";
open $foz1, ">tmp/z1" || die "Cannot open file tmp/z1 for output.";
open $foz2, ">tmp/z2" || die "Cannot open file tmp/z2 for output.";
open $foz3, ">tmp/z3" || die "Cannot open file tmp/z3 for output.";
open $foz4, ">tmp/z4" || die "Cannot open file tmp/z4 for output.";

# Ok, start reading in the stuff. 
my $maxOfMins = 0;
my $minOfMaxes = 99999999999;
my $m1x = 0;
my $m1n = 99999999999;
my $m2x = 0;
my $m2n = 99999999999;
my $m3x = 0;
my $m3n = 99999999999;
my $m4x = 0;
my $m4n = 99999999999;
my $sr = 0;
my $c = 0;

while ( <$fi1> ) {
	chomp;
	($t, $x, $y, $z ) = split();
	$it = int($t);
	$x1[$it] = $x;
	$y1[$it] = $y;
	$z1[$it] = $z;
	$c++;
	if ( defined $t && $m1n > $t ) { $m1n = $t; }
	if ( defined $t && $m1x < $t ) { $m1x = $t; }
}
# We want the min of the max's and the max of the min's 
if ( $minOfMaxes > $m1x ) { $minOfMaxes = $m1x; }
if ( $maxOfMins < $m1n ) { $maxOfMins = $m1n; }
$sr = ($m1x-$m1n)/($c-1);

while ( <$fi2> ) {
	chomp;
	($t, $x, $y, $z ) = split();
	$it = int($t);
	$x2[$it] = $x;
	$y2[$it] = $y;
	$z2[$it] = $z;
	if ( defined $t && $m2n > $t ) { $m2n = $t; }
	if ( defined $t && $m2x < $t ) { $m2x = $t; }
}
if ( $minOfMaxes > $m2x ) { $minOfMaxes = $m2x; }
if ( $maxOfMins < $m2n ) { $maxOfMins = $m2n; }

while ( <$fi3> ) {
	chomp;
	($t, $x, $y, $z ) = split();
	$it = int($t);
	$x3[$it] = $x;
	$y3[$it] = $y;
	$z3[$it] = $z;
	if ( defined $t && $m3n > $t ) { $m3n = $t; }
	if ( defined $t && $m3x < $t ) { $m3x = $t; }
}
if ( $minOfMaxes > $m3x ) { $minOfMaxes = $m3x; }
if ( $maxOfMins < $m3n ) { $maxOfMins = $m3n; }

while ( <$fi4> ) {
	chomp;
	($t, $x, $y, $z ) = split();
	$it = int($t);
	$x4[$it] = $x;
	$y4[$it] = $y;
	$z4[$it] = $z;
	if ( defined $t && $m4n > $t ) { $m4n = $t; }
	if ( defined $t && $m4x < $t ) { $m4x = $t; }
}
if ( $minOfMaxes > $m4x ) { $minOfMaxes = $m4x; }
if ( $maxOfMins < $m4n ) { $maxOfMins = $m4n; }

print "We interpolate between ${minOfMaxes} and ${maxOfMins}. The sample rate is $sr.\n";
# Set up the interpolation array. 
$npoints = int($m1x-$m1n);
my $last = $m1n;
# Fill in all integer keys of the arry x1. 
for my $i ( int($m1n)..int($m1x) ) {
	if ( ! defined $x1[$i] ) {
		$x1[$i] = $x1[$last];
		$y1[$i] = $y1[$last];
		$z1[$i] = $z1[$last];
	} else {
		$last = $i;
	}
}

for my $i ( int($m2n)..int($m2x) ) {
	if ( ! defined $x2[$i] ) {
		$x2[$i] = $x2[$last];
		$y2[$i] = $y2[$last];
		$z2[$i] = $z2[$last];
	} else {
		$last = $i;
	}
}

for my $i ( int($m3n)..int($m3x) ) {
	if ( ! defined $x3[$i] ) {
		$x3[$i] = $x3[$last];
		$y3[$i] = $y3[$last];
		$z3[$i] = $z3[$last];
	} else {
		$last = $i;
	}
}

for my $i ( int($m4n)..int($m4x) ) {
	if ( ! defined $x4[$i] ) {
		$x4[$i] = $x4[$last];
		$y4[$i] = $y4[$last];
		$z4[$i] = $z4[$last];
	} else {
		$last = $i;
	}
}

# Write the data.
$sample = int( $maxOfMins);
while ( $sample < $minOfMaxes ) {
  print $foT "$sample\n";

	print $fox1 "$x1[$sample]\n";
	print $fox2 "$x2[$sample]\n";
	print $fox3 "$x3[$sample]\n";
	print $fox4 "$x4[$sample]\n";

	print $foy1 "$y1[$sample]\n";
	print $foy2 "$y2[$sample]\n";
	print $foy3 "$y3[$sample]\n";
	print $foy4 "$y4[$sample]\n";

	print $foz1 "$z1[$sample]\n";
	print $foz2 "$z2[$sample]\n";
	print $foz3 "$z3[$sample]\n";
	print $foz4 "$z4[$sample]\n";

	$sample += $sr;
}

print $foSr "$sr\n";
print $foMin "$maxOfMins\n";
print $foMax "$minOfMaxes\n";

close $fi1 || die "Cannot close input file $f1.";
close $fi2 || die "Cannot close input file $f2.";
close $fi3 || die "Cannot close input file $f3.";
close $fi4 || die "Cannot close input file $f4.";

close $foSr || die "Cannot close file tmp/SampleRate.";
close $foMax || die "Cannot close file tmp/maxTime.";
close $foMin || die "Cannot close file tmp/minTime.";

close $foT || die "Cannot close file tmp/time .";
close $fox1 || die "Cannot close file tmp/x1.";
close $fox2 || die "Cannot close file tmp/x2.";
close $fox3 || die "Cannot close file tmp/x3.";
close $fox4 || die "Cannot close file tmp/x4.";
close $foy1 || die "Cannot close file tmp/y1.";
close $foy2 || die "Cannot close file tmp/y2.";
close $foy3 || die "Cannot close file tmp/y3.";
close $foy4 || die "Cannot close file tmp/y4.";
close $foz1 || die "Cannot close file tmp/z1.";
close $foz2 || die "Cannot close file tmp/z2.";
close $foz3 || die "Cannot close file tmp/z3.";
close $foz4 || die "Cannot close file tmp/z4.";

exit;

Bill Kamp