Index: trunk/tools/bills/comparestackdetections
===================================================================
--- trunk/tools/bills/comparestackdetections	(revision 39014)
+++ trunk/tools/bills/comparestackdetections	(revision 39055)
@@ -7,5 +7,6 @@
 use Pod::Usage qw( pod2usage );
 use File::Basename;
-
+use File::Temp qw( tempdir  tempfile );
+use PS::IPP::Config 1.01 qw( :standard );
 
 my $joinType = '1and2';
@@ -21,14 +22,18 @@
 my $testdata;
 
-my ($verbose, $save_temps);
+my ($checkdeltas, $verbose, $save_temps);
+
+my $label = 'SAS.20141118.sc8';
 
 GetOptions(
+    "label=s"           =>  \$label,
     "testdata=s"        =>  \$testdata,
     "join=s"            =>  \$joinType,
+    "check"             =>  \$checkdeltas,
     "verbose|v"         =>  \$verbose,
     "save-temps"        =>  \$save_temps,
 ) or pod2usage( 2 );
 
-die "usage: $0 <stack batch file> <out> [--testdata <location of testdata directory]\n" unless (scalar @ARGV == 2);
+die "usage: $0 <stack batch file> <out> --label <skycal label> | --testdata <location of testdata directory\n" unless (scalar @ARGV == 2);
 
 $in = shift;
@@ -53,19 +58,20 @@
 
 my @cmfs;
-if ($testdata) {
-    for (my $i=1; $i<=5; $i++) {
-        my $stack_id = $stackIDs[$i];
-        my $cmf = 'none';
-        if ($stack_id > 0) {
+for (my $i=1; $i<=5; $i++) {
+    my $stack_id = $stackIDs[$i];
+    my $cmf = 'none';
+    if ($stack_id > 0) {
+        if ($testdata) {
             $cmf = sprintf "%s/stack.%02d.cmf", $testdata, $stack_id;
+        } else {
+            my $path_base = `staticskytool -skycalresult -label $label -stack_id $stack_id | grep path_base | awk '{print \$3}'`;
+            chomp $path_base;
+            die "failed to find skycal result for $stack_id $label\n" unless $path_base;
+            $cmf = $path_base . ".cmf";
         }
-        $cmfs[$i] = $cmf;
-        print "$filters[$i] $stack_id $cmf\n" if $verbose;
-    }
-} else {
-    # XXX: look up the cmf for this stack here
-    die "this program only works in test mode right now\n";
-}
-
+    }
+    $cmfs[$i] = $cmf;
+    print "$filters[$i] $stack_id $cmf\n" if $verbose;
+}
 
 my $outfile;
@@ -75,10 +81,13 @@
     next if $cmf eq 'none';
 
+    my $base = basename($cmf);
+
     my $filter = $filters[$i];
-    my $ippjoined = $filter . "_joined" . basename($cmf);
+    my $ippjoined = $filter . "_joined" . $base;
 
     $command = "joinstackcmf $cmf $ippjoined";
     $command .= " --ofmt fits";
     $command .= " --test" if $testdata;
+    $command .= " --verbose" if $verbose;
 
     runcommand($command);
@@ -86,5 +95,5 @@
     my $stackColumn = $filter . "stackImageID";
     my $idColumn    = $filter . "ippDetectID";
-    $outfile = $filter . "_matched" . basename($cmf);
+    $outfile = $filter . "_matched" . $base;
 
     $command = "$stilts tmatch2 in1=$input'#StackObjectAttributes' in2=$ippjoined matcher=2d out=$outfile ofmt=fits"
@@ -104,4 +113,5 @@
 }
 
+my @deltas;
 my $list = getStackColumns();
 my $cmd = "'";
@@ -121,4 +131,5 @@
     my $dcol = "d_$col";
     $cmd .= "addcol -after $col $dcol $equation;";
+    push @deltas, $dcol;
 }
 $cmd .= "'";
@@ -128,4 +139,50 @@
 runcommand($command);
 unlink($outfile) unless $save_temps;
+
+if ($checkdeltas) {
+    $save_temps = 1;
+    my ($manafile, $mananame) = tempfile( "/var/tmp/manacmds.XXXX", UNLINK => !$save_temps);
+    print "$mananame\n" if $save_temps;
+    print $manafile (<<HERE);
+\$limit = 1e-9
+output /dev/null
+macro checkdelta
+    \$col = \$1
+    \$thislimit = \$limit
+    subset good = \$col where \$col != -999
+    \$length = good[]
+    for i 0 10
+        subset d = good where abs(good) > \$thislimit
+        \$noutliers = d[]
+        if (\$noutliers > 0) 
+            echo \$col has \$noutliers out of \$length with difference > \$thislimit
+            \$thislimit = \$thislimit * 10
+        else
+            return
+        end
+    end
+end
+HERE
+
+    print $manafile "\nmacro dotests\n";
+    foreach my $col (@deltas) {
+        print $manafile "    checkdelta $col\n";
+    }
+    print $manafile "end\n";
+
+    print $manafile "datafile $out\n";
+    print $manafile "read -fits StackObjectAttributes";
+    foreach my $col (@deltas) {
+        print $manafile " $col";
+    }
+    print $manafile "\n";
+
+
+    print $manafile "output stdout\n";
+    print $manafile "dotests\n";
+    close $manafile;
+
+    system "echo input $mananame | mana";
+}
 
 exit 0;
@@ -188,5 +245,5 @@
 x_apFlux          AP_FLUX		*x_zpFactor/x_expTime
 x_apFluxErr       AP_FLUX_SIG		*x_zpFactor/x_expTime
-x_apFillFac       AP_NPIX               /(3.14159265359*pow(AP_MAG_RADIUS_x-0.5,2))
+x_apFillFac       AP_NPIX               /(3.14159265359*AP_MAG_RADIUS_x*AP_MAG_RADIUS_x)
 x_apRadius        AP_MAG_RADIUS         *abs(PLTSCALE_x)
 x_KronFlux        KRON_FLUX		*x_zpFactor/x_expTime
