Index: /trunk/psModules/src/astrom/pmAstrometryDistortion.c
===================================================================
--- /trunk/psModules/src/astrom/pmAstrometryDistortion.c	(revision 10876)
+++ /trunk/psModules/src/astrom/pmAstrometryDistortion.c	(revision 10877)
@@ -7,6 +7,6 @@
 *  @author EAM, IfA
 *
-*  @version $Revision: 1.12 $ $Name: not supported by cvs2svn $
-*  @date $Date: 2007-01-01 21:05:09 $
+*  @version $Revision: 1.13 $ $Name: not supported by cvs2svn $
+*  @date $Date: 2007-01-03 00:57:10 $
 *
 *  Copyright 2006 Institute for Astronomy, University of Hawaii
@@ -77,13 +77,17 @@
             int Ymax = Ymin + DY;
 
+            psStats *stats = NULL;
+            psVector *mask = NULL;
+            pmAstromGradient *grad = NULL;
+
             psVector *L  = psVectorAllocEmpty (100, PS_TYPE_F32);
             psVector *M  = psVectorAllocEmpty (100, PS_TYPE_F32);
             psVector *dP = psVectorAllocEmpty (100, PS_TYPE_F32);
             psVector *dQ = psVectorAllocEmpty (100, PS_TYPE_F32);
-            int Npts = 0;
 
             // XXX this is a bit inefficient: first sorting by X or Y could speed this up.
             // XXX or assigning to a segment in a single pass first
             // select the stars within this chip region
+            int Npts = 0;
             for (int i = 0; i < matches->n; i++) {
 
@@ -106,6 +110,8 @@
                 M->data.F32[Npts] = ref->FP->y;
 
-                dP->data.F32[Npts] = ref->TP->x - raw->TP->x;
-                dQ->data.F32[Npts] = ref->TP->y - raw->TP->y;
+                // P,Q = L,M + terms of order epsilon.
+                // measuring the gradient constrains thos terms
+                dP->data.F32[Npts] = ref->TP->x - raw->FP->x;
+                dQ->data.F32[Npts] = ref->TP->y - raw->FP->y;
 
                 psVectorExtend (L, 100, 1);
@@ -115,14 +121,15 @@
                 Npts++;
             }
+
             if (Npts < 5)
-                continue;
+                goto skip;
 
             // stats structure and mask for use in measuring the clipping statistic
             // this analysis has too few data points to use the robust median method
-            psStats *stats = psStatsAlloc (PS_STAT_SAMPLE_MEDIAN | PS_STAT_SAMPLE_STDEV);
-            psVector *mask = psVectorAlloc (Npts, PS_TYPE_MASK);
+            stats = psStatsAlloc (PS_STAT_SAMPLE_MEDIAN | PS_STAT_SAMPLE_STDEV);
+            mask = psVectorAlloc (Npts, PS_TYPE_MASK);
             psVectorInit (mask, 0);
 
-            pmAstromGradient *grad = pmAstromGradientAlloc ();
+            grad = pmAstromGradientAlloc ();
 
             // fit the collection of positions and offsets with a local 1st order gradient
@@ -130,6 +137,5 @@
             // the mask is used to mark the points which pass / fail the fit
             if (!psVectorClipFitPolynomial2D (local, stats, mask, 0xff, dP, NULL, L, M)) {
-                psFree (grad);
-                continue;
+                goto skip;
             }
 
@@ -141,6 +147,5 @@
             // the mask is used to mark the points which pass / fail the fit
             if (!psVectorClipFitPolynomial2D (local, stats, mask, 0xff, dQ, NULL, L, M)) {
-                psFree (grad);
-                continue;
+                goto skip;
             }
 
@@ -156,9 +161,16 @@
 
             psArrayAdd (gradients, 100, grad);
+
+skip:
             psFree (grad);
             psFree (stats);
             psFree (mask);
-        }
-    }
+            psFree (L);
+            psFree (M);
+            psFree (dP);
+            psFree (dQ);
+        }
+    }
+    psFree (local);
     return gradients;
 }
@@ -191,20 +203,4 @@
         M->data.F32[i] = grad->FP.y;
     }
-
-    char key;
-    pmKapaPlotVectorPair (L, M);
-    fscanf (stdin, "%c", &key);
-
-    pmKapaPlotVectorPair (L, dPdL);
-    fscanf (stdin, "%c", &key);
-
-    pmKapaPlotVectorPair (L, dPdM);
-    fscanf (stdin, "%c", &key);
-
-    pmKapaPlotVectorPair (L, dQdL);
-    fscanf (stdin, "%c", &key);
-
-    pmKapaPlotVectorPair (L, dQdM);
-    fscanf (stdin, "%c", &key);
 
     // mask and stats structure for measuring the clipping statistic
