Index: trunk/psModules/src/objects/pmPSFtryModel.c
===================================================================
--- trunk/psModules/src/objects/pmPSFtryModel.c	(revision 25754)
+++ trunk/psModules/src/objects/pmPSFtryModel.c	(revision 25818)
@@ -70,5 +70,5 @@
         psFree(psfTry);
         return NULL;
-    }      
+    }
 
     int orderMin = (options->psfTrendMode == PM_TREND_MAP) ? 1 : 0;
@@ -80,12 +80,13 @@
     if ((sources->n <  8) && (orderMax >= 1)) orderMax = 0;
     if ((sources->n <  3)) {
-	psError (PS_ERR_UNKNOWN, true, "failed to determine PSF parameters");
-	return NULL;
-    }
+        psError (PS_ERR_UNKNOWN, true, "failed to determine PSF parameters");
+        return NULL;
+    }
+    orderMin = PS_MIN(orderMin, orderMax);
 
     // save the raw source mask (generated by pmPSFtryFitEXT)
     psVector *srcMask = psVectorCopy (NULL, psfTry->mask, PS_TYPE_VECTOR_MASK);
 
-    // we will save the PSF with the best fit (min systematic error) 
+    // we will save the PSF with the best fit (min systematic error)
     pmPSF *minPSF = NULL;
     psVector *minMask = NULL;
@@ -96,53 +97,53 @@
     int Ny = options->psfTrendNy;
     for (int i = orderMin; i <= orderMax; i++) {
-	
-	if (Nx > Ny) {
-	    options->psfTrendNx = i;
-	    options->psfTrendNy = PS_MAX (orderMin, (int)(i * (Ny / Nx) + 0.5));
-	} else {
-	    options->psfTrendNy = i;
-	    options->psfTrendNx = PS_MAX (orderMin, (int)(i * (Nx / Ny) + 0.5));
-	}
-
-	// free existing data, if any
-	psFree(psfTry->psf);
-	psFree(psfTry->mask);
-
-	// allocate a mask and a psf model using the current Nx,Ny order values;
-	psfTry->psf = pmPSFAlloc (options);
-	psfTry->mask = psVectorCopy (NULL, srcMask, PS_TYPE_VECTOR_MASK);
-
-	// stage 2: construct a psf (pmPSF) from this collection of model fits, including the 2D variation
-	if (!pmPSFtryMakePSF (psfTry)) {
-	    psError(PS_ERR_UNKNOWN, false, "failed to construct a psf model from collection of sources");
-	    psFree(psfTry);
-	    return NULL;
-	}
-
-	// stage 3: refit with fixed shape parameters, measure pmPSFtry->metric
-	if (!pmPSFtryFitPSF (psfTry, options, maskVal, markVal)) {
-	    psError(PS_ERR_UNKNOWN, false, "failed to construct a psf model from collection of sources");
-	    psFree(psfTry);
-	    return NULL;
-	}
-
-	// stage 4: measure systematic error in pmPSFtry->metric
-	if (!pmPSFtryMetric (psfTry)) {
-	    psError(PS_ERR_UNKNOWN, false, "failed to measure systematic error of metric");
-	    psFree(psfTry);
-	    return NULL;
-	}
-
-	if (!minPSF) {
-	    minPSF = psMemIncrRefCounter(psfTry->psf);
-	    minMask = psMemIncrRefCounter(psfTry->mask);
-	}
-
-	if (psfTry->psf->dApResid < minPSF->dApResid) {
-	    psFree (minPSF);
-	    psFree (minMask);
-	    minPSF = psMemIncrRefCounter(psfTry->psf);
-	    minMask = psMemIncrRefCounter(psfTry->mask);
-	}
+
+        if (Nx > Ny) {
+            options->psfTrendNx = i;
+            options->psfTrendNy = PS_MAX (orderMin, (int)(i * (Ny / Nx) + 0.5));
+        } else {
+            options->psfTrendNy = i;
+            options->psfTrendNx = PS_MAX (orderMin, (int)(i * (Nx / Ny) + 0.5));
+        }
+
+        // free existing data, if any
+        psFree(psfTry->psf);
+        psFree(psfTry->mask);
+
+        // allocate a mask and a psf model using the current Nx,Ny order values;
+        psfTry->psf = pmPSFAlloc (options);
+        psfTry->mask = psVectorCopy (NULL, srcMask, PS_TYPE_VECTOR_MASK);
+
+        // stage 2: construct a psf (pmPSF) from this collection of model fits, including the 2D variation
+        if (!pmPSFtryMakePSF (psfTry)) {
+            psError(PS_ERR_UNKNOWN, false, "failed to construct a psf model from collection of sources");
+            psFree(psfTry);
+            return NULL;
+        }
+
+        // stage 3: refit with fixed shape parameters, measure pmPSFtry->metric
+        if (!pmPSFtryFitPSF (psfTry, options, maskVal, markVal)) {
+            psError(PS_ERR_UNKNOWN, false, "failed to construct a psf model from collection of sources");
+            psFree(psfTry);
+            return NULL;
+        }
+
+        // stage 4: measure systematic error in pmPSFtry->metric
+        if (!pmPSFtryMetric (psfTry)) {
+            psError(PS_ERR_UNKNOWN, false, "failed to measure systematic error of metric");
+            psFree(psfTry);
+            return NULL;
+        }
+
+        if (!minPSF) {
+            minPSF = psMemIncrRefCounter(psfTry->psf);
+            minMask = psMemIncrRefCounter(psfTry->mask);
+        }
+
+        if (psfTry->psf->dApResid < minPSF->dApResid) {
+            psFree (minPSF);
+            psFree (minMask);
+            minPSF = psMemIncrRefCounter(psfTry->psf);
+            minMask = psMemIncrRefCounter(psfTry->mask);
+        }
     }
     psFree (srcMask);
@@ -153,5 +154,5 @@
     psfTry->psf = minPSF;
     psfTry->mask = minMask;
-    
+
     // XXXXX this is probably not used any more.  Are the chisq of the fits so bad? can we
     // fix them by softening the errors on the brightest pixels?
