Index: /trunk/Ohana/src/opihi/dvo/fitcolors.c
===================================================================
--- /trunk/Ohana/src/opihi/dvo/fitcolors.c	(revision 7399)
+++ /trunk/Ohana/src/opihi/dvo/fitcolors.c	(revision 7400)
@@ -17,7 +17,11 @@
   char *RegionName, *RegionList;
   char *cmd, *outcmd, *camera;
+  char name[64], filename[64], plotname[64], label[64];
   double *M1, *M2;
-  float *out;
+  float *out, *colorFit, *deltaFit, color, dColor, C0, C1;
   float minDelta, maxDelta, minColor, maxColor;
+  int fd, Npx, Npy, NPX, NPY, Nplot, PLOT;
+  Graphdata graphdata;
+  KapaSection section;
 
   Catalog *catalog;
@@ -32,4 +36,6 @@
   codelist = NULL;
   xvec = yvec = NULL;
+  colorFit = NULL;
+  deltaFit = NULL;
 
   oldsignal = signal (SIGINT, handle_interrupt);
@@ -64,6 +70,39 @@
   }
 
+  PLOT = FALSE;
+  if ((N = get_argument (argc, argv, "-plot"))) {
+    remove_argument (N, &argc, argv);
+    strcpy (plotname, argv[N]);
+    remove_argument (N, &argc, argv);
+    NPX = atof(argv[N]);
+    remove_argument (N, &argc, argv);
+    NPY = atof(argv[N]);
+    remove_argument (N, &argc, argv);
+    PLOT = TRUE;
+  }
+
   /* interpret command-line options */
   if (argc != 4) goto usage;
+
+  if (PLOT) {
+    if (!GetGraph (&graphdata, &fd, NULL)) return (FALSE);
+    Nplot = 0;
+    Npx = Npy = 0;
+    graphdata.xmin = minColor;
+    graphdata.xmax = maxColor;
+    graphdata.ymin = minDelta;
+    graphdata.ymax = maxDelta;
+    graphdata.style = 2;
+    graphdata.ptype = 2;
+    KapaClear (fd, TRUE);
+    KapaSetFont (fd, "helvetica", 14);
+
+    ALLOCATE (colorFit, float, 11);
+    ALLOCATE (deltaFit, float, 11);
+    dColor = (maxColor - minColor) / 10.0;
+    for (i = 0; i < 11; i++) {
+      colorFit[i] = minColor + i*dColor;
+    }
+  }
 
   /* determine relevant photcodes, colors */
@@ -154,4 +193,18 @@
   if ((yvec = SelectVector ("tmp_y", ANYVECTOR, TRUE)) == NULL) goto escape;
 
+  // set up basic windows
+  if (PLOT) {
+    Nplot = 0;
+    Npx = Npy = 0;
+    NPX = NPY = 6;
+    KapaInitGraph (&graphdata);
+    graphdata.xmin = minColor;
+    graphdata.xmax = maxColor;
+    graphdata.ymin = minDelta;
+    graphdata.ymax = maxDelta;
+    graphdata.style = 2;
+    graphdata.ptype = 2;
+  }
+
   /*** generate the color-color vectors for the pairs ***/
   // XXXX this function also needs to check for interrupts
@@ -218,15 +271,98 @@
       if (outcmd != NULL) free (outcmd);
       
+      C0 = get_double_variable ("C0");
+      C1 = get_double_variable ("C1");
+      
       /* do something useful with the results (stored in Cn, C0, C1, etc) */
       fprintf (GetOutfile(), "%s - %s : ", code[0][0].name, code[1][0].name);
       fprintf (GetOutfile(), "%7.4f %7.4f   %7.4f   ", 
-	       get_double_variable ("C0"), get_double_variable ("C1"), get_double_variable ("dC"));
+	       C0, C1, get_double_variable ("dC"));
       fprintf (GetOutfile(), "%5s of %5d\n", get_variable ("Cnv"), Npts);
