Index: /trunk/psastro/src/psastroMosaicOneChip.c
===================================================================
--- /trunk/psastro/src/psastroMosaicOneChip.c	(revision 15258)
+++ /trunk/psastro/src/psastroMosaicOneChip.c	(revision 15259)
@@ -11,4 +11,7 @@
     bool status;
     char errorWord[64];
+    char orderWord[64];
+
+    assert (iteration < 4);
 
     PS_ASSERT_PTR_NON_NULL(chip,    false);
@@ -31,34 +34,51 @@
 
     // allowed limits for valid solutions
-    sprintf (errorWord, "PSASTRO.MOSAIC.MAX.ERROR.N%d", iteration);
+    snprintf (errorWord, 64, "PSASTRO.MOSAIC.MAX.ERROR.N%d", iteration);
     REQUIRED_RECIPE_VALUE (float maxError, errorWord, F32, "failed to find single-chip max allowed error\n");
     REQUIRED_RECIPE_VALUE (int minNstar, "PSASTRO.MOSAIC.MIN.NSTAR", S32, "failed to find single-chip min allowed stars\n");
 
     // set the order of the per-chip fit (higher order only if iteration > 0)
-    int order = 1;
-    if (iteration > 0) {
-	// select the desired chip order
-	REQUIRED_RECIPE_VALUE (order, "PSASTRO.MOSAIC.CHIP.ORDER", S32, "failed to find mosaic chip-level fit order\n");
+    REQUIRED_RECIPE_VALUE (int defaultOrder, "PSASTRO.MOSAIC.CHIP.ORDER", S32, "failed to find mosaic chip-level fit default order\n");
 
-	// modify the order to correspond to the actual number of matched stars:
-	if ((match->n < 15) && (order >= 3)) order = 2;
-	if ((match->n < 11) && (order >= 2)) order = 1;
-	if ((match->n <  8) && (order >= 1)) order = 0;
-	if (order < 1) {
-	    psLogMsg ("psastro", 3, "insufficient stars or invalid order: %ld stars", match->n); 
-	    return false; 
-	} 
+    snprintf (orderWord, 64, "PSASTRO.MOSAIC.CHIP.ORDER.N%d", iteration);
+    int order = psMetadataLookupS32 (&status, recipe, orderWord);
+    if (!status || (order == -1)) {
+	order = defaultOrder;
     }
 
-    // create output toFPA; set masks appropriate to the Elixir DVO astrometry format
-    psFree (chip->toFPA);
-    chip->toFPA = psPlaneTransformAlloc (order, order);
-    for (int i = 0; i <= chip->toFPA->x->nX; i++) {
-        for (int j = 0; j <= chip->toFPA->x->nY; j++) {
-            if (i + j > order) {
-		chip->toFPA->x->mask[i][j] = 1;
-		chip->toFPA->y->mask[i][j] = 1;
-            }
-        }
+    // modify the order to correspond to the actual number of matched stars:
+    if ((match->n < 15) && (order >= 3)) order = 2;
+    if ((match->n < 11) && (order >= 2)) order = 1;
+    if ((match->n <  8) && (order >= 1)) order = 0;
+    if ((match->n <  3) || (order < 0) || (order > 3)) {
+	psLogMsg ("psastro", 3, "insufficient stars (%ld) or invalid order (%d)", match->n, order); 
+	return false; 
+    } 
+
+    // create output toFPA; set masks appropriate to the Elixir DVO astrometry format if we are
+    // fitting 0th order, use the current polynomial of whatever order, with higher order
+    // coefficients frozen to the current values
+    if (order == 0) {
+	// set FIT mask for all higher order terms of the existing solution
+	// any existing SET masks will be retained.
+	for (int i = 0; i <= chip->toFPA->x->nX; i++) {
+	    for (int j = 0; j <= chip->toFPA->x->nY; j++) {
+		if (i + j > 0) {
+		    chip->toFPA->x->coeffMask[i][j] |= PS_POLY_MASK_FIT;
+		    chip->toFPA->y->coeffMask[i][j] |= PS_POLY_MASK_FIT;
+		}
+	    }
+	}
+    } else {
+	psFree (chip->toFPA);
+	chip->toFPA = psPlaneTransformAlloc (order, order);
+	for (int i = 0; i <= chip->toFPA->x->nX; i++) {
+	    for (int j = 0; j <= chip->toFPA->x->nY; j++) {
+		if (i + j > order) {
+		    chip->toFPA->x->coeffMask[i][j] = PS_POLY_MASK_SET;
+		    chip->toFPA->y->coeffMask[i][j] = PS_POLY_MASK_SET;
+		}
+	    }
+	}
     }
 
