Index: /branches/eam_branches/ipp-20130509/Ohana/src/libkapa/src/IOfuncs.c
===================================================================
--- /branches/eam_branches/ipp-20130509/Ohana/src/libkapa/src/IOfuncs.c	(revision 35603)
+++ /branches/eam_branches/ipp-20130509/Ohana/src/libkapa/src/IOfuncs.c	(revision 35604)
@@ -30,4 +30,6 @@
   sscanf (buffer, "%*s %d", &Nbytes);
   ALLOCATE (data, char, Nbytes + 1);
+  memset (data, 0, Nbytes + 1);
+
   status = read (device, data, Nbytes);
   if (status != Nbytes) {
@@ -78,9 +80,10 @@
       // XXX BUT: I need to think a bit more about hand-shaking.
   }
-  if (DEBUG) fprintf (stderr, "recv buffer: %s\n", buffer);
+  if (DEBUG) fprintf (stderr, "recv buffer: %s...\n", buffer);
 
   /* find the message length, allocate space */
   sscanf (buffer, "%*s %d", &Nbytes);
   ALLOCATE (message, char, Nbytes + 1);
+  memset (message, 0, Nbytes + 1);
 
   /* read Nbytes from the device */
@@ -92,5 +95,5 @@
   /* make the string easy to parse */
 
-  if (DEBUG) fprintf (stderr, "recv: %s\n", message);
+  if (DEBUG) fprintf (stderr, "recv: %s...\n", message);
 
   /* scan the incoming message */
@@ -131,5 +134,5 @@
   if (Nwrite != length) return (FALSE);
 
-  if (DEBUG) fprintf (stderr, "send: %s\n", string);
+  if (DEBUG) fprintf (stderr, "send: %s...\n", string);
 
   free (string);
@@ -145,8 +148,9 @@
 
   ALLOCATE (message, char, length + 1);
+  memset (message, 0, length + 1);
 
   /* read Nbytes from the device */
   status = read (device, message, length);
-  if (DEBUG) fprintf (stderr, "recv message: %s\n", message);
+  if (DEBUG) fprintf (stderr, "recv message: %s...\n", message);
 
   if (status != length) {
@@ -173,4 +177,5 @@
   Nbytes = strlen (expect);
   ALLOCATE (answer, char, Nbytes + 1);
+  memset (answer, 0, Nbytes + 1);
 
   KiiScanCommand (device, Nbytes, "%s", answer);
Index: /branches/eam_branches/ipp-20130509/Ohana/src/relphot/include/relphot.h
===================================================================
--- /branches/eam_branches/ipp-20130509/Ohana/src/relphot/include/relphot.h	(revision 35603)
+++ /branches/eam_branches/ipp-20130509/Ohana/src/relphot/include/relphot.h	(revision 35604)
@@ -328,4 +328,9 @@
 void          plot_star_coords    PROTO((Catalog *catalog, int Ncatalog));
 void          plot_stars          PROTO((Catalog *catalog, int Ncatalog));
+
+void          plot_list_add       PROTO((Graphdata *graphdata, double *xlist, double *ylist, int N));
+int           get_graph           PROTO((int N));
+int           open_graph          PROTO((int N));
+
 void          reload_catalogs     PROTO((SkyList *skylist, FlatCorrectionTable *flatcorr, int hostID, char *hostpath));
 int           reload_catalogs_parallel PROTO((SkyList *sky));
Index: /branches/eam_branches/ipp-20130509/Ohana/src/relphot/src/ImageOps.c
===================================================================
--- /branches/eam_branches/ipp-20130509/Ohana/src/relphot/src/ImageOps.c	(revision 35603)
+++ /branches/eam_branches/ipp-20130509/Ohana/src/relphot/src/ImageOps.c	(revision 35604)
@@ -595,5 +595,8 @@
     image[i].Xm    = 100.0*log10(stats.chisq);
 
-    plot_setMcal (list, N, &stats, CLOUD_TOLERANCE);
+    if (PLOTSTUFF) {
+      fprintf (stderr, "Mcal for : %s\n", image[i].name);
+      plot_setMcal (list, N, &stats, CLOUD_TOLERANCE);
+    }
 
     // bright end scatter
@@ -683,4 +686,5 @@
 }
 
