Index: /trunk/psphot/src/psphotApResid.c
===================================================================
--- /trunk/psphot/src/psphotApResid.c	(revision 7341)
+++ /trunk/psphot/src/psphotApResid.c	(revision 7342)
@@ -100,8 +100,17 @@
 	pmPSF_MaskApTrend (psf->ApTrend, PM_PSF_CONSTANT);
 	psf->ApTrend  = psVectorClipFitPolynomial4D (psf->ApTrend, stats, mask, PSFTRY_MASK_ALL, apResid, dMag, xPos, yPos, r2rflux, flux);
-
-	stats->clipIter = 3;
-	pmPSF_MaskApTrend (psf->ApTrend, PM_PSF_CONSTANT);
-	psf->ApTrend  = psVectorChiClipFitPolynomial4D (psf->ApTrend, stats, mask, PSFTRY_MASK_ALL, apResid, dMag, xPos, yPos, r2rflux, flux);
+	if (psf->ApTrend == NULL) {
+	    psError(PSPHOT_ERR_PHOTOM, false, "clipping, fitting nothing");
+	    return false;
+	}
+
+	// apply the fit
+	stats->clipIter = 3;
+	pmPSF_MaskApTrend (psf->ApTrend, PM_PSF_CONSTANT);
+	psf->ApTrend  = psVectorChiClipFitPolynomial4D (psf->ApTrend, stats, mask, PSFTRY_MASK_ALL, apResid, dMag, xPos, yPos, r2rflux, flux);
+	if (psf->ApTrend == NULL) {
+	    psError(PSPHOT_ERR_PHOTOM, false, "Fitting aperture correction");
+	    return false;
+	}
     }
 
@@ -112,4 +121,8 @@
 	pmPSF_MaskApTrend (psf->ApTrend, PM_PSF_CONSTANT);
 	psf->ApTrend  = psVectorClipFitPolynomial4D (psf->ApTrend, stats, mask, PSFTRY_MASK_ALL, apResid, dMag, xPos, yPos, r2rflux, flux);
+	if (psf->ApTrend == NULL) {
+	    psError(PSPHOT_ERR_PHOTOM, false, "clipping, fitting nothing");
+	    return false;
+	}
 
 	// apply the fit
@@ -117,4 +130,8 @@
 	pmPSF_MaskApTrend (psf->ApTrend, PM_PSF_SKYBIAS);
 	psf->ApTrend  = psVectorChiClipFitPolynomial4D (psf->ApTrend, stats, mask, PSFTRY_MASK_ALL, apResid, dMag, xPos, yPos, r2rflux, flux);
+	if (psf->ApTrend == NULL) {
+	    psError(PSPHOT_ERR_PHOTOM, false, "clipping, fitting sky bias");
+	    return false;
+	}
     }
 
@@ -124,4 +141,8 @@
 	pmPSF_MaskApTrend (psf->ApTrend, PM_PSF_CONSTANT);
 	psf->ApTrend  = psVectorClipFitPolynomial4D (psf->ApTrend, stats, mask, PSFTRY_MASK_ALL, apResid, dMag, xPos, yPos, r2rflux, flux);
+	if (psf->ApTrend == NULL) {
+	    psError(PSPHOT_ERR_PHOTOM, false, "clipping, fitting nothing");
+	    return false;
+	}
 
 	// apply the fit
@@ -129,4 +150,8 @@
 	pmPSF_MaskApTrend (psf->ApTrend, PM_PSF_SKYBIAS);
 	psf->ApTrend  = psVectorChiClipFitPolynomial4D (psf->ApTrend, stats, mask, PSFTRY_MASK_ALL, apResid, dMag, xPos, yPos, r2rflux, flux);
+	if (psf->ApTrend == NULL) {
+	    psError(PSPHOT_ERR_PHOTOM, false, "clipping, fitting sky bias");
+	    return false;
+	}
 
 	// apply the fit
