Index: /trunk/ppSub/src/ppSub.c
===================================================================
--- /trunk/ppSub/src/ppSub.c	(revision 30618)
+++ /trunk/ppSub/src/ppSub.c	(revision 30619)
@@ -112,4 +112,5 @@
         psLibFinalize();
 
+	fprintf (stderr, "found %d leaks at %s\n", psMemCheckLeaks (0, NULL, stdout, false), "ppSub");
         exitValue = ppSubExitCode(exitValue);
         exit(exitValue);
Index: /trunk/ppSub/src/ppSubFiles.c
===================================================================
--- /trunk/ppSub/src/ppSubFiles.c	(revision 30618)
+++ /trunk/ppSub/src/ppSubFiles.c	(revision 30619)
@@ -125,4 +125,5 @@
     // FPA
     if (!pmFPAfileIOChecks(config, view, PM_FPA_BEFORE)) {
+	psFree (view);
         return false;
     }
@@ -131,4 +132,5 @@
     view->chip = 0;
     if (!pmFPAfileIOChecks(config, view, PM_FPA_BEFORE)) {
+	psFree (view);
         return false;
     }
@@ -137,4 +139,5 @@
     view->cell = 0;
     if (!pmFPAfileIOChecks(config, view, PM_FPA_BEFORE)) {
+	psFree (view);
         return false;
     }
@@ -143,9 +146,11 @@
     view->readout = 0;
     if (!pmFPAfileIOChecks(config, view, PM_FPA_BEFORE)) {
-        return false;
-    }
-
-    ppSubFilesActivate(config, PPSUB_FILES_ALL, false);
-
+	psFree (view);
+        return false;
+    }
+
+    ppSubFilesActivate(config, PPSUB_FILES_ALL, false);
+
+    psFree (view);
     return true;
 }
@@ -162,4 +167,5 @@
     // Readout
     if (!pmFPAfileIOChecks(config, view, PM_FPA_AFTER)) {
+	psFree (view);
         return false;
     }
@@ -168,4 +174,5 @@
     view->readout = -1;
     if (!pmFPAfileIOChecks(config, view, PM_FPA_AFTER)) {
+	psFree (view);
         return false;
     }
@@ -174,4 +181,5 @@
     view->cell = -1;
     if (!pmFPAfileIOChecks(config, view, PM_FPA_AFTER)) {
+	psFree (view);
         return false;
     }
@@ -180,4 +188,5 @@
     view->chip = -1;
     if (!pmFPAfileIOChecks(config, view, PM_FPA_AFTER)) {
+	psFree (view);
         return false;
     }
@@ -222,4 +231,5 @@
     ppSubFilesActivate(config, PPSUB_FILES_ALL, false);
 
+    psFree (view);
     return true;
 }
Index: /trunk/ppSub/src/ppSubInputDetections.c
===================================================================
--- /trunk/ppSub/src/ppSubInputDetections.c	(revision 30618)
+++ /trunk/ppSub/src/ppSubInputDetections.c	(revision 30619)
@@ -118,4 +118,5 @@
 	if (!psMetadataCopy(sourcesHDU->header, imageHDU->header)) {
 	    psError(PPSUB_ERR_PROG, false, "Unable to copy header");
+	    psFree(view);
 	    return false;
 	}
@@ -130,8 +131,10 @@
     if (!psphotCopyResults (foundDetections, sourcesFile, photFile, view)) {
 	psError(PPSUB_ERR_PROG, false, "Unable to copy psphot outputs");
+	psFree(view);
 	return false;
     }
     // if no sources were found here, we report that back and let them handle it
 
+    psFree(view);
     return true;
 }
Index: /trunk/ppSub/src/ppSubLoop.c
===================================================================
--- /trunk/ppSub/src/ppSubLoop.c	(revision 30618)
+++ /trunk/ppSub/src/ppSubLoop.c	(revision 30619)
@@ -26,4 +26,5 @@
   pmReadout *out = pmFPAfileThisReadout(config->files, view, "PPSUB.OUTPUT");
   psphotSaveImage (NULL, out->image, name);