+static int setMcal_init_done = FALSE;
 void plot_setMcal (double *list, int Npts, StatType *stats, float clouds) {
 
@@ -692,28 +696,35 @@
 
   if (Npts == 0) {
-    fprintf (stderr, "cannot set image Mcal values yet (nan Mave)\n");
+    // fprintf (stderr, "cannot set image Mcal values yet (nan Mave)\n");
     return;
   }
 
+  plot_defaults (&graphdata);
+  graphdata.xmin = -0.01;
+  graphdata.xmax = +1.01;
+  graphdata.ymin = -0.21;
+  graphdata.ymax = +0.21;
+
+  if (!setMcal_init_done) {
+    int kapa = get_graph(0);
+    if (kapa < 1) {
+      kapa = open_graph(0);
+      if (!kapa) return;
+    }
+
+    KapaClearSections (kapa);
+    KapaClearPlots (kapa);
+    KapaSetLimits (kapa, &graphdata);
+    KapaSetFont (kapa, "helvetica", 14);
+    KapaBox (kapa, &graphdata);
+    setMcal_init_done = TRUE;
+  }
+
   ALLOCATE (xlist, double, Npts);
-
-  /**** Mcal vs seq ****/
-  float minMcal    = +100.0;
-  float maxMcal    = -100.0;
   for (i = 0; i < Npts; i++) {
-    xlist[i] = i;
-    minMcal = MIN (list[i], minMcal);
-    maxMcal = MAX (list[i], maxMcal);
-  }
-
-  float McalRange  = MAX(1.2*(maxMcal - minMcal), 0.21);
-  float McalCenter = 0.5*(maxMcal + minMcal);
-
-  plot_defaults (&graphdata);
-  graphdata.xmin = -1;
-  graphdata.xmax = Npts + 1;
-  graphdata.ymin = McalCenter - 0.5*McalRange;
-  graphdata.ymax = McalCenter + 0.5*McalRange;
-  plot_list (&graphdata, xlist, list, Npts, "sequence vs Mcal", "%s.seq.png", OUTROOT);
+    xlist[i] = i / (float) Npts;
+  }
+
+  plot_list_add (&graphdata, xlist, list, Npts);
 
   free (xlist);
Index: /branches/eam_branches/ipp-20130509/Ohana/src/relphot/src/MosaicOps.c
===================================================================
--- /branches/eam_branches/ipp-20130509/Ohana/src/relphot/src/MosaicOps.c	(revision 35603)
+++ /branches/eam_branches/ipp-20130509/Ohana/src/relphot/src/MosaicOps.c	(revision 35604)
@@ -711,5 +711,7 @@
   if (FREEZE_MOSAICS) return (FALSE);
 
-  if (NTHREADS) {
+  // plots cannot be done in a threaded context (the plot commands collide)
+  // so do not run setMmos in threaded mode if PLOTSTUFF is set
+  if (NTHREADS && !PLOTSTUFF) {
     int status = setMmos_threaded (catalog, PoorImages, flatcorr);
     return status;
@@ -734,8 +736,16 @@
   SetMmosInfoInit (&info, Nmax, TRUE, PoorImages);
 
+  // int savePlotDelay = PLOTDELAY;
+  // if (PLOTSTUFF) PLOTDELAY = 0.0;
+
   for (i = 0; i < Nmosaic; i++) {
     setMmos_mosaic (&mosaic[i], i, image, catalog, &info, flatcorr);
   }
   SetMmosInfoFree (&info);
+
+  if (PLOTSTUFF) {
+    fprintf (stdout, "press return\n"); 
+    if (fscanf (stdin, "%*c") != 1) fprintf (stderr, "\n");
+  }
 
   npass_output ++;
@@ -757,5 +767,6 @@
 
   StatType stats;
-  liststats_setmode (&stats, STATMODE);
+  // liststats_setmode (&stats, STATMODE);
+  liststats_setmode (&stats, "INNER_WTMEAN");
 
   double *list   = info->list;
@@ -937,5 +948,8 @@
   }
 
-  plot_setMcal (list, N, &stats, CLOUD_TOLERANCE);
+  if (PLOTSTUFF) {
+    fprintf (stderr, "Mmos: %6.3f %6.3f +/- %6.3f %5d %5d | %s\n", stats.mean, stats.median, stats.sigma, stats.Nmeas, N, image[MosaicToImage[Nmos][0]].name);
+    plot_setMcal (list, N, &stats, CLOUD_TOLERANCE);
+  }
 
   // bright end scatter
@@ -961,4 +975,5 @@
     // fprintf (stderr, "%d %d %d\n", (int) i, (int) im, image[im].ubercalDist);
   }
+  
   return TRUE;
 }
Index: /branches/eam_branches/ipp-20130509/Ohana/src/relphot/src/plotstuff.c
===================================================================
--- /branches/eam_branches/ipp-20130509/Ohana/src/relphot/src/plotstuff.c	(revision 35603)
+++ /branches/eam_branches/ipp-20130509/Ohana/src/relphot/src/plotstuff.c	(revision 35604)
@@ -17,4 +17,8 @@
   fprintf (stderr, "kapa is dead, must restart\n");
   Xgraph[active] = -1;
+}
+
+int get_graph (int N) {
+  return Xgraph[N];
 }
 
@@ -156,4 +160,12 @@
 }
 
+// plot the vector pair to a file with name defined by the varargs format
+void plot_list_add (Graphdata *graphdata, double *xlist, double *ylist, int Npts) {
+
+  KapaPrepPlot (Xgraph[0], Npts, graphdata);
+  PlotVector (Npts, xlist, 0, 0, "x");
+  PlotVector (Npts, ylist, 1, 0, "y");
+}
+
 void plot_defaults (Graphdata *graphdata) {
 
