Index: trunk/ppSub/src/ppSubReadout.c
===================================================================
--- trunk/ppSub/src/ppSubReadout.c	(revision 14193)
+++ trunk/ppSub/src/ppSubReadout.c	(revision 14196)
@@ -6,9 +6,7 @@
 #include <pslib.h>
 #include <psmodules.h>
+#include <psphot.h>
 
 #include "ppSub.h"
-
-#define MASK_BAD      0x01              // Mask value for bad pixel
-#define MASK_STAMP    0x02              // Mask value for bad stamp (and bad stamp region)
 
 bool ppSubReadout(pmConfig *config, const pmFPAview *view)
@@ -16,8 +14,6 @@
     pmReadout *inRO = pmFPAfileThisReadout(config->files, view, "PPSUB.INPUT"); // Input readout
     pmReadout *refRO = pmFPAfileThisReadout(config->files, view, "PPSUB.REF"); // Reference readout
-#if 0
     pmCell *outCell = pmFPAfileThisCell(config->files, view, "PPSUB.OUTPUT"); // Output cell
     pmReadout *outRO = pmReadoutAlloc(outCell); // Output readout
-#endif
 
     psImage *input = inRO->image;       // Input image
@@ -45,32 +41,16 @@
                                         config); // Mask for blank reg.
 
-    if (!inRO->mask && !pmReadoutGenerateMask(inRO, pmConfigMask("SAT", config),
-        pmConfigMask("BAD", config))) {
-        psError(PS_ERR_UNKNOWN, false, "Unable to generate mask for input image");
-        return false;
+    int numCols = input->numCols, numRows = input->numRows; // Image dimensions
+    if (!inRO->mask) {
+        inRO->mask = psImageAlloc(numCols, numRows, PS_TYPE_MASK);
+        psImageInit(inRO->mask, 0);
     }
-    if (!inRO->weight && !pmReadoutGenerateWeight(inRO, true)) {
-        psError(PS_ERR_UNKNOWN, false, "Unable to generate weight map for input image");
-        return false;
-    }
-    if (!refRO->mask && !pmReadoutGenerateMask(refRO, pmConfigMask("SAT", config),
-        pmConfigMask("BAD", config))) {
-        psError(PS_ERR_UNKNOWN, false, "Unable to generate mask for reference image");
-        return false;
-    }
-    if (!refRO->weight && !pmReadoutGenerateWeight(refRO, true)) {
-        psError(PS_ERR_UNKNOWN, false, "Unable to generate weight map for reference image");
-        return false;
+    if (!refRO->mask) {
+        refRO->mask = psImageAlloc(numCols, numRows, PS_TYPE_MASK);
+        psImageInit(refRO->mask, 0);
     }
 
-    // Worried about the masks for bad pixels and bad stamps colliding, so make our own mask
-    int numCols = input->numCols, numRows = input->numRows; // Image dimensions
-    psImage *stampMask = psImageAlloc(numCols, numRows, PS_TYPE_MASK); // Mask to use for stamps
-    for (int y = 0; y < numRows; y++) {
-        for (int x = 0; x < numCols; x++) {
-            stampMask->data.PS_TYPE_MASK_DATA[y][x] =
-                (refRO->mask->data.PS_TYPE_MASK_DATA[y][x] & maskBad) ? MASK_BAD : 0;
-        }
-    }
+    // Mask for subtraction
+    psImage *subMask = pmSubtractionMask(inRO->mask, refRO->mask, maskBad, size, footprint);
 
 #if 0
@@ -97,6 +77,5 @@
     int numRejected = -1;               // Number of rejected stamps in each iteration
     for (int i = 0; i < iter && numRejected != 0; i++) {
-        stamps = pmSubtractionFindStamps(stamps, refRO->image, stampMask, MASK_BAD, MASK_STAMP,
-                                         threshold, spacing, size + footprint);
+        stamps = pmSubtractionFindStamps(stamps, refRO->image, subMask, threshold, spacing);
         if (!stamps) {
             psError(PS_ERR_UNKNOWN, false, "Unable to find stamps on reference image.");
@@ -116,5 +95,5 @@
         }
 
-        numRejected = pmSubtractionRejectStamps(stamps, refRO->image, inRO->image, stampMask, MASK_STAMP,
+        numRejected = pmSubtractionRejectStamps(stamps, refRO->image, inRO->image, subMask,
                                                 solution, footprint, rej, kernels);
         if (numRejected < 0) {
@@ -124,10 +103,9 @@
         psLogMsg("ppSub", PS_LOG_INFO, "%d stamps rejected on iteration %d.", numRejected, i);
     }
-    psFree(stampMask);
+    psFree(subMask);
 
     psImage *convImage = NULL, *convWeight = NULL, *convMask = NULL; // Convolved images
-    if (!pmSubtractionConvolve(&convImage, &convWeight, &convMask,
-                               refRO->image, refRO->weight, refRO->mask,
-                               MASK_BAD, maskBlank, solution, kernels)) {
+    if (!pmSubtractionConvolve(&convImage, &convWeight, &convMask, refRO->image, refRO->weight, subMask,
+                               maskBlank, solution, kernels)) {
         psError(PS_ERR_UNKNOWN, false, "Unable to convolve reference image.");
         goto ERROR;
@@ -136,14 +114,25 @@
     // Do the subtraction
     if (reverse) {
-        (void)psBinaryOp(inRO->image, convImage, "-", inRO->image);
+        outRO->image = (psImage*)psBinaryOp(NULL, convImage, "-", inRO->image);
     } else {
-        (void)psBinaryOp(inRO->image, inRO->image, "-", convImage);
+        outRO->image = (psImage*)psBinaryOp(NULL, inRO->image, "-", convImage);
     }
-    (void)psBinaryOp(inRO->mask, convMask, "|", inRO->mask);
-    (void)psBinaryOp(inRO->weight, convWeight, "+", inRO->weight);
+    outRO->mask = (psImage*)psBinaryOp(NULL, convMask, "|", inRO->mask);
+    if (convWeight) {
+        outRO->weight = (psImage*)psBinaryOp(NULL, convWeight, "+", inRO->weight);
+    }
 
     psFree(convImage);
     psFree(convMask);
     psFree(convWeight);
+
+    outRO->data_exists = true;
+    outCell->data_exists = true;
+    outCell->parent->data_exists = true;
+
+    if (!pmFPACopyConcepts(outCell->parent->parent, inRO->parent->parent->parent)) {
+        psError(PS_ERR_UNKNOWN, false, "Unable to copy concepts from input to output.");
+        return false;
+    }
 
 #if 0
@@ -180,14 +169,23 @@
 #endif
 
+    psFree(kernels);
+    psFree(stamps);
+    psFree(solution);
 
-        psFree(kernels);
-        psFree(stamps);
-        psFree(solution);
-        return true;
+    if (psMetadataLookupBool(NULL, config->arguments, "PHOTOMETRY")) {
+        pmFPAfile *photFile = psMetadataLookupPtr(NULL, config->files, "PSPHOT.INPUT");
+        pmFPACopy(photFile->fpa, inRO->parent->parent->parent);
 
-    ERROR:
-        psFree(kernels);
-        psFree(stamps);
-        psFree(solution);
-        return false;
+        psphotReadout(config, view);
+
+        pmFPAfileActivate(config->files, false, "PSPHOT.INPUT");
+    }
+
+    return true;
+
+ERROR:
+    psFree(kernels);
+    psFree(stamps);
+    psFree(solution);
+    return false;
 }
