Index: /trunk/ppSub/src/ppSub.c
===================================================================
--- /trunk/ppSub/src/ppSub.c	(revision 21523)
+++ /trunk/ppSub/src/ppSub.c	(revision 21524)
@@ -6,8 +6,17 @@
  *
  *  @author IfA
- *  @version $Revision: 1.11 $ $Name: not supported by cvs2svn $
- *  @date $Date: 2009-02-09 21:26:05 $
+ *  @version $Revision: 1.12 $ $Name: not supported by cvs2svn $
+ *  @date $Date: 2009-02-18 00:31:20 $
  *  Copyright 2009 Institute for Astronomy, University of Hawaii
  */
+
+#ifdef HAVE_CONFIG_H
+#include <config.h>
+#endif
+
+#include <stdio.h>
+#include <pslib.h>
+#include <psmodules.h>
+#include <psphot.h>
 
 #include "ppSub.h"
Index: /trunk/ppSub/src/ppSub.h
===================================================================
--- /trunk/ppSub/src/ppSub.h	(revision 21523)
+++ /trunk/ppSub/src/ppSub.h	(revision 21524)
@@ -6,6 +6,6 @@
  *
  *  @author IfA
- *  @version $Revision: 1.6 $ $Name: not supported by cvs2svn $
- *  @date $Date: 2009-02-06 01:37:17 $
+ *  @version $Revision: 1.7 $ $Name: not supported by cvs2svn $
+ *  @date $Date: 2009-02-18 00:31:20 $
  *  Copyright 2009 Institute for Astronomy, University of Hawaii
  */
@@ -14,14 +14,6 @@
 #define PP_SUB_H
 
-#ifdef HAVE_CONFIG_H
-#include <config.h>
-#endif
-
-#include <stdio.h>
-#include <string.h>
 #include <pslib.h>
 #include <psmodules.h>
-#include <psphot.h>
-#include <ppStats.h>
 
 /// @addtogroup ppSub
@@ -32,5 +24,5 @@
 /// Setup the arguments parsing
 bool ppSubArgumentsSetup(int argc, char *argv[], ///< Command-line arguments
-			 pmConfig *config    ///< Configuration
+                         pmConfig *config    ///< Configuration
     );
 
@@ -53,47 +45,41 @@
     );
 