+  psFree(view);
   return true;
 }
@@ -111,4 +112,5 @@
         goto ESCAPE;
     }
+
     if (data->quality) {
         // Can't do anything at all
@@ -257,6 +259,8 @@
 	psError(psErrorCodeLast(), false, "Unable to generate mask statistics");
 	success = false;
+        psFree(view);
         goto ESCAPE;
       }
+      psFree(view);
     }
     // dumpout(config, "diff.4.fits");
Index: /trunk/ppSub/src/ppSubMakePSF.c
===================================================================
--- /trunk/ppSub/src/ppSubMakePSF.c	(revision 30618)
+++ /trunk/ppSub/src/ppSubMakePSF.c	(revision 30619)
@@ -79,4 +79,5 @@
     if (!detections || !detections->allSources) {
         psError(PPSUB_ERR_CONFIG, true, "No sources from which to determine PSF.");
+        psFree(view);
         return false;
     }
@@ -86,4 +87,5 @@
     // use flags to toss totally bogus entries?
     psArray *goodSources = ppSubSelectPSFSources (sources);
+
     if (!psphotReadoutFindPSF(config, view, "PSPHOT.INPUT", goodSources)) {
         // This is likely a data quality issue
@@ -126,4 +128,5 @@
         outputRO = pmReadoutAlloc(outputCell);
         outputRO->image = psMemIncrRefCounter(inputRO->image);
+	psFree(outputRO); // I have a copy on the outputCell
     }
 
Index: /trunk/ppSub/src/ppSubMatchPSFs.c
===================================================================
--- /trunk/ppSub/src/ppSubMatchPSFs.c	(revision 30618)
+++ /trunk/ppSub/src/ppSubMatchPSFs.c	(revision 30619)
@@ -78,5 +78,4 @@
         return true;
     }
-
     psFree(view);
 
@@ -127,4 +126,5 @@
     if (!inSourceRO || !refSourceRO) {
         psError(PPSUB_ERR_DATA, false, "Unable to scale kernel, since no sources were provided.");
+        psFree(view);
         return false;
     }
@@ -164,24 +164,22 @@
 
     // is auto-scaling needed?
-    if (!psMetadataLookupBool(NULL, recipe, "SCALE")) {
-        // No scaling requested
-        psFree(view);
-        return true;
-    }
-
+    bool scale = psMetadataLookupBool(NULL, recipe, "SCALE");
     float scaleRef = psMetadataLookupF32(NULL, recipe, "SCALE.REF"); // Reference for scaling
     float scaleMin = psMetadataLookupF32(NULL, recipe, "SCALE.MIN"); // Minimum for scaling
     float scaleMax = psMetadataLookupF32(NULL, recipe, "SCALE.MAX"); // Maximum for scaling
-    if (!isfinite(scaleRef) || !isfinite(scaleMin) || !isfinite(scaleMax)) {
+
+    if (scale && (!isfinite(scaleRef) || !isfinite(scaleMin) || !isfinite(scaleMax))) {
         psError(PPSUB_ERR_ARGUMENTS, false,
-                "Scale parameters (SCALE.REF=%f, SCALE.MIN=%f, SCALE.MAX=%f) not set in recipe.",
+                "auto-scale selected but scale parameters (SCALE.REF=%f, SCALE.MIN=%f, SCALE.MAX=%f) not set in recipe.",
                 scaleRef, scaleMin, scaleMax);
         return false;
     }
 
-    if (!pmSubtractionParamsScale(kernelSize, stampSize, kernelWidths, scaleRef, scaleMin, scaleMax)) {
-        psError(PPSUB_ERR_DATA, false, "Unable to scale parameters.");
-        return false;
-    }
+    pmSubtractionParamScaleOptions(scale, scaleRef, scaleMin, scaleMax);
+
+    // if (!pmSubtractionParamsScale(kernelSize, stampSize, kernelWidths)) {
+    //     psError(PPSUB_ERR_DATA, false, "Unable to scale parameters.");
+    //     return false;
+    // }
 
     return true;
@@ -221,4 +219,5 @@
         pmCell *cell = pmFPAfileThisCell(config->files, view, "PPSUB.INPUT.CONV"); // Cell for convolved input
         inConv = pmReadoutAlloc(cell);
+	psFree(inConv);
     }
     pmReadout *refConv = pmFPAfileThisReadout(config->files, view, "PPSUB.REF.CONV"); // Reference convolved
