Index: branches/eam_branches/20091113/psModules/src/astrom/pmAstrometryObjects.c
===================================================================
--- branches/eam_branches/20091113/psModules/src/astrom/pmAstrometryObjects.c	(revision 26128)
+++ branches/eam_branches/20091113/psModules/src/astrom/pmAstrometryObjects.c	(revision 26129)
@@ -634,17 +634,17 @@
         }
 
-# if 0
-        char line[16];
-        psFits *fits = psFitsOpen ("grid.image.fits", "w");
-        psFitsWriteImage (fits, NULL, gridNP, 0, NULL);
-        psFitsClose (fits);
-        fprintf (stderr, "wrote grid image, press return to continue\n");
-        fgets (line, 15, stdin);
-# endif
+	if (psTraceGetLevel("psModules.astrom") >= 5) {
+	    char line[16];
+	    psFits *fits = psFitsOpen ("grid.image.fits", "w");
+	    psFitsWriteImage (fits, NULL, gridNP, 0, NULL);
+	    psFitsClose (fits);
+	    fprintf (stderr, "wrote grid image, press return to continue\n");
+	    fgets (line, 15, stdin);
+	}
 
         // 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, 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);
+        psTrace("psModule.astrom", 4, "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
@@ -687,5 +687,6 @@
 
         // XXX this function is crashing
-        // pmAstromVisualPlotGridMatch(raw, ref, gridNP, stats->offset.x, stats->offset.y, maxOffpix, Scale, Offset);
+        pmAstromVisualPlotGridMatch(raw, ref, gridNP, stats->offset.x, stats->offset.y, maxOffpix, Scale, Offset);
+        pmAstromVisualPlotGridMatchOverlay(raw, ref);
 
         psFree (imStats);
Index: branches/eam_branches/20091113/psModules/src/astrom/pmAstrometryVisual.c
===================================================================
--- branches/eam_branches/20091113/psModules/src/astrom/pmAstrometryVisual.c	(revision 26128)
+++ branches/eam_branches/20091113/psModules/src/astrom/pmAstrometryVisual.c	(revision 26129)
@@ -450,5 +450,5 @@
 
     graphdata.color = KapaColorByName ("red");
-    graphdata.style = 1;
+    graphdata.style = 0;
 
     //overplot clumpy regions excluded from analysis
@@ -905,6 +905,8 @@
     KapaPlotVector (kapa, gridNP->numCols, horizontalIndices, "x");
     KapaPlotVector (kapa, gridNP->numCols, horizHistSlice, "y");
+
     float xslice[2] = {offsetX - Scale / 2., offsetX - Scale / 2.};
     float yslice[2] = {-5, 100};
+    graphdata.style = 0;
     graphdata.color = KapaColorByName("red");
     KapaPrepPlot(kapa, 2, &graphdata);
@@ -927,6 +929,8 @@
     KapaPlotVector (kapa, gridNP->numRows, vertHistSlice, "x");
     KapaPlotVector (kapa, gridNP->numRows, verticalIndices, "y");
+
     yslice[0] = yslice[1] = offsetY - Scale / 2.;
     xslice[0] = -5; xslice[1] = 100;
+    graphdata.style = 0;
     graphdata.color = KapaColorByName("red");
     KapaPrepPlot(kapa, 2, &graphdata);
@@ -940,4 +944,105 @@
 } // end of pmAstromVisualPlotGridMatch
 
