Index: trunk/tools/bills/comparestackdetections
===================================================================
--- trunk/tools/bills/comparestackdetections	(revision 39011)
+++ trunk/tools/bills/comparestackdetections	(revision 39012)
@@ -68,6 +68,7 @@
 }
 
-# 
-my @joined;
+
+my $outfile;
+my $input = $in;
 for (my $i=1; $i<=5; $i++) {
     my $cmf = $cmfs[$i];
@@ -84,29 +85,48 @@
 
     my $stackColumn = $filter . "stackImageID";
-    my $idColumn = $filter . "ippDetectID";
-    my $outfile = $filter . "_matched" . basename($cmf);
-    $command = "$stilts tmatch2 in1=$in'#StackObjectAttributes' in2=$ippjoined matcher=2d out=$outfile ofmt=fits"
+    my $idColumn    = $filter . "ippDetectID";
+    $outfile = $filter . "_matched" . basename($cmf);
+
+    $command = "$stilts tmatch2 in1=$input'#StackObjectAttributes' in2=$ippjoined matcher=2d out=$outfile ofmt=fits"
         . " values1='$stackColumn $idColumn' values2='STACK_ID IPP_IDET' params=.01 join=$joinType"
         . " fixcols=all suffix1='' suffix2=_$filter ocmd='tablename StackObjectAttributes'";
-    $in = $outfile;
     runcommand($command);
-}
+
+    if (!$save_temps) {
+        unlink $ippjoined;
+        if ($input ne $in) {
+            unlink $input;
+        }
+    }
+    # input for next round of matches is the output from this one
+    $input = $outfile;
+}
+
+my $list = getStackColumns();
+my $cmd = "'";
+# add x_zpFactor columns
+for (my $i = 1; $i<=5; $i++) {
+    next if $cmfs[$i] eq 'none';
+    my $filter = $filters[$i];
+    my $zp = $filter . "zp";
+    $cmd .= "addcol -after $zp ${filter}zpFactor 3630.78*pow(10,-0.4*$zp);";
+}
+foreach my $line (@$list) {
+    my ($col, $ipp, $conversion) = split ' ', $line;
+    next if !$ipp;
+    $conversion = "" unless $conversion;
+    my $equation = "$ipp$conversion";
+    $equation = "$col-$equation";
+    my $dcol = "d_$col";
+    $cmd .= "addcol -after $col $dcol $equation;";
+}
+$cmd .= "'";
+
+$command = "$stilts tpipe in=$outfile out=$out cmd=$cmd";
+
+runcommand($command);
+unlink($outfile) unless $save_temps;
 
 exit 0;
-
-my $matched = "m." . basename($out);
-
-# $commmand = "$stilts tmatch2 fixcols=dups suffix1=
-my $ippjoined;
-runcommand("matchIppToPsps $in $ippjoined $matched");
-
-if (!$save_temps) {
-    unlink $ippjoined;
-}
-
-# runcommand("adddeltacolumns $matched $out");
-
-exit 0;
-
 
 sub runcommand {
@@ -119,2 +139,103 @@
     }
 }
+
+# list of columns from stackObjectAttributes table
+sub getStackColumns {
+    my $filters = shift;
+    my $cmfs = shift;
+
+    # format of the list is "psps column"   "ipp column"    "equation to convert ipp value to psps value"
+    # If equation is blank the psps column is the same as the ipp column
+    # columns which begin in x_ have a different value for each of the 5 filters
+    # similarly ipp columns that end in _x have the name col_$filter
+
+    my $list = <<END_OF_LIST;
+# Note x_zpFactor = 3630.78*pow(10, -0.4*x_zp)
+
+objID
+uniquePspsSTid
+ippObjID
+randomStackObjID
+stackDetectRowID
+primaryDetection
+bestDetection
+
+x_ippDetectID     IPP_IDET
+x_stackDetectID
+x_stackImageID    STACK_ID
+x_xPos            X_PSF
+x_yPos            Y_PSF
+x_xPosErr         X_PSF_SIG
+x_yPosErr         Y_PSF_SIG
+x_psfMajorFWHM    PSF_FWHM_MAJ            *abs(PLTSCALE_x)
+x_psfMinorFWHM    PSF_FWHM_MIN            *abs(PLTSCALE_x)
+x_psfTheta        PSF_THETA
+x_psfCore         PSF_CORE
+x_psfLikelihood                                                 # actually psfLikelihood(EXT_NSIGMA). We assume the formula is ok.
+x_psfQf           PSF_QF
+x_psfQfPerfect    PSF_QF_PERFECT
+x_psfChiSq        PSF_CHISQ
+x_momentXX        MOMENTS_XX		*PLTSCALE_x*PLTSCALE_x
+x_momentXY        MOMENTS_XY		*PLTSCALE_x*PLTSCALE_x
+x_momentYY        MOMENTS_YY		*PLTSCALE_x*PLTSCALE_x
+x_momentR1        MOMENTS_R1            *abs(PLTSCALE_x)
+x_momentRH        MOMENTS_RH            *sqrt(abs(PLTSCALE_x))
+
+x_psfFlux         PSF_INST_FLUX 	*x_zpFactor/x_expTime
+x_psfFluxErr      PSF_INST_FLUX_SIG	*x_zpFactor/x_expTime
+
+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_apRadius        AP_MAG_RADIUS         *abs(PLTSCALE_x)
+x_KronFlux        KRON_FLUX		*x_zpFactor/x_expTime
+x_KronFluxErr     KRON_FLUX_ERR		*x_zpFactor/x_expTime
+x_KronRad         MOMENTS_R1            *2.5*abs(PLTSCALE_x)
+x_expTime
+x_ExtNSigma       EXT_NSIGMA
+x_sky             SKY                   /PLTSCALE_x/PLTSCALE_x*x_zpFactor/x_expTime
+x_skyErr          SKY_SIGMA             /PLTSCALE_x/PLTSCALE_x*x_zpFactor/x_expTime
+x_zp
+x_PlateScale      PLTSCALE
+END_OF_LIST
+
+    my @lines = split "\n", $list;
+
+    my @output;
+    foreach my $l (@lines) {
+        # skip comment lines and empty lines
+        next if $l =~ '^#';
+        next if !$l;
+        # strip off trailing comment
+        my ($data, $comment) = split '#', $l;
+        my ($column, $ippColumn, $equation) = split " ", $data;
+
+        $ippColumn = "" unless $ippColumn;
+        $equation  = "" unless $equation;
+        if ($column =~ '^x_') {
+            for (my $i=1; $i<=5; $i++) {
+                my $c = $cmfs[$i];
+                next if $c eq 'none';
+                my $f = $filters[$i];
+                my $newCol = $f . substr($column, 2);
+                my $newIpp = "";
+                if ($ippColumn) {
+                    $newIpp = $ippColumn . "_$f";
+                }
+                my $newEquation = $equation;
+                $newEquation =~ s/_x/_$f/g;
+                $newEquation =~ s/x_/$f/g;
+                my $newline = sprintf "%s\t%s\t%s", $newCol, $newIpp, $newEquation;
+                # print "$newline\n";
+                push @output, $newline;
+            }
+        } else {
+            # print "$l\n";
+            push @output, $l;
+        }
+        
+    }
+
+    return \@output;
+}
+
