Index: trunk/ippconfig/recipes/psphot.config
===================================================================
--- trunk/ippconfig/recipes/psphot.config	(revision 34787)
+++ trunk/ippconfig/recipes/psphot.config	(revision 34788)
@@ -384,11 +384,11 @@
 # Note: if an input has a filter not in this list it will be processed after all of the others with MATCH.ALL = FALSE
 PSPHOT.STACK.MATCH.FILTERS   METADATA
-    TYPE        PSPHOT_STACK.MATCH.FILTER FILTER.ID ORDER MATCH.ALL
-    gband       PSPHOT_STACK.MATCH.FILTER   g        4       FALSE
-    rband       PSPHOT_STACK.MATCH.FILTER   r        2       FALSE
-    iband       PSPHOT_STACK.MATCH.FILTER   i        1       FALSE
-    zband       PSPHOT_STACK.MATCH.FILTER   z        3       FALSE
-    yband       PSPHOT_STACK.MATCH.FILTER   y        5       TRUE
-    wband       PSPHOT_STACK.MATCH.FILTER   w        0       FALSE
+    TYPE        PSPHOT_STACK.MATCH.FILTER FILTER.ID ORDER MATCH.ALL Y.RATIO.MAX
+    gband       PSPHOT_STACK.MATCH.FILTER   g        4       FALSE      0
+    rband       PSPHOT_STACK.MATCH.FILTER   r        2       FALSE      0.6
+    iband       PSPHOT_STACK.MATCH.FILTER   i        1       FALSE      0.4
+    zband       PSPHOT_STACK.MATCH.FILTER   z        3       FALSE      0
+    yband       PSPHOT_STACK.MATCH.FILTER   y        5       TRUE       0
+    wband       PSPHOT_STACK.MATCH.FILTER   w        0       FALSE      0
 END
 
Index: trunk/psphot/src/psphotSourceMatch.c
===================================================================
--- trunk/psphot/src/psphotSourceMatch.c	(revision 34787)
+++ trunk/psphot/src/psphotSourceMatch.c	(revision 34788)
@@ -7,4 +7,6 @@
     int     order;
     bool    matchAll;
+    long    nSources;
+    float   yRatioMax;
     char    filterID[MATCH_INFO_FILTER_STR_LEN];
 } psphotStackMatchInfo;
@@ -218,7 +220,48 @@
 	pmDetections *detections = psMetadataLookupPtr (&status, readout->analysis, "PSPHOT.DETECTIONS");
 	psAssert (detections, "missing detections?");
+        psAssert (detections->allSources, "all sources not defined?");
 
 	detArrays->data[i] = psMemIncrRefCounter(detections);
 	readouts->data[i] = psMemIncrRefCounter(readout);
+    }
+
+    // We are having a problem with large number of false y band detections causing large numbers of
+    // matched detections to be created.
+    // In matchInfo recipe each filter can have a value for Y.RATIO.MAX. For each filter with a non-zero value
+    // we compare the ratio of the number of y detections to the number of detections in that filter to Y.RATIO.MAX.
+    // If the ratio in all specified filters is above its limit, we disable matchAll for y band.
+
+    int iy = -1;
+    // now we are looping over matchInfo
+    for (int i = 0; i < nImages; i++) {
+        // Remember which input is y
+        if (!strcmp(matchInfo[i].filterID, "y")) {
+            iy = i;
+        }
+	pmDetections *detections = detArrays->data[matchInfo[i].inputNum];
+        matchInfo[i].nSources = detections->allSources->n;
+    }
+
+    if (iy >= 0) {
+        int nCut = 0;
+        int nTry = 0;
+        for (int i = 0; i < nImages; i++) {
+            if (i == iy) continue;
+            if (matchInfo[i].yRatioMax != 0.0 && matchInfo[i].nSources > 0) {
+                ++nTry;
+                float ratio = (float) matchInfo[iy].nSources / (float) matchInfo[i].nSources;
+                psLogMsg ("psphot", PS_LOG_DETAIL, "nSrc_y / nSrc_%s: %6.3f max: %6.3f", matchInfo[i].filterID, ratio,
+                    matchInfo[i].yRatioMax);
+                if (ratio > matchInfo[i].yRatioMax) {
+                    // This one is above it's threshold
+                    ++nCut;
+                }
+            }
+        }
+        if (nTry > 0 && nCut == nTry) {
+            // All filters with a cut were above the threshold. Disable matchAll for y band
+            psLogMsg ("psphot", PS_LOG_INFO, "Clearing y band matchAll because y ratio was too high in %d filters", nTry);
+            matchInfo[iy].matchAll = false;
+        }
     }
 
@@ -516,4 +559,7 @@
             bool matchAll = (strcasecmp("true", matchAllStr) == 0);
 
+            psString yRatioStr = psMetadataLookupStr (&status, item->data.md, "Y.RATIO.MAX");
+            psF32 yRatioMax = atof(yRatioStr);
+
             // look for this filter in the inputs
             for (int i = 0; i < num; i++) {
@@ -524,4 +570,6 @@
                     matchInfo[i].order = order;
                     matchInfo[i].matchAll = matchAll;
+                    matchInfo[i].yRatioMax = yRatioMax;
+                    matchInfo[i].nSources = 0;
                     psLogMsg ("psphot", PS_LOG_DETAIL, "input: %d %s match order: %d match all: %d\n",
                                                                i, thisFilter, order, matchAll);
