Index: trunk/psphot/src/psphotReadout.c
===================================================================
--- trunk/psphot/src/psphotReadout.c	(revision 11230)
+++ trunk/psphot/src/psphotReadout.c	(revision 11263)
@@ -5,13 +5,20 @@
     // select the current recipe
     psMetadata *recipe  = psMetadataLookupPtr (NULL, config->recipes, PSPHOT_RECIPE);
+    if (!recipe) {
+        psError(PSPHOT_ERR_CONFIG, false, "missing recipe %s", PSPHOT_RECIPE);
+        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
     char *breakPt = psMetadataLookupStr (NULL, recipe, "BREAK_POINT");
+    PS_ASSERT_PTR_NON_NULL (breakPt, false);
 
     // generate mask & weight images if they don't already exit
     if (!pmReadoutGenerateMaskWeight (readout, true)) {
+        psError (PSPHOT_ERR_CONFIG, false, "trouble creating mask and/or weight");
         return false;
     }
@@ -34,5 +41,7 @@
 
     // generate a background model (median, smoothed image)
-    psphotImageMedian (config, view);
+    if (!psphotImageMedian (config, view)) {
+        return psphotReadoutCleanup (config, readout, recipe, NULL, NULL);
+    }
 
     if (!strcasecmp (breakPt, "BACKMDL")) {
@@ -40,12 +49,14 @@
     }
 
-    pmReadout *background = psphotSelectBackground (config, view);
-
     // find the peaks in the image
     psArray *peaks = psphotFindPeaks (readout, recipe, 1);
+    if (!peaks) {
+	psLogMsg ("psphot", 3, "unable to find peaks in this image");
+	return psphotReadoutCleanup (config, readout, recipe, NULL, NULL);
+    }
 
     // construct sources and measure basic stats
-    // limit moments analysis by S/N?
     psArray *sources = psphotSourceStats (readout, recipe, peaks);
+    if (!sources) return false;
     psFree (peaks);
 
@@ -55,13 +66,13 @@
 
     // mark blended peaks PS_SOURCE_BLEND
-    psphotBasicDeblend (sources, recipe);
+    if (!psphotBasicDeblend (sources, recipe)) {
+	psLogMsg ("psphot", 3, "failed on deblend analysis");
+	return psphotReadoutCleanup (config, readout, recipe, NULL, sources);
+    }
 
     // classify sources based on moments, brightness
     if (!psphotRoughClass (sources, recipe)) {
-        psFree (sources);
-        psLogMsg ("psphot", 3, "failed to find a valid PSF clump for image");
-        pmFPAfileDropInternal (config->files, "PSPHOT.BACKMDL");
-        pmFPAfileDropInternal (config->files, "PSPHOT.BACKGND");
-        return false;
+	psLogMsg ("psphot", 3, "failed to find a valid PSF clump for image");
+	return psphotReadoutCleanup (config, readout, recipe, NULL, sources);
     }
     if (!strcasecmp (breakPt, "MOMENTS")) {
@@ -72,9 +83,9 @@
     pmPSF *psf = psphotChoosePSF (readout, sources, recipe);
     if (psf == NULL) {
-        psError(PSPHOT_ERR_UNKNOWN, false, "failure to construct a psf model");
-        return false;
+	psLogMsg ("psphot", 3, "failure to construct a psf model");
+	return psphotReadoutCleanup (config, readout, recipe, psf, sources);
     }
     if (!strcasecmp (breakPt, "PSFMODEL")) {
-        return psphotReadoutCleanup(config, readout, recipe, psf, sources);
+        return psphotReadoutCleanup (config, readout, recipe, psf, sources);
     }
 
@@ -142,4 +153,6 @@
 
     // calculate source magnitudes
+    // XXX modify this API to take config, view?
+    pmReadout *background = psphotSelectBackground (config, view);
     psphotMagnitudes(sources, recipe, psf, background);
 
