Index: trunk/ppImage/src/ppImage.h
===================================================================
--- trunk/ppImage/src/ppImage.h	(revision 41265)
+++ trunk/ppImage/src/ppImage.h	(revision 41382)
@@ -28,4 +28,5 @@
     bool doVarianceBuild;               // Build internal variance map
     bool doApplyBurntool;               // apply burntool correction
+    bool doApplyPixelZero;              // put to zero pixels underneath mask
     bool doMaskBurntool;                // mask potential burntool trails
     bool doMaskSat;                     // mask saturated pixels
Index: trunk/ppImage/src/ppImageOptions.c
===================================================================
--- trunk/ppImage/src/ppImageOptions.c	(revision 41265)
+++ trunk/ppImage/src/ppImageOptions.c	(revision 41382)
@@ -53,4 +53,6 @@
     options->useVideoDark    = false;   // Use video dark if we can?
     options->useVideoMask    = false;   // Use video mask if we can?
+    options->doApplyPixelZero  = true;   // option for zero'ing pixels under masks
+
     // output files requested
     options->BaseFITS        = false;   // create output image
@@ -234,4 +236,6 @@
     options->doMaskBurntool  = psMetadataLookupBool(NULL, recipe, "MASK.BURNTOOL");
     options->doApplyBurntool = psMetadataLookupBool(NULL, recipe, "APPLY.BURNTOOL");
+    //TdB: read in switch for zero'ing pixels under masks
+    options->doApplyPixelZero = psMetadataLookupBool(NULL, recipe, "APPLY.PIXELZERO");
     options->doVarianceBuild = psMetadataLookupBool(NULL, recipe, "VARIANCE.BUILD");
     options->doAuxMask       = psMetadataLookupBool(NULL, recipe, "MASK.AUXMASK");
@@ -245,4 +249,5 @@
         }
     }
+
 
     // Mask recipe options (note that mask bit values are set in ppImageSetMaskBits.c)
Index: trunk/ppImage/src/ppImageReplaceBackground.c
===================================================================
--- trunk/ppImage/src/ppImageReplaceBackground.c	(revision 41265)
+++ trunk/ppImage/src/ppImageReplaceBackground.c	(revision 41382)
@@ -144,6 +144,16 @@
     for (int y = 0; y < numRows; y++) {
         for (int x = 0; x < numCols; x++) {
-            if (mask && mask->data.PS_TYPE_IMAGE_MASK_DATA[y][x] & maskVal) {
-                image->data.F32[y][x] = 0.0;
+            if(options->doApplyPixelZero) {
+              if (mask && mask->data.PS_TYPE_IMAGE_MASK_DATA[y][x] & maskVal) {
+                  image->data.F32[y][x] = 0.0;
+              } else {
+		float value = backData[y][x];
+		if (!isfinite(value)) {
+		    image->data.F32[y][x] = NAN;
+		    mask->data.PS_TYPE_IMAGE_MASK_DATA[y][x] |= options->lowMask;
+		} else {
+		    image->data.F32[y][x] -= value;
+		}
+  	      }
             } else {
 		float value = backData[y][x];
@@ -154,5 +164,5 @@
 		    image->data.F32[y][x] -= value;
 		}
-	    }
+            }
         }
     }
@@ -162,6 +172,16 @@
     for (int y = 0; y < numRows; y++) {
         for (int x = 0; x < numCols; x++) {
-            if (mask && mask->data.PS_TYPE_IMAGE_MASK_DATA[y][x] & maskVal) {
-                image->data.F32[y][x] = 0.0;
+            if(options->doApplyPixelZero) {
+              if (mask && mask->data.PS_TYPE_IMAGE_MASK_DATA[y][x] & maskVal) {
+                  image->data.F32[y][x] = 0.0;
+              } else {
+                float value = psImageUnbinPixel(x + 0.5, y + 0.5, modelRO->image, binning); // Background value
+                if (!isfinite(value)) {
+                    image->data.F32[y][x] = NAN;
+                    mask->data.PS_TYPE_IMAGE_MASK_DATA[y][x] |= options->lowMask;
+                } else {
+                    image->data.F32[y][x] -= value;
+                }
+              }
             } else {
                 float value = psImageUnbinPixel(x + 0.5, y + 0.5, modelRO->image, binning); // Background value
