Index: /branches/cnb_branch_20081011/psModules/src/astrom/pmAstrometryVisual.c
===================================================================
--- /branches/cnb_branch_20081011/psModules/src/astrom/pmAstrometryVisual.c	(revision 20336)
+++ /branches/cnb_branch_20081011/psModules/src/astrom/pmAstrometryVisual.c	(revision 20336)
@@ -0,0 +1,208 @@
+/****************************************************/
+/* Diagnostic plots for pmAstrometry routines.      */
+/****************************************************/
+
+#ifdef HAVE_CONFIG_H
+#include <config.h>
+#endif
+
+/********************************************************************/
+/* INCLUDE FILES                                                    */
+/********************************************************************/
+#include <stdio.h>
+#include <strings.h>
+#include <string.h>
+#include <math.h>
+#include <assert.h>
+#include <pslib.h>
+
+#include "pmKapaPlots.h"
+
+#include "pmHDU.h"
+#include "pmFPA.h"
+#include "pmAstrometryObjects.h"
+
+# if (HAVE_KAPA)
+# include <kapa.h>
+
+# define KAPAX 800
+# define KAPAY 800
+
+//variables to determine when things are plotted
+static bool isVisual             = false;
+static bool plotGridMatch        = false;
+
+
+// variables to store plotting window indices
+static int kapa = -1;
+
+
+/******************************/
+/* Initialization Routines    */
+/******************************/
+
+
+/** start or stop plotting */
+bool pmAstromSetVisual (bool mode) {
+    isVisual = mode;
+    return true;
+}
+
+
+/** open, name, and resize a window if necessary */
+bool pmAstromVisualInitWindow (int *kapid, char *name) {
+    if (*kapid == -1) {
+        *kapid = KapaOpenNamedSocket("kapa", name);
+        if (*kapid == -1) {
+            fprintf (stderr, "failure to open kapa; visual mode disabled for pmAstrom\n");
+            isVisual = false;
+            return false;
+        }
+        KapaResize (*kapid, KAPAX, KAPAY);
+    }
+    return true;
+}
+
+
+/** ask the user how to proceed */
+bool pmAstromVisualAskUser( bool *plotflag ) {
+    char key[10];
+    fprintf (stdout, "[c]ontinue? [s]kip the rest of these plots? [a]bort all visual plots?");
+    if (!fgets(key, 8, stdin)) {
+        psWarning("Unable to read option");
+    }
+    if (key[0] == 's') {
+        *plotflag = false;
+    }
+    if (key[0] == 'a') {
+        pmAstromSetVisual(false);
+    }
+    return true;
+}
+
+
+/** destroy windows at the end of a run*/
+bool pmAstromVisualClose() {
+    if(kapa != -1)
+        KiiClose(kapa);
+    return true;
+}
+
+// // TEMPORARY - cant get psAstrometryObjects.h to link??
+// typedef struct
+// {
+//     psPlane *pix;   ///< the position in the pmReadout frame
+//     psPlane *cell;   ///< the position in the pmCell frame
+//     psPlane *chip;   ///< the position in the pmChip frame
+//     psPlane *FP;   ///< the position in the pmFPA frame
+//     psPlane *TP;   ///< the position in the tangent plane
+//     psSphere *sky;        ///< the position on the Celestial Sphere.
+//     double Mag;                         ///< object magnitude XXX what filter?
+//     double dMag;                        ///< error on object magnitude
+// }
+// pmAstromObj;
+
+/** plotting routines */
+
+/*
+ * Plot the offset between every pair of reference and raw source locations. The peak of this
+ * distribution nominally gives the offset, scale difference, and rotation of the two catalogs.
+ * Overplots the location of this peak as determined by pmAstromGridAngle.
+ */
+bool pmAstromVisualPlotGridMatch (const psArray *raw, const psArray *ref,
+                                  psImage *gridNP, float offsetX, float offsetY,
+                                  double maxOffpix, double Scale, double Offset) {
+
+    if (!isVisual || !plotGridMatch) return true;
+    if (!pmAstromVisualInitWindow(&kapa, "pmAstrom:plots"))
+        return false;
+
+    // vectors to hold dX and dY
+    int nplot = raw->n * ref->n;
+    float dXplot[nplot];
+    float dYplot[nplot];
+
+    pmAstromObj *ob1;
+    pmAstromObj *ob2;
+    float dX, dY;
+    for (int i = 0; i < raw->n; i++) {
+        ob1 = (pmAstromObj *)raw->data[i];
+        for (int j = 0; j < ref->n; j++) {
+            ob2 = (pmAstromObj *)raw->data[j];
+            dX = ob1->FP->x - ob2->FP->x;
+            dY = ob1->FP->y - ob2->FP->y;
+            dXplot[(i * ref->n) + j] = dX;
+            dYplot[(i * ref->n) + j] = dY;
+        }
+    }
+
+#if 0
+    // vectors to hold the 1D histogram along horizontal and vertical slices
+    // passing through the peak. Note: Xbin(x pos) = (x pos) / Scale + Offset
+    psU32 **NP = gridNP->data.U32;
+    int vertHistSlice[gridNP->numRows];
+    int horizHistSlice[gridNP->numCols];
+    int peakXbin = (offsetX - Offset) * Scale;
+    int peakYbin = (offsetY - Offset) * Scale;
+    for (int i = 0; i < gridNP->numRows; i++) {}
+# endif
+
+    // plot information
+    KapaSection section = {"s1", 0.00, 0.00, 1.0, 1.0};
+    Graphdata graphdata;
+    KapaClearPlots(kapa);
+    KapaInitGraph(&graphdata);
+    KapaSetSection(kapa, &section);
+    graphdata.xmin = offsetX - 1.5 * maxOffpix;
+    graphdata.xmax = offsetX + 1.5 * maxOffpix;
+    graphdata.ymin = offsetY - 1.5 * maxOffpix;
+    graphdata.ymax = offsetY + 1.5 * maxOffpix;
+
+    KapaSetLimits(kapa, &graphdata);
+    KapaSetFont(kapa, "helvetica", 14);
+    KapaBox(kapa, &graphdata);
+    KapaSendLabel (kapa, "X offset", KAPA_LABEL_XM);
+    KapaSendLabel (kapa, "Y offset", KAPA_LABEL_YM);
+    KapaSendLabel (kapa, "pmAstromGridAngle residuals. Big Box: Serach Region. Small Box: Correlation Peak.",
+                   KAPA_LABEL_XP);
+    graphdata.style = 2;
+    graphdata.ptype = 0;
+    graphdata.size = 0.4;
+    graphdata.color = KapaColorByName ("black");
+    KapaPrepPlot(kapa, nplot, &graphdata);
+    KapaPlotVector (kapa, nplot, dXplot, "x");
+    KapaPlotVector (kapa, nplot, dYplot, "y");
+
+    //Overplot bounding box, peak of distribution
+    float xbound[5] = { -maxOffpix, maxOffpix, maxOffpix, -maxOffpix, -maxOffpix};
+    float ybound[5] = { -maxOffpix, -maxOffpix, maxOffpix, maxOffpix, -maxOffpix};
+    float xbin[5] = {offsetX - 0.5 * Scale, offsetX + 0.5 * Scale,
+                     offsetX + 0.5 * Scale, offsetX - 0.5 * Scale,
+                     offsetX - 0.5 * Scale};
+    float ybin[5] = {offsetY - 0.5 * Scale, offsetY - 0.5 * Scale,
+                     offsetY + 0.5 * Scale, offsetY + 0.5 * Scale,
+                     offsetY - 0.5 * Scale};
+    graphdata.color = KapaColorByName("red");
+    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");
+
+    pmAstromVisualAskUser(&plotGridMatch);
+    return true;
+}
+
+
+# else
+
+bool pmAstromSetVisual(bool mode) { return true; }
+bool pmAstromVisualInitWindow (int *kapid, char *name) { return true; }
+bool pmAstromVisualAskUser (bool *plotflag) { return true; }
+bool pmAstromVisualClose() { return true; }
+bool pmAstromVisualPlotGridMatch (const psArray *raw, const psArray *ref, psImage *gridNP, float offsetX, float offsetY, double maxOffpix, double Scale, double Offset) { return true; }
+
+# endif
Index: /branches/cnb_branch_20081011/psModules/src/astrom/pmAstrometryVisual.h
===================================================================
--- /branches/cnb_branch_20081011/psModules/src/astrom/pmAstrometryVisual.h	(revision 20336)
+++ /branches/cnb_branch_20081011/psModules/src/astrom/pmAstrometryVisual.h	(revision 20336)
@@ -0,0 +1,5 @@
+bool pmAstromSetVisual(bool mode);
+bool pmAstromVisualInitWindow (int *kapid, char *name);
+bool pmAstromVisualAskUser (bool *plotflag);
+bool pmAstromVisualClose();
+bool pmAstromVisualPlotGridMatch (const psArray *raw, const psArray *ref, psImage *gridNP, float offsetX, float offsetY, double maxOffpix, double Scale, double Offset);