-/// generate (if needed) and set or update the masks for input and reference images
-bool ppSubSetMasks (pmConfig *config,	  ///< Configuration
-		    const pmFPAview *view ///< View of active readout
+/// Generate (if needed) and set or update the masks for input and reference images
+bool ppSubSetMasks(pmConfig *config,     ///< Configuration
+                   const pmFPAview *view ///< View of active readout
     );
 
-/// Generate the PSF-matching kernel and convolve the images as needed.  Most of this function
-/// involves looking up the parameters in the recipe and supplying them to the function
-/// pmSubtractionMatch()
-bool ppSubMatchPSFs (pmConfig *config,	  ///< Configuration
-		     const pmFPAview *view ///< View of active readout
+/// Generate the PSF-matching kernel and convolve the images as needed.  Most of this function involves
+/// looking up the parameters in the recipe and supplying them to the function pmSubtractionMatch()
+bool ppSubMatchPSFs(pmConfig *config,    ///< Configuration
+                    const pmFPAview *view ///< View of active readout
     );
 
-/// generate the output readout and pass the kernel info to the header 
-bool ppSubDefineOutput(pmConfig *config,	  ///< Configuration
-		       const pmFPAview *view ///< View of active readout
+/// Generate the output readout and pass the kernel info to the header
+bool ppSubDefineOutput(pmConfig *config, ///< Configuration
+                       const pmFPAview *view ///< View of active readout
     );
 
-/// Calculate the variance factor for the output image based on the input images
-bool ppSubVarianceFactors(pmConfig *config,	  ///< Configuration
-			  psMetadata *stats,    ///< Statistics, for output
-			  const pmFPAview *view ///< View of active readout
-    );
-
-/// Photometry stage 1: measure the PSF from the minuend image 
-bool ppSubMakePSF(pmConfig *config,	  ///< Configuration
-		  const pmFPAview *view ///< View of active readout
+/// Photometry stage 1: measure the PSF from the minuend image
+bool ppSubMakePSF(pmConfig *config,       ///< Configuration
+                  const pmFPAview *view ///< View of active readout
     );
 
 /// Perform the actual image subtraction, update output concepts
-bool ppSubReadoutSubtract(pmConfig *config,	  ///< Configuration
-			  const pmFPAview *view ///< View of active readout
+bool ppSubReadoutSubtract(pmConfig *config,       ///< Configuration
+                          const pmFPAview *view ///< View of active readout
     );
 
 
 /// Photometry stage 2: find and measure sources on the subtracted image
-bool ppSubReadoutPhotometry(pmConfig *config,	  ///< Configuration
-			    psMetadata *stats,    ///< Statistics, for output
-			    const pmFPAview *view ///< View of active readout
+bool ppSubReadoutPhotometry(pmConfig *config,     ///< Configuration
+                            psMetadata *stats,    ///< Statistics, for output
+                            const pmFPAview *view ///< View of active readout
     );
 
 /// Renormalize, update headers and generate JPEGs
-bool ppSubReadoutUpdate(pmConfig *config,	  ///< Configuration
-			const pmFPAview *view ///< View of active readout
+bool ppSubReadoutUpdate(pmConfig *config, ///< Configuration
+                        psMetadata *stats, ///< Statistics for output, or NULL
+                        const pmFPAview *view ///< View of active readout
     );
 
@@ -107,23 +93,4 @@
     );
 
-/// Generate and Set the masks if needed
-bool ppSubSetMasks (
-    pmConfig *config,			///< Configuration
-    const pmFPAview *view 		///< view to readout
-    );
-
-/// Renormalize readout for peak pixels
-bool ppSubReadoutRenormPixels (
-    pmConfig *config, 			///< Configuration
-    psMetadata *recipe, 			///< Recipe
-    pmReadout *readout			///< Readout
-    );
-
-/// Renormalize readout for photometry analysis
-bool ppSubReadoutRenormPhot (
-    pmConfig *config, 			///< Configuration
-    psMetadata *recipe, 			///< Recipe
-    pmReadout *readout			///< Readout
-    );
 
 // Copy every instance of a single keyword from one metadata to another
Index: /trunk/ppSub/src/ppSubArguments.c
===================================================================
--- /trunk/ppSub/src/ppSubArguments.c	(revision 21523)
+++ /trunk/ppSub/src/ppSubArguments.c	(revision 21524)
@@ -6,8 +6,17 @@
  *
  *  @author IfA
- *  @version $Revision: 1.58 $ $Name: not supported by cvs2svn $
- *  @date $Date: 2009-02-09 21:26:05 $
+ *  @version $Revision: 1.59 $ $Name: not supported by cvs2svn $
+ *  @date $Date: 2009-02-18 00:31:20 $
  *  Copyright 2009 Institute for Astronomy, University of Hawaii
  */
+
+#ifdef HAVE_CONFIG_H
+#include <config.h>
+#endif
+
+#include <stdio.h>
+#include <pslib.h>
+#include <psmodules.h>
+#include <psphot.h>
 
 #include "ppSub.h"
@@ -240,11 +249,4 @@
     psMetadataAddS32(arguments, PS_LIST_TAIL, "-opt-order", 0, "Maximum order for optimum kernel search", -1);
     psMetadataAddBool(arguments, PS_LIST_TAIL, "-dual", 0, "Dual convolution", false);
-    psMetadataAddBool(arguments, PS_LIST_TAIL, "-renorm", 0, "Renormalise variance maps?", false);
-    psMetadataAddStr(arguments, PS_LIST_TAIL, "-renorm-mean", 0,
-                     "Statistic for mean in renormalisation", NULL);
-    psMetadataAddStr(arguments, PS_LIST_TAIL, "-renorm-stdev", 0,
-                     "Statistic for stdev in renormalisation", NULL);
-    psMetadataAddF32(arguments, PS_LIST_TAIL, "-renorm-width", 0, "Gaussian width for renormalisation", NAN);
-    psMetadataAddS32(arguments, PS_LIST_TAIL, "-renorm-num", 0, "Number of samples for renormalisation", 0);
     psMetadataAddBool(arguments, PS_LIST_TAIL, "-photometry", 0, "Perform photometry?", false);
     psMetadataAddS32(arguments, PS_LIST_TAIL, "-threads", 0, "Number of threads", 0);
@@ -352,13 +354,4 @@
                       psMetadataLookupBool(NULL, arguments, "-dual"));
 
-    if (psMetadataLookupBool(NULL, arguments, "-renorm") ||
-        psMetadataLookupBool(NULL, recipe, "RENORM")) {
-        psMetadataAddBool(arguments, PS_LIST_TAIL, "RENORM", 0, "Renormalise variance maps?", true);
-    }
-    VALUE_ARG_RECIPE_INT("-renorm-num", "RENORM.NUM", S32, 0);
-    VALUE_ARG_RECIPE_FLOAT("-renorm-width", "RENORM.WIDTH", F32);
-    valueArgRecipeStr(arguments, recipe, "-renorm-mean", "RENORM.MEAN", recipe);
-    valueArgRecipeStr(arguments, recipe, "-renorm-stdev", "RENORM.STDEV", recipe);
-
     // Need to update this because it could have been overwritten by the camera's own recipe
     if (psMetadataLookupBool(NULL, arguments, "-photometry")) {
Index: /trunk/ppSub/src/ppSubBackground.c
===================================================================
--- /trunk/ppSub/src/ppSubBackground.c	(revision 21523)
+++ /trunk/ppSub/src/ppSubBackground.c	(revision 21524)
@@ -6,36 +6,38 @@
  *
  *  @author IfA
- *  @version $Revision: 1.6 $ $Name: not supported by cvs2svn $
- *  @date $Date: 2009-02-06 01:37:17 $
+ *  @version $Revision: 1.7 $ $Name: not supported by cvs2svn $
+ *  @date $Date: 2009-02-18 00:31:20 $
  *  Copyright 2009 Institute for Astronomy, University of Hawaii
  */
 
+#ifdef HAVE_CONFIG_H
+#include <config.h>
+#endif
+
+#include <stdio.h>
+#include <pslib.h>
+#include <psmodules.h>
+#include <psphot.h>
+
 #include "ppSub.h"
 
-/**
- * Based on ppImageSubtractBackground()
- */
 bool ppSubBackground(pmConfig *config, const pmFPAview *view)
 {
-    psAssert(config, "Need configuration");
-    psAssert(view, "Need view to chip");
+    psAssert(config, "Require configuration");
+    psAssert(view, "Require view");
 
-    bool status; // Status of metadata lookups
+    bool mdok; // Status of metadata lookups
 
-    psMetadata *ppSubRecipe = psMetadataLookupPtr(NULL, config->recipes, PPSUB_RECIPE);
+    psMetadata *ppSubRecipe = psMetadataLookupPtr(NULL, config->recipes, PPSUB_RECIPE); // Recipe for ppSub
     psAssert(ppSubRecipe, "Need PPSUB recipe");
-
-    psMetadata *psphotRecipe = psMetadataLookupPtr(NULL, config->recipes, PSPHOT_RECIPE);
+    psMetadata *psphotRecipe = psMetadataLookupPtr(NULL, config->recipes, PSPHOT_RECIPE); // Recipe for psphot
     psAssert(psphotRecipe, "Need PSPHOT recipe for binning");
 
-    psImageMaskType maskBad = pmConfigMaskGet("BLANK", config); 
+    psImageMaskType maskBad = pmConfigMaskGet("BLANK", config); // Bits to mask
 
-    // select the output readout
-    pmReadout *outRO = pmFPAfileThisReadout(config->files, view, "PPSUB.OUTPUT");
+    pmReadout *outRO = pmFPAfileThisReadout(config->files, view, "PPSUB.OUTPUT"); // Output image
+    pmReadout *modelRO = pmFPAfileThisReadout(config->files, view, "PSPHOT.BACKMDL"); // Background model
 
-    // select the model readout, if it exist already; if not, generate it
-    pmReadout *modelRO = pmFPAfileThisReadout(config->files, view, "PSPHOT.BACKMDL");
-
-    // if necessary, generate the background model
+    // Generate the background model, if required
     if (!modelRO) {
         // Create the background model
@@ -44,6 +46,6 @@
             return false;
         }
-	// select the model readout (should now exist)
-	modelRO = pmFPAfileThisReadout(config->files, view, "PSPHOT.BACKMDL");
+        // select the model readout (should now exist)
+        modelRO = pmFPAfileThisReadout(config->files, view, "PSPHOT.BACKMDL");
         if (!modelRO) {
             psError(PS_ERR_UNEXPECTED_NULL, true, "Unable to find background model");
@@ -51,8 +53,7 @@
         }
     }
-    psImageBinning *binning = psMetadataLookupPtr(&status, psphotRecipe,
+    psImageBinning *binning = psMetadataLookupPtr(&mdok, psphotRecipe,
                                                   "PSPHOT.BACKGROUND.BINNING"); // Binning for model
     psImage *modelImage = modelRO->image; // Background model
-
     psImage *image = outRO->image; // Image of interest
     psImage *mask = outRO->mask; // Mask of interest
Index: /trunk/ppSub/src/ppSubCamera.c
===================================================================
--- /trunk/ppSub/src/ppSubCamera.c	(revision 21523)
+++ /trunk/ppSub/src/ppSubCamera.c	(revision 21524)
@@ -6,8 +6,17 @@
  *
  *  @author IfA
- *  @version $Revision: 1.33 $ $Name: not supported by cvs2svn $
- *  @date $Date: 2009-02-06 03:20:02 $
+ *  @version $Revision: 1.34 $ $Name: not supported by cvs2svn $
+ *  @date $Date: 2009-02-18 00:31:20 $
  *  Copyright 2009 Institute for Astronomy, University of Hawaii
  */
+
+#ifdef HAVE_CONFIG_H
+#include <config.h>
+#endif
+
+#include <stdio.h>
+#include <pslib.h>
+#include <psmodules.h>
+#include <psphot.h>
 
 #include "ppSub.h"
@@ -257,8 +266,7 @@
     // psPhot input
     if (psMetadataLookupBool(NULL, recipe, "PHOTOMETRY")) {
-        psphotModelClassInit ();        // load implementation-specific models
-
-
-        // Internal-ish file for getting the PSF from the matched addition
+        psphotModelClassInit();        // load implementation-specific models
+
+        // Internal-ish file for getting the PSF from the minuend
         pmFPAfile *psf = pmFPAfileDefineOutputFromFile(config, output, "PSPHOT.PSF.LOAD");
         if (!psf) {
@@ -272,15 +280,4 @@
         pmFPAfileActivate(config->files, false, "PSPHOT.PSF.LOAD");
 
-        pmFPAfile *psphotResid = pmFPAfileDefineOutputForFormat (config, NULL, "PSPHOT.RESID", output->cameraName, output->formatName);
-        if (!psphotResid) {
-            psError(PSPHOT_ERR_CONFIG, false, "Cannot find a rule for PSPHOT.RESID");
-            return false;
-        }
-        // make this optional: psphotResid->save = true;
-    }
-
-    // Always do this, since we're using psphot for the background model.
-    // XXX Not certain that I need to generate a separate pmFPAfile for PSPHOT.INPUT
-    if (0) {
         pmFPAfile *psphot = pmFPAfileDefineFromFPA(config, output->fpa, 1, 1, "PSPHOT.INPUT");
         if (!psphot) {
@@ -297,5 +294,4 @@
             return false;
         }
-
     }
 
Index: /trunk/ppSub/src/ppSubDefineOutput.c
===================================================================
--- /trunk/ppSub/src/ppSubDefineOutput.c	(revision 21523)
+++ /trunk/ppSub/src/ppSubDefineOutput.c	(revision 21524)
@@ -6,16 +6,25 @@
  *
  *  @author IfA
- *  @version $Revision: 1.2 $ $Name: not supported by cvs2svn $
- *  @date $Date: 2009-02-06 01:37:17 $
+ *  @version $Revision: 1.3 $ $Name: not supported by cvs2svn $
+ *  @date $Date: 2009-02-18 00:31:20 $
  *  Copyright 2009 Institute for Astronomy, University of Hawaii
  */
 
+#ifdef HAVE_CONFIG_H
+#include <config.h>
+#endif
+
+#include <stdio.h>
+#include <pslib.h>
+#include <psmodules.h>
+
 #include "ppSub.h"
 
-bool ppSubDefineOutput (pmConfig *config, const pmFPAview *view) {
+bool ppSubDefineOutput(pmConfig *config, const pmFPAview *view)
+{
+    psAssert(config, "Require configuration");
+    psAssert(view, "Require view");
 
-    bool mdok;
-
-    // generate an output readout 
+    // generate an output readout
     pmCell *outCell = pmFPAfileThisCell(config->files, view, "PPSUB.OUTPUT"); // Output cell
     pmReadout *outRO = pmReadoutAlloc(outCell); // Output readout: subtraction
@@ -30,11 +39,13 @@
     pmReadout *refConv = pmFPAfileThisReadout(config->files, view, "PPSUB.REF.CONV"); // Reference readout
 
-    // Add kernel descrption to header.  We don't know which readout gets the kernels because
-    // it depends on which is larger (the choice is set in ppSubMatchPSFs)
-    bool inputHasKernel = true;
-    pmSubtractionKernels *kernels = psMetadataLookupPtr(&mdok, inConv->analysis, PM_SUBTRACTION_ANALYSIS_KERNEL); // Subtraction kernel
+    // Add kernel descrption to header.
+    // We don't know which readout has the kernels because it depends on which PSF is larger
+    bool mdok;                          // Status of MD lookup
+    psMetadata *analysis = inConv->analysis; // Analysis metadata with kernel information
+    pmSubtractionKernels *kernels = psMetadataLookupPtr(&mdok, analysis,
+                                                        PM_SUBTRACTION_ANALYSIS_KERNEL); // Subtraction kernel
     if (!kernels) {
-        kernels = psMetadataLookupPtr(&mdok, refConv->analysis, PM_SUBTRACTION_ANALYSIS_KERNEL);
-	inputHasKernel = false;
+        analysis = refConv->analysis;
+        kernels = psMetadataLookupPtr(&mdok, analysis, PM_SUBTRACTION_ANALYSIS_KERNEL);
     }
     if (!kernels) {
@@ -45,24 +56,17 @@
         return false;
     }
-    psMetadataAddStr(outHDU->header, PS_LIST_TAIL, "PPSUB.KERNEL", 0, "Subtraction kernel", kernels->description);
+    psMetadataAddStr(outHDU->header, PS_LIST_TAIL, "PPSUB.KERNEL", 0, "Subtraction kernel",
+                     kernels->description);
 
-    if (inputHasKernel) {
-	outRO->analysis = psMetadataCopy(outRO->analysis, inConv->analysis);
-    } else {
-	outRO->analysis = psMetadataCopy(outRO->analysis, refConv->analysis);
-    } 
+    outRO->analysis = psMetadataCopy(outRO->analysis, analysis);
 
 #ifdef TESTING
-    psImage *kernelImage = NULL;
-
-    if (inputHasKernel) {
-	kernelImage = psMetadataLookupPtr(&mdok, inConv->analysis, "SUBTRACTION.KERNEL.IMAGE"); // Image of the kernels
-    } else {
-        kernelImage = psMetadataLookupPtr(&mdok, refConv->analysis, "SUBTRACTION.KERNEL.IMAGE");
+    {
+        psImage *kernelImage = psMetadataLookupPtr(&mdok, analysis,
+                                                   "SUBTRACTION.KERNEL.IMAGE"); // Image of kernel
+        psFits *fits = psFitsOpen("kernel.fits", "w");
+        psFitsWriteImage(fits, NULL, kernelImage, 0, NULL);
+        psFitsClose(fits);
     }
-
-    psFits *fits = psFitsOpen("kernel.fits", "w");
-    psFitsWriteImage(fits, NULL, kernelImage, 0, NULL);
-    psFitsClose(fits);
 #endif
 
Index: /trunk/ppSub/src/ppSubLoop.c
===================================================================
--- /trunk/ppSub/src/ppSubLoop.c	(revision 21523)
+++ /trunk/ppSub/src/ppSubLoop.c	(revision 21524)
@@ -6,8 +6,16 @@
  *
  *  @author IfA
- *  @version $Revision: 1.23 $ $Name: not supported by cvs2svn $
- *  @date $Date: 2009-02-06 01:37:17 $
+ *  @version $Revision: 1.24 $ $Name: not supported by cvs2svn $
+ *  @date $Date: 2009-02-18 00:31:20 $
  *  Copyright 2009 Institute for Astronomy, University of Hawaii
  */
+
+#ifdef HAVE_CONFIG_H
+#include <config.h>
+#endif
+
+#include <pslib.h>
+#include <psmodules.h>
+#include <ppStats.h>
 
 #include "ppSub.h"
@@ -138,5 +146,5 @@
                     return false;
                 }
-		psImageMaskType maskValue = pmConfigMaskGet("MASK.VALUE", config);
+                psImageMaskType maskValue = pmConfigMaskGet("MASK.VALUE", config);
                 ppStatsFPA(stats, output->fpa, view, maskValue, config);
             }
Index: /trunk/ppSub/src/ppSubMakePSF.c
===================================================================
--- /trunk/ppSub/src/ppSubMakePSF.c	(revision 21523)
+++ /trunk/ppSub/src/ppSubMakePSF.c	(revision 21524)
@@ -6,61 +6,84 @@
  *
  *  @author IfA
- *  @version $Revision: 1.2 $ $Name: not supported by cvs2svn $
- *  @date $Date: 2009-02-06 01:37:17 $
+ *  @version $Revision: 1.3 $ $Name: not supported by cvs2svn $
+ *  @date $Date: 2009-02-18 00:31:20 $
  *  Copyright 2009 Institute for Astronomy, University of Hawaii
  */
 
+#ifdef HAVE_CONFIG_H
+#include <config.h>
+#endif
+
+#include <stdio.h>
+#include <pslib.h>
+#include <psmodules.h>
+#include <psphot.h>
+
 #include "ppSub.h"
 
-/**
- * Photometry stage 1: measure the PSF from the minuend image 
- */
-bool ppSubMakePSF (pmConfig *config, const pmFPAview *view) {
+bool ppSubMakePSF(pmConfig *config, const pmFPAview *view)
+{
+    psAssert(config, "Require configuration");
+    psAssert(view, "Require view");
 
-    bool mdok = false;
-
-    // Photometry is to be performed in two stages:
-    // 1. Measure the PSF using the PSF-matched images
-    // 2. Find and measure sources on the subtracted image
     psTimerStart("PPSUB_PHOT");
 
-    // Look up recipe values
     psMetadata *recipe = psMetadataLookupMetadata(NULL, config->recipes, PPSUB_RECIPE); // Recipe for ppSub
     psAssert(recipe, "We checked this earlier, so it should be here.");
 
-    psMetadata *psphotRecipe = psMetadataLookupMetadata(NULL, config->recipes, PSPHOT_RECIPE); // Recipe for psphot
+    if (!psMetadataLookupBool(NULL, recipe, "PHOTOMETRY")) {
+        return true;
+    }
+
+    psMetadata *psphotRecipe = psMetadataLookupMetadata(NULL, config->recipes, PSPHOT_RECIPE);// psphot recipe
     psAssert(recipe, "We checked this earlier, so it should be here.");
-
-    if (!psMetadataLookupBool(NULL, recipe, "PHOTOMETRY")) return true;
 
     bool reverse = psMetadataLookupBool(NULL, config->arguments, "REVERSE"); // Reverse sense of subtraction?
 
-    pmReadout *minuend = NULL;
-    pmFPAfile *minuendFile = NULL;
+    bool mdok = false;                  // Status of MD lookup
+    pmReadout *minuend = NULL;          // Image that will be positive following subtraction
+    pmFPAfile *minuendFile = NULL;      // File for minuend image
     if (reverse) {
-	minuend = pmFPAfileThisReadout(config->files, view, "PPSUB.REF.CONV");
-	minuendFile = psMetadataLookupPtr(&mdok, config->files, "PPSUB.REF.CONV");
+        minuend = pmFPAfileThisReadout(config->files, view, "PPSUB.REF.CONV");
+        minuendFile = psMetadataLookupPtr(&mdok, config->files, "PPSUB.REF.CONV");
     } else {
-	minuend = pmFPAfileThisReadout(config->files, view, "PPSUB.INPUT.CONV");
-	minuendFile = psMetadataLookupPtr(&mdok, config->files, "PPSUB.INPUT.CONV");
+        minuend = pmFPAfileThisReadout(config->files, view, "PPSUB.INPUT.CONV");
+        minuendFile = psMetadataLookupPtr(&mdok, config->files, "PPSUB.INPUT.CONV");
     }
 
-    // supply the minuend pmFPAfile to psphot as PSPHOT.INPUT:
-    psMetadataAddPtr (config->files, PS_LIST_TAIL, "PSPHOT.INPUT", PS_DATA_UNKNOWN | PS_META_REPLACE, "psphot input : view on another pmFPAfile", minuendFile);
+#if 1
+    pmReadout *template = minuend;
+    pmFPAfile *photFile = psMetadataLookupPtr(&mdok, config->files, "PSPHOT.INPUT"); // Photometry file
+    pmReadout *photRO = pmFPAviewThisReadout(view, photFile->fpa); // Readout to photometer
+    if (!photRO) {
+        pmCell *cell = pmFPAviewThisCell(view, photFile->fpa); // Cell to photometer
+        photRO = pmReadoutAlloc(cell); // Output readout: subtraction
+    }
+    photRO->image = psImageCopy(photRO->image, template->image, PS_TYPE_F32);
+    if (template->variance) {
+        photRO->variance = psImageCopy(photRO->variance, template->variance, PS_TYPE_F32);
+    } else {
+        psFree(photRO->variance);
+        photRO->variance = NULL;
+    }
+    if (template->mask) {
+        photRO->mask = psImageCopy(photRO->mask, template->mask, PS_TYPE_IMAGE_MASK);
+    } else {
+        psFree(photRO->mask);
+        photRO->mask = NULL;
+    }
+#else
+    // Supply the minuend pmFPAfile to psphot as PSPHOT.INPUT:
+    psMetadataAddPtr(config->files, PS_LIST_TAIL, "PSPHOT.INPUT", PS_DATA_UNKNOWN | PS_META_REPLACE,
+                     "psphot input: view on another pmFPAfile", minuendFile);
+#endif
 
-    // old-style variance renormalization
-    if (!ppSubReadoutRenormPhot (config, recipe, minuend)) {
-	psError(PS_ERR_UNKNOWN, false, "failure in renormalization");
-	return false;
-    }
-
-    // extract the loaded sources from the associated readout
+    // Extract the loaded sources from the associated readout, and generate PSF
+    // Here, we assume the image is background-subtracted
     pmReadout *sourcesRO = pmFPAfileThisReadout(config->files, view, "PPSUB.SOURCES");
     psArray *sources = psMetadataLookupPtr(&mdok, sourcesRO->analysis, "PSPHOT.SOURCES");
-
-    // generate PSF from the supplied sources (assumes image is background-subtracted)
     if (!psphotReadoutFindPSF(config, view, sources)) {
-	psError(PS_ERR_UNKNOWN, false, "Unable to perform photometry on subtracted image.");
-	return false;
+        psError(PS_ERR_UNKNOWN, false, "Unable to perform photometry on subtracted image.");
+        return false;
     }
 
@@ -70,4 +93,7 @@
     psMetadataItemSupplement(hdu->header, psphotRecipe, "FWHM_MAJ");
     psMetadataItemSupplement(hdu->header, psphotRecipe, "FWHM_MIN");
+
+    // Get rid of the generated header; it will be regenerated by the real photometry run
+    psMetadataRemoveKey(photRO->analysis, "PSPHOT.HEADER");
 
     return true;
@@ -87,5 +113,5 @@
 
 // Blow away the sources psphot found --- they're irrelevant for the subtraction
-// XXX is this still needed?  These are now probably not being set 
+// XXX is this still needed?  These are now probably not being set
 // pmReadout *photRO = pmFPAviewThisReadout(view, photFile->fpa); // Readout with sources
 // psMetadataRemoveKey(photRO->analysis, "PSPHOT.SOURCES");
Index: /trunk/ppSub/src/ppSubMatchPSFs.c
===================================================================
--- /trunk/ppSub/src/ppSubMatchPSFs.c	(revision 21523)
+++ /trunk/ppSub/src/ppSubMatchPSFs.c	(revision 21524)
@@ -6,19 +6,24 @@
  *
  *  @author IfA
- *  @version $Revision: 1.2 $ $Name: not supported by cvs2svn $
- *  @date $Date: 2009-02-06 01:37:17 $
+ *  @version $Revision: 1.3 $ $Name: not supported by cvs2svn $
+ *  @date $Date: 2009-02-18 00:31:20 $
  *  Copyright 2009 Institute for Astronomy, University of Hawaii
  */
 
+#ifdef HAVE_CONFIG_H
+#include <config.h>
+#endif
+
+#include <stdio.h>
+#include <pslib.h>
+#include <psmodules.h>
+#include <psphot.h>
+
 #include "ppSub.h"
 
-/** Generate the PSF-matching kernel and convolve the images as needed.  Most of this function
- * involves looking up the parameters in the recipe and supplying them to the function
- * pmSubtractionMatch();
- */
-
-bool ppSubMatchPSFs (pmConfig *config, const pmFPAview *view) {
-
-    bool mdok;                          // Status of MD lookup
+bool ppSubMatchPSFs(pmConfig *config, const pmFPAview *view)
+{
+    psAssert(config, "Require configuration");
+    psAssert(view, "Require view");
 
     // Look up recipe values
@@ -26,35 +31,25 @@
     psAssert(recipe, "We checked this earlier, so it should be here.");
 
-    // input images
+    // Input images
     pmReadout *inRO = pmFPAfileThisReadout(config->files, view, "PPSUB.INPUT"); // Input readout
     pmReadout *refRO = pmFPAfileThisReadout(config->files, view, "PPSUB.REF"); // Reference readout
 
-    // get or generate output image holders
-    pmReadout *inConv = pmFPAfileThisReadout(config->files, view, "PPSUB.INPUT.CONV"); // Input convolved readout
+    // Output image holders
+    pmReadout *inConv = pmFPAfileThisReadout(config->files, view, "PPSUB.INPUT.CONV"); // Input convolved
     if (!inConv) {
-	pmCell *cell = pmFPAfileThisCell(config->files, view, "PPSUB.INPUT.CONV"); // Input convolved readout
-	inConv = pmReadoutAlloc(cell); // Convolved version of input
+        pmCell *cell = pmFPAfileThisCell(config->files, view, "PPSUB.INPUT.CONV"); // Cell for convolved input
+        inConv = pmReadoutAlloc(cell);
     }
-    pmReadout *refConv = pmFPAfileThisReadout(config->files, view, "PPSUB.REF.CONV"); // Reference convolved readout
+    pmReadout *refConv = pmFPAfileThisReadout(config->files, view, "PPSUB.REF.CONV"); // Reference convolved
     if (!refConv) {
-	pmCell *cell = pmFPAfileThisCell(config->files, view, "PPSUB.REF.CONV"); // Input convolved readout
-	refConv = pmReadoutAlloc(cell); // Convolved version of input
+        pmCell *cell = pmFPAfileThisCell(config->files, view, "PPSUB.REF.CONV"); // Cell for convolved ref.
+        refConv = pmReadoutAlloc(cell);
     }
 
-    // options which control the generation of optimal parameters
-    bool optimum = psMetadataLookupBool(&mdok, recipe, "OPTIMUM"); // Derive optimum parameters?
-    float optMin = psMetadataLookupF32(&mdok, recipe, "OPTIMUM.MIN"); // Minimum width for search
-    float optMax = psMetadataLookupF32(&mdok, recipe, "OPTIMUM.MAX"); // Maximum width for search
-    float optStep = psMetadataLookupF32(&mdok, recipe, "OPTIMUM.STEP"); // Step for search
+    bool mdok;                          // Status of MD lookup
 
-    // Vector with FWHMs for optimum search
-    psVector *optWidths = NULL;
-    if (optimum) {
-        optWidths = psVectorCreate(optWidths, optMin, optMax, optStep, PS_TYPE_F32);
-    }
-
-    // Sources in image : these must be loaded from previous analysis stages
-    pmReadout *sourcesRO = pmFPAfileThisReadout(config->files, view, "PPSUB.SOURCES"); // Readout with sources 
-    psArray *sources = NULL;            
+    // Sources in image, used for stamps: these must be loaded from previous analysis stages
+    pmReadout *sourcesRO = pmFPAfileThisReadout(config->files, view, "PPSUB.SOURCES"); // Readout with sources
+    psArray *sources = NULL;            // Sources in image; used for stamps
     if (sourcesRO) {
         sources = psMetadataLookupPtr(&mdok, sourcesRO->analysis, "PSPHOT.SOURCES");
@@ -66,10 +61,8 @@
     float spacing = psMetadataLookupF32(NULL, recipe, "STAMP.SPACING"); // Typical stamp spacing
     float threshold = psMetadataLookupF32(NULL, recipe, "STAMP.THRESHOLD"); // Threshold for stmps
-
     const char *stampsName = psMetadataLookupStr(&mdok, config->arguments, "STAMPS"); // Filename for stamps
 
     const char *typeStr = psMetadataLookupStr(NULL, recipe, "KERNEL.TYPE"); // Kernel type
     psAssert(typeStr, "We put it here in ppSubArguments.c");
-
     pmSubtractionKernelsType type = pmSubtractionKernelsTypeFromString(typeStr); // Type of kernel
     if (type == PM_SUBTRACTION_KERNEL_NONE) {
@@ -87,8 +80,4 @@
     int binning = psMetadataLookupS32(NULL, recipe, "SPAM.BINNING"); // Binning for SPAM kernel
     float penalty = psMetadataLookupF32(NULL, recipe, "PENALTY"); // Penalty for wideness
-
-    int optOrder = psMetadataLookupS32(&mdok, recipe, "OPTIMUM.ORDER"); // Order for search
-    float optThresh = psMetadataLookupF32(&mdok, recipe, "OPTIMUM.TOL"); // Tolerance for search
-
     int iter = psMetadataLookupS32(NULL, recipe, "ITER"); // Rejection iterations
     float rej = psMetadataLookupF32(NULL, recipe, "REJ"); // Rejection threshold
@@ -98,6 +87,18 @@
     float poorFrac = psMetadataLookupF32(&mdok, recipe, "POOR.FRACTION"); // Fraction for "poor"
 
-    // XXX EAM : do we need to / want to define different values for BAD and POOR subtraction vs BAD and POOR warp?
-    psImageMaskType maskVal = pmConfigMaskGet("MASK.VALUE", config); // Bits to mask going in to pmSubtractionMatch
+    // Options which control the generation of optimal parameters
+    bool optimum = psMetadataLookupBool(&mdok, recipe, "OPTIMUM"); // Derive optimum parameters?
+    float optMin = psMetadataLookupF32(&mdok, recipe, "OPTIMUM.MIN"); // Minimum width for search
+    float optMax = psMetadataLookupF32(&mdok, recipe, "OPTIMUM.MAX"); // Maximum width for search
+    float optStep = psMetadataLookupF32(&mdok, recipe, "OPTIMUM.STEP"); // Step for search
+    psVector *optWidths = NULL;         // Vector with FWHMs for optimum search
+    if (optimum) {
+        optWidths = psVectorCreate(optWidths, optMin, optMax, optStep, PS_TYPE_F32);
+    }
+    int optOrder = psMetadataLookupS32(&mdok, recipe, "OPTIMUM.ORDER"); // Order for search
+    float optThresh = psMetadataLookupF32(&mdok, recipe, "OPTIMUM.TOL"); // Tolerance for search
+
+    // XXX Do we need/want to define different values for BAD and POOR subtraction vs BAD and POOR warp?
+    psImageMaskType maskVal = pmConfigMaskGet("MASK.VALUE", config); // Bits to mask in inputs
     psImageMaskType maskPoor = pmConfigMaskGet("POOR.WARP", config); // Bits to mask for poor pixels
     psImageMaskType maskBad = pmConfigMaskGet("BLANK", config); // Bits to mask for bad pixels
@@ -124,4 +125,7 @@
     pmSubtractionThreadsFinalize(inRO, refRO);
 
+    psImageCovarianceTransfer(inConv->variance, inConv->covariance);
+    psImageCovarianceTransfer(refConv->variance, refConv->covariance);
+
     // XXX drop the pixels associated with inRO and refRO (now that we have inConv and refConf)
 
Index: /trunk/ppSub/src/ppSubReadout.c
===================================================================
--- /trunk/ppSub/src/ppSubReadout.c	(revision 21523)
+++ /trunk/ppSub/src/ppSubReadout.c	(revision 21524)
@@ -6,8 +6,16 @@
  *
  *  @author IfA
- *  @version $Revision: 1.112 $ $Name: not supported by cvs2svn $
- *  @date $Date: 2009-02-09 21:26:05 $
+ *  @version $Revision: 1.113 $ $Name: not supported by cvs2svn $
+ *  @date $Date: 2009-02-18 00:31:20 $
  *  Copyright 2009 Institute for Astronomy, University of Hawaii
  */
+
+#ifdef HAVE_CONFIG_H
+#include <config.h>
+#endif
+
+#include <stdio.h>
+#include <pslib.h>
+#include <psmodules.h>
 
 #include "ppSub.h"
@@ -17,31 +25,26 @@
     psTimerStart("PPSUB_MATCH");
 
-    if (!ppSubSetMasks (config, view)) {
-        psError(PS_ERR_UNKNOWN, false, "Unable to subtract background.");
+    if (!ppSubSetMasks(config, view)) {
+        psError(PS_ERR_UNKNOWN, false, "Unable to set masks.");
         return false;
     }
 
-    if (!ppSubMatchPSFs (config, view)) {
-        psError(PS_ERR_UNKNOWN, false, "Unable to subtract background.");
+    if (!ppSubMatchPSFs(config, view)) {
+        psError(PS_ERR_UNKNOWN, false, "Unable to match PSFs.");
         return false;
     }
 
-    if (!ppSubDefineOutput (config, view)) {
-        psError(PS_ERR_UNKNOWN, false, "Unable to subtract background.");
-        return false;
-    }
-
-    if (!ppSubVarianceFactors (config, stats, view)) {
-        psError(PS_ERR_UNKNOWN, false, "Unable to subtract background.");
+    if (!ppSubDefineOutput(config, view)) {
+        psError(PS_ERR_UNKNOWN, false, "Unable to define output.");
         return false;
     }
 
     if (!ppSubMakePSF(config, view)) {
-        psError(PS_ERR_UNKNOWN, false, "Unable to subtract background.");
+        psError(PS_ERR_UNKNOWN, false, "Unable to generate PSF.");
         return false;
     }
 
-    if (!ppSubReadoutSubtract (config, view)) {
-        psError(PS_ERR_UNKNOWN, false, "Unable to subtract background.");
+    if (!ppSubReadoutSubtract(config, view)) {
+        psError(PS_ERR_UNKNOWN, false, "Unable to subtract images.");
         return false;
     }
@@ -53,11 +56,11 @@
     }
 
-    if (!ppSubReadoutPhotometry (config, stats, view)) {
-        psError(PS_ERR_UNKNOWN, false, "Unable to subtract background.");
+    if (!ppSubReadoutPhotometry(config, stats, view)) {
+        psError(PS_ERR_UNKNOWN, false, "Unable to perform photometry.");
         return false;
     }
 
-    if (!ppSubReadoutUpdate (config, view)) {
-        psError(PS_ERR_UNKNOWN, false, "Unable to subtract background.");
+    if (!ppSubReadoutUpdate(config, stats, view)) {
+        psError(PS_ERR_UNKNOWN, false, "Unable to update.");
         return false;
     }
Index: /trunk/ppSub/src/ppSubReadoutPhotometry.c
===================================================================
--- /trunk/ppSub/src/ppSubReadoutPhotometry.c	(revision 21523)
+++ /trunk/ppSub/src/ppSubReadoutPhotometry.c	(revision 21524)
@@ -6,17 +6,24 @@
  *
  *  @author IfA
- *  @version $Revision: 1.3 $ $Name: not supported by cvs2svn $
- *  @date $Date: 2009-02-06 03:20:02 $
+ *  @version $Revision: 1.4 $ $Name: not supported by cvs2svn $
+ *  @date $Date: 2009-02-18 00:31:20 $
  *  Copyright 2009 Institute for Astronomy, University of Hawaii
  */
 
+#ifdef HAVE_CONFIG_H
+#include <config.h>
+#endif
+
+#include <stdio.h>
+#include <pslib.h>
+#include <psmodules.h>
+#include <psphot.h>
+
 #include "ppSub.h"
 
-/**
- * Photometry stage 2: find and measure sources on the subtracted image
- */
-bool ppSubReadoutPhotometry (pmConfig *config, psMetadata *stats, const pmFPAview *view) {
-
-    bool mdok = false;
+bool ppSubReadoutPhotometry (pmConfig *config, psMetadata *stats, const pmFPAview *view)
+{
+    psAssert(config, "Require configuration");
+    psAssert(view, "Require view");
 
     // Look up recipe values
@@ -32,28 +39,29 @@
     // The PSF (measured in ppSubMakePSF) is stored on the chip->analysis of PSPHOT.INPUT
     // In order to use an incoming PSF, it must be stored on the chip->analysis of PSPHOT.PSF.LOAD
-    pmChip *psfInputChip = pmFPAfileThisChip(config->files, view, "PSPHOT.INPUT");
+    pmChip *psfInputChip = pmFPAfileThisChip(config->files, view, "PSPHOT.INPUT"); // Chip with PSF
     psAssert (psfInputChip, "should have been generated for ppSubMakePSF");
-
-    pmChip *psfLoadChip = pmFPAfileThisChip(config->files, view, "PSPHOT.PSF.LOAD");
+    pmChip *psfLoadChip = pmFPAfileThisChip(config->files, view, "PSPHOT.PSF.LOAD"); // Chip to have PSF
     psAssert (psfLoadChip, "PSPHOT.PSF.LOAD should have been defined in ppSubCamera");
-
-    pmPSF *psf = psMetadataLookupPtr(NULL, psfInputChip->analysis, "PSPHOT.PSF");
+    pmPSF *psf = psMetadataLookupPtr(NULL, psfInputChip->analysis, "PSPHOT.PSF"); // PSF for photometry
     if (!psf) {
         psError(PS_ERR_UNEXPECTED_NULL, true, "Unable to find PSF from psphot");
         return false;
     }
-    psMetadataAddPtr(psfLoadChip->analysis, PS_LIST_TAIL, "PSPHOT.PSF", PS_DATA_UNKNOWN | PS_META_REPLACE, "PSF from matched addition", psf);
+    psMetadataAddPtr(psfLoadChip->analysis, PS_LIST_TAIL, "PSPHOT.PSF", PS_DATA_UNKNOWN | PS_META_REPLACE,
+                     "PSF from ppSubMakePSF", psf);
+
+    bool mdok = false;
 
     // psphotReadoutMinimal performs the photometry analysis on PSPHOT.INPUT; we need to move
     // around the pointers so PSPHOT.INPUT corresponds to the output image; previously, it was
     // equivalent to the minuend image.
-    pmFPAfile *outputFile = psMetadataLookupPtr(&mdok, config->files, "PPSUB.OUTPUT");
+    pmFPAfile *outputFile = psMetadataLookupPtr(&mdok, config->files, "PPSUB.OUTPUT"); // Output file
     pmReadout *outRO = pmFPAviewThisReadout(view, outputFile->fpa); // Readout with the sources
 
     // XXX possibly rename this to PPSUB.RESID?
-    pmFPAfile *photFile = psMetadataLookupPtr(&mdok, config->files, "PSPHOT.RESID");
-    pmReadout *photRO = pmFPAviewThisReadout(view, photFile->fpa); // Readout with the sources
+    pmFPAfile *photFile = psMetadataLookupPtr(&mdok, config->files, "PSPHOT.INPUT"); // Photometry file
+    pmReadout *photRO = pmFPAviewThisReadout(view, photFile->fpa); // Readout to photometer
     if (!photRO) {
-        pmCell *cell = pmFPAfileThisCell(config->files, view, "PSPHOT.RESID"); // Output cell
+        pmCell *cell = pmFPAviewThisCell(view, photFile->fpa); // Cell to photometer
         photRO = pmReadoutAlloc(cell); // Output readout: subtraction
     }
@@ -62,5 +70,5 @@
         photRO->variance = psImageCopy(photRO->variance, outRO->variance, PS_TYPE_F32);
     } else {
-        psFree (photRO->variance);
+        psFree(photRO->variance);
         photRO->variance = NULL;
     }
@@ -68,16 +76,12 @@
         photRO->mask = psImageCopy(photRO->mask, outRO->mask, PS_TYPE_IMAGE_MASK);
     } else {
-        psFree (photRO->mask);
+        psFree(photRO->mask);
         photRO->mask = NULL;
     }
 
-    // pmFPAfile *photFile = outputFile;
-    psMetadataAddPtr (config->files, PS_LIST_TAIL, "PSPHOT.INPUT", PS_DATA_UNKNOWN | PS_META_REPLACE, "psphot input : view on another pmFPAfile", photFile);
-
-    // old-style variance renormalization
-    if (!ppSubReadoutRenormPhot (config, recipe, photRO)) {
-        psError(PS_ERR_UNKNOWN, false, "failure in renormalization");
-        return false;
-    }
+#if 0
+    psMetadataAddPtr(config->files, PS_LIST_TAIL, "PSPHOT.INPUT", PS_DATA_UNKNOWN | PS_META_REPLACE,
+                     "psphot input: view on another pmFPAfile", photFile);
+#endif
 
     if (!psphotReadoutMinimal(config, view)) {
@@ -86,4 +90,9 @@
         psErrorClear();
     }
+#if 1
+    photRO->data_exists = true;
+    photRO->parent->data_exists = true;
+    photRO->parent->parent->data_exists = true;
+#endif
 
     if (stats) {
@@ -101,14 +110,6 @@
 }
 
-    // XXX not sure that this is still needed (only if psphotReadoutMinimal measures the background)
-    // if (!pmFPAfileDropInternal(config->files, "PSPHOT.BACKMDL") ||
-    //  !pmFPAfileDropInternal (config->files, "PSPHOT.BACKMDL.STDEV") ||
-    //  !pmFPAfileDropInternal (config->files, "PSPHOT.BACKGND")) {
-    //  psError(PS_ERR_UNKNOWN, false, "Unable to drop PSPHOT internal files.");
-    //  return false;
-    // }
 
-    // pmFPAfileActivate(config->files, false, "PSPHOT.INPUT");
-    // pmFPAfileActivate(config->files, false, "PSPHOT.LOAD.PSF");
+
 
 
Index: /trunk/ppSub/src/ppSubReadoutSubtract.c
===================================================================
--- /trunk/ppSub/src/ppSubReadoutSubtract.c	(revision 21523)
+++ /trunk/ppSub/src/ppSubReadoutSubtract.c	(revision 21524)
@@ -6,18 +6,30 @@
  *
  *  @author IfA
- *  @version $Revision: 1.4 $ $Name: not supported by cvs2svn $
- *  @date $Date: 2009-02-09 21:26:05 $
+ *  @version $Revision: 1.5 $ $Name: not supported by cvs2svn $
+ *  @date $Date: 2009-02-18 00:31:20 $
  *  Copyright 2009 Institute for Astronomy, University of Hawaii
  */
 
+#ifdef HAVE_CONFIG_H
+#include <config.h>
+#endif
+
+#include <stdio.h>
+#include <pslib.h>
+#include <psmodules.h>
+
 #include "ppSub.h"
+
 #define WCS_TOLERANCE 0.001             // Tolerance for WCS
 
-bool ppSubReadoutSubtract (pmConfig *config, const pmFPAview *view) {
+bool ppSubReadoutSubtract(pmConfig *config, const pmFPAview *view)
+{
+    psAssert(config, "Require configuration");
+    psAssert(view, "Require view");
 
-    bool mdok = false;
 
     // Look up recipe values
-    psMetadata *recipe = psMetadataLookupMetadata(&mdok, config->recipes, PPSUB_RECIPE); // Recipe for ppSim
+    bool mdok = false;                  // Status of MD lookup
+    psMetadata *recipe = psMetadataLookupMetadata(&mdok, config->recipes, PPSUB_RECIPE); // Recipe for ppSub
     psAssert(recipe, "We checked this earlier, so it should be here.");
 
@@ -25,6 +37,6 @@
 
     // Subtraction is: minuend - subtrahend
-    pmReadout *minuend = NULL;
-    pmReadout *subtrahend = NULL;
+    pmReadout *minuend = NULL;          // Positive image
+    pmReadout *subtrahend = NULL;       // Negative image
     if (reverse) {
         minuend = pmFPAfileThisReadout(config->files, view, "PPSUB.REF.CONV");
@@ -59,4 +71,10 @@
     outRO->mask = (psImage*)psBinaryOp(outRO->mask, minuend->mask, "|", subtrahend->mask);
 
+    psArray *covars = psArrayAlloc(2);  // Covariance pseudo-matrices
+    covars->data[0] = psMemIncrRefCounter(minuend->covariance);
+    covars->data[1] = psMemIncrRefCounter(subtrahend->covariance);
+    outRO->covariance = psImageCovarianceSum(covars);
+    psFree(covars);
+
     outRO->data_exists = true;
     outRO->parent->data_exists = true;
@@ -65,11 +83,9 @@
     pmSubtractionVisualShowSubtraction(minuend->image, subtrahend->image, outRO->image);
 
-    // copy concepts from the input to the output (XXX should this always use minuend?)
-    pmFPAfile *inFile = psMetadataLookupPtr (&mdok, config->files, "PPSUB.INPUT");
-    pmFPA *inFPA = inFile->fpa;
-
-    pmFPAfile *outFile = psMetadataLookupPtr (&mdok, config->files, "PPSUB.OUTPUT");
-    pmFPA *outFPA = outFile->fpa;
-
+    // Copy concepts from the input to the output
+    pmFPAfile *inFile = psMetadataLookupPtr(&mdok, config->files, "PPSUB.INPUT"); // Input file
+    pmFPA *inFPA = inFile->fpa;         // Input FPA
+    pmFPAfile *outFile = psMetadataLookupPtr(&mdok, config->files, "PPSUB.OUTPUT"); // Output file
+    pmFPA *outFPA = outFile->fpa;       // Output FPA
     if (!pmFPACopyConcepts(outFPA, inFPA)) {
         psError(PS_ERR_UNKNOWN, false, "Unable to copy concepts from input to output.");
@@ -78,21 +94,17 @@
     }
 
-    // get the HDUs and output Chip to copy the astrometry
-    pmHDU *inHDU = inFPA->hdu;        // input HDU
-    pmHDU *outHDU = outFPA->hdu;
-    pmChip *outChip = pmFPAfileThisChip(config->files, view, "PPSUB.OUTPUT");
-
+    // Copy astrometry over
+    // It should find its way into the output images and photometry
+    pmHDU *inHDU = inFPA->hdu;          // Input HDU
+    pmHDU *outHDU = outFPA->hdu;        // Output HDU
+    pmChip *outChip = pmFPAfileThisChip(config->files, view, "PPSUB.OUTPUT"); // Output chip
     if (!outHDU || !inHDU) {
         psError(PS_ERR_UNKNOWN, false, "Unable to find HDU at FPA level to copy astrometry.");
         return false;
     }
-
-    // Copy astrometry over
-    // It should get into the output images and photometry
     if (!pmAstromReadWCS(outFPA, outChip, inHDU->header, 1.0)) {
         psError(PS_ERR_UNKNOWN, false, "Unable to read WCS astrometry from input FPA.");
         return false;
     }
-
     if (!pmAstromWriteWCS(outHDU->header, outFPA, outChip, WCS_TOLERANCE)) {
         psError(PS_ERR_UNKNOWN, false, "Unable to write WCS astrometry to output FPA.");
@@ -102,17 +114,2 @@
     return true;
 }
-
-// XXX this test code was in place to check for and squash unexpected NANs
-
-#if 0
-    pmReadoutMaskApply(outRO, maskBad);
-
-    for (int y = 0; y < outRO->image->numRows; y++) {
-        for (int x = 0; x < outRO->image->numCols; x++) {
-            if (isnan(outRO->image->data.F32[y][x]) && !(outRO->mask->data.PS_TYPE_IMAGE_MASK_DATA[y][x] & maskVal)) {
-                printf("Unmasked NAN at %d %d --> %d\n", x, y, outRO->mask->data.PS_TYPE_IMAGE_MASK_DATA[y][x]);
-            }
-        }
-    }
-#endif
-
Index: /trunk/ppSub/src/ppSubReadoutUpdate.c
===================================================================
--- /trunk/ppSub/src/ppSubReadoutUpdate.c	(revision 21523)
+++ /trunk/ppSub/src/ppSubReadoutUpdate.c	(revision 21524)
@@ -6,17 +6,25 @@
  *
  *  @author IfA
- *  @version $Revision: 1.3 $ $Name: not supported by cvs2svn $
- *  @date $Date: 2009-02-07 00:15:00 $
+ *  @version $Revision: 1.4 $ $Name: not supported by cvs2svn $
+ *  @date $Date: 2009-02-18 00:31:20 $
  *  Copyright 2009 Institute for Astronomy, University of Hawaii
  */
 
+#ifdef HAVE_CONFIG_H
+#include <config.h>
+#endif
+
+#include <stdio.h>
+#include <pslib.h>
+#include <psmodules.h>
+
 #include "ppSub.h"
 
-/**
- * Renormalize, update headers and generate JPEGs
- */
-bool ppSubReadoutUpdate (pmConfig *config, const pmFPAview *view) {
+bool ppSubReadoutUpdate(pmConfig *config, psMetadata *stats, const pmFPAview *view)
+{
+    psAssert(config, "Require configuration");
+    psAssert(view, "Require view");
 
-    bool mdok = false;
+    bool mdok = false;                  // Status of MD lookup
 
     // Look up recipe values
@@ -24,17 +32,8 @@
     psAssert(recipe, "We checked this earlier, so it should be here.");
 
-    // select the output readout
-    pmReadout *outRO = pmFPAfileThisReadout(config->files, view, "PPSUB.OUTPUT");
-
-    // Renormalising for pixels, because that's what magic desires
-    if (!ppSubReadoutRenormPixels (config, recipe, outRO)) {
-        psError(PS_ERR_BAD_PARAMETER_VALUE, false, "failure in renormalization");
-        return false;
-    }
-
-    // select the output FPA and HDU to get the output header
-    pmFPAfile *outFile = psMetadataLookupPtr (&mdok, config->files, "PPSUB.OUTPUT");
-    pmFPA *outFPA = outFile->fpa;
-    pmHDU *outHDU = outFPA->hdu;
+    pmFPAfile *outFile = psMetadataLookupPtr(&mdok, config->files, "PPSUB.OUTPUT"); // Output file
+    pmReadout *outRO = pmFPAfileThisReadout(config->files, view, "PPSUB.OUTPUT"); // Output image
+    pmFPA *outFPA = outFile->fpa;       // Output FPA
+    pmHDU *outHDU = outFPA->hdu;        // Output HDU
 
     // Add additional data to the header
@@ -46,4 +45,22 @@
                      "Subtraction input", inFile->filename);
 
+    // Statistics on the matching
+    if (stats) {
+        psMetadataCopySingle(stats, outRO->analysis, PM_SUBTRACTION_ANALYSIS_MODE);
+        psMetadataCopySingle(stats, outRO->analysis, PM_SUBTRACTION_ANALYSIS_STAMPS);
+        psMetadataCopySingle(stats, outRO->analysis, PM_SUBTRACTION_ANALYSIS_DEV_MEAN);
+        psMetadataCopySingle(stats, outRO->analysis, PM_SUBTRACTION_ANALYSIS_DEV_RMS);
+        psMetadataCopySingle(stats, outRO->analysis, PM_SUBTRACTION_ANALYSIS_NORM);
+        psMetadataCopySingle(stats, outRO->analysis, PM_SUBTRACTION_ANALYSIS_BGDIFF);
+        psMetadataCopySingle(stats, outRO->analysis, PM_SUBTRACTION_ANALYSIS_MX);
+        psMetadataCopySingle(stats, outRO->analysis, PM_SUBTRACTION_ANALYSIS_MY);
+        psMetadataCopySingle(stats, outRO->analysis, PM_SUBTRACTION_ANALYSIS_MXX);
+        psMetadataCopySingle(stats, outRO->analysis, PM_SUBTRACTION_ANALYSIS_MXY);
+        psMetadataCopySingle(stats, outRO->analysis, PM_SUBTRACTION_ANALYSIS_MYY);
+
+        psMetadataAddF32(stats, PS_LIST_TAIL, "TIME_MATCH", 0, "Time to match PSFs",
+                         psTimerClear("PPSUB_MATCH"));
+    }
+
     // Generate binned JPEGs
     {
@@ -54,9 +71,8 @@
 
         // Target cells
-        pmCell *cell1 = pmFPAfileThisCell(config->files, view, "PPSUB.OUTPUT.JPEG1");
-        pmCell *cell2 = pmFPAfileThisCell(config->files, view, "PPSUB.OUTPUT.JPEG2");
+        pmCell *cell1 = pmFPAfileThisCell(config->files, view, "PPSUB.OUTPUT.JPEG1"); // Rebinned cell once
+        pmCell *cell2 = pmFPAfileThisCell(config->files, view, "PPSUB.OUTPUT.JPEG2"); // Rebinned cell twice
 
-        pmReadout *ro1 = pmReadoutAlloc(cell1);
-        pmReadout *ro2 = pmReadoutAlloc(cell2); // Binned readouts
+        pmReadout *ro1 = pmReadoutAlloc(cell1), *ro2 = pmReadoutAlloc(cell2); // Binned readouts
         if (!pmReadoutRebin(ro1, outRO, maskBad, bin1, bin1)) {
             psError(PS_ERR_UNKNOWN, false, "Unable to bin output (1st binning)");
Index: /trunk/ppSub/src/ppSubSetMasks.c
===================================================================
--- /trunk/ppSub/src/ppSubSetMasks.c	(revision 21523)
+++ /trunk/ppSub/src/ppSubSetMasks.c	(revision 21524)
@@ -6,22 +6,26 @@
  *
  *  @author IfA
- *  @version $Revision: 1.2 $ $Name: not supported by cvs2svn $
- *  @date $Date: 2009-02-06 01:37:17 $
+ *  @version $Revision: 1.3 $ $Name: not supported by cvs2svn $
+ *  @date $Date: 2009-02-18 00:31:20 $
  *  Copyright 2009 Institute for Astronomy, University of Hawaii
  */
 
+#ifdef HAVE_CONFIG_H
+#include <config.h>
+#endif
+
+#include <stdio.h>
+#include <pslib.h>
+#include <psmodules.h>
+#include <psphot.h>
+
 #include "ppSub.h"
 
-/** this function generates (if needed) and sets or updates the masks for both the input and
- * reference images.  this function also has the code for interpolation over bad pixels, but it
- * is currently if-def-ed out
- */
+bool ppSubSetMasks(pmConfig *config, const pmFPAview *view)
+{
+    psAssert(config, "Require configuration");
+    psAssert(view, "Require view");
 
-bool ppSubSetMasks (pmConfig *config, const pmFPAview *view) {
-
-    psImageMaskType maskValue;
-    psImageMaskType markValue;
-    bool mdok = false;
-
+    psImageMaskType maskValue, markValue; // Mask values
     if (!pmConfigMaskSetBits(&maskValue, &markValue, config)) {
         psError(PS_ERR_UNKNOWN, false, "Unable to determine mask value.");
@@ -29,16 +33,16 @@
     }
 
-    // set the mask bits needed by psphot (in psphot recipe) 
+    // Set the mask bits needed by psphot (in psphot recipe)
     psphotSetMaskRecipe (config, maskValue, markValue);
 
     // Look up recipe values
-    psMetadata *recipe = psMetadataLookupMetadata(&mdok, config->recipes, PPSUB_RECIPE); // Recipe for ppSim
+    psMetadata *recipe = psMetadataLookupMetadata(NULL, config->recipes, PPSUB_RECIPE); // Recipe for ppSim
     psAssert(recipe, "We checked this earlier, so it should be here.");
 
     psImageMaskType satValue = pmConfigMaskGet("SAT", config);
-    psAssert (satValue, "SAT must be non-zero");
+    psAssert(satValue, "SAT must be non-zero");
 
     psImageMaskType badValue = pmConfigMaskGet("BAD", config);
-    psAssert (badValue, "BAD must be non-zero");
+    psAssert(badValue, "BAD must be non-zero");
 
     // input images
@@ -49,8 +53,5 @@
     psImage *reference = refRO->image;  // Reference image
     PS_ASSERT_IMAGES_SIZE_EQUAL(input, reference, false);
-
-    // XXX use psImageCopy below to avoid caring about image dimensions
-    int numCols = input->numCols;
-    int numRows = input->numRows;
+    int numCols = input->numCols, numRows = input->numRows; // Size of image
 
     // Generate masks if they don't exist
@@ -78,15 +79,12 @@
     }
     if (!pmReadoutMaskNonfinite(refRO, satValue)) {
-	psError(PS_ERR_UNKNOWN, false, "Unable to mask non-finite pixels in reference.");
-	return false;
+        psError(PS_ERR_UNKNOWN, false, "Unable to mask non-finite pixels in reference.");
+        return false;
     }
 
-#if (0)
-    // Look up recipe values
-    psMetadata *recipe = psMetadataLookupMetadata(NULL, config->recipes, PPSUB_RECIPE); // Recipe for ppSim
-    psAssert(recipe, "We checked this earlier, so it should be here.");
 
+#if 0
+    // Interpolation over bad pixels: this takes a while!
     bool mdok = false;
-
     psString interpModeStr = psMetadataLookupStr(&mdok, recipe, "INTERPOLATION"); // Interpolation mode
     psImageInterpolateMode interpMode = psImageInterpolateModeFromString(interpModeStr); // Interp
@@ -102,10 +100,10 @@
     // Interpolate over bad pixels, so the bad pixels don't explode
     if (!pmReadoutInterpolateBadPixels(inRO, maskVal, interpMode, poorFrac, maskPoor, maskBad)) {
-	psError(PS_ERR_UNKNOWN, false, "Unable to interpolate bad pixels for input image.");
-	return false;
+        psError(PS_ERR_UNKNOWN, false, "Unable to interpolate bad pixels for input image.");
+        return false;
     }
     if (!pmReadoutInterpolateBadPixels(refRO, maskVal, interpMode, poorFrac, maskPoor, maskBad)) {
-	psError(PS_ERR_UNKNOWN, false, "Unable to interpolate bad pixels for reference image.");
-	return false;
+        psError(PS_ERR_UNKNOWN, false, "Unable to interpolate bad pixels for reference image.");
+        return false;
     }
     maskVal |= maskBad;
Index: /trunk/ppSub/src/ppSubVarianceFactors.c
===================================================================
--- /trunk/ppSub/src/ppSubVarianceFactors.c	(revision 21523)
+++ /trunk/ppSub/src/ppSubVarianceFactors.c	(revision 21524)
@@ -6,6 +6,6 @@
  *
  *  @author IfA
- *  @version $Revision: 1.3 $ $Name: not supported by cvs2svn $
- *  @date $Date: 2009-02-06 03:20:02 $
+ *  @version $Revision: 1.4 $ $Name: not supported by cvs2svn $
+ *  @date $Date: 2009-02-18 00:31:20 $
  *  Copyright 2009 Institute for Astronomy, University of Hawaii
  */
@@ -96,22 +96,4 @@
     vfItem->data.F32 = vf;
 
-    // Statistics on the matching
-    if (stats) {
-        psMetadataCopySingle(stats, outRO->analysis, PM_SUBTRACTION_ANALYSIS_MODE);
-        psMetadataCopySingle(stats, outRO->analysis, PM_SUBTRACTION_ANALYSIS_STAMPS);
-        psMetadataCopySingle(stats, outRO->analysis, PM_SUBTRACTION_ANALYSIS_DEV_MEAN);
-        psMetadataCopySingle(stats, outRO->analysis, PM_SUBTRACTION_ANALYSIS_DEV_RMS);
-        psMetadataCopySingle(stats, outRO->analysis, PM_SUBTRACTION_ANALYSIS_NORM);
-        psMetadataCopySingle(stats, outRO->analysis, PM_SUBTRACTION_ANALYSIS_BGDIFF);
-        psMetadataCopySingle(stats, outRO->analysis, PM_SUBTRACTION_ANALYSIS_MX);
-        psMetadataCopySingle(stats, outRO->analysis, PM_SUBTRACTION_ANALYSIS_MY);
-        psMetadataCopySingle(stats, outRO->analysis, PM_SUBTRACTION_ANALYSIS_MXX);
-        psMetadataCopySingle(stats, outRO->analysis, PM_SUBTRACTION_ANALYSIS_MXY);
-        psMetadataCopySingle(stats, outRO->analysis, PM_SUBTRACTION_ANALYSIS_MYY);
-
-        psMetadataAddF32(stats, PS_LIST_TAIL, "TIME_MATCH", 0, "Time to match PSFs",
-                         psTimerClear("PPSUB_MATCH"));
-    }
-
     return true;
 }
Index: /trunk/ppSub/src/ppSubVersion.c
===================================================================
--- /trunk/ppSub/src/ppSubVersion.c	(revision 21523)
+++ /trunk/ppSub/src/ppSubVersion.c	(revision 21524)
@@ -6,8 +6,17 @@
  *
  *  @author IfA
- *  @version $Revision: 1.3 $ $Name: not supported by cvs2svn $
- *  @date $Date: 2009-02-06 01:37:17 $
+ *  @version $Revision: 1.4 $ $Name: not supported by cvs2svn $
+ *  @date $Date: 2009-02-18 00:31:20 $
  *  Copyright 2009 Institute for Astronomy, University of Hawaii
  */
+
+#ifdef HAVE_CONFIG_H
+#include <config.h>
+#endif
+
+#include <stdio.h>
+#include <pslib.h>
+#include <psmodules.h>
+#include <ppStats.h>
 
 #include "ppSub.h"
