Index: /tags/ipp-20121026/ippconfig/recipes/psphot.config
===================================================================
--- /tags/ipp-20121026/ippconfig/recipes/psphot.config	(revision 34796)
+++ /tags/ipp-20121026/ippconfig/recipes/psphot.config	(revision 34797)
@@ -345,4 +345,6 @@
 PSPHOT.EXT.NSIGMA.LIMIT             F32   3.0  # sources with extNsigma greater that this get tagged as likely extended sources
 PSPHOT.EXT.NSIGMA.MOMENTS           F32   3.0  # sources with extNsigma greater that this get tagged as likely extended sources
+PSPHOT.EXT.DIFF.ALTERNATE	    BOOL  FALSE           # use alternate extended source calculation; designed for diff trails
+PSPHOT.EXT.DIFF.ALTERNATE.THRESH    F32	  1.5		  # threshold for alternate extended source calculations
 PSPHOT.CR.GROW                      S32   1               # Number of pixels to grow CR mask
 PSPHOT.CR.NSIGMA.SOFTEN             F32   0.0025          # Softening parameter for weights
@@ -382,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: /tags/ipp-20121026/psphot/src/psphotOutput.c
===================================================================
--- /tags/ipp-20121026/psphot/src/psphotOutput.c	(revision 34796)
+++ /tags/ipp-20121026/psphot/src/psphotOutput.c	(revision 34797)
@@ -201,8 +201,13 @@
     int nCR  = 0;
     int nEXT = 0;
+    int nForced = 0;
+    int nDetections = sources != NULL ? sources->n : 0;
 
     // count the number of sources which will be written and other sub-types
     for (int i = 0; (sources != NULL) && (i < sources->n); i++) {
         pmSource *source = (pmSource *) sources->data[i];
+        if (source->mode2 & PM_SOURCE_MODE2_MATCHED) {
+            nForced ++;
+        }
         pmModel *model = pmSourceGetModel (NULL, source);
         if (model == NULL)
@@ -217,4 +222,6 @@
     }
 
+    // XXX: This loop cauess nSrc to be twice as large as it should be. This is kept
+    // for compatability
     for (int i = 0; (sources != NULL) && (i < sources->n); i++) {
         pmSource *source = (pmSource *) sources->data[i];
@@ -226,6 +233,8 @@
 
     psMetadataAddS32 (header, PS_LIST_TAIL, "NSTARS",   PS_META_REPLACE, "Number of sources", nSrc);
+    psMetadataAddS32 (header, PS_LIST_TAIL, "NDET",     PS_META_REPLACE, "Number of detections", nDetections);
     psMetadataAddS32 (header, PS_LIST_TAIL, "NDET_EXT", PS_META_REPLACE, "Number of extended sources", nEXT);
     psMetadataAddS32 (header, PS_LIST_TAIL, "NDET_CR",  PS_META_REPLACE, "Number of cosmic rays", nCR);
+    psMetadataAddS32 (header, PS_LIST_TAIL, "NDET_FRC", PS_META_REPLACE, "Number of Forced detections", nForced);
     return true;
 }
@@ -306,6 +315,8 @@
     psMetadataItemSupplement (&status, header, analysis, "NSTARS");
 
+    psMetadataItemSupplement (&status, header, analysis, "NDET");
     psMetadataItemSupplement (&status, header, analysis, "NDET_EXT");
     psMetadataItemSupplement (&status, header, analysis, "NDET_CR");
+    psMetadataItemSupplement (&status, header, analysis, "NDET_FRC");
 
     psMetadataItemSupplement (&status, header, analysis, "PSPHOT.PSF.APRESID");
Index: /tags/ipp-20121026/psphot/src/psphotSourceMatch.c
===================================================================
--- /tags/ipp-20121026/psphot/src/psphotSourceMatch.c	(revision 34796)
+++ /tags/ipp-20121026/psphot/src/psphotSourceMatch.c	(revision 34797)
@@ -7,4 +7,6 @@
     int     order;
     bool    matchAll;
+    long    nSources;
+    float   yRatioMax;
     char    filterID[MATCH_INFO_FILTER_STR_LEN];
 } psphotStackMatchInfo;
@@ -218,7 +220,49 @@
 	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.
+    // The matchInfo recipe each entry contains 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 filters with a non-zero is above its limit we consider the y band stack to
+    // be suspect and so disable the y band matchAll recipe (if set)
+
+    int iy = -1;
+    // note 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 +560,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 +571,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);
