Index: trunk/psastro/src/psastroMosaicGradients.c
===================================================================
--- trunk/psastro/src/psastroMosaicGradients.c	(revision 10880)
+++ trunk/psastro/src/psastroMosaicGradients.c	(revision 12492)
@@ -16,4 +16,6 @@
         if (!chip->process || !chip->file_exists) { continue; }
 	
+	psRegion *region = pmChipExtent (chip);
+
 	while ((cell = pmFPAviewNextCell (view, fpa, 1)) != NULL) {
             psTrace ("psastro", 4, "Cell %d: %x %x\n", view->cell, cell->file_exists, cell->process);
@@ -37,7 +39,9 @@
 
 		// measure the local gradients for this set of stars
-		gradients = pmAstromMeasureGradients (gradients, rawstars, refstars, match, recipe);
+		// XXX 2,2 are the number of test boxes on the chip.  put this in the recipe
+		gradients = pmAstromMeasureGradients (gradients, rawstars, refstars, match, region, 2, 2);
 	    }
 	}
+	psFree (region);
     }
 
@@ -59,7 +63,25 @@
     }
 
+    // physical pixel scale in microns per pixel (FP is in physical units, chip is in pixels)
+    double pixelScale = psMetadataLookupF32 (&status, recipe, "PSASTRO.PIXEL.SCALE");
+    if (!status) {
+	psError(PS_ERR_IO, false, "Failed to lookup pixel scale"); 
+	return false; 
+    } 
+
     // fit the measured gradients with the telescope distortion model (polynomial order based on toTPA)
-    pmAstromFitDistortion (fpa, gradients, recipe);
-    psastroMosaicSetAstrom (fpa);
+    if (!pmAstromFitDistortion (fpa, gradients, pixelScale)) {
+	psError(PSASTRO_ERR_UNKNOWN, false, "failed to fit the distortion terms\n");
+	psFree (gradients);
+	psFree (view);
+        return false;
+    }
+	
+    if (!psastroMosaicSetAstrom (fpa)) {
+	psError(PSASTRO_ERR_UNKNOWN, false, "failed to apply mosaic distortion terms\n");
+	psFree (gradients);
+	psFree (view);
+        return false;
+    }
 
     psFree (gradients);
