Index: trunk/psphot/src/psphotReadout.c
===================================================================
--- trunk/psphot/src/psphotReadout.c	(revision 28160)
+++ trunk/psphot/src/psphotReadout.c	(revision 28375)
@@ -14,5 +14,5 @@
     // by the multiple threads, not the total time used by all threads.
     psTimerStart ("psphotReadout");
-
+    printf("%x %s\n",psErrorCodeLast(),psErrorCodeString(psErrorCodeLast()));
     pmModelClassSetLimits(PM_MODEL_LIMITS_LAX);
 
@@ -26,5 +26,5 @@
     char *breakPt = psMetadataLookupStr (NULL, recipe, "BREAK_POINT");
     psAssert (breakPt, "configuration error: set BREAK_POINT");
-
+    printf("%x %s\n",psErrorCodeLast(),psErrorCodeString(psErrorCodeLast()));
     // set the photcode for this image
     if (!psphotAddPhotcode (config, view, "PSPHOT.INPUT")) {
@@ -32,5 +32,5 @@
         return false;
     }
-
+        printf("%x %s\n",psErrorCodeLast(),psErrorCodeString(psErrorCodeLast()));
     // Generate the mask and weight images, including the user-defined analysis region of interest
     if (!psphotSetMaskAndVariance (config, view, "PSPHOT.INPUT")) {
@@ -40,9 +40,10 @@
         return psphotReadoutCleanup (config, view, "PSPHOT.INPUT");
     }
-
+        printf("%x %s\n",psErrorCodeLast(),psErrorCodeString(psErrorCodeLast()));
     // generate a background model (median, smoothed image)
     if (!psphotModelBackground (config, view, "PSPHOT.INPUT")) {
         return psphotReadoutCleanup (config, view, "PSPHOT.INPUT");
     }
+            printf("%x %s\n",psErrorCodeLast(),psErrorCodeString(psErrorCodeLast()));
     if (!psphotSubtractBackground (config, view, "PSPHOT.INPUT")) {
         return psphotReadoutCleanup (config, view, "PSPHOT.INPUT");
@@ -51,5 +52,5 @@
         return psphotReadoutCleanup (config, view, "PSPHOT.INPUT");
     }
-
+        printf("%x %s\n",psErrorCodeLast(),psErrorCodeString(psErrorCodeLast()));
     // load the psf model, if suppled.  FWHM_X,FWHM_Y,etc are determined and saved on
     // readout->analysis XXX this function currently only works with a single PSPHOT.INPUT
@@ -58,5 +59,5 @@
         return psphotReadoutCleanup (config, view, "PSPHOT.INPUT");
     }
-
+        printf("%x %s\n",psErrorCodeLast(),psErrorCodeString(psErrorCodeLast()));
     // find the detections (by peak and/or footprint) in the image.
     if (!psphotFindDetections (config, view, "PSPHOT.INPUT", true)) { // pass 1
@@ -65,5 +66,5 @@
         return psphotReadoutCleanup (config, view, "PSPHOT.INPUT");
     }
-
+        printf("%x %s\n",psErrorCodeLast(),psErrorCodeString(psErrorCodeLast()));
     // construct sources and measure basic stats (saved on detections->newSources)
     if (!psphotSourceStats (config, view, "PSPHOT.INPUT", true)) { // pass 1
@@ -74,5 +75,5 @@
         return psphotReadoutCleanup (config, view, "PSPHOT.INPUT");
     }
-
+        printf("%x %s\n",psErrorCodeLast(),psErrorCodeString(psErrorCodeLast()));
     // find blended neighbors of very saturated stars (detections->newSources)
     if (!psphotDeblendSatstars (config, view, "PSPHOT.INPUT")) {
@@ -80,5 +81,5 @@
         return psphotReadoutCleanup (config, view, "PSPHOT.INPUT");
     }
-
+        printf("%x %s\n",psErrorCodeLast(),psErrorCodeString(psErrorCodeLast()));
     // mark blended peaks PS_SOURCE_BLEND (detections->newSources)
     if (!psphotBasicDeblend (config, view, "PSPHOT.INPUT")) {
@@ -86,5 +87,5 @@
         return psphotReadoutCleanup (config, view, "PSPHOT.INPUT");
     }
-
+        printf("%x %s\n",psErrorCodeLast(),psErrorCodeString(psErrorCodeLast()));
     // classify sources based on moments, brightness.  if a PSF model has been loaded, the PSF
     // clump defined for it is used not measured (detections->newSources)
@@ -93,4 +94,5 @@
         return psphotReadoutCleanup (config, view, "PSPHOT.INPUT");
     }
