Index: /trunk/psphot/src/psphotReadout.c
===================================================================
--- /trunk/psphot/src/psphotReadout.c	(revision 11187)
+++ /trunk/psphot/src/psphotReadout.c	(revision 11188)
@@ -43,5 +43,5 @@
 
     // find the peaks in the image
-    psArray *peaks = psphotFindPeaks (readout, recipe);
+    psArray *peaks = psphotFindPeaks (readout, recipe, 1);
 
     // construct sources and measure basic stats
@@ -58,5 +58,4 @@
 
     // classify sources based on moments, brightness
-    // faint sources not classified?
     if (!psphotRoughClass (sources, recipe)) {
         psFree (sources);
@@ -66,13 +65,7 @@
         return false;
     }
-    // XXX test the plotting function
-    // psphotPlotMoments (config, view, sources);
-
     if (!strcasecmp (breakPt, "MOMENTS")) {
         return psphotReadoutCleanup(config, readout, recipe, NULL, sources);
     }
-
-    // psphotTestPSF (readout, sources, recipe);
-    // exit (1);
 
     // use bright stellar objects to measure PSF
@@ -82,9 +75,9 @@
         return false;
     }
-
     if (!strcasecmp (breakPt, "PSFMODEL")) {
         return psphotReadoutCleanup(config, readout, recipe, psf, sources);
     }
 
+    // construct an initial PSF model for each object 
     psphotGuessModels (readout, sources, recipe, psf);
 
@@ -114,6 +107,5 @@
 
     // find the peaks in the image
-    psMetadataAddF32 (recipe, PS_LIST_TAIL, "PEAKS_NSIGMA_LIMIT", PS_META_REPLACE, "", 5.0);
-    psArray *newPeaks = psphotFindPeaks (readout, recipe);
+    psArray *newPeaks = psphotFindPeaks (readout, recipe, 2);
 
     // define new sources based on the new peaks
@@ -127,10 +119,13 @@
     psphotGuessModels (readout, newSources, recipe, psf);
 
-    // linear PSF fit to remaining peaks
-    psphotEnsemblePSF (readout, newSources, recipe, psf, TRUE);
+    // replace all sources
+    psphotReplaceAll (sources);
 
     // merge the newly selected peaks into the existing list
-    // psphotIncludeNewPeaks (sources, newSources);
+    psphotMergeSources (sources, newSources);
     psFree (newSources);
+
+    // linear PSF fit to remaining peaks
+    psphotEnsemblePSF (readout, sources, recipe, psf, TRUE);
 
 finish:
@@ -146,6 +141,5 @@
     psphotMagnitudes(sources, recipe, psf, background);
 
-    // replace fitted sources
-    // XXX we need to replace failed objects, not all objects
+    // replace failed sources?
     // psphotReplaceUnfit (sources);
 
@@ -155,44 +149,6 @@
     // drop the references to the image pixels held by each source
     psphotSourceFreePixels (sources);
-    // psphotSaveImage (NULL, readout->image, "resid.fits");
 
     // create the exported-metadata and free local data
     return psphotReadoutCleanup(config, readout, recipe, psf, sources);
 }
-
-bool psphotReadoutCleanup (pmConfig *config, pmReadout *readout, psMetadata *recipe, pmPSF *psf, psArray *sources) {
-
-    // use the psf-model to measure FWHM stats
-    if (psf) {
-        if (!psphotPSFstats (readout, recipe, psf)) {
-            psError(PS_ERR_IO, true, "Failed to measure PSF shape parameters");
-            return false;
-        }
-    } else if (sources) {
-        if (!psphotMomentsStats (readout, recipe, sources)) {
-            psError(PS_ERR_IO, true, "Failed to measure Moment shape parameters");
-            return false;
-        }
-    }
-
-    // create an output header with stats results
-    psMetadata *header = psphotDefineHeader (recipe);
-
-    // save the results of the analysis
-    psMetadataAdd (readout->analysis, PS_LIST_TAIL, "PSPHOT.SOURCES", PS_DATA_ARRAY,    "psphot sources", sources);
-    psMetadataAdd (readout->analysis, PS_LIST_TAIL, "PSPHOT.HEADER",  PS_DATA_METADATA, "header stats", header);
-    psMetadataAdd (readout->analysis, PS_LIST_TAIL, "PSPHOT.PSF",     PS_DATA_UNKNOWN,  "psphot psf", psf);
-
-    // remove internal pmFPAfiles, if created
-    pmFPAfileDropInternal (config->files, "PSPHOT.BACKMDL");
-    pmFPAfileDropInternal (config->files, "PSPHOT.BACKGND");
-
-    // XXX move this to top of loop
-    pmKapaClose ();
-
-    psFree (psf);
-    psFree (header);
-    psFree (sources);
-
-    return true;
-}