@@ -134,4 +159,8 @@
 	pmPSF_MaskApTrend (psf->ApTrend, PM_PSF_SKYSAT);
 	psf->ApTrend  = psVectorChiClipFitPolynomial4D (psf->ApTrend, stats, mask, PSFTRY_MASK_ALL, apResid, dMag, xPos, yPos, r2rflux, flux);
+	if (psf->ApTrend == NULL) {
+	    psError(PSPHOT_ERR_PHOTOM, false, "clipping, fitting skysat");
+	    return false;
+	}
     }
 
@@ -142,4 +171,8 @@
 	pmPSF_MaskApTrend (psf->ApTrend, PM_PSF_CONSTANT);
 	psf->ApTrend  = psVectorClipFitPolynomial4D (psf->ApTrend, stats, mask, PSFTRY_MASK_ALL, apResid, dMag, xPos, yPos, r2rflux, flux);
+	if (psf->ApTrend == NULL) {
+	    psError(PSPHOT_ERR_PHOTOM, false, "clipping, fitting nothing");
+	    return false;
+	}
 
 	// apply the fit
@@ -147,4 +180,8 @@
 	pmPSF_MaskApTrend (psf->ApTrend, PM_PSF_XY_LIN);
 	psf->ApTrend  = psVectorChiClipFitPolynomial4D (psf->ApTrend, stats, mask, PSFTRY_MASK_ALL, apResid, dMag, xPos, yPos, r2rflux, flux);
+	if (psf->ApTrend == NULL) {
+	    psError(PSPHOT_ERR_PHOTOM, false, "fitting, XY_LIN");
+	    return false;
+	}
     }
 
@@ -155,4 +192,8 @@
 	pmPSF_MaskApTrend (psf->ApTrend, PM_PSF_CONSTANT);
 	psf->ApTrend  = psVectorClipFitPolynomial4D (psf->ApTrend, stats, mask, PSFTRY_MASK_ALL, apResid, dMag, xPos, yPos, r2rflux, flux);
+	if (psf->ApTrend == NULL) {
+	    psError(PSPHOT_ERR_PHOTOM, false, "clipping, fitting nothing");
+	    return false;
+	}
 
 	// apply the fit
@@ -160,4 +201,8 @@
 	pmPSF_MaskApTrend (psf->ApTrend, PM_PSF_XY_QUAD);
 	psf->ApTrend  = psVectorChiClipFitPolynomial4D (psf->ApTrend, stats, mask, PSFTRY_MASK_ALL, apResid, dMag, xPos, yPos, r2rflux, flux);
+	if (psf->ApTrend == NULL) {
+	    psError(PSPHOT_ERR_PHOTOM, false, "Fitting XY_QUAD");
+	    return false;
+	}
     }
 
@@ -168,4 +213,8 @@
 	pmPSF_MaskApTrend (psf->ApTrend, PM_PSF_CONSTANT);
 	psf->ApTrend  = psVectorClipFitPolynomial4D (psf->ApTrend, stats, mask, PSFTRY_MASK_ALL, apResid, dMag, xPos, yPos, r2rflux, flux);
+	if (psf->ApTrend == NULL) {
+	    psError(PSPHOT_ERR_PHOTOM, false, "clipping, fitting nothing");
+	    return false;
+	}
 
 	// apply the fit
@@ -173,4 +222,8 @@
 	pmPSF_MaskApTrend (psf->ApTrend, PM_PSF_SKY_XY_LIN);
 	psf->ApTrend  = psVectorChiClipFitPolynomial4D (psf->ApTrend, stats, mask, PSFTRY_MASK_ALL, apResid, dMag, xPos, yPos, r2rflux, flux);
+	if (psf->ApTrend == NULL) {
+	    psError(PSPHOT_ERR_PHOTOM, false, "Fitting sky xy_lin");
+	    return false;
+	}
     }
 
@@ -181,4 +234,8 @@
 	pmPSF_MaskApTrend (psf->ApTrend, PM_PSF_CONSTANT);
 	psf->ApTrend  = psVectorClipFitPolynomial4D (psf->ApTrend, stats, mask, PSFTRY_MASK_ALL, apResid, dMag, xPos, yPos, r2rflux, flux);