-      out[NP1 + NP2*NP] = get_double_variable ("C1");
+      out[NP1 + NP2*NP] = C1;
+
+      // make an illustrating plot of each chip vs each other chip
+      // each page should have, say, a 6x6 grid of plots. each one should show a single chip pair
+      // as the page fills up, it gets written and a new one created.  
+      if (PLOT) {
+	sprintf (name, "s%02d.%02d", Npx, Npy);
+	section.name = strcreate (name);
+	if (Npx || Npy) {
+	  section.dx = 0.9 / NPX;
+	  section.dy = 0.9 / NPY;
+	  section.x = 0.1 + Npx * section.dx;
+	  section.y = 0.1 + Npy * section.dy;
+	  strcpy (graphdata.labels, "0000");
+	} 
+	if (Npx == 0) {
+	  section.dx = 0.9 / NPX + 0.1;
+	  section.dy = 0.9 / NPY;
+	  section.x = 0.0;
+	  section.y = 0.1 + Npy * section.dy;
+	  strcpy (graphdata.labels, "0100");
+	}
+	if (Npy == 0) {
+	  section.dx = 0.9 / NPX;
+	  section.dy = 0.9 / NPY + 0.1;
+	  section.x = 0.1 + Npx * section.dx;
+	  section.y = 0.0;
+	  strcpy (graphdata.labels, "1000");
+	}
+	if (!Npx && !Npy) {
+	  section.dx = 0.9 / NPX + 0.1;
+	  section.dy = 0.9 / NPY + 0.1;
+	  section.x = 0.0;
+	  section.y = 0.0;
+	  strcpy (graphdata.labels, "1100");
+	}
+	KapaSetSection (fd, &section);
+	KapaSetLimits (fd, &graphdata);
+	KapaBox (fd, &graphdata);
+	KapaPrepPlot (fd, Npts, &graphdata);
+	KapaPlotVector (fd, Npts, xvec[0].elements);
+	KapaPlotVector (fd, Npts, yvec[0].elements);
+
+	for (i = 0; i < 11; i++) {
+	  deltaFit[i] = C0 + C1*colorFit[i];
+	}
+	graphdata.style = 0;
+	graphdata.color = KapaColorByName ("red");
+	KapaPrepPlot (fd, 11, &graphdata);
+	KapaPlotVector (fd, 11, colorFit);
+	KapaPlotVector (fd, 11, deltaFit);
+
+	KapaSetFont (fd, "helvetica", 8);
+	sprintf (label, "%s", code[0][0].name);
+	KapaSendTextline (fd, label, 0.2*maxColor + 0.8*minColor, 0.8*maxDelta + 0.2*minDelta, 0.0);
+	sprintf (label, "%s", code[1][0].name);
+	KapaSendTextline (fd, label, 0.2*maxColor + 0.8*minColor, 0.2*maxDelta + 0.8*minDelta, 0.0);
+	KapaSetFont (fd, "helvetica", 14);
+
+	graphdata.style = 2;
+	graphdata.color = KapaColorByName ("black");
+
+	free (section.name);
+
+	Npx++;
+	if (Npx == NPX) {
+	  Npx = 0;
+	  Npy ++;
+	  if (Npy == NPY) {
+	    Npy = 0;
+	    sprintf (filename, "%s.%02d.png", plotname, Nplot);
+	    KapaPNG (fd, filename);
+	    KapaClear (fd, TRUE);
+	    Nplot++;
+	  }
+	}
+      }	
     }
   }
-  SkyListFree (skylist, ((RegionName != NULL) || (RegionList != NULL)));
+  if (skylist != NULL) free_catalog (catalog, skylist[0].Nregions);
+  if (skylist != NULL) SkyListFree (skylist, ((RegionName != NULL) || (RegionList != NULL)));
   if (RegionName != NULL) free (RegionName);
   if (RegionList != NULL) free (RegionList);
+  if (codelist != NULL) free (codelist);
+  if (colorFit != NULL) free (colorFit);
+  if (deltaFit != NULL) free (deltaFit);
   signal (SIGINT, oldsignal);
   return (TRUE);
@@ -250,4 +386,6 @@
   if (RegionList != NULL) free (RegionList);
   if (codelist != NULL) free (codelist);
+  if (colorFit != NULL) free (colorFit);
+  if (deltaFit != NULL) free (deltaFit);
   DeleteVector (xvec);
   DeleteVector (yvec);
