Index: trunk/psphot/src/psphotMakePSFReadout.c
===================================================================
--- trunk/psphot/src/psphotMakePSFReadout.c	(revision 25982)
+++ trunk/psphot/src/psphotMakePSFReadout.c	(revision 26894)
@@ -19,12 +19,8 @@
 
     // set the photcode for this image
-    if (!psphotAddPhotcode (recipe, config, view, "PSPHOT.INPUT")) {
+    if (!psphotAddPhotcode (config, view)) {
         psError (PSPHOT_ERR_CONFIG, false, "trouble defining the photcode");
         return false;
     }
-
-    // find the currently selected readout
-    pmReadout  *readout = pmFPAfileThisReadout (config->files, view, "PSPHOT.INPUT");
-    PS_ASSERT_PTR_NON_NULL (readout, false);
 
     // optional break-point for processing
@@ -33,96 +29,48 @@
 
     // Generate the mask and weight images, including the user-defined analysis region of interest
-    psphotSetMaskAndVariance (config, readout, recipe);
+    psphotSetMaskAndVariance (config, view);
     if (!strcasecmp (breakPt, "NOTHING")) {
-        return psphotReadoutCleanup(config, readout, recipe, NULL, NULL, NULL);
+        return psphotReadoutCleanup(config, view);
     }
 
-    // display the image, weight, mask (ch 1,2,3)
-    psphotVisualShowImage (readout);
-
     // generate a background model (median, smoothed image)
-    if (!psphotModelBackground (config, view, "PSPHOT.INPUT")) {
-        return psphotReadoutCleanup (config, readout, recipe, NULL, NULL, NULL);
+    if (!psphotModelBackground (config, view)) {
+        return psphotReadoutCleanup (config, view);
     }
-    if (!psphotSubtractBackground (config, view, "PSPHOT.INPUT")) {
-        return psphotReadoutCleanup (config, readout, recipe, NULL, NULL, NULL);
+    if (!psphotSubtractBackground (config, view)) {
+        return psphotReadoutCleanup (config, view);
     }
     if (!strcasecmp (breakPt, "BACKMDL")) {
-        return psphotReadoutCleanup (config, readout, recipe, NULL, NULL, NULL);
+        return psphotReadoutCleanup (config, view);
     }
 
-    // display the backsub and backgnd images
-    psphotVisualShowBackground (config, view, readout);
+    psphotLoadExtSources (config, view);
 
-    pmDetections *detections = NULL;
-
-    // If sources have been supplied, then these should be used to measure the PSF
-    // include externally-supplied sources
-
-    // XXX sources loaded from a text file have no valid SN values, but psphotChoosePSF
-    // selected the top PSF_MAX_NSTARS to generate the PSF, excluding an arbitrary subset.
-    psArray *sources = psArrayAllocEmpty(100);
-    psphotLoadExtSources (config, view, sources);
-    if (sources->n) {
-	// the user wants to make the psf from these stars; define them as psf stars:
-	for (int i = 0; i < sources->n; i++) {
-	    pmSource *source = sources->data[i];
-	    source->mode |= PM_SOURCE_MODE_PSFSTAR;
-	}
-	// force psphotChoosePSF to use all loaded sources
-	psMetadataAddS32 (recipe, PS_LIST_TAIL, "PSF_MAX_NSTARS", PS_META_REPLACE, "fit radius", sources->n);
-
-	// measure stats of externally specified sources
-	if (!psphotSourceStatsUpdate (sources, config, readout)) {
-	    psError(PSPHOT_ERR_CONFIG, false, "failure to measure stats of existing sources");
-	    return false;
-	}
-
-    } else {
-	// find the detections (by peak and/or footprint) in the image.
-	detections = psphotFindDetections (NULL, readout, recipe);
-	if (!detections) {
-	    psLogMsg ("psphot", 3, "unable to find detections in this image");
-	    return psphotReadoutCleanup (config, readout, recipe, detections, NULL, sources);
-	}
-
-	// construct sources and measure basic stats
-	psFree (sources);
-	sources = psphotSourceStats (config, readout, detections, true);
-	if (!sources) return false;
-
-	// find blended neighbors of very saturated stars
-	// XXX merge this with Basic Deblend?
-	psphotDeblendSatstars (readout, sources, recipe);
-
-	// mark blended peaks PS_SOURCE_BLEND
-	if (!psphotBasicDeblend (sources, recipe)) {
-	    psLogMsg ("psphot", 3, "failed on deblend analysis");
-	    return psphotReadoutCleanup (config, readout, recipe, detections, NULL, sources);
-	}
-
-	// classify sources based on moments, brightness
-	if (!psphotRoughClass (readout, sources, recipe, false)) {
-	    psLogMsg ("psphot", 3, "failed to find a valid PSF clump for image");
-	    return psphotReadoutCleanup (config, readout, recipe, detections, NULL, sources);
-	}
+    // If sources have been supplied, then these should be used to measure the PSF include
+    // externally-supplied sources; if not, we need to generate a set of possible PSF sources.
+    // This function updates the SN entries for the loaded sources or generates a set of
+    // detections from the image, if no external ones have been supplied.  Sources loaded from
+    // a text file have no valid SN values, but psphotChoosePSF needs to select the top
+    // PSF_MAX_NSTARS to generate the PSF.
+    if (!psphotCheckExtSources (config, view)) {
+	psLogMsg ("psphot", 3, "failure to select possible PSF sources (external or internal)");
+	return psphotReadoutCleanup (config, view);
     }
 
-    // use bright stellar objects to measure PSF
-    // XXX if we do not have enough stars to generate the PSF, build one
-    // from the SEEING guess and model class
-    pmPSF *psf = psphotChoosePSF (readout, sources, recipe);
-    if (psf == NULL) {
+    // Use bright stellar objects to measure PSF. If we do not have enough stars to generate
+    // the PSF, build one from the SEEING guess and model class
+    if (!psphotChoosePSF (config, view)) {
 	psLogMsg ("psphot", 3, "failure to construct a psf model");
-	return psphotReadoutCleanup (config, readout, recipe, detections, psf, sources);
+	return psphotReadoutCleanup (config, view);
     }
-    psphotVisualShowPSFModel (readout, psf);
-    return psphotReadoutCleanup (config, readout, recipe, detections, psf, sources);
-}
 
     // measure aperture photometry corrections
-    // XXX isn't this part of the PSF??
-    // if (!psphotApResid (config, readout, sources, psf)) {
-    //     psLogMsg ("psphot", 3, "failed on psphotApResid");
-    //     return psphotReadoutCleanup (config, readout, recipe, detections, psf, sources);
-    // }
+# if 0
+    if (!psphotApResid (config, view)) {
+        psLogMsg ("psphot", 3, "failed on psphotApResid");
+        return psphotReadoutCleanup (config, view);
+    }
+# endif
+
+    return psphotReadoutCleanup (config, view);
+}
