Index: /trunk/psModules/src/astrom/pmAstrometryDistortion.c
===================================================================
--- /trunk/psModules/src/astrom/pmAstrometryDistortion.c	(revision 10850)
+++ /trunk/psModules/src/astrom/pmAstrometryDistortion.c	(revision 10851)
@@ -7,6 +7,6 @@
 *  @author EAM, IfA
 *
-*  @version $Revision: 1.9 $ $Name: not supported by cvs2svn $
-*  @date $Date: 2006-12-25 01:49:49 $
+*  @version $Revision: 1.10 $ $Name: not supported by cvs2svn $
+*  @date $Date: 2006-12-29 18:29:59 $
 *
 *  Copyright 2006 Institute for Astronomy, University of Hawaii
@@ -29,4 +29,5 @@
 #include "pmAstrometryObjects.h"
 #include "pmAstrometryDistortion.h"
+#include "pmKapaPlots.h"
 
 static void pmAstromGradientFree (pmAstromGradient *grad)
@@ -117,4 +118,14 @@
                 continue;
 
+            char key;
+            pmKapaPlotVectorPair (L, M);
+            fscanf (stdin, "%c", &key);
+
+            pmKapaPlotVectorPair (L, dP);
+            fscanf (stdin, "%c", &key);
+
+            pmKapaPlotVectorPair (L, dQ);
+            fscanf (stdin, "%c", &key);
+
             // fit the collection of positions and offsets with a local 1st order gradient
             // 3hi/3lo sigma clipping on the rflux vs metric fit
@@ -131,10 +142,17 @@
             grad->FP.y = stats->sampleMedian;
 
-            psVectorClipFitPolynomial2D (local, stats, mask, 0xff, dP, NULL, L, M);
+            // test how many survive and include / exclude points on that basis
+            if (!psVectorClipFitPolynomial2D (local, stats, mask, 0xff, dP, NULL, L, M)) {
+                psFree (grad);
+                continue;
+            }
 
             grad->dTPdL.x = local->coeff[1][0];
             grad->dTPdM.x = local->coeff[0][1];
 
-            psVectorClipFitPolynomial2D (local, stats, mask, 0xff, dQ, NULL, L, M);
+            if (!psVectorClipFitPolynomial2D (local, stats, mask, 0xff, dQ, NULL, L, M)) {
+                psFree (grad);
+                continue;
+            }
 
             grad->dTPdL.y = local->coeff[1][0];
@@ -142,4 +160,5 @@
 
             psArrayAdd (gradients, 100, grad);
+            psFree (grad);
         }
     }
@@ -175,4 +194,20 @@
     }
 
+    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);
+
     psStats *stats = psStatsAlloc (PS_STAT_SAMPLE_MEDIAN | PS_STAT_SAMPLE_STDEV);
     psVector *mask = psVectorAlloc (gradients->n, PS_TYPE_MASK);
@@ -182,4 +217,5 @@
     // determine the gradient order(s) from the fpa->toTP structure
     localX = psPolynomial2DAlloc (PS_POLYNOMIAL_ORD, fpa->toTPA->x->nX-1, fpa->toTPA->x->nY);
+
     localY = psPolynomial2DAlloc (PS_POLYNOMIAL_ORD, fpa->toTPA->x->nX,   fpa->toTPA->x->nY-1);
 
@@ -188,8 +224,8 @@
         for (int j = 0; j <= fpa->toTPA->x->nY; j++) {
             if (fpa->toTPA->x->mask[i][j]) {
-                if ((i > 0) && (i < fpa->toTPA->x->nX)) {
+                if ((i > 0) && (i <= fpa->toTPA->x->nX)) {
                     localX->mask[i-1][j] = 1;
                 }
-                if ((j > 0) && (j < fpa->toTPA->y->nY)) {
+                if ((j > 0) && (j <= fpa->toTPA->y->nY)) {
                     localY->mask[i][j-1] = 1;
                 }
@@ -199,6 +235,13 @@
 
     // fit the local gradients in each direction
-    psVectorClipFitPolynomial2D (localX, stats, mask, 0xff, dPdL, NULL, L, M);
-    psVectorClipFitPolynomial2D (localY, stats, mask, 0xff, dPdM, NULL, L, M);
+    if (!psVectorClipFitPolynomial2D (localX, stats, mask, 0xff, dPdL, NULL, L, M)) {
+        psLogMsg ("psastro", 3, "failed to fit x-dir gradient\n");
+        return false;
+    }
+
+    if (!psVectorClipFitPolynomial2D (localY, stats, mask, 0xff, dPdM, NULL, L, M)) {
+        psLogMsg ("psastro", 3, "failed to fit y-dir gradient\n");
+        return false;
+    }
 
     // XXX test: generate fit