@@ -226,4 +225,5 @@
         pmCell *cell = pmFPAfileThisCell(config->files, view, "PPSUB.REF.CONV"); // Cell for convolved ref.
         refConv = pmReadoutAlloc(cell);
+	psFree(refConv);
     }
 
@@ -380,8 +380,12 @@
 
     if (inRO->covariance) {
-        psImageCovarianceTruncate(inRO->covariance, COVAR_FRAC);
+        psKernel *truncated = psImageCovarianceTruncate(inRO->covariance, COVAR_FRAC);
+        psFree(inRO->covariance);
+	inRO->covariance = truncated;
     }
     if (refRO->covariance) {
-        psImageCovarianceTruncate(refRO->covariance, COVAR_FRAC);
+        psKernel *truncated = psImageCovarianceTruncate(refRO->covariance, COVAR_FRAC);
+        psFree(refRO->covariance);
+	refRO->covariance = truncated;
     }
 
@@ -494,8 +498,12 @@
 
     if (inConv->covariance) {
-        psImageCovarianceTruncate(inConv->covariance, COVAR_FRAC);
+        psKernel *truncated = psImageCovarianceTruncate(inConv->covariance, COVAR_FRAC);
+        psFree(inConv->covariance);
+	inConv->covariance = truncated;
     }
     if (refConv->covariance) {
-        psImageCovarianceTruncate(refConv->covariance, COVAR_FRAC);
+        psKernel *truncated = psImageCovarianceTruncate(refConv->covariance, COVAR_FRAC);
+        psFree(refConv->covariance);
+	refConv->covariance = truncated;
     }
 
Index: /trunk/ppSub/src/ppSubReadoutForcedPhot.c
===================================================================
--- /trunk/ppSub/src/ppSubReadoutForcedPhot.c	(revision 30618)
+++ /trunk/ppSub/src/ppSubReadoutForcedPhot.c	(revision 30619)
@@ -61,4 +61,5 @@
 	// XXX remove the pixels from photFile?
 	// XXX other cleanup operations?
+        psFree(view);
 	return true;
     }
@@ -89,4 +90,5 @@
     if (!psphotCopyResults (&foundDetections, outputFile, photFile, view)) {
 	psError(PPSUB_ERR_PROG, false, "Unable to copy psphot outputs");
+        psFree(view);
 	return false;
     }
@@ -97,4 +99,5 @@
     psMetadataAddF32(data->stats, PS_LIST_TAIL, "TIME_PHOT", PS_META_REPLACE, "Time to do photometry", elapsed);
 
+    psFree(view);
     return true;
 }
Index: /trunk/ppSub/src/ppSubReadoutInverse.c
===================================================================
--- /trunk/ppSub/src/ppSubReadoutInverse.c	(revision 30618)
+++ /trunk/ppSub/src/ppSubReadoutInverse.c	(revision 30619)
@@ -36,4 +36,5 @@
     if (!pmAstromWriteWCS(invHDU->header, outFPA, outChip, WCS_TOLERANCE)) {
         psError(psErrorCodeLast(), false, "Unable to write WCS astrometry to PPSUB.INVERSE.");
+	psFree(view);
         return false;
     }
@@ -41,7 +42,9 @@
     if (!pmAstromReadWCS(invFPA, invChip, invHDU->header, 1.0)) {
         psError(psErrorCodeLast(), false, "Unable to read WCS astrometry.");
+	psFree(view);
         return false;
     }
 
+    psFree(view);
     return true;
 }
