Index: /trunk/psModules/src/astrom/pmAstrometryObjects.c
===================================================================
--- /trunk/psModules/src/astrom/pmAstrometryObjects.c	(revision 16068)
+++ /trunk/psModules/src/astrom/pmAstrometryObjects.c	(revision 16069)
@@ -8,6 +8,6 @@
 *  @author EAM, IfA
 *
-*  @version $Revision: 1.36 $ $Name: not supported by cvs2svn $
-*  @date $Date: 2007-12-31 03:03:02 $
+*  @version $Revision: 1.37 $ $Name: not supported by cvs2svn $
+*  @date $Date: 2008-01-15 02:49:45 $
 *
 *  Copyright 2004-2005 Maui High Performance Computing Center, University of Hawaii
@@ -246,10 +246,28 @@
     // need to use the stats lookups functions to get the width and center
     for (int i = 0; i < nIter; i++) {
-        psVectorClipFitPolynomial2D (map->x, results->xStats, mask, 0xff, x, wt, X, Y);
-        // psTrace ("psModules.astrom", 3, "x resid: %f +/- %f (%ld of %ld)\n", results->xStats->robustMedian, results->xStats->robustStdev, results->xStats->clippedNvalues, x->n);
+	if (!psVectorClipFitPolynomial2D (map->x, results->xStats, mask, 0xff, x, wt, X, Y)) {
+            psError(PS_ERR_UNKNOWN, false, "failure in clip-fitting for x\n");
+	    psFree (x);
+	    psFree (y);
+	    psFree (X);
+	    psFree (Y);
+	    psFree (wt);
+	    psFree (mask);
+
+	    return results;
+	}
         psTrace ("psModules.astrom", 3, "x resid: %f +/- %f (%ld of %ld)\n", results->xStats->clippedMean, results->xStats->clippedStdev, results->xStats->clippedNvalues, x->n);
 
-        psVectorClipFitPolynomial2D (map->y, results->yStats, mask, 0xff, y, wt, X, Y);
-        // psTrace ("psModules.astrom", 3, "y resid: %f +/- %f (%ld of %ld)\n", results->yStats->robustMedian, results->yStats->robustStdev, results->yStats->clippedNvalues, y->n);
+        if (!psVectorClipFitPolynomial2D (map->y, results->yStats, mask, 0xff, y, wt, X, Y)) {
+            psError(PS_ERR_UNKNOWN, false, "failure in clip-fitting for y\n");
+	    psFree (x);
+	    psFree (y);
+	    psFree (X);
+	    psFree (Y);
+	    psFree (wt);
+	    psFree (mask);
+
+	    return results;
+	}
         psTrace ("psModules.astrom", 3, "y resid: %f +/- %f (%ld of %ld)\n", results->yStats->clippedMean, results->yStats->clippedStdev, results->yStats->clippedNvalues, y->n);
     }
@@ -598,14 +616,11 @@
         // only check bins with at least 1/2 of max bin
         // XXX requiring at least 3 matches in bin
-        int minNpts = PS_MAX (0.5*imStats->max, 3);
-        psTrace("psModule.astrom.grid.angle", 5, "minNpts: %d, max: %d", minNpts, (int)(imStats->max));
+        int minNpts = PS_MAX (0.5*imStats->max, 5);
+        psTrace("psModule.astrom", 5, "minNpts: %d, min: %d, max: %d, median: %f, stdev: %f", minNpts, (int)(imStats->min), (int)(imStats->max), imStats->sampleMedian, imStats->sampleStdev);
 
         // find the 'best' bin
-        for (int j = 0; j < gridNP->numRows; j++)
-        {
+        for (int j = 0; j < gridNP->numRows; j++) {
             for (int i = 0; i < gridNP->numCols; i++) {
-
-                if (NP[j][i] < minNpts)
-                    continue;
+                if (NP[j][i] < minNpts) continue;
 
                 // this metric emphasizes a narrow peak with lots of sources over one with few.
@@ -658,4 +673,6 @@
     stats->nTest = sort->data.U32[sort->n - 5];
     stats->nSigma = (stats->nMatch - stats->nTest) / sqrt(stats->nTest);
+    // XXX this needs a better analysis of the image histogram..
+    // fprintf (stderr, "sigma: nMatch: %d, nTest: %d, nTen: %d\n", stats->nMatch, stats->nTest, sort->data.U32[sort->n - 10]);
 
     psFree (sort);
@@ -742,5 +759,5 @@
 	    newStat->center = center;
 
-	    if (newStat->minMetric < minStat->minMetric) {
+	    if (isfinite(newStat->minMetric) && (newStat->minMetric < minStat->minMetric)) {
 		*minStat = *newStat;
 		psLogMsg ("psModule.astrom", 4, "grid test - offset: %7.2f,%7.2f @ %6.1f deg x %7.3f (%4d pts, %5.1f sig, %5.1f var, %6.3f log metric) *",
