Index: /branches/eam_branches/ipp-20110213/psModules/src/astrom/pmAstrometryObjects.c
===================================================================
--- /branches/eam_branches/ipp-20110213/psModules/src/astrom/pmAstrometryObjects.c	(revision 31018)
+++ /branches/eam_branches/ipp-20110213/psModules/src/astrom/pmAstrometryObjects.c	(revision 31019)
@@ -893,5 +893,5 @@
         // XXX this function is crashing
         pmAstromVisualPlotGridMatch(raw, ref, gridNP, stats->offset.x, stats->offset.y, maxOffpix, Scale, Offset);
-        pmAstromVisualPlotGridMatchOverlay(raw, ref);
+        pmAstromVisualPlotGridMatchOverlay(raw, ref, stats->offset);
 
         psFree (imStats);
Index: /branches/eam_branches/ipp-20110213/psModules/src/astrom/pmAstrometryVisual.c
===================================================================
--- /branches/eam_branches/ipp-20110213/psModules/src/astrom/pmAstrometryVisual.c	(revision 31018)
+++ /branches/eam_branches/ipp-20110213/psModules/src/astrom/pmAstrometryVisual.c	(revision 31019)
@@ -21,6 +21,7 @@
 #include "pmKapaPlots.h"
 #include "pmVisual.h"
-
-//variables to determine when things are plotted
+#include "pmVisualUtils.h"
+
+// variables to determine when things are plotted
 static bool plotGridMatch        = true;
 static bool plotTweak            = true;
@@ -37,8 +38,9 @@
 
 // variables to store plotting window indices
-static int kapa = -1;
+static int kapa1 = -1;
 static int kapa2 = -1;
-
-//helper prototypes
+static int kapa3 = -1;
+
+// helper prototypes
 bool residPlot (psArray *rawstars, psArray *refstars, psArray *match, psMetadata *recipe,
                 char *title);
@@ -49,8 +51,6 @@
 bool pmAstromVisualClose(void)
 {
-    if(kapa != -1)
-        KiiClose(kapa);
-    if(kapa2 != -1)
-        KiiClose(kapa2);
+    if (kapa1 != -1) KiiClose(kapa1);
+    if (kapa2 != -1) KiiClose(kapa2);
     return true;
 }
