Index: /trunk/psphot/src/psphotApResid.c
===================================================================
--- /trunk/psphot/src/psphotApResid.c	(revision 15077)
+++ /trunk/psphot/src/psphotApResid.c	(revision 15078)
@@ -29,7 +29,14 @@
 
     // XXX is this still needed?  the pmTrend2D stuff should be auto-adjusting...
-    int NSTAR_APERTURE_CORRECTION_MIN = psMetadataLookupS32(&status, recipe, "NSTAR_APERTURE_CORRECTION_MIN");
-    if (!status) {
-        NSTAR_APERTURE_CORRECTION_MIN = 5;
+    int APTREND_NSTAR_MIN = psMetadataLookupS32(&status, recipe, "APTREND.NSTAR.MIN");
+    assert (status);
+
+    // maximum order for aperture correction
+    int APTREND_ORDER_MAX = psMetadataLookupS32(&status, recipe, "APTREND.ORDER.MAX");
+    assert (status);
+
+    if (APTREND_ORDER_MAX < 1) {
+        psError(PSPHOT_ERR_CONFIG, true, "APTREND.ORDER.MAX must be 1 or more");
+        return false;
     }
 
@@ -116,7 +123,7 @@
 
     // XXX choose a better value here?
-    if (Npsf < NSTAR_APERTURE_CORRECTION_MIN) {
+    if (Npsf < APTREND_NSTAR_MIN) {
         psError(PSPHOT_ERR_APERTURE, true, "Only %d valid aperture residual sources (need %d), giving up",
-                Npsf, NSTAR_APERTURE_CORRECTION_MIN);
+                Npsf, APTREND_NSTAR_MIN);
         return false;
     }
@@ -132,8 +139,7 @@
     int entryMin = -1;
 
-    // *** iterate over spatial scale until error Floor increases
-    // *** fit out the dap vs mag trend?
-    // *** stop if Npsf / (Nx * Ny) < 3
-    for (int i = 1; i < 10; i++) {
+    // Fit out the dap vs mag trend, iterate over spatial scale until error Floor increases.
+    // Stop if Npsf / (Nx * Ny) < 3
+    for (int i = 1; i <= APTREND_ORDER_MAX; i++) {
 
 	if (!psphotApResidTrend (readout, psf, Npsf, i, &errorScale, &errorFloor, mask, xPos, yPos, apResid, dMag)) {
