Index: /branches/eam_branches/ipp-20100621/ppSub/src/ppSubFlagNeighbors.c
===================================================================
--- /branches/eam_branches/ipp-20100621/ppSub/src/ppSubFlagNeighbors.c	(revision 28857)
+++ /branches/eam_branches/ipp-20100621/ppSub/src/ppSubFlagNeighbors.c	(revision 28858)
@@ -23,4 +23,6 @@
 bool ppSubFlagNeighbors(pmConfig *config, pmFPAview *view, psArray *sources, bool matchRef) {
 
+    bool status;
+
     psAssert(config, "Require config");
     psAssert(view, "Require config");
@@ -28,6 +30,12 @@
 
     // Look up recipe values
-    psMetadata *recipe = psMetadataLookupMetadata(NULL, config->recipes, PPSUB_RECIPE); // should this be psphot?
+    psMetadata *recipe = psMetadataLookupMetadata(&status, config->recipes, PPSUB_RECIPE); // should this be psphot?
     psAssert(recipe, "We checked this earlier, so it should be here.");
+
+    float radius = psMetadataLookupF32(&status, recipe, "INPUT.MATCH.RADIUS");
+    psAssert (status, "cannot find INPUT.MATCH.RADIUS");
+
+    float minSN  = psMetadataLookupF32(&status, recipe, "INPUT.MATCH.MIN.SN");
+    psAssert (status, "cannot find INPUT.MATCH.MIN.SN");
 
     // Input sources
@@ -67,13 +75,10 @@
     }
 
-    // XXX put in recipe
-    float radius = 20.0; // radius in pixels
-
     // Match the sources to both inSources and refSources.  We want to find the closest source
     // in the positive source lists to each source in the diff source list.
     // XXX we need to either (a) apply S/N and other filters before we pass the in/ref sources
     // to this function, or (b) add those filters in the function
-    ppSubMatchSources (objects, inSources, radius);
-    ppSubMatchSources (objects, refSources, radius);
+    ppSubMatchSources (objects, inSources, radius, minSN);
+    ppSubMatchSources (objects, refSources, radius, minSN);
     
     // now mark or flag the sources with matches that meet some criterion
@@ -160,5 +165,5 @@
 # define NEXT1 { i++; continue; } 
 # define NEXT2 { j++; continue; } 
-bool ppSubMatchSources (psArray *objects, psArray *sources, float RADIUS) { 
+bool ppSubMatchSources (psArray *objects, psArray *sources, float RADIUS, float MIN_SN) { 
  
     float dx, dy; 
@@ -172,5 +177,5 @@
     // match sources to existing objects
 
-    psLogMsg ("psphot", PS_LOG_DETAIL, "attempt to match sources (%ld vs %ld)", sources->n, objects->n);
+    psLogMsg ("psphot", PS_LOG_DETAIL, "attempt to match sources (%ld vs %ld) (SN > %f)", sources->n, objects->n, MIN_SN);
 
     int nMatch = 0;
@@ -186,4 +191,6 @@
         if (!finite(src->peak->xf)) NEXT1; 
         if (!finite(src->peak->yf)) NEXT1; 
+        if (!finite(src->peak->SN)) NEXT1; 
+        if (src->peak->SN < MIN_SN) NEXT1; 
  
         if (!obj) NEXT2; 
@@ -198,8 +205,15 @@
 	int Imin = -1;
 	float Rmin = RADIUS2;
-        for (int I = i; (dx > -1.02*RADIUS) && (I < objects->n); I++) { 
+        for (int I = i; (dx < +1.02*RADIUS) && (I < sources->n); I++) { 
  
 	    src = sources->data[I]; 
 	    
+	    if (!src) NEXT1; 
+	    if (!src->peak) NEXT1; 
+	    if (!finite(src->peak->xf)) NEXT1; 
+	    if (!finite(src->peak->yf)) NEXT1; 
+	    if (!finite(src->peak->SN)) NEXT1; 
+	    if (src->peak->SN < MIN_SN) NEXT1; 
+
 	    dx = src->peak->xf - obj->x; 
             dy = src->peak->yf - obj->y; 
@@ -210,12 +224,16 @@
 	    Rmin = dr;
 	    Imin  = I;
+	    // fprintf (stderr, "j: %d, I: %d, Imin: %d, dr: %f, dx: %f, dy: %f\n", j, I, Imin, dr, dx, dy);
 	}
 
 	// no match, try next object
 	if (Imin == -1) {
+	    // fprintf (stderr, "*** missed j: %d, Imin: %d, obj x,y: %f, %f\n", j, Imin, obj->x, obj->y);
 	    j++;
 	    continue;
 	}
 	src = sources->data[Imin]; 
+
+	fprintf (stderr, "j: %d, Imin: %d, obj x,y: %f, %f  src x,y: %f, %f, SN: %f, ID: %d\n", j, Imin, obj->x, obj->y, src->peak->xf, src->peak->yf, src->peak->SN, src->id);
 
 	// add source to object