+	if (psf->ApTrend == NULL) {
+	    psError(PSPHOT_ERR_PHOTOM, false, "clipping, fitting nothing");
+	    return false;
+	}
 
 	// apply the fit
@@ -186,4 +243,8 @@
 	pmPSF_MaskApTrend (psf->ApTrend, PM_PSF_SKYBIAS);
 	psf->ApTrend  = psVectorChiClipFitPolynomial4D (psf->ApTrend, stats, mask, PSFTRY_MASK_ALL, apResid, dMag, xPos, yPos, r2rflux, flux);
+	if (psf->ApTrend == NULL) {
+	    psError(PSPHOT_ERR_PHOTOM, false, "clipping, fitting sky bias");
+	    return false;
+	}
 
 	// apply the fit
@@ -191,4 +252,8 @@
 	pmPSF_MaskApTrend (psf->ApTrend, PM_PSF_SKYSAT_XY_LIN);
 	psf->ApTrend  = psVectorChiClipFitPolynomial4D (psf->ApTrend, stats, mask, PSFTRY_MASK_ALL, apResid, dMag, xPos, yPos, r2rflux, flux);
+	if (psf->ApTrend == NULL) {
+	    psError(PSPHOT_ERR_PHOTOM, false, "Fitting skyset xy_lin");
+	    return false;
+	}
     }
 
@@ -198,4 +263,8 @@
 	pmPSF_MaskApTrend (psf->ApTrend, PM_PSF_CONSTANT);
 	psf->ApTrend  = psVectorClipFitPolynomial4D (psf->ApTrend, stats, mask, PSFTRY_MASK_ALL, apResid, dMag, xPos, yPos, r2rflux, flux);
+	if (psf->ApTrend == NULL) {
+	    psError(PSPHOT_ERR_PHOTOM, false, "constant");
+	    return false;
+	}
 
 	// fit just SkyBias and clip out objects which are too far from the median 
@@ -203,4 +272,8 @@
 	pmPSF_MaskApTrend (psf->ApTrend, PM_PSF_SKYBIAS);
 	psf->ApTrend  = psVectorChiClipFitPolynomial4D (psf->ApTrend, stats, mask, PSFTRY_MASK_ALL, apResid, dMag, xPos, yPos, r2rflux, flux);
+	if (psf->ApTrend == NULL) {
+	    psError(PSPHOT_ERR_PHOTOM, false, "fitting skyBias");
+	    return false;
+	}
 
 	// finally, fit x, y, SkyBias and clip out objects which are too far from the median 
@@ -208,4 +281,8 @@
 	pmPSF_MaskApTrend (psf->ApTrend, PM_PSF_ALL);
 	psf->ApTrend  = psVectorChiClipFitPolynomial4D (psf->ApTrend, stats, mask, PSFTRY_MASK_ALL, apResid, dMag, xPos, yPos, r2rflux, flux);
+	if (psf->ApTrend == NULL) {
+	    psError(PSPHOT_ERR_PHOTOM, false, "fitting all");
+	    return false;
+	}
     }
 
Index: /trunk/psphot/src/psphotReadout.c
===================================================================
--- /trunk/psphot/src/psphotReadout.c	(revision 7341)
+++ /trunk/psphot/src/psphotReadout.c	(revision 7342)
@@ -11,5 +11,7 @@
     // XXX does this need to invoke I/O?
     // XXX move this input the psphotImageLoop level?
-    pmReadoutSetMaskWeight (readout);
+    if (!pmReadoutSetMaskWeight (readout)) {
+	return false;
+    }
 
     // I have a valid mask, now mask in the analysis region of interest
@@ -59,5 +61,8 @@
 
     // measure aperture photometry corrections
-    psphotApResid (readout, sources, recipe, psf);
+    if (!psphotApResid (readout, sources, recipe, psf)) {
+	psError(PSPHOT_ERR_PHOTOM, false, "Measure aperture photometry corrections");
+	return false;
+    }
 
     // calculate source magnitudes