Index: /trunk/ppSub/src/ppSubReadoutJpeg.c
===================================================================
--- /trunk/ppSub/src/ppSubReadoutJpeg.c	(revision 30618)
+++ /trunk/ppSub/src/ppSubReadoutJpeg.c	(revision 30619)
@@ -139,4 +139,5 @@
     }
 
+    psFree(view);
     return true;
 }
Index: /trunk/ppSub/src/ppSubReadoutPhotometry.c
===================================================================
--- /trunk/ppSub/src/ppSubReadoutPhotometry.c	(revision 30618)
+++ /trunk/ppSub/src/ppSubReadoutPhotometry.c	(revision 30619)
@@ -109,12 +109,15 @@
         if (!psMetadataCopySingle(inRO->analysis, photRO->analysis, "PSPHOT.DETECTIONS")) {
             psError(PPSUB_ERR_PROG, false, "Unable to copy PSPHOT.DETECTIONS");
+	    psFree(view);
             return false;
         }
         if (!psMetadataCopySingle(inRO->analysis, photRO->analysis, "PSPHOT.HEADER")) {
             psError(PPSUB_ERR_PROG, false, "Unable to copy PSPHOT.HEADER");
+	    psFree(view);
             return false;
         }
         if (!psMetadataCopySingle(inRO->analysis, photRO->analysis, PM_DETEFF_ANALYSIS)) {
             psError(PPSUB_ERR_PROG, false, "Unable to copy Detection Efficiency");
+	    psFree(view);
             return false;
         }
@@ -128,9 +131,10 @@
     }
 
+    psFree(view);
     return true;
 }
 
 #ifdef TESTING
