Index: trunk/psModules/src/objects/pmPSFtryMakePSF.c
===================================================================
--- trunk/psModules/src/objects/pmPSFtryMakePSF.c	(revision 29004)
+++ trunk/psModules/src/objects/pmPSFtryMakePSF.c	(revision 30031)
@@ -50,5 +50,5 @@
 Note: some of the array entries may be NULL (failed fits); ignore them.
  *****************************************************************************/
-bool pmPSFtryMakePSF (pmPSFtry *psfTry)
+bool pmPSFtryMakePSF (bool *goodFit, pmPSFtry *psfTry)
 {
     PS_ASSERT_PTR_NON_NULL(psfTry, false);
@@ -74,8 +74,14 @@
 
     // fit the shape parameters (SXX, SYY, SXY) as a function of position
-    if (!pmPSFFitShapeParams (psf, psfTry->sources, x, y, srcMask)) {
+    if (!pmPSFFitShapeParams (goodFit, psf, psfTry->sources, x, y, srcMask)) {
         psFree(x);
         psFree(y);
         return false;
+    }
+    if (!goodFit) {
+	psWarning ("poor fit to PSF shape parameters for trend order %d, %d, skipping\n", psf->trendNx, psf->trendNy);
+	psFree(x);
+	psFree(y);
+	return true;
     }
 
@@ -115,5 +121,5 @@
         // the mask is carried from previous steps and updated with this operation
         // the weight is either the flux error or NULL, depending on 'psf->poissonErrorParams'
-        if (!pmTrend2DFit (trend, srcMask, 0xff, x, y, z, NULL)) {
+        if (!pmTrend2DFit (goodFit, trend, srcMask, 0xff, x, y, z, NULL)) {
             psError(PS_ERR_UNKNOWN, false, "failed to build psf model for parameter %d", i);
             psFree(x);
@@ -122,4 +128,13 @@
             return false;
         }
+	if (!goodFit) {
+	    // if we do not get a good fit (but do not actually hit an error), 
+	    // tell the calling program to try something else
+	    psWarning ("poor fit to PSF parameter %d for trend order %d, %d, skipping\n", i, psf->trendNx, psf->trendNy);
+            psFree(x);
+            psFree(y);
+            psFree(z);
+            return true;
+	}
 	if (trend->mode == PM_TREND_MAP) {
 	    // p_psImagePrint (2, trend->map->map, "param N Before"); // XXX TEST:
@@ -163,5 +178,5 @@
 
 // fit the shape parameters using the supplied order (pmPSF->trendNx,trendNy)
-bool pmPSFFitShapeParams (pmPSF *psf, psArray *sources, psVector *x, psVector *y, psVector *srcMask) {
+bool pmPSFFitShapeParams (bool *goodFit, pmPSF *psf, psArray *sources, psVector *x, psVector *y, psVector *srcMask) {
 
     // we are doing a robust fit.  after each pass, we drop points which are more deviant than
@@ -219,5 +234,11 @@
 	trend = psf->params->data[PM_PAR_E0];
 	trend->stats->clipIter = 1; // in allocation, this value is set to the value of nIter, but we should use 1 here
-	status &= pmTrend2DFit (trend, srcMask, 0xff, x, y, e0, NULL);
+	status &= pmTrend2DFit (goodFit, trend, srcMask, 0xff, x, y, e0, NULL);
+	if (!goodFit) {
+	    psFree (e0);
+	    psFree (e1);
+	    psFree (e2);
+	    return true;
+	}
 	mean = psStatsGetValue (trend->stats, meanOption);
 	stdev = psStatsGetValue (trend->stats, stdevOption);
@@ -228,5 +249,11 @@
 	trend = psf->params->data[PM_PAR_E1];
 	trend->stats->clipIter = 1; // in allocation, this value is set to the value of nIter, but we should use 1 here
-	status &= pmTrend2DFit (trend, srcMask, 0xff, x, y, e1, NULL);
+	status &= pmTrend2DFit (goodFit, trend, srcMask, 0xff, x, y, e1, NULL);
+	if (!goodFit) {
+	    psFree (e0);
+	    psFree (e1);
+	    psFree (e2);
+	    return true;
+	}
 	mean = psStatsGetValue (trend->stats, meanOption);
 	stdev = psStatsGetValue (trend->stats, stdevOption);
@@ -237,5 +264,11 @@
 	trend = psf->params->data[PM_PAR_E2];
 	trend->stats->clipIter = 1; // in allocation, this value is set to the value of nIter, but we should use 1 here
-	status &= pmTrend2DFit (trend, srcMask, 0xff, x, y, e2, NULL);
+	status &= pmTrend2DFit (goodFit, trend, srcMask, 0xff, x, y, e2, NULL);
+	if (!goodFit) {
+	    psFree (e0);
+	    psFree (e1);
+	    psFree (e2);
+	    return true;
+	}
 	mean = psStatsGetValue (trend->stats, meanOption);
 	stdev = psStatsGetValue (trend->stats, stdevOption);
@@ -246,4 +279,7 @@
 	if (!status) {
 	    psError (PS_ERR_UNKNOWN, true, "failed to fit PSF shape params");
+	    psFree (e0);
+	    psFree (e1);
+	    psFree (e2);
 	    return false;
 	}
