Index: /trunk/psphot/src/psphotApResid.c
===================================================================
--- /trunk/psphot/src/psphotApResid.c	(revision 26260)
+++ /trunk/psphot/src/psphotApResid.c	(revision 26261)
@@ -217,8 +217,21 @@
     }
 
-    // XXX set the min number of needed source more carefully
-    if ((Npsf < 15) && (APTREND_ORDER_MAX >= 4)) APTREND_ORDER_MAX = 3;
-    if ((Npsf < 11) && (APTREND_ORDER_MAX >= 3)) APTREND_ORDER_MAX = 2;
-    if ((Npsf <  8) && (APTREND_ORDER_MAX >= 2)) APTREND_ORDER_MAX = 1;
+    // this is a bit tricky, because we have two cases (MAP vs POLY), and they have a different 
+    // definition for 'order' (order_MAP = order_POLY + 1).  in addition, we have a 
+    // user-specified MAX order, which we should respect, regardless of the mode
+
+    // set the max order (0 = constant) which the number of psf stars can support:
+    // rule of thumb: require 3 stars per 'cell' (order+1)^2
+    int MaxOrderForStars = 0;
+    if (Npsf >= 12) MaxOrderForStars = 1; // 4 cells
+    if (Npsf >= 27) MaxOrderForStars = 2; // 9 cells
+    if (Npsf >= 48) MaxOrderForStars = 3; // 16 cells
+    if (Npsf >  75) MaxOrderForStars = 4; // 25 cells
+
+    pmTrend2DMode mode = PM_TREND_MAP;
+    if (mode == PM_TREND_MAP) {
+	MaxOrderForStars ++;
+    } 
+    APTREND_ORDER_MAX = PS_MIN (APTREND_ORDER_MAX, MaxOrderForStars);
 
     psFree (psf->ApTrend);