-    // Record data about sources: not everything gets into the output CMF files
+// Record data about sources: not everything gets into the output CMF files
     {
         pmReadout *photRO = pmFPAviewThisReadout(view, photFile->fpa); // Readout with the sources
Index: /trunk/ppSub/src/ppSubReadoutSubtract.c
===================================================================
--- /trunk/ppSub/src/ppSubReadoutSubtract.c	(revision 30618)
+++ /trunk/ppSub/src/ppSubReadoutSubtract.c	(revision 30619)
@@ -31,4 +31,5 @@
 
     bool reverse = psMetadataLookupBool(&mdok, config->arguments, "REVERSE"); // Reverse sense of subtraction?
+    bool addPair = psMetadataLookupBool(&mdok, recipe, "ADD.NOT.SUBTRACT"); // add instead of subtracting
 
     pmFPAview *view = ppSubViewReadout(); // View to readout
@@ -47,5 +48,10 @@
     // Do the actual subtraction
     pmReadout *outRO = pmFPAfileThisReadout(config->files, view, "PPSUB.OUTPUT");
-    outRO->image = (psImage*)psBinaryOp(outRO->image, minuend->image, "-", subtrahend->image);
+
+    if (addPair) {
+	outRO->image = (psImage*)psBinaryOp(outRO->image, minuend->image, "+", subtrahend->image);
+    } else {
+	outRO->image = (psImage*)psBinaryOp(outRO->image, minuend->image, "-", subtrahend->image);
+    }
     outRO->mask = (psImage*)psBinaryOp(outRO->mask, minuend->mask, "|", subtrahend->mask);
     outRO->variance = (psImage*)psBinaryOp(outRO->variance, minuend->variance, "+", subtrahend->variance);
@@ -70,4 +76,9 @@
     psFree(rng);
 
+    // XXX EAM : I suspect that the weighted averaging is giving the wrong answer for
+    // single-direction PSF matching (and maybe DUAL as well).  I am testing the difference by
+    // generating A+B images instead of A-B images and then checking the significance of the
+    // sources in the image
+
     // Combine the covariances
     // These are weighted by the appropriate mean variance.  This is probably not perfectly correct, but it
@@ -79,5 +90,9 @@
     covarWeights->data.F32[0] = minuendVar;
     covarWeights->data.F32[1] = subtrahendVar;
+# if (0)    
     outRO->covariance = psImageCovarianceAverageWeighted(covars, covarWeights);
+# else
+    outRO->covariance = psImageCovarianceAverage(covars);
+# endif
     psFree(covars);
     psFree(covarWeights);
@@ -109,4 +124,5 @@
     pmChip *outChip = pmFPAfileThisChip(config->files, view, "PPSUB.OUTPUT"); // Output chip
     psFree(view);
+
     if (!outHDU || !inHDU) {
         psError(PPSUB_ERR_PROG, true, "Unable to find HDU at FPA level to copy astrometry.");
Index: /trunk/ppSub/src/ppSubSetMasks.c
===================================================================
--- /trunk/ppSub/src/ppSubSetMasks.c	(revision 30618)
+++ /trunk/ppSub/src/ppSubSetMasks.c	(revision 30619)
@@ -80,8 +80,10 @@
     if (!pmReadoutMaskInvalid(inRO, maskValue, satValue)) {
         psError(PPSUB_ERR_DATA, false, "Unable to mask non-finite pixels in input.");
+	psFree(view);
         return false;
     }
     if (!pmReadoutMaskInvalid(refRO, maskValue, satValue)) {
         psError(PPSUB_ERR_DATA, false, "Unable to mask non-finite pixels in reference.");
+	psFree(view);
         return false;
     }
@@ -94,4 +96,5 @@
     if (interpMode == PS_INTERPOLATE_NONE) {
         psError(PPSUB_ERR_CONFIG, false, "Unknown interpolation mode: %s", interpModeStr);
+	psFree(view);
         return false;
     }
@@ -104,8 +107,10 @@
     if (!pmReadoutInterpolateBadPixels(inRO, maskVal, interpMode, poorFrac, maskPoor, maskBad)) {
         psError(PPSUB_ERR_DATA, false, "Unable to interpolate bad pixels for input image.");
+	psFree(view);
         return false;
     }
     if (!pmReadoutInterpolateBadPixels(refRO, maskVal, interpMode, poorFrac, maskPoor, maskBad)) {
         psError(PPSUB_ERR_DATA, false, "Unable to interpolate bad pixels for reference image.");
+	psFree(view);
         return false;
     }
@@ -113,4 +118,5 @@
 #endif
 
+    psFree(view);
     return true;
 }
Index: /trunk/ppSub/src/ppSubThreshold.c
===================================================================
--- /trunk/ppSub/src/ppSubThreshold.c	(revision 30618)
+++ /trunk/ppSub/src/ppSubThreshold.c	(revision 30619)
@@ -98,4 +98,5 @@
     if (!in) {
         psError(PPSUB_ERR_UNKNOWN, false, "Unable to find readout.");
+	psFree(view);
         return false;
     }
@@ -104,4 +105,5 @@
     if (!ref) {
         psError(PPSUB_ERR_UNKNOWN, false, "Unable to find readout.");
+	psFree(view);
         return false;
     }
@@ -115,8 +117,10 @@
         if (!lowThreshold(in, thresh, maskVal, maskThresh, region, "input convolved image")) {
             psError(psErrorCodeLast(), false, "Unable to threshold input image.");
+	    psFree(view);
             return false;
         }
         if (!lowThreshold(ref, thresh, maskVal, maskThresh, region, "reference convolved image")) {
             psError(psErrorCodeLast(), false, "Unable to threshold input image.");
+	    psFree(view);
             return false;
         }
Index: /trunk/ppSub/src/ppSubVarianceRescale.c
===================================================================
--- /trunk/ppSub/src/ppSubVarianceRescale.c	(revision 30618)
+++ /trunk/ppSub/src/ppSubVarianceRescale.c	(revision 30619)
@@ -57,4 +57,5 @@
         // Nothing to renormalise
         psWarning("Renormalisation of the variance requested, but no variance provided.");
+	psFree(view);
         return true;
     }
@@ -69,4 +70,5 @@
     }
 
+    psFree(view);
     return true;
 }
