Index: /branches/eam_branches/20091201/ppSub/src/ppSub.h
===================================================================
--- /branches/eam_branches/20091201/ppSub/src/ppSub.h	(revision 26431)
+++ /branches/eam_branches/20091201/ppSub/src/ppSub.h	(revision 26432)
@@ -160,4 +160,6 @@
 bool psMetadataCopySingle(psMetadata *target, psMetadata *source, const char *name);
 
+bool ppSubCopyPSF (pmFPAfile *output, pmFPAfile *input, pmFPAview *view);
+
 ///@}
 #endif
Index: /branches/eam_branches/20091201/ppSub/src/ppSubCamera.c
===================================================================
--- /branches/eam_branches/20091201/ppSub/src/ppSubCamera.c	(revision 26431)
+++ /branches/eam_branches/20091201/ppSub/src/ppSubCamera.c	(revision 26432)
@@ -319,6 +319,6 @@
         pmFPAfileActivate(config->files, false, "PSPHOT.INPUT");
 
-        // Internal-ish file for getting the PSF from the minuend
-        pmFPAfile *psf = pmFPAfileDefineOutputFromFile(config, psphot, "PSPHOT.PSF.LOAD");
+        // Internal file for getting the PSF from the minuend
+        pmFPAfile *psf = pmFPAfileDefineFromFPA(config, output->fpa, 1, 1, "PSPHOT.PSF.LOAD");
         if (!psf) {
             psError(PS_ERR_IO, false, "Failed to build FPA from PSPHOT.PSF.LOAD");
Index: /branches/eam_branches/20091201/ppSub/src/ppSubMakePSF.c
===================================================================
--- /branches/eam_branches/20091201/ppSub/src/ppSubMakePSF.c	(revision 26431)
+++ /branches/eam_branches/20091201/ppSub/src/ppSubMakePSF.c	(revision 26432)
@@ -61,13 +61,18 @@
         return false;
     }
+
     pmReadout *photRO = pmFPAviewThisReadout(view, photFile->fpa); // Readout to photometer
+
+    // we need to remove any existing PSPHOT.SOURCES (why not do this in psphot?)
     if (psMetadataLookup(photRO->analysis, "PSPHOT.SOURCES")) {
         psMetadataRemoveKey(photRO->analysis, "PSPHOT.SOURCES");
     }
 
+# ifdef TESTING
     // XXX for testing, dump these images:
     psphotSaveImage (NULL, photRO->image, "findpsf.im.fits");
     psphotSaveImage (NULL, photRO->variance, "findpsf.wt.fits");
     psphotSaveImage (NULL, photRO->mask, "findpsf.mk.fits");
+# endif
 
     // Extract the loaded sources from the associated readout, and generate PSF
@@ -84,11 +89,14 @@
     }
 
+    // save the resulting PSF information on the pmFPAfile PSPHOT.PSF.LOAD
+    pmFPAfile *psfFile = psMetadataLookupPtr(&mdok, config->files, "PSPHOT.PSF.LOAD"); // PSF file
+    if (!ppSubCopyPSF (psfFile, photFile, view)) {
+        psErrorStackPrint(stderr, "PSF was not generated");
+        psWarning("PSF was not generated --- suspect bad data quality.");
+	ppSubDataQuality(data, psErrorCodeLast(), PPSUB_FILES_PHOT_SUB | PPSUB_FILES_PHOT_INV);
+    }
+
     // Get rid of the generated header; it will be regenerated by the real photometry run
     psMetadataRemoveKey(photRO->analysis, "PSPHOT.HEADER");
-
-    if (!data->quality) {
-        data->psf = psMemIncrRefCounter(psMetadataLookupPtr(NULL, photRO->parent->parent->analysis,
-                                                            "PSPHOT.PSF"));
-    }
 
     psFree(view);
@@ -96,2 +104,54 @@
     return true;
 }