+
+bool pmAstromVisualPlotGridMatchOverlay (const psArray *raw,
+					 const psArray *ref)
+{
+    //make sure we want to plot this
+    if (!pmVisualIsVisual() || !plotGridMatch) return true;
+    if (!pmVisualInitWindow(&kapa2, "psastro:plots")){
+        return false;
+    }
+
+    Graphdata graphdata;
+    psVector *xPlot = psVectorAlloc (PS_MAX(raw->n, ref->n), PS_TYPE_F32); // x data points
+    psVector *yPlot = psVectorAlloc (PS_MAX(raw->n, ref->n), PS_TYPE_F32); // y data points
+    psVector *zPlot = psVectorAlloc (PS_MAX(raw->n, ref->n), PS_TYPE_F32); // y data points
+
+    // set up plot information
+    KapaClearPlots(kapa2);
+    KapaInitGraph(&graphdata);
+
+    KapaSetFont(kapa2, "helvetica", 14);
+    KapaBox(kapa2, &graphdata);
+    KapaSendLabel (kapa2, "X (FP)", KAPA_LABEL_XM);
+    KapaSendLabel (kapa2, "Y (FP)", KAPA_LABEL_YM);
+    KapaSendLabel (kapa2, "pmAstromGridAngle residuals. Box: Correlation Peak.", KAPA_LABEL_XP);
+
+    // plot the REF data.  (also calculate the plot ranges, accumulate the plot vectors)
+    graphdata.xmin = +INT_MAX;
+    graphdata.xmax = -INT_MAX;
+    graphdata.ymin = +INT_MAX;
+    graphdata.ymax = -INT_MAX;
+    for (int i = 0; i < ref->n; i++) {
+        pmAstromObj *obj = ref->data[i];
+	graphdata.xmin = PS_MIN(graphdata.xmin, obj->FP->x);
+	graphdata.xmax = PS_MAX(graphdata.xmax, obj->FP->x);
+	graphdata.ymin = PS_MIN(graphdata.ymin, obj->FP->y);
+	graphdata.ymax = PS_MAX(graphdata.ymax, obj->FP->y);
+	xPlot->data.F32[i] = obj->FP->x;
+	yPlot->data.F32[i] = obj->FP->y;
+	zPlot->data.F32[i] = obj->Mag;
+    }
+    xPlot->n = yPlot->n = zPlot->n = ref->n;
+    KapaSetLimits(kapa2, &graphdata);
+
+    psStats *stats = psStatsAlloc(PS_STAT_SAMPLE_MEDIAN);
+    psVectorStats (stats, zPlot, NULL, NULL, 0);
+    float zero = stats->sampleMedian + 3.0;
+    float range = 6.0;
+
+    for (int i = 0; i < zPlot->n; i++) {
+	float value = (zero - zPlot->data.F32[i]) / range;
+	zPlot->data.F32[i] = PS_MAX(0.0, PS_MIN(1.0, value));
+    }
+
+    // the point size will be scaled from the z vector
+    graphdata.style = 2;
+    graphdata.ptype = 7;
+    graphdata.size = -1;
+    graphdata.color = KapaColorByName ("black");
+
+    KapaPrepPlot   (kapa2, xPlot->n, &graphdata);
+    KapaPlotVector (kapa2, xPlot->n, xPlot->data.F32, "x");
+    KapaPlotVector (kapa2, yPlot->n, yPlot->data.F32, "y");
+    KapaPlotVector (kapa2, zPlot->n, zPlot->data.F32, "z");
+
+    // plot the RAW data (keep previous limits)
+    for (int i = 0; i < raw->n; i++) {
+        pmAstromObj *obj = raw->data[i];
+	xPlot->data.F32[i] = obj->FP->x;
+	yPlot->data.F32[i] = obj->FP->y;
+	zPlot->data.F32[i] = obj->Mag;
+    }
+    xPlot->n = yPlot->n = zPlot->n = raw->n;
+
+    psStatsInit(stats);
+    psVectorStats (stats, zPlot, NULL, NULL, 0);
+    zero = stats->sampleMedian + 3.0;
+    range = 6.0;
+
+    for (int i = 0; i < zPlot->n; i++) {
+	float value = (zero - zPlot->data.F32[i]) / range;
+	zPlot->data.F32[i] = PS_MAX(0.0, PS_MIN(1.0, value));
+    }
+
+    // the point size will be scaled from the z vector
+    graphdata.style = 2;
+    graphdata.ptype = 7;
+    graphdata.size = -1;
+    graphdata.color = KapaColorByName ("red");
+
+    KapaPrepPlot   (kapa2, xPlot->n, &graphdata);
+    KapaPlotVector (kapa2, xPlot->n, xPlot->data.F32, "x");
+    KapaPlotVector (kapa2, yPlot->n, yPlot->data.F32, "y");
+    KapaPlotVector (kapa2, zPlot->n, zPlot->data.F32, "z");
+
+    pmVisualAskUser(&plotGridMatch);
+    psFree(xPlot);
+    psFree(yPlot);
+    psFree(zPlot);
+    psFree(stats);
+    return true;
+}
 
 bool pmAstromVisualPlotTweak (psVector *xHist, // Smoothed Horizontal cut through the histogram
Index: branches/eam_branches/20091113/psModules/src/astrom/pmAstrometryVisual.h
===================================================================
--- branches/eam_branches/20091113/psModules/src/astrom/pmAstrometryVisual.h	(revision 26128)
+++ branches/eam_branches/20091113/psModules/src/astrom/pmAstrometryVisual.h	(revision 26129)
@@ -45,4 +45,7 @@
                                   );
 
+
+bool pmAstromVisualPlotGridMatchOverlay (const psArray *raw,
+					 const psArray *ref);
 
 /**
