Index: trunk/psModules/src/astrom/pmAstrometryDistortion.c
===================================================================
--- trunk/psModules/src/astrom/pmAstrometryDistortion.c	(revision 11461)
+++ trunk/psModules/src/astrom/pmAstrometryDistortion.c	(revision 12485)
@@ -7,6 +7,6 @@
 *  @author EAM, IfA
 *
-*  @version $Revision: 1.15 $ $Name: not supported by cvs2svn $
-*  @date $Date: 2007-01-31 03:12:27 $
+*  @version $Revision: 1.16 $ $Name: not supported by cvs2svn $
+*  @date $Date: 2007-03-18 22:06:58 $
 *
 *  Copyright 2006 Institute for Astronomy, University of Hawaii
@@ -47,10 +47,8 @@
     psMemSetDeallocator(gradient, (psFreeFunc) pmAstromGradientFree);
 
-    // XXX init any of the data values?
-
     return (gradient);
 }
 
-psArray *pmAstromMeasureGradients(psArray *gradients, psArray *rawstars, psArray *refstars, psArray *matches, psMetadata *config)
+psArray *pmAstromMeasureGradients(psArray *gradients, psArray *rawstars, psArray *refstars, psArray *matches, psRegion *region, int Nx, int Ny)
 {
 
@@ -59,11 +57,8 @@
     }
 
+    // NOTE: region specifies the FP region in pixels covered by the chip (NOT in FP units)
     // determine range
-    // XXX for the moment, I'm hard-wiring this for megacam
-    // XXX we need to get the chip dimensions from the metadata somewhere
-    int Nx = 2;
-    int Ny = 2;
-    int DX = 2080 / Nx;
-    int DY = 4600 / Ny;
+    int DX = (region->x1 - region->x0) / Nx;
+    int DY = (region->y1 - region->y0) / Ny;
 
     psPolynomial2D *local = psPolynomial2DAlloc (PS_POLYNOMIAL_ORD, 1, 1);
@@ -97,12 +92,8 @@
                 pmAstromObj *raw = rawstars->data[match->raw];
 
-                if (raw->chip->x < Xmin)
-                    continue;
-                if (raw->chip->x > Xmax)
-                    continue;
-                if (raw->chip->y < Ymin)
-                    continue;
-                if (raw->chip->y > Ymax)
-                    continue;
+                if (raw->chip->x < Xmin) continue;
+                if (raw->chip->x > Xmax) continue;
+                if (raw->chip->y < Ymin) continue;
+                if (raw->chip->y > Ymax) continue;
 
                 pmAstromObj *ref = refstars->data[match->ref];
@@ -177,5 +168,5 @@
 }
 
-bool pmAstromFitDistortion(pmFPA *fpa, psArray *gradients, psMetadata *config)
+bool pmAstromFitDistortion(pmFPA *fpa, psArray *gradients, double pixelScale)
 {
 
@@ -233,10 +224,14 @@
     if (!psVectorClipFitPolynomial2D (localX, stats, mask, 0xff, dPdL, NULL, L, M)) {
         psLogMsg ("psastro", 3, "failed to fit x-dir gradient\n");
-        return false;
+	psFree (localX);
+	psFree (localY);
+        goto escape;
     }
 
     if (!psVectorClipFitPolynomial2D (localY, stats, mask, 0xff, dPdM, NULL, L, M)) {
         psLogMsg ("psastro", 3, "failed to fit y-dir gradient\n");
-        return false;
+	psFree (localX);
+	psFree (localY);
+        goto escape;
     }
 
@@ -310,5 +305,5 @@
     psFree (localY);
 
-    // XXX free unneeded structures
+    // free unneeded structures
     psFree (dPdL);
     psFree (dPdM);
@@ -320,13 +315,38 @@
     psFree (mask);
 
-    // XXX need to reset the fromTPA terms here
-    // XXX choose an appropriate region based on the range of values
-    // in L and M?
-    // psRegion region = psRegionSet (-125000, +125000, -125000, +125000);
+    // reset the fromTPA terms here. choose an appropriate region based on the dimensions of
+    // the complete FPA
     psRegion *region = pmFPAExtent (fpa);
+    region->x0 *= pixelScale;
+    region->x1 *= pixelScale;
+    region->y0 *= pixelScale;
+    region->y1 *= pixelScale;
+    int xCenter = 0.5*(region->x0 + region->x1);
+    int yCenter = 0.5*(region->y0 + region->y1);
+    region->x0 -= xCenter;
+    region->x1 -= xCenter;
+    region->y0 -= yCenter;
+    region->y1 -= yCenter;
     psFree (fpa->fromTPA);
     fpa->fromTPA = psPlaneTransformInvert(NULL, fpa->toTPA, *region, 50);
     psFree (region);
 
+    if (fpa->fromTPA == NULL) {
+        psError (PS_ERR_UNKNOWN, false, "failed to invert fpa->toTPA\n");
+	return false;
+    }
+
     return true;
+
+escape:
+    // free unneeded structures
+    psFree (dPdL);
+    psFree (dPdM);
+    psFree (dQdL);
+    psFree (dQdM);
+    psFree (L);
+    psFree (M);
+    psFree (stats);
+    psFree (mask);
+    return false;
 }