@@ -60,16 +60,14 @@
 bool pmAstromVisualPlotRawStars (psArray *rawstars, pmFPA *fpa, pmChip *chip, psMetadata *recipe)
 {
-    // make sure we want to plot this
-    if (!plotRawStars || !pmVisualIsVisual()) return true;
-
-    //set up plot region
-    if (!pmVisualInitWindow (&kapa, "psastro:plots")){
-        return false;
-    }
+    if (!plotRawStars) return true;
+    if (!pmVisualTestLevel("psastro.plot1", 1)) return true;
+    if (!pmVisualInitWindow (&kapa1, "psastro:plots")) return false;
+
     Graphdata graphdata;
     KapaSection section;
 
     KapaInitGraph (&graphdata);
-    KapaClearPlots (kapa);
+    KapaClearPlots (kapa1);
+    section.bg = KapaColorByName ("none"); // XXX probably should be 'none'
 
     graphdata.color = KapaColorByName ("black");
@@ -78,8 +76,8 @@
     graphdata.style = 2;
 
-    section.dx = 0.4;
-    section.dy = 0.4;
-
-    //initialize and populate plotting vectors
+    section.dx = 0.5;
+    section.dy = 0.5;
+
+    // initialize and populate plotting vectors
     bool status = false;
     float iMagMin = psMetadataLookupF32 (&status, recipe, "PSASTRO.PLOT.INST.MAG.MIN");
@@ -94,8 +92,8 @@
     section.name = NULL;
     psStringAppend (&section.name, "a0");
-    KapaSetSection (kapa, &section);
+    KapaSetSection (kapa1, &section);
     psFree (section.name);
 
-    //Chip coordinates
+    // Chip coordinates
     int n = 0;
     for (int i = 0; i < rawstars->n; i++) {
@@ -112,15 +110,15 @@
     xVec->n = yVec->n = zVec->n = n;
 
-    KapaSendLabel(kapa, "Chip", KAPA_LABEL_XP);
-    KapaSendLabel(kapa, "X", KAPA_LABEL_XM);
-    KapaSendLabel(kapa, "Y", KAPA_LABEL_YM);
-    pmVisualTriplePlot (kapa, &graphdata, xVec, yVec, zVec, false);
-
-    //Focal Plane Coordinates
+    KapaSendLabel(kapa1, "Chip", KAPA_LABEL_XP);
+    KapaSendLabel(kapa1, "X", KAPA_LABEL_XM);
+    KapaSendLabel(kapa1, "Y", KAPA_LABEL_YM);
+    pmVisualTriplePlot (kapa1, &graphdata, xVec, yVec, zVec, false);
+
+    // Focal Plane Coordinates
     section.x = 0.5;
     section.y = 0.5;
     section.name = NULL;
     psStringAppend (&section.name, "a1");
-    KapaSetSection (kapa, &section);
+    KapaSetSection (kapa1, &section);
     psFree (section.name);
 
@@ -139,8 +137,8 @@
     xVec->n = yVec->n = zVec->n = n;
 
-    KapaSendLabel (kapa, "Focal Plane", KAPA_LABEL_XP);
-    KapaSendLabel (kapa, "L", KAPA_LABEL_XM);
-    KapaSendLabel (kapa, "M", KAPA_LABEL_YM);
-    pmVisualTriplePlot (kapa, &graphdata, xVec, yVec, zVec, false);
+    KapaSendLabel (kapa1, "Focal Plane", KAPA_LABEL_XP);
+    KapaSendLabel (kapa1, "L", KAPA_LABEL_XM);
+    KapaSendLabel (kapa1, "M", KAPA_LABEL_YM);
+    pmVisualTriplePlot (kapa1, &graphdata, xVec, yVec, zVec, false);
 
     // Tangent Plane Coordinates
@@ -149,5 +147,5 @@
     section.name = NULL;
     psStringAppend (&section.name, "a2");
-    KapaSetSection (kapa, &section);
+    KapaSetSection (kapa1, &section);
     psFree (section.name);
 
@@ -166,15 +164,15 @@
     xVec->n = yVec->n = zVec->n = n;
 
-    KapaSendLabel (kapa, "Tangential Plane", KAPA_LABEL_XP);
-    KapaSendLabel (kapa, "P", KAPA_LABEL_XM);
-    KapaSendLabel (kapa, "Q", KAPA_LABEL_YM);
-    pmVisualTriplePlot (kapa, &graphdata, xVec, yVec, zVec, false);
-
-    //sky coordinates
+    KapaSendLabel (kapa1, "Tangential Plane", KAPA_LABEL_XP);
+    KapaSendLabel (kapa1, "P", KAPA_LABEL_XM);
+    KapaSendLabel (kapa1, "Q", KAPA_LABEL_YM);
+    pmVisualTriplePlot (kapa1, &graphdata, xVec, yVec, zVec, false);
+
+    // sky coordinates
     section.x = 0.5;
     section.y = 0.0;
     section.name = NULL;
     psStringAppend (&section.name, "a3");
-    KapaSetSection (kapa, &section);
+    KapaSetSection (kapa1, &section);
     psFree (section.name);
 
@@ -193,22 +191,22 @@
     xVec->n = yVec->n = zVec->n = n;
 
-    KapaSendLabel (kapa, "Sky", KAPA_LABEL_XP);
-    KapaSendLabel(kapa, "RA", KAPA_LABEL_XM);
-    KapaSendLabel(kapa, "Dec", KAPA_LABEL_YM);
-    pmVisualTriplePlot (kapa, &graphdata, xVec, yVec, zVec, false);
+    KapaSendLabel (kapa1, "Sky", KAPA_LABEL_XP);
+    KapaSendLabel(kapa1, "RA", KAPA_LABEL_XM);
+    KapaSendLabel(kapa1, "Dec", KAPA_LABEL_YM);
+    pmVisualTriplePlot (kapa1, &graphdata, xVec, yVec, zVec, false);
 
     // flip x (East increase to left)
     SWAP (graphdata.xmin, graphdata.xmax);
-    KapaSetLimits (kapa, &graphdata);
+    KapaSetLimits (kapa1, &graphdata);
 
     // plot label
     section.x = 0.0;
     section.y = 0.0;
-    section.dx = .95;
-    section.dy = .95;
+    section.dx = 1.0;
+    section.dy = 1.0;
     section.name = NULL;
     psStringAppend (&section.name, "a5");
-    KapaSetSection (kapa, &section);
-    KapaSendLabel (kapa, "Raw Star Selection - Initial Astrometry", KAPA_LABEL_XP);
+    KapaSetSection (kapa1, &section);
+    KapaSendLabel (kapa1, "Raw Star Selection - Initial Astrometry", KAPA_LABEL_XP);
     psFree (section.name);
 
@@ -225,15 +223,11 @@
 bool pmAstromVisualPlotRefStars (psArray *refstars, psMetadata *recipe)
 {
-    //make sure we want to plot this
-    if (!pmVisualIsVisual() || !plotRefStars) return true;
-
-    //set up plotting variables
-    if (!pmVisualInitWindow (&kapa, "psastro:plots")) {
-        return false;
-    }
+    if (!plotRefStars) return true;
+    if (!pmVisualTestLevel("psastro.plot2", 1)) return true;
+    if (!pmVisualInitWindow (&kapa1, "psastro:plots")) return false;
 
     Graphdata graphdata;
     KapaInitGraph (&graphdata);
-    KapaClearSections (kapa);
+    KapaClearSections (kapa1);
 
     graphdata.color = KapaColorByName ("black");
@@ -242,5 +236,5 @@
     graphdata.style = 2;
 
-    //initialize and populate plot vectors
+    // initialize and populate plot vectors
     bool status = false;
     float rMagMin = psMetadataLookupF32 (&status, recipe, "PSASTRO.PLOT.REF.MAG.MIN");
@@ -265,12 +259,12 @@
     xVec->n = yVec->n = zVec->n = n;
 
-    KapaSendLabel (kapa, "RA", KAPA_LABEL_XM);
-    KapaSendLabel (kapa, "Dec", KAPA_LABEL_YM);
-    KapaSendLabel (kapa, "Reference Stars", KAPA_LABEL_XP);
-    pmVisualTriplePlot(kapa, &graphdata, xVec, yVec, zVec, false);
+    KapaSendLabel (kapa1, "RA", KAPA_LABEL_XM);
+    KapaSendLabel (kapa1, "Dec", KAPA_LABEL_YM);
+    KapaSendLabel (kapa1, "Reference Stars", KAPA_LABEL_XP);
+    pmVisualTriplePlot(kapa1, &graphdata, xVec, yVec, zVec, false);
 
     // flip x (East increase to left)
     SWAP (graphdata.xmin, graphdata.xmax);
-    KapaSetLimits (kapa, &graphdata);
+    KapaSetLimits (kapa1, &graphdata);
 
     // pause and wait for user input:
@@ -290,60 +284,58 @@
                                           )
 {
-
-    // make sure we want to plot this
-    if ( !pmVisualIsVisual() || !plotLumFunc ) return true;
-
-    //set up plotting variables
-    if ( !pmVisualInitWindow (&kapa, "psastro:plots")){
-        return false;
-    }
+    if (!plotLumFunc ) return true;
+    if (!pmVisualTestLevel("psastro.plot3", 1)) return true;
+    if (!pmVisualInitWindow (&kapa1, "psastro:plots")) return false;
+
+    int colorNone = KapaColorByName ("none"); // XXX probably should be 'none'
 
     Graphdata graphdata;
-    KapaSection section1 = {"s1", .1, .1, .85, .35};
-    KapaSection section2 =  {"s2", .1, .5, .85, .35};
+    KapaSection section1 = {"s1", 0.0, 0.0, 1.0, 0.5, colorNone};
+    KapaSection section2 = {"s2", 0.0, 0.5, 1.0, 0.5, colorNone};
     KapaSection section;
-    if(rawFunc == NULL)
+    if (rawFunc == NULL) {
         section = section1;
-    else
+    } else {
         section = section2;
-    if (rawFunc == NULL)
-        KapaClearPlots(kapa);
+    }
+
+    if (rawFunc == NULL) KapaClearPlots(kapa1);
     KapaInitGraph(&graphdata);
 
-    //Determine Plot Limits
+    // Determine Plot Limits
     pmVisualScaleGraphdata(&graphdata, Mag, lnMag, false);
 
-    //Make a line for the fit
+    // Make a line for the fit
     float x[2] = {graphdata.xmin, graphdata.xmax};
     float y[2] = {lumFunc->offset + x[0] * lumFunc->slope,
                  lumFunc->offset + x[1] * lumFunc->slope};
 
-    //Plot Data
-    KapaSetSection(kapa, &section);
-    KapaSetLimits(kapa, &graphdata);
-
-    KapaSetFont (kapa, "helvetica", 14);
-    KapaBox (kapa, &graphdata);
-    KapaSendLabel (kapa, "Magnitude", KAPA_LABEL_XM);
-    KapaSendLabel (kapa, "Log(N)", KAPA_LABEL_YM);
+    // Plot Data
+    KapaSetSection(kapa1, &section);
+    KapaSetLimits(kapa1, &graphdata);
+
+    KapaSetFont (kapa1, "helvetica", 14);
+    KapaBox (kapa1, &graphdata);
+    KapaSendLabel (kapa1, "Magnitude", KAPA_LABEL_XM);
+    KapaSendLabel (kapa1, "Log(N)", KAPA_LABEL_YM);
     if (rawFunc == NULL)
-        KapaSendLabel (kapa, "Raw Star Luminosity Function", KAPA_LABEL_XP);
+        KapaSendLabel (kapa1, "Raw Star Luminosity Function", KAPA_LABEL_XP);
     else
-        KapaSendLabel (kapa,
+        KapaSendLabel (kapa1,
                        "Reference Star Luminosity Function, Shifted Raw Fit, and Cutoff",
                        KAPA_LABEL_XP);
     graphdata.color=KapaColorByName("black");
     graphdata.style = 1;
-    KapaPrepPlot (kapa, lnMag->n, &graphdata);
-    KapaPlotVector(kapa, lnMag->n,   Mag->data.F32, "x");
-    KapaPlotVector(kapa, lnMag->n, lnMag->data.F32, "y");
-
-    //Overplot fit
+    KapaPrepPlot (kapa1, lnMag->n, &graphdata);
+    KapaPlotVector(kapa1, lnMag->n,   Mag->data.F32, "x");
+    KapaPlotVector(kapa1, lnMag->n, lnMag->data.F32, "y");
+
+    // Overplot fit
     graphdata.style=0;
-    KapaPrepPlot(kapa,2,&graphdata);
-    KapaPlotVector(kapa, 2, x, "x");
-    KapaPlotVector(kapa, 2, y, "y");
-
-    //If rawFunc was supplied, overplot the raw star fit + cutoff
+    KapaPrepPlot(kapa1,2,&graphdata);
+    KapaPlotVector(kapa1, 2, x, "x");
+    KapaPlotVector(kapa1, 2, y, "y");
+
+    // If rawFunc was supplied, overplot the raw star fit + cutoff
     if( rawFunc != NULL) {
         double mRef = 0.5*(lumFunc->mMin + lumFunc->mMax);
@@ -359,10 +351,10 @@
         float y[2] = {graphdata.ymin, graphdata.ymax};
         graphdata.color= KapaColorByName("red");
-        KapaPrepPlot(kapa, 2, &graphdata);
-        KapaPlotVector(kapa, 2, x, "x");
-        KapaPlotVector(kapa, 2, y, "y");
-        KapaPrepPlot (kapa, 2, &graphdata);
-        KapaPlotVector (kapa, 2, xraw, "x");
-        KapaPlotVector (kapa, 2, yraw, "y");
+        KapaPrepPlot(kapa1, 2, &graphdata);
+        KapaPlotVector(kapa1, 2, x, "x");
+        KapaPlotVector(kapa1, 2, y, "y");
+        KapaPrepPlot (kapa1, 2, &graphdata);
+        KapaPlotVector (kapa1, 2, xraw, "x");
+        KapaPlotVector (kapa1, 2, yraw, "y");
 
         // pause and wait for user input:
@@ -379,23 +371,22 @@
                                     )
 {
-
-    //make sure we want to plot this
-    if (!pmVisualIsVisual() || !plotRemoveClumps) return true;
-
-    //set up plot variables
-    if ( !pmVisualInitWindow (&kapa, "psastro:plots")) {
-        return false;
-    }
+    if (!plotRemoveClumps) return true;
+    if (!pmVisualTestLevel("psastro.plot4", 1)) return true;
+    if (!pmVisualInitWindow (&kapa1, "psastro:plots")) return false;
 
     KapaSection section;
     Graphdata graphdata;
-    section.x = 0;
-    section.dx = .9;
-    section.y = 0;
-    section.dy = .9;
+
+    KapaClearSections (kapa1);
+    KapaInitGraph(&graphdata);
+    section.bg = KapaColorByName ("none"); // XXX probably should be 'none'
+
+    section.x = 0.0;
+    section.dx = 1;
+    section.y = 0.0;
+    section.dy = 1.0;
     section.name = NULL;
     psStringAppend( &section.name, "a0");
-    KapaInitGraph(&graphdata);
-    KapaSetSection(kapa, &section);
+    KapaSetSection(kapa1, &section);
     psFree(section.name);
 
@@ -404,7 +395,7 @@
     graphdata.style = 2;
     graphdata.color = KapaColorByName ("black");
-    KapaClearPlots(kapa);
-
-    //set up plot vectors
+    KapaClearPlots(kapa1);
+
+    // set up plot vectors
     float Xmin = +FLT_MAX;
     float Xmax = -FLT_MAX;
@@ -414,5 +405,5 @@
     psVector *yVec = psVectorAlloc (input->n, PS_TYPE_F32);
 
-    //determine boundaries for histogram bin calculation
+    // determine boundaries for histogram bin calculation
     int n = 0;
     for (int i=0; i< input->n; i++) {
@@ -430,30 +421,30 @@
     xVec->n = yVec->n = n;
 
-    //plot stars
+    // plot stars
     graphdata.xmax = Xmax;
     graphdata.xmin = Xmin;
     graphdata.ymax = Ymax;
     graphdata.ymin = Ymin;
-    KapaSetLimits (kapa, &graphdata);
-    KapaSetFont (kapa, "helvetica", 14);
-
-    KapaBox (kapa, &graphdata);
-
-    KapaSendLabel (kapa, "L (pixels)", KAPA_LABEL_XM);
-    KapaSendLabel (kapa, "M (pixels)", KAPA_LABEL_YM);
-    KapaSendLabel (kapa, "Regions Flagged as Clumps (Red Boxes)",
+    KapaSetLimits (kapa1, &graphdata);
+    KapaSetFont (kapa1, "helvetica", 14);
+
+    KapaBox (kapa1, &graphdata);
+
+    KapaSendLabel (kapa1, "L (pixels)", KAPA_LABEL_XM);
+    KapaSendLabel (kapa1, "M (pixels)", KAPA_LABEL_YM);
+    KapaSendLabel (kapa1, "Regions Flagged as Clumps (Red Boxes)",
                    KAPA_LABEL_XP);
 
-    KapaPrepPlot (kapa, xVec->n, &graphdata);
-    KapaPlotVector (kapa, xVec->n, xVec->data.F32, "x");
-    KapaPlotVector (kapa, yVec->n, yVec->data.F32, "y");
+    KapaPrepPlot (kapa1, xVec->n, &graphdata);
+    KapaPlotVector (kapa1, xVec->n, xVec->data.F32, "x");
+    KapaPlotVector (kapa1, yVec->n, yVec->data.F32, "y");
 
     graphdata.color = KapaColorByName ("red");
     graphdata.style = 0;
 
-    //overplot clumpy regions excluded from analysis
-    for(int i = 0; i < count->numCols; i++) {
+    // overplot clumpy regions excluded from analysis
+    for (int i = 0; i < count->numCols; i++) {
         for (int j = 0; j < count->numRows; j++) {
-            if(count->data.U32[j][i] <= limit) continue; //not a clump
+            if(count->data.U32[j][i] <= limit) continue; // not a clump
             float Xbot = (i - 5) * scale + Xmin;
             float Ybot = (j - 5) * scale + Ymin;
@@ -462,11 +453,11 @@
             float x[5] = {Xbot, Xbot + scale, Xbot + scale, Xbot, Xbot};
             float y[5] = {Ybot, Ybot, Ybot + scale, Ybot + scale, Ybot};
-            KapaPrepPlot (kapa, 5, &graphdata);
-            KapaPlotVector (kapa, 5, x, "x");
-            KapaPlotVector (kapa, 5, y, "y");
+            KapaPrepPlot (kapa1, 5, &graphdata);
+            KapaPlotVector (kapa1, 5, x, "x");
+            KapaPlotVector (kapa1, 5, y, "y");
         }
     }
 
-    //ask for user input and finish
+    // ask for user input and finish
     pmVisualAskUser (&plotRemoveClumps);
     psFree (xVec);
@@ -483,14 +474,11 @@
                                   )
 {
-
-    //make sure we want to plot this
-    if (!pmVisualIsVisual() || !plotOneChipFit)
-        return true;
-
-    if(!pmVisualInitWindow(&kapa, "psastro:plots") || !pmVisualInitWindow(&kapa2, "psastro:plots")) {
-        return false;
-    }
-
-    //plot the residuals
+    if (!plotOneChipFit) return true;
+    if (!pmVisualTestLevel("psastro.plot5", 1)) return true;
+    if (!pmVisualInitWindow(&kapa1, "psastro:plot1")) return false;
+    if (!pmVisualInitWindow(&kapa2, "psastro:plot2")) return false;
+    if (!pmVisualInitWindow(&kapa3, "psastro:plot3")) return false;
+
+    // plot the residuals
     if (!residPlot(rawstars, refstars, match, recipe, "Single Chip Fit Residuals (Chip Coordinates)")) {
         plotOneChipFit = false;
@@ -498,5 +486,5 @@
     }
 
-    //ask for user input and finish
+    // ask for user input and finish
     pmVisualAskUser(&plotOneChipFit);
     return true;
@@ -509,15 +497,14 @@
                                 )
 {
-    //make sure we want to plot this
-    if(!pmVisualIsVisual() || !plotFixChips) return true;
-
-    if(!pmVisualInitWindow(&kapa, "psastro:plots")) {
-        return false;
-    }
-
-    KapaSection section = {"s1", .05, .05, .9, .9};
+    if (!plotFixChips) return true;
+    if (!pmVisualTestLevel("psastro.plot6", 1)) return true;
+    if (!pmVisualInitWindow(&kapa1, "psastro:plots")) return false;
+
+    int colorNone = KapaColorByName ("none"); // XXX probably should be 'none'
+
+    KapaSection section = {"s1", 0.0, 0.0, 1.0, 1.0, colorNone};
     Graphdata graphdata;
-    KapaInitGraph( &graphdata);
-    KapaClearPlots (kapa);
+    KapaInitGraph (&graphdata);
+    KapaClearPlots (kapa1);
     graphdata.ptype = 2;
     graphdata.style = 2;
@@ -559,19 +546,19 @@
     }
 
-    //set up graph
+    // set up graph
     pmVisualScaleGraphdata(&graphdata, xOld, yOld, true);
-    pmVisualInitGraph(kapa, &section, &graphdata);
-    KapaSendLabel (kapa, "L (FP)", KAPA_LABEL_XM);
-    KapaSendLabel (kapa, "M (FP)", KAPA_LABEL_YM);
-    KapaSendLabel (kapa, "Chip corners before (black) and after (red) FixChips", KAPA_LABEL_XP);
-    KapaPrepPlot (kapa, xOld->n, &graphdata);
-    KapaPlotVector (kapa, xOld->n, xOld->data.F32, "x");
-    KapaPlotVector (kapa, xOld->n, yOld->data.F32, "y");
+    pmVisualInitGraph(kapa1, &section, &graphdata);
+    KapaSendLabel (kapa1, "L (FP)", KAPA_LABEL_XM);
+    KapaSendLabel (kapa1, "M (FP)", KAPA_LABEL_YM);
+    KapaSendLabel (kapa1, "Chip corners before (black) and after (red) FixChips", KAPA_LABEL_XP);
+    KapaPrepPlot (kapa1, xOld->n, &graphdata);
+    KapaPlotVector (kapa1, xOld->n, xOld->data.F32, "x");
+    KapaPlotVector (kapa1, xOld->n, yOld->data.F32, "y");
 
     graphdata.ptype = 1;
     graphdata.color = KapaColorByName("red");
-    KapaPrepPlot (kapa, xNew->n, &graphdata);
-    KapaPlotVector (kapa, xNew->n, xNew->data.F32, "x");
-    KapaPlotVector (kapa, xNew->n, yNew->data.F32, "y");
+    KapaPrepPlot (kapa1, xNew->n, &graphdata);
+    KapaPlotVector (kapa1, xNew->n, xNew->data.F32, "x");
+    KapaPlotVector (kapa1, xNew->n, yNew->data.F32, "y");
 
     pmVisualAskUser(&plotFixChips);
@@ -591,17 +578,14 @@
                                         )
 {
-
-    //make sure we want to plot this
-    if (!pmVisualIsVisual() || !plotAstromGuessCheck) return true;
-
-    //set up graph window
-    if ( !pmVisualInitWindow (&kapa, "psastro:plots")) {
-        return false;
-    }
+    if (!plotAstromGuessCheck) return true;
+    if (!pmVisualTestLevel("psastro.plot7", 1)) return true;
+    if (!pmVisualInitWindow (&kapa1, "psastro:plots")) return false;
 
     Graphdata graphdata;
     KapaSection section;
-    KapaInitGraph(&graphdata);
-    KapaClearPlots (kapa);
+    KapaInitGraph (&graphdata);
+    KapaClearPlots (kapa1);
+
+    section.bg = KapaColorByName ("none"); // XXX probably should be 'none'
 
     graphdata.color = KapaColorByName ("black");
@@ -613,23 +597,23 @@
     section.dy = 0.4;
 
-    //Old Corners
+    // Old Corners
     section.x = 0.30;
     section.y = 0.50;
     section.name = NULL;
     psStringAppend (&section.name, "a0");
-    KapaSetSection (kapa, &section);
+    KapaSetSection (kapa1, &section);
     psFree(section.name);
 
     pmVisualScaleGraphdata (&graphdata, cornerPo, cornerPo, true);
-    KapaSetLimits (kapa, &graphdata);
-    KapaBox (kapa, &graphdata);
-    KapaSendLabel (kapa, "P (Pixels)", KAPA_LABEL_XM);
-    KapaSendLabel (kapa, "Q (Pixels)", KAPA_LABEL_YM);
-    KapaSendLabel (kapa,
+    KapaSetLimits (kapa1, &graphdata);
+    KapaBox (kapa1, &graphdata);
+    KapaSendLabel (kapa1, "P (Pixels)", KAPA_LABEL_XM);
+    KapaSendLabel (kapa1, "Q (Pixels)", KAPA_LABEL_YM);
+    KapaSendLabel (kapa1,
                    "Fiducial Points in the Tangent Plane. Black: Initial Astrometry. Red: Final Astrometry",
                    KAPA_LABEL_XP);
-    KapaPrepPlot (kapa, cornerPo->n, &graphdata);
-    KapaPlotVector (kapa, cornerPo->n, cornerPo->data.F32, "x");
-    KapaPlotVector (kapa, cornerQo->n, cornerQo->data.F32, "y");
+    KapaPrepPlot (kapa1, cornerPo->n, &graphdata);
+    KapaPlotVector (kapa1, cornerPo->n, cornerPo->data.F32, "x");
+    KapaPlotVector (kapa1, cornerQo->n, cornerQo->data.F32, "y");
 
     // New Corners
@@ -637,7 +621,7 @@
     graphdata.ptype = 7;
     graphdata.size = 1.5;
-    KapaPrepPlot (kapa, cornerPn->n, &graphdata);
-    KapaPlotVector (kapa, cornerPn->n, cornerPn->data.F32, "x");
-    KapaPlotVector (kapa, cornerQn->n, cornerQn->data.F32, "y");
+    KapaPrepPlot (kapa1, cornerPn->n, &graphdata);
+    KapaPlotVector (kapa1, cornerPn->n, cornerPn->data.F32, "x");
+    KapaPlotVector (kapa1, cornerQn->n, cornerQn->data.F32, "y");
 
     // Residuals
@@ -655,18 +639,18 @@
     section.name = NULL;
     psStringAppend (&section.name, "a1");
-    KapaSetSection (kapa, &section);
+    KapaSetSection (kapa1, &section);
     psFree(section.name);
 
     pmVisualScaleGraphdata (&graphdata, xResid, yResid, true);
-    KapaSetLimits (kapa, &graphdata);
-    KapaBox (kapa, &graphdata);
-    KapaSendLabel (kapa, "dP", KAPA_LABEL_XM);
-    KapaSendLabel (kapa, "dQ", KAPA_LABEL_YM);
-    KapaSendLabel (kapa,
+    KapaSetLimits (kapa1, &graphdata);
+    KapaBox (kapa1, &graphdata);
+    KapaSendLabel (kapa1, "dP", KAPA_LABEL_XM);
+    KapaSendLabel (kapa1, "dQ", KAPA_LABEL_YM);
+    KapaSendLabel (kapa1,
                    "Residual of the Fit from the Initial Astrometry to the Final Astrometry",
                    KAPA_LABEL_XP);
-    KapaPrepPlot (kapa, cornerPd->n, &graphdata);
-    KapaPlotVector (kapa, cornerPd->n, xResid->data.F32, "x");
-    KapaPlotVector (kapa, cornerQd->n, yResid->data.F32, "y");
+    KapaPrepPlot (kapa1, cornerPd->n, &graphdata);
+    KapaPlotVector (kapa1, cornerPd->n, xResid->data.F32, "x");
+    KapaPlotVector (kapa1, cornerQd->n, yResid->data.F32, "y");
 
     psFree(xResid);
@@ -681,13 +665,12 @@
                                    )
 {
-    //make sure we want to plot this
-    if (!pmVisualIsVisual() || !plotCommonScale) return true;
-
-    if (!pmVisualInitWindow(&kapa, "psastro:plots")){
-        return false;
-    }
-
-    KapaSection section = {"s1", .05, .05, .9, .9};
+    if (!plotCommonScale) return true;
+    if (!pmVisualTestLevel("psastro.plot8", 1)) return true;
+    if (!pmVisualInitWindow(&kapa1, "psastro:plots")) return false;
+
+    int colorNone = KapaColorByName ("none");
+    KapaSection section = {"s1", 0.0, 0.0, 1.0, 1.0, colorNone};
     Graphdata graphdata;
+
     psPlane ptCH, ptFP;
     ptCH.x = 0;
@@ -698,5 +681,5 @@
     int nobj = 0;
 
-    //project each chip corner to the Focal Plane
+    // project each chip corner to the Focal Plane
     for(int i = 0; i < fpa->chips->n; i++) {
         pmChip *chip = fpa->chips->data[i];
@@ -711,13 +694,13 @@
     }
 
-    //set up plot window
+    // set up plot window
     KapaInitGraph (&graphdata);
-    KapaClearPlots (kapa);
-    KapaSetSection (kapa, &section);
-    KapaSetFont (kapa, "helvetica", 14);
-    pmVisualTriplePlot (kapa, &graphdata, xVec, yVec, oldScale, false);
-    KapaSendLabel (kapa, "L (FP)", KAPA_LABEL_XM);
-    KapaSendLabel (kapa, "M (FP)", KAPA_LABEL_YM);
-    KapaSendLabel (kapa, "Old Pixel Scale of FPA Chips (Not to Scale)", KAPA_LABEL_XP);
+    KapaClearPlots (kapa1);
+    KapaSetSection (kapa1, &section);
+    KapaSetFont (kapa1, "helvetica", 14);
+    pmVisualTriplePlot (kapa1, &graphdata, xVec, yVec, oldScale, false);
+    KapaSendLabel (kapa1, "L (FP)", KAPA_LABEL_XM);
+    KapaSendLabel (kapa1, "M (FP)", KAPA_LABEL_YM);
+    KapaSendLabel (kapa1, "Old Pixel Scale of FPA Chips (Not to Scale)", KAPA_LABEL_XP);
 
     pmVisualAskUser (&plotCommonScale);
@@ -733,13 +716,11 @@
                                      psArray *match, psMetadata *recipe)
 {
-
-    //make sure we want to plot this
-    if (!pmVisualIsVisual() || !plotMosaicOneChip) return true;
-
-    if(!pmVisualInitWindow(&kapa, "psastro:plots") || !pmVisualInitWindow(&kapa2, "psastro:plots")) {
-        return false;
-    }
-
-    //plot the residuals
+    if (!plotMosaicOneChip) return true;
+    if (!pmVisualTestLevel("psastro.plot9", 1)) return true;
+    if (!pmVisualInitWindow(&kapa1, "psastro:plot1")) return false;
+    if (!pmVisualInitWindow(&kapa2, "psastro:plot2")) return false;
+    if (!pmVisualInitWindow(&kapa3, "psastro:plot3")) return false;
+
+    // plot the residuals
     if (!residPlot(rawstars, refstars, match, recipe, "Single Chip Fit Residuals - Mosaic Mode")) {
         pmVisualSetVisual(false);
@@ -747,5 +728,5 @@
     }
 
-    //ask for user input and finish
+    // ask for user input and finish
     pmVisualAskUser(&plotMosaicOneChip);
 
@@ -758,14 +739,12 @@
                                     psMetadata *recipe)
 {
-    //make sure we want to plot this
-    if (!pmVisualIsVisual() || !plotMosaicMatches) return true;
+    if (!plotMosaicMatches) return true;
+    if (!pmVisualTestLevel("psastro.plot10", 1)) return true;
+    if (!pmVisualInitWindow(&kapa1, "psastro:plot1")) return false;
+    if (!pmVisualInitWindow(&kapa2, "psastro:plot2")) return false;
+    if (!pmVisualInitWindow(&kapa3, "psastro:plot3")) return false;
 
     char title[60];
     sprintf(title, "Matches found during psastroMosaicSetMatch iteration %d", iteration);
-
-
-    if(!pmVisualInitWindow(&kapa, "psastro:plots") || !pmVisualInitWindow(&kapa2, "psastro:plots")) {
-        return false;
-    }
 
     if (!residPlot(rawstars, refstars, match, recipe, title)){
@@ -774,5 +753,5 @@
     }
 
-    //ask for user input
+    // ask for user input
     pmVisualAskUser (&plotMosaicMatches);
     return true;
@@ -789,13 +768,12 @@
                                   double Offset)
 {
-    //make sure we want to plot this
-    if (!pmVisualIsVisual() || !plotGridMatch) return true;
-    if (!pmVisualInitWindow(&kapa, "psastro:plots")){
-        return false;
-    }
-
-    KapaSection section = {"s1", 0.05, 0.05, .75, .75};
-    KapaSection sectionY = {"s2", 0.8, 0.05, .15, .75};
-    KapaSection sectionX = {"s3", .05, .8, .75, .15};
+    if (!plotGridMatch) return true;
+    if (!pmVisualTestLevel("psastro.plot11", 1)) return true;
+    if (!pmVisualInitWindow(&kapa1, "psastro:plots")) return false;
+
+    int colorNone = KapaColorByName ("none");
+    KapaSection section  = {"s1", 0.00, 0.00, 0.75, 0.75, colorNone};
+    KapaSection sectionY = {"s2", 0.75, 0.00, 0.25, 0.75, colorNone};
+    KapaSection sectionX = {"s3", 0.00, 0.75, 0.75, 0.25, colorNone};
 
     Graphdata graphdata;
@@ -818,7 +796,7 @@
 
     // set up plot information
-    KapaClearPlots(kapa);
+    KapaClearPlots(kapa1);
     KapaInitGraph(&graphdata);
-    KapaSetSection(kapa, &section);
+    KapaSetSection(kapa1, &section);
 
     graphdata.xmin = -1.0 * maxOffpix;
@@ -826,11 +804,11 @@
     graphdata.ymin = -1.0 * maxOffpix;
     graphdata.ymax =  1.0 * maxOffpix;
-    KapaSetLimits(kapa, &graphdata);
-
-    KapaSetFont(kapa, "helvetica", 14);
-    KapaBox(kapa, &graphdata);
-    KapaSendLabel (kapa, "X offset (FP)", KAPA_LABEL_XM);
-    KapaSendLabel (kapa, "Y offset (FP)", KAPA_LABEL_YM);
-    KapaSendLabel (kapa, "pmAstromGridAngle residuals. Box: Correlation Peak.",
+    KapaSetLimits(kapa1, &graphdata);
+
+    KapaSetFont(kapa1, "helvetica", 14);
+    KapaBox(kapa1, &graphdata);
+    KapaSendLabel (kapa1, "X offset (FP)", KAPA_LABEL_XM);
+    KapaSendLabel (kapa1, "Y offset (FP)", KAPA_LABEL_YM);
+    KapaSendLabel (kapa1, "pmAstromGridAngle residuals. Box: Correlation Peak.",
                    KAPA_LABEL_XP);
     graphdata.style = 2;
@@ -868,10 +846,10 @@
     }
 
-    //Plot the offsets
-    KapaPrepPlot(kapa, nplot, &graphdata);
-    KapaPlotVector (kapa, nplot, dXplot->data.F32, "x");
-    KapaPlotVector (kapa, nplot, dYplot->data.F32, "y");
-
-    //Overplot bounding box, peak of distribution
+    // Plot the offsets
+    KapaPrepPlot(kapa1, nplot, &graphdata);
+    KapaPlotVector (kapa1, nplot, dXplot->data.F32, "x");
+    KapaPlotVector (kapa1, nplot, dYplot->data.F32, "y");
+
+    // Overplot bounding box, peak of distribution
     float xbound[5] = { -maxOffpix, maxOffpix, maxOffpix, -maxOffpix, -maxOffpix};
     float ybound[5] = { -maxOffpix, -maxOffpix, maxOffpix, maxOffpix, -maxOffpix};
@@ -885,13 +863,13 @@
     graphdata.style = 0;
     graphdata.size = 1.0;
-    KapaPrepPlot(kapa, 5, &graphdata);
-    KapaPlotVector (kapa, 5, xbound, "x");
-    KapaPlotVector (kapa, 5, ybound, "y");
-    KapaPrepPlot(kapa, 5, &graphdata);
-    KapaPlotVector (kapa, 5, xbin, "x");
-    KapaPlotVector (kapa, 5, ybin, "y");
-
-    //plot X profile
-    KapaSetSection(kapa, &sectionX);
+    KapaPrepPlot(kapa1, 5, &graphdata);
+    KapaPlotVector (kapa1, 5, xbound, "x");
+    KapaPlotVector (kapa1, 5, ybound, "y");
+    KapaPrepPlot(kapa1, 5, &graphdata);
+    KapaPlotVector (kapa1, 5, xbin, "x");
+    KapaPlotVector (kapa1, 5, ybin, "y");
+
+    // plot X profile
+    KapaSetSection(kapa1, &sectionX);
     graphdata.color = KapaColorByName("black");
     graphdata.ptype = 1;
@@ -899,10 +877,11 @@
     graphdata.ymin = 0;
     graphdata.ymax = maxHorizontalSlice + 0.5;
-    KapaSetLimits(kapa, &graphdata);
-
-    KapaBox(kapa, &graphdata);
-    KapaPrepPlot(kapa, gridNP->numCols, &graphdata);
-    KapaPlotVector (kapa, gridNP->numCols, horizontalIndices, "x");
-    KapaPlotVector (kapa, gridNP->numCols, horizHistSlice, "y");
+    strcpy (graphdata.labels, "0200");
+    KapaSetLimits(kapa1, &graphdata);
+
+    KapaBox(kapa1, &graphdata);
+    KapaPrepPlot(kapa1, gridNP->numCols, &graphdata);
+    KapaPlotVector (kapa1, gridNP->numCols, horizontalIndices, "x");
+    KapaPlotVector (kapa1, gridNP->numCols, horizHistSlice, "y");
 
     float xslice[2] = {offsetX - Scale / 2., offsetX - Scale / 2.};
@@ -910,10 +889,10 @@
     graphdata.style = 0;
     graphdata.color = KapaColorByName("red");
-    KapaPrepPlot(kapa, 2, &graphdata);
-    KapaPlotVector (kapa, 2, xslice, "x");
-    KapaPlotVector (kapa, 2, yslice, "y");
-
-    //plot Y profile
-    KapaSetSection(kapa, &sectionY);
+    KapaPrepPlot(kapa1, 2, &graphdata);
+    KapaPlotVector (kapa1, 2, xslice, "x");
+    KapaPlotVector (kapa1, 2, yslice, "y");
+
+    // plot Y profile
+    KapaSetSection(kapa1, &sectionY);
     graphdata.color = KapaColorByName("black");
     graphdata.ptype = 1;
@@ -923,10 +902,11 @@
     graphdata.xmin = -1.0 ;
     graphdata.xmax = maxVerticalSlice + 0.5;
-    KapaSetLimits(kapa, &graphdata);
-
-    KapaBox(kapa, &graphdata);
-    KapaPrepPlot(kapa, gridNP->numRows, &graphdata);
-    KapaPlotVector (kapa, gridNP->numRows, vertHistSlice, "x");
-    KapaPlotVector (kapa, gridNP->numRows, verticalIndices, "y");
+    strcpy (graphdata.labels, "2000");
+    KapaSetLimits(kapa1, &graphdata);
+
+    KapaBox(kapa1, &graphdata);
+    KapaPrepPlot(kapa1, gridNP->numRows, &graphdata);
+    KapaPlotVector (kapa1, gridNP->numRows, vertHistSlice, "x");
+    KapaPlotVector (kapa1, gridNP->numRows, verticalIndices, "y");
 
     yslice[0] = yslice[1] = offsetY - Scale / 2.;
@@ -934,7 +914,7 @@
     graphdata.style = 0;
     graphdata.color = KapaColorByName("red");
-    KapaPrepPlot(kapa, 2, &graphdata);
-    KapaPlotVector (kapa, 2, xslice, "x");
-    KapaPlotVector (kapa, 2, yslice, "y");
+    KapaPrepPlot(kapa1, 2, &graphdata);
+    KapaPlotVector (kapa1, 2, xslice, "x");
+    KapaPlotVector (kapa1, 2, yslice, "y");
 
     pmVisualAskUser(&plotGridMatch);
@@ -946,11 +926,10 @@
 
 bool pmAstromVisualPlotGridMatchOverlay (const psArray *raw,
-					 const psArray *ref)
+					 const psArray *ref,
+					 const psPlane offset)
 {
-    //make sure we want to plot this
-    if (!pmVisualIsVisual() || !plotGridMatch) return true;
-    if (!pmVisualInitWindow(&kapa2, "psastro:plots")){
-        return false;
-    }
+    if (!plotGridMatch) return true;
+    if (!pmVisualTestLevel("psastro.plot12", 1)) return true;
+    if (!pmVisualInitWindow(&kapa2, "psastro:plots")) return false;
 
     Graphdata graphdata;
@@ -980,6 +959,6 @@
 	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;
+	xPlot->data.F32[i] = obj->FP->x + offset.x;
+	yPlot->data.F32[i] = obj->FP->y + offset.y;
 	zPlot->data.F32[i] = obj->Mag;
     }
@@ -999,5 +978,5 @@
     // the point size will be scaled from the z vector
     graphdata.style = 2;
-    graphdata.ptype = 7;
+    graphdata.ptype = 2;
     graphdata.size = -1;
     graphdata.color = KapaColorByName ("black");
@@ -1052,18 +1031,19 @@
     )
 {
-    //make sure we want to plot this
-    if (!pmVisualIsVisual() || !plotTweak) return true;
-    if (!pmVisualInitWindow(&kapa, "psastro:plots")) {
-        return false;
-    }
+    if (!plotTweak) return true;
+    if (!pmVisualTestLevel("psastro.plot13", 1)) return true;
+    if (!pmVisualInitWindow(&kapa1, "psastro:plots")) return false;
 
     Graphdata graphdata;
-    KapaSection section1 = {"s1", 0.05, 0.05, 0.90, 0.4};
-    KapaSection section2 = {"s2", 0.05, 0.5, 0.90, 0.4};
+
+    int colorNone = KapaColorByName ("none"); // XXX probably should be 'none'
+    KapaSection section1 = {"s1", 0.0, 0.0, 1.0, 0.5, colorNone};
+    KapaSection section2 = {"s2", 0.0, 0.5, 1.0, 0.5, colorNone};
+    KapaSection section3 = {"s3", 0.0, 0.0, 1.0, 1.0, colorNone};
 
     psVector *xIndices = psVectorAlloc (xHist->n, PS_TYPE_F32);
     psVector *yIndices = psVectorAlloc (yHist->n, PS_TYPE_F32);
 
-    //populate the Indices vectors
+    // populate the Indices vectors
     for(int i = 0; i < xHist->n; i++) {
         xIndices->data.F32[i] = i;
@@ -1074,16 +1054,16 @@
 
     // set up plot information
-    KapaClearPlots(kapa);
+    KapaClearPlots(kapa1);
     KapaInitGraph(&graphdata);
 
     // plot the X histogram
     pmVisualScaleGraphdata(&graphdata, xIndices, xHist, false);
-    KapaSetSection(kapa, &section1);
-    KapaSetLimits (kapa, &graphdata);
-    KapaSetFont(kapa, "helvetica", 14);
-    KapaBox(kapa, &graphdata);
-    KapaSendLabel (kapa, "X offset Bin", KAPA_LABEL_XM);
-    KapaSendLabel (kapa, "Number of Sources", KAPA_LABEL_YM);
-    KapaSendLabel (kapa, "Horizontal Profile",
+    KapaSetSection(kapa1, &section1);
+    KapaSetLimits (kapa1, &graphdata);
+    KapaSetFont(kapa1, "helvetica", 14);
+    KapaBox(kapa1, &graphdata);
+    KapaSendLabel (kapa1, "X offset Bin", KAPA_LABEL_XM);
+    KapaSendLabel (kapa1, "Number of Sources", KAPA_LABEL_YM);
+    KapaSendLabel (kapa1, "Horizontal Profile",
                    KAPA_LABEL_XP);
     graphdata.style = 1;
@@ -1092,26 +1072,26 @@
     graphdata.color = KapaColorByName ("black");
 
-    KapaPrepPlot (kapa, xHist->n, &graphdata);
-    KapaPlotVector (kapa, xHist->n, xIndices->data.F32, "x");
-    KapaPlotVector (kapa, xHist->n, xHist->data.F32, "y");
-
-    //overplot the peak
+    KapaPrepPlot (kapa1, xHist->n, &graphdata);
+    KapaPlotVector (kapa1, xHist->n, xIndices->data.F32, "x");
+    KapaPlotVector (kapa1, xHist->n, xHist->data.F32, "y");
+
+    // overplot the peak
     float x[2] = {xBin, xBin};
     float y[2] = {-500, 500};
     graphdata.color = KapaColorByName ("red");
-    KapaPrepPlot (kapa, 2, &graphdata);
-    KapaPlotVector (kapa, 2, x, "x");
-    KapaPlotVector (kapa, 2, y, "y");
-
-    //plot the Y histogram
+    KapaPrepPlot (kapa1, 2, &graphdata);
+    KapaPlotVector (kapa1, 2, x, "x");
+    KapaPlotVector (kapa1, 2, y, "y");
+
+    // plot the Y histogram
     pmVisualScaleGraphdata(&graphdata, yIndices, yHist, false);
-    KapaSetSection(kapa, &section2);
-    KapaSetLimits (kapa, &graphdata);
-    KapaSetFont(kapa, "helvetica", 14);
+    KapaSetSection(kapa1, &section2);
+    KapaSetLimits (kapa1, &graphdata);
+    KapaSetFont(kapa1, "helvetica", 14);
     graphdata.color = KapaColorByName ("black");
-    KapaBox(kapa, &graphdata);
-    KapaSendLabel (kapa, "Y offset Bin", KAPA_LABEL_XM);
-    KapaSendLabel (kapa, "Number of Sources", KAPA_LABEL_YM);
-    KapaSendLabel (kapa, "Vertical Profile",
+    KapaBox(kapa1, &graphdata);
+    KapaSendLabel (kapa1, "Y offset Bin", KAPA_LABEL_XM);
+    KapaSendLabel (kapa1, "Number of Sources", KAPA_LABEL_YM);
+    KapaSendLabel (kapa1, "Vertical Profile",
                    KAPA_LABEL_XP);
     graphdata.style = 1;
@@ -1119,20 +1099,20 @@
     graphdata.size = 0.4;
 
-    KapaPrepPlot (kapa, yHist->n, &graphdata);
-    KapaPlotVector (kapa, yHist->n, yIndices->data.F32, "x");
-    KapaPlotVector (kapa, yHist->n, yHist->data.F32, "y");
-
-    //overplot the peak
+    KapaPrepPlot (kapa1, yHist->n, &graphdata);
+    KapaPlotVector (kapa1, yHist->n, yIndices->data.F32, "x");
+    KapaPlotVector (kapa1, yHist->n, yHist->data.F32, "y");
+
+    // overplot the peak
     x[0] = x[1] = yBin;
     graphdata.color = KapaColorByName ("red");
-    KapaPrepPlot (kapa, 2, &graphdata);
-    KapaPlotVector (kapa, 2, x, "x");
-    KapaPlotVector (kapa, 2, y, "y");
-
-    //plot title
+    KapaPrepPlot (kapa1, 2, &graphdata);
+    KapaPlotVector (kapa1, 2, x, "x");
+    KapaPlotVector (kapa1, 2, y, "y");
+
+    // plot title
     graphdata.color = KapaColorByName("black");
-    KapaSection section3 = {"s3", 0, 0, 1, 1};
-    KapaSetSection( kapa, &section3);
-    KapaSendLabel (kapa, "Tweaking the Astrometry Grid Solution. Smoothed profiles + peak location",
+
+    KapaSetSection( kapa1, &section3);
+    KapaSendLabel (kapa1, "Tweaking the Astrometry Grid Solution. Smoothed profiles + peak location",
                    KAPA_LABEL_XP);
 
@@ -1142,5 +1122,5 @@
     psFree(yIndices);
     return true;
-} //end of pmAstromPlotTweak
+} // end of pmAstromPlotTweak
 
 
@@ -1149,10 +1129,11 @@
 
 
-    //initialize graph information
+    // initialize graph information
     Graphdata graphdata;
     KapaSection section;
+    section.bg = KapaColorByName ("none"); // XXX probably should be 'none'
 
     KapaInitGraph (&graphdata);
-    KapaClearPlots (kapa);
+    KapaClearPlots (kapa1);
 
     graphdata.color = KapaColorByName ("black");
@@ -1164,5 +1145,5 @@
     section.dy = 0.4;
 
-    //initialize and populate the plotting vectors
+    // initialize and populate the plotting vectors
     bool status = false;
     float iMagMin = psMetadataLookupF32 (&status, recipe, "PSASTRO.PLOT.INST.MAG.MIN");
@@ -1180,5 +1161,5 @@
     section.name = NULL;
     psStringAppend (&section.name, "a0");
-    KapaSetSection (kapa, &section);
+    KapaSetSection (kapa1, &section);
     psFree (section.name);
 
@@ -1202,7 +1183,7 @@
     xVec->n = yVec->n = zVec->n = n;
 
-    KapaSendLabel (kapa, "X", KAPA_LABEL_XM);
-    KapaSendLabel (kapa, "dX", KAPA_LABEL_YM);
-    pmVisualTriplePlot (kapa, &graphdata, xVec, yVec, zVec, false);
+    KapaSendLabel (kapa1, "X", KAPA_LABEL_XM);
+    KapaSendLabel (kapa1, "dX", KAPA_LABEL_YM);
+    pmVisualTriplePlot (kapa1, &graphdata, xVec, yVec, zVec, false);
 
     // X vs dY
@@ -1211,5 +1192,5 @@
     section.name = NULL;
     psStringAppend (&section.name, "a1");
-    KapaSetSection (kapa, &section);
+    KapaSetSection (kapa1, &section);
     psFree (section.name);
 
@@ -1233,7 +1214,7 @@
     xVec->n = yVec->n = zVec->n = n;
 
-    KapaSendLabel (kapa, "X", KAPA_LABEL_XM);
-    KapaSendLabel (kapa, "dY", KAPA_LABEL_YM);
-    pmVisualTriplePlot (kapa, &graphdata, xVec, yVec, zVec, false);
+    KapaSendLabel (kapa1, "X", KAPA_LABEL_XM);
+    KapaSendLabel (kapa1, "dY", KAPA_LABEL_YM);
+    pmVisualTriplePlot (kapa1, &graphdata, xVec, yVec, zVec, false);
 
     // Y vs dX
@@ -1242,5 +1223,5 @@
     section.name = NULL;
     psStringAppend (&section.name, "a2");
-    KapaSetSection (kapa, &section);
+    KapaSetSection (kapa1, &section);
     psFree (section.name);
 
@@ -1264,7 +1245,7 @@
     xVec->n = yVec->n = zVec->n = n;
 
-    KapaSendLabel (kapa, "Y", KAPA_LABEL_XM);
-    KapaSendLabel (kapa, "dX", KAPA_LABEL_YM);
-    pmVisualTriplePlot (kapa, &graphdata, xVec, yVec, zVec, false);
+    KapaSendLabel (kapa1, "Y", KAPA_LABEL_XM);
+    KapaSendLabel (kapa1, "dX", KAPA_LABEL_YM);
+    pmVisualTriplePlot (kapa1, &graphdata, xVec, yVec, zVec, false);
 
     // Y vs dY
@@ -1273,5 +1254,5 @@
     section.name = NULL;
     psStringAppend (&section.name, "a3");
-    KapaSetSection (kapa, &section);
+    KapaSetSection (kapa1, &section);
     psFree (section.name);
 
@@ -1295,7 +1276,11 @@
     xVec->n = yVec->n = zVec->n = n;
 
-    KapaSendLabel (kapa, "Y", KAPA_LABEL_XM);
-    KapaSendLabel (kapa, "dY", KAPA_LABEL_YM);
-    pmKapaPlotVectorTriple_AutoLimits_OpenGraph (kapa, &graphdata, xVec, yVec, zVec, false);
+    KapaSendLabel (kapa1, "Y", KAPA_LABEL_XM);
+    KapaSendLabel (kapa1, "dY", KAPA_LABEL_YM);
+    pmKapaPlotVectorTriple_AutoLimits_OpenGraph (kapa1, &graphdata, xVec, yVec, zVec, false);
+
+    psFree (xVec);
+    psFree (yVec);
+    psFree (zVec);
 
     section.x = 0.0;
@@ -1305,9 +1290,10 @@
     section.name = NULL;
     psStringAppend (&section.name, "a5");
-    KapaSetSection (kapa, &section);
-    KapaSendLabel (kapa, title, KAPA_LABEL_XP);
+    KapaSetSection (kapa1, &section);
+    KapaSendLabel (kapa1, title, KAPA_LABEL_XP);
     psFree (section.name);
 
-    //second window
+    // ***************************************
+    // second window
 
     KapaInitGraph (&graphdata);
@@ -1322,8 +1308,4 @@
     graphdata.ptype = 7;
     graphdata.style = 2;
-
-    psFree (xVec);
-    psFree (yVec);
-    psFree (zVec);
 
     xVec = psVectorAlloc (refstars->n, PS_TYPE_F32);
@@ -1346,5 +1328,5 @@
     pmVisualTriplePlot (kapa2, &graphdata, xVec, yVec, zVec, false);
 
-    //rescale the graph to include all points
+    // rescale the graph to include all points
     float xmin = graphdata.xmin;
     float ymin = graphdata.ymin;
@@ -1358,10 +1340,10 @@
     KapaSetLimits (kapa2, &graphdata);
 
-    bool plotTweak;
-    pmVisualAskUser(&plotTweak);
+    // bool plotTweak;
+    // pmVisualAskUser(&plotTweak);
 
     // X vs Y by mag (raw)
     graphdata.color = KapaColorByName ("black");
-    graphdata.ptype = 2;
+    graphdata.ptype = 7;
     graphdata.style = 2;
 
@@ -1389,5 +1371,5 @@
     pmVisualTripleOverplot (kapa2, &graphdata, xVec, yVec, zVec, false);
 
-    //overplot matched stars in blue
+    // overplot matched stars in blue
     psFree (xVec);
     psFree (yVec);
@@ -1398,4 +1380,5 @@
     zVec = psVectorAlloc (match->n, PS_TYPE_F32);
 
+    graphdata.ptype = 2;
     graphdata.color = KapaColorByName ("blue");
     n = 0;
@@ -1411,13 +1394,206 @@
         xVec->data.F32[n] = raw->chip->x;
         yVec->data.F32[n] = raw->chip->y;
-        zVec->data.F32[n] = iMagMin;
+        zVec->data.F32[n] = raw->Mag;
         n++;
     }
     xVec->n = yVec->n = zVec->n = n;
+    fprintf (stderr, "plotting %d matched stars (raw = blue)\n", n);
     pmVisualTripleOverplot (kapa2, &graphdata, xVec, yVec, zVec, false);
+
+    if (0) {
+	graphdata.ptype = 3;
+	graphdata.color = KapaColorByName ("green");
+	n = 0;
+	for (int i = 0; i < match->n; i++) {
+	    pmAstromMatch *pair = match->data[i];
+	    pmAstromObj *raw = rawstars->data[pair->raw];
+	    pmAstromObj *ref = refstars->data[pair->ref];
+	    if (raw->Mag < iMagMin) continue;
+	    if (raw->Mag > iMagMax) continue;
+	    if (ref->Mag < rMagMin) continue;
+	    if (ref->Mag > rMagMax) continue;
+
+	    xVec->data.F32[n] = ref->chip->x;
+	    yVec->data.F32[n] = ref->chip->y;
+	    zVec->data.F32[n] = ref->Mag;
+	    n++;
+	}
+	xVec->n = yVec->n = zVec->n = n;
+	fprintf (stderr, "plotting %d matched stars (ref = green)\n", n);
+	pmVisualTripleOverplot (kapa2, &graphdata, xVec, yVec, zVec, false);
+    }
 
     psFree (xVec);
     psFree (yVec);
     psFree (zVec);
+
+    // ***************************************
+    // third window, pt1
+
+    xVec = psVectorAlloc (match->n, PS_TYPE_F32);
+    yVec = psVectorAlloc (match->n, PS_TYPE_F32);
+
+    KapaInitGraph (&graphdata);
+    KapaClearPlots (kapa3);
+
+    // X vs Y by mag (ref)
+    graphdata.color = KapaColorByName ("black");
+    graphdata.ptype = 2;
+    graphdata.style = 2;
+    graphdata.size = 1;
+
+    section.x  = 0.0;
+    section.y  = 0.0;
+    section.dx = 0.5;
+    section.dy = 0.5;
+    section.name = NULL;
+    psStringAppend (&section.name, "s1");
+    KapaSetSection (kapa3, &section);
+    psFree (section.name);
+
+    n = 0;
+    for (int i = 0; i < match->n; i++) {
+        pmAstromMatch *pair = match->data[i];
+        pmAstromObj *raw = rawstars->data[pair->raw];
+        pmAstromObj *ref = refstars->data[pair->ref];
+        if (raw->Mag < iMagMin) continue;
+        if (raw->Mag > iMagMax) continue;
+        if (ref->Mag < rMagMin) continue;
+        if (ref->Mag > rMagMax) continue;
+
+        xVec->data.F32[n] = raw->Mag;
+        yVec->data.F32[n] = raw->chip->y - ref->chip->y;
+        n++;
+    }
+    xVec->n = yVec->n = n;
+
+    // rescale the graph to include all points
+    pmVisualScaleGraphdata(&graphdata, xVec, yVec, true);
+    KapaSetLimits (kapa3, &graphdata);
+    KapaBox (kapa3, &graphdata);
+    KapaSendLabel(kapa1, "raw mag", KAPA_LABEL_XM);
+    KapaSendLabel(kapa1, "dY", KAPA_LABEL_YM);
+
+    KapaPrepPlot (kapa3, xVec->n, &graphdata);
+    KapaPlotVector (kapa3, xVec->n, xVec->data.F32, "x");
+    KapaPlotVector (kapa3, yVec->n, yVec->data.F32, "y");
+
+    // ***************************************
+    // third window, pt2
+
+    section.x  = 0.5;
+    section.y  = 0.0;
+    section.dx = 0.5;
+    section.dy = 0.5;
+    section.name = NULL;
+    psStringAppend (&section.name, "s2");
+    KapaSetSection (kapa3, &section);
+    psFree (section.name);
+
+    n = 0;
+    for (int i = 0; i < match->n; i++) {
+        pmAstromMatch *pair = match->data[i];
+        pmAstromObj *raw = rawstars->data[pair->raw];
+        pmAstromObj *ref = refstars->data[pair->ref];
+        if (raw->Mag < iMagMin) continue;
+        if (raw->Mag > iMagMax) continue;
+        if (ref->Mag < rMagMin) continue;
+        if (ref->Mag > rMagMax) continue;
+
+        xVec->data.F32[n] = ref->Mag;
+        yVec->data.F32[n] = raw->chip->y - ref->chip->y;
+        n++;
+    }
+    xVec->n = yVec->n = n;
+
+    // rescale the graph to include all points
+    pmVisualScaleGraphdata(&graphdata, xVec, yVec, true);
+    KapaSetLimits (kapa3, &graphdata);
+    KapaBox (kapa3, &graphdata);
+    KapaSendLabel(kapa1, "ref mag", KAPA_LABEL_XM);
+    KapaSendLabel(kapa1, "dY", KAPA_LABEL_YM);
+
+    KapaPrepPlot (kapa3, xVec->n, &graphdata);
+    KapaPlotVector (kapa3, xVec->n, xVec->data.F32, "x");
+    KapaPlotVector (kapa3, yVec->n, yVec->data.F32, "y");
+
+    // ***************************************
+    // third window, pt3
+
+    section.x  = 0.0;
+    section.y  = 0.5;
+    section.dx = 0.5;
+    section.dy = 0.5;
+    section.name = NULL;
+    psStringAppend (&section.name, "s3");
+    KapaSetSection (kapa3, &section);
+    psFree (section.name);
+
+    n = 0;
+    for (int i = 0; i < match->n; i++) {
+        pmAstromMatch *pair = match->data[i];
+        pmAstromObj *raw = rawstars->data[pair->raw];
+        pmAstromObj *ref = refstars->data[pair->ref];
+        if (raw->Mag < iMagMin) continue;
+        if (raw->Mag > iMagMax) continue;
+        if (ref->Mag < rMagMin) continue;
+        if (ref->Mag > rMagMax) continue;
+
+        xVec->data.F32[n] = raw->Mag;
+        yVec->data.F32[n] = raw->chip->x - ref->chip->x;
+        n++;
+    }
+    xVec->n = yVec->n = n;
+
+    // rescale the graph to include all points
+    pmVisualScaleGraphdata(&graphdata, xVec, yVec, true);
+    KapaSetLimits (kapa3, &graphdata);
+    KapaBox (kapa3, &graphdata);
+    KapaSendLabel(kapa1, "raw mag", KAPA_LABEL_XM);
+    KapaSendLabel(kapa1, "dX", KAPA_LABEL_YM);
+
+    KapaPrepPlot (kapa3, xVec->n, &graphdata);
+    KapaPlotVector (kapa3, xVec->n, xVec->data.F32, "x");
+    KapaPlotVector (kapa3, yVec->n, yVec->data.F32, "y");
+
+    // ***************************************
+    // third window, pt4
+
+    section.x  = 0.5;
+    section.y  = 0.5;
+    section.dx = 0.5;
+    section.dy = 0.5;
+    section.name = NULL;
+    psStringAppend (&section.name, "s4");
+    KapaSetSection (kapa3, &section);
+    psFree (section.name);
+
+    n = 0;
+    for (int i = 0; i < match->n; i++) {
+        pmAstromMatch *pair = match->data[i];
+        pmAstromObj *raw = rawstars->data[pair->raw];
+        pmAstromObj *ref = refstars->data[pair->ref];
+        if (raw->Mag < iMagMin) continue;
+        if (raw->Mag > iMagMax) continue;
+        if (ref->Mag < rMagMin) continue;
+        if (ref->Mag > rMagMax) continue;
+
+        xVec->data.F32[n] = ref->Mag;
+        yVec->data.F32[n] = raw->chip->x - ref->chip->x;
+        n++;
+    }
+    xVec->n = yVec->n = n;
+
+    // rescale the graph to include all points
+    pmVisualScaleGraphdata(&graphdata, xVec, yVec, true);
+    KapaSetLimits (kapa3, &graphdata);
+    KapaBox (kapa3, &graphdata);
+    KapaSendLabel(kapa1, "ref mag", KAPA_LABEL_XM);
+    KapaSendLabel(kapa1, "dX", KAPA_LABEL_YM);
+
+    KapaPrepPlot (kapa3, xVec->n, &graphdata);
+    KapaPlotVector (kapa3, xVec->n, xVec->data.F32, "x");
+    KapaPlotVector (kapa3, yVec->n, yVec->data.F32, "y");
+
     return true;
 }
Index: /branches/eam_branches/ipp-20110213/psModules/src/astrom/pmAstrometryVisual.h
===================================================================
--- /branches/eam_branches/ipp-20110213/psModules/src/astrom/pmAstrometryVisual.h	(revision 31018)
+++ /branches/eam_branches/ipp-20110213/psModules/src/astrom/pmAstrometryVisual.h	(revision 31019)
@@ -47,5 +47,6 @@
 
 bool pmAstromVisualPlotGridMatchOverlay (const psArray *raw,
-					 const psArray *ref);
+					 const psArray *ref,
+					 const psPlane offset);
 
 /**
Index: /branches/eam_branches/ipp-20110213/psModules/src/extras/pmVisual.c
===================================================================
--- /branches/eam_branches/ipp-20110213/psModules/src/extras/pmVisual.c	(revision 31018)
+++ /branches/eam_branches/ipp-20110213/psModules/src/extras/pmVisual.c	(revision 31019)
@@ -1,3 +1,3 @@
-/** The following are a collection of core procedures to aid the creation of visaual diagnostics
+/** The following are a collection of core procedures to aid the creation of visual diagnostics
  *  @author Chris Beaumont, IfA
  *  @date January 23, 2008
@@ -80,9 +80,10 @@
 
 bool pmVisualInitWindow (int *kapid, char *name) {
+
     if (*kapid == -1) {
         *kapid = KapaOpenNamedSocket("kapa", name);
         if (*kapid == -1) {
-            fprintf (stderr, "Failure to open kapa.\n");
-            isVisual = false;
+            fprintf (stderr, "Failure to open kapa; visual mode disabled.\n");
+	    pmVisualSetVisual(false);
             return false;
         }
@@ -255,4 +256,10 @@
     }
 
+    if (increasing) {
+	fprintf (stderr, "plotting points scaled from %f to %f\n", zmin, zmax);
+    } else {
+	fprintf (stderr, "plotting points scaled from %f to %f\n", zmax, zmin);
+    }
+
     float range = zmax - zmin;
     if (range == 0.0) {
Index: /branches/eam_branches/ipp-20110213/psModules/src/extras/pmVisualUtils.c
===================================================================
--- /branches/eam_branches/ipp-20110213/psModules/src/extras/pmVisualUtils.c	(revision 31018)
+++ /branches/eam_branches/ipp-20110213/psModules/src/extras/pmVisualUtils.c	(revision 31019)
@@ -16,4 +16,5 @@
 #include <pslib.h>
 
+#include <pmVisual.h>
 #include <pmVisualUtils.h>
 
@@ -89,4 +90,7 @@
     }
 
+    // turn on overall visualization
+    pmVisualSetVisual(true);
+
     // If the component name has no leading dot, then supply it.
     if (comp[0] != '.') {
@@ -145,6 +149,10 @@
     PS_ASSERT_STRING_NON_EMPTY(func, false);
 
-   // return true if level is set to be shown
+    // if visualization is turned off, just skip
+    if (!pmVisualIsVisual()) return false;
+
+    // return true if level is set to be shown
     FACILITY(facility, func, name);
+
     bool valid = level <= getLevel(facility);
 
Index: /branches/eam_branches/ipp-20110213/psModules/src/objects/pmSourceVisual.c
===================================================================
--- /branches/eam_branches/ipp-20110213/psModules/src/objects/pmSourceVisual.c	(revision 31018)
+++ /branches/eam_branches/ipp-20110213/psModules/src/objects/pmSourceVisual.c	(revision 31019)
@@ -56,13 +56,5 @@
 
     if (!pmVisualTestLevel("psphot.psf.metric", 2)) return true;
-
-    if (kapa1 == -1) {
-        kapa1 = KapaOpenNamedSocket ("kapa", "pmSource:plots");
-        if (kapa1 == -1) {
-            fprintf (stderr, "failure to open kapa; visual mode disabled\n");
-            pmVisualSetVisual(false);
-            return false;
-        }
-    }
+    if (!pmVisualInitWindow (&kapa1, "pmSource:plots")) return false;
 
     KapaClearSections (kapa1);
@@ -140,16 +132,9 @@
 
     if (!pmVisualTestLevel("psphot.psf.subpix", 3)) return true;
-
-    if (kapa1 == -1) {
-        kapa1 = KapaOpenNamedSocket ("kapa", "pmSource:plots");
-        if (kapa1 == -1) {
-            fprintf (stderr, "failure to open kapa; visual mode disabled\n");
-            pmVisualSetVisual(false);
-            return false;
-        }
-    }
+    if (!pmVisualInitWindow (&kapa1, "pmSource:plots")) return false;
 
     KapaClearSections (kapa1);
     KapaInitGraph (&graphdata);
+    section.bg = KapaColorByName ("none"); // XXX probably should be 'none'
 
     int n;
@@ -302,13 +287,5 @@
 
     if (!pmVisualTestLevel("psphot.psf.fits", 2)) return true;
-
-    if (kapa2 == -1) {
-        kapa2 = KapaOpenNamedSocket ("kapa", "pmSource:images");
-        if (kapa2 == -1) {
-            fprintf (stderr, "failure to open kapa; visual mode disabled\n");
-            pmVisualSetVisual(false);
-            return false;
-        }
-    }
+    if (!pmVisualInitWindow (&kapa2, "pmSource:images")) return false;
 
     // create images 1/10 scale:
@@ -385,13 +362,5 @@
     if (!source->pixels) return false;
     if (!source->modelFlux) return false;
-
-    if (kapa2 == -1) {
-        kapa2 = KapaOpenNamedSocket ("kapa", "pmSource:images");
-        if (kapa2 == -1) {
-            fprintf (stderr, "failure to open kapa; visual mode disabled\n");
-            pmVisualSetVisual(false);
-            return false;
-        }
-    }
+    if (!pmVisualInitWindow (&kapa2, "pmSource:images")) return false;
 
     // KapaClearSections (kapa2);
@@ -428,16 +397,9 @@
     if (!plotPSF) return true;
     if (!pmVisualTestLevel("psphot.psf.resid", 2)) return true;
-
-    if (kapa1 == -1) {
-        kapa1 = KapaOpenNamedSocket ("kapa", "pmSource:plots");
-        if (kapa1 == -1) {
-            fprintf (stderr, "failure to open kapa; visual mode disabled\n");
-            pmVisualSetVisual(false);
-            return false;
-        }
-    }
+    if (!pmVisualInitWindow (&kapa1, "pmSource:plots")) return false;
 
     KapaClearPlots (kapa1);
     KapaInitGraph (&graphdata);
+    section.bg = KapaColorByName ("none"); // XXX probably should be 'none'
 
     float Xmin = +1e32;
