Index: trunk/ppStack/src/ppStackSources.c
===================================================================
--- trunk/ppStack/src/ppStackSources.c	(revision 19215)
+++ trunk/ppStack/src/ppStackSources.c	(revision 19226)
@@ -5,6 +5,7 @@
 #include "ppStack.h"
 
-#define SOURCE_MASK (PM_SOURCE_MODE_FAIL | PM_SOURCE_MODE_DEFECT | PM_SOURCE_MODE_SATURATED | \
-                     PM_SOURCE_MODE_CR_LIMIT) // Mask to apply to input sources
+#define SOURCE_MASK (PM_SOURCE_MODE_FAIL | PM_SOURCE_MODE_SATSTAR | PM_SOURCE_MODE_BLEND | \
+                     PM_SOURCE_MODE_BADPSF | PM_SOURCE_MODE_DEFECT | PM_SOURCE_MODE_SATURATED | \
+                     PM_SOURCE_MODE_CR_LIMIT | PM_SOURCE_MODE_EXT_LIMIT) // Mask to apply to input sources
 #define SOURCE_FAINTEST 50.0            // Faintest magnitude to consider
 #define SOURCES_MAX_LEAF 2              // Maximum number of points on a tree leaf
@@ -220,4 +221,13 @@
     long numIn = 0;                     // Number of sources in list
     long numOut = 0;                    // Number of sources outside list
+
+#ifdef TESTING
+    static int fileNum = 0;             // Number of file
+    psString filename = NULL;   // Name of file
+    psStringAppend(&filename, "source_match_%d.txt", fileNum++);
+    FILE *file = fopen(filename, "w"); // File to write
+    psFree(filename);
+    fprintf(file, "# x y mag1 mag2\n");
+#endif
 
     psVector *magDiff = psVectorAlloc(numSources, PS_TYPE_F32); // Magnitude differences
@@ -239,5 +249,10 @@
         float listMag = listSource->psfMag; // Magnitude of source in list
         float sourceMag = source->psfMag; // Magnitude of source
-        if (isfinite(listMag) && isfinite(sourceMag)) {
+        if (!(listSource->mode & SOURCE_MASK) && !(source->mode & SOURCE_MASK) &&
+            isfinite(listMag) && isfinite(sourceMag)) {
+#ifdef TESTING
+            fprintf(file, "%f %f %f %f %.4x %.4x\n", x->data.F32[i], y->data.F32[i],
+                    listSource->psfMag, source->psfMag, listSource->mode, source->mode);
+#endif
             magDiff->data.F32[numIn] = listSource->psfMag - source->psfMag;
             psArrayAdd(inside, 1, source);
@@ -246,4 +261,8 @@
     }
     magDiff->n = numIn;
+
+#ifdef TESTING
+    fclose(file);
+#endif
 
     psFree(coords);
@@ -256,5 +275,6 @@
     if (numIn > minOverlap) {
         // There's sufficient overlap --- calculate the magnitude difference
-        psStats *stats = psStatsAlloc(iter == 0 ? PS_STAT_SAMPLE_MEAN : PS_STAT_CLIPPED_MEAN);
+        psStats *stats = psStatsAlloc(iter == 0 ? (PS_STAT_SAMPLE_MEAN | PS_STAT_SAMPLE_STDEV) :
+                                      (PS_STAT_CLIPPED_MEAN | PS_STAT_CLIPPED_STDEV));
         stats->clipSigma = rej;
         stats->clipIter = iter;
@@ -265,6 +285,9 @@
             return -1;
         }
-        float meanDiff = stats->clippedMean; // Mean magnitude difference
-        psTrace("ppStack.sources", 7, "Magnitude difference is %f\n", meanDiff);
+        float meanDiff = iter == 0 ? stats->sampleMean : stats->clippedMean; // Mean magnitude difference
+        float stdev = iter == 0 ? stats->sampleStdev : stats->clippedStdev; // Standard deviation
+        long numStats = iter == 0 ? numIn : stats->clippedNvalues; // Number used for statistics
+        psLogMsg("ppStack.sources", 7, "Magnitude difference from %ld overlaps is %f +/- %f\n",
+                 numStats, meanDiff, stdev);
         psFree(stats);
 
@@ -437,18 +460,4 @@
     }
 
-#ifdef TESTING
-        FILE *srcFile = fopen("sources.txt", "w");
-        for (long i = 0; i < firstList->sources->n; i++) {
-            pmSource *source = firstList->sources->data[i];
-            if (source->mode & SOURCE_MASK) {
-                continue;
-            }
-            float x, y;
-            coordsFromSource(&x, &y, source);
-            fprintf(srcFile, "%f\t%f\t%f\n", x, y, source->psfMag);
-        }
-        fclose(srcFile);
-#endif
-
     return psMemIncrRefCounter(firstList->sources);
 }