+
+bool ppSubCopyPSF (pmFPAfile *output, pmFPAfile *input, pmFPAview *view) {
+
+    bool mdok = false;
+
+    pmChip *inputChip   = pmFPAviewThisChip(view, input->fpa); // Chip with PSF info
+    pmChip *outputChip  = pmFPAviewThisChip(view, output->fpa); // Chip to store PSF info
+
+    pmReadout *inputRO  = pmFPAviewThisReadout(view, input->fpa); // Readout with PSF info
+    pmReadout *outputRO = pmFPAviewThisReadout(view, output->fpa); // Readout to store PSF info
+
+    if (!outputRO) {
+	pmCell *outputCell  = pmFPAviewThisCell(view, output->fpa); 
+	outputRO = pmReadoutAlloc(outputCell);
+	outputRO->image = psMemIncrRefCounter (inputRO->image);
+    }
+
+    // copy the PSF-related data to PSPHOT.PSF.LOAD for safe-keeping
+    psMetadata *psfRegions = psMetadataAlloc();
+
+    int nRegions = psMetadataLookupS32 (&mdok, inputRO->analysis, "PSF.CLUMP.NREGIONS");
+    if (!nRegions) {
+        psErrorStackPrint(stderr, "No PSF available");
+	return false;
+    }
+    psMetadataAddS32 (psfRegions, PS_LIST_TAIL, "PSF.CLUMP.NREGIONS",  PS_META_REPLACE, "psf clump regions", nRegions);
+
+    for (int i = 0; i < nRegions; i++) {
+	char fieldName[80];
+	snprintf (fieldName, 80, "PSF.CLUMP.REGION.%03d", i);
+        psMetadata *regionMD = psMetadataLookupPtr (&mdok, inputRO->analysis, fieldName);
+        if (!regionMD) {
+	    psWarning ("missing psf clump region metadata for entry %d", i);
+	    continue;
+	}
+	psMetadataAddMetadata (psfRegions, PS_LIST_TAIL, fieldName, PS_META_REPLACE, "psf clump region", regionMD);
+    }
+
+    // XXX why am I replacing the entire analysis MD?
+    psFree(outputRO->analysis);
+    outputRO->analysis = psfRegions;
+
+    // copy the PSF model data
+    pmPSF *psf = psMetadataLookupPtr(NULL, inputChip->analysis, "PSPHOT.PSF"); // PSF for photometry
+    if (!psf) {
+        psErrorStackPrint(stderr, "No PSF available");
+        return false;
+    }
+    psMetadataAddPtr(outputChip->analysis, PS_LIST_TAIL, "PSPHOT.PSF", PS_DATA_UNKNOWN | PS_META_REPLACE, "PSF from ppSubMakePSF", psf);
+
+    return true;
+}
Index: /branches/eam_branches/20091201/ppSub/src/ppSubMatchPSFs.c
===================================================================
--- /branches/eam_branches/20091201/ppSub/src/ppSubMatchPSFs.c	(revision 26431)
+++ /branches/eam_branches/20091201/ppSub/src/ppSubMatchPSFs.c	(revision 26432)
@@ -193,4 +193,5 @@
     }
 
+# ifdef TESTING
     // XXX for testing
     psphotSaveImage (NULL, refRO->image,    "refRO.im.fits");
@@ -209,4 +210,5 @@
     psphotSaveImage (NULL, refConv->variance, "refConv.wt.fits");
     psphotSaveImage (NULL, refConv->mask,     "refConv.mk.fits");
+# endif
 
     psFree(optWidths);
Index: /branches/eam_branches/20091201/ppSub/src/ppSubReadoutPhotometry.c
===================================================================
--- /branches/eam_branches/20091201/ppSub/src/ppSubReadoutPhotometry.c	(revision 26431)
+++ /branches/eam_branches/20091201/ppSub/src/ppSubReadoutPhotometry.c	(revision 26432)
@@ -24,4 +24,6 @@
 bool ppSubReadoutPhotometry(const char *name, ppSubData *data)
 {
+    bool mdok = false;
+
     psAssert(data, "Require processing data");
     pmConfig *config = data->config;    // Configuration
@@ -42,31 +44,10 @@
     }
 
