#!/usr/bin/perl
#
# testOffsets.pl
# Concerned that different offsets are producing different sensitivity to finding CRs.
# For example, half pixel offsets in each direction seem to produce lots of false positives,
# but tiny offsets produce few CRs found.  Need statistics.

my @offsets = ();
my @numcr = ();
my $badNum = 0;
for (my $iter = 0; $iter < 500; $iter++) {
    system "./testParams.pl";
    my $bad = 0;
    for (my $i = 0; $i < 4; $i++) {

	# Number of CRs found
	my $wc = `wc -l test_$i.fits.cr`;
	my ($number) = ($wc =~ /^\s*(\d+)/);
	print "Found $number CRs in image $i\n";
	if ($number > 2000) {
	    $bad = 1;
	}

	# Offset
	my $mapFile;
	open $mapFile, "test_$i.fits.map";
	my @map = <$mapFile>;
	print @map . "\n";
	close $mapFile;
	my ($x) = ($map[1] =~ /^(\S+)/);
	my ($y) = ($map[2] =~ /^(\S+)/);
	my $offset = sqrt($x**2 + $y**2);
	print "Offset for image $i is $offset\n";

	push @offsets, $offset;
	push @numcr, $number;
    }

    if ($bad) {
	$badNum++;
	system "mkdir bad_$badNum";
	system "cp -f test_* testout.fits* bad_$badNum";
    }
}

my $cr;
open $cr, "> cr.dat";
for (my $i = 0; $i <= $#offsets; $i++) {
    print $cr "$offsets[$i] $numcr[$i]\n";
}
close $cr;
system "sort -k 1 -n cr.dat > cr.dat.sort";

__END__