+            printf("%x %s\n",psErrorCodeLast(),psErrorCodeString(psErrorCodeLast()));
     // if we were not supplied a PSF model, determine the IQ stats here (detections->newSources)
     if (!psphotImageQuality (config, view, "PSPHOT.INPUT")) { // pass 1
@@ -101,5 +103,5 @@
         return psphotReadoutCleanup (config, view, "PSPHOT.INPUT");
     }
-
+        printf("%x %s\n",psErrorCodeLast(),psErrorCodeString(psErrorCodeLast()));
     // use bright stellar objects to measure PSF if we were supplied a PSF for any input file,
     // this step is skipped
@@ -115,16 +117,16 @@
     // XXX fix this in the new multi-input context
     // psphotLoadExtSources (config, view, "PSPHOT.INPUT"); // pass 1
-
+        printf("%x %s\n",psErrorCodeLast(),psErrorCodeString(psErrorCodeLast()));
     // construct an initial model for each object, set the radius to fitRadius, set circular
     // fit mask (detections->newSources)
     psphotGuessModels (config, view, "PSPHOT.INPUT"); // pass 1
-
+        printf("%x %s\n",psErrorCodeLast(),psErrorCodeString(psErrorCodeLast()));
     // merge the newly selected sources into the existing list
     // NOTE: merge OLD and NEW
     psphotMergeSources (config, view, "PSPHOT.INPUT");
-
+        printf("%x %s\n",psErrorCodeLast(),psErrorCodeString(psErrorCodeLast()));
     // linear PSF fit to source peaks, subtract the models from the image (in PSF mask)
     psphotFitSourcesLinear (config, view, "PSPHOT.INPUT", false); // pass 1 (detections->allSources)
-
+        printf("%x %s\n",psErrorCodeLast(),psErrorCodeString(psErrorCodeLast()));
     // identify CRs and extended sources (only unmeasured sources are measured)
     psphotSourceSize (config, view, "PSPHOT.INPUT", true); // pass 1 (detections->allSources)
@@ -132,36 +134,36 @@
         goto finish;
     }
-
+        printf("%x %s\n",psErrorCodeLast(),psErrorCodeString(psErrorCodeLast()));
     // non-linear PSF and EXT fit to brighter sources
     // replace model flux, adjust mask as needed, fit, subtract the models (full stamp)
     psphotBlendFit (config, view, "PSPHOT.INPUT"); // pass 1 (detections->allSources)
-
+        printf("%x %s\n",psErrorCodeLast(),psErrorCodeString(psErrorCodeLast()));
     // replace all sources
     psphotReplaceAllSources (config, view, "PSPHOT.INPUT"); // pass 1 (detections->allSources)
-
+        printf("%x %s\n",psErrorCodeLast(),psErrorCodeString(psErrorCodeLast()));
     // linear fit to include all sources (subtract again)
     // NOTE : apply to ALL sources (extended + psf)
     psphotFitSourcesLinear (config, view, "PSPHOT.INPUT", true); // pass 2 (detections->allSources)
-
+        printf("%x %s\n",psErrorCodeLast(),psErrorCodeString(psErrorCodeLast()));
     // if we only do one pass, skip to extended source analysis
     if (!strcasecmp (breakPt, "PASS1")) goto pass1finish;
-
+        printf("%x %s\n",psErrorCodeLast(),psErrorCodeString(psErrorCodeLast()));
     // NOTE: possibly re-measure background model here with objects subtracted / or masked
 
     // add noise for subtracted objects
     psphotAddNoise (config, view, "PSPHOT.INPUT"); // pass 1 (detections->allSources)
-
+        printf("%x %s\n",psErrorCodeLast(),psErrorCodeString(psErrorCodeLast()));
     // find fainter sources
     // NOTE: finds new peaks and new footprints, OLD and FULL set are saved on detections
     psphotFindDetections (config, view, "PSPHOT.INPUT", false); // pass 2 (detections->peaks, detections->footprints)
-
+        printf("%x %s\n",psErrorCodeLast(),psErrorCodeString(psErrorCodeLast()));
     // remove noise for subtracted objects (ie, return to normal noise level)
     // NOTE: this needs to operate only on the OLD sources
     psphotSubNoise (config, view, "PSPHOT.INPUT"); // pass 1 (detections->allSources)
-
+        printf("%x %s\n",psErrorCodeLast(),psErrorCodeString(psErrorCodeLast()));
     // define new sources based on only the new peaks
     // NOTE: new sources are saved on detections->newSources
     psphotSourceStats (config, view, "PSPHOT.INPUT", false); // pass 2 (detections->newSources)
-
+        printf("%x %s\n",psErrorCodeLast(),psErrorCodeString(psErrorCodeLast()));
     // set source type
     // NOTE: apply only to detections->newSources
