Index: trunk/ppSub/src/ppSubFlagNeighbors.c
===================================================================
--- trunk/ppSub/src/ppSubFlagNeighbors.c	(revision 32807)
+++ trunk/ppSub/src/ppSubFlagNeighbors.c	(revision 32822)
@@ -18,8 +18,11 @@
 #include "ppSub.h"
 
+# define MIN_ERROR 0.02 /* padding for self-match flux test */
+# define MAX_OFFSET 5.0 /* delta mag greater than this is not a self match */
+
 // match the diff sources to the detections from the positive or negative input images
 // matchRef : false if this is the standard diff image, true if this is the inverse (in-ref vs ref-in)
 // this is needed to choose which value is positive and which is negative relative to the difference
-bool ppSubFlagNeighbors(pmConfig *config, pmFPAview *view, psArray *sources, bool matchRef) {
+bool ppSubFlagNeighbors(pmConfig *config, pmFPAview *view, psArray *sources, bool subInverse) {
 
     bool status;
@@ -106,10 +109,24 @@
 	      psAssert ((sourceM1->imageID == 1) || (sourceM1->imageID == 2), "error in pmPhotObj construction (second entry is not from 1 or 2");
 
+	      // which image gives the match, the positive or negative one?
+	      bool positive = false;
+	      if (subInverse) {
+		  positive = (sourceM1->imageID == 2);
+	      } else {
+		  positive = (sourceM1->imageID == 1);
+	      }
+
+	      // check if source and sourceM1 are likely to be the same flux (ie, isolated detection in one image)
+	      if (positive && isfinite(sourceM1->psfMag) && isfinite(source->psfMag)) {
+		  float dMag = sourceM1->psfMag - source->psfMag;
+		  float nSig = fabs(dMag) / hypot(MIN_ERROR, source->psfMagErr);
+		  if (nSig < MAX_OFFSET) {
+		      source->mode2 |= PM_SOURCE_MODE2_DIFF_SELF_MATCH;
+		  }
+	      }
+
 	      // only one match.  set a flag?
 	      source->mode2 |= PM_SOURCE_MODE2_DIFF_WITH_SINGLE;
 	      if (source->diffStats) {
-		  // which is match need an xor here...
-		  bool positive = !matchRef && (sourceM1->imageID == 1);
-		  positive |= matchRef && (sourceM1->imageID == 2);
 		  float SN1 = isfinite(sourceM1->psfMagErr) ? 1.0 / sourceM1->psfMagErr : NAN;
 		  if (positive) {
@@ -133,9 +150,26 @@
 	      psAssert (sourceM1->imageID != sourceM2->imageID, "error in pmPhotObj construction (case 3.3)");
 
+	      // we don't know which matched source (sourceM1 or sourceM2) is the positive detection,
+	      // and the answer depends on both the ID and the subtraction order (in-ref vs ref-in)
+	      // positive is true if sourceM1 is the positive detection
+	      bool positive = false;
+	      if (subInverse) {
+		  positive = (sourceM1->imageID == 2);
+	      } else {
+		  positive = (sourceM1->imageID == 1);
+	      }
+
+	      // check for self-detection
+	      float magPSF1 = positive ? sourceM1->psfMag : sourceM2->psfMag;
+	      if (isfinite(magPSF1) && isfinite(source->psfMag)) {
+		  float dMag = magPSF1 - source->psfMag;
+		  float nSig = fabs(dMag) / hypot(MIN_ERROR, source->psfMagErr);
+		  if (nSig < MAX_OFFSET) {
+		      source->mode2 |= PM_SOURCE_MODE2_DIFF_SELF_MATCH;
+		  }
+	      }
+
 	      source->mode2 |= PM_SOURCE_MODE2_DIFF_WITH_DOUBLE;
 	      if (source->diffStats) {
-		  // which is match need an xor here...
-		  bool positive = !matchRef && (sourceM1->imageID == 1);
-		  positive |= matchRef && (sourceM1->imageID == 2);
 		  float SN1 = isfinite(sourceM1->psfMagErr) ? 1.0 / sourceM1->psfMagErr : NAN;
 		  float SN2 = isfinite(sourceM2->psfMagErr) ? 1.0 / sourceM2->psfMagErr : NAN;
Index: trunk/ppSub/src/ppSubReadoutPhotometry.c
===================================================================
--- trunk/ppSub/src/ppSubReadoutPhotometry.c	(revision 32807)
+++ trunk/ppSub/src/ppSubReadoutPhotometry.c	(revision 32822)
@@ -92,6 +92,6 @@
     // detections in the wings (or cores) of bright(er) stars found in both images.
     // flag detections based on their distance from the bright(er) input sources.
-    bool matchRef = !strcasecmp(name, "PPSUB.INVERSE");
-    ppSubFlagNeighbors (config, view, sources, matchRef);
+    bool subInverse = !strcasecmp(name, "PPSUB.INVERSE");
+    ppSubFlagNeighbors (config, view, sources, subInverse);
 
     if (data->stats) {
