Index: trunk/psModules/src/imcombine/pmSubtraction.c
===================================================================
--- trunk/psModules/src/imcombine/pmSubtraction.c	(revision 15300)
+++ trunk/psModules/src/imcombine/pmSubtraction.c	(revision 15305)
@@ -4,6 +4,6 @@
  *  @author GLG, MHPCC
  *
- *  @version $Revision: 1.62 $ $Name: not supported by cvs2svn $
- *  @date $Date: 2007-09-17 21:40:22 $
+ *  @version $Revision: 1.63 $ $Name: not supported by cvs2svn $
+ *  @date $Date: 2007-10-12 23:00:36 $
  *
  *  Copyright 2004-2007 Institute for Astronomy, University of Hawaii
@@ -459,5 +459,5 @@
 
 psImage *pmSubtractionMask(const psImage *refMask, const psImage *inMask, psMaskType maskVal,
-                           int size, int footprint)
+                           int size, int footprint, float badFrac)
 {
     PS_ASSERT_IMAGE_NON_NULL(refMask, NULL);
@@ -470,4 +470,8 @@
     PS_ASSERT_INT_NONNEGATIVE(size, NULL);
     PS_ASSERT_INT_NONNEGATIVE(footprint, NULL);
+    if (isfinite(badFrac)) {
+        PS_ASSERT_FLOAT_LARGER_THAN(badFrac, 0.0, NULL);
+        PS_ASSERT_FLOAT_LESS_THAN_OR_EQUAL(badFrac, 1.0, NULL);
+    }
 
     // Size of the images
@@ -475,15 +479,36 @@
     int numRows = refMask->numRows;
 
+    // Dereference inputs for convenience
+    psMaskType **inData = NULL;
+    if (inMask) {
+        inData = inMask->data.PS_TYPE_MASK_DATA;
+    }
+    psMaskType **refData = refMask->data.PS_TYPE_MASK_DATA;
+
+    // First, a pass through to determine the fraction of bad pixels
+    if (isfinite(badFrac) && badFrac != 1.0) {
+        int numBad = 0;                 // Number of bad pixels
+        for (int y = 0; y < numRows; y++) {
+            for (int x = 0; x < numCols; x++) {
+                if (inData && inData[y][x] & maskVal) {
+                    numBad++;
+                    continue;
+                }
+                if (refData[y][x] & maskVal) {
+                    numBad++;
+                }
+            }
+        }
+        if (numBad > badFrac * numCols * numRows) {
+            psError(PS_ERR_BAD_PARAMETER_VALUE, true, "Fraction of bad pixels (%d/%d) exceeds limit (%f)\n",
+                    numBad, numCols * numRows, badFrac);
+            return NULL;
+        }
+    }
+
     // Worried about the masks for bad pixels and bad stamps colliding, so make our own mask
     psImage *mask = psImageAlloc(numCols, numRows, PS_TYPE_MASK); // The global mask
     psImageInit(mask, 0);
-
-    // Dereference for convenience
-    psMaskType **maskData = mask->data.PS_TYPE_MASK_DATA;
-    psMaskType **inData = NULL;
-    if (inMask) {
-        inData = inMask->data.PS_TYPE_MASK_DATA;
-    }
-    psMaskType **refData = refMask->data.PS_TYPE_MASK_DATA;
+    psMaskType **maskData = mask->data.PS_TYPE_MASK_DATA; // Dereference for convenience
 
     // Block out a border around the edge of the image
@@ -510,4 +535,9 @@
         }
     }
+
+    // XXX Could do something smarter here --- we will get images that are predominantly masked (where the
+    // skycell isn't overlapped by a large fraction by the observation), so that convolving around every bad
+    // pixel is wasting time.  As a first cut, I've put in a check on the fraction of bad pixels, but we could
+    // imagine looking for the edge of big regions and convolving just at the edge.
 
     // Mask the bad pixels, and around them
Index: trunk/psModules/src/imcombine/pmSubtraction.h
===================================================================
--- trunk/psModules/src/imcombine/pmSubtraction.h	(revision 15300)
+++ trunk/psModules/src/imcombine/pmSubtraction.h	(revision 15305)
@@ -6,6 +6,6 @@
  * @author GLG, MHPCC
  *
- * @version $Revision: 1.17 $ $Name: not supported by cvs2svn $
- * @date $Date: 2007-09-10 20:10:05 $
+ * @version $Revision: 1.18 $ $Name: not supported by cvs2svn $
+ * @date $Date: 2007-10-12 23:00:37 $
  *
  * Copyright 2004-207 Institute for Astronomy, University of Hawaii
@@ -37,5 +37,6 @@
                            psMaskType maskVal, ///< Value to mask out
                            int size, ///< Half-size of the kernel (pmSubtractionKernels.size)
-                           int footprint ///< Half-size of the kernel footprint
+                           int footprint, ///< Half-size of the kernel footprint
+                           float badFrac ///< Maximum fraction of bad input pixels to accept
     );
 
Index: trunk/psModules/src/imcombine/pmSubtractionMatch.c
===================================================================
--- trunk/psModules/src/imcombine/pmSubtractionMatch.c	(revision 15300)
+++ trunk/psModules/src/imcombine/pmSubtractionMatch.c	(revision 15305)
@@ -98,5 +98,5 @@
                         int inner, int ringsOrder, int binning, bool optimum, const psVector *optFWHMs,
                         int optOrder, float optThreshold, int iter, float rej, psMaskType maskBad,
-                        psMaskType maskBlank)
+                        psMaskType maskBlank, float badFrac)
 {
     PS_ASSERT_PTR_NON_NULL(convolved, false);
@@ -165,4 +165,8 @@
     // Don't care about maskBad
     // Don't care about maskBlank
+    if (isfinite(badFrac)) {
+        PS_ASSERT_FLOAT_LARGER_THAN(badFrac, 0.0, NULL);
+        PS_ASSERT_FLOAT_LESS_THAN_OR_EQUAL(badFrac, 1.0, NULL);
+    }
 
     // If the stamp footprint is smaller than the kernel size, then we won't get much signal in the outer
@@ -219,5 +223,9 @@
     memCheck("start");
 
-    subMask = pmSubtractionMask(reference->mask, inMask, maskBad, size, footprint);
+    subMask = pmSubtractionMask(reference->mask, inMask, maskBad, size, footprint, badFrac);
+    if (subMask) {
+        psError(PS_ERR_UNKNOWN, false, "Unable to generate subtraction mask.");
+        return false;
+    }
 
     memCheck("mask");
Index: trunk/psModules/src/imcombine/pmSubtractionMatch.h
===================================================================
--- trunk/psModules/src/imcombine/pmSubtractionMatch.h	(revision 15300)
+++ trunk/psModules/src/imcombine/pmSubtractionMatch.h	(revision 15305)
@@ -37,5 +37,6 @@
                         float rej,      ///< Rejection threshold
                         psMaskType maskBad, ///< Value to mask
-                        psMaskType maskBlank ///< Mask for blank region
+                        psMaskType maskBlank, ///< Mask for blank region
+                        float badFrac   ///< Maximum fraction of bad input pixels to accept
     );
 
