Index: trunk/psModules/src/astrom/pmAstrometryDistortion.c
===================================================================
--- trunk/psModules/src/astrom/pmAstrometryDistortion.c	(revision 10859)
+++ trunk/psModules/src/astrom/pmAstrometryDistortion.c	(revision 10872)
@@ -7,6 +7,6 @@
 *  @author EAM, IfA
 *
-*  @version $Revision: 1.11 $ $Name: not supported by cvs2svn $
-*  @date $Date: 2006-12-30 03:27:19 $
+*  @version $Revision: 1.12 $ $Name: not supported by cvs2svn $
+*  @date $Date: 2007-01-01 21:05:09 $
 *
 *  Copyright 2006 Institute for Astronomy, University of Hawaii
@@ -118,14 +118,4 @@
                 continue;
 
-            char key;
-            pmKapaPlotVectorPair (L, M);
-            fscanf (stdin, "%c", &key);
-
-            pmKapaPlotVectorPair (L, dP);
-            fscanf (stdin, "%c", &key);
-
-            pmKapaPlotVectorPair (L, dQ);
-            fscanf (stdin, "%c", &key);
-
             // stats structure and mask for use in measuring the clipping statistic
             // this analysis has too few data points to use the robust median method
@@ -236,5 +226,5 @@
                     localX->mask[i-1][j] = 1;
                 }
-                if ((j > 0) && (j <= fpa->toTPA->y->nY)) {
+                if ((j > 0) && (j <= fpa->toTPA->x->nY)) {
                     localY->mask[i][j-1] = 1;
                 }
@@ -260,16 +250,16 @@
     // update fpa->toTP distortion terms
     fpa->toTPA->x->coeff[0][0] = 0;
-    for (int i = 1; i < fpa->toTPA->x->nX; i++) {
+    for (int i = 1; i <= fpa->toTPA->x->nX; i++) {
         if (!fpa->toTPA->x->mask[i][0]) {
             fpa->toTPA->x->coeff[i][0] = localX->coeff[i-1][0] / i;
         }
     }
-    for (int j = 1; j < fpa->toTPA->x->nY; j++) {
+    for (int j = 1; j <= fpa->toTPA->x->nY; j++) {
         if (!fpa->toTPA->x->mask[0][j]) {
             fpa->toTPA->x->coeff[0][j] = localY->coeff[0][j-1] / j;
         }
     }
-    for (int i = 1; i < fpa->toTPA->x->nX; i++) {
-        for (int j = 1; j < fpa->toTPA->x->nY; j++) {
+    for (int i = 1; i <= fpa->toTPA->x->nX; i++) {
+        for (int j = 1; j <= fpa->toTPA->x->nY; j++) {
             if (!fpa->toTPA->x->mask[i][j]) {
                 fpa->toTPA->x->coeff[i][j] = 0.5*(localX->coeff[i-1][j] / i + localY->coeff[i][j-1] / j);
@@ -277,4 +267,5 @@
         }
     }
+    fpa->toTPA->x->coeff[1][0] += 1.0;
     psFree (localX);
     psFree (localY);
@@ -289,6 +280,10 @@
         for (int j = 0; j < fpa->toTPA->y->nY; j++) {
             if (fpa->toTPA->y->mask[i][j]) {
-                localX->mask[i-1][j] = 1;
-                localY->mask[i][j-1] = 1;
+                if ((i > 0) && (i <= fpa->toTPA->y->nX)) {
+                    localX->mask[i-1][j] = 1;
+                }
+                if ((j > 0) && (j <= fpa->toTPA->y->nY)) {
+                    localY->mask[i][j-1] = 1;
+                }
             }
         }
@@ -320,16 +315,16 @@
     // update fpa->toTP distortion terms
     fpa->toTPA->y->coeff[0][0] = 0;
-    for (int i = 1; i < fpa->toTPA->y->nX; i++) {
+    for (int i = 1; i <= fpa->toTPA->y->nX; i++) {
         if (!fpa->toTPA->y->mask[i][0]) {
             fpa->toTPA->y->coeff[i][0] = localX->coeff[i-1][0] / i;
         }
     }
-    for (int j = 1; j < fpa->toTPA->y->nY; j++) {
+    for (int j = 1; j <= fpa->toTPA->y->nY; j++) {
         if (!fpa->toTPA->y->mask[0][j]) {
             fpa->toTPA->y->coeff[0][j] = localY->coeff[0][j-1] / j;
         }
     }
-    for (int i = 1; i < fpa->toTPA->y->nX; i++) {
-        for (int j = 1; j < fpa->toTPA->y->nY; j++) {
+    for (int i = 1; i <= fpa->toTPA->y->nX; i++) {
+        for (int j = 1; j <= fpa->toTPA->y->nY; j++) {
             if (!fpa->toTPA->y->mask[i][j]) {
                 fpa->toTPA->y->coeff[i][j] = 0.5*(localX->coeff[i-1][j] / i + localY->coeff[i][j-1] / j);
@@ -337,4 +332,5 @@
         }
     }
+    fpa->toTPA->y->coeff[0][1] += 1.0;
     psFree (localX);
     psFree (localY);
@@ -353,6 +349,7 @@
     // XXX choose an appropriate region based on the range of values
     // in L and M?
-    psRegion region = psRegionSet (0, 4000, 0, 4000);
-    psPlaneTransformInvert(fpa->fromTPA, fpa->toTPA, region, 50);
+    psRegion region = psRegionSet (-125000, +125000, -125000, +125000);
+    psFree (fpa->fromTPA);
+    fpa->fromTPA = psPlaneTransformInvert(NULL, fpa->toTPA, region, 50);
 
     return true;
