Index: trunk/psModules/src/imcombine/pmPSFEnvelope.c
===================================================================
--- trunk/psModules/src/imcombine/pmPSFEnvelope.c	(revision 23242)
+++ trunk/psModules/src/imcombine/pmPSFEnvelope.c	(revision 23955)
@@ -33,5 +33,5 @@
 
 
-//#define TESTING                         // Enable test output
+#define TESTING                         // Enable test output
 #define PEAK_FLUX 1.0e4                 // Peak flux for each source
 #define SKY_VALUE 0.0e0                 // Sky value for fake image
@@ -113,4 +113,6 @@
     pmReadout *fakeRO = pmReadoutAlloc(NULL); // Fake readout
     float maxRadius = 0.0;              // Maximum radius of sources
+    psVector *numbers = psVectorAlloc(numFakes, PS_TYPE_S32); // Number of detections for each source
+    psVectorInit(numbers, 0);
     for (int i = 0; i < inputs->n; i++) {
         pmPSF *psf = inputs->data[i];   // PSF of interest
@@ -127,4 +129,5 @@
             psFree(xOffset);
             psFree(fakes);
+            psFree(numbers);
             psf->residuals = resid;
             return NULL;
@@ -140,4 +143,8 @@
 
             double flux = fakeRO->image->data.F32[(int)y][(int)x];
+            if (flux > 0) {
+                // The source is present
+                numbers->data.S32[j]++;
+            }
             float norm = PEAK_FLUX / flux; // Normalisation for source
             psRegion region = psRegionSet(x - radius, x + radius, y - radius, y + radius); // PSF region
@@ -190,4 +197,5 @@
 
     // Put the fake sources onto a full-size image
+    psArray *goodFakes = psArrayAllocEmpty(numFakes); // Good fake sources
     pmReadout *readout = pmReadoutAlloc(NULL); // Readout to contain envelope pixels
     readout->image = psImageAlloc(numCols, numRows, PS_TYPE_F32);
@@ -195,4 +203,8 @@
     for (int i = 0; i < numFakes; i++) {
         pmSource *source = fakes->data[i]; // Fake source
+        if (numbers->data.S32[i] > 0) {
+            psArrayAdd(goodFakes, goodFakes->n, source);
+        }
+
         // Position of source on fake image
         int xFake = source->peak->x + xOffset->data.S32[i];
@@ -213,4 +225,5 @@
             psFree(yOffset);
             psFree(fakes);
+            psFree(numbers);
             return NULL;
         }
@@ -220,4 +233,9 @@
     psFree(yOffset);
     psFree(envelope);
+    psFree(numbers);
+
+    psFree(fakes);
+    fakes = goodFakes;
+    numFakes = fakes->n;
 
     // XXX Setting the variance seems to be an art