-    // 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
+    // select the view of interest
     pmFPAview *view = ppSubViewReadout(); // View to readout
-    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"); // Chip to have PSF
-    psAssert (psfLoadChip, "PSPHOT.PSF.LOAD should have been defined in ppSubCamera");
-    pmPSF *psf = psMetadataLookupPtr(NULL, psfInputChip->analysis, "PSPHOT.PSF"); // PSF for photometry
-    if (!psf) {
-        psErrorStackPrint(stderr, "No PSF available");
-        psWarning("No PSF available --- suspect bad data quality.");
-        ppSubDataQuality(data, psErrorCodeLast(), PPSUB_FILES_PHOT_SUB | PPSUB_FILES_PHOT_INV);
-        return true;
-    }
-    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.
-    pmReadout *inRO = pmFPAfileThisReadout(config->files, view, name); // Readout with image and sources
-    if (psMetadataLookup(inRO->analysis, "PSPHOT.SOURCES")) {
-        psMetadataRemoveKey(inRO->analysis, "PSPHOT.SOURCES");
-    }
-
+    // around the pointers so PSPHOT.INPUT corresponds to the output image of interest (on one
+    // pass this is the subtraction image, in another it is negative of the subtraction
     pmFPAfile *photFile = psMetadataLookupPtr(&mdok, config->files, "PSPHOT.INPUT"); // Photometry file
     pmFPAfile *inFile = psMetadataLookupPtr(&mdok, config->files, name); // Input file
@@ -76,18 +57,27 @@
         return false;
     }
+
+    // drop references to PSPHOT.SOURCES on both of these  (why is this needed for both??)
     pmReadout *photRO = pmFPAviewThisReadout(view, photFile->fpa); // Readout to photometer
     if (psMetadataLookup(photRO->analysis, "PSPHOT.SOURCES")) {
         psMetadataRemoveKey(photRO->analysis, "PSPHOT.SOURCES");
     }
+    pmReadout *inRO = pmFPAfileThisReadout(config->files, view, name); // Readout with image and sources
+    if (psMetadataLookup(inRO->analysis, "PSPHOT.SOURCES")) {
+        psMetadataRemoveKey(inRO->analysis, "PSPHOT.SOURCES");
+    }
 
-    psMetadataAddPtr(photRO->parent->parent->analysis, PS_LIST_TAIL, "PSPHOT.PSF",
-                     PS_META_REPLACE | PS_DATA_UNKNOWN, "Point-spread function", data->psf);
-
-    psFree(photRO->analysis);
-    photRO->analysis = psMetadataAlloc();
+    // grab the PSF information from the pmFPAfile PSPHOT.PSF.LOAD
+    pmFPAfile *psfFile = psMetadataLookupPtr(&mdok, config->files, "PSPHOT.PSF.LOAD"); // PSF file
+    ppSubCopyPSF (photFile, psfFile, view);
 
     // psphotSaveImage (photFile->fpa->hdu->header, photRO->image, "findsrc.im.fits");
     // psphotSaveImage (photFile->fpa->hdu->header, photRO->variance, "findsrc.wt.fits");
     // psphotSaveImage (photFile->fpa->hdu->header, photRO->mask, "findsrc.mk.fits");
+
+    // erase the overlays from a previous psphot-related step
+    if (pmVisualIsVisual()) {
+	psphotVisualEraseOverlays (1, "all");
+    }
 
     if (!psphotReadoutMinimal(config, view)) {
@@ -126,4 +116,8 @@
             return false;
         }
+        if (!psMetadataCopySingle(inRO->analysis, photRO->analysis, PM_DETEFF_ANALYSIS)) {
+            psError(PS_ERR_UNEXPECTED_NULL, false, "Unable to copy Detection Efficiency");
+            return false;
+        }
     }
 
@@ -133,8 +127,4 @@
     return true;
 }
-
-
-
-
 
 #ifdef TESTING
