Index: branches/tap_branches/ppSub/src/ppSubLoop.c
===================================================================
--- branches/tap_branches/ppSub/src/ppSubLoop.c	(revision 25900)
+++ branches/tap_branches/ppSub/src/ppSubLoop.c	(revision 27838)
@@ -27,5 +27,5 @@
 
     pmConfigCamerasCull(config, NULL);
-    pmConfigRecipesCull(config, "PPSUB,PPSTATS,PSPHOT,MASKS,JPEG");
+    pmConfigRecipesCull(config, "PPSUB,PPSTATS,PSPHOT,PSASTRO,MASKS,JPEG");
 
 
@@ -43,10 +43,10 @@
 
     if (!ppSubSetMasks(config)) {
-        psError(PS_ERR_UNKNOWN, false, "Unable to set masks.");
+        psError(psErrorCodeLast(), false, "Unable to set masks.");
         return false;
     }
 
     if (!ppSubMatchPSFs(data)) {
-        psError(PS_ERR_UNKNOWN, false, "Unable to match PSFs.");
+        psError(psErrorCodeLast(), false, "Unable to match PSFs.");
         return false;
     }
@@ -55,4 +55,9 @@
         return true;
     }
+    // generate the residual stamp grid for visualization
+    if (!ppSubResidualSampleJpeg(config)) {
+        psError(psErrorCodeLast(), false, "Unable to update.");
+        return false;
+    }
 
     psMetadataAddF32(data->stats, PS_LIST_TAIL, "TIME_MATCH", 0, "Time to match PSFs",
@@ -66,5 +71,5 @@
 
     if (!ppSubLowThreshold(data)) {
-        psError(PS_ERR_UNKNOWN, false, "Unable to threshold images.");
+        psError(psErrorCodeLast(), false, "Unable to threshold images.");
         return false;
     }
@@ -77,15 +82,25 @@
 
     if (!ppSubDefineOutput("PPSUB.OUTPUT", config)) {
-        psError(PS_ERR_UNKNOWN, false, "Unable to define output.");
+        psError(psErrorCodeLast(), false, "Unable to define output.");
         return false;
     }
 
     if (!data->quality && !ppSubMakePSF(data)) {
-        psError(PS_ERR_UNKNOWN, false, "Unable to generate PSF.");
-        return false;
+        psError(psErrorCodeLast(), false, "Unable to generate PSF.");
+        return false;
+    }
+
+    // Now we've got a PSF, blow away detections in case they're confused with real output detections
+    {
+        pmFPAview *view = ppSubViewReadout(); // View to readout
+        pmReadout *out = pmFPAfileThisReadout(config->files, view, "PPSUB.OUTPUT");
+        if (psMetadataLookup(out->analysis, "PSPHOT.DETECTIONS")) {
+            psMetadataRemoveKey(out->analysis, "PSPHOT.DETECTIONS");
+        }
+        psFree(view);
     }
 
     if (!ppSubReadoutSubtract(config)) {
-        psError(PS_ERR_UNKNOWN, false, "Unable to subtract images.");
+        psError(psErrorCodeLast(), false, "Unable to subtract images.");
         return false;
     }
@@ -99,5 +114,5 @@
     // Higher order background subtraction using psphot
     if (!ppSubBackground(config)) {
-        psError(PS_ERR_UNKNOWN, false, "Unable to subtract background.");
+        psError(psErrorCodeLast(), false, "Unable to subtract background.");
         return false;
     }
@@ -105,5 +120,5 @@
     // Perform Variance correction (rescale within a modest range)
     if (!ppSubVarianceRescale(config)) {
-        psError(PS_ERR_UNKNOWN, false, "Unable to rescale variance.");
+        psError(psErrorCodeLast(), false, "Unable to rescale variance.");
         return false;
     }
@@ -115,5 +130,5 @@
 
     if (!data->quality && !ppSubReadoutPhotometry("PPSUB.OUTPUT", data)) {
-        psError(PS_ERR_UNKNOWN, false, "Unable to perform photometry.");
+        psError(psErrorCodeLast(), false, "Unable to perform photometry.");
         return false;
     }
@@ -126,10 +141,11 @@
     // Perform statistics on the cell
     if (!ppSubReadoutStats(data)) {
-        psError(PS_ERR_UNKNOWN, false, "Unable to collect statistics");
-        return false;
-    }
-
+        psError(psErrorCodeLast(), false, "Unable to collect statistics");
+        return false;
+    }
+
+    // generate the binned image used to write the jpeg
     if (!ppSubReadoutJpeg(config)) {
-        psError(PS_ERR_UNKNOWN, false, "Unable to update.");
+        psError(psErrorCodeLast(), false, "Unable to update.");
         return false;
     }
@@ -143,10 +159,10 @@
 
         if (data->inverse && !ppSubDefineOutput("PPSUB.INVERSE", config)) {
-            psError(PS_ERR_UNKNOWN, false, "Unable to define inverse.");
+            psError(psErrorCodeLast(), false, "Unable to define inverse.");
             return false;
         }
 
         if (!ppSubReadoutInverse(config)) {
-            psError(PS_ERR_UNKNOWN, false, "Unable to invert images.");
+            psError(psErrorCodeLast(), false, "Unable to invert images.");
             return false;
         }
@@ -164,5 +180,5 @@
 
         if (!data->quality && !ppSubReadoutPhotometry("PPSUB.INVERSE", data)) {
-            psError(PS_ERR_UNKNOWN, false, "Unable to perform photometry.");
+            psError(psErrorCodeLast(), false, "Unable to perform photometry.");
             return false;
         }