@@ -170,21 +172,21 @@
         return psphotReadoutCleanup (config, view, "PSPHOT.INPUT");
     }
-
+        printf("%x %s\n",psErrorCodeLast(),psErrorCodeString(psErrorCodeLast()));
     // create full input models, set the radius to fitRadius, set circular fit mask
     // NOTE: apply only to detections->newSources
     psphotGuessModels (config, view, "PSPHOT.INPUT"); // pass 2 (detections->newSources)
-
+        printf("%x %s\n",psErrorCodeLast(),psErrorCodeString(psErrorCodeLast()));
     // replace all sources so fit below applies to all at once
     // NOTE: apply only to OLD sources (which have been subtracted)
     psphotReplaceAllSources (config, view, "PSPHOT.INPUT"); // pass 2
-
+        printf("%x %s\n",psErrorCodeLast(),psErrorCodeString(psErrorCodeLast()));
     // merge the newly selected sources into the existing list
     // NOTE: merge OLD and NEW
     // XXX check on free of sources...
     psphotMergeSources (config, view, "PSPHOT.INPUT"); // (detections->newSources + detections->allSources -> detections->allSources)
-
+        printf("%x %s\n",psErrorCodeLast(),psErrorCodeString(psErrorCodeLast()));
     // NOTE: apply to ALL sources
     psphotFitSourcesLinear (config, view, "PSPHOT.INPUT", true); // pass 3 (detections->allSources)
-
+        printf("%x %s\n",psErrorCodeLast(),psErrorCodeString(psErrorCodeLast()));
 pass1finish:
 
@@ -192,13 +194,13 @@
     // NOTE: applies only to NEW (unmeasured) sources
     psphotSourceSize (config, view, "PSPHOT.INPUT", false); // pass 2 (detections->allSources)
-
+        printf("%x %s\n",psErrorCodeLast(),psErrorCodeString(psErrorCodeLast()));
     psphotExtendedSourceAnalysis (config, view, "PSPHOT.INPUT"); // pass 1 (detections->allSources)
     psphotExtendedSourceFits (config, view, "PSPHOT.INPUT"); // pass 1 (detections->allSources)
-
+        printf("%x %s\n",psErrorCodeLast(),psErrorCodeString(psErrorCodeLast()));
 finish:
 
     // plot positive sources
     // psphotSourcePlots (readout, sources, recipe);
-
+        printf("%x %s\n",psErrorCodeLast(),psErrorCodeString(psErrorCodeLast()));
     // measure aperture photometry corrections
     if (!psphotApResid (config, view, "PSPHOT.INPUT")) { // pass 1 (detections->allSources)
@@ -206,8 +208,10 @@
         return psphotReadoutCleanup (config, view, "PSPHOT.INPUT");
     }
-
+        printf("%x %s\n",psErrorCodeLast(),psErrorCodeString(psErrorCodeLast()));
     // calculate source magnitudes
-    psphotMagnitudes(config, view, "PSPHOT.INPUT"); // pass 1 (detections->allSources)
-
+    if (!psphotMagnitudes(config, view, "PSPHOT.INPUT")) { // pass 1 (detections->allSources)
+      psErrorStackPrint(stderr, "Unable to do magnitudes.");
+        psErrorClear();
+    }      
     if (!psphotEfficiency(config, view, "PSPHOT.INPUT")) { // pass 1
         psErrorStackPrint(stderr, "Unable to determine detection efficiencies from fake sources");
@@ -219,9 +223,19 @@
 
     // replace background in residual image
-    psphotSkyReplace (config, view, "PSPHOT.INPUT"); // pass 1
-
+    if (!psphotSkyReplace (config, view, "PSPHOT.INPUT")) { // pass 1
+      psErrorStackPrint(stderr, "Unable to replace sky");
+      psErrorClear();
+
+/*       psLogMsg("psphot", 3, "failed on psphotSkyReplace"); */
+/*       return(psphotReadoutCleanup(config, view, "PSPHOT.INPUT")); */
+    }
     // drop the references to the image pixels held by each source
-    psphotSourceFreePixels (config, view, "PSPHOT.INPUT"); // pass 1
-
+    if (!psphotSourceFreePixels (config, view, "PSPHOT.INPUT")) { // pass 1
+      psErrorStackPrint(stderr, "Unable to free source pixels");
+      psErrorClear();
+
+/*       psLogMsg ("psphot", 3, "failed on psphotSourceFreePixels"); */
+/*       return(psphotReadoutCleanup(config, view, "PSPHOT.INPUT")); */
+    }
     // create the exported-metadata and free local data
     return psphotReadoutCleanup(config, view, "PSPHOT.INPUT");
