#!/usr/local/bin/perl

open STARS, "test.stars";
my @xSky = ();
my @ySky = ();
while (<STARS>) {
    my ($x, $y, $flux) = split;
    push @xSky, $x;
    push @ySky, $y;
}

for (my $i = 0; $i < 4; $i++) {
    open MAP, "test_$i.fits.map";
    my $line = <MAP>;
    ($A0, $A1) = split /\s+/, $line;
    my $line = <MAP>;
    ($B00, $B01) = split /\s+/, $line;
    my $line = <MAP>;
    ($B10, $B11) = split /\s+/, $line;
    close MAP;

    open STARS, "test_$i.stars";
    my @diff = ();
    my $num = 0;
    while (<STARS>) {
	my ($x, $y, $flux) = split;

	$xPrime = $A0 + $B00*$x + $B10*$y;
	$yPrime = $A1 + $B01*$x + $B11*$y;

	push @xdiff, $xPrime - $xSky[$num];
	push @ydiff, $yPrime - $ySky[$num];

	$num++;
    }

    print "File: $i, Difference: " . mean(\@xdiff) . ", " . mean(\@ydiff) . "\n";
}

### ENDS


# Return the mean
sub mean
{
    my ($arrayRef) = @_;

    my @array = @$arrayRef;
    my $mean = 0;
    foreach my $value (@array) {
	$mean += $value;
    }
    return $mean / (scalar @array);
}
