#!/usr/athena/bin/perl

$smooth = 49;
@h = (90, 80, 80, 80, 80, 80, 80);
$lasthr=80;
$lastelev = 0;
$slope = 0;
$core=$lastcore=37;
while(<>){
	@d = split(",", $_);
	$lat = $d[5] + ($d[6]/60);
	$long = $d[7] + ($d[8]/60);
	$elev = $d[10];
	$hr = int($d[11]);
	$core = $d[12];

	$lat = $lastlat if ($lat < 1);
	$long = $lastlong if ($long < 1);
	$elev = $lastelev if ($elev < 0);
#	$slope = ((3*$slope)+$elev-$lastelev)/4;
	push(@c, $core);
	shift @c if ($#c > $smooth);

	push(@h, $hr);
	shift @h if ($#h > $smooth);

	push(@e, $elev);
	shift @e if ($#e > $smooth);

	push(@lt, $lat);
	shift @lt if ($#lt > $smooth);

	push(@ln, $long);
	shift @ln if ($#ln > $smooth);

	$lastlat = $lat;
	$lastlong = $long;
	$lastelev = $elev;
	$lasthr = $hr;

	$core = &avg(@c);
	$hr = &avg(@h);
	$elev = &avg(@e);
	push(@s, $elev);
	shift @s if ($#s > $smooth);
	$slope = $elev - $s[0];
	next if $elev !~ /\d/;
	next unless $lat;
	next unless $long;
	next unless $hr > 10;

	$xv = $long - $ln[0];
	$yv = $lat - $lt[0];

	$v = sqrt($xv*$xv + $yv*$yv);
	next unless $v < 1;

#	print "$lat $long $elev $hr $core $slope $v\n" if ($core=~/\d/);
	print "$hr $core $slope $v\n" if ($core=~/\d/);
}

sub avg {
    @x = @_;
    $ct=0;
    $tot=0;
    for $x (@x){
	$tot += $x;
	$ct++;
    }
    ($tot/$ct);
}
