Index: trunk/psModules/src/imcombine/pmSubtractionStamps.c
===================================================================
--- trunk/psModules/src/imcombine/pmSubtractionStamps.c	(revision 19234)
+++ trunk/psModules/src/imcombine/pmSubtractionStamps.c	(revision 20465)
@@ -262,86 +262,104 @@
     int numStamps = stamps->num;        // Number of stamp regions
     int numFound = 0;                   // Number of stamps found
-    int numValid = 0;                   // Number of valid regions
+    int numSearch = 0;                  // Number of regions searched for new stamp
+    int numGood = 0;                    // Number of good stamps in total
     for (int i = 0; i < numStamps; i++) {
         pmSubtractionStamp *stamp = stamps->stamps->data[i]; // Stamp of interest
 
-        // Only find a new stamp if we need to
-        if (stamp->status != PM_SUBTRACTION_STAMP_REJECTED && stamp->status != PM_SUBTRACTION_STAMP_INIT) {
-            continue;
-        }
-        numValid++;
-
-        float xStamp = 0, yStamp = 0;   // Coordinates of stamp
-        float fluxStamp = NAN;          // Flux of stamp
-        bool goodStamp = false;         // Found a good stamp?
-
-        // A couple different ways of finding stamps:
-        if (stamps->x && stamps->y) {
-            // Get the next stamp from the list
-            psVector *xList = stamps->x->data[i], *yList = stamps->y->data[i]; // Coordinate lists
-            psVector *fluxList = stamps->flux->data[i]; // List of stamp fluxes
-
-            // Take stamp off the top of the (sorted) list
-            if (xList->n > 0) {
-                int index = xList->n - 1; // Index of new stamp
-                xStamp = xList->data.F32[index];
-                yStamp = yList->data.F32[index];
-                fluxStamp = fluxList->data.F32[index];
-
-                // Chop off the top of the list
-                xList->n = index;
-                yList->n = index;
-                fluxList->n = index;
-
-                goodStamp = true;
-            }
-        } else {
-            // Use a simple method of automatically finding stamps --- take the highest pixel in the subregion
-            fluxStamp = threshold;
-            psRegion *subRegion = stamps->regions->data[i]; // Sub-region of interest
-            for (int y = subRegion->y0; y <= subRegion->y1; y++) {
-                for (int x = subRegion->x0; x <= subRegion->x1; x++) {
-                    if (checkStampMask(x, y, subMask, mode, footprint) && image->data.F32[y][x] > fluxStamp) {
-                        fluxStamp = image->data.F32[y][x];
-                        xStamp = x;
-                        yStamp = y;
-                        goodStamp = true;
+        switch (stamp->status) {
+          case PM_SUBTRACTION_STAMP_NONE:
+            continue;
+          case PM_SUBTRACTION_STAMP_FOUND:
+          case PM_SUBTRACTION_STAMP_CALCULATE:
+          case PM_SUBTRACTION_STAMP_USED:
+            numGood++;
+            continue;
+          case PM_SUBTRACTION_STAMP_INIT:
+          case PM_SUBTRACTION_STAMP_REJECTED:
+            numSearch++;
+
+            float xStamp = 0, yStamp = 0;   // Coordinates of stamp
+            float fluxStamp = NAN;          // Flux of stamp
+            bool goodStamp = false;         // Found a good stamp?
+
+            // A couple different ways of finding stamps:
+            if (stamps->x && stamps->y) {
+                // Get the next stamp from the list
+                psVector *xList = stamps->x->data[i], *yList = stamps->y->data[i]; // Coordinate lists
+                psVector *fluxList = stamps->flux->data[i]; // List of stamp fluxes
+
+                // Take stamp off the top of the (sorted) list
+                if (xList->n > 0) {
+                    int index = xList->n - 1; // Index of new stamp
+                    xStamp = xList->data.F32[index];
+                    yStamp = yList->data.F32[index];
+                    fluxStamp = fluxList->data.F32[index];
+
+                    // Chop off the top of the list
+                    xList->n = index;
+                    yList->n = index;
+                    fluxList->n = index;
+
+                    goodStamp = true;
+                } else {
+                    psTrace("psModules.imcombine", 9, "No sources in subregion %d", i);
+                }
+            } else {
+                // Use a simple method of automatically finding stamps --- take the highest pixel in the
+                // subregion
+                fluxStamp = threshold;
+                psRegion *subRegion = stamps->regions->data[i]; // Sub-region of interest
+                for (int y = subRegion->y0; y <= subRegion->y1; y++) {
+                    for (int x = subRegion->x0; x <= subRegion->x1; x++) {
+                        if (checkStampMask(x, y, subMask, mode, footprint) &&
+                            image->data.F32[y][x] > fluxStamp) {
+                            fluxStamp = image->data.F32[y][x];
+                            xStamp = x;
+                            yStamp = y;
+                            goodStamp = true;
+                        }
                     }
                 }
             }
-        }
-
-        if (goodStamp) {
-            stamp->x = xStamp;
-            stamp->y = yStamp;
-            stamp->flux = fluxStamp;
-
-            // Reset the postage stamps since we're making a new stamp
-            psFree(stamp->image1);
-            psFree(stamp->image2);
-            psFree(stamp->weight);
-            psFree(stamp->convolutions1);
-            psFree(stamp->convolutions2);
-            stamp->image1 = stamp->image2 = stamp->weight = NULL;
-            stamp->convolutions1 = stamp->convolutions2 = NULL;
-
-            stamp->status = PM_SUBTRACTION_STAMP_FOUND;
-            numFound++;
-            psTrace("psModules.imcombine", 5, "Found stamp in subregion %d: %d,%d\n",
-                    i, (int)stamp->x, (int)stamp->y);
-        } else {
-            stamp->status = PM_SUBTRACTION_STAMP_NONE;
-        }
-    }
-
-    if (numValid > 0) {
+
+            if (goodStamp) {
+                stamp->x = xStamp;
+                stamp->y = yStamp;
+                stamp->flux = fluxStamp;
+
+                // Reset the postage stamps since we're making a new stamp
+                psFree(stamp->image1);
+                psFree(stamp->image2);
+                psFree(stamp->weight);
+                psFree(stamp->convolutions1);
+                psFree(stamp->convolutions2);
+                stamp->image1 = stamp->image2 = stamp->weight = NULL;
+                stamp->convolutions1 = stamp->convolutions2 = NULL;
+
+                stamp->status = PM_SUBTRACTION_STAMP_FOUND;
+                numFound++;
+                psTrace("psModules.imcombine", 5, "Found stamp in subregion %d: %d,%d\n",
+                        i, (int)stamp->x, (int)stamp->y);
+            } else {
+                stamp->status = PM_SUBTRACTION_STAMP_NONE;
+            }
+        }
+    }
+
+    if (numSearch > 0) {
         psLogMsg("psModules.imcombine", PS_LOG_INFO, "Found %d stamps", numFound);
     }
 
+    if (numGood == 0 && numFound == 0) {
+        // No good stamps
+        psFree(stamps);
+        return NULL;
+    }
+
     return stamps;
 }
 
 
-pmSubtractionStampList *pmSubtractionStampsSet(const psVector *x, const psVector *y, const psVector *flux,
+pmSubtractionStampList *pmSubtractionStampsSet(const psVector *x, const psVector *y,
                                                const psImage *image, const psImage *subMask,
                                                const psRegion *region, int footprint, float spacing,
@@ -354,11 +372,5 @@
     PS_ASSERT_VECTOR_TYPE(y, PS_TYPE_F32, NULL);
     PS_ASSERT_VECTORS_SIZE_EQUAL(y, x, NULL);
-    if (flux) {
-        PS_ASSERT_VECTOR_NON_NULL(flux, NULL);
-        PS_ASSERT_VECTOR_TYPE(flux, PS_TYPE_F32, NULL);
-        PS_ASSERT_VECTORS_SIZE_EQUAL(flux, x, NULL);
-    } else {
-        PS_ASSERT_IMAGE_NON_NULL(image, NULL);
-    }
+    PS_ASSERT_IMAGE_NON_NULL(image, NULL);
     if (subMask) {
         PS_ASSERT_IMAGE_NON_NULL(subMask, NULL);
@@ -418,10 +430,5 @@
                 xList->data.F32[index] = xStamp;
                 yList->data.F32[index] = yStamp;
-
-                if (flux) {
-                    fluxList->data.F32[index] = flux->data.F32[i];
-                } else {
-                    fluxList->data.F32[index] = image->data.F32[yPix][xPix];
-                }
+                fluxList->data.F32[index] = image->data.F32[yPix][xPix];
 
                 found = true;
@@ -581,7 +588,8 @@
 #endif
 
-pmSubtractionStampList *pmSubtractionStampsSetFromSources(const psArray *sources, const psImage *subMask,
-                                                          const psRegion *region, int footprint,
-                                                          float spacing, pmSubtractionMode mode)
+pmSubtractionStampList *pmSubtractionStampsSetFromSources(const psArray *sources, const psImage *image,
+                                                          const psImage *subMask, const psRegion *region,
+                                                          int footprint, float spacing,
+                                                          pmSubtractionMode mode)
 {
     PS_ASSERT_ARRAY_NON_NULL(sources, NULL);
@@ -592,11 +600,9 @@
     psVector *x = psVectorAllocEmpty(numIn, PS_TYPE_F32); // x coordinates
     psVector *y = psVectorAllocEmpty(numIn, PS_TYPE_F32); // y coordinates
-    psVector *flux = psVectorAllocEmpty(numIn, PS_TYPE_F32); // Fluxes
 
     int numOut = 0;                     // Number of sources in output list
     for (int i = 0; i < numIn; i++) {
         pmSource *source = sources->data[i]; // Source of interest
-        if (!source || (source->mode & SOURCE_MASK) || !isfinite(source->psfMag) ||
-            source->psfMag > SOURCE_FAINTEST) {
+        if (!source || (source->mode & SOURCE_MASK) || source->psfMag > SOURCE_FAINTEST) {
             continue;
         }
@@ -608,16 +614,13 @@
             y->data.F32[numOut] = source->peak->yf;
         }
-        flux->data.F32[numOut] = powf(10.0, -0.4 * source->psfMag);
         numOut++;
     }
     x->n = numOut;
     y->n = numOut;
-    flux->n = numOut;
-
-    pmSubtractionStampList *stamps = pmSubtractionStampsSet(x, y, flux, NULL, subMask, region,
+
+    pmSubtractionStampList *stamps = pmSubtractionStampsSet(x, y, image, subMask, region,
                                                             footprint, spacing, mode); // Stamps to return
     psFree(x);
     psFree(y);
-    psFree(flux);
 
     if (!stamps) {
@@ -647,5 +650,5 @@
     psBinaryOp(y, y, "-", psScalarAlloc(1.0, PS_TYPE_F32));
 
-    pmSubtractionStampList *stamps = pmSubtractionStampsSet(x, y, NULL, image, subMask, region, footprint,
+    pmSubtractionStampList *stamps = pmSubtractionStampsSet(x, y, image, subMask, region, footprint,
                                                             spacing, mode);
     psFree(data);
