Index: trunk/psModules/src/astrom/pmAstrometryVisual.c
===================================================================
--- trunk/psModules/src/astrom/pmAstrometryVisual.c	(revision 23241)
+++ trunk/psModules/src/astrom/pmAstrometryVisual.c	(revision 23242)
@@ -23,5 +23,4 @@
 
 //variables to determine when things are plotted
-static bool isVisual             = false;
 static bool plotGridMatch        = true;
 static bool plotTweak            = true;
@@ -48,10 +47,4 @@
 /* Initialization Routines  */
 
-bool pmAstromSetVisual (bool mode) {
-    isVisual = mode;
-    return true;
-}
-
-
 bool pmAstromVisualClose()
 {
@@ -68,9 +61,8 @@
 {
     // make sure we want to plot this
-    if (!plotRawStars || !isVisual) return true;
+    if (!plotRawStars || !pmVisualIsVisual()) return true;
 
     //set up plot region
     if (!pmVisualInitWindow (&kapa, "psastro:plots")){
-        isVisual = false;
         return false;
     }
@@ -222,5 +214,5 @@
 
     // pause and wait for user input:
-    pmVisualAskUser(&plotRawStars, &isVisual);
+    pmVisualAskUser(&plotRawStars);
 
     psFree (xVec);
@@ -234,9 +226,8 @@
 {
     //make sure we want to plot this
-    if (!isVisual || !plotRefStars) return true;
+    if (!pmVisualIsVisual() || !plotRefStars) return true;
 
     //set up plotting variables
     if (!pmVisualInitWindow (&kapa, "psastro:plots")) {
-        isVisual = false;
         return false;
     }
@@ -284,5 +275,5 @@
 
     // pause and wait for user input:
-    pmVisualAskUser(&plotRefStars, &isVisual);
+    pmVisualAskUser(&plotRefStars);
 
     psFree (xVec);
@@ -301,9 +292,8 @@
 
     // make sure we want to plot this
-    if ( !isVisual || !plotLumFunc ) return true;
+    if ( !pmVisualIsVisual() || !plotLumFunc ) return true;
 
     //set up plotting variables
     if ( !pmVisualInitWindow (&kapa, "psastro:plots")){
-        isVisual = false;
         return false;
     }
@@ -377,5 +367,5 @@
 
         // pause and wait for user input:
-        pmVisualAskUser (&plotLumFunc, &isVisual);
+        pmVisualAskUser (&plotLumFunc);
     }
     return true;
@@ -391,9 +381,8 @@
 
     //make sure we want to plot this
-    if (!isVisual || !plotRemoveClumps) return true;
+    if (!pmVisualIsVisual() || !plotRemoveClumps) return true;
 
     //set up plot variables
     if ( !pmVisualInitWindow (&kapa, "psastro:plots")) {
-        isVisual = false;
         return false;
     }
@@ -480,5 +469,5 @@
 
     //ask for user input and finish
-    pmVisualAskUser (&plotRemoveClumps, &isVisual);
+    pmVisualAskUser (&plotRemoveClumps);
     psFree (xVec);
     psFree (yVec);
@@ -496,9 +485,8 @@
 
     //make sure we want to plot this
-    if (!isVisual || !plotOneChipFit)
+    if (!pmVisualIsVisual() || !plotOneChipFit)
         return true;
 
     if(!pmVisualInitWindow(&kapa, "psastro:plots") || !pmVisualInitWindow(&kapa2, "psastro:plots")) {
-        isVisual = false;
         return false;
     }
@@ -511,5 +499,5 @@
 
     //ask for user input and finish
-    pmVisualAskUser(&plotOneChipFit, &isVisual);
+    pmVisualAskUser(&plotOneChipFit);
     return true;
 }
@@ -522,8 +510,7 @@
 {
     //make sure we want to plot this
-    if(!isVisual || !plotFixChips) return true;
+    if(!pmVisualIsVisual() || !plotFixChips) return true;
 
     if(!pmVisualInitWindow(&kapa, "psastro:plots")) {
-        isVisual = false;
         return false;
     }
@@ -588,5 +575,5 @@
     KapaPlotVector (kapa, xNew->n, yNew->data.F32, "y");
 
-    pmVisualAskUser(&plotFixChips, &isVisual);
+    pmVisualAskUser(&plotFixChips);
     psFree(xNew);
     psFree(yNew);
@@ -606,9 +593,8 @@
 
     //make sure we want to plot this
-    if (!isVisual || !plotAstromGuessCheck) return true;
+    if (!pmVisualIsVisual() || !plotAstromGuessCheck) return true;
 
     //set up graph window
     if ( !pmVisualInitWindow (&kapa, "psastro:plots")) {
-        isVisual = false;
         return false;
     }
@@ -686,5 +672,5 @@
     psFree(xResid);
     psFree(yResid);
-    pmVisualAskUser (&plotAstromGuessCheck, &isVisual);
+    pmVisualAskUser (&plotAstromGuessCheck);
     return true;
 }
@@ -696,8 +682,7 @@
 {
     //make sure we want to plot this
-    if (!isVisual || !plotCommonScale) return true;
+    if (!pmVisualIsVisual() || !plotCommonScale) return true;
 
     if (!pmVisualInitWindow(&kapa, "psastro:plots")){
-        isVisual = false;
         return false;
     }
@@ -736,5 +721,5 @@
     KapaSendLabel (kapa, "Old Pixel Scale of FPA Chips (Not to Scale)", KAPA_LABEL_XP);
 
-    pmVisualAskUser (&plotCommonScale, &isVisual);
+    pmVisualAskUser (&plotCommonScale);
 
     psFree(xVec);
@@ -750,8 +735,7 @@
 
     //make sure we want to plot this
-    if (!isVisual || !plotMosaicOneChip) return true;
+    if (!pmVisualIsVisual() || !plotMosaicOneChip) return true;
 
     if(!pmVisualInitWindow(&kapa, "psastro:plots") || !pmVisualInitWindow(&kapa2, "psastro:plots")) {
-        isVisual = false;
         return false;
     }
@@ -759,10 +743,10 @@
     //plot the residuals
     if (!residPlot(rawstars, refstars, match, recipe, "Single Chip Fit Residuals - Mosaic Mode")) {
-        isVisual = false;
+        pmVisualSetVisual(false);
         return false;
     }
 
     //ask for user input and finish
-    pmVisualAskUser(&plotMosaicOneChip, &isVisual);
+    pmVisualAskUser(&plotMosaicOneChip);
 
     return true;
@@ -775,5 +759,5 @@
 {
     //make sure we want to plot this
-    if (!isVisual || !plotMosaicMatches) return true;
+    if (!pmVisualIsVisual() || !plotMosaicMatches) return true;
 
     char title[60];
@@ -782,15 +766,14 @@
 
     if(!pmVisualInitWindow(&kapa, "psastro:plots") || !pmVisualInitWindow(&kapa2, "psastro:plots")) {
-        isVisual = false;
         return false;
     }
 
     if (!residPlot(rawstars, refstars, match, recipe, title)){
-        isVisual = false;
+        pmVisualSetVisual(false);
         return false;
     }
 
     //ask for user input
-    pmVisualAskUser (&plotMosaicMatches, &isVisual);
+    pmVisualAskUser (&plotMosaicMatches);
     return true;
 }
@@ -807,7 +790,6 @@
 {
     //make sure we want to plot this
-    if (!isVisual || !plotGridMatch) return true;
+    if (!pmVisualIsVisual() || !plotGridMatch) return true;
     if (!pmVisualInitWindow(&kapa, "psastro:plots")){
-        isVisual = false;
         return false;
     }
@@ -952,5 +934,5 @@
     KapaPlotVector (kapa, 2, yslice, "y");
 
-    pmVisualAskUser(&plotGridMatch, &isVisual);
+    pmVisualAskUser(&plotGridMatch);
     psFree(dXplot);
     psFree(dYplot);
@@ -966,7 +948,6 @@
 {
     //make sure we want to plot this
-    if (!isVisual || !plotTweak) return true;
+    if (!pmVisualIsVisual() || !plotTweak) return true;
     if (!pmVisualInitWindow(&kapa, "psastro:plots")) {
-        isVisual = false;
         return false;
     }
@@ -1051,5 +1032,5 @@
                    KAPA_LABEL_XP);
 
-    pmVisualAskUser(&plotTweak, &isVisual);
+    pmVisualAskUser(&plotTweak);
 
     psFree(xIndices);
@@ -1341,5 +1322,4 @@
 # else
 
-bool pmAstromSetVisual(bool mode) { return true; }
 bool pmAstromVisualClose() { return true; }
 bool pmAstromVisualPlotGridMatch (const psArray *raw, const psArray *ref, psImage *gridNP, double offsetX, double offsetY, double maxOffpix, double Scale, double Offset) { return true; }
Index: trunk/psModules/src/astrom/pmAstrometryVisual.h
===================================================================
--- trunk/psModules/src/astrom/pmAstrometryVisual.h	(revision 23241)
+++ trunk/psModules/src/astrom/pmAstrometryVisual.h	(revision 23242)
@@ -22,10 +22,4 @@
     int sPeak;                          ///< sum of stars to peak bin
 } pmLumFunc;
-
-
-/** Enable or disable visual plotting for psastro routines
- * @param mode true/false to enable/disable plotting
- * @return true for success */
-bool pmAstromSetVisual(bool mode);
 
 
Index: trunk/psModules/src/extras/pmVisual.c
===================================================================
--- trunk/psModules/src/extras/pmVisual.c	(revision 23241)
+++ trunk/psModules/src/extras/pmVisual.c	(revision 23242)
@@ -18,5 +18,8 @@
 #include "pmHDU.h"
 #include "pmFPA.h"
+#include "pmFPAfile.h"
 #include "pmAstrometryObjects.h"
+#include "pmSubtractionStamps.h"
+#include "pmTrend2D.h"
 #include "pmFPAExtent.h"
 
@@ -28,4 +31,31 @@
 # define KAPAY 700
 
+#include "pmAstrometryVisual.h"
+#include "pmSubtractionVisual.h"
+#include "pmStackVisual.h"
+#include "pmSourceVisual.h"
+
+//#define TESTING
+
+static bool isVisual = false;
+
+
+bool pmVisualSetVisual(bool value) {
+    isVisual = value;
+    return true;
+}
+
+
+bool pmVisualIsVisual(void) {return isVisual;}
+
+bool pmVisualClose(void) {
+    pmAstromVisualClose();
+    pmSubtractionVisualClose();
+    pmStackVisualClose();
+    pmSourceVisualClose();
+    //XXX handle psphot
+    //    psphotVisualClose();
+    return true;
+}
 
 bool pmVisualInitWindow (int *kapid, char *name) {
@@ -34,4 +64,5 @@
         if (*kapid == -1) {
             fprintf (stderr, "Failure to open kapa.\n");
+            isVisual = false;
             return false;
         }
@@ -52,5 +83,5 @@
 
 
-bool pmVisualAskUser(bool *plotFlag, bool *packageFlag)
+bool pmVisualAskUser(bool *plotFlag)
 {
     char key[10];
@@ -63,5 +94,5 @@
     }
     if (key[0] == 'a') {
-        *packageFlag = false;
+        isVisual = false;
     }
     return true;
@@ -182,10 +213,14 @@
 
 bool pmVisualScaleImage(int kapaFD, psImage *inImage, const char *name, int channel, bool clip) {
-
     KiiImage image;
     KapaImageData data;
     Coords coords;
 
-    //make sure we have a compatible image type
+    //make sure we have a compatible image
+    if (inImage == NULL) {
+        fprintf(stderr, "Image is NULL, and cannot be displayed\n");
+        return false;
+    }
+
     if(inImage->type.type != PS_TYPE_F32) {
         fprintf(stderr, "Cannot display this image (imcompatible data type)\n");
@@ -194,4 +229,5 @@
 
     strcpy (coords.ctype, "RA---TAN");
+
 
     double min, max, stdev, mean;
@@ -304,9 +340,11 @@
 
 #else
-
+bool pmVisualSetVisual(bool value) {return true;}
+bool pmVisualIsVisual(void) {return false;}
+bool pmVisualClose(void) {return true;}
 bool pmVisualInitWindow(int *kapid, char *name){return true;}
 bool pmVisualInitGraph (int kapa, void *section, void *graphdata){return true;}
 
-bool pmVisualAskUser(bool *plotFlag, bool *packageFlag){return true;}
+bool pmVisualAskUser(bool *plotFlag){return true;}
 bool pmVisualImStats(psImage *image, double *mean,
                      double *stdev, double *min, double *max){return true;}
Index: trunk/psModules/src/extras/pmVisual.h
===================================================================
--- trunk/psModules/src/extras/pmVisual.h	(revision 23241)
+++ trunk/psModules/src/extras/pmVisual.h	(revision 23242)
@@ -10,4 +10,22 @@
 
 #if (HAVE_KAPA)
+
+
+/** Globally enable or disable plotting
+ * @param value - true to enable plotting
+ * @return true
+ */
+bool pmVisualSetVisual(bool value);
+
+
+/** Check whether plotting is enabled
+ * @retrun true if plots should be generated
+ */
+bool pmVisualIsVisual(void);
+
+
+/** Destroy plotting windows at the end of a run
+ * @return true for success */
+bool pmVisualClose(void);
 
 
@@ -32,8 +50,6 @@
  * At the user's request, this will disable diagnostic plotting.
  * @param plotFlag, set to false if this plot should be disabled in the future
- * @param packageFlag, set to false if all plots from this package (e.g. psastro, pmSubtraction)
- *                      should be disabled.
  */
-bool pmVisualAskUser(bool *plotFlag, bool *packageFlag);
+bool pmVisualAskUser(bool *plotFlag);
 
 
@@ -115,7 +131,10 @@
 
 // kapa-specific data types are changed to void
+bool pmVisualSetVisual(bool value);
+bool pmVisualIsVisual(void);
+bool pmVisualClose(void);
 bool pmVisualInitWindow (int *kapid, char *name);
 bool pmVisualInitGraph (int kapa, void *section, void *graphdata);
-bool pmVisualAskUser(bool *plotFlag, bool *packageFlag);
+bool pmVisualAskUser(bool *plotFlag);
 bool pmVisualScaleImage(int kapaFD, psImage *inImage,
                         const char *name, int channel, bool clip);
Index: trunk/psModules/src/imcombine/Makefile.am
===================================================================
--- trunk/psModules/src/imcombine/Makefile.am	(revision 23241)
+++ trunk/psModules/src/imcombine/Makefile.am	(revision 23242)
@@ -19,5 +19,6 @@
 	pmSubtractionThreads.c	\
 	pmPSFEnvelope.c         \
-	pmSubtractionVisual.c
+	pmSubtractionVisual.c   \
+	pmStackVisual.c
 
 pkginclude_HEADERS = \
@@ -37,5 +38,6 @@
 	pmSubtractionThreads.h	\
 	pmPSFEnvelope.h         \
-	pmSubtractionVisual.h
+	pmSubtractionVisual.h   \
+	pmStackVisual.h
 
 CLEANFILES = *~
Index: trunk/psModules/src/imcombine/pmPSFEnvelope.c
===================================================================
--- trunk/psModules/src/imcombine/pmPSFEnvelope.c	(revision 23241)
+++ trunk/psModules/src/imcombine/pmPSFEnvelope.c	(revision 23242)
@@ -30,5 +30,5 @@
 
 #include "pmPSFEnvelope.h"
-
+#include "pmStackVisual.h"
 
 
@@ -165,4 +165,5 @@
             psStringAppend(&name, "psf_field_%03d.fits", i);
             psFits *fits = psFitsOpen(name, "w");
+            pmStackVisualPlotTestImage(fakeRO->image, name);
             psFitsWriteImage(fits, NULL, fakeRO->image, 0, NULL);
             psFitsClose(fits);
@@ -182,4 +183,5 @@
         // Write out the envelope
         psFits *fits = psFitsOpen("psf_field_envelope.fits", "w");
+        pmStackVisualPlotTestImage(envelope, "psf_field_envelope.fits");
         psFitsWriteImage(fits, NULL, envelope, 0, NULL);
         psFitsClose(fits);
@@ -234,4 +236,5 @@
         // Write out the envelope
         psFits *fits = psFitsOpen("psf_field_full.fits", "w");
+        pmStackVisualPlotTestImage(readout->image, "psf_field_full.fits");
         psFitsWriteImage(fits, NULL, readout->image, 0, NULL);
         psFitsClose(fits);
@@ -315,4 +318,5 @@
         {
             psFits *fits = psFitsOpen("psf_field_model.fits", "w");
+            pmStackVisualPlotTestImage(generated->image, "psf_field_model.fits");
             psFitsWriteImage(fits, NULL, generated->image, 0, NULL);
             psFitsClose(fits);
@@ -321,4 +325,5 @@
         {
             psFits *fits = psFitsOpen("psf_field_resid.fits", "w");
+            pmStackVisualPlotTestImage(generated->image, "psf_field_resid.fits");
             psFitsWriteImage(fits, NULL, generated->image, 0, NULL);
             psFitsClose(fits);
Index: trunk/psModules/src/imcombine/pmStack.c
===================================================================
--- trunk/psModules/src/imcombine/pmStack.c	(revision 23241)
+++ trunk/psModules/src/imcombine/pmStack.c	(revision 23242)
@@ -8,6 +8,6 @@
  *  @author GLG, MHPCC
  *
- *  @version $Revision: 1.48 $ $Name: not supported by cvs2svn $
- *  @date $Date: 2009-02-13 23:52:14 $
+ *  @version $Revision: 1.48.2.1 $ $Name: not supported by cvs2svn $
+ *  @date $Date: 2009-02-19 17:59:50 $
  *  Copyright 2004-2007 Institute for Astronomy, University of Hawaii
  *
Index: trunk/psModules/src/imcombine/pmStackVisual.c
===================================================================
--- trunk/psModules/src/imcombine/pmStackVisual.c	(revision 23242)
+++ trunk/psModules/src/imcombine/pmStackVisual.c	(revision 23242)
@@ -0,0 +1,60 @@
+/** Diagnostic plots for pmStack
+ * @author Chris Beaumont, IfA
+ */
+
+/* Include Files   */
+#ifdef HAVE_CONFIG_H
+#include <config.h>
+#endif
+
+#include <stdio.h>
+#include <strings.h>
+#include <string.h>
+#include <math.h>
+#include <assert.h>
+#include <pslib.h>
+
+#include "pmKapaPlots.h"
+#include "pmVisual.h"
+
+#include "pmHDU.h"
+#include "pmFPA.h"
+#include "pmAstrometryObjects.h"
+
+# if (HAVE_KAPA)
+# include <kapa.h>
+
+//variables to determine when things are plotted
+static bool plotTestImage        = true;
+
+// variables to store plotting window indices
+static int kapa  = -1;
+static int kapa2 = -1;
+
+/** destroy windows at the end of a run*/
+bool pmStackVisualClose()
+{
+    if(kapa != -1)
+        KiiClose(kapa);
+    if(kapa2 != -1)
+        KiiClose(kapa2);
+    return true;
+}
+
+/** Display a test image
+ * @param image to plot
+ * @param name for plot. No spaces allowed.
+ */
+bool pmStackVisualPlotTestImage(psImage *image, char *name) {
+    if (!pmVisualIsVisual() || !plotTestImage) return true;
+    if (!pmVisualInitWindow(&kapa, "pmStack:Images")) return false;
+
+    if(!pmVisualScaleImage(kapa, image, (const char*)name, 0, true)) return false;
+    pmVisualAskUser(&plotTestImage);
+    return true;
+}
+
+#else
+bool pmStackVisualClose() {return true;}
+bool pmStackVisualPlotTestImage(psImage *image, char *name) {return true;}
+#endif
Index: trunk/psModules/src/imcombine/pmStackVisual.h
===================================================================
--- trunk/psModules/src/imcombine/pmStackVisual.h	(revision 23242)
+++ trunk/psModules/src/imcombine/pmStackVisual.h	(revision 23242)
@@ -0,0 +1,7 @@
+#ifndef PM_STACK_VISUAL_H
+#define PM_STACK_VISUAL_H
+
+bool pmStackVisualClose(void);
+bool pmStackVisualPlotTestImage(psImage *image, char *name);
+
+#endif
Index: trunk/psModules/src/imcombine/pmSubtractionIO.c
===================================================================
--- trunk/psModules/src/imcombine/pmSubtractionIO.c	(revision 23241)
+++ trunk/psModules/src/imcombine/pmSubtractionIO.c	(revision 23242)
@@ -16,4 +16,5 @@
 
 #include "pmSubtractionIO.h"
+#include "pmStackVisual.h"
 
 #define ARRAY_BUFFER 16                 // Number to add to array at a time
@@ -144,6 +145,6 @@
     // CVS tags, used to identify the version of this file (in case incompatibilities are introduced)
     psString cvsFile = psStringCopy("$RCSfile: pmSubtractionIO.c,v $");
-    psString cvsRev  = psStringCopy("$Revision: 1.9 $");
-    psString cvsDate = psStringCopy("$Date: 2008-10-24 21:21:06 $");
+    psString cvsRev  = psStringCopy("$Revision: 1.9.18.1 $");
+    psString cvsDate = psStringCopy("$Date: 2009-02-19 17:59:50 $");
     psStringSubstitute(&cvsFile, NULL, "RCSfile: ");
     psStringSubstitute(&cvsRev,  NULL, "Revision: ");
@@ -167,4 +168,6 @@
 
     psImage *image = psMetadataLookupPtr(NULL, ro->analysis, PM_SUBTRACTION_ANALYSIS_KERNEL_IMAGE); // Image
+    pmStackVisualPlotTestImage(image, "Subtraction_kernels.fits");
+
     if (image && !psFitsWriteImage(fits, header, image, 0, EXTNAME_IMAGE)) {
         psError(PS_ERR_IO, false, "Unable to write subtraction kernel image.");
Index: trunk/psModules/src/imcombine/pmSubtractionVisual.c
===================================================================
--- trunk/psModules/src/imcombine/pmSubtractionVisual.c	(revision 23241)
+++ trunk/psModules/src/imcombine/pmSubtractionVisual.c	(revision 23242)
@@ -28,5 +28,4 @@
 
 //variables to determine when things are plotted
-static bool isVisual             = false;
 static bool plotConvKernels      = true;
 static bool plotStamps           = true;
@@ -42,9 +41,4 @@
 // Initialization Routines
 
-/** start or stop plotting */
-bool pmSubtractionSetVisual (bool mode) {
-    isVisual = mode;
-    return true;
-}
 
 
@@ -65,11 +59,10 @@
  *    @return true for success */
 bool pmSubtractionVisualPlotConvKernels(psImage *convKernels) {
-    if (!isVisual || !plotConvKernels) return true;
+    if (!pmVisualIsVisual() || !plotConvKernels) return true;
     if (!pmVisualInitWindow(&kapa, "ppSub:Images")) {
-        isVisual = false;
         return false;
     }
     pmVisualScaleImage(kapa, convKernels, "Convolution_Kernels", 0, true);
-    pmVisualAskUser(&plotConvKernels, &isVisual);
+    pmVisualAskUser(&plotConvKernels);
     return true;
 }
@@ -80,11 +73,9 @@
     @return true for success */
 bool pmSubtractionVisualPlotStamps(pmSubtractionStampList *stamps, pmReadout *ro) {
-    if (!isVisual || !plotStamps) return true;
+    if (!pmVisualIsVisual() || !plotStamps) return true;
     if (!pmVisualInitWindow (&kapa, "ppSub:Images")) {
-        isVisual = false;
         return false;
     }
     if (!pmVisualInitWindow (&kapa2, "ppSub:StampMasterImage")) {
-        isVisual = false;
         return false;
     }
@@ -145,5 +136,5 @@
     pmVisualScaleImage(kapa, canvas, "Subtraction_Stamps", 0, true);
 
-    pmVisualAskUser(&plotStamps, &isVisual);
+    pmVisualAskUser(&plotStamps);
     return true;
 }
@@ -152,7 +143,6 @@
 bool pmSubtractionVisualPlotLeastSquares (pmSubtractionStampList *stamps, bool dual) {
 
-    if (!isVisual || !plotLeastSquares) return true;
+    if (!pmVisualIsVisual() || !plotLeastSquares) return true;
     if (!pmVisualInitWindow (&kapa, "PPSub:Images")) {
-        isVisual = false;
         return false;
     }
@@ -209,5 +199,5 @@
     pmVisualScaleImage(kapa, canvas32, "Least_Squares", 0, true);
 
-    pmVisualAskUser(&plotLeastSquares, &isVisual);
+    pmVisualAskUser(&plotLeastSquares);
     psFree(canvas);
     psFree(canvas32);
@@ -216,7 +206,6 @@
 
 bool pmSubtractionVisualShowSubtraction(psImage *image, psImage *ref, psImage *sub) {
-    if (!isVisual || !plotImage) return true;
+    if (!pmVisualIsVisual() || !plotImage) return true;
     if (!pmVisualInitWindow (&kapa, "PPSub:Images")) {
-        isVisual = false;
         return false;
     }
@@ -225,5 +214,5 @@
     pmVisualScaleImage(kapa, ref, "Reference", 1, true);
     pmVisualScaleImage(kapa, sub, "Subtraction", 2, true);
-    pmVisualAskUser(&plotImage, &isVisual);
+    pmVisualAskUser(&plotImage);
     return true;
 }
@@ -267,5 +256,4 @@
 
 #else
-bool pmSubtractionSetVisual (bool mode) {return true;}
 bool pmSubtractionVisualClose() {return true;}
 bool pmSubtractionVisualPlotConvKernels(psImage *convKernels) {return true;}
Index: trunk/psModules/src/imcombine/pmSubtractionVisual.h
===================================================================
--- trunk/psModules/src/imcombine/pmSubtractionVisual.h	(revision 23241)
+++ trunk/psModules/src/imcombine/pmSubtractionVisual.h	(revision 23242)
@@ -2,5 +2,4 @@
 #define PM_SUBTRACTION_VISUAL_H
 
-bool pmSubtractionSetVisual (bool mode);
 bool pmSubtractionVisualClose();
 bool pmSubtractionVisualPlotConvKernels(psImage *convKernels);
Index: trunk/psModules/src/objects/pmSourceVisual.c
===================================================================
--- trunk/psModules/src/objects/pmSourceVisual.c	(revision 23241)
+++ trunk/psModules/src/objects/pmSourceVisual.c	(revision 23242)
@@ -9,20 +9,22 @@
 #if (HAVE_KAPA)
 #include <kapa.h>
+#include "pmVisual.h"
 
 // functions used to visualize the analysis as it goes
 // these are invoked by the -visual options
 
-static bool isVisual = false;
 static int kapa1 = -1;
+static bool plotPSF = true;
 // static int kapa2 = -1;
 // static int kapa3 = -1;
 
+bool pmSourceVisualClose() {
+    if (kapa1 != -1)
+        KiiClose(kapa1);
+    return true;
+}
+
 bool pmSourcePlotPoints3D (int myKapa, Graphdata *graphdata, psVector *xn, psVector *yn, psVector *zn, float theta, float phi);
 
-bool pmSourceSetVisual (bool mode) {
-
-    isVisual = mode;
-    return true;
-}
 
 bool pmSourceVisualPSFModelResid (pmTrend2D *trend, psVector *x, psVector *y, psVector *param, psVector *mask) {
@@ -32,5 +34,5 @@
     Graphdata graphdata;
 
-    if (!isVisual) return true;
+    if (!pmVisualIsVisual() || !plotPSF) return true;
 
     if (kapa1 == -1) {
@@ -38,5 +40,5 @@
         if (kapa1 == -1) {
             fprintf (stderr, "failure to open kapa; visual mode disabled\n");
-            isVisual = false;
+            pmVisualSetVisual(false);
             return false;
         }
@@ -152,9 +154,6 @@
     // pause and wait for user input:
     // continue, save (provide name), ??
-    char key[10];
-    fprintf (stdout, "[c]ontinue? ");
-    if (!fgets(key, 8, stdin)) {
-        psWarning("Unable to read option");
-    }
+    pmVisualAskUser(&plotPSF);
+
     return true;
 }
Index: trunk/psModules/src/objects/pmSourceVisual.h
===================================================================
--- trunk/psModules/src/objects/pmSourceVisual.h	(revision 23241)
+++ trunk/psModules/src/objects/pmSourceVisual.h	(revision 23242)
@@ -1,9 +1,9 @@
 /* @file  pmKapaPlots.h
- * @brief functions to make plots with the external program 'kapa' 
+ * @brief functions to make plots with the external program 'kapa'
  *
  * @author EAM, IfA
  *
- * @version $Revision: 1.1 $ $Name: not supported by cvs2svn $
- * @date $Date: 2008-11-08 01:52:34 $
+ * @version $Revision: 1.1.16.1 $ $Name: not supported by cvs2svn $
+ * @date $Date: 2009-02-19 17:59:50 $
  * Copyright 2006 Institute for Astronomy, University of Hawaii
  */
@@ -12,8 +12,9 @@
 #define PM_SOURCE_VISUAL_H
 
+bool pmSourceVisualClose(void);
+
 /// @addtogroup Extras Miscellaneous Funtions
 /// @{
 
-bool pmSourceSetVisual (bool mode);
 bool pmSourceVisualPSFModelResid (pmTrend2D *trend, psVector *x, psVector *y, psVector *param, psVector *mask);
 
Index: trunk/psModules/src/psmodules.h
===================================================================
--- trunk/psModules/src/psmodules.h	(revision 23241)
+++ trunk/psModules/src/psmodules.h	(revision 23242)
@@ -101,4 +101,5 @@
 #include <pmReadoutCombine.h>
 #include <pmSubtractionVisual.h>
+#include <pmStackVisual.h>
 
 // the following headers are from psModule:objects
