Index: /tags/ipp-20120404/ippconfig/recipes/psphot.config
===================================================================
--- /tags/ipp-20120404/ippconfig/recipes/psphot.config	(revision 33761)
+++ /tags/ipp-20120404/ippconfig/recipes/psphot.config	(revision 33762)
@@ -57,4 +57,6 @@
 PEAKS_POS2_NSIGMA_LIMIT             F32   25.0            # peak signficance threshold for POS2 sources. (ppSub)
 				    	  		  # input pixels contribute less than this fraction of the flux
+PEAKS_NMAX_TOTAL                    S32   0               # maximum allowed number of peaks 0 == unlimited
+
 # parameters which adjust the footprint analysis
 USE_FOOTPRINTS                      BOOL  TRUE       	  # use new pmFootprint peak packaging
@@ -368,4 +370,5 @@
   PSF_MODEL                           STR   PS_MODEL_GAUSS 
   EXT_MODEL                           STR   PS_MODEL_QGAUSS
+  PEAKS_NMAX_TOTAL                    S32   50000 # maximum allowed number of peaks - To avoid memory explosion
 END
 
Index: /tags/ipp-20120404/psphot/src/psphot.h
===================================================================
--- /tags/ipp-20120404/psphot/src/psphot.h	(revision 33761)
+++ /tags/ipp-20120404/psphot/src/psphot.h	(revision 33762)
@@ -189,5 +189,5 @@
 // used by psphotFindDetections
 pmReadout      *psphotSignificanceImage (pmReadout *readout, psMetadata *recipe, psImageMaskType maskVal);
-psArray        *psphotFindPeaks (pmReadout *significance, pmReadout *readout, psMetadata *recipe, const float threshold, const int nMax);
+psArray        *psphotFindPeaks (pmReadout *significance, pmReadout *readout, psMetadata *recipe, const float threshold, const int nMax, int *totalPeaks);
 bool            psphotFindFootprints (pmDetections *detections, pmReadout *significance, pmReadout *readout, psMetadata *recipe, const float threshold, const int pass, psImageMaskType maskVal);
 psErrorCode     psphotCullPeaks(const pmReadout *readout, const pmReadout *signifRO, const psMetadata *recipe, psArray *footprints);
Index: /tags/ipp-20120404/psphot/src/psphotFindDetections.c
===================================================================
--- /tags/ipp-20120404/psphot/src/psphotFindDetections.c	(revision 33761)
+++ /tags/ipp-20120404/psphot/src/psphotFindDetections.c	(revision 33762)
@@ -96,5 +96,6 @@
 
     // detect the peaks in the significance image
-    detections->peaks = psphotFindPeaks (significance, readout, recipe, threshold, NMAX);
+    int totalPeaks = 0;
+    detections->peaks = psphotFindPeaks (significance, readout, recipe, threshold, NMAX, &totalPeaks);
     psMetadataAddF32  (readout->analysis, PS_LIST_TAIL, "PEAK_THRESHOLD", PS_META_REPLACE, "Peak Detection Threshold", threshold);
     if (!detections->peaks) {
@@ -103,4 +104,11 @@
 	psFree (detections);
 	psError (PSPHOT_ERR_CONFIG, false, "failed on peak search");
+        return false;
+    }
+    // hard limit on number of peaks we will accept. (To avoid memory overload in psphotStack)
+    int maxPeaks = psMetadataLookupS32 (&status, recipe, "PEAKS_NMAX_TOTAL"); PS_ASSERT (status, NULL);
+    if (maxPeaks && (totalPeaks > maxPeaks)) {
+	psFree (detections);
+	psError (PSPHOT_ERR_DATA, true, "Too many peaks %d found PEAKS_NMAX_TOTAL: %d", totalPeaks, maxPeaks);
         return false;
     }
Index: /tags/ipp-20120404/psphot/src/psphotFindPeaks.c
===================================================================
--- /tags/ipp-20120404/psphot/src/psphotFindPeaks.c	(revision 33761)
+++ /tags/ipp-20120404/psphot/src/psphotFindPeaks.c	(revision 33762)
@@ -4,5 +4,5 @@
 // image must be constructed to represent (S/N)^2.  If nMax is non-zero, only return a maximum
 // of nMax peaks
-psArray *psphotFindPeaks (pmReadout *significance, pmReadout *readout, psMetadata *recipe, const float threshold, const int nMax) {
+psArray *psphotFindPeaks (pmReadout *significance, pmReadout *readout, psMetadata *recipe, const float threshold, const int nMax, int *totalPeaks) {
 
     bool status = false;
@@ -18,4 +18,8 @@
         psError(PSPHOT_ERR_DATA, false, "no peaks found in this image");
         return NULL;
+    }
+    // return the total number of peaks found before the nMax limit is applied
+    if (totalPeaks) {
+        *totalPeaks = peaks->n;
     }
 
