Index: trunk/Ohana/src/opihi/cmd.astro/cgrid.c
===================================================================
--- trunk/Ohana/src/opihi/cmd.astro/cgrid.c	(revision 13344)
+++ trunk/Ohana/src/opihi/cmd.astro/cgrid.c	(revision 13479)
@@ -14,8 +14,8 @@
   double x, y;
   Vector Xvec, Yvec;
-  int NorthPole, SouthPole, N, OnPic, LOnPic, status, NELEMENTS, First;
+  int kapa, NorthPole, SouthPole, N, OnPic, LOnPic, status, NELEMENTS, First;
   Graphdata graphmode;
 
-  if (!style_args (&graphmode, &argc, argv, 0)) return FALSE;
+  if (!style_args (&graphmode, &argc, argv, &kapa)) return FALSE;
 
   if (argc != 1) {
@@ -23,5 +23,4 @@
     return (FALSE);
   }
-  SetGraph (graphmode);
 
   /* are we plotting one of the poles? */
@@ -392,7 +391,5 @@
   graphmode.ptype = 100; /* connect a pair */
   graphmode.etype = 0;
-  PrepPlotting (N, &graphmode);
-  PlotVector (N, Xvec.elements);
-  PlotVector (N, Yvec.elements);
+  PlotVectorPair (kapa, N, Xvec.elements, Yvec.elements, &graphmode);
 
   free (Xvec.elements);
Index: trunk/Ohana/src/opihi/cmd.astro/cplot.c
===================================================================
--- trunk/Ohana/src/opihi/cmd.astro/cplot.c	(revision 13344)
+++ trunk/Ohana/src/opihi/cmd.astro/cplot.c	(revision 13479)
@@ -3,10 +3,10 @@
 int cplot (int argc, char **argv) {
   
-  int i, Npts, status;
+  int i, kapa, Npts, status;
   float *x, *y, *r, *d, Rmin, Rmax;
   Vector Xvec, Yvec, *xvec, *yvec;
   Graphdata graphmode;
 
-  if (!style_args (&graphmode, &argc, argv, 0)) return FALSE;
+  if (!style_args (&graphmode, &argc, argv, &kapa)) return FALSE;
 
   if (argc != 3) {
@@ -14,5 +14,4 @@
     return (FALSE);
   }
-  SetGraph (graphmode);
 
   Rmin = graphmode.coords.crval1 - 182.0;
@@ -67,7 +66,5 @@
 
   graphmode.etype = 0;
-  PrepPlotting (Npts, &graphmode);
-  PlotVector (Npts, Xvec.elements);
-  PlotVector (Npts, Yvec.elements);
+  PlotVectorPair (kapa, Npts, Xvec.elements, Yvec.elements, &graphmode);
   
   free (Xvec.elements);
Index: trunk/Ohana/src/opihi/cmd.astro/czplot.c
===================================================================
--- trunk/Ohana/src/opihi/cmd.astro/czplot.c	(revision 13344)
+++ trunk/Ohana/src/opihi/cmd.astro/czplot.c	(revision 13479)
@@ -3,5 +3,5 @@
 int czplot (int argc, char **argv) {
   
-  int i, Npts;
+  int i, kapa, Npts;
   double min, range, Rmin, Rmax;
   float *in, *out, *r, *d, *x, *y;
@@ -9,5 +9,5 @@
   Graphdata graphmode;
 
-  if (!style_args (&graphmode, &argc, argv, 0)) return FALSE;
+  if (!style_args (&graphmode, &argc, argv, &kapa)) return FALSE;
 
   if (argc != 6) {
@@ -15,5 +15,4 @@
     return (FALSE);
   }
-  SetGraph (graphmode);
 
   min = atof(argv[4]);
@@ -59,9 +58,5 @@
   graphmode.etype = 0;
   Npts = Xvec.Nelements;
-  PrepPlotting (Npts, &graphmode);
-
-  PlotVector (Npts, Xvec.elements);
-  PlotVector (Npts, Yvec.elements);
-  PlotVector (Npts, Zvec.elements);
+  PlotVectorTriplet (kapa, Npts, Xvec.elements, Yvec.elements, Zvec.elements, &graphmode);
 
   free (Xvec.elements);
Index: trunk/Ohana/src/opihi/cmd.astro/gauss.c
===================================================================
--- trunk/Ohana/src/opihi/cmd.astro/gauss.c	(revision 13344)
+++ trunk/Ohana/src/opihi/cmd.astro/gauss.c	(revision 13479)
@@ -5,15 +5,18 @@
   char key[20];
   int i, N, Npix, Nborder, Nspot;
-  double X, Y, Z, max;
-  int Ximage, Nimage;
+  double X, Y, Z, ZP, RA, DEC, max;
+  int kapa;
+  char *name;
   Buffer *buf;
+  KapaImageData data;
 
-  Nimage = -1;
+  name = NULL;
   if ((N = get_argument (argc, argv, "-n"))) {
     remove_argument (N, &argc, argv);
-    Nimage = atof (argv[N]);
+    name = strcreate (argv[N]);
     remove_argument (N, &argc, argv);
   }
-  if (!GetImage (&Ximage, &Nimage)) return (FALSE);
+  if (!GetImageData (&data, &kapa, name)) return (FALSE);
+  FREE (name);
 
   Nborder = 3;
@@ -37,5 +40,5 @@
   }
   
-  if (Ximage < 1) {
+  if (kapa < 1) {
     gprint (GP_ERR, "no active TV\n");
     return (FALSE);
@@ -48,14 +51,14 @@
   }
 
-  if ((buf = SelectBuffer (GetImageName(), OLDBUFFER, TRUE)) == NULL) return (FALSE);
+  if ((buf = SelectBuffer (data.name, OLDBUFFER, TRUE)) == NULL) return (FALSE);
 
-  KiiCursorOn (Ximage);
+  KiiCursorOn (kapa);
 
   for (i = 0; (i < Nspot) || (Nspot == 0); i++) {
-    KiiCursorRead (Ximage, &X, &Y, key);
+    KiiCursorRead (kapa, &X, &Y, &ZP, &RA, &DEC, key);
     if (!strcasecmp (key, "Q")) break;
     Z = get_aperture_stats (&buf[0].matrix, (int)(X+0.5), (int)(Y+0.5), Npix, Nborder, max);
   }
-  KiiCursorOff (Ximage);
+  KiiCursorOff (kapa);
   return (TRUE);
 }
Index: trunk/Ohana/src/opihi/cmd.astro/objload.c
===================================================================
--- trunk/Ohana/src/opihi/cmd.astro/objload.c	(revision 13344)
+++ trunk/Ohana/src/opihi/cmd.astro/objload.c	(revision 13479)
@@ -7,15 +7,16 @@
   int i, N, Objtype, type, Nline, status;
   FILE *f;
-  char *buffer, *line;
-  int Ximage, Nimage, Noverlay, NOVERLAY;
+  char *buffer, *line, *name;
+  int kapa, Noverlay, NOVERLAY;
   KiiOverlay *overlay;
   
-  Nimage = -1;
+  name = NULL;
   if ((N = get_argument (argc, argv, "-n"))) {
     remove_argument (N, &argc, argv);
-    Nimage = atof (argv[N]);
+    name = strcreate (argv[N]);
     remove_argument (N, &argc, argv);
   }
-  if (!GetImage (&Ximage, &Nimage)) return (FALSE);
+  if (!GetImage (NULL, &kapa, name)) return (FALSE);
+  FREE (name);
 
   Objtype = 0;
@@ -63,5 +64,5 @@
   free (buffer);
 
-  KiiLoadOverlay (Ximage, overlay, Noverlay, argv[1]);
+  KiiLoadOverlay (kapa, overlay, Noverlay, argv[1]);
 
   free (overlay);
Index: trunk/Ohana/src/opihi/cmd.astro/outline.c
===================================================================
--- trunk/Ohana/src/opihi/cmd.astro/outline.c	(revision 13344)
+++ trunk/Ohana/src/opihi/cmd.astro/outline.c	(revision 13479)
@@ -145,5 +145,5 @@
     /* code to draw dots on Ximage */
     {
-      int Nimage, Ximage;
+      int kapa;
       float xp, yp, x, y;
       float dx, dy, theta, t, dt;
@@ -151,6 +151,5 @@
       KiiOverlay *overlay;
       
-      Nimage = -1;
-      if (!GetImage (&Ximage, &Nimage)) return (FALSE);
+      if (!GetImage (NULL, &kapa, NULL)) return (FALSE);
       
       Noverlay = 0;
@@ -179,5 +178,5 @@
 	CHECK_REALLOCATE (overlay, KiiOverlay, NOVERLAY, Noverlay, 1000);
       }
-      KiiLoadOverlay (Ximage, overlay, Noverlay, "red");
+      KiiLoadOverlay (kapa, overlay, Noverlay, "red");
       free (overlay);
     }
Index: trunk/Ohana/src/opihi/cmd.astro/outline2.c
===================================================================
--- trunk/Ohana/src/opihi/cmd.astro/outline2.c	(revision 13344)
+++ trunk/Ohana/src/opihi/cmd.astro/outline2.c	(revision 13479)
@@ -303,5 +303,5 @@
 int plot_outline () {
   
-  int Nimage, Ximage;
+  int kapa;
   float xp, yp, x, y;
   float dx, dy, theta, t, dt;
@@ -309,6 +309,5 @@
   KiiOverlay *overlay;
   
-  Nimage = -1;
-  if (!GetImage (&Ximage, &Nimage)) return (FALSE);
+  if (!GetImage (NULL, &kapa, NULL)) return (FALSE);
   
   Noverlay = 0;
@@ -337,5 +336,5 @@
     CHECK_REALLOCATE (overlay, KiiOverlay, NOVERLAY, Noverlay, 1000);
   }
-  KiiLoadOverlay (Ximage, overlay, Noverlay, "red");
+  KiiLoadOverlay (kapa, overlay, Noverlay, "red");
   free (overlay);
   return (TRUE);
Index: trunk/Ohana/src/opihi/cmd.astro/region.c
===================================================================
--- trunk/Ohana/src/opihi/cmd.astro/region.c	(revision 13344)
+++ trunk/Ohana/src/opihi/cmd.astro/region.c	(revision 13479)
@@ -5,10 +5,17 @@
   char string[256];
   double Ra, Dec, Radius;
-  double dx, dy;
-  int N, Ngraph, Xgraph;
+  float dx, dy;
+  int N, kapa;
+  char *name;
   Graphdata graphmode;
 
-  Ngraph = 0;
-  if (!GetGraph (&graphmode, &Xgraph, &Ngraph)) return (FALSE);
+  name = NULL;
+  if ((N = get_argument (argc, argv, "-n"))) {
+    remove_argument (N, &argc, argv);
+    name = strcreate (argv[N]);
+    remove_argument (N, &argc, argv);
+  }
+  if (!GetGraph (&graphmode, &kapa, name)) return (FALSE);
+  FREE (name);
 
   if ((N = get_argument (argc, argv, "-ew"))) {
@@ -60,6 +67,5 @@
   
   /* ask kapa for coordinate limits, so get the right aspect ratio */
-  KiiSendCommand (Xgraph, 4, "LIMS"); 
-  KiiScanMessage (Xgraph, "%lf %lf", &dx, &dy); 
+  KapaGetLimits (kapa, &dx, &dy);
   dx = fabs (dx);
   dy = fabs (dy); 
@@ -101,12 +107,13 @@
   graphmode.coords.cdelt1 = graphmode.coords.cdelt2 = 1.0;
 
-  KapaClear (Xgraph, TRUE);
-  KapaSetLimits (Xgraph, &graphmode);
+  KapaClearSections (kapa);
+  KapaSetLimits (kapa, &graphmode);
 
   /* drop this? */
   sprintf (string, "%8.4f %8.4f (%f)", Ra, Dec, Radius);
-  KapaSendLabel (Xgraph, string, 2);
+  KapaSendLabel (kapa, string, 2);
 
-  SetGraph (graphmode);
+  // XXX is this the right thing to be doing?
+  SetGraph (&graphmode);
   return (TRUE);
 }
Index: trunk/Ohana/src/opihi/cmd.data/box.c
===================================================================
--- trunk/Ohana/src/opihi/cmd.data/box.c	(revision 13344)
+++ trunk/Ohana/src/opihi/cmd.data/box.c	(revision 13479)
@@ -3,14 +3,16 @@
 int box (int argc, char **argv) {
   
-  int i, N, Ngraph, Xgraph;
+  int i, N, kapa;
+  char *name;
   Graphdata graphmode;
   
-  Ngraph = -1;
+  name = NULL;
   if ((N = get_argument (argc, argv, "-n"))) {
     remove_argument (N, &argc, argv);
-    Ngraph = atof (argv[N]);
+    name = strcreate (argv[N]);
     remove_argument (N, &argc, argv);
   }
-  if (!GetGraph (&graphmode, &Xgraph, &Ngraph)) return (FALSE);
+  if (!GetGraph (&graphmode, &kapa, name)) return (FALSE);
+  FREE (name);
 
   strcpy (graphmode.ticks, "2222");
@@ -49,5 +51,5 @@
   if (argc != 1) goto usage;
 
-  KapaBox (Xgraph, &graphmode);
+  KapaBox (kapa, &graphmode);
   return (TRUE);
       
Index: trunk/Ohana/src/opihi/cmd.data/center.c
===================================================================
--- trunk/Ohana/src/opihi/cmd.data/center.c	(revision 13344)
+++ trunk/Ohana/src/opihi/cmd.data/center.c	(revision 13479)
@@ -5,13 +5,15 @@
   double x, y;
   int zoom;
-  int Ximage, Nimage, N;
+  int kapa, N;
+  char *name;
   
-  Nimage = -1;
+  name = NULL;
   if ((N = get_argument (argc, argv, "-n"))) {
     remove_argument (N, &argc, argv);
-    Nimage = atof (argv[N]);
+    name = strcreate (argv[N]);
     remove_argument (N, &argc, argv);
   }
-  if (!GetImage (&Ximage, &Nimage)) return (FALSE);
+  if (!GetImage (NULL, &kapa, name)) return (FALSE);
+  FREE (name);
 
   if ((argc != 3) && (argc != 4)) {
@@ -25,5 +27,5 @@
   if (argc == 4) zoom = atof (argv[3]);
 
-  KiiCenter (Ximage, x, y, zoom);
+  KiiCenter (kapa, x, y, zoom);
   return (TRUE);
 }
Index: trunk/Ohana/src/opihi/cmd.data/clear.c
===================================================================
--- trunk/Ohana/src/opihi/cmd.data/clear.c	(revision 13344)
+++ trunk/Ohana/src/opihi/cmd.data/clear.c	(revision 13479)
@@ -1,29 +1,48 @@
 # include "data.h"
 
+// default is to clear all plots, but not the sections or the images
 int clear (int argc, char **argv) {
   
-  int N, ClearSection;
-  int Ngraph, Xgraph;
+  int N;
+  int kapa;
+  char *name;
 
-  Ngraph = -1;
+  name = NULL;
   if ((N = get_argument (argc, argv, "-n"))) {
     remove_argument (N, &argc, argv);
-    Ngraph = atof (argv[N]);
+    name = strcreate (argv[N]);
     remove_argument (N, &argc, argv);
   }
-  if (!GetGraph (NULL, &Xgraph, &Ngraph)) return (FALSE);
+  if (!GetGraph (NULL, &kapa, name)) return (FALSE);
+  FREE (name);
 
-  ClearSection = FALSE;
-  if ((N = get_argument (argc, argv, "-s"))) {
-    remove_argument (N, &argc, argv);
-    ClearSection = TRUE;
+  // clear all sections
+  if ((N = get_argument (argc, argv, "-s")) || 
+      (N = get_argument (argc, argv, "-section"))) {
+      KapaClearSections (kapa);
+      return (TRUE);
+  }
+
+  // clear all sections
+  if ((N = get_argument (argc, argv, "-graph"))) {
+      KapaClearCurrentPlot (kapa);
+      return (TRUE);
+  }
+
+  // clear image
+  if ((N = get_argument (argc, argv, "-image"))) {
+      KapaClearImage (kapa);
+      return (TRUE);
   }
 
   if (argc != 1) {
-    gprint (GP_ERR, "USAGE: clear [-n Xgraph]\n");
+    gprint (GP_ERR, "USAGE: clear [-n Xgraph] [-s|-section] [-image] [-graph]\n");
+    gprint (GP_ERR, "       [-s|-section] : clear all sections\n");
+    gprint (GP_ERR, "       [-graph]      : clear current graph\n");
+    gprint (GP_ERR, "       [-image]      : clear current image\n");
     return (FALSE);
   }
 
-  KapaClear (Xgraph, ClearSection);
+  KapaClearPlots (kapa);
   return (TRUE);
 }
Index: trunk/Ohana/src/opihi/cmd.data/close.c
===================================================================
--- trunk/Ohana/src/opihi/cmd.data/close.c	(revision 13344)
+++ trunk/Ohana/src/opihi/cmd.data/close.c	(revision 13479)
@@ -3,30 +3,19 @@
 int close_device (int argc, char **argv) {
 
-  int N, Source, Nsource, IsImage;
+  int N, kapa, IsImage;
+  char *name;
   /* close current graphics device */
 
-  Nsource = -1;
+  name = NULL;
   if ((N = get_argument (argc, argv, "-n"))) {
     remove_argument (N, &argc, argv);
-    Nsource = atof (argv[N]);
+    name = strcreate (argv[N]);
     remove_argument (N, &argc, argv);
   }
-  if ((N = get_argument (argc, argv, "-g"))) {
-    remove_argument (N, &argc, argv);
-    SetImageDevice (FALSE);
-  }  
-  if ((N = get_argument (argc, argv, "-i"))) {
-    remove_argument (N, &argc, argv);
-    SetImageDevice (TRUE);
-  }  
 
-  IsImage = GetCurrentDevice ();
-  if (IsImage) {
-    if (!GetImage (&Source, &Nsource)) return (FALSE);
-    close_image (Nsource); 
-  } else {
-    if (!GetGraph (NULL, &Source, &Nsource)) return (FALSE);
-    close_graph (Nsource); 
-  }
+  if (!GetGraph (NULL, &kapa, name)) return (FALSE);
+
+  close_kapa (name); 
+  FREE (name);
 
   return (TRUE);
Index: trunk/Ohana/src/opihi/cmd.data/cursor.c
===================================================================
--- trunk/Ohana/src/opihi/cmd.data/cursor.c	(revision 13344)
+++ trunk/Ohana/src/opihi/cmd.data/cursor.c	(revision 13479)
@@ -3,31 +3,23 @@
 int cursor (int argc, char **argv) {
 
-  char string[20], key[20];
-  int i, N, Nsource, Source, IsImage;
+  char string[20], key[20], *name;
+  int i, N, kapa;
   double X, Y, R, D, Z;
   void *oldsignal;
-  Graphdata graphmode;
-  Buffer *buf;
 
-  Nsource = -1;
+  // XXX need to be able to specify graph vs image coords
+  // currently, if only one exists, that frame will be used
+  // if both exist, defaults to ??
+  // if ((N = get_argument (argc, argv, "-g"))) {
+  // if ((N = get_argument (argc, argv, "-i"))) {
+
+  name = NULL;
   if ((N = get_argument (argc, argv, "-n"))) {
     remove_argument (N, &argc, argv);
-    Nsource = atof (argv[N]);
+    name = strcreate (argv[N]);
     remove_argument (N, &argc, argv);
   }
-  if ((N = get_argument (argc, argv, "-g"))) {
-    remove_argument (N, &argc, argv);
-    SetImageDevice (FALSE);
-  }  
-  if ((N = get_argument (argc, argv, "-i"))) {
-    remove_argument (N, &argc, argv);
-    SetImageDevice (TRUE);
-  }  
-  IsImage = GetCurrentDevice ();
-  if (IsImage) {
-    if (!GetImage (&Source, &Nsource)) return (FALSE);
-  } else {
-    if (!GetGraph (&graphmode, &Source, &Nsource)) return (FALSE);
-  }
+  if (!GetGraphData (NULL, &kapa, name)) return (FALSE);
+  FREE (name);
 
   N = 0;
@@ -39,14 +31,12 @@
   }
   
-  buf = NULL;
-  if (IsImage) buf = SelectBuffer (GetImageName(), OLDBUFFER, FALSE);
-  
-  KiiCursorOn (Source);
+  KiiCursorOn (kapa);
   
   oldsignal = signal (SIGINT, handle_interrupt);
   interrupt = FALSE;
-  Z = -1.0;
+
   for (i = 0; ((i < N) || (N == 0)) && !interrupt; i++) {
-    KiiCursorRead (Source, &X, &Y, key);
+
+    KiiCursorRead (kapa, &X, &Y, &Z, &R, &D, key);
 
     sprintf (string, "X%s", key);
@@ -54,24 +44,20 @@
     sprintf (string, "Y%s", key);
     set_variable (string, Y);
+    sprintf (string, "Z%s", key);
+    set_variable (string, Z);
+    sprintf (string, "R%s", key);
+    set_variable (string, R);
+    sprintf (string, "D%s", key);
+    set_variable (string, D);
+
     set_str_variable ("KEY", key);
     
-    if (IsImage) {
-      if (buf != NULL) { 
-	Z = gfits_get_matrix_value (&buf[0].matrix, (int) X, (int) Y);
-      }
-      gprint (GP_LOG, "%s %f %f  %f\n", key, X, Y, Z);
-    } else {
-      XY_to_RD (&R, &D, X, Y, &graphmode.coords);
-      sprintf (string, "R%s", key);
-      set_variable (string, R);
-      sprintf (string, "D%s", key);
-      set_variable (string, D);
-      gprint (GP_LOG, "%s %f %f\n", key, X, Y);
-    }
+    gprint (GP_LOG, "%s %f %f %f %f %f\n", key, X, Y, Z, R, D);
+
     if (!strcasecmp (key, "Q")) break;
   }
 
   signal (SIGINT, oldsignal);
-  KiiCursorOff (Source);
+  KiiCursorOff (kapa);
   return (TRUE);
 }
Index: trunk/Ohana/src/opihi/cmd.data/device.c
===================================================================
--- trunk/Ohana/src/opihi/cmd.data/device.c	(revision 13344)
+++ trunk/Ohana/src/opihi/cmd.data/device.c	(revision 13479)
@@ -3,30 +3,25 @@
 int device (int argc, char **argv) {
 
-  int N, Source, Nsource, IsImage;
+  int N, kapa, IsImage;
+  char *name;;
   /* set / get current graphics device */
 
-  Nsource = -1;
+  name = NULL;
   if ((N = get_argument (argc, argv, "-n"))) {
     remove_argument (N, &argc, argv);
-    Nsource = atof (argv[N]);
+    name = strcreate (argv[N]);
     remove_argument (N, &argc, argv);
   }
-  if ((N = get_argument (argc, argv, "-g"))) {
-    remove_argument (N, &argc, argv);
-    SetImageDevice (FALSE);
-  }  
-  if ((N = get_argument (argc, argv, "-i"))) {
-    remove_argument (N, &argc, argv);
-    SetImageDevice (TRUE);
-  }  
 
-  IsImage = GetCurrentDevice ();
-  if (IsImage) {
-    if (!GetImage (&Source, &Nsource)) return (FALSE);
-    gprint (GP_ERR, "kii %d\n", Nsource); 
+  if (name == NULL) {
+    name = GetKapaName ();
+    if (name == NULL) {
+      gprint (GP_ERR, "no device defined\n");
+      return (NULL);
+    }
   } else {
-    if (!GetGraph (NULL, &Source, &Nsource)) return (FALSE);
-    gprint (GP_ERR, "kapa %d\n", Nsource); 
+    if (!GetGraph (NULL, &kapa, name)) return (FALSE);
   }
+  gprint (GP_ERR, "kapa %s\n", name); 
 
   return (TRUE);
Index: trunk/Ohana/src/opihi/cmd.data/dot.c
===================================================================
--- trunk/Ohana/src/opihi/cmd.data/dot.c	(revision 13344)
+++ trunk/Ohana/src/opihi/cmd.data/dot.c	(revision 13479)
@@ -3,8 +3,9 @@
 int dot (int argc, char **argv) {
   
+  int kapa;
   Graphdata graphmode;
   float x, y;
 
-  if (!style_args (&graphmode, &argc, argv, -1)) return FALSE;
+  if (!style_args (&graphmode, &argc, argv, &kapa)) return FALSE;
 
   if (argc != 3) {
@@ -15,15 +16,11 @@
   y = atof(argv[2]);
 
-  /* set plotting options (these are sticky) */
-  SetGraph (graphmode);
-
   /* set point style and errorbar mode (these are NOT sticky) */
   graphmode.style = 2;
   graphmode.etype = 0;
 
-  if (!PrepPlotting (1, &graphmode)) return (FALSE);
-  
-  PlotVector (1, &x);
-  PlotVector (1, &y);
+  if (!KapaPrepPlot (kapa, 1, &graphmode)) return (FALSE);
+  KapaPlotVector (kapa, 1, &x, "x");
+  KapaPlotVector (kapa, 1, &y, "y");
   
   return (TRUE);
Index: trunk/Ohana/src/opihi/cmd.data/erase.c
===================================================================
--- trunk/Ohana/src/opihi/cmd.data/erase.c	(revision 13344)
+++ trunk/Ohana/src/opihi/cmd.data/erase.c	(revision 13479)
@@ -4,13 +4,15 @@
   
   int i, N;
-  int Ximage, Nimage;
+  int kapa;
+  char *name;
   
-  Nimage = -1;
+  name = NULL;
   if ((N = get_argument (argc, argv, "-n"))) {
     remove_argument (N, &argc, argv);
-    Nimage = atof (argv[N]);
+    name = strcreate (argv[N]);
     remove_argument (N, &argc, argv);
   }
-  if (!GetImage (&Ximage, &Nimage)) return (FALSE);
+  if (!GetImage (NULL, &kapa, name)) return (FALSE);
+  FREE (name);
 
   if (argc < 2) {
@@ -22,8 +24,8 @@
   for (i = 1; i < argc; i++) {
     if (!(strcasecmp (argv[i], "all"))) {
-      KiiEraseOverlay (Ximage, "red");
-      KiiEraseOverlay (Ximage, "green");
-      KiiEraseOverlay (Ximage, "blue");
-      KiiEraseOverlay (Ximage, "yellow");
+      KiiEraseOverlay (kapa, "red");
+      KiiEraseOverlay (kapa, "green");
+      KiiEraseOverlay (kapa, "blue");
+      KiiEraseOverlay (kapa, "yellow");
       continue;
     }
@@ -32,5 +34,5 @@
       return (FALSE);
     }
-    KiiEraseOverlay (Ximage, argv[i]);
+    KiiEraseOverlay (kapa, argv[i]);
   }
   return (TRUE);
Index: trunk/Ohana/src/opihi/cmd.data/grid.c
===================================================================
--- trunk/Ohana/src/opihi/cmd.data/grid.c	(revision 13344)
+++ trunk/Ohana/src/opihi/cmd.data/grid.c	(revision 13479)
@@ -3,17 +3,10 @@
 int grid (int argc, char **argv) {
   
-  int j, N, MinorTick, MajorTick;
+  int j, kapa, N, MinorTick, MajorTick;
   Vector Xvec, Yvec;
   double range, lrange, factor, mantis, fmantis, power, major, minor, first, next;
   Graphdata graphmode;
-  int Ngraph;
-
-  Ngraph = -1;
-  if ((N = get_argument (argc, argv, "-n"))) {
-    remove_argument (N, &argc, argv);
-    Ngraph = atof (argv[N]);
-    remove_argument (N, &argc, argv);
-  }
-  if (!GetGraph (&graphmode, NULL, &Ngraph)) return (FALSE);
+
+  if (!style_args (&graphmode, &argc, argv, &kapa)) return (FALSE);
 
   MajorTick = TRUE;
@@ -200,7 +193,5 @@
   graphmode.ptype = 100; /* connect a pair */
   graphmode.etype = 0;
-  PrepPlotting (N, &graphmode);
-  PlotVector (N, Xvec.elements);
-  PlotVector (N, Yvec.elements);
+  PlotVectorPair (kapa, N, Xvec.elements, Yvec.elements, &graphmode);
 
   free (Xvec.elements);
Index: trunk/Ohana/src/opihi/cmd.data/init.c
===================================================================
--- trunk/Ohana/src/opihi/cmd.data/init.c	(revision 13344)
+++ trunk/Ohana/src/opihi/cmd.data/init.c	(revision 13479)
@@ -235,6 +235,5 @@
   int i;
 
-  InitGraph ();
-  InitImage ();
+  InitKapa ();
   InitQueues ();
 
Index: trunk/Ohana/src/opihi/cmd.data/jpeg.c
===================================================================
--- trunk/Ohana/src/opihi/cmd.data/jpeg.c	(revision 13344)
+++ trunk/Ohana/src/opihi/cmd.data/jpeg.c	(revision 13479)
@@ -4,5 +4,6 @@
 
   char filename[1024];
-  int N, Source, Nsource, IsImage, IsPNG;
+  int N, kapa, IsImage, IsPNG, IsPPM;
+  char *name;
   
   if ((N = get_argument (argc, argv, "--help"))) {
@@ -12,9 +13,15 @@
 
   /* image type */
-  IsPNG = TRUE;
+  IsPPM = IsPNG = FALSE;
   if ((N = get_argument (argc, argv, "-ppm"))) {
     remove_argument (N, &argc, argv);
-    IsPNG = FALSE;
+    IsPPM = TRUE;
   }
+  if ((N = get_argument (argc, argv, "-png"))) {
+    remove_argument (N, &argc, argv);
+    IsPNG = TRUE;
+  }
+  if (!strcmp (argv[0], "png")) IsPNG = TRUE;
+  if (!strcmp (argv[0], "ppm")) IsPPM = TRUE;
 
   /* file name */
@@ -27,31 +34,30 @@
 
   /* display source */
-  Nsource = -1;
+  name = NULL;
   if ((N = get_argument (argc, argv, "-n"))) {
     remove_argument (N, &argc, argv);
-    Nsource = atof (argv[N]);
+    name = strcreate (argv[N]);
     remove_argument (N, &argc, argv);
   }
+
+  /* XXX output png / jpeg needs to include both graph and image
+     if available.  this is a poor mix of data representations 
+     (png for image / jpeg for plots)
   if ((N = get_argument (argc, argv, "-g"))) {
-    remove_argument (N, &argc, argv);
-    SetImageDevice (FALSE);
-  }  
   if ((N = get_argument (argc, argv, "-i"))) {
-    remove_argument (N, &argc, argv);
-    SetImageDevice (TRUE);
-  }  
-  IsImage = GetCurrentDevice ();
-  if (IsImage) {
-    if (!GetImage (&Source, &Nsource)) return (FALSE);
-    if (!filename[0]) strcpy (filename, "Ximage.jpg");
-    KiiJPEG (Source, filename);
-  } else {
-    if (!GetGraph (NULL, &Source, &Nsource)) return (FALSE);
-    if (!filename[0]) strcpy (filename, "Xgraph.png");
-    if (IsPNG) {
-      KapaPNG (Source, filename);
-    } else {
-      KapaPPM (Source, filename);
-    }
+  */
+
+  if (!GetGraph (NULL, &kapa, name)) return (FALSE);
+  if (!IsPNG && !IsPPM) {
+    if (!filename[0]) strcpy (filename, "kapa.jpg");
+    KiiJPEG (kapa, filename);
+  }
+  if (IsPNG) {
+    if (!filename[0]) strcpy (filename, "kapa.png");
+    KapaPNG (kapa, filename);
+  } 
+  if (IsPPM) {
+    if (!filename[0]) strcpy (filename, "kapa.ppm");
+    KapaPPM (kapa, filename);
   }
   return (TRUE);
Index: trunk/Ohana/src/opihi/cmd.data/labels.c
===================================================================
--- trunk/Ohana/src/opihi/cmd.data/labels.c	(revision 13344)
+++ trunk/Ohana/src/opihi/cmd.data/labels.c	(revision 13479)
@@ -4,7 +4,7 @@
   
   char name[64];
-  int N, size, Xgraph;
+  int N, size, kapa;
 
-  if (!GetGraph (NULL, &Xgraph, NULL)) return (FALSE);
+  if (!GetGraph (NULL, &kapa, NULL)) return (FALSE);
 
   if (get_argument (argc, argv, "-h")) {
@@ -36,10 +36,10 @@
     size = atof (argv[N]);
     remove_argument (N, &argc, argv);
-    KapaSetFont (Xgraph, name, size);
+    KapaSetFont (kapa, name, size);
   } 
 
   if ((N = get_argument (argc, argv, "-x"))) {
     remove_argument (N, &argc, argv);
-    KapaSendLabel (Xgraph, argv[N], 0);
+    KapaSendLabel (kapa, argv[N], 0);
     remove_argument (N, &argc, argv);
   }
@@ -47,5 +47,5 @@
   if ((N = get_argument (argc, argv, "-y"))) {
     remove_argument (N, &argc, argv);
-    KapaSendLabel (Xgraph, argv[N], 1);
+    KapaSendLabel (kapa, argv[N], 1);
     remove_argument (N, &argc, argv);
   }
@@ -53,5 +53,5 @@
   if ((N = get_argument (argc, argv, "+x"))) {
     remove_argument (N, &argc, argv);
-    KapaSendLabel (Xgraph, argv[N], 2);
+    KapaSendLabel (kapa, argv[N], 2);
     remove_argument (N, &argc, argv);
   }
@@ -59,5 +59,5 @@
   if ((N = get_argument (argc, argv, "+y"))) {
     remove_argument (N, &argc, argv);
-    KapaSendLabel (Xgraph, argv[N], 3);
+    KapaSendLabel (kapa, argv[N], 3);
     remove_argument (N, &argc, argv);
   }
@@ -65,5 +65,5 @@
   if ((N = get_argument (argc, argv, "-ul"))) {
     remove_argument (N, &argc, argv);
-    KapaSendLabel (Xgraph, argv[N], 4);
+    KapaSendLabel (kapa, argv[N], 4);
     remove_argument (N, &argc, argv);
   }
@@ -71,5 +71,5 @@
   if ((N = get_argument (argc, argv, "-ur"))) {
     remove_argument (N, &argc, argv);
-    KapaSendLabel (Xgraph, argv[N], 5);
+    KapaSendLabel (kapa, argv[N], 5);
     remove_argument (N, &argc, argv);
   }
@@ -77,5 +77,5 @@
   if ((N = get_argument (argc, argv, "-ll"))) {
     remove_argument (N, &argc, argv);
-    KapaSendLabel (Xgraph, argv[N], 6);
+    KapaSendLabel (kapa, argv[N], 6);
     remove_argument (N, &argc, argv);
   }
@@ -83,5 +83,5 @@
   if ((N = get_argument (argc, argv, "-lr"))) {
     remove_argument (N, &argc, argv);
-    KapaSendLabel (Xgraph, argv[N], 7);
+    KapaSendLabel (kapa, argv[N], 7);
     remove_argument (N, &argc, argv);
   }
Index: trunk/Ohana/src/opihi/cmd.data/limits.c
===================================================================
--- trunk/Ohana/src/opihi/cmd.data/limits.c	(revision 13344)
+++ trunk/Ohana/src/opihi/cmd.data/limits.c	(revision 13479)
@@ -4,5 +4,6 @@
 
   int N, APPLY;
-  int Ngraph, Xgraph;
+  int kapa;
+  char *name;
   Graphdata graphmode;
   Vector *xvec, *yvec;
@@ -15,15 +16,15 @@
     APPLY = TRUE;
   }
-
-  Ngraph = -1;
+  name = NULL;
   if ((N = get_argument (argc, argv, "-n"))) {
     remove_argument (N, &argc, argv);
-    Ngraph = atof (argv[N]);
+    name = strcreate (argv[N]);
     remove_argument (N, &argc, argv);
   }
-  if (!GetGraph (&graphmode, &Xgraph, &Ngraph)) return (FALSE);
+  if (!GetGraph (&graphmode, &kapa, name)) return (FALSE);
+  FREE (name);
 
   if (argc == 1) {
-    gprint (GP_ERR, "limits: %f %f %f %f [-a] [-n Xgraph]\n",
+    gprint (GP_ERR, "limits: %f %f %f %f [-a] [-n device]\n",
 	     graphmode.xmin, graphmode.xmax,
 	     graphmode.ymin, graphmode.ymax);
@@ -71,5 +72,5 @@
  success:
   SetLimits (xvec, yvec, &graphmode);
-  if (APPLY) KapaSetLimits (Xgraph, &graphmode);
+  if (APPLY) KapaSetLimits (kapa, &graphmode);
   return (TRUE);
 }
Index: trunk/Ohana/src/opihi/cmd.data/line.c
===================================================================
--- trunk/Ohana/src/opihi/cmd.data/line.c	(revision 13344)
+++ trunk/Ohana/src/opihi/cmd.data/line.c	(revision 13479)
@@ -3,8 +3,9 @@
 int line (int argc, char **argv) {
   
+  int kapa;
   Graphdata graphmode;
   float x[2], y[2];
 
-  if (!style_args (&graphmode, &argc, argv, -1)) return FALSE;
+  if (!style_args (&graphmode, &argc, argv, &kapa)) return FALSE;
 
   if (argc != 6) {
@@ -17,16 +18,12 @@
   y[1] = atof(argv[5]);
 
-  SetGraph (graphmode);
-
   /* set point style and errorbar mode (these are NOT sticky) */
   graphmode.style = 0;
   graphmode.etype = 0;
 
-  if (!PrepPlotting (2, &graphmode)) return (FALSE);
-  
-  PlotVector (2, x);
-  PlotVector (2, y);
+  if (!KapaPrepPlot (kapa, 2, &graphmode)) return (FALSE);
+  KapaPlotVector (kapa, 2, x, "x");
+  KapaPlotVector (kapa, 2, y, "y");
   
   return (TRUE);
-
 }
Index: trunk/Ohana/src/opihi/cmd.data/load.c
===================================================================
--- trunk/Ohana/src/opihi/cmd.data/load.c	(revision 13344)
+++ trunk/Ohana/src/opihi/cmd.data/load.c	(revision 13479)
@@ -4,21 +4,23 @@
   
   int i, N, n, ISCEL;
-  int Ximage, Nimage, Noverlay, NOVERLAY;
+  int kapa, Noverlay, NOVERLAY;
   char *c, type[10], string[128], line[1024];
   double x, y, dx, dy, x1, y1;
   double dra, ddec, ra1, dec1, ra, dec;
   FILE *f;
-  char *buffer;
+  char *buffer, *name;
   Coords coords;
   Buffer *buf;
   KiiOverlay *overlay;
+  KapaImageData data;
   
-  Nimage = -1;
+  name = NULL;
   if ((N = get_argument (argc, argv, "-n"))) {
     remove_argument (N, &argc, argv);
-    Nimage = atof (argv[N]);
+    name = strcreate (argv[N]);
     remove_argument (N, &argc, argv);
   }
-  if (!GetImage (&Ximage, &Nimage)) return (FALSE);
+  if (!GetImageData (&data, &kapa, name)) return (FALSE);
+  FREE (name);
 
   ISCEL = FALSE;
@@ -45,5 +47,5 @@
 
   if (ISCEL) {
-    if ((buf = SelectBuffer (GetImageName(), OLDBUFFER, TRUE)) == NULL) return (FALSE);
+    if ((buf = SelectBuffer (data.name, OLDBUFFER, TRUE)) == NULL) return (FALSE);
     GetCoords (&coords, &buf[0].header);
   }
@@ -125,5 +127,5 @@
   }
 
-  KiiLoadOverlay (Ximage, overlay, Noverlay, argv[1]);
+  KiiLoadOverlay (kapa, overlay, Noverlay, argv[1]);
 
   for (i = 0; i < Noverlay; i++) {
Index: trunk/Ohana/src/opihi/cmd.data/plot.c
===================================================================
--- trunk/Ohana/src/opihi/cmd.data/plot.c	(revision 13344)
+++ trunk/Ohana/src/opihi/cmd.data/plot.c	(revision 13479)
@@ -3,9 +3,9 @@
 int plot (int argc, char **argv) {
   
-  int N, Npts;
+  int kapa, N, Npts;
   Graphdata graphmode;
   Vector *xvec, *yvec, *dxmvec, *dxpvec, *dymvec, *dypvec;
 
-  if (!style_args (&graphmode, &argc, argv, -1)) return FALSE;
+  if (!style_args (&graphmode, &argc, argv, &kapa)) return FALSE;
 
   /* decide on error bars */
@@ -36,5 +36,4 @@
     return (FALSE);
   }
-  SetGraph (graphmode);
 
   /* set errorbar mode (these are NOT sticky) */
@@ -57,18 +56,16 @@
   if (Npts == 0) return (TRUE);
 
-  if (!PrepPlotting (Npts, &graphmode)) return (FALSE);
+  if (!KapaPrepPlot (kapa, Npts, &graphmode)) return (FALSE);
   
-  PlotVector (Npts, xvec[0].elements);
-  PlotVector (Npts, yvec[0].elements);
+  KapaPlotVector (kapa, Npts, xvec[0].elements, "x");
+  KapaPlotVector (kapa, Npts, yvec[0].elements, "y");
   if (graphmode.etype & 0x01) {
-    PlotVector (Npts, dymvec[0].elements);
-    PlotVector (Npts, dypvec[0].elements);
+    KapaPlotVector (kapa, Npts, dymvec[0].elements, "dym");
+    KapaPlotVector (kapa, Npts, dypvec[0].elements, "dyp");
   }
   if (graphmode.etype & 0x02) {
-    PlotVector (Npts, dxmvec[0].elements);
-    PlotVector (Npts, dxpvec[0].elements);
+    KapaPlotVector (kapa, Npts, dxmvec[0].elements, "dxm");
+    KapaPlotVector (kapa, Npts, dxpvec[0].elements, "dxp");
   }
-  
   return (TRUE);
-
 }
Index: trunk/Ohana/src/opihi/cmd.data/point.c
===================================================================
--- trunk/Ohana/src/opihi/cmd.data/point.c	(revision 13344)
+++ trunk/Ohana/src/opihi/cmd.data/point.c	(revision 13479)
@@ -4,19 +4,22 @@
   
   int N, celestial, pixscale;
-  int Ximage, Nimage;
+  int kapa;
   double ra, dec, dra, ddec;
   double x1, y1, ra1, dec1;
+  char *name;
   Coords coords;
   Buffer *buf;
   KiiOverlay overlay;
+  KapaImageData data;
 
-  Nimage = -1;
+  name = NULL;
   if ((N = get_argument (argc, argv, "-n"))) {
     remove_argument (N, &argc, argv);
-    Nimage = atof (argv[N]);
+    name = strcreate (argv[N]);
     remove_argument (N, &argc, argv);
   }
-  if (!GetImage (&Ximage, &Nimage)) return (FALSE);
-  
+  if (!GetImageData (&data, &kapa, name)) return (FALSE);
+  FREE (name);
+
   celestial = FALSE;
   if ((N = get_argument (argc, argv, "-c"))) {
@@ -37,5 +40,5 @@
   
   if (celestial) {
-    if ((buf = SelectBuffer (GetImageName(), OLDBUFFER, TRUE)) == NULL) return (FALSE);
+    if ((buf = SelectBuffer (data.name, OLDBUFFER, TRUE)) == NULL) return (FALSE);
     GetCoords (&coords, &buf[0].header);
   }
@@ -66,5 +69,5 @@
   }
   overlay.type = KiiOverlayTypeByName (argv[2]);
-  KiiLoadOverlay (Ximage, &overlay, 1, argv[1]);
+  KiiLoadOverlay (kapa, &overlay, 1, argv[1]);
   return (TRUE);
 }
Index: trunk/Ohana/src/opihi/cmd.data/ps.c
===================================================================
--- trunk/Ohana/src/opihi/cmd.data/ps.c	(revision 13344)
+++ trunk/Ohana/src/opihi/cmd.data/ps.c	(revision 13479)
@@ -3,6 +3,6 @@
 int ps (int argc, char **argv) {
 
-  char filename[1024], pagename[1024];
-  int N, Source, Nsource, scaleMode, pageMode, IsImage;
+  char filename[1024], pagename[1024], *name;
+  int N, kapa, scaleMode, pageMode, IsImage;
   
   if ((N = get_argument (argc, argv, "--help"))) goto help;
@@ -44,35 +44,23 @@
 
   /* which tool */
-  Nsource = -1;
+  name = NULL;
   if ((N = get_argument (argc, argv, "-n"))) {
     remove_argument (N, &argc, argv);
-    Nsource = atof (argv[N]);
+    name = strcreate (argv[N]);
     remove_argument (N, &argc, argv);
   }
-  /* which device */
-  if ((N = get_argument (argc, argv, "-g"))) {
-    remove_argument (N, &argc, argv);
-    SetImageDevice (FALSE);
-  }  
-  if ((N = get_argument (argc, argv, "-i"))) {
-    remove_argument (N, &argc, argv);
-    SetImageDevice (TRUE);
-  }  
-  
+
   if ((argc > 1) && filename[0]) goto help;
 
   if (argc > 1) strcpy (filename, argv[1]);
 
-  IsImage = GetCurrentDevice ();
-  if (IsImage) {
-    if (!GetImage (&Source, &Nsource)) return (FALSE);
-    if (!filename[0]) strcpy (filename, "Ximage.ps");
-  } else {
-    if (!GetGraph (NULL, &Source, &Nsource)) return (FALSE);
-    if (!filename[0]) strcpy (filename, "Xgraph.ps");
-  }
+  // get the connection to kapa, false if none available
+  if (!GetGraphData (NULL, &kapa, name)) return (FALSE);
+  FREE (name);
+
+  if (!filename[0]) strcpy (filename, "kapa.ps");
   
   /* tell Ximage/Xgraph to ps the image */
-  KiiPS (Source, filename, scaleMode, pageMode, pagename);
+  KiiPS (kapa, filename, scaleMode, pageMode, pagename);
   return (TRUE);
 
Index: trunk/Ohana/src/opihi/cmd.data/rd.c
===================================================================
--- trunk/Ohana/src/opihi/cmd.data/rd.c	(revision 13344)
+++ trunk/Ohana/src/opihi/cmd.data/rd.c	(revision 13479)
@@ -59,5 +59,8 @@
 
   /* find matrix, free old data */
-  if ((buf = SelectBuffer (argv[1], ANYBUFFER, TRUE)) == NULL) return (FALSE);
+  if ((buf = SelectBuffer (argv[1], ANYBUFFER, TRUE)) == NULL) {
+      fclose (f);
+      return (FALSE);
+  }
   gfits_free_matrix (&buf[0].matrix);
   gfits_free_header (&buf[0].header);
@@ -79,4 +82,5 @@
       gprint (GP_ERR, "entry in %s not found\n", argv[2]);
       DeleteBuffer (buf);
+      fclose (f);
       return (FALSE);
     }
@@ -99,4 +103,5 @@
 	DeleteBuffer (buf);
 	free (CCDKeyword);
+	fclose (f);
 	return (FALSE);
       }
@@ -105,4 +110,5 @@
 	DeleteBuffer (buf);
 	free (CCDKeyword);
+	fclose (f);
 	return (FALSE);
       }
@@ -143,4 +149,5 @@
     buf[0].header.bzero = 0;
     buf[0].header.bscale = 1;
+    fclose (f);
     return (TRUE);
   }
@@ -152,4 +159,5 @@
     gprint (GP_ERR, "-plane is too large: %d total planes\n", Nplane);
     DeleteBuffer (buf);
+    fclose (f);
     return (FALSE);
   }
Index: trunk/Ohana/src/opihi/cmd.data/resize.c
===================================================================
--- trunk/Ohana/src/opihi/cmd.data/resize.c	(revision 13344)
+++ trunk/Ohana/src/opihi/cmd.data/resize.c	(revision 13479)
@@ -5,27 +5,16 @@
   char *end;
   double NX, NY;
-  int N, Source, Nsource, IsImage;
+  int N, kapa;
+  char *name;
   
   /* display source */
-  Nsource = -1;
+  name = NULL;
   if ((N = get_argument (argc, argv, "-n"))) {
     remove_argument (N, &argc, argv);
-    Nsource = atof (argv[N]);
+    name = strcreate (argv[N]);
     remove_argument (N, &argc, argv);
   }
-  if ((N = get_argument (argc, argv, "-g"))) {
-    remove_argument (N, &argc, argv);
-    SetImageDevice (FALSE);
-  }  
-  if ((N = get_argument (argc, argv, "-i"))) {
-    remove_argument (N, &argc, argv);
-    SetImageDevice (TRUE);
-  }  
-  IsImage = GetCurrentDevice ();
-  if (IsImage) {
-    if (!GetImage (&Source, &Nsource)) return (FALSE);
-  } else {
-    if (!GetGraph (NULL, &Source, &Nsource)) return (FALSE);
-  }
+  if (!GetImage (NULL, &kapa, name)) return (FALSE);
+  FREE (name);
 
   if (argc != 3) {
@@ -49,5 +38,5 @@
   if (!strcmp (end, "cm")) { NY *= 38; }
 
-  KiiResize (Source, NX, NY);
+  KiiResize (kapa, NX, NY);
   return (TRUE);
 }
Index: trunk/Ohana/src/opihi/cmd.data/save.c
===================================================================
--- trunk/Ohana/src/opihi/cmd.data/save.c	(revision 13344)
+++ trunk/Ohana/src/opihi/cmd.data/save.c	(revision 13479)
@@ -4,13 +4,15 @@
   
   int N, celestial;
-  int Ximage, Nimage;
+  int kapa;
+  char *name;
   
-  Nimage = -1;
+  name = NULL;
   if ((N = get_argument (argc, argv, "-n"))) {
     remove_argument (N, &argc, argv);
-    Nimage = atof (argv[N]);
+    name = strcreate (argv[N]);
     remove_argument (N, &argc, argv);
   }
-  if (!GetImage (&Ximage, &Nimage)) return (FALSE);
+  if (!GetImage (NULL, &kapa, name)) return (FALSE);
+  FREE (name);
 
   celestial = FALSE;
@@ -26,5 +28,5 @@
   }
 
-  KiiSaveOverlay (Ximage, celestial, argv[1], argv[2]);
+  KiiSaveOverlay (kapa, celestial, argv[1], argv[2]);
   return (TRUE);
 }
Index: trunk/Ohana/src/opihi/cmd.data/section.c
===================================================================
--- trunk/Ohana/src/opihi/cmd.data/section.c	(revision 13344)
+++ trunk/Ohana/src/opihi/cmd.data/section.c	(revision 13479)
@@ -1,29 +1,61 @@
 # include "data.h"
+
+enum {NONE, LIST, UP, DOWN, TOP, BOTTOM, TOOL};
 
 int section (int argc, char **argv) {
   
-  int N, List;
-  int Ngraph, Xgraph;
+  int N, action, kapa;
+  char *name, *location;
   Graphdata graphmode;
   KapaSection section;
 
-  List = FALSE;
+  action = NONE;
   if ((N = get_argument (argc, argv, "-list"))) {
+    action = LIST;
     remove_argument (N, &argc, argv);
-    List = TRUE;
+  }
+  if ((N = get_argument (argc, argv, "-up"))) {
+    action = UP;
+    remove_argument (N, &argc, argv);
+  }
+  if ((N = get_argument (argc, argv, "-down"))) {
+    action = DOWN;
+    remove_argument (N, &argc, argv);
+  }
+  if ((N = get_argument (argc, argv, "-top"))) {
+    action = TOP;
+    remove_argument (N, &argc, argv);
+  }
+  if ((N = get_argument (argc, argv, "-bottom"))) {
+    action = BOTTOM;
+    remove_argument (N, &argc, argv);
+  }
+  if ((N = get_argument (argc, argv, "-imtool"))) {
+    action = TOOL;
+    remove_argument (N, &argc, argv);
+    location = argv[N];
+    remove_argument (N, &argc, argv);
   }
 
-  Ngraph = -1;
+  name = NULL;
   if ((N = get_argument (argc, argv, "-n"))) {
     remove_argument (N, &argc, argv);
-    Ngraph = atof (argv[N]);
+    name = strcreate (argv[N]);
     remove_argument (N, &argc, argv);
   }
-  if (!GetGraph (&graphmode, &Xgraph, &Ngraph)) return (FALSE);
+  if (!GetGraph (&graphmode, &kapa, name)) return (FALSE);
+  FREE (name);
 
   /* list sections */
-  if (argc == 1) {
-    KapaGetSection (Xgraph, "*");
-    gprint (GP_ERR, "USAGE: section name [x y dx dy]\n");
+  if ((argc == 1) && (action == NONE)) {
+    KapaGetSection (kapa, "*");
+    gprint (GP_ERR, "USAGE: section name [x y dx dy] [options]\n");
+    gprint (GP_ERR, "OPTIONS: -list   : show properties of all sections\n");
+    gprint (GP_ERR, "         -up     : move section up in display stack\n");
+    gprint (GP_ERR, "         -down   : move section down in display stack\n");
+    gprint (GP_ERR, "         -top    : move section to top of display stack\n");
+    gprint (GP_ERR, "         -bottom : move section to bottom of display stack\n");
+    gprint (GP_ERR, "         -imtool (position) : set location of image zoom / status box\n");
+    gprint (GP_ERR, "                 (position may be: -x, +x, -y, +y, none)\n");
     return (TRUE);
   } 
@@ -31,8 +63,50 @@
   if (argc == 2) {
     /* select / show section */
-    if (List) {
-      KapaGetSection (Xgraph, argv[1]);
-    } else {
-      KapaSelectSection (Xgraph, argv[1]);
+    switch (action) {
+      case NONE:
+	KapaSelectSection (kapa, argv[1]);
+	break;
+
+      case UP:
+	KapaMoveSection (kapa, argv[1], "up");
+	break;
+      case DOWN:
+	KapaMoveSection (kapa, argv[1], "down");
+	break;
+      case TOP:
+	KapaMoveSection (kapa, argv[1], "top");
+	break;
+      case BOTTOM:
+	KapaMoveSection (kapa, argv[1], "bottom");
+	break;
+
+      case TOOL:
+	if (!strcmp(location, "-x")) {
+	  KapaSetToolbox (kapa, 1);
+	  break;
+	}
+	if (!strcmp(location, "+x")) {
+	  KapaSetToolbox (kapa, 3);
+	  break;
+	}
+	if (!strcmp(location, "-y")) {
+	  KapaSetToolbox (kapa, 2);
+	  break;
+	}
+	if (!strcmp(location, "+y")) {
+	  KapaSetToolbox (kapa, 4);
+	  break;
+	}
+	if (!strcmp(location, "none")) {
+	  KapaSetToolbox (kapa, 0);
+	  break;
+	}
+	gprint (GP_ERR, "unknown toolbox location %s\n", location);
+	gprint (GP_ERR, "valid values: -x, +x, -y, +y, none\n");
+	return (FALSE);
+
+      case LIST:
+	KapaGetSection (kapa, argv[1]);
+	break;
     }
     return (TRUE);
@@ -46,8 +120,9 @@
     section.dx = atof (argv[4]);
     section.dy = atof (argv[5]);
-    KapaSetSection (Xgraph, &section);
+    KapaSetSection (kapa, &section);
     return (TRUE);
   }
   gprint (GP_ERR, "USAGE: section name [x y dx dy]\n");
+  gprint (GP_ERR, "USAGE: section name [-list] [-up] [-down] [-top] [-bottom]\n");
   return (FALSE);
 }
Index: trunk/Ohana/src/opihi/cmd.data/style.c
===================================================================
--- trunk/Ohana/src/opihi/cmd.data/style.c	(revision 13344)
+++ trunk/Ohana/src/opihi/cmd.data/style.c	(revision 13479)
@@ -3,7 +3,8 @@
 int style (int argc, char **argv) {
   
-  Graphdata graphmode;
+  int kapa;
+  Graphdata data;
 
-  if (!style_args (&graphmode, &argc, argv, -1)) return FALSE;
+  if (!style_args (&data, &argc, argv, &kapa)) return FALSE;
 
   if (argc > 1) {
@@ -11,5 +12,5 @@
     return (FALSE);
   }
-  SetGraph (graphmode);
+  KapaSetGraphData (kapa, &data);
 
   return (TRUE);
Index: trunk/Ohana/src/opihi/cmd.data/textline.c
===================================================================
--- trunk/Ohana/src/opihi/cmd.data/textline.c	(revision 13344)
+++ trunk/Ohana/src/opihi/cmd.data/textline.c	(revision 13479)
@@ -6,14 +6,8 @@
   char name[64];
   double x, y, angle;
-  int Ngraph, Xgraph;
+  int kapa;
   Graphdata graphmode;
 
-  Ngraph = -1;
-  if ((N = get_argument (argc, argv, "-n"))) {
-    remove_argument (N, &argc, argv);
-    Ngraph = atof (argv[N]);
-    remove_argument (N, &argc, argv);
-  }
-  if (!GetGraph (&graphmode, &Xgraph, &Ngraph)) return (FALSE);
+  if (!style_args (&graphmode, &argc, argv, &kapa)) return (FALSE);
 
   if ((N = get_argument (argc, argv, "-fn"))) {
@@ -23,5 +17,5 @@
     size = atof (argv[N]);
     remove_argument (N, &argc, argv);
-    KapaSetFont (Xgraph, name, size);
+    KapaSetFont (kapa, name, size);
   } 
 
@@ -58,5 +52,5 @@
   }    
 
-  KapaSendTextline (Xgraph, argv[3], x, y, angle);
+  KapaSendTextline (kapa, argv[3], x, y, angle);
   return (TRUE);
 }
Index: trunk/Ohana/src/opihi/cmd.data/tv.c
===================================================================
--- trunk/Ohana/src/opihi/cmd.data/tv.c	(revision 13344)
+++ trunk/Ohana/src/opihi/cmd.data/tv.c	(revision 13479)
@@ -3,31 +3,33 @@
 int tv (int argc, char **argv) {
   
-  int N, Ximage, Nimage;
+  int N, kapa;
+  char *name, *file;
   Coords coords;
   Buffer *buf;
   KiiImage image;
-  KiiDisplayMode mode;
+  KapaImageData data;
 
-  Nimage = -1;
+  name = NULL;
   if ((N = get_argument (argc, argv, "-n"))) {
     remove_argument (N, &argc, argv);
-    Nimage = atof (argv[N]);
+    name = strcreate (argv[N]);
     remove_argument (N, &argc, argv);
   }
-  if (!GetImage (&Ximage, &Nimage)) return (FALSE);
+  if (!GetImage (&data, &kapa, name)) return (FALSE);
+  FREE (name);
 
   /* shell exits on pipe close, FIX */
   if ((N = get_argument (argc, argv, "-kill"))) {
-    KiiClose (Ximage);
-    Ximage = 0;
+    KiiClose (kapa);
     return (TRUE);
   }
 
-  mode.logflux = FALSE;
+  data.logflux = FALSE;
   if ((N = get_argument (argc, argv, "-log"))) {
     remove_argument (N, &argc, argv);
-    mode.logflux = TRUE;
+    data.logflux = TRUE;
   }
 
+  // use the currently-set zero,range values if not supplied
   if ((argc != 2) && (argc != 4)) {
     gprint (GP_ERR, "USAGE: tv <buffer> [zero range] [-n Nimage] [-log] [-kill]\n");
@@ -35,10 +37,8 @@
   }
 
-  GetImageScale (&mode.zero, &mode.range);
   if (argc == 4) {
-    mode.zero = atof (argv[2]);
-    mode.range = atof (argv[3]);
-    if (mode.range == 0.0) mode.range = 0.001;
-    SetImageScale (mode.zero, mode.range);
+    data.zero = atof (argv[2]);
+    data.range = atof (argv[3]);
+    if (data.range == 0.0) data.range = 0.001;
   }
 
@@ -49,89 +49,15 @@
   image.Nx = buf[0].matrix.Naxis[0];
   image.Ny = buf[0].matrix.Naxis[1];
-  image.file = buf[0].file;
-  image.name = buf[0].name;
 
-  KiiNewPicture1D (Ximage, &image, &mode, &coords);
+  // send only the root of the file, not the full path
+  file = filerootname (buf[0].file);
+  strcpy (data.file, file);
+  free (file);
 
-  SetImageName (argv[1]);
+  strcpy (data.name, argv[1]);
+  
+  KiiNewPicture1D (kapa, &image, &data, &coords);
+
   set_str_variable ("TV", argv[1]);
   return (TRUE);
 }
-
-
-/* 
-projections and coordinates:
-
-IRAS:
-CTYPE1  = 'LL      '                    /TANGENT PLANE (L)                     
-CRVAL1  =         0.100000D+02          /R.A. OF MAP CENTER  *** SEE COMMENT   
-CDELT1  =        -0.133333D-01          /SEPARATION IN L (DEG)                 
-CRPIX1  =                  128          /MAPCENTER IN L                        
-CROTA1  =         0.000000E+00          /NO ROTATION            
-                                                                               
-CTYPE2  = 'MM      '                    /TANGENT PLANE (M)                     
-CRVAL2  =         0.410000D+02          /DEC. OF MAP CENTER  *** SEE COMMENT   
-CDELT2  =         0.133333D-01          /SEPARATION IN M (DEG)                 
-CRPIX2  =                  128          /MAP CENTER IN M                       
-CROTA2  =         0.000000E+00          /NO ROTATION                           
-
-HIRAS:
-CTYPE1  = 'RA---TAN          ' / PRIMARY AXIS NAME                             
-CRVAL1  =   1.000000000000E+01 / PRIMARY REFERENCE VALUE                       
-CDELT1  =  -5.555555555556E-03 / PRIMARY PIXEL SEPARATION                      
-CRPIX1  =   2.560000000000E+02 / PRIMARY REFERENCE PIXEL                       
-CUNIT1  = 'DEGREE            ' / PRIMARY AXIS UNITS                            
-
-CDELT2  =   5.555555555556E-03 / PRIMARY PIXEL SEPARATION                      
-CRPIX2  =   2.560000000000E+02 / PRIMARY REFERENCE PIXEL                       
-CRVAL2  =   4.100000000000E+01 / PRIMARY REFERENCE VALUE                       
-CTYPE2  = 'DEC--TAN          ' / PRIMARY AXIS NAME                             
-CUNIT2  = 'DEGREE            ' / PRIMARY AXIS UNITS                            
-
-PROJTYPE= 'GNOMON    '                                                         
-COORDSYS= 'EQUATORIAL     '                                                    
-
-gene (small FOV linear):
-RA_O
-RA_X 
-RA_Y 
-DEC_O
-DEC_X
-DEC_Y
-
-
-*/
-
-# ifdef NEWTVMODE  
-  {
-    double *pixvalues, slope;
-
-    ALLOCATE (pixvalues, double, Ncolors + 1);
-    range = fabs (range);
-    for (i = 0; i < Ncolors + 1; i++) {
-      if (ISLOG) {
-	pixvalues[i] = i*range / NNcol + zero;
-      }
-      if (!ISLOG) {
-	pixvalues[i] = pow (10.0, i*log10(range - 1.0) / NNcol) + zero - 1.0;
-      }      
-    } 
-    for (j = 0; j < Npix; j++, in++, out++) {
-      if (*in < pixvalues[0]) {
-	i = 0;
-      } else {
-	if (*in > pixvalues[NNcol]) {
-	  i = NNcol;
-	} else {
-	  for (i = 0; (*in < pixvalues[i]) || (*in > pixvalues[i+1]); ) {
-	    slope = 1.0 / (pixvalues[i+1] - pixvalues[i]);
-	    i = MIN (MAX (0, i + (*in - pixvalues[i]) * slope), NNcol);
-	  }
-	}	
-      }
-      /* for (i = 0; (i < Ncolors - 1) && (pixvalues[i] < *in); i++); */
-      *out = i;
-    }
-    free (pixvalues);
-  }
-# endif
Index: trunk/Ohana/src/opihi/cmd.data/tvcontour.c
===================================================================
--- trunk/Ohana/src/opihi/cmd.data/tvcontour.c	(revision 13344)
+++ trunk/Ohana/src/opihi/cmd.data/tvcontour.c	(revision 13479)
@@ -58,15 +58,17 @@
   float *v00, *v01, *v10, *v11;
   float *Vout, *Vin, *matrix;
-  int Ximage, Nimage, N, Noverlay, NOVERLAY;
+  char *name;
+  int kapa, N, Noverlay, NOVERLAY;
   Buffer *buf;
   KiiOverlay *overlay;
   
-  Nimage = -1;
+  name = NULL;
   if ((N = get_argument (argc, argv, "-n"))) {
     remove_argument (N, &argc, argv);
-    Nimage = atof (argv[N]);
+    name = strcreate (argv[N]);
     remove_argument (N, &argc, argv);
   }
-  if (!GetImage (&Ximage, &Nimage)) return (FALSE);
+  if (!GetImage (NULL, &kapa, name)) return (FALSE);
+  FREE (name);
 
   if ((argc != 4) && (argc != 5)) {
@@ -277,5 +279,5 @@
   }
   
-  KiiLoadOverlay (Ximage, overlay, Noverlay, argv[2]);
+  KiiLoadOverlay (kapa, overlay, Noverlay, argv[2]);
   free (overlay);
 
Index: trunk/Ohana/src/opihi/cmd.data/tvgrid.c
===================================================================
--- trunk/Ohana/src/opihi/cmd.data/tvgrid.c	(revision 13344)
+++ trunk/Ohana/src/opihi/cmd.data/tvgrid.c	(revision 13479)
@@ -11,16 +11,18 @@
   char format[16];
   Coords coords;
-  int Ximage, Nimage, N, Noverlay, NOVERLAY;
+  int kapa, N, Noverlay, NOVERLAY;
+  char *name;
   Buffer *buf;
   KiiOverlay *overlay;
 
-  Nimage = -1;
+  name = NULL;
   if ((N = get_argument (argc, argv, "-n"))) {
     remove_argument (N, &argc, argv);
-    Nimage = atof (argv[N]);
+    name = strcreate (argv[N]);
     remove_argument (N, &argc, argv);
   }
-  if (!GetImage (&Ximage, &Nimage)) return (FALSE);
-  
+  if (!GetImage (NULL, &kapa, name)) return (FALSE);
+  FREE (name);
+
   if (argc != 3) {
     gprint (GP_ERR, "USAGE: tvgrid (overlay) (buffer)\n");
@@ -144,25 +146,5 @@
   }
 
-  # if (0)
-  /* write the labels on the axes */
-  dDEC = fabs(dec1 - dec0) / 100;
-  for (ra = dRA * ((int)(MIN(ra0, ra1)/dRA) + 1); ra < MAX(ra0, ra1); ra += dRA) {
-    RD_to_XY (&x0, &y0, ra + 0.1*dRA, MIN(dec0, dec1), &coords);
-    sprintf (label, format, ra);
-    sprintf (buffer, "%15s %20.10f %20.10f %20.10f %20.10f ", "TEXT", x0, y0, (double)(strlen(label) + 1), 0.0);
-    write (Ximage, buffer, 128);
-    write (Ximage, label, strlen(label) + 1);
-    RD_to_XY (&x0, &y0, ra, MIN(dec0, dec1), &coords);
-    for (dec = MIN(dec0, dec1) + dDEC; dec < MAX(dec0,dec1); dec += dDEC) {
-      RD_to_XY (&x1, &y1, ra, dec, &coords);
-      sprintf (buffer, "%15s %20.10f %20.10f %20.10f %20.10f ", "LINE", x0, y0, (x1 - x0), (y1 - y0));
-      write (Ximage, buffer, 128);
-      x0 = x1;
-      y0 = y1;
-    }
-  }
-  # endif
-
-  KiiLoadOverlay (Ximage, overlay, Noverlay, argv[1]);
+  KiiLoadOverlay (kapa, overlay, Noverlay, argv[1]);
   free (overlay);
 
Index: trunk/Ohana/src/opihi/cmd.data/vload.c
===================================================================
--- trunk/Ohana/src/opihi/cmd.data/vload.c	(revision 13344)
+++ trunk/Ohana/src/opihi/cmd.data/vload.c	(revision 13479)
@@ -4,16 +4,18 @@
   
   int i, N, Noverlay;
-  int Ximage, Nimage, type;
+  int kapa, type;
+  char *name;
   double dx, dy, size;
   KiiOverlay *overlay;
   Vector *vecx, *vecy;
   
-  Nimage = -1;
+  name = NULL;
   if ((N = get_argument (argc, argv, "-n"))) {
     remove_argument (N, &argc, argv);
-    Nimage = atof (argv[N]);
+    name = strcreate (argv[N]);
     remove_argument (N, &argc, argv);
   }
-  if (!GetImage (&Ximage, &Nimage)) return (FALSE);
+  if (!GetImage (NULL, &kapa, name)) return (FALSE);
+  FREE (name);
 
   type = KII_OVERLAY_BOX;
@@ -65,5 +67,5 @@
   }
 
-  KiiLoadOverlay (Ximage, overlay, Noverlay, argv[1]);
+  KiiLoadOverlay (kapa, overlay, Noverlay, argv[1]);
   free (overlay);
   return (TRUE);
Index: trunk/Ohana/src/opihi/cmd.data/zplot.c
===================================================================
--- trunk/Ohana/src/opihi/cmd.data/zplot.c	(revision 13344)
+++ trunk/Ohana/src/opihi/cmd.data/zplot.c	(revision 13479)
@@ -3,5 +3,5 @@
 int zplot (int argc, char **argv) {
   
-  int i, N, Npts, Ngraph;
+  int i, kapa, N, Npts;
   float *in, *out;
   double min, range;
@@ -9,11 +9,5 @@
   Vector *xvec, *yvec, *zvec, Zvec;
 
-  Ngraph = -1;
-  if ((N = get_argument (argc, argv, "-n"))) {
-    remove_argument (N, &argc, argv);
-    Ngraph = atof (argv[N]);
-    remove_argument (N, &argc, argv);
-  }
-  if (!GetGraph (&graphmode, NULL, &Ngraph)) return (FALSE);
+  if (!style_args (&graphmode, &argc, argv, &kapa)) return (FALSE);
 
   if (argc != 6) {
@@ -51,9 +45,5 @@
   graphmode.etype = 0; /* no errorbars */
   Npts = xvec[0].Nelements;
-  PrepPlotting (Npts, &graphmode);
-
-  PlotVector (Npts, xvec[0].elements);
-  PlotVector (Npts, yvec[0].elements);
-  PlotVector (Npts, Zvec.elements);
+  PlotVectorTriplet (kapa, Npts, xvec[0].elements, yvec[0].elements, Zvec.elements, &graphmode);
 
   free (Zvec.elements);
Index: trunk/Ohana/src/opihi/dimm/dimm.c.in
===================================================================
--- trunk/Ohana/src/opihi/dimm/dimm.c.in	(revision 13344)
+++ trunk/Ohana/src/opihi/dimm/dimm.c.in	(revision 13479)
@@ -50,6 +50,5 @@
 /* add program-dependent exit functions here */
 void cleanup () {
-  QuitImage ();
-  QuitGraph ();
+  QuitKapa ();
   return;
 }
Index: trunk/Ohana/src/opihi/dvo/ImageSelection.c
===================================================================
--- trunk/Ohana/src/opihi/dvo/ImageSelection.c	(revision 13344)
+++ trunk/Ohana/src/opihi/dvo/ImageSelection.c	(revision 13479)
@@ -20,6 +20,6 @@
   
   if (RegionSelect) {
-    Ngraph = 0;
-    if (!GetGraphData (&graphsky, NULL, &Ngraph)) {
+    // how does this know which sky plot to use?
+    if (!GetGraphData (&graphsky, NULL, NULL)) {
       gprint (GP_ERR, "region display not available\n");
       return (FALSE);
Index: trunk/Ohana/src/opihi/dvo/Makefile
===================================================================
--- trunk/Ohana/src/opihi/dvo/Makefile	(revision 13344)
+++ trunk/Ohana/src/opihi/dvo/Makefile	(revision 13479)
@@ -21,12 +21,22 @@
 $(SRC)/init.$(ARCH).o            	\
 $(SRC)/ImageOps.$(ARCH).o		\
-$(SRC)/ImageSelection.$(ARCH).o	\
+$(SRC)/ImageSelection.$(ARCH).o	        \
 $(SRC)/LoadImages.$(ARCH).o		\
 $(SRC)/cmpReadFile.$(ARCH).o		\
-$(SRC)/compare.$(ARCH).o               \
+$(SRC)/compare.$(ARCH).o                \
 $(SRC)/match_image.$(ARCH).o		\
-$(SRC)/photometry.$(ARCH).o            \
+$(SRC)/photometry.$(ARCH).o             \
 $(SRC)/dvomisc.$(ARCH).o		\
 $(SRC)/region_list.$(ARCH).o		\
+$(SRC)/dvomisc.$(ARCH).o		\
+$(SRC)/dbBooleanCond.$(ARCH).o		\
+$(SRC)/dbCheckStack.$(ARCH).o		\
+$(SRC)/dbCmdlineFields.$(ARCH).o	\
+$(SRC)/dbExtractAverages.$(ARCH).o	\
+$(SRC)/dbExtractMeasures.$(ARCH).o	\
+$(SRC)/dbFields.$(ARCH).o		\
+$(SRC)/dbRPN.$(ARCH).o			\
+$(SRC)/dbStackMath.$(ARCH).o		\
+$(SRC)/dbStackOps.$(ARCH).o		\
 $(SRC)/dvo.$(ARCH).o
 
Index: trunk/Ohana/src/opihi/dvo/avextract.c
===================================================================
--- trunk/Ohana/src/opihi/dvo/avextract.c	(revision 13344)
+++ trunk/Ohana/src/opihi/dvo/avextract.c	(revision 13479)
@@ -3,12 +3,17 @@
 int avextract (int argc, char **argv) {
   
-  int i, j, m, N, NPTS, param;
+  int i, j, n, m, Npts, NPTS, last, Nfields, Nreturn, Ncstack, Nstack;
   int Nsecfilt, mode;
   char *RegionName, *RegionList, *p;
+  char **cstack;
+  float *values;
 
   Catalog catalog;
   SkyList *skylist;
   PhotCode *code;
-  Vector *vec;
+  Vector **vec;
+
+  dbField *fields;
+  dbStack *stack;
 
   /* defaults */
@@ -24,31 +29,36 @@
   Nsecfilt = GetPhotcodeNsecfilt ();
 
-  /* interpret command-line options */
-  SetSelectionParam (0);
+  // remove skyregion options
+  // XXX this needs to explicitly handle -qregion and -skyregion
   if (!SetRegionSelection (&argc, argv, &RegionName, &RegionList)) goto escape;
-  if (!SetPhotSelections (&argc, argv, 1)) goto usage;
 
-  /* interpret required command-line arguments: mextract (value) */
-  if (argc != 2) { goto usage; }
-  param = GetAverageParam (argv[1]);
-  if (param == AVE_ZERO) {
-    if (!GetPhotcodeInfo (argv[1], &code, &mode)) {
-      GetAverageParamHelp ();
-      goto escape;
-    }
-    param = AVE_MAG;
-    for (p = argv[1]; *p != 0; p++) { 
-      if (*p == '.') *p = ':';
-    }
-  } 
-  if (!TestPhotSelections (&code, &mode, param)) goto escape;
+  // command-line is of the form: avextract field,field, field [where (field op value)...]
+
+  // parse the fields to be extracted and returned
+  fields = dbCmdlineFields (argc, argv, DVO_TABLE_AVERAGE, &last, &Nfields);
+  if (fields == NULL) return (FALSE);
+
+  // parse the remainder of the line as a boolean math expression
+  cstack = isolate_elements (argc-last, &argv[last], &Ncstack);
+  
+  // construct the db Boolean math stack (frees cstack)
+  stack = dbRPN (Ncstack, cstack, &Nstack);
+
+  Nreturn = Nfields; 
+  dbCheckStack (stack, Nstack, DVO_TABLE_AVERAGE, &fields, &Nfields);
+  // XXX handle errors
 
   /* load region corresponding to selection above */
   if ((skylist = SelectRegions (RegionName, RegionList)) == NULL) goto escape;
 
-  /* create storage vector */
-  N = 0;
+  /* create output storage vectors */
+  ALLOCATE (values, float, Nfields);
+  ALLOCATE (vec, Vector *, Nreturn);
+  for (i = 0; i < Nreturn; i++) {
+    if ((vec[i] = SelectVector (fields[i].name, ANYVECTOR, TRUE)) == NULL) goto escape;
+  }
+
+  Npts = 0;
   NPTS = 1;
-  if ((vec = SelectVector (argv[1], ANYVECTOR, TRUE)) == NULL) goto escape;
 
   // XXX need to add interrupt test to this loop
@@ -68,12 +78,30 @@
     for (j = 0; j < catalog.Naverage; j++) {
       m = catalog.average[j].offset;
-      vec[0].elements[N] = ExtractAverages (code, mode, &catalog.average[j], &catalog.secfilt[j*Nsecfilt], &catalog.measure[m], param);
-      N++;
-      CHECK_REALLOCATE (vec[0].elements, float, NPTS, N, 2000);
+      // extract the relevant values
+      // XXX for measure values, this could be optimized for one loop over measures...
+      for (n = 0; n < Nfields; n++) {
+	values[n] = dbExtractAverages (&catalog.average[j], &catalog.secfilt[j*Nsecfilt], &catalog.measure[m], &fields[n]);
+      }
+      // test the conditional statement
+      if (!dbBooleanCond (stack, Nstack, values)) continue;
+      for (n = 0; n < Nreturn; n++) {
+	vec[n][0].elements[Npts] = values[n];
+      }
+      Npts++;
+      if (Npts >= NPTS) {
+	NPTS += 2000;
+	for (n = 0; n < Nreturn; n++) {
+	  REALLOCATE (vec[n][0].elements, float, NPTS);
+	}
+      }
     }
     dvo_catalog_free (&catalog);
   }
-  vec[0].Nelements = N;
-  REALLOCATE (vec[0].elements, float, MAX(1,N));
+  for (n = 0; n < Nreturn; n++) {
+    vec[n][0].Nelements = Npts;
+    REALLOCATE (vec[n][0].elements, float, MAX(1,Npts));
+  }
+
+  // XXX free fields and stack 
 
   SkyListFree (skylist, ((RegionName != NULL) || (RegionList != NULL)));
Index: trunk/Ohana/src/opihi/dvo/catalog.c
===================================================================
--- trunk/Ohana/src/opihi/dvo/catalog.c	(revision 13344)
+++ trunk/Ohana/src/opihi/dvo/catalog.c	(revision 13479)
@@ -28,6 +28,5 @@
   int Ngraph;
 
-  Ngraph = 0;
-  if (!GetGraph (&graphmode, NULL, &Ngraph)) return (FALSE);
+  if (!GetGraph (&graphmode, NULL, NULL)) return (FALSE);
 
   VarConfig ("GSCDIR", "%s", gscdir);
@@ -392,7 +391,7 @@
     PrepPlotting (N, &graphmode);
     
-    PlotVector (N, Xvec.elements);
-    PlotVector (N, Yvec.elements);
-    PlotVector (N, Zvec.elements);
+    PlotVector (N, Xvec.elements, "x");
+    PlotVector (N, Yvec.elements, "y");
+    PlotVector (N, Zvec.elements, "z");
     
     free (Xvec.elements);
Index: trunk/Ohana/src/opihi/dvo/cmpload.c
===================================================================
--- trunk/Ohana/src/opihi/dvo/cmpload.c	(revision 13344)
+++ trunk/Ohana/src/opihi/dvo/cmpload.c	(revision 13479)
@@ -8,19 +8,20 @@
   int i, Noverlay, NOVERLAY, Nstar, N, Nin, Nextra, Objtype, type;
   int doneread, done, Nskip, Nbytes, nbytes, Ninstar;
-  char *c, *c2;
+  char *c, *c2, *name;
   double dtmp;
   FILE *f;
   char *buffer;
-  int Ximage, Nimage;
+  int kapa;
   Header header;
   KiiOverlay *overlay;
   
-  Nimage = -1;
+  name = NULL;
   if ((N = get_argument (argc, argv, "-n"))) {
     remove_argument (N, &argc, argv);
-    Nimage = atof (argv[N]);
+    name = strcreate (argv[N]);
     remove_argument (N, &argc, argv);
   }
-  if (!GetImage (&Ximage, &Nimage)) return (FALSE);
+  if (!GetImage (NULL, &kapa, name)) return (FALSE);
+  FREE (name);
 
   Objtype = 0;
@@ -131,5 +132,5 @@
   fclose (f);
 
-  KiiLoadOverlay (Ximage, overlay, Noverlay, argv[1]);
+  KiiLoadOverlay (kapa, overlay, Noverlay, argv[1]);
   free (overlay);
 
Index: trunk/Ohana/src/opihi/dvo/dbBooleanCond.c
===================================================================
--- trunk/Ohana/src/opihi/dvo/dbBooleanCond.c	(revision 13479)
+++ trunk/Ohana/src/opihi/dvo/dbBooleanCond.c	(revision 13479)
@@ -0,0 +1,81 @@
+# include "dvoshell.h"
+
+// evaluate the expression in inStack as a boolean; necessary db field values are
+// supplied by fields, in order 0 - Nfields (validate before calling)
+int dbBooleanCond (dbStack *inStack, int NinStack, float *fields) {
+  
+  float value;
+  int i, j, N, Nstack;
+  dbStack **stack, *output;
+
+  // 'no stack' means 'no where statement'
+  if (NinStack == 0) return (TRUE);
+
+  Nstack = NinStack;
+  ALLOCATE (stack, dbStack *, NinStack);
+  for (i = 0; i < NinStack; i++) {
+    stack[i] = &inStack[i];
+  }
+
+  for (i = 0; i < Nstack; i++) {
+
+    /***** binary operators *****/
+    if ((stack[i][0].type >= 3) && (stack[i][0].type <= 8)) {
+
+      // pre-test that op and entries match
+      output = dbBinary (stack[i-2], stack[i-1], stack[i][0].name, fields); 
+
+      // free temporary stack items, drop external items
+      dbFreeEntry (stack[i-2]);
+      dbFreeEntry (stack[i-1]);
+
+      stack[i-2] = output;
+      for (j = i + 1; j < Nstack; j++) {
+	stack[j-2] = stack[j];
+      }
+
+      Nstack -= 2;
+      i -= 2;
+      continue;
+    }
+
+    /***** unary operators **/
+    if (stack[i][0].type == 9) {
+
+      // pre-test that op and entries match
+      output = dbUnary (stack[i-1], stack[i][0].name, fields); 
+
+      // free temporary stack items, drop external items
+      dbFreeEntry (stack[i-1]);
+
+      stack[i-1] = output;
+      for (j = i + 1; j < Nstack; j++) {
+	stack[j-1] = stack[j];
+      }
+
+      Nstack -= 1;
+      i -= 1;
+      continue;
+    } 
+  }
+
+  // the result here is a single stack entry with a value:
+  if (stack[0][0].type == 'F') {
+    N = stack[0][0].field;
+    value = fields[N];
+  } else {
+    value = stack[0][0].Float;
+  }
+    
+  free (stack);
+
+  // XXX fix this limit
+  if (fabs(value) > 1e-7) {
+    return (TRUE);
+  } else {
+    return (FALSE);
+  }
+
+  // pre-test that op and entries match
+  return (TRUE);
+}
Index: trunk/Ohana/src/opihi/dvo/dbCheckStack.c
===================================================================
--- trunk/Ohana/src/opihi/dvo/dbCheckStack.c	(revision 13479)
+++ trunk/Ohana/src/opihi/dvo/dbCheckStack.c	(revision 13479)
@@ -0,0 +1,57 @@
+# include "dvoshell.h"
+
+int dbCheckStack (dbStack *stack, int Nstack, int table, dbField **inFields, int *Nfields) {
+
+  int i, j, status, NFIELDS;
+  char *c;
+  dbField *fields;
+
+  NFIELDS = *Nfields + 10;
+  REALLOCATE (*inFields, dbField, NFIELDS);
+  fields = *inFields;
+
+  for (i = 0; i < Nstack; i++) {
+    if (stack[i].type == 'X') {
+
+      /** if this is a number, put it on the list of scalers and move on **/
+      stack[i].Float = strtod (stack[i].name, &c);
+      if (c == stack[i].name + strlen (stack[i].name)) {
+	stack[i].type  = 'S';
+	continue;
+      } 
+
+      // this must be a field : is it already in the list?
+      for (j = 0; (j < *Nfields) && strcasecmp (stack[i].name, fields[j].name); j++);
+      if (j < *Nfields) {
+	stack[i].field = j;
+	stack[i].type  = 'F';
+	continue;
+      }
+
+      // this must be a field : is it a valid name?
+      if (table == DVO_TABLE_MEASURE) {
+	status = ParseMeasureField (&fields[*Nfields], stack[i].name);
+      } 
+      if (table == DVO_TABLE_AVERAGE) {
+	status = ParseAverageField (&fields[*Nfields], stack[i].name);
+      } 
+      if (!status) {
+	gprint (GP_ERR, "unknown database field %s\n", stack[i].name);
+	return (FALSE);
+      }
+      stack[i].field = *Nfields;
+      stack[i].type  = 'F';
+
+      *Nfields ++;
+      CHECK_REALLOCATE (*inFields, dbField, NFIELDS, *Nfields, 10);
+      fields = *inFields;
+    }
+  }
+  return (TRUE);
+}
+
+/* check stack identifies the data elements as plain scalars or table fields
+   operators have already been identified.  
+   check stack returns the total stack dimensionality (0,1,2)
+   on error, check stack returns FALSE
+*/
Index: trunk/Ohana/src/opihi/dvo/dbCmdlineFields.c
===================================================================
--- trunk/Ohana/src/opihi/dvo/dbCmdlineFields.c	(revision 13479)
+++ trunk/Ohana/src/opihi/dvo/dbCmdlineFields.c	(revision 13479)
@@ -0,0 +1,74 @@
+# include "dvoshell.h"
+
+// identify the fields to be extracted (test for where, check syntax)
+dbField *dbCmdlineFields (int argc, char **argv, int table, int *last, int *nfields) {
+
+  int i, j, status, Nfields, NFIELDS;
+  char *p, *q, *field;
+  dbField *fields;
+
+  *nfields = 0;
+  Nfields = 0;
+  NFIELDS = 10;
+  ALLOCATE (fields, dbField, NFIELDS);
+  dbInitField (&fields[0]);
+
+  // examine each argv[i] entry until we reach a where 
+  for (i = 1; (i < argc) && strcasecmp (argv[i], "where"); i++) {
+    // split the word by ","
+    p = argv[i];
+    while (*p) {
+      q = strchr (p, ',');
+      if (q == NULL) {
+	field = strcreate (p);
+	p = p + strlen(p);
+      } else {
+	field = strncreate (p, q-p);
+	p = q + 1;
+      }
+      // identify field for word
+      // need to know which type of fields to look for...
+      // xxx extend this more generally later
+      if (table == DVO_TABLE_MEASURE) {
+	status = ParseMeasureField (&fields[Nfields], field);
+      } 
+      if (table == DVO_TABLE_AVERAGE) {
+	status = ParseAverageField (&fields[Nfields], field);
+      } 
+      if (!status) {
+	gprint (GP_ERR, "unknown database field %s\n", field);
+	free (field);
+	for (j = 0; j <= Nfields; j++) {
+	  if (fields->name != NULL) free (fields->name);
+	}
+	free (fields);
+	return (NULL);
+      }
+      free (field);
+
+      Nfields ++;
+      CHECK_REALLOCATE (fields, dbField, NFIELDS, Nfields, 10);
+      dbInitField (&fields[Nfields]);
+    }
+  }
+
+  // require 'where' before boolean math
+  if (i != argc) {
+    if (strcasecmp(argv[i], "where")) {
+      gprint (GP_ERR, "syntax error\n");
+      free (fields);
+      return (NULL);
+    }
+    if (i > argc - 2) {
+      gprint (GP_ERR, "missing boolean expression\n");
+      free (fields);
+      return (NULL);
+    }
+    i++; // skip over the 'where'
+  }
+
+  *last = i;
+  *nfields = Nfields;
+
+  return (fields);
+}
Index: trunk/Ohana/src/opihi/dvo/dbExtractAverages.c
===================================================================
--- trunk/Ohana/src/opihi/dvo/dbExtractAverages.c	(revision 13479)
+++ trunk/Ohana/src/opihi/dvo/dbExtractAverages.c	(revision 13479)
@@ -0,0 +1,112 @@
+# include "dvoshell.h"
+
+/* return average.field based on the selection */
+double dbExtractAverages (Average *average, SecFilt *secfilt, Measure *measure, dbField *field) {
+
+  int i;
+  double value;
+
+  value = NO_MAG;
+
+  /* assign vector values */
+  switch (field->ID) {
+    case AVE_RA:
+      value = average[0].R;
+      break;
+    case AVE_DEC:
+      value = average[0].D;
+      break;
+    case AVE_RA_ERR:
+      value = average[0].dR;
+      break;
+    case AVE_DEC_ERR:
+      value = average[0].dD;
+      break;
+
+    case AVE_U_RA:
+      value = average[0].uR;
+      break;
+    case AVE_U_DEC:
+      value = average[0].uD;
+      break;
+    case AVE_U_RA_ERR:
+      value = average[0].duR;
+      break;
+    case AVE_U_DEC_ERR:
+      value = average[0].duD;
+      break;
+
+    case AVE_PAR:
+      value = average[0].P;
+      break;
+    case AVE_PAR_ERR:
+      value = average[0].dP;
+      break;
+
+    case AVE_NMEAS:
+      value = average[0].Nm;
+      break;
+    case AVE_NMISS:
+      value = average[0].Nn;
+      break;
+    case AVE_Xp:
+      value = 0.01*average[0].Xp;
+      break;
+    case AVE_FLAG:
+      value = average[0].code;
+      break;
+
+    case AVE_MAG:
+      switch (field->magMode) {
+	case MAG_AVE:
+	  value = PhotAve  (field->photcode, average, secfilt);
+	  break;
+	case MAG_REF:
+	  value = PhotRef  (field->photcode, average, secfilt, measure);
+	  break;
+	case MAG_INST:
+	case MAG_CAT:
+	case MAG_SYS:
+	case MAG_REL:
+	case MAG_CAL:
+	  // XXX need to code this correctly: this returns just the first matching value
+	  value = NO_MAG;
+	  for (i = 0; i < average[0].Nm; i++) {
+	    if (field->photcode->code != measure[i].photcode) continue;
+	    value = measure[i].M;
+	    break;
+	  }
+	  break;
+      }
+      break;
+    case AVE_dMAG:
+      value = PhotdM (field->photcode, average, secfilt);
+      break;
+    case AVE_Xm:
+      value = PhotXm (field->photcode, average, secfilt);
+      break;
+    case AVE_TYPE:
+      break;
+    case AVE_TYPEFRAC:
+      break;
+    case AVE_NCODE:
+      value = 0;
+      for (i = 0; i < average[0].Nm; i++) {
+	if (field->photcode->code != GetPhotcodeEquivCodebyCode (measure[i].photcode)) continue;
+	value ++;
+      }
+      break;
+    case AVE_NPHOT:
+      value = 0;
+      for (i = 0; i < average[0].Nm; i++) {
+	if (field->photcode->code != GetPhotcodeEquivCodebyCode (measure[i].photcode)) continue;
+	if (measure[i].flags & (ID_MEAS_POOR | ID_MEAS_SKIP)) continue;
+	value ++;
+      }
+      break;
+    case AVE_NCRIT:
+      break;
+  }
+  return (value);
+}  
+
Index: trunk/Ohana/src/opihi/dvo/dbExtractMeasures.c
===================================================================
--- trunk/Ohana/src/opihi/dvo/dbExtractMeasures.c	(revision 13479)
+++ trunk/Ohana/src/opihi/dvo/dbExtractMeasures.c	(revision 13479)
@@ -0,0 +1,135 @@
+# include "dvoshell.h"
+
+/* time concepts */
+static time_t TimeReference;
+static int TimeFormat;
+
+void dbExtractMeasuresInit () {
+  GetTimeFormat (&TimeReference, &TimeFormat);
+}
+
+/* return measure.field based on the selection */
+double dbExtractMeasures (Average *average, SecFilt *secfilt, Measure *measure, dbField *field) {
+
+  int i;
+  double value;
+  double ra, dec, x, y;
+
+  Image *image;
+  Coords *mosaic;
+
+  value = NO_MAG;
+
+  switch (field->ID) {
+    // XXX not sure how to apply the photcode here....
+    case MEAS_MAG: /* magnitudes are already determined above */
+      switch (field->magMode) {
+	case MAG_INST:
+	  value = PhotInst (measure);  
+	  break;
+	case MAG_CAT:
+	  value = PhotCat  (measure); 
+	  break;
+	case MAG_SYS:
+	  value = PhotSys  (measure, average, secfilt); 
+	  break;
+	case MAG_REL:
+	  value = PhotRel  (measure, average, secfilt); 
+	  break;
+	case MAG_CAL:
+	  value = PhotCal  (measure, average, secfilt, measure, GetPhotcodeEquivbyCode (measure[0].photcode)); 
+	  break;
+	case MAG_AVE:
+	  value = PhotAve  (GetPhotcodeEquivbyCode (measure[0].photcode), average, secfilt); 
+	  break;
+	case MAG_REF:
+	  value = PhotRef  (GetPhotcodeEquivbyCode (measure[0].photcode), average, secfilt, measure); 
+	  break;
+      }
+      break;
+    case MEAS_RA: /* OK */
+      value = average[0].R - measure[0].dR / 3600.0;
+      break;
+    case MEAS_DEC: /* OK */
+      value = average[0].D - measure[0].dD / 3600.0;
+      break;
+    case MEAS_dMAG: /* OK */
+      value = measure[0].dM;
+      break;
+    case MEAS_AIRMASS: /* OK */
+      value = measure[0].airmass;
+      break;
+    case MEAS_EXPTIME: /* OK */
+      value = pow (10.0, measure[0].dt * 0.4);
+      break;
+    case MEAS_PHOTCODE: /* OK */
+      value = measure[0].photcode;
+      break;
+    case MEAS_TIME: /* OK */
+      value = TimeValue (measure[0].t, TimeReference, TimeFormat);
+      break;
+    case MEAS_dR: /* OK */
+      value = measure[0].dR;
+      break;
+    case MEAS_dD: /* OK */
+      value = measure[0].dD;
+      break;
+    case MEAS_FWHM: /* OK */
+      value = 0.01*measure[0].FWx;
+      break;
+    case MEAS_DOPHOT: /* OK */
+      value = measure[0].dophot;
+      break;
+    case MEAS_FLAGS: /* ? */
+      value = measure[0].flags;
+      break;
+    case MEAS_XCCD: /* OK */
+/* I need to perform this conversion for ELIXIR and LONEOS formats on load */      
+# if 0
+      value = measure[0].Xccd;
+# else
+      ra  = average[0].R - measure[0].dR / 3600.0;
+      dec = average[0].D - measure[0].dD / 3600.0;
+      image = MatchImage (measure[0].t, measure[0].photcode);
+      if (image == NULL) break;
+      RD_to_XY (&x, &y, ra, dec, &image[0].coords);
+      value = x;
+# endif
+      break;
+    case MEAS_YCCD: /* OK */
+/* I need to perform this conversion for ELIXIR and LONEOS formats on load */      
+# if 0
+      value = measure[0].Yccd;
+# else
+      ra  = average[0].R - measure[0].dR / 3600.0;
+      dec = average[0].D - measure[0].dD / 3600.0;
+      image = MatchImage (measure[0].t, measure[0].photcode);
+      if (image == NULL) break;
+      RD_to_XY (&x, &y, ra, dec, &image[0].coords);
+      value = y;
+# endif
+      break;
+    case MEAS_XMOSAIC: /* OK */
+      ra  = average[0].R - measure[0].dR / 3600.0;
+      dec = average[0].D - measure[0].dD / 3600.0;
+      mosaic = MatchMosaic (measure[0].t, measure[0].photcode);
+      if (mosaic == NULL) break;
+      RD_to_XY (&x, &y, ra, dec, mosaic);
+      value = x;
+      break;
+    case MEAS_YMOSAIC: /* OK */
+      ra  = average[0].R - measure[0].dR / 3600.0;
+      dec = average[0].D - measure[0].dD / 3600.0;
+      mosaic = MatchMosaic (measure[0].t, measure[0].photcode);
+      if (mosaic == NULL) break;
+      RD_to_XY (&x, &y, ra, dec, mosaic);
+      value = y;
+      break;
+  }
+  return (value);
+}
+
+/** the mosaic entries do not use the registered mosaic found 
+    by MatchImage (via FindMosaicForImage).  Rather, they use
+    a coordinate frame saved by SetImageSelection 
+**/
Index: trunk/Ohana/src/opihi/dvo/dbFields.c
===================================================================
--- trunk/Ohana/src/opihi/dvo/dbFields.c	(revision 13479)
+++ trunk/Ohana/src/opihi/dvo/dbFields.c	(revision 13479)
@@ -0,0 +1,142 @@
+# include "dvoshell.h"
+
+dbInitField (dbField *field) {
+  field->name = NULL;
+  field->extract = FALSE;
+  field->table = 0;
+  field->ID = 0;
+  field->magMode = 0;
+  field->photcode = NULL;
+}
+
+int GetMagMode (char *string) {
+
+  if (!strcasecmp (string, "inst"))  return (MAG_INST);
+  if (!strcasecmp (string, "cat"))   return (MAG_CAT);
+  if (!strcasecmp (string, "sys"))   return (MAG_SYS);
+  if (!strcasecmp (string, "rel"))   return (MAG_REL);
+  if (!strcasecmp (string, "cal"))   return (MAG_CAL);
+  if (!strcasecmp (string, "ave"))   return (MAG_AVE);
+  if (!strcasecmp (string, "ref"))   return (MAG_REF);
+  if (!strcasecmp (string, "err"))   return (MAG_ERR);
+  if (!strcasecmp (string, "chisq")) return (MAG_CHISQ);
+  return (MAG_NONE);
+}
+
+PhotCode *ParsePhotcodeField (char *field, int *mode, int defMode) {
+
+  char *tmpstring, *p;
+  PhotCode *code;
+
+  *mode = defMode;
+
+  p = strchr (field, ':');
+  if (p != NULL) {
+    *mode = GetMagMode (p + 1);
+    if (*mode == MAG_NONE) return (NULL);
+    tmpstring = strncreate (field, p - field);
+  } else {
+    tmpstring = strcreate (field);
+  }
+  code = GetPhotcodebyName (tmpstring);
+  free (tmpstring);
+
+  return (code);
+}
+
+# define ESCAPE(F,M) { \
+  field->ID = (F); \
+  field->magMode = (M); \
+  field->photcode = NULL; \
+  return (TRUE); }
+
+int ParseMeasureField (dbField *field, char *fieldName) {
+
+  int mode;
+  PhotCode *code;
+
+  field->table = DVO_TABLE_MEASURE;
+  field->name  = strcreate (fieldName);
+
+  if (!strcasecmp (fieldName, "RA"))       ESCAPE (MEAS_RA,   	  MAG_NONE);
+  if (!strcasecmp (fieldName, "DEC"))      ESCAPE (MEAS_DEC,  	  MAG_NONE);
+  // if (!strcasecmp (fieldName, "MAG"))      ESCAPE (MEAS_MAG,  	  MAG_REL);
+  // if (!strcasecmp (fieldName, "dMAG"))     ESCAPE (MEAS_dMAG, 	  MAG_ERR);
+  if (!strcasecmp (fieldName, "AIRMASS"))  ESCAPE (MEAS_AIRMASS,  MAG_NONE);
+  if (!strcasecmp (fieldName, "EXPTIME"))  ESCAPE (MEAS_EXPTIME,  MAG_NONE);
+  if (!strcasecmp (fieldName, "PHOTCODE")) ESCAPE (MEAS_PHOTCODE, MAG_NONE);
+  if (!strcasecmp (fieldName, "TIME"))     ESCAPE (MEAS_TIME,     MAG_NONE);
+  if (!strcasecmp (fieldName, "dR"))       ESCAPE (MEAS_dR, 	  MAG_NONE);
+  if (!strcasecmp (fieldName, "dD"))       ESCAPE (MEAS_dD, 	  MAG_NONE);
+  if (!strcasecmp (fieldName, "FWHM"))     ESCAPE (MEAS_FWHM,     MAG_NONE);
+  if (!strcasecmp (fieldName, "DOPHOT"))   ESCAPE (MEAS_DOPHOT,   MAG_NONE);
+  if (!strcasecmp (fieldName, "FLAGS"))    ESCAPE (MEAS_FLAGS,    MAG_NONE);
+  if (!strcasecmp (fieldName, "XCCD"))     ESCAPE (MEAS_XCCD, 	  MAG_NONE);
+  if (!strcasecmp (fieldName, "YCCD"))     ESCAPE (MEAS_YCCD, 	  MAG_NONE);
+  if (!strcasecmp (fieldName, "XMOSAIC"))  ESCAPE (MEAS_XMOSAIC,  MAG_NONE);
+  if (!strcasecmp (fieldName, "YMOSAIC"))  ESCAPE (MEAS_YMOSAIC,  MAG_NONE);
+
+  // for words that don't parse, try a photcode
+
+  // check for code:mode in photcode name 
+  code = ParsePhotcodeField (fieldName, &mode, MAG_REL);
+  if (code == NULL) return (FALSE);
+
+  if (mode == MAG_ERR) {
+    field->ID = MEAS_dMAG;
+  } else {
+    field->ID = MEAS_MAG;
+  }    
+  
+  field->magMode = mode;
+  field->photcode = code;
+  return (TRUE);
+}
+  
+int ParseAverageField (dbField *field, char *fieldName) {
+
+  int mode;
+  PhotCode *code;
+
+  field->table = DVO_TABLE_AVERAGE;
+  field->name  = strcreate (fieldName);
+
+  if (!strcasecmp (fieldName, "RA"))    ESCAPE (AVE_RA,        MAG_NONE);
+  if (!strcasecmp (fieldName, "DEC"))   ESCAPE (AVE_DEC,       MAG_NONE);
+  if (!strcasecmp (fieldName, "dRA"))   ESCAPE (AVE_RA_ERR,    MAG_NONE);
+  if (!strcasecmp (fieldName, "dDEC"))  ESCAPE (AVE_DEC_ERR,   MAG_NONE);
+  if (!strcasecmp (fieldName, "uRA"))   ESCAPE (AVE_U_RA,      MAG_NONE);
+  if (!strcasecmp (fieldName, "uDEC"))  ESCAPE (AVE_U_DEC,     MAG_NONE);
+  if (!strcasecmp (fieldName, "duRA"))  ESCAPE (AVE_U_RA_ERR,  MAG_NONE);
+  if (!strcasecmp (fieldName, "duDEC")) ESCAPE (AVE_U_DEC_ERR, MAG_NONE);
+  if (!strcasecmp (fieldName, "PAR"))   ESCAPE (AVE_PAR,       MAG_NONE);
+  if (!strcasecmp (fieldName, "dPAR"))  ESCAPE (AVE_PAR_ERR,   MAG_NONE);
+  if (!strcasecmp (fieldName, "MAG"))   ESCAPE (AVE_MAG,       MAG_AVE);
+  if (!strcasecmp (fieldName, "dMAG"))  ESCAPE (AVE_dMAG,      MAG_ERR);
+  if (!strcasecmp (fieldName, "NMEAS")) ESCAPE (AVE_NMEAS,     MAG_NONE);
+  if (!strcasecmp (fieldName, "NMISS")) ESCAPE (AVE_NMISS,     MAG_NONE);
+  if (!strcasecmp (fieldName, "Xp"))    ESCAPE (AVE_Xp,        MAG_NONE);
+  if (!strcasecmp (fieldName, "Xm"))    ESCAPE (AVE_Xm,        MAG_CHISQ);
+  if (!strcasecmp (fieldName, "FLAG"))  ESCAPE (AVE_FLAG,      MAG_NONE);
+  if (!strcasecmp (fieldName, "TYPE"))  ESCAPE (AVE_TYPE,      MAG_NONE);
+  if (!strcasecmp (fieldName, "NPHOT")) ESCAPE (AVE_NPHOT,     MAG_NONE);
+  if (!strcasecmp (fieldName, "NCODE")) ESCAPE (AVE_NCODE,     MAG_NONE);
+  if (!strcasecmp (fieldName, "NCRIT")) ESCAPE (AVE_NCRIT,     MAG_NONE);
+
+  // for words that don't parse, try a photcode
+
+  // check for code:mode in photcode name 
+  code = ParsePhotcodeField (fieldName, &mode, MAG_AVE);
+  if (code == NULL) return (FALSE);
+
+  // need to distinguish phot, sys errors and scatter
+  if (mode == MAG_ERR) {
+    field->ID = AVE_dMAG;
+  } else {
+    field->ID = AVE_MAG;
+  }    
+  
+  field->magMode = mode;
+  field->photcode = code;
+  return (TRUE);
+}
Index: trunk/Ohana/src/opihi/dvo/dbRPN.c
===================================================================
--- trunk/Ohana/src/opihi/dvo/dbRPN.c	(revision 13479)
+++ trunk/Ohana/src/opihi/dvo/dbRPN.c	(revision 13479)
@@ -0,0 +1,210 @@
+# include "dvoshell.h"
+# define DUMPSTACK 0
+
+dbStack *dbRPN (int argc, char **argv, int *nstack) {
+  
+  int type, Nx, Ny;
+  int i, j, Nstack, Nop_stack, NSTACK;
+  dbStack *stack, *op_stack;
+
+  /* max total stack size is argc, though should be less, this is safe */
+  NSTACK = argc + 5;
+  ALLOCATE (stack, dbStack, NSTACK);
+  ALLOCATE (op_stack, dbStack, NSTACK);
+  for (i = 0; i < NSTACK; i++) {
+    dbInitStack (&stack[i]);
+    dbInitStack (&op_stack[i]);
+  }
+  
+  Nx = Ny = Nstack = Nop_stack = 0;
+  for (i = 0; i < argc; i++) {
+    
+    /* decide on priority of object */
+    type = 0;
+    /* unary operations */
+    if (!strcmp (argv[i], "abs"))    { type = 9; goto gotit; }
+    if (!strcmp (argv[i], "int"))    { type = 9; goto gotit; }
+    if (!strcmp (argv[i], "exp"))    { type = 9; goto gotit; }
+    if (!strcmp (argv[i], "ten"))    { type = 9; goto gotit; }
+    if (!strcmp (argv[i], "log"))    { type = 9; goto gotit; }
+    if (!strcmp (argv[i], "ln"))     { type = 9; goto gotit; }
+    if (!strcmp (argv[i], "sqrt"))   { type = 9; goto gotit; }
+    if (!strcmp (argv[i], "erf"))    { type = 9; goto gotit; }
+
+    if (!strcmp (argv[i], "sinh"))   { type = 9; goto gotit; }
+    if (!strcmp (argv[i], "cosh"))   { type = 9; goto gotit; }
+    if (!strcmp (argv[i], "asinh"))  { type = 9; goto gotit; }
+    if (!strcmp (argv[i], "acosh"))  { type = 9; goto gotit; }
+
+    if (!strcmp (argv[i], "sin"))    { type = 9; goto gotit; }
+    if (!strcmp (argv[i], "cos"))    { type = 9; goto gotit; }
+    if (!strcmp (argv[i], "tan"))    { type = 9; goto gotit; }
+    if (!strcmp (argv[i], "dsin"))   { type = 9; goto gotit; }
+    if (!strcmp (argv[i], "dcos"))   { type = 9; goto gotit; }
+    if (!strcmp (argv[i], "dtan"))   { type = 9; goto gotit; }
+    if (!strcmp (argv[i], "asin"))   { type = 9; goto gotit; }
+    if (!strcmp (argv[i], "acos"))   { type = 9; goto gotit; }
+    if (!strcmp (argv[i], "atan"))   { type = 9; goto gotit; }
+    if (!strcmp (argv[i], "dasin"))  { type = 9; goto gotit; }
+    if (!strcmp (argv[i], "dacos"))  { type = 9; goto gotit; }
+    if (!strcmp (argv[i], "datan"))  { type = 9; goto gotit; }
+
+    if (!strcmp (argv[i], "lgamma")) { type = 9; goto gotit; }
+
+    if (!strcmp (argv[i], "rnd"))    { type = 9; goto gotit; }
+    if (!strcmp (argv[i], "xramp"))  { type = 9; goto gotit; }
+    if (!strcmp (argv[i], "yramp"))  { type = 9; goto gotit; }
+    if (!strcmp (argv[i], "ramp"))   { type = 9; goto gotit; }
+    if (!strcmp (argv[i], "zero"))   { type = 9; goto gotit; }
+    if (!strcmp (argv[i], "--"))     { type = 9; goto gotit; }
+    if (!strcmp (argv[i], "not"))    { type = 9; goto gotit; }
+    if (!strcmp (argv[i], "isinf"))  { type = 9; goto gotit; }
+    if (!strcmp (argv[i], "isnan"))  { type = 9; goto gotit; }
+
+    /* binary operations */
+    if (!strcmp (argv[i], "^"))      { type = 8; goto gotit; }
+
+    if (!strcmp (argv[i], "@"))      { type = 7; goto gotit; }
+    if (!strcmp (argv[i], "/"))      { type = 7; goto gotit; }
+    if (!strcmp (argv[i], "*"))      { type = 7; goto gotit; }
+    if (!strcmp (argv[i], "%"))      { type = 7; goto gotit; }
+
+    if (!strcmp (argv[i], "+"))      { type = 6; goto gotit; }
+    if (!strcmp (argv[i], "-"))      { type = 6; goto gotit; }
+	
+    if (!strcmp (argv[i], "&"))      { type = 5; goto gotit; }
+    if (!strcmp (argv[i], "|"))      { type = 5; goto gotit; }
+
+    if (!strcmp (argv[i], "<"))      { type = 4; goto gotit; }
+    if (!strcmp (argv[i], ">"))      { type = 4; goto gotit; }
+    if (!strcmp (argv[i], "=="))     { type = 4; strcpy (argv[i], "E"); goto gotit; }
+    if (!strcmp (argv[i], "!="))     { type = 4; strcpy (argv[i], "N"); goto gotit; }
+    if (!strcmp (argv[i], "<="))     { type = 4; strcpy (argv[i], "L"); goto gotit; }
+    if (!strcmp (argv[i], ">="))     { type = 4; strcpy (argv[i], "G"); goto gotit; }
+    if (!strcmp (argv[i], ">>"))     { type = 4; strcpy (argv[i], "U"); goto gotit; }
+    if (!strcmp (argv[i], "<<"))     { type = 4; strcpy (argv[i], "D"); goto gotit; }
+
+    if (!strcmp (argv[i], "&&"))     { type = 3; strcpy (argv[i], "A"); goto gotit; }
+    if (!strcmp (argv[i], "||"))     { type = 3; strcpy (argv[i], "O"); goto gotit; }
+
+    if (!strcmp (argv[i], "("))      { type = 2; goto gotit; }
+    if (!strcmp (argv[i], ")"))      { type = 1; goto gotit; }
+
+  gotit:
+    /* choose how to deal with object */
+    switch (type) {
+      case 8:  /* exponentiation: 2^2^3 = 64 != 256 (precedence is right-to-left, not left-to-right!) */
+	/* pop previous, higher operators from OP stack to stack */
+	for (j = Nop_stack - 1; (j >= 0) && (op_stack[j].type > type); j--) {
+	  stack[Nstack] = op_stack[j];
+	  op_stack[j].name = NULL;
+	  Nstack ++;
+	  Nop_stack --;
+	}
+	/* push operator on OP stack */
+	op_stack[Nop_stack].name = strcreate (argv[i]);
+	op_stack[Nop_stack].type = type;
+	Nop_stack ++;
+	break;
+      case 9: /* unary OPs */
+      case 7: /* binary OPs */
+      case 6:
+      case 5: 
+      case 4: 
+      case 3: 
+	/* pop previous, higher or equal operators from OP stack to stack */
+	for (j = Nop_stack - 1; (j >= 0) && (op_stack[j].type >= type); j--) {
+	  stack[Nstack] = op_stack[j];
+	  op_stack[j].name = NULL;
+	  Nstack ++;
+	  Nop_stack --;
+	}
+	/* push operator on OP stack */
+	op_stack[Nop_stack].name = strcreate (argv[i]);
+	op_stack[Nop_stack].type = type;
+	Nop_stack ++;
+	break;
+      case 2:  
+	/* push operator on OP stack */
+	op_stack[Nop_stack].name = strcreate (argv[i]);
+	op_stack[Nop_stack].type = type;
+	Nop_stack ++;
+	break;
+      case 1: 
+	/* pop rest of operators from OP stack to stack, looking for '(' */
+	for (j = Nop_stack - 1; (j >= 0) && (op_stack[j].type != 2); j--) {
+	  stack[Nstack] = op_stack[j];
+	  op_stack[j].name = NULL;
+	  Nstack ++;
+	  Nop_stack --;
+	}
+	if ((j == -1) || (op_stack[j].type != 2)) {
+	  push_error ("syntax error: mismatched parenthesis");
+	  Nstack = 0;
+	  goto cleanup;
+	}
+	Nop_stack --;
+	break;
+      case 0:
+	/* place the value (number or vector/matrix name) on stack */
+	/* value of 'X' is used as sentinel until we sort out values */
+	stack[Nstack].name = strcreate (argv[i]);
+	stack[Nstack].type = 'X';
+	Nstack ++;
+	break;
+    }
+  }
+
+  /* dump remaining operators on stack, checking for ')' */
+  for (j = Nop_stack - 1; j >= 0; j--) {
+    if (op_stack[j].type == 2) {
+      push_error ("syntax error: mismatched parenthesis");
+      Nstack = 0;
+      goto cleanup;
+    }
+    stack[Nstack] = op_stack[j];
+    op_stack[j].name = NULL;
+    Nstack ++;
+  }
+
+cleanup: 
+  /*** free up unused stack space ***/
+  dbFreeStack (op_stack, NSTACK);
+  free (op_stack);
+  dbFreeStack (&stack[Nstack], NSTACK - Nstack);
+  REALLOCATE (stack, dbStack, MAX (Nstack, 1));
+  *nstack = Nstack;
+
+  for (i = 0; i < argc; i++) {
+    free (argv[i]);
+  }
+  free (argv);
+
+# if (DUMPSTACK)
+  for (i = 0; i < Nstack; i++) {
+    gprint (GP_ERR, "%s ", stack[i].name);
+  }
+  if (Nstack > 0) gprint (GP_ERR, "\n");
+  for (i = 0; i < Nstack; i++) {
+    gprint (GP_ERR, "%d ", stack[i].type);
+  }
+  if (Nstack > 0) gprint (GP_ERR, "\n");
+# endif
+
+  return (stack);
+
+}
+
+/* here are the rules for parsing a math AOL expression to RPN:
+
+1) if object is a number, push on stack
+2) if object is a third order operand (exp, sin, cos), push on op stack
+3) if object is an open paren, push on op stack,
+4) if object is a second order operand, push on stack
+5) if object is a first order operand, pop all second order operands from stack 
+until paren, push on stack
+6) if object is an end paren, pop all objects from stack until paren, 
+pop next stack, if third order op
+7) if end of line, pop all remaining objects, second order first, etc.
+   
+*/
Index: trunk/Ohana/src/opihi/dvo/dbStackMath.c
===================================================================
--- trunk/Ohana/src/opihi/dvo/dbStackMath.c	(revision 13479)
+++ trunk/Ohana/src/opihi/dvo/dbStackMath.c	(revision 13479)
@@ -0,0 +1,140 @@
+# include "dvoshell.h"
+
+dbStack *dbBinary (dbStack *V1, dbStack *V2, char *op, float *fields) {
+
+  int N;
+  float M1, M2;
+  dbStack *OUT;
+  
+  if (V1->type == 'F') {
+    N = V1->field;
+    M1 = fields[N];
+  } else {
+    M1 = V1->Float;
+  }
+  
+  if (V2->type == 'F') {
+    N = V2->field;
+    M2 = fields[N];
+  } else {
+    M2 = V2->Float;
+  }
+
+  ALLOCATE (OUT, dbStack, 1);
+  OUT->type = 'T';
+
+  // use an enum for the op...
+  switch (op[0]) { 
+  case '+': 
+    OUT->Float = M1 + M2;
+    break; 
+  case '-': 
+    OUT->Float = M1 - M2;
+    break; 
+  case '*': 
+    OUT->Float = M1 * M2;
+    break; 
+  case '/': 
+    OUT->Float = M1 / M2;
+    break; 
+  case '%': 
+    OUT->Float = (int) M1 % (int) M2;
+    break; 
+  case 0x5e: 
+    OUT->Float = pow (M1, M2);
+    break; 
+  case 'D': 
+    OUT->Float = MIN (M1, M2);
+    break; 
+  case 'U': 
+    OUT->Float = MAX (M1, M2);
+    break; 
+  case '<': 
+    OUT->Float = (M1 < M2) ? 1 : 0;
+    break; 
+  case '>': 
+    OUT->Float = (M1 > M2) ? 1 : 0;
+    break; 
+  case '&': 
+    OUT->Float = ((int)M1 & (int)M2);
+    break; 
+  case '|': 
+    OUT->Float = ((int)M1 | (int)M2);
+    break; 
+  case 'E': 
+    OUT->Float = (M1 == M2) ? 1 : 0;
+    break; 
+  case 'N': 
+    OUT->Float = (M1 != M2) ? 1 : 0;
+    break; 
+  case 'L': 
+    OUT->Float = (M1 <= M2) ? 1 : 0;
+    break; 
+  case 'G': 
+    OUT->Float = (M1 >= M2) ? 1 : 0;
+    break; 
+  case 'A': 
+    OUT->Float = (M1 && M2) ? 1 : 0;
+    break; 
+  case 'O': 
+    OUT->Float = (M1 || M2) ? 1 : 0;
+    break; 
+  default:
+    return (NULL);
+  }
+
+  return (OUT);
+}
+
+dbStack *dbUnary (dbStack *V1, char *op, float *fields) {
+
+  int N;
+  float M1;
+  dbStack *OUT;
+
+  if (V1->type == 'F') {
+    N = V1->field;
+    M1 = fields[N];
+  } else {
+    M1 = V1->Float;
+  }
+  
+  ALLOCATE (OUT, dbStack, 1);
+  OUT->type = 'T';
+
+  if (!strcmp (op, "="))      {   OUT->Float = M1;                 }
+  if (!strcmp (op, "abs"))    {   OUT->Float = fabs(M1);           }
+  if (!strcmp (op, "int"))    {   OUT->Float = (float)(int)(M1);   }
+  if (!strcmp (op, "exp"))    {   OUT->Float = exp (M1);           }
+  if (!strcmp (op, "ten"))    {   OUT->Float = pow (10.0,M1);      }
+  if (!strcmp (op, "log"))    {   OUT->Float = log10 (M1);         }
+  if (!strcmp (op, "ln"))     {   OUT->Float = log (M1);           }
+  if (!strcmp (op, "sqrt"))   {   OUT->Float = sqrt (M1);          }
+  if (!strcmp (op, "erf"))    {   OUT->Float = erf (M1);           }
+			      			      
+  if (!strcmp (op, "sinh"))   {   OUT->Float = sinh (M1);          }
+  if (!strcmp (op, "cosh"))   {   OUT->Float = cosh (M1);          }
+  if (!strcmp (op, "asinh"))  {   OUT->Float = asinh (M1);         }
+  if (!strcmp (op, "acosh"))  {   OUT->Float = acosh (M1);         }
+  if (!strcmp (op, "lgamma")) {   OUT->Float = lgamma (M1);        }
+
+  if (!strcmp (op, "sin"))    {   OUT->Float = sin (M1);           }
+  if (!strcmp (op, "cos"))    {   OUT->Float = cos (M1);           }
+  if (!strcmp (op, "tan"))    {   OUT->Float = tan (M1);           }
+  if (!strcmp (op, "dsin"))   {   OUT->Float = sin (M1*RAD_DEG);   }
+  if (!strcmp (op, "dcos"))   {   OUT->Float = cos (M1*RAD_DEG);   }
+  if (!strcmp (op, "dtan"))   {   OUT->Float = tan (M1*RAD_DEG);   }
+  if (!strcmp (op, "asin"))   {   OUT->Float = asin (M1);          }
+  if (!strcmp (op, "acos"))   {   OUT->Float = acos (M1);          }
+  if (!strcmp (op, "atan"))   {   OUT->Float = atan (M1);          }
+  if (!strcmp (op, "dasin"))  {   OUT->Float = asin (M1)*DEG_RAD;  }
+  if (!strcmp (op, "dacos"))  {   OUT->Float = acos (M1)*DEG_RAD;  }
+  if (!strcmp (op, "datan"))  {   OUT->Float = atan (M1)*DEG_RAD;  }
+  if (!strcmp (op, "rnd"))    {   OUT->Float = drand48();          }
+  if (!strcmp (op, "not"))    {   OUT->Float = !(M1);              }
+  if (!strcmp (op, "--"))     {   OUT->Float = - (M1);             }
+  if (!strcmp (op, "isinf"))  {   OUT->Float = !finite(M1);        }
+  if (!strcmp (op, "isnan"))  {   OUT->Float = isnan(M1);          } 
+
+  return (OUT);
+}
Index: trunk/Ohana/src/opihi/dvo/dbStackOps.c
===================================================================
--- trunk/Ohana/src/opihi/dvo/dbStackOps.c	(revision 13479)
+++ trunk/Ohana/src/opihi/dvo/dbStackOps.c	(revision 13479)
@@ -0,0 +1,26 @@
+# include "dvoshell.h"
+
+void dbInitStack (dbStack *stack) {
+  stack[0].type   = 0;
+  stack[0].name   = NULL;
+}
+
+// free data for stack entries (free stack explicitly)
+void dbFreeStack (dbStack *stack, int Nstack) {
+
+  int i;
+
+  for (i = 0; i < Nstack; i++) {
+    if (stack[i].name != NULL) {
+      free (stack[i].name);
+      stack[i].name = NULL;
+    }
+  }
+}
+
+/* delete name and data */
+void dbFreeEntry (dbStack *stack) {
+
+  if (stack->type != 'T') return;
+  free (stack);
+}
Index: trunk/Ohana/src/opihi/dvo/db_check_stack.c
===================================================================
--- trunk/Ohana/src/opihi/dvo/db_check_stack.c	(revision 13479)
+++ trunk/Ohana/src/opihi/dvo/db_check_stack.c	(revision 13479)
@@ -0,0 +1,54 @@
+# include "opihi.h"
+
+int db_check_stack (dvoStack *stack, int Nstack, int table, dvoField *fields, int *Nfields) {
+
+  int i, Nx, Ny, Nv, size;
+  char *c;
+
+  NFIELDS = *Nfields + 10;
+  REALLOCATE (fields, dvoField, NFIELDS);
+
+  for (i = 0; i < Nstack; i++) {
+    if (stack[i].type == 'X') {
+
+      /** if this is a number, put it on the list of scalers and move on **/
+      stack[i].Float = strtod (stack[i].name, &c);
+      if (c == stack[i].name + strlen (stack[i].name)) {
+	stack[i].type  = 'S';
+	continue;
+      } 
+
+      // this must be a field : is it already in the list?
+      for (j = 0; (j < *Nfields) && strcasecmp (stack[i].name, fields[j].name); j++);
+      if (j < *Nfields) {
+	stack[i].field = j;
+	stack[i].type  = 'F';
+	continue;
+      }
+
+      // this must be a field : is it a valid name?
+      if (table == DVO_TABLE_MEASURE) {
+	status = ParseMeasureField (&fields[*Nfields], stack[i].name);
+      } 
+      if (table == DVO_TABLE_AVERAGE) {
+	status = ParseAverageField (&fields[*Nfields], stack[i].name);
+      } 
+      if (!status) {
+	gprint (GP_ERR, "unknown database field %s\n", stack[i].name);
+	return (FALSE);
+      }
+      stack[i].field = *Nfields;
+      stack[i].type  = 'F';
+
+      *Nfields ++;
+      CHECK_REALLOCATE (fields, dvoField, NFIELDS, *Nfields, 10);
+    }
+  }
+  return (TRUE);
+}
+
+/* check stack identifies the data elements as plain scalars or table fields
+   operators have already been identified.  
+   check stack returns the total stack dimensionality (0,1,2)
+   on error, check stack returns FALSE
+*/
Index: trunk/Ohana/src/opihi/dvo/db_convert_to_RPN.c
===================================================================
--- trunk/Ohana/src/opihi/dvo/db_convert_to_RPN.c	(revision 13479)
+++ trunk/Ohana/src/opihi/dvo/db_convert_to_RPN.c	(revision 13479)
@@ -0,0 +1,210 @@
+# include "opihi.h"
+# define DUMPSTACK 0
+
+dvoStack *convert_to_RPN (int argc, char **argv, int *nstack) {
+  
+  int type, Nx, Ny;
+  int i, j, Nstack, Nop_stack, NSTACK;
+  dvoStack *stack, *op_stack;
+
+  /* max total stack size is argc, though should be less, this is safe */
+  NSTACK = argc + 5;
+  ALLOCATE (stack, dvoStack, NSTACK);
+  ALLOCATE (op_stack, dvoStack, NSTACK);
+  for (i = 0; i < NSTACK; i++) {
+    init_stack (&stack[i]);
+    init_stack (&op_stack[i]);
+  }
+  
+  Nx = Ny = Nstack = Nop_stack = 0;
+  for (i = 0; i < argc; i++) {
+    
+    /* decide on priority of object */
+    type = 0;
+    /* unary operations */
+    if (!strcmp (argv[i], "abs"))    { type = 9; goto gotit; }
+    if (!strcmp (argv[i], "int"))    { type = 9; goto gotit; }
+    if (!strcmp (argv[i], "exp"))    { type = 9; goto gotit; }
+    if (!strcmp (argv[i], "ten"))    { type = 9; goto gotit; }
+    if (!strcmp (argv[i], "log"))    { type = 9; goto gotit; }
+    if (!strcmp (argv[i], "ln"))     { type = 9; goto gotit; }
+    if (!strcmp (argv[i], "sqrt"))   { type = 9; goto gotit; }
+    if (!strcmp (argv[i], "erf"))    { type = 9; goto gotit; }
+
+    if (!strcmp (argv[i], "sinh"))   { type = 9; goto gotit; }
+    if (!strcmp (argv[i], "cosh"))   { type = 9; goto gotit; }
+    if (!strcmp (argv[i], "asinh"))  { type = 9; goto gotit; }
+    if (!strcmp (argv[i], "acosh"))  { type = 9; goto gotit; }
+
+    if (!strcmp (argv[i], "sin"))    { type = 9; goto gotit; }
+    if (!strcmp (argv[i], "cos"))    { type = 9; goto gotit; }
+    if (!strcmp (argv[i], "tan"))    { type = 9; goto gotit; }
+    if (!strcmp (argv[i], "dsin"))   { type = 9; goto gotit; }
+    if (!strcmp (argv[i], "dcos"))   { type = 9; goto gotit; }
+    if (!strcmp (argv[i], "dtan"))   { type = 9; goto gotit; }
+    if (!strcmp (argv[i], "asin"))   { type = 9; goto gotit; }
+    if (!strcmp (argv[i], "acos"))   { type = 9; goto gotit; }
+    if (!strcmp (argv[i], "atan"))   { type = 9; goto gotit; }
+    if (!strcmp (argv[i], "dasin"))  { type = 9; goto gotit; }
+    if (!strcmp (argv[i], "dacos"))  { type = 9; goto gotit; }
+    if (!strcmp (argv[i], "datan"))  { type = 9; goto gotit; }
+
+    if (!strcmp (argv[i], "lgamma")) { type = 9; goto gotit; }
+
+    if (!strcmp (argv[i], "rnd"))    { type = 9; goto gotit; }
+    if (!strcmp (argv[i], "xramp"))  { type = 9; goto gotit; }
+    if (!strcmp (argv[i], "yramp"))  { type = 9; goto gotit; }
+    if (!strcmp (argv[i], "ramp"))   { type = 9; goto gotit; }
+    if (!strcmp (argv[i], "zero"))   { type = 9; goto gotit; }
+    if (!strcmp (argv[i], "--"))     { type = 9; goto gotit; }
+    if (!strcmp (argv[i], "not"))    { type = 9; goto gotit; }
+    if (!strcmp (argv[i], "isinf"))  { type = 9; goto gotit; }
+    if (!strcmp (argv[i], "isnan"))  { type = 9; goto gotit; }
+
+    /* binary operations */
+    if (!strcmp (argv[i], "^"))      { type = 8; goto gotit; }
+
+    if (!strcmp (argv[i], "@"))      { type = 7; goto gotit; }
+    if (!strcmp (argv[i], "/"))      { type = 7; goto gotit; }
+    if (!strcmp (argv[i], "*"))      { type = 7; goto gotit; }
+    if (!strcmp (argv[i], "%"))      { type = 7; goto gotit; }
+
+    if (!strcmp (argv[i], "+"))      { type = 6; goto gotit; }
+    if (!strcmp (argv[i], "-"))      { type = 6; goto gotit; }
+	
+    if (!strcmp (argv[i], "&"))      { type = 5; goto gotit; }
+    if (!strcmp (argv[i], "|"))      { type = 5; goto gotit; }
+
+    if (!strcmp (argv[i], "<"))      { type = 4; goto gotit; }
+    if (!strcmp (argv[i], ">"))      { type = 4; goto gotit; }
+    if (!strcmp (argv[i], "=="))     { type = 4; strcpy (argv[i], "E"); goto gotit; }
+    if (!strcmp (argv[i], "!="))     { type = 4; strcpy (argv[i], "N"); goto gotit; }
+    if (!strcmp (argv[i], "<="))     { type = 4; strcpy (argv[i], "L"); goto gotit; }
+    if (!strcmp (argv[i], ">="))     { type = 4; strcpy (argv[i], "G"); goto gotit; }
+    if (!strcmp (argv[i], ">>"))     { type = 4; strcpy (argv[i], "U"); goto gotit; }
+    if (!strcmp (argv[i], "<<"))     { type = 4; strcpy (argv[i], "D"); goto gotit; }
+
+    if (!strcmp (argv[i], "&&"))     { type = 3; strcpy (argv[i], "A"); goto gotit; }
+    if (!strcmp (argv[i], "||"))     { type = 3; strcpy (argv[i], "O"); goto gotit; }
+
+    if (!strcmp (argv[i], "("))      { type = 2; goto gotit; }
+    if (!strcmp (argv[i], ")"))      { type = 1; goto gotit; }
+
+  gotit:
+    /* choose how to deal with object */
+    switch (type) {
+      case 8:  /* exponentiation: 2^2^3 = 64 != 256 (precedence is right-to-left, not left-to-right!) */
+	/* pop previous, higher operators from OP stack to stack */
+	for (j = Nop_stack - 1; (j >= 0) && (op_stack[j].type > type); j--) {
+	  strcpy (stack[Nstack].name, op_stack[j].name);
+	  stack[Nstack].type = op_stack[j].type;
+	  Nstack ++;
+	  Nop_stack --;
+	}
+	/* push operator on OP stack */
+	strcpy (op_stack[Nop_stack].name, argv[i]);
+	op_stack[Nop_stack].type = type;
+	Nop_stack ++;
+	break;
+      case 9: /* unary OPs */
+      case 7: /* binary OPs */
+      case 6:
+      case 5: 
+      case 4: 
+      case 3: 
+	/* pop previous, higher or equal operators from OP stack to stack */
+	for (j = Nop_stack - 1; (j >= 0) && (op_stack[j].type >= type); j--) {
+	  strcpy (stack[Nstack].name, op_stack[j].name);
+	  stack[Nstack].type = op_stack[j].type;
+	  Nstack ++;
+	  Nop_stack --;
+	}
+	/* push operator on OP stack */
+	strcpy (op_stack[Nop_stack].name, argv[i]);
+	op_stack[Nop_stack].type = type;
+	Nop_stack ++;
+	break;
+      case 2:  
+	/* push operator on OP stack */
+	strcpy (op_stack[Nop_stack].name, argv[i]);
+	op_stack[Nop_stack].type = type;
+	Nop_stack ++;
+	break;
+      case 1: 
+	/* pop rest of operators from OP stack to stack, looking for '(' */
+	for (j = Nop_stack - 1; (j >= 0) && (op_stack[j].type != 2); j--) {
+	  strcpy (stack[Nstack].name, op_stack[j].name);
+	  stack[Nstack].type = op_stack[j].type;
+	  Nstack ++;
+	  Nop_stack --;
+	}
+	if ((j == -1) || (op_stack[j].type != 2)) {
+	  push_error ("syntax error: mismatched parenthesis");
+	  Nstack = 0;
+	  goto cleanup;
+	}
+	Nop_stack --;
+	break;
+      case 0:
+	/* place the value (number or vector/matrix name) on stack */
+	/* value of 'X' is used as sentinel until we sort out values */
+	strcpy (stack[Nstack].name, argv[i]);
+	stack[Nstack].type = 'X';
+	Nstack ++;
+	break;
+    }
+  }
+
+  /* dump remaining operators on stack, checking for ')' */
+  for (j = Nop_stack - 1; j >= 0; j--) {
+    if (op_stack[j].type == 2) {
+      push_error ("syntax error: mismatched parenthesis");
+      Nstack = 0;
+      goto cleanup;
+    }
+    strcpy (stack[Nstack].name, op_stack[j].name);
+    stack[Nstack].type = op_stack[j].type;
+    Nstack ++;
+  }
+
+cleanup: 
+  /*** free up unused stack space ***/
+  clean_stack (op_stack, NSTACK);
+  free (op_stack);
+  clean_stack (&stack[Nstack], NSTACK - Nstack);
+  REALLOCATE (stack, dvoStack, MAX (Nstack, 1));
+  *nstack = Nstack;
+
+  for (i = 0; i < argc; i++) {
+    free (argv[i]);
+  }
+  free (argv);
+
+# if (DUMPSTACK)
+  for (i = 0; i < Nstack; i++) {
+    gprint (GP_ERR, "%s ", stack[i].name);
+  }
+  if (Nstack > 0) gprint (GP_ERR, "\n");
+  for (i = 0; i < Nstack; i++) {
+    gprint (GP_ERR, "%d ", stack[i].type);
+  }
+  if (Nstack > 0) gprint (GP_ERR, "\n");
+# endif
+
+  return (stack);
+
+}
+
+/* here are the rules for parsing a math AOL line to RPN:
+
+1) if object is a number, push on stack
+2) if object is a third order operand (exp, sin, cos), push on op stack
+3) if object is an open paren, push on op stack,
+4) if object is a second order operand, push on stack
+5) if object is a first order operand, pop all second order operands from stack 
+until paren, push on stack
+6) if object is an end paren, pop all objects from stack until paren, 
+pop next stack, if third order op
+7) if end of line, pop all remaining objects, second order first, etc.
+   
+*/
Index: trunk/Ohana/src/opihi/dvo/dmt.c
===================================================================
--- trunk/Ohana/src/opihi/dvo/dmt.c	(revision 13344)
+++ trunk/Ohana/src/opihi/dvo/dmt.c	(revision 13479)
@@ -4,5 +4,5 @@
 int dmt (int argc, char **argv) {
   
-  int i, m, k, N, Ngraph, SaveVectors;
+  int i, m, k, N, kapa, SaveVectors;
   int Nsec, Nsecfilt, NPTS;
   double Radius;
@@ -38,6 +38,8 @@
   }
 
-  Ngraph = 0;
-  if (!GetGraphData (&graphsky, NULL, &Ngraph)) return (FALSE);
+  // XXX this needs to be fixed: how to access different graphs at once?
+  gprint (GP_ERR, "ERROR: this function is currently disabled\n");
+  return (FALSE);
+  if (!GetGraphData (&graphsky, &kapa, NULL)) return (FALSE);
   if (!GetGraph (&graphmode, NULL, NULL)) return (FALSE);
 
@@ -142,8 +144,5 @@
   } else {
     graphmode.style = 2; /* set style to points */
-    PrepPlotting (N, &graphmode);
-    
-    PlotVector (N, Xvec.elements);
-    PlotVector (N, Yvec.elements);
+    PlotVector (kapa, N, Xvec.elements, Yvec.elements, &graphmode);
 
     free (Xvec.elements);
Index: trunk/Ohana/src/opihi/dvo/dvo.c.in
===================================================================
--- trunk/Ohana/src/opihi/dvo/dvo.c.in	(revision 13344)
+++ trunk/Ohana/src/opihi/dvo/dvo.c.in	(revision 13479)
@@ -55,6 +55,5 @@
 /* add program-dependent exit functions here */
 void cleanup () {
-  QuitImage ();
-  QuitGraph ();
+  QuitKapa ();
   return;
 }
Index: trunk/Ohana/src/opihi/dvo/dvoBooleanElements.c
===================================================================
--- trunk/Ohana/src/opihi/dvo/dvoBooleanElements.c	(revision 13479)
+++ trunk/Ohana/src/opihi/dvo/dvoBooleanElements.c	(revision 13479)
@@ -0,0 +1,158 @@
+# include "opihi.h"
+
+/* local private functions */
+void InsertValue (char c);
+void EndOfString (void);
+int IsAnOp (char *c);
+int IsTwoOp (char *c);
+
+/* local private static variables */
+int Nchar, Nout, NOUT;
+char **out;
+
+// split up the input arguments into appropriate blocks
+char **dvoBooleanElements (int Nin, char **in, int *nout) {
+  
+  int i, j, minus, negate, plus, posate, OpStat, SciNotation;
+
+  NOUT = Nin;
+  Nchar = Nout = 0;
+  ALLOCATE (out, char *, NOUT);
+  ALLOCATE (out[Nout], char, NCHARS);
+
+  for (i = 0; i < Nin; i++) {
+    for (j = 0; j < strlen(in[i]); j++) {
+      SciNotation = FALSE;
+      /* identify 'negate' or 'minus' ops */
+      negate = minus = FALSE;
+      if (in[i][j] == '-') { 
+	minus = TRUE;  
+	/* if - is first thing on line, must be a negator */
+	if ((Nout == 0) && (Nchar == 0)) {  
+	  minus = FALSE;
+	  negate = TRUE;
+	  goto skip1;
+	}
+	/* check previous entry on line */
+	if (Nchar) {
+	  OpStat = IsAnOp (out[Nout]);
+	  if (out[Nout][0] == ')') OpStat = FALSE;
+	} else {
+	  OpStat = IsAnOp (out[Nout-1]);
+	  if (out[Nout-1][0] == ')') OpStat = FALSE;
+	}
+	/* if - follows an operator, must be negator */
+	if (OpStat) {
+	  minus = FALSE;
+	  negate = TRUE;
+	  goto skip1;
+	}
+	/* if - follows 'e' is part of 1e-5 */
+	if (j == 0) goto skip1;
+	if ((in[i][j-1] == 'e') || (in[i][j-1] == 'E')) {
+	  SciNotation = TRUE;
+	  negate = minus = FALSE;
+	}
+      }
+    skip1:
+      /* idenfity 'posate' or 'plus' ops */
+      posate = plus = FALSE;
+      if (in[i][j] == '+') { 
+	plus = TRUE;  
+	/* if + is first thing on line, must be a posator */
+	if ((Nout == 0) && (Nchar == 0)) {  
+	  plus = FALSE;
+	  posate = TRUE;
+	  goto skip2;
+	}
+	/* check previous entry on line */
+	if (Nchar) {
+	  OpStat = IsAnOp (out[Nout]);
+	  if (out[Nout][0] == ')') OpStat = FALSE;
+	} else {
+	  OpStat = IsAnOp (out[Nout-1]);
+	  if (out[Nout-1][0] == ')') OpStat = FALSE;
+	}
+	/* if + follows an operator, must be posator */
+	if (OpStat) {
+	  plus = FALSE;
+	  posate = TRUE;
+	  goto skip2;
+	}
+	/* if + follows 'e' is part of 1e+5 */
+	if (j == 0) goto skip2;
+	if ((in[i][j-1] == 'e') || (in[i][j-1] == 'E')) {
+	  SciNotation = TRUE;
+	  posate = plus = FALSE;
+	}
+      }
+    skip2:
+      /* operators */
+      if (negate || minus || posate || plus || (IsAnOp (&in[i][j]) && !SciNotation)) {
+	if (posate) continue;
+	EndOfString ();
+	/* copy operator to out[Nout] */
+	InsertValue (in[i][j]);
+	if (negate) InsertValue ('-');
+
+	if (IsTwoOp (&in[i][j])) {
+	  InsertValue (in[i][j+1]);
+	  j++;
+	} 
+	EndOfString ();
+	continue;
+      }
+      /* quoted string */
+      if (in[i][j] == '"') {
+	InsertValue (in[i][j]);
+	j++;
+	while ((j < strlen(in[i])) && (in[i][j] != '"')) {
+	  InsertValue (in[i][j]);
+	  j++;
+	}
+	if (in[i][j] != '"') continue;
+	/* 
+	  gprint (GP_ERR, "mismatched quotes\n");
+	  return (FALSE);
+	}
+	*/
+	InsertValue (in[i][j]);
+	EndOfString ();
+	continue;
+      }
+      /* not an operator, not a quoted string */
+      if (!OHANA_WHITESPACE (in[i][j])) {
+	InsertValue (in[i][j]);
+      } else {
+	EndOfString ();
+      }
+    }
+    EndOfString ();
+  }
+
+  /* one extra entry is allocated, free here */
+  free (out[Nout]);
+  *nout = Nout;
+  return (out);
+
+}
+
+void InsertValue (char c) {
+  out[Nout][Nchar] = c;
+  Nchar ++;
+  out[Nout][Nchar] = 0;
+}
+
+void EndOfString () {
+  if (Nchar > 0) {
+    out[Nout][Nchar] = 0;
+    Nout ++;
+    Nchar = 0;
+    
+    if (Nout >= NOUT - 1) {
+      NOUT += 10; 
+      REALLOCATE (out, char *, NOUT); 
+    } 
+    ALLOCATE (out[Nout], char, NCHARS); 
+  }
+}
Index: trunk/Ohana/src/opihi/dvo/dvoEvaluateStack.c
===================================================================
--- trunk/Ohana/src/opihi/dvo/dvoEvaluateStack.c	(revision 13479)
+++ trunk/Ohana/src/opihi/dvo/dvoEvaluateStack.c	(revision 13479)
@@ -0,0 +1,65 @@
+# include "opihi.h"
+
+int CheckBooleanCondition (dvoStack dbStack, int NdbStack, float *values, dvoFields *fields, int Nfields) {
+  
+  int i, j, Nstack;
+  dvoStack **stack, *output;
+
+  Nstack = NdbStack;
+  ALLOCATE (stack, dvoStack *, NdbStack);
+  for (i = 0; i < NdbStack; i++) {
+    stack[i] = &dbStack[i];
+  }
+
+  for (i = 0; i < Nstack; i++) {
+
+    /***** binary operators *****/
+    if ((stack[i].type >= 3) && (stack[i].type <= 8)) {
+
+      // pre-test that op and entries match
+      output = db_binary (stack[i-2], stack[i-1], stack[i].name, fields, Nfields); 
+
+      // free temporary stack items, drop external items
+      clear_stack (stack[i-2]);
+      clear_stack (stack[i-1]);
+
+      stack[i-2] = output;
+      for (j = i + 1; j < Nstack; j++) {
+	stack[j-2] = stack[j];
+      }
+
+      Nstack -= 2;
+      i -= 2;
+      continue;
+    }
+
+    /***** unary operators **/
+    if (stack[i].type == 9) {
+
+      // pre-test that op and entries match
+      output = db_unary (&stack[i-1], stack[i].name, fields, Nfields); 
+
+      // free temporary stack items, drop external items
+      clear_stack (stack[i-2]);
+      clear_stack (stack[i-1]);
+
+      for (j = i + 1; j < Nstack; j++) {
+	stack[j-1] = stack[j];
+      }
+
+      Nstack -= 1;
+      i -= 1;
+      continue;
+    } 
+  }
+
+  // pre-test that op and entries match
+  return (TRUE);
+}
+
+/* delete name and data */
+void clear_stack (dvoStack *stack) {
+
+  if (stack->type != 'T') return;
+  free (stack);
+}
Index: trunk/Ohana/src/opihi/dvo/dvodb.c
===================================================================
--- trunk/Ohana/src/opihi/dvo/dvodb.c	(revision 13479)
+++ trunk/Ohana/src/opihi/dvo/dvodb.c	(revision 13479)
@@ -0,0 +1,77 @@
+
+/* dvo fields and boolean expressions
+   - evaluate the command line:
+     * avextract ra,dec,g,r,i where (uRA < 0.1)
+     1) identify all fields in extraction list
+     2) parse boolean expression
+     3) identify additional fields from expression
+*/
+
+enum {DVO_TABLE_AVERAGE, DVO_TABLE_MEASURE};
+
+// a single db field 
+typedef struct {
+  char *name;
+  int extract;
+  int table;
+  int ID;
+  int magMode;
+  PhotCode *photcode;
+} dvoField;
+
+// db boolean operations
+typedef struct {
+  char   *name;
+  char    type;
+  int     field;
+  float   Float;
+} dvoStack;
+
+dvoField *ParseCmdlineFields (int argc, char **argv, int table, int *last, int *nfields) {
+
+  int i, Nfields, NFIELDS, 
+
+  *nfields = 0;
+  Nfields = 0;
+  NFIELDS = 10;
+  ALLOCATE (fields, dvoField, NFIELDS);
+
+  // examine each argv[i] entry until we reach a where 
+  for (i = 1; (i < argc) && strcasecmp (argv[i], "where"); i++) {
+    // split the word by ","
+    p = argv[i];
+    while (*p) {
+      q = strchr (p, ',');
+      if (q == NULL) {
+	field = strcreate (p);
+	p = p + strlen(p);
+      } else {
+	field = strncreate (p, q-p);
+	p = q + 1;
+      }
+      // identify field for word
+      // need to know which type of fields to look for...
+      // xxx extend this more generally later
+      if (table == DVO_TABLE_MEASURE) {
+	status = ParseMeasureField (&fields[Nfields], field);
+      } 
+      if (table == DVO_TABLE_AVERAGE) {
+	status = ParseAverageField (&fields[Nfields], field);
+      } 
+      if (!status) {
+	gprint (GP_ERR, "unknown database field %s\n", field);
+	free (field);
+	free (fields);
+	return (NULL);
+      }
+      free (field);
+
+      Nfields ++;
+      CHECK_REALLOCATE (fields, dvoField, NFIELDS, Nfields, 10);
+    }
+  }
+
+  *last = i;
+  *nfields = Nfields;
+  return (fields);
+}
Index: trunk/Ohana/src/opihi/dvo/dvofields.c
===================================================================
--- trunk/Ohana/src/opihi/dvo/dvofields.c	(revision 13479)
+++ trunk/Ohana/src/opihi/dvo/dvofields.c	(revision 13479)
@@ -0,0 +1,129 @@
+
+int GetMagMode (char *string) {
+
+  if (!strcasecmp (string, "inst"))  return (MAG_INST);
+  if (!strcasecmp (string, "cat"))   return (MAG_CAT);
+  if (!strcasecmp (string, "sys"))   return (MAG_SYS);
+  if (!strcasecmp (string, "rel"))   return (MAG_REL);
+  if (!strcasecmp (string, "cal"))   return (MAG_CAL);
+  if (!strcasecmp (string, "ave"))   return (MAG_AVE);
+  if (!strcasecmp (string, "ref"))   return (MAG_REF);
+  if (!strcasecmp (string, "err"))   return (MAG_ERR);
+  if (!strcasecmp (string, "chisq")) return (MAG_CHISQ);
+  return (MAG_NONE);
+}
+
+PhotCode *ParsePhotcodeField (char *field, int *mode, int default) {
+
+  char *tmpstring, *p;
+  PhotCode *code;
+
+  *mode = default;
+
+  p = strchr (field, ':');
+  if (p != NULL) {
+    *mode = GetMagMode (p + 1);
+    if (*mode == MAG_NONE) return (NULL);
+    tmpstring = strncreate (field, p - field);
+  } else {
+    tmpstring = strcreate (field);
+  }
+  code = GetPhotcodebyName (tmpstring);
+  free (tmpstring);
+
+  return (code);
+}
+
+# define ESCAPE(F,M) { \
+  field->ID = (F); \
+  field->magMode = (M); \
+  field->photcode = NULL; \
+  return (TRUE);
+
+int ParseMeasureField (dvoField *field, char *fieldName) {
+
+  PhotCode *code;
+
+  field->table = DVO_TABLE_MEASURE;
+  field->name  = strcreate (fieldName);
+
+  if (!strcasecmp (fieldName, "RA"))       ESCAPE (MEAS_RA,   	  MAG_NONE);
+  if (!strcasecmp (fieldName, "DEC"))      ESCAPE (MEAS_DEC,  	  MAG_NONE);
+  if (!strcasecmp (fieldName, "MAG"))      ESCAPE (MEAS_MAG,  	  MAG_REL);
+  if (!strcasecmp (fieldName, "dMAG"))     ESCAPE (MEAS_dMAG, 	  MAG_ERR);
+  if (!strcasecmp (fieldName, "AIRMASS"))  ESCAPE (MEAS_AIRMASS,  MAG_NONE);
+  if (!strcasecmp (fieldName, "EXPTIME"))  ESCAPE (MEAS_EXPTIME,  MAG_NONE);
+  if (!strcasecmp (fieldName, "PHOTCODE")) ESCAPE (MEAS_PHOTCODE, MAG_NONE);
+  if (!strcasecmp (fieldName, "TIME"))     ESCAPE (MEAS_TIME,     MAG_NONE);
+  if (!strcasecmp (fieldName, "dR"))       ESCAPE (MEAS_dR, 	  MAG_NONE);
+  if (!strcasecmp (fieldName, "dD"))       ESCAPE (MEAS_dD, 	  MAG_NONE);
+  if (!strcasecmp (fieldName, "FWHM"))     ESCAPE (MEAS_FWHM,     MAG_NONE);
+  if (!strcasecmp (fieldName, "DOPHOT"))   ESCAPE (MEAS_DOPHOT,   MAG_NONE);
+  if (!strcasecmp (fieldName, "FLAGS"))    ESCAPE (MEAS_FLAGS,    MAG_NONE);
+  if (!strcasecmp (fieldName, "XCCD"))     ESCAPE (MEAS_XCCD, 	  MAG_NONE);
+  if (!strcasecmp (fieldName, "YCCD"))     ESCAPE (MEAS_YCCD, 	  MAG_NONE);
+  if (!strcasecmp (fieldName, "XMOSAIC"))  ESCAPE (MEAS_XMOSAIC,  MAG_NONE);
+  if (!strcasecmp (fieldName, "YMOSAIC"))  ESCAPE (MEAS_YMOSAIC,  MAG_NONE);
+
+  // for words that don't parse, try a photcode
+
+  // check for code:mode in photcode name 
+  code = ParsePhotcodeField (&mode, MAG_REL);
+  if (code == NULL) return (FALSE);
+
+  if (mode == MAG_ERR) {
+    field->ID = MEAS_dMAG;
+  } else {
+    field->ID = MEAS_MAG;
+  }    
+  
+  field->magMode = mode;
+  field->photcode = code;
+  return (TRUE);
+}
+  
+int ParseAverageField (char *fieldName) {
+
+  PhotCode *code;
+
+  field->table = DVO_TABLE_AVERAGE;
+
+  if (!strcasecmp (fieldName, "RA"))    ESCAPE (AVE_RA,        MAG_NONE);
+  if (!strcasecmp (fieldName, "DEC"))   ESCAPE (AVE_DEC,       MAG_NONE);
+  if (!strcasecmp (fieldName, "dRA"))   ESCAPE (AVE_RA_ERR,    MAG_NONE);
+  if (!strcasecmp (fieldName, "dDEC"))  ESCAPE (AVE_DEC_ERR,   MAG_NONE);
+  if (!strcasecmp (fieldName, "uRA"))   ESCAPE (AVE_U_RA,      MAG_NONE);
+  if (!strcasecmp (fieldName, "uDEC"))  ESCAPE (AVE_U_DEC,     MAG_NONE);
+  if (!strcasecmp (fieldName, "duRA"))  ESCAPE (AVE_U_RA_ERR,  MAG_NONE);
+  if (!strcasecmp (fieldName, "duDEC")) ESCAPE (AVE_U_DEC_ERR, MAG_NONE);
+  if (!strcasecmp (fieldName, "PAR"))   ESCAPE (AVE_PAR,       MAG_NONE);
+  if (!strcasecmp (fieldName, "dPAR"))  ESCAPE (AVE_PAR_ERR,   MAG_NONE);
+  if (!strcasecmp (fieldName, "MAG"))   ESCAPE (AVE_MAG,       MAG_AVE);
+  if (!strcasecmp (fieldName, "dMAG"))  ESCAPE (AVE_dMAG,      MAG_AVE_ERR);
+  if (!strcasecmp (fieldName, "NMEAS")) ESCAPE (AVE_NMEAS,     MAG_NONE);
+  if (!strcasecmp (fieldName, "NMISS")) ESCAPE (AVE_NMISS,     MAG_NONE);
+  if (!strcasecmp (fieldName, "Xp"))    ESCAPE (AVE_Xp,        MAG_NONE);
+  if (!strcasecmp (fieldName, "Xm"))    ESCAPE (AVE_Xm,        MAG_CHISQ);
+  if (!strcasecmp (fieldName, "FLAG"))  ESCAPE (AVE_FLAG,      MAG_NONE);
+  if (!strcasecmp (fieldName, "TYPE"))  ESCAPE (AVE_TYPE,      MAG_NONE);
+  if (!strcasecmp (fieldName, "NPHOT")) ESCAPE (AVE_NPHOT,     MAG_NONE);
+  if (!strcasecmp (fieldName, "NCODE")) ESCAPE (AVE_NCODE,     MAG_NONE);
+  if (!strcasecmp (fieldName, "NCRIT")) ESCAPE (AVE_NCRIT,     MAG_NONE);
+
+  // for words that don't parse, try a photcode
+
+  // check for code:mode in photcode name 
+  code = ParsePhotcodeField (&mode, MAG_AVE);
+  if (code == NULL) return (FALSE);
+
+  // need to distinguish phot, sys errors and scatter
+  if (mode == MAG_ERR) {
+    field->ID = MEAS_dMAG;
+  } else {
+    field->ID = MEAS_MAG;
+  }    
+  
+  field->magMode = mode;
+  field->photcode = code;
+  return (TRUE);
+}
Index: trunk/Ohana/src/opihi/dvo/extract.c
===================================================================
--- trunk/Ohana/src/opihi/dvo/extract.c	(revision 13344)
+++ trunk/Ohana/src/opihi/dvo/extract.c	(revision 13479)
@@ -31,6 +31,5 @@
   RegionFile *regions;
 
-  Ngraph = 0;
-  if (!GetGraphData (&graphmode, NULL, &Ngraph)) return (FALSE);
+  if (!GetGraphData (&graphmode, NULL, NULL)) return (FALSE);
   if (!InitPhotcodes ()) return (FALSE);
 
Index: trunk/Ohana/src/opihi/dvo/fitcolors.c
===================================================================
--- trunk/Ohana/src/opihi/dvo/fitcolors.c	(revision 13344)
+++ trunk/Ohana/src/opihi/dvo/fitcolors.c	(revision 13479)
@@ -20,5 +20,5 @@
   float *out, *colorFit, *deltaFit, dColor, C0, C1;
   float minDelta, maxDelta, minColor, maxColor;
-  int fd, Npx, Npy, NPX, NPY, Nplot, PLOT;
+  int kapa, Npx, Npy, NPX, NPY, Nplot, PLOT;
   Graphdata graphdata;
   KapaSection section;
@@ -86,5 +86,5 @@
 
   if (PLOT) {
-    if (!GetGraph (&graphdata, &fd, NULL)) return (FALSE);
+    if (!GetGraph (&graphdata, &kapa, NULL)) return (FALSE);
     Nplot = 0;
     Npx = Npy = 0;
@@ -95,6 +95,6 @@
     graphdata.style = 2;
     graphdata.ptype = 2;
-    KapaClear (fd, TRUE);
-    KapaSetFont (fd, "helvetica", 14);
+    KapaClearSections (kapa);
+    KapaSetFont (kapa, "helvetica", 14);
 
     ALLOCATE (colorFit, float, 11);
@@ -301,10 +301,10 @@
 	  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);
+	KapaSetSection (kapa, &section);
+	KapaSetLimits (kapa, &graphdata);
+	KapaBox (kapa, &graphdata);
+	KapaPrepPlot (kapa, Npts, &graphdata);
+	KapaPlotVector (kapa, Npts, xvec[0].elements, "x");
+	KapaPlotVector (kapa, Npts, yvec[0].elements, "y");
 
 	for (i = 0; i < 11; i++) {
@@ -313,14 +313,14 @@
 	graphdata.style = 0;
 	graphdata.color = KapaColorByName ("red");
-	KapaPrepPlot (fd, 11, &graphdata);
-	KapaPlotVector (fd, 11, colorFit);
-	KapaPlotVector (fd, 11, deltaFit);
-
-	KapaSetFont (fd, "helvetica", 8);
+	KapaPrepPlot (kapa, 11, &graphdata);
+	KapaPlotVector (kapa, 11, colorFit, "x");
+	KapaPlotVector (kapa, 11, deltaFit, "y");
+
+	KapaSetFont (kapa, "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);
+	KapaSendTextline (kapa, 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);
+	KapaSendTextline (kapa, label, 0.2*maxColor + 0.8*minColor, 0.2*maxDelta + 0.8*minDelta, 0.0);
+	KapaSetFont (kapa, "helvetica", 14);
 
 	graphdata.style = 2;
@@ -336,6 +336,6 @@
 	    Npy = 0;
 	    sprintf (filename, "%s.%02d.png", plotname, Nplot);
-	    KapaPNG (fd, filename);
-	    KapaClear (fd, TRUE);
+	    KapaPNG (kapa, filename);
+	    KapaClearSections (kapa);
 	    Nplot++;
 	  }
Index: trunk/Ohana/src/opihi/dvo/fitsed.c
===================================================================
--- trunk/Ohana/src/opihi/dvo/fitsed.c	(revision 13344)
+++ trunk/Ohana/src/opihi/dvo/fitsed.c	(revision 13479)
@@ -31,6 +31,6 @@
   float *fitmags, *fiterrs, *wavecode, *vegaToAB;
   float color;
-  double X, Y;
-  int fd, PLOT;
+  double X, Y, ZP, RA, DEC;
+  int kapa, PLOT;
   int Nrow, NROW, idx, Nfilter, start, row;
   unsigned short colorP, colorM, code, USNOred, USNOblu;
@@ -157,9 +157,9 @@
 
   if (PLOT) {
-    if (!GetGraph (&graphdata, &fd, NULL)) return (FALSE);
+    if (!GetGraph (&graphdata, &kapa, NULL)) return (FALSE);
     SetLimitsRaw (wavecode, NULL, Nfilter, &graphdata);
     graphdata.style = 2;
     graphdata.ptype = 2;
-    KapaClear (fd, TRUE);
+    KapaClearSections (kapa);
     magSection.name = strcreate ("mag");
     magSection.x  = 0;
@@ -173,5 +173,5 @@
     resSection.dy = 0.5;
     
-    KapaSetFont (fd, "helvetica", 14);
+    KapaSetFont (kapa, "helvetica", 14);
     ALLOCATE (fitmags, float, Nfilter);
     ALLOCATE (fiterrs, float, Nfilter);
@@ -261,13 +261,13 @@
 	SWAP (graphdata.ymin, graphdata.ymax);
 
-	KapaClear (fd, TRUE);
-	KapaSetSection (fd, &magSection);
-    	KapaSetLimits (fd, &graphdata);
-	KapaBox (fd, &graphdata);
+	KapaClearSections (kapa);
+	KapaSetSection (kapa, &magSection);
+    	KapaSetLimits (kapa, &graphdata);
+	KapaBox (kapa, &graphdata);
 	graphdata.color = KapaColorByName ("blue");
 	graphdata.etype = 0;
-	KapaPrepPlot (fd, Nfilter, &graphdata);
-	KapaPlotVector (fd, Nfilter, wavecode);
-	KapaPlotVector (fd, Nfilter, SEDtable[minFit.row][0].mags);
+	KapaPrepPlot (kapa, Nfilter, &graphdata);
+	KapaPlotVector (kapa, Nfilter, wavecode, "x");
+	KapaPlotVector (kapa, Nfilter, SEDtable[minFit.row][0].mags, "y");
 	graphdata.color = KapaColorByName ("red");
 	graphdata.etype = 1;
@@ -279,22 +279,22 @@
 	  fiterrs[j] = sourceError.mags[j];
 	}
-	KapaPrepPlot (fd, Nfilter, &graphdata);
-	KapaPlotVector (fd, Nfilter, wavecode);
-	KapaPlotVector (fd, Nfilter, fitmags);
-	KapaPlotVector (fd, Nfilter, fiterrs);
-	KapaPlotVector (fd, Nfilter, fiterrs);
-	KapaSendLabel (fd, "model,fit (mags)", 1);
+	KapaPrepPlot (kapa, Nfilter, &graphdata);
+	KapaPlotVector (kapa, Nfilter, wavecode, "x");
+	KapaPlotVector (kapa, Nfilter, fitmags, "y");
+	KapaPlotVector (kapa, Nfilter, fiterrs, "dym");
+	KapaPlotVector (kapa, Nfilter, fiterrs, "dyp");
+	KapaSendLabel (kapa, "model,fit (mags)", 1);
 
 	sprintf (line, "star: %10.6f %10.6f  T: %5.0fK  A_V|: %4.2f  M_D|: %5.2f  &sc&h^2|: %5.2f", 
 		 catalog.average[i].R, catalog.average[i].D, 
 		 SEDtable[minFit.row][0].Temp, SEDtable[minFit.row][0].Av, minFit.Md, minFit.chisq);
-	KapaSendLabel (fd, line, 2);
-	KapaSendLabel (fd, "model,fit (mags)", 1);
-
-	KapaSetSection (fd, &resSection);
+	KapaSendLabel (kapa, line, 2);
+	KapaSendLabel (kapa, "model,fit (mags)", 1);
+
+	KapaSetSection (kapa, &resSection);
 	graphdata.ymin = -1.0;
 	graphdata.ymax = +1.0;
-    	KapaSetLimits (fd, &graphdata);
-	KapaBox (fd, &graphdata);
+    	KapaSetLimits (kapa, &graphdata);
+	KapaBox (kapa, &graphdata);
 	graphdata.color = KapaColorByName ("red");
 	graphdata.etype = 1;
@@ -307,21 +307,21 @@
 	  fiterrs[j] = sourceError.mags[j];
 	}
-	KapaPrepPlot (fd, Nfilter, &graphdata);
-	KapaPlotVector (fd, Nfilter, wavecode);
-	KapaPlotVector (fd, Nfilter, fitmags);
-	KapaPlotVector (fd, Nfilter, fiterrs);
-	KapaPlotVector (fd, Nfilter, fiterrs);
-	KapaSendLabel (fd, "wavelength (nm)", 0);
-	KapaSendLabel (fd, "resid (mags)", 1);
-
-	KiiCursorOn (fd);
-	while (KiiCursorRead (fd, &X, &Y, key)) {
+	KapaPrepPlot (kapa, Nfilter, &graphdata);
+	KapaPlotVector (kapa, Nfilter, wavecode, "x");
+	KapaPlotVector (kapa, Nfilter, fitmags, "y");
+	KapaPlotVector (kapa, Nfilter, fiterrs, "dym");
+	KapaPlotVector (kapa, Nfilter, fiterrs, "dyp");
+	KapaSendLabel (kapa, "wavelength (nm)", 0);
+	KapaSendLabel (kapa, "resid (mags)", 1);
+
+	KiiCursorOn (kapa);
+	while (KiiCursorRead (kapa, &X, &Y, &ZP, &RA, &DEC, key)) {
 	  gprint (GP_ERR, "window: %f %f (%s)\n", X, Y, key);
 	  if (!strcasecmp (key, "Q")) {
-	    KiiCursorOff (fd);
+	    KiiCursorOff (kapa);
 	    break;
 	  }
 	  if (!strcasecmp (key, "ESCAPE")) {
-	    KiiCursorOff (fd);
+	    KiiCursorOff (kapa);
 	    goto escape;
 	  }
Index: trunk/Ohana/src/opihi/dvo/images.c
===================================================================
--- trunk/Ohana/src/opihi/dvo/images.c	(revision 13344)
+++ trunk/Ohana/src/opihi/dvo/images.c	(revision 13479)
@@ -14,5 +14,5 @@
   int WITH_MOSAIC, SOLO_MOSAIC, HIDDEN;
   time_t tzero, tend;
-  int N, NPTS, n, npts, Npts;
+  int N, NPTS, n, npts, Npts, kapa;
   Vector Xvec, Yvec;
   double r[8], d[8], x[8], y[8], Rmin, Rmax, Rmid, trange, Radius;
@@ -22,5 +22,5 @@
   int typehash;
 
-  if (!style_args (&graphmode, &argc, argv, 0)) return FALSE;
+  if (!style_args (&graphmode, &argc, argv, &kapa)) return FALSE;
 
   WITH_MOSAIC = FALSE;
@@ -101,5 +101,4 @@
     return (FALSE);
   }
-  SetGraph (graphmode);
   
   /* it is not an error for the database not to have any images */
@@ -282,7 +281,5 @@
     graphmode.ptype = 100; /* connect pairs of points */
     graphmode.etype = 0;
-    PrepPlotting (N, &graphmode);
-    PlotVector (N, Xvec.elements);
-    PlotVector (N, Yvec.elements);
+    PlotVectorPair (kapa, N, Xvec.elements, Yvec.elements, &graphmode);
   }
 
Index: trunk/Ohana/src/opihi/dvo/imbox.c
===================================================================
--- trunk/Ohana/src/opihi/dvo/imbox.c	(revision 13344)
+++ trunk/Ohana/src/opihi/dvo/imbox.c	(revision 13479)
@@ -3,5 +3,5 @@
 int imbox (int argc, char **argv) {
   
-  int j, Nskip, status, InPic, flipped;
+  int j, kapa, Nskip, status, InPic, flipped;
   Vector Xvec, Yvec;
   double r, d, x[4], y[4], Rmin, Rmax, Rmid;
@@ -12,5 +12,5 @@
   FILE *f;
 
-  if (!style_args (&graphmode, &argc, argv, 0)) return FALSE;
+  if (!style_args (&graphmode, &argc, argv, &kapa)) return FALSE;
 
   if (argc != 2) {
@@ -18,5 +18,4 @@
     return (FALSE);
   }
-  SetGraph (graphmode);
 
   f = fopen (argv[1], "r");
@@ -77,7 +76,5 @@
       graphmode.ptype = 100; /* connect pairs of points */
       graphmode.etype = 0;
-      PrepPlotting (8, &graphmode);
-      PlotVector (8, Xvec.elements);
-      PlotVector (8, Yvec.elements);
+      PlotVectorPair (kapa, 8, Xvec.elements, Yvec.elements, &graphmode);
     }
   skip:
Index: trunk/Ohana/src/opihi/dvo/imdense.c
===================================================================
--- trunk/Ohana/src/opihi/dvo/imdense.c	(revision 13344)
+++ trunk/Ohana/src/opihi/dvo/imdense.c	(revision 13479)
@@ -5,9 +5,11 @@
   
   long A, B;
-  int i, N, Nimage, status, NPTS, Ngraph;
+  int i, kapa, N, Nimage, status, NPTS;
   double r, d, x, y, Rmin, Rmax;
   Vector Xvec, Yvec;
   Image *image;
   Graphdata graphmode;
+
+  if (!style_args (&graphmode, &argc, argv, &kapa)) return FALSE;
 
   /* need options to list only images in region and only images in a time range */
@@ -17,6 +19,4 @@
     return (FALSE);
   }
-  Ngraph = 0;
-  if (!GetGraph (&graphmode, NULL, &Ngraph)) return (FALSE);
 
   if ((image = LoadImages (&Nimage)) == NULL) return (FALSE);
@@ -62,7 +62,5 @@
     graphmode.style = 2; /* points */
     graphmode.etype = 0;
-    PrepPlotting (N, &graphmode);
-    PlotVector (N, Xvec.elements);
-    PlotVector (N, Yvec.elements);
+    PlotVectorPair (kapa, N, Xvec.elements, Yvec.elements, &graphmode);
   }
 
Index: trunk/Ohana/src/opihi/dvo/imextract.c
===================================================================
--- trunk/Ohana/src/opihi/dvo/imextract.c	(revision 13344)
+++ trunk/Ohana/src/opihi/dvo/imextract.c	(revision 13479)
@@ -5,5 +5,5 @@
 int imextract (int argc, char **argv) {
   
-  int i, j, Nimage, mode, N, Ngraph, PhotcodeSelect;
+  int i, j, Nimage, mode, N, PhotcodeSelect;
   int TimeSelect, RegionSelect, *subset, Nsubset, TimeFormat, FlagSelect, FlagValue;
   double x, y, ra, dec, t, trange;
@@ -15,6 +15,5 @@
   Vector *vec;
 
-  Ngraph = 0;
-  if (!GetGraphData (&graphsky, NULL, &Ngraph)) return (FALSE);
+  if (!GetGraphData (&graphsky, NULL, NULL)) return (FALSE);
   if (!InitPhotcodes ()) return (FALSE);
 
Index: trunk/Ohana/src/opihi/dvo/imlist.c
===================================================================
--- trunk/Ohana/src/opihi/dvo/imlist.c	(revision 13344)
+++ trunk/Ohana/src/opihi/dvo/imlist.c	(revision 13479)
@@ -8,11 +8,9 @@
   double r, d, trange, t;
   char *name;
-  int Ngraph;
   Image *image;
   Graphdata graphmode;
   PhotCode *PhotcodeValue;
 
-  Ngraph = 0;
-  if (!GetGraphData (&graphmode, NULL, &Ngraph)) return (FALSE);
+  if (!GetGraphData (&graphmode, NULL, NULL)) return (FALSE);
 
   TimeSelect = FALSE;
Index: trunk/Ohana/src/opihi/dvo/imstats.c
===================================================================
--- trunk/Ohana/src/opihi/dvo/imstats.c	(revision 13344)
+++ trunk/Ohana/src/opihi/dvo/imstats.c	(revision 13479)
@@ -3,6 +3,6 @@
 int imstats (int argc, char **argv) {
   
-  int i, Nimage, N;
-  int Mcal, AutoLimits, Ngraph;
+  int i, kapa, Nimage, N;
+  int Mcal, AutoLimits;
   double r, d;
   Image *image;
@@ -10,11 +10,5 @@
   Graphdata graphmode;
 
-  Ngraph = -1;
-  if ((N = get_argument (argc, argv, "-n"))) {
-    remove_argument (N, &argc, argv);
-    Ngraph = atof (argv[N]);
-    remove_argument (N, &argc, argv);
-  }
-  if (!GetGraph (&graphmode, NULL, &Ngraph)) return (FALSE);
+  if (!style_args (&graphmode, &argc, argv, &kapa)) return (FALSE);
 
   Mcal = TRUE;
@@ -60,7 +54,5 @@
   graphmode.style = 2;
   graphmode.etype = 0;
-  PrepPlotting (Nimage, &graphmode);
-  PlotVector (Nimage, Xvec.elements);
-  PlotVector (Nimage, Yvec.elements);
+  PlotVectorPair (kapa, Nimage, Xvec.elements, Yvec.elements, &graphmode);
   
   free (Xvec.elements);
Index: trunk/Ohana/src/opihi/dvo/lcat.c
===================================================================
--- trunk/Ohana/src/opihi/dvo/lcat.c	(revision 13344)
+++ trunk/Ohana/src/opihi/dvo/lcat.c	(revision 13479)
@@ -8,10 +8,8 @@
   struct stat filestat;
   Graphdata graphmode;
-  int Ngraph;
   SkyTable *sky;
   SkyList *skylist;
 
-  Ngraph = 0;
-  if (!GetGraphData (&graphmode, NULL, &Ngraph)) return (FALSE);
+  if (!GetGraphData (&graphmode, NULL, NULL)) return (FALSE);
 
   ShowAll = FALSE;
Index: trunk/Ohana/src/opihi/dvo/lcurve.c
===================================================================
--- trunk/Ohana/src/opihi/dvo/lcurve.c	(revision 13344)
+++ trunk/Ohana/src/opihi/dvo/lcurve.c	(revision 13479)
@@ -6,5 +6,5 @@
   double Ra, Dec, Radius, Radius2, r;
   float *RA, *DEC;
-  int Ngraph, Xgraph, TimeFormat;
+  int kapa, TimeFormat;
   int Nstars, found, AutoLimits, ErrorBars, GalMag, AbsPhot, SaveVectors;
   int i, j, m, N, NPTS, *N1;
@@ -19,12 +19,5 @@
 
   if (!InitPhotcodes ()) return (FALSE);
-
-  Ngraph = -1;
-  if ((N = get_argument (argc, argv, "-n"))) {
-    remove_argument (N, &argc, argv);
-    Ngraph = atof (argv[N]);
-    remove_argument (N, &argc, argv);
-  }
-  if (!GetGraph (&graphmode, &Xgraph, &Ngraph)) return (FALSE);
+  if (!style_args (&graphmode, &argc, argv, &kapa)) return (FALSE);
 
   AutoLimits = FALSE;
@@ -161,10 +154,10 @@
     graphmode.etype = 0;  
 
-  PrepPlotting (N, &graphmode);
-  PlotVector (N, Xvec.elements);
-  PlotVector (N, Yvec.elements);
+  KapaPrepPlot (kapa, N, &graphmode);
+  KapaPlotVector (kapa, N, Xvec.elements, "x");
+  KapaPlotVector (kapa, N, Yvec.elements, "y");
   if (ErrorBars) {
-    PlotVector (N, dYvec.elements);
-    PlotVector (N, dYvec.elements);
+    KapaPlotVector (kapa, N, dYvec.elements, "dym");
+    KapaPlotVector (kapa, N, dYvec.elements, "dyp");
   }
 
@@ -175,5 +168,5 @@
 	   timeptr[0].tm_year, timeptr[0].tm_mon+1, timeptr[0].tm_mday);
   free (p);
-  KapaSendLabel (Xgraph, string, 0);
+  KapaSendLabel (kapa, string, 0);
 
   free (RA);
Index: trunk/Ohana/src/opihi/dvo/mextract.c
===================================================================
--- trunk/Ohana/src/opihi/dvo/mextract.c	(revision 13344)
+++ trunk/Ohana/src/opihi/dvo/mextract.c	(revision 13479)
@@ -3,13 +3,18 @@
 int mextract (int argc, char **argv) {
   
-  int i, j, k, m, N, N1, NPTS;
+  int i, j, k, m, n, N, N1, Npts, NPTS, last, Nfields, Nreturn, Ncstack, Nstack;
   int param, mode, Nsecfilt;
   char *RegionName, *RegionList, *p;
   double *M1;
+  char **cstack;
+  float *values;
 
-  PhotCode *code;
   Catalog catalog;
   SkyList *skylist;
-  Vector *vec;
+  PhotCode *code;
+  Vector **vec;
+
+  dbField *fields;
+  dbStack *stack;
 
   /* defaults */
@@ -24,35 +29,47 @@
   if (!InitPhotcodes ()) goto escape;
   Nsecfilt = GetPhotcodeNsecfilt ();
+  
+  // init locally static variables (time refs)
+  dbExtractMeasuresInit();
 
-  /* interpret command-line options */
-  SetSelectionParam (0);
+  // remove skyregion options
+  // XXX this needs to explicitly handle -qregion and -skyregion
   if (!SetRegionSelection (&argc, argv, &RegionName, &RegionList)) goto escape;
-  if (!SetPhotSelections (&argc, argv, 1)) goto usage;
 
-  /* interpret required command-line arguments: mextract (value) */
-  if (argc != 2) goto usage;
-  param = GetMeasureParam (argv[1]);
-  if (param == MEAS_ZERO) {
-    if (!GetPhotcodeInfo (argv[1], &code, &mode)) {
-      GetMeasureParam ("help");
-      goto escape;
-    }
-    param = MEAS_MAG;
-    for (p = argv[1]; *p != 0; p++) { 
-      if (*p == '.') *p = ':';
-    }
-  }
-  if (!TestPhotSelections (&code, &mode, MEAS_ZERO)) goto escape;
+  // command-line is of the form: avextract field,field, field [where (field op value)...]
+
+  // parse the fields to be extracted and returned
+  fields = dbCmdlineFields (argc, argv, DVO_TABLE_MEASURE, &last, &Nfields);
+  if (fields == NULL) return (FALSE);
+
+  // parse the remainder of the line as a boolean math expression
+  cstack = isolate_elements (argc-last, &argv[last], &Ncstack);
+  
+  // construct the db Boolean math stack (frees cstack)
+  stack = dbRPN (Ncstack, cstack, &Nstack);
+
+  Nreturn = Nfields; 
+  dbCheckStack (stack, Nstack, DVO_TABLE_MEASURE, &fields, &Nfields);
+  // XXX handle errors
 
   /* load region corresponding to selection above */
   if ((skylist = SelectRegions (RegionName, RegionList)) == NULL) goto escape;
-  if (!SetImageSelection (param, ((RegionName == NULL) && (RegionList == NULL)))) goto escape;
+
+  // XXX is this still needed?
+  // if (!SetImageSelection (param, ((RegionName == NULL) && (RegionList == NULL)))) goto escape;
 
   /* create storage vector */
-  N = 0;
+  ALLOCATE (values, float, Nfields);
+  ALLOCATE (vec, Vector *, Nreturn);
+  for (i = 0; i < Nreturn; i++) {
+    if ((vec[i] = SelectVector (fields[i].name, ANYVECTOR, TRUE)) == NULL) goto escape;
+  }
+
+  Npts = 0;
   NPTS = 1;
-  if ((vec = SelectVector (argv[1], ANYVECTOR, TRUE)) == NULL) goto escape;
 
+  // XXX need to add interrupt test to this loop
   for (i = 0; i < skylist[0].Nregions; i++) {
+    /* lock, load, unlock catalog */
     catalog.filename = skylist[0].filename[i];
     catalog.catflags = LOAD_AVES | LOAD_MEAS | LOAD_SECF;
@@ -69,18 +86,34 @@
 
     for (j = 0; j < catalog.Naverage; j++) {
-      M1 = NULL;
       m = catalog.average[j].offset;
-      M1 = ExtractMeasures (code, mode, &catalog.average[j], &catalog.secfilt[j*Nsecfilt], &catalog.measure[m], &N1, param);
-      for (k = 0; k < N1; k++) {
-	vec[0].elements[N] = M1[k];
-	N++;
-	CHECK_REALLOCATE (vec[0].elements, float, NPTS, N, 2000);
+      for (k = 0; k < catalog.average[j].Nm; k++, m++) {
+
+	// extract the relevant values for this measurement
+	for (n = 0; n < Nfields; n++) {
+	  values[n] = dbExtractMeasures (&catalog.average[j], &catalog.secfilt[j*Nsecfilt], &catalog.measure[m], &fields[n]);
+	}
+	
+	// test the conditional statement
+	if (!dbBooleanCond (stack, Nstack, values)) continue;
+	for (n = 0; n < Nreturn; n++) {
+	  vec[n][0].elements[Npts] = values[n];
+	}
+	Npts++;
+	if (Npts >= NPTS) {
+	  NPTS += 2000;
+	  for (n = 0; n < Nreturn; n++) {
+	    REALLOCATE (vec[n][0].elements, float, NPTS);
+	  }
+	}
       }
-      if (M1 != NULL) free (M1);
     }
     dvo_catalog_free (&catalog);
   }
-  vec[0].Nelements = N;
-  REALLOCATE (vec[0].elements, float, MAX(1,N));
+  for (n = 0; n < Nreturn; n++) {
+    vec[n][0].Nelements = Npts;
+    REALLOCATE (vec[n][0].elements, float, MAX(1,Npts));
+  }
+
+  // XXX free fields and stack 
 
   FreeImageSelection ();
Index: trunk/Ohana/src/opihi/dvo/paverage.c
===================================================================
--- trunk/Ohana/src/opihi/dvo/paverage.c	(revision 13344)
+++ trunk/Ohana/src/opihi/dvo/paverage.c	(revision 13479)
@@ -5,5 +5,5 @@
   
   FILE *f;
-  int i, j, Narg, Npts, NPTS, status, VERBOSE;
+  int i, j, kapa, Narg, Npts, NPTS, status, VERBOSE;
   int Nsecfilt, Nsec;
   double Mz, Mr, mag;
@@ -23,5 +23,5 @@
   Nsecfilt = GetPhotcodeNsecfilt ();
 
-  if (!style_args (&graphmode, &argc, argv, 0)) return FALSE;
+  if (!style_args (&graphmode, &argc, argv, &kapa)) return FALSE;
 
   f = (FILE *) NULL;
@@ -82,5 +82,4 @@
     return (FALSE);
   }
-  SetGraph (graphmode);
   graphmode.style = 2; /* set style to points */
   graphmode.size = -1; /* point size determined by Zvec */
@@ -141,5 +140,5 @@
       }
       if (Npts > NCHUNK) {
-	  PlotVectorTriplet (Npts, Xvec, Yvec, Zvec, &graphmode);
+	  PlotVectorTriplet (kapa, Npts, Xvec, Yvec, Zvec, &graphmode);
 	  Npts = 0;
       }
@@ -147,5 +146,5 @@
     dvo_catalog_free (&catalog);
   }
-  if (Npts > 0) PlotVectorTriplet (Npts, Xvec, Yvec, Zvec, &graphmode);
+  if (Npts > 0) PlotVectorTriplet (kapa, Npts, Xvec, Yvec, Zvec, &graphmode);
 
   free (Xvec);
Index: trunk/Ohana/src/opihi/dvo/pcat.c
===================================================================
--- trunk/Ohana/src/opihi/dvo/pcat.c	(revision 13344)
+++ trunk/Ohana/src/opihi/dvo/pcat.c	(revision 13479)
@@ -6,5 +6,5 @@
   
   double Radius;
-  int i, j, N, Nregions, ShowAll, NPTS, Npts, leftside;
+  int i, j, kapa, N, Nregions, ShowAll, NPTS, Npts, leftside;
   RegionFile *regions;
   char filename[128];
@@ -14,5 +14,5 @@
   double X[4], Y[4], Rmin, Rmax, Rmid;
   char catdir[256];
-  int Ngraph, VERBOSE;
+  int VERBOSE;
 
   VarConfig ("CATDIR", "%s", catdir);
@@ -33,6 +33,5 @@
     return (FALSE);
   }
-  Ngraph = 0;
-  if (!GetGraph (&graphmode, NULL, &Ngraph)) return (FALSE);
+  if (!GetGraph (&graphmode, &kapa, NULL)) return (FALSE);
   
   Radius = MAX (fabs(graphmode.xmax), fabs(graphmode.ymax));
@@ -84,7 +83,5 @@
     graphmode.ptype = 100; /* connect pairs of points */
     graphmode.etype = 0;
-    PrepPlotting (Npts, &graphmode);
-    PlotVector (Npts, Xvec.elements);
-    PlotVector (Npts, Yvec.elements);
+    PlotVectorPair (kapa, Npts, Xvec.elements, Yvec.elements, &graphmode);
   }
 
Index: trunk/Ohana/src/opihi/dvo/photometry.c
===================================================================
--- trunk/Ohana/src/opihi/dvo/photometry.c	(revision 13344)
+++ trunk/Ohana/src/opihi/dvo/photometry.c	(revision 13479)
@@ -174,4 +174,6 @@
 }
 
+// XXX moved to dbFields
+# if (0)
 int GetMagMode (char *string) {
 
@@ -188,4 +190,5 @@
   return (mode);
 }
+# endif
 
 int GetMeasureParam (char *parname) {
Index: trunk/Ohana/src/opihi/dvo/pmeasure.c
===================================================================
--- trunk/Ohana/src/opihi/dvo/pmeasure.c	(revision 13344)
+++ trunk/Ohana/src/opihi/dvo/pmeasure.c	(revision 13479)
@@ -5,5 +5,5 @@
   
   FILE *f;
-  int i, j, k, m, Narg, Npts, NPTS, status, VERBOSE;
+  int i, j, k, m, kapa, Narg, Npts, NPTS, status, VERBOSE;
   double Mz, Mr, mag;
   double Radius, Rmin, Rmax, R, D;
@@ -19,5 +19,5 @@
 
   if (!InitPhotcodes ()) return (FALSE);
-  if (!style_args (&graphmode, &argc, argv, 0)) return FALSE;
+  if (!style_args (&graphmode, &argc, argv, &kapa)) return FALSE;
 
   f = (FILE *) NULL;
@@ -75,5 +75,4 @@
     return (FALSE);
   }
-  SetGraph (graphmode);
   graphmode.style = 2; /* set style to points */
   graphmode.size = -1; /* point size determined by Zvec */
@@ -144,5 +143,5 @@
 	}
 	if (Npts > NCHUNK) {
-	    PlotVectorTriplet (Npts, Xvec, Yvec, Zvec, &graphmode);
+	    PlotVectorTriplet (kapa, Npts, Xvec, Yvec, Zvec, &graphmode);
 	    Npts = 0;
 	}
@@ -151,5 +150,5 @@
     dvo_catalog_free (&catalog);
   }
-  if (Npts > 0) PlotVectorTriplet (Npts, Xvec, Yvec, Zvec, &graphmode);
+  if (Npts > 0) PlotVectorTriplet (kapa, Npts, Xvec, Yvec, Zvec, &graphmode);
 
   free (Xvec);
Index: trunk/Ohana/src/opihi/dvo/procks.c
===================================================================
--- trunk/Ohana/src/opihi/dvo/procks.c	(revision 13344)
+++ trunk/Ohana/src/opihi/dvo/procks.c	(revision 13479)
@@ -16,5 +16,5 @@
   FILE *f;
   Vector Xvec, Yvec;
-  int i, j, N, NROCKS;
+  int kapa, i, j, N, NROCKS;
   int N0, N1, SpeedClip, Reload;
   unsigned int t0, t1;
@@ -23,9 +23,7 @@
   Graphdata graphmode;
   char rockcat[256];
-  int Ngraph;
 
-  Ngraph = 0;
   VarConfig ("ROCK_CATALOG", "%s", rockcat);
-  if (!GetGraph (&graphmode, NULL, &Ngraph)) return (FALSE);
+  if (!GetGraph (&graphmode, &kapa, NULL)) return (FALSE);
 
   f = (FILE *) NULL;
@@ -115,8 +113,5 @@
   graphmode.style = 2; /* set style to points */
   graphmode.etype = 0; /* no errorbars */
-  PrepPlotting (N, &graphmode);
-  
-  PlotVector (N, Xvec.elements);
-  PlotVector (N, Yvec.elements);
+  PlotVectorPair (kapa, N, Xvec.elements, Yvec.elements, &graphmode);
 
   /* now plot vectors between two extrema */
@@ -148,8 +143,6 @@
   graphmode.ptype = 100; /* connect pairs */
   graphmode.etype = 0; /* no errorbars */
-  PrepPlotting (N, &graphmode);
-  
-  PlotVector (N, Xvec.elements);
-  PlotVector (N, Yvec.elements);
+
+  PlotVectorPair (kapa, N, Xvec.elements, Yvec.elements, &graphmode);
 
   free (Xvec.elements);
Index: trunk/Ohana/src/opihi/dvo/region_list.c
===================================================================
--- trunk/Ohana/src/opihi/dvo/region_list.c	(revision 13344)
+++ trunk/Ohana/src/opihi/dvo/region_list.c	(revision 13479)
@@ -95,5 +95,4 @@
 
   double Radius;
-  int Ngraph;
   Graphdata graphsky;
   SkyList *skylist;
@@ -121,6 +120,5 @@
   }
 
-  Ngraph = 0;
-  if (!GetGraphData (&graphsky, NULL, &Ngraph)) {
+  if (!GetGraphData (&graphsky, NULL, NULL)) {
     gprint (GP_ERR, "region display not available\n");
     return (NULL);
Index: trunk/Ohana/src/opihi/dvo/scripts/navigate
===================================================================
--- trunk/Ohana/src/opihi/dvo/scripts/navigate	(revision 13344)
+++ trunk/Ohana/src/opihi/dvo/scripts/navigate	(revision 13479)
@@ -11,5 +11,5 @@
   $KEY = "none"
   while ("$KEY" != "q")
-    cursor -g 1
+    cursor 1
 
     # help list
@@ -147,5 +147,5 @@
       $ok = $KEY
       echo "type 'd' again at endpoint"
-      cursor -g 1
+      cursor 1
       $r1 = $R$KEY
       $d1 = $D$KEY
@@ -168,5 +168,5 @@
       $ok = $KEY
       echo "type 'z' again at radius"
-      cursor -g 1
+      cursor 1
       $r1 = $R$KEY
       $d1 = $D$KEY
@@ -400,5 +400,5 @@
     if ("$KEY" == "p") 
       echo "P - new coords; p - old coords"
-      cursor -g 1
+      cursor 1
       exec echo $Rp $Dp $RP $DP >> fix.coords
     end
Index: trunk/Ohana/src/opihi/dvo/showtile.c
===================================================================
--- trunk/Ohana/src/opihi/dvo/showtile.c	(revision 13344)
+++ trunk/Ohana/src/opihi/dvo/showtile.c	(revision 13479)
@@ -5,5 +5,5 @@
 int showtile (int argc, char **argv) {
 
-  int Nd, N, NPTS, Ngraph, status, i, InPic;
+  int kapa, Nd, N, NPTS, status, i, InPic;
   Graphdata graphmode;
   Coords coords;
@@ -13,7 +13,5 @@
 
   /* show tile pattern in viewed region */
-
-  Ngraph = 0;
-  if (!GetGraph (&graphmode, NULL, &Ngraph)) return (FALSE);
+  if (!GetGraph (&graphmode, &kapa, NULL)) return (FALSE);
 
   if (argc != 1) {
@@ -88,7 +86,5 @@
     graphmode.ptype = 100; /* connect pairs of points */
     graphmode.etype = 0;
-    PrepPlotting (N, &graphmode);
-    PlotVector (N, Xvec.elements);
-    PlotVector (N, Yvec.elements);
+    PlotVectorPair (kapa, N, Xvec.elements, Yvec.elements, &graphmode);
   }
 
Index: trunk/Ohana/src/opihi/dvo/simage.c
===================================================================
--- trunk/Ohana/src/opihi/dvo/simage.c	(revision 13344)
+++ trunk/Ohana/src/opihi/dvo/simage.c	(revision 13479)
@@ -12,10 +12,8 @@
   Header header;
   Coords coords;
-  int i, j, Nstars, nstars, Nbytes, nbytes, Npts, N;
+  int i, j, kapa, Nstars, nstars, Nbytes, nbytes, Npts, N;
   Graphdata graphmode;
-  int Ngraph;
 
-  Ngraph = 0;
-  if (!GetGraph (&graphmode, NULL, &Ngraph)) return (FALSE);
+  if (!GetGraph (&graphmode, &kapa, NULL)) return (FALSE);
 
   zero = 17.0;
@@ -156,9 +154,6 @@
   graphmode.etype = 0;
   Npts = Xvec.Nelements;
-  PrepPlotting (Npts, &graphmode);
 
-  PlotVector (Npts, Xvec.elements);
-  PlotVector (Npts, Yvec.elements);
-  PlotVector (Npts, Zvec.elements);
+  PlotVectorTriplet (kapa, Npts, Xvec.elements, Yvec.elements, Zvec.elements, &graphmode);
 
   free (Xvec.elements);
Index: trunk/Ohana/src/opihi/dvo/skycat.c
===================================================================
--- trunk/Ohana/src/opihi/dvo/skycat.c	(revision 13344)
+++ trunk/Ohana/src/opihi/dvo/skycat.c	(revision 13479)
@@ -6,5 +6,5 @@
   
   double Radius;
-  int i, j, N, Nregions, ShowAll, NPTS, Npts, leftside, Depth, VERBOSE;
+  int i, j, N, Nregions, kapa, ShowAll, NPTS, Npts, leftside, Depth, VERBOSE;
   struct stat filestat;
   Vector Xvec, Yvec;
@@ -32,5 +32,5 @@
   }
 
-  if (!style_args (&graphmode, &argc, argv, 0)) return FALSE;
+  if (!style_args (&graphmode, &argc, argv, &kapa)) return FALSE;
 
   if (argc != 1) {
@@ -38,5 +38,4 @@
     return (FALSE);
   }
-  SetGraph (graphmode);
 
   Radius = MAX (fabs(graphmode.xmax), fabs(graphmode.ymax));
@@ -97,7 +96,5 @@
     graphmode.ptype = 100; /* connect pairs of points */
     graphmode.etype = 0;
-    PrepPlotting (Npts, &graphmode);
-    PlotVector (Npts, Xvec.elements);
-    PlotVector (Npts, Yvec.elements);
+    PlotVectorPair (kapa, Npts, Xvec.elements, Yvec.elements, &graphmode);
   }
 
Index: trunk/Ohana/src/opihi/include/data.h
===================================================================
--- trunk/Ohana/src/opihi/include/data.h	(revision 13344)
+++ trunk/Ohana/src/opihi/include/data.h	(revision 13479)
@@ -162,5 +162,5 @@
 void          SetLimitsRaw          PROTO((float *xvec, float *yvec, int Npts, Graphdata *graphmode));
 void          ApplyLimits           PROTO((int Xgraph, Graphdata *graphmode, int apply));
-int           style_args            PROTO((Graphdata *graphmode, int *argc, char **argv, int Nforce));
+int           style_args            PROTO((Graphdata *graphmode, int *argc, char **argv, int *kapa));
 
 int read_table_vectors (int argc, char **argv, char *extname);
Index: trunk/Ohana/src/opihi/include/display.h
===================================================================
--- trunk/Ohana/src/opihi/include/display.h	(revision 13344)
+++ trunk/Ohana/src/opihi/include/display.h	(revision 13479)
@@ -5,43 +5,24 @@
 # define DISPLAY_H
 
-# if (0)
-typedef struct {
-  double xmin, xmax, ymin, ymax;
-  int style, ptype, ltype, etype, ebar, color;
-  double lweight, size;
-  Coords coords;
-  int flipeast, flipnorth;
-  char axis[8], labels[8], ticks[8];
-} Graphdata;
-# endif
+/*** kapa graph functions ***/
+int           PlotVectorPair        PROTO((int kapa, int Npts, float *xValues, float *yValues, Graphdata *graphmode));
+int           PlotVectorTriplet     PROTO((int kapa, int Npts, float *xValues, float *yValues, float *zValues, Graphdata *graphmode));
+int           GetGraphData          PROTO((Graphdata *data, int *kapa, char *name));
+int           GetGraph              PROTO((Graphdata *data, int *kapa, char *name));
+int           SetGraph              PROTO((Graphdata *data));
 
-/*** plotting functions ***/
-int           GetCurrentDevice      PROTO((void));
-int           PrepPlotting          PROTO((int Npts, Graphdata *graphmode));
-int           PlotVector            PROTO((int, float *));
-int           PlotVectorTriplet     PROTO((int N, float *xValues, float *yValues, float *zValues, Graphdata *graphmode));
-int           GetGraphData          PROTO((Graphdata *data, int *sock, int *N));
-int           GetGraph              PROTO((Graphdata *data, int *socket, int *N));
-void          InitGraph             PROTO((void));
-int           open_graph            PROTO((int N));
-int           close_graph           PROTO((int N));
-void          QuitGraph             PROTO((void));
-void          SetGraph              PROTO((Graphdata data));
-void          XGraphDead            PROTO((int input));
-int           GetColor              PROTO((char *name));
+/*** kapa image functions */
+int           GetImageData          PROTO((KapaImageData *data, int *kapa, char *name));
+int           GetImage              PROTO((KapaImageData *data, int *kapa, char *name));
+int           SetImage              PROTO((KapaImageData *data));
 
-/* image (tv) functions */
-int           GetImage              PROTO((int *socket, int *N));
-char         *GetImageName          PROTO((void));
-void          GetImageScale         PROTO((double *zero, double *range));
-void          InitImage             PROTO((void));
-int           open_image            PROTO((int N));
-int           close_image           PROTO((int N));
-void          QuitImage             PROTO((void));
-void          SetImageDevice        PROTO((int state));
-void          SetImageName          PROTO((char *name));
-void          SetImageScale         PROTO((double zero, double range));
-void          XImageDead            PROTO((int input));
-int           SelectOverlay         PROTO((char *name, int *number));
+void	      QuitKapa              PROTO(());
+void	      InitKapa		    PROTO(());
+int 	      open_kapa		    PROTO((int entry));
+int 	      close_kapa	    PROTO((char *name));
+int 	      AddKapaDevice	    PROTO((char *name));
+int 	      DelKapaDevice	    PROTO((char *name));
+int 	      FindKapaDevice	    PROTO((char *name));
+char	     *GetKapaName	    PROTO(());
 
 /* calling program need to define a function 'get_variable' which
Index: trunk/Ohana/src/opihi/include/dvoshell.h
===================================================================
--- trunk/Ohana/src/opihi/include/dvoshell.h	(revision 13344)
+++ trunk/Ohana/src/opihi/include/dvoshell.h	(revision 13479)
@@ -7,16 +7,36 @@
 
 /* magnitude types */
-enum {MAG_NONE, MAG_INST, MAG_CAT, MAG_SYS, MAG_REL, MAG_CAL, MAG_AVE, MAG_REF};
+enum {MAG_NONE, MAG_INST, MAG_CAT, MAG_SYS, MAG_REL, MAG_CAL, MAG_AVE, MAG_REF, MAG_ERR, MAG_CHISQ};
 
-/* measure params */
+/* measure fields */
 enum {MEAS_ZERO, MEAS_RA, MEAS_DEC, MEAS_MAG, MEAS_dMAG, MEAS_MINST, MEAS_MCAT, 
       MEAS_MSYS, MEAS_MREL, MEAS_MCAL, MEAS_PHOTCODE, MEAS_TIME, MEAS_dR, MEAS_dD, 
       MEAS_FWHM, MEAS_DOPHOT, MEAS_XCCD, MEAS_YCCD, MEAS_XMOSAIC, MEAS_YMOSAIC, MEAS_FLAGS, MEAS_EXPTIME, MEAS_AIRMASS};
 
-/* average params */
+/* average fields */
 enum {AVE_ZERO, AVE_RA, AVE_DEC, AVE_RA_ERR, AVE_DEC_ERR, AVE_U_RA, AVE_U_DEC, 
       AVE_U_RA_ERR, AVE_U_DEC_ERR, AVE_PAR, AVE_PAR_ERR, AVE_MAG, AVE_REF, AVE_dMAG, 
       AVE_Xm, AVE_Xp, AVE_NMEAS, AVE_NMISS, AVE_TYPE, AVE_NPHOT, AVE_NCODE, AVE_NCRIT, 
       AVE_FLAG, AVE_TYPEFRAC};
+
+enum {DVO_TABLE_AVERAGE, DVO_TABLE_MEASURE};
+
+// a single db field 
+typedef struct {
+  char *name;
+  int extract;
+  int table;
+  int ID;
+  int magMode;
+  PhotCode *photcode;
+} dbField;
+
+// db boolean operations
+typedef struct {
+  char   *name;
+  char    type;
+  int     field;
+  float   Float;
+} dbStack;
 
 typedef struct {
@@ -96,3 +116,24 @@
 int           RD_to_XYpic           PROTO((double *x, double *y, double r, double d, Coords *coords, double Rmin, double Rmax, double Rmid, int *leftside));
 
+// dvo DB field functions
+dbField     *dbCmdlineFields        PROTO((int argc, char **argv, int table, int *last, int *nfields));
+dbStack     *dbRPN                  PROTO((int argc, char **argv, int *nstack));
+int          dbCheckStack           PROTO((dbStack *stack, int Nstack, int table, dbField **inFields, int *Nfields));
+int          dbBooleanCond          PROTO((dbStack *inStack, int NinStack, float *fields));
+void         dbInitStack            PROTO((dbStack *stack));
+void 	     dbFreeStack            PROTO((dbStack *stack, int Nstack));
+void 	     dbFreeEntry            PROTO((dbStack *stack));
+
+dbStack     *dbBinary               PROTO((dbStack *V1, dbStack *V2, char *op, float *fields));
+dbStack     *dbUnary                PROTO((dbStack *V1, char *op, float *fields));
+
+int          GetMagMode             PROTO((char *string));
+PhotCode    *ParsePhotcodeField     PROTO((char *field, int *mode, int def));
+int          ParseMeasureField      PROTO((dbField *field, char *fieldName));
+int          ParseAverageField      PROTO((dbField *field, char *fieldName));
+
+double       dbExtractAverages      PROTO((Average *average, SecFilt *secfilt, Measure *measure, dbField *field));
+double       dbExtractMeasures      PROTO((Average *average, SecFilt *secfilt, Measure *measure, dbField *field));
+void         dbExtractMeasuresInit  PROTO(());
+
 # endif
Index: trunk/Ohana/src/opihi/lib.data/Makefile
===================================================================
--- trunk/Ohana/src/opihi/lib.data/Makefile	(revision 13344)
+++ trunk/Ohana/src/opihi/lib.data/Makefile	(revision 13479)
@@ -29,9 +29,11 @@
 $(SDIR)/gaussian.$(ARCH).o		\
 $(SDIR)/graphtools.$(ARCH).o            \
-$(SDIR)/open_graph.$(ARCH).o            \
-$(SDIR)/open_image.$(ARCH).o            \
 $(SDIR)/queues.$(ARCH).o		\
 $(SDIR)/PlotVectors.$(ARCH).o		\
+$(SDIR)/open_kapa.$(ARCH).o             \
 $(SDIR)/style_args.$(ARCH).o
+
+#$(SDIR)/open_graph.$(ARCH).o            \
+#$(SDIR)/open_image.$(ARCH).o            \
 
 # dependancy rules for include files ########################
Index: trunk/Ohana/src/opihi/lib.data/PlotVectors.c
===================================================================
--- trunk/Ohana/src/opihi/lib.data/PlotVectors.c	(revision 13344)
+++ trunk/Ohana/src/opihi/lib.data/PlotVectors.c	(revision 13479)
@@ -1,52 +1,20 @@
 # include "display.h"
 
-static int Xgraph;
+int PlotVectorPair (int kapa, int Npts, float *xValues, float *yValues, Graphdata *graphmode) {
 
-int PrepPlotting (int Npts, Graphdata *graphmode) {
+  KapaPrepPlot (kapa, Npts, graphmode);
+  KapaPlotVector (kapa, Npts, xValues, "x");
+  KapaPlotVector (kapa, Npts, yValues, "y");
 
-  if (!GetGraph (NULL, &Xgraph, NULL)) return (FALSE);
-  
-  /* tell kapa to look for the incoming image */
-  KiiSendCommand (Xgraph, 4, "PLOT"); 
-  
-  /* send Xgraph the plot details */
-  KiiSendMessage (Xgraph, "%8d %8d %d %d %d %d %d %f %f", 
-		  Npts, graphmode[0].style, 
-		  graphmode[0].ptype, graphmode[0].ltype, 
-		  graphmode[0].etype, graphmode[0].ebar, graphmode[0].color, 
-		  graphmode[0].lweight, graphmode[0].size);
-  KiiSendMessage (Xgraph, "%g %g %g %g", 
-		  graphmode[0].xmin, graphmode[0].xmax, 
-		  graphmode[0].ymin, graphmode[0].ymax);
   return (TRUE);
 }
 
-int PlotVector (int Npts, float *values) {
+int PlotVectorTriplet (int kapa, int Npts, float *xValues, float *yValues, float *zValues, Graphdata *graphmode) {
 
-  int Nbytes;
+  KapaPrepPlot   (kapa, Npts, graphmode);
+  KapaPlotVector (kapa, Npts, xValues, "x");
+  KapaPlotVector (kapa, Npts, yValues, "y");
+  KapaPlotVector (kapa, Npts, zValues, "z");
 
-  Nbytes = Npts * sizeof (float);
-  write (Xgraph, values, Nbytes);
   return (TRUE);
 }
-
-int PlotVectorPair (int N, float *xValues, float *yValues, Graphdata *graphmode) {
-
-    PrepPlotting (N, graphmode);
-    PlotVector (N, xValues);
-    PlotVector (N, yValues);
-
-    return (TRUE);
-}
-
-int PlotVectorTriplet (int N, float *xValues, float *yValues, float *zValues, Graphdata *graphmode) {
-
-    PrepPlotting (N, graphmode);
-    PlotVector (N, xValues);
-    PlotVector (N, yValues);
-    PlotVector (N, zValues);
-
-    return (TRUE);
-}
-
-/* we don't use GetGraph in PlotVector because it flushes the connection */
Index: trunk/Ohana/src/opihi/lib.data/graphtools.c
===================================================================
--- trunk/Ohana/src/opihi/lib.data/graphtools.c	(revision 13344)
+++ trunk/Ohana/src/opihi/lib.data/graphtools.c	(revision 13479)
@@ -33,5 +33,5 @@
     graphmode[0].ymax = maxY + 0.05*range;
   }
-  SetGraph (graphmode[0]);
+  SetGraph (graphmode);
 
   set_variable ("XMIN", graphmode[0].xmin);
@@ -73,5 +73,5 @@
     graphmode[0].ymax = maxY + 0.05*range;
   }
-  SetGraph (graphmode[0]);
+  SetGraph (graphmode);
 
   set_variable ("XMIN", graphmode[0].xmin);
Index: trunk/Ohana/src/opihi/lib.data/open_image.c
===================================================================
--- trunk/Ohana/src/opihi/lib.data/open_image.c	(revision 13344)
+++ trunk/Ohana/src/opihi/lib.data/open_image.c	(revision 13479)
@@ -122,5 +122,6 @@
   return (Ximbuffer[Active]);
 }
-  
+ 
+// leave this information on kapa, request when needed? 
 void SetImageScale (double zero, double range) {
   Xzero[Active] = zero;
Index: trunk/Ohana/src/opihi/lib.data/open_kapa.c
===================================================================
--- trunk/Ohana/src/opihi/lib.data/open_kapa.c	(revision 13344)
+++ trunk/Ohana/src/opihi/lib.data/open_kapa.c	(revision 13479)
@@ -1,4 +1,6 @@
 # include "display.h"
 # include "shell.h"
+# include <assert.h>
+
 
 /* kapa support for the new version of kapa (v2.0), which has both graph and image
@@ -9,53 +11,84 @@
 
 /* list of available socket connections */
-static int        Active;        // currently active socket (not device)
+static int        Active;        // currently active socket entry (index value, not socket value)
 static int       *Socket = NULL; // list of available sockets
-static int       *Device = NULL; // list of device numbers for each socked
-static int       Nsocket = 0;    // number of available sockets / devices
+static char     **Device = NULL; // list of device names for each socket
+static int       Ndevice = 0;    // number of available sockets / devices
 
 void InitKapa () {
 
-  Active  = -1;		      // -1 is the INVALID entry
-  Nsocket = 0;		      // number of defined sockets
-  ALLOCATE (Device, int, 1);
-  ALLOCATE (Socket, int, 1);
-  Device[Active] = 0;  
-  Socket[Active] = 0;  
-}
-
-// returns the number of the requested device, or -1 if not found
-int FindKapaDevice (int thisDevice) {
+  Active  = -1;					  // -1 is the INVALID entry
+  Ndevice = 0;					  // number of defined sockets
+  ALLOCATE (Device, char *, 1);			  // for future REALLOCATE calls
+  ALLOCATE (Socket, int, 1);			  // for future REALLOCATE calls
+}
+
+// add new device name if not found
+int AddKapaDevice (char *name) {
+
+  int N;
+
+  N = FindKapaDevice (name);
+  if (N != -1) return (N);
+  N = Ndevice;
+  Ndevice ++;
+  REALLOCATE (Device, char *, Ndevice);
+  REALLOCATE (Socket, int, Ndevice);
+  Device[N] = strcreate (name);
+  Socket[N] = -1;
+  return (N);
+}
+
+// delete device by name, close if not closed
+int DelKapaDevice (char *name) {
+
+  int i, N;
+
+  N = FindKapaDevice (name);
+  if (N == -1) return (FALSE);
+
+  if (Socket[N] != -1) close (Socket[N]);
+  free (Device[N]);
+  for (i = N; i < Ndevice - 1; i++) {
+    Device[i] = Device[i+1];
+    Socket[i] = Socket[i+1];
+  }
+
+  if (N == Active) {
+    Active = -1;
+  }
+
+  Ndevice --;
+  REALLOCATE (Device, char *, Ndevice);
+  REALLOCATE (Socket, int, Ndevice);
+
+  return (TRUE);
+}
+
+// returns the entry of the requested device, or -1 if not found
+int FindKapaDevice (char *name) {
 
   int i;
 
-  for (i = 0; i < Nsocket; i++) {
-    if (Device[i] == thisDevice) {
-      return (i);
-    }
+  if (name == NULL) return (-1); 
+
+  for (i = 0; i < Ndevice; i++) {
+    if (!strcmp(Device[i], name)) return (i);
   }
   return (-1);
 }
 
-// set the active device to the given device number, open if needed
-int open_kapa (int thisDevice) {
-
-  int fd, entry;
-  char *kapa_exec, name[16];
+// set the active device to the given device, open if needed
+int open_kapa (int entry) {
+
+  int fd;
+  char *kapa_exec, *kapa_name;
 
   // find the given device number, or create. set this to active
-  entry = FindKapaDevice (thisDevice);
-  if (entry < 0) {
-    Active = Nsocket;
-    Nsocket ++;
-    REALLOCATE (Device, int, Nsocket);
-    REALLOCATE (Socket, int, Nsocket);
-    Device[Active] = -1;
-    Socket[Active] = -1;
-  } else {
-    Active = entry;
-  }
+  assert (entry >= 0);
+  assert (entry <  Ndevice);
 
   // if the (now) active socket is not open, open it
-  if (Socket[Active] < 0) {
+  if (Socket[entry] < 0) {
     kapa_exec = get_variable ("KAPA");
     if (kapa_exec == (char *) NULL) {
@@ -65,77 +98,50 @@
 
     // KAPA may be either kapa://host or /path/to/program
-
-    snprintf (name, 16, "[%d]", thisDevice);
-    fd = KapaOpen (kapa_exec, name);
+    ALLOCATE (kapa_name, char, strlen(Device[entry]) + 5);
+    snprintf (kapa_name, strlen(Device[entry]) + 5, "[%s]", Device[entry]);
+    fd = KapaOpen (kapa_exec, kapa_name);
     free (kapa_exec);
+    free (kapa_name);
 
     if (fd < 0) {
-      gprint (GP_ERR, "error starting kapa\n");
-      // delete the active entry
+      gprint (GP_ERR, "error starting kapa device %s\n", Device[entry]);
       return (FALSE);
     } 
-    Device[Active] = thisDevice;
-    Socket[Active] = fd;
+    Socket[entry] = fd;
   } 
-  return (TRUE);
-}
-
-/**************** graph ops */
-
-// XXX for now, use a local variable.  later: get from kapa
-static Graphdata graphdata;
-
-void InitGraph () {
-  graphdata.xmin = graphdata.ymin = 0.0;
-  graphdata.xmax = graphdata.ymax = 1.0;
-  graphdata.style = graphdata.ptype = 0;
-  graphdata.ltype = graphdata.color = 0;
-  graphdata.etype = graphdata.ebar = 0;
-  graphdata.lweight = graphdata.size = 1.0;
-    
-  graphdata.coords.pc1_1 = graphdata.coords.pc2_2 = 1.0;
-  graphdata.coords.pc1_2 = graphdata.coords.pc2_1 = 0.0;
-  strcpy (graphdata.coords.ctype, "RA---LIN");
-  graphdata.coords.crval1 = 0.0;
-  graphdata.coords.crval2 = 0.0;
-  graphdata.coords.crpix1 = 0.0;
-  graphdata.coords.crpix2 = 0.0;
-  graphdata.coords.cdelt1 = graphdata.coords.cdelt2 = 1.0;
-  graphdata.flipeast = TRUE;
-  graphdata.flipnorth = FALSE;
-  strcpy (graphdata.axis, "2222");
-  strcpy (graphdata.ticks, "2222");
-  strcpy (graphdata.labels, "2222");
-}
-
-# if (0)
-  /* test Xgraph[0], flush junk from pipe */
-  signal (SIGPIPE, XGraphDead);
-  fcntl (Socket[n], F_SETFL,  O_NONBLOCK); 
-  for (i = 0; (read (Socket[n], buffer, 64) > 0) && (i < 20); i++);
-  fcntl (Socket[n], F_SETFL, !O_NONBLOCK); 
-# endif
-
-/* return pointers for current Xgraph, set if desired, test, open if needed */
-int GetGraph (Graphdata *data, int *fd, int *dev) {
-
-  int thisDevice;
-
-  SetImageDevice (FALSE);
-  if (dev == NULL) {
-    if (Active < 0) {
-      thisDevice = 0;
+  Active = entry;
+  return (TRUE);
+}
+
+/**************** graph specific ops *******************/
+
+// return the current device name, if set 
+char *GetKapaName () {
+  if (Active < 0) return NULL;
+  return Device[Active];
+}
+
+/* return pointers for named device or current; open if needed */
+// if fd == NULL, don't return the value
+// if name == NULL, use the currently active device
+int GetGraph (Graphdata *data, int *fd, char *name) {
+
+  int entry;
+
+  if (name == NULL) {
+    if (Active < 0) {
+      entry = AddKapaDevice ("0");
     } else {
-      thisDevice = Device[Active];
-    }
-  } else {
-    thisDevice = *dev;
-  }
-  
-  if (!open_kapa (thisDevice)) {
-    return (FALSE);
-  }
-  
-  if (data != NULL) *data = graphdata;
+      entry = Active;
+    }
+  } else {
+    entry = AddKapaDevice (name);
+  }
+  
+  if (!open_kapa (entry)) {
+    return (FALSE);
+  }
+  
+  if (data != NULL) KapaGetGraphData (Socket[Active], data);
   if (fd != NULL) *fd = Socket[Active];
 
@@ -143,84 +149,130 @@
 }
 
-/* return pointers for given Xgraph, don't set or open */
-int GetGraphData (Graphdata *data, int *fd, int *dev) {
-
-  int n;
-
-  if (dev == NULL) {
-    if (Active < 0) {
-      gprint (GP_ERR, "invalid Xgraph window %d\n", *dev); 
-      return (FALSE);
-    }
-    *fd = Socket[Active];
-    return (TRUE);
-  } 
-
-  n = FindKapaDevice (*dev);
-  if (n < 0) {
-    gprint (GP_ERR, "invalid Xgraph window %d\n", *dev); 
-    return (FALSE);
-  }
-
-  Active = n;
-
+/* return pointers for given kapa, don't set or open */
+int GetGraphData (Graphdata *data, int *fd, char *name) {
+
+  int entry;
+
+  if (name == NULL) {
+    if (Active < 0) {
+      gprint (GP_ERR, "no active kapa window\n"); 
+      return (FALSE);
+    }
+    entry = Active;
+  } else {
+    entry = FindKapaDevice (name);
+    if (entry < 0) {
+      gprint (GP_ERR, "invalid kapa window %s\n", name); 
+      return (FALSE);
+    }
+  }
+
+  if (fd != NULL) *fd = Socket[entry];
+  if (data != NULL) KapaGetGraphData (Socket[entry], data);
+  return (TRUE);
+}
+
+/* assign given values to current kapa */
+int SetGraph (Graphdata *data) {
+  if (Active < 0) {
+    gprint (GP_ERR, "no active kapa window\n"); 
+    return (FALSE);
+  }
+  if (Socket[Active] == -1) {
+    gprint (GP_ERR, "no active kapa window\n"); 
+    return (FALSE);
+  }
+  KapaSetGraphData (Socket[Active], data);
+}
+
+/************* image ops ***********/
+
+/* return pointers for current Ximage, set if desired, test, open if needed */
+int GetImage (KapaImageData *data, int *fd, char *name) {
+
+  int entry;
+
+  if (name == NULL) {
+    if (Active < 0) {
+      entry = AddKapaDevice ("0");
+    } else {
+      entry = Active;
+    }
+  } else {
+    entry = AddKapaDevice (name);
+  }
+  
+  if (!open_kapa (entry)) {
+    return (FALSE);
+  }
+  
+  if (data != NULL) KapaGetImageData (Socket[Active], data);
   if (fd != NULL) *fd = Socket[Active];
-  if (data != NULL) *data = graphdata;
-
-  return (TRUE);
-}
-
-/* assign given values to current Xgraph */
-void SetGraph (Graphdata data) {
-  graphdata = data;
-}
-
-/** internal tracking of current active device type **/
-/* drop this stuff */
-static int       IsImage = FALSE;
-
-int GetCurrentDevice () {
-  return (IsImage);
-}
-
-void SetImageDevice (int state) {
-  IsImage = state;
-}
-
-/************* image ops ***********/
-// XXX for now, use local static:
-
-static char      Ximbuffer[512];
-static double    Xzero;
-static double    Xrange;
-
-void InitImage () {
-  Xzero = 0;
-  Xrange = 1024;
-  strcpy (Ximbuffer, "none");
-}
-
-/* return pointers for current Ximage, set if desired, test, open if needed */
-int GetImage (int *fd, int *N) {
-  int status;
-  status = GetGraph (NULL, fd, N);
-  return (status);
-}
-
-void SetImageName (char *name) {
-  strcpy (Ximbuffer, name);
-}
-
-char *GetImageName () {
-  return (Ximbuffer);
-}
- 
-// leave this information on kapa, request when needed? 
-void SetImageScale (double zero, double range) {
-  Xzero = zero;
-  Xrange = range;
-}
-void GetImageScale (double *zero, double *range) {
-  *zero = Xzero;
-  *range = Xrange;
-}
+
+  return (TRUE);
+}
+
+/* return pointers for given kapa, don't set or open */
+int GetImageData (KapaImageData *data, int *fd, char *name) {
+
+  int entry;
+
+  if (name == NULL) {
+    if (Active < 0) {
+      gprint (GP_ERR, "no active kapa window\n"); 
+      return (FALSE);
+    }
+    entry = Active;
+  } else {
+    entry = FindKapaDevice (name);
+    if (entry < 0) {
+      gprint (GP_ERR, "invalid kapa window %s\n", name); 
+      return (FALSE);
+    }
+  }
+
+  if (fd != NULL) *fd = Socket[entry];
+  if (data != NULL) KapaGetImageData (Socket[entry], data);
+  return (TRUE);
+}
+
+/* assign given values to current kapa */
+int SetImage (KapaImageData *data) {
+  if (Active < 0) {
+    gprint (GP_ERR, "no active kapa window\n"); 
+    return (FALSE);
+  }
+  if (Socket[Active] == -1) {
+    gprint (GP_ERR, "no active kapa window\n"); 
+    return (FALSE);
+  }
+  KapaSetImageData (Socket[Active], data);
+}
+
+int close_kapa (char *name) {
+
+  int N;
+
+  N = FindKapaDevice (name);
+  if (N == -1) {
+    if (Active < 0) return (FALSE);
+    name = Device[Active];
+  }
+  DelKapaDevice (name);
+  return (TRUE);
+}
+
+void QuitKapa () {
+
+  int i;
+  
+  for (i = 0; i < Ndevice; i++) {
+    if (Socket[i] != -1) close (Socket[i]);
+    if (Device[i] != NULL) free (Device[i]);
+  }
+  REALLOCATE (Socket, int, 1);
+  REALLOCATE (Device, char *, 1);
+
+  Ndevice = 0;
+  Active = -1;
+}
Index: trunk/Ohana/src/opihi/lib.data/style_args.c
===================================================================
--- trunk/Ohana/src/opihi/lib.data/style_args.c	(revision 13344)
+++ trunk/Ohana/src/opihi/lib.data/style_args.c	(revision 13479)
@@ -2,22 +2,24 @@
 # include "display.h"
 
-int style_args (Graphdata *graphmode, int *argc, char **argv, int Nforce) {
+int style_args (Graphdata *graphmode, int *argc, char **argv, int *kapa) {
   
-  int N, Ngraph;
-  char *name;
+  int N;
+  char *colorName;
+  char *kapaName;
 
-  Ngraph = -1;
+  kapaName = NULL;
   if ((N = get_argument (*argc, argv, "-n"))) {
     remove_argument (N, argc, argv);
-    Ngraph = atof (argv[N]);
+    kapaName = strcreate (argv[N]);
     remove_argument (N, argc, argv);
   }
-  if (Nforce != -1) Ngraph = Nforce;
-  if (!GetGraph (graphmode, NULL, &Ngraph)) return (FALSE);
+  if (!GetGraph (graphmode, kapa, kapaName)) return (FALSE);
+  FREE (kapaName);
 
   if (*argc == 1) {
-    name = KapaColorName (graphmode[0].color);
-    gprint (GP_ERR, "current style (%d): -x %d -c %s -pt %d -lt %d -lw %f -sz %f\n", Ngraph,
-	     graphmode[0].style, name, graphmode[0].ptype, 
+    kapaName = GetKapaName();
+    colorName = KapaColorName (graphmode[0].color);
+    gprint (GP_ERR, "current style (%s): -x %d -c %s -pt %d -lt %d -lw %f -sz %f\n", kapaName,
+	     graphmode[0].style, colorName, graphmode[0].ptype, 
 	     graphmode[0].ltype, graphmode[0].lweight,
 	     graphmode[0].size);
Index: trunk/Ohana/src/opihi/lib.shell/BufferOps.c
===================================================================
--- trunk/Ohana/src/opihi/lib.shell/BufferOps.c	(revision 13344)
+++ trunk/Ohana/src/opihi/lib.shell/BufferOps.c	(revision 13479)
@@ -195,25 +195,4 @@
 }
 
-int SelectOverlay (char *name, int *number) {
-
-  *number = -1;
-  if (!strcmp (name, "red") || !strcmp (name, "0"))
-    *number = 0;
-  if (!strcmp (name, "green") || !strcmp (name, "1"))
-    *number = 1;
-  if (!strcmp (name, "blue") || !strcmp (name, "2"))
-    *number = 2;
-  if (!strcmp (name, "yellow") || !strcmp (name, "3"))
-    *number = 3;
-
-  if (*number < 0) {
-    gprint (GP_ERR, "valid overlays may be: red (0), green (1), blue (2), yellow (3)\n");
-    return (FALSE);
-  }
-  else {
-    return (TRUE);
-  }
-}
-
 void dump_buffers (int n) {
 
Index: trunk/Ohana/src/opihi/lib.shell/startup.c
===================================================================
--- trunk/Ohana/src/opihi/lib.shell/startup.c	(revision 13344)
+++ trunk/Ohana/src/opihi/lib.shell/startup.c	(revision 13479)
@@ -43,5 +43,4 @@
     else 
       set_str_variable ("HOME", home);
-    set_str_variable ("KII", "kii");
     set_str_variable ("KAPA", "kapa");
     set_variable ("PID", getpid());
Index: trunk/Ohana/src/opihi/mana/mana.c.in
===================================================================
--- trunk/Ohana/src/opihi/mana/mana.c.in	(revision 13344)
+++ trunk/Ohana/src/opihi/mana/mana.c.in	(revision 13479)
@@ -51,6 +51,5 @@
 /* add program-dependent exit functions here */
 void cleanup () {
-  QuitImage ();
-  QuitGraph ();
+  QuitKapa ();
   return;
 }
Index: trunk/Ohana/src/opihi/mana/opihi.c
===================================================================
--- trunk/Ohana/src/opihi/mana/opihi.c	(revision 13344)
+++ trunk/Ohana/src/opihi/mana/opihi.c	(revision 13479)
@@ -64,7 +64,5 @@
 /* add program-dependent exit functions here */
 void cleanup () {
-  /* -libdisplay
-  QuitImage ();
-  QuitGraph (); */
+  QuitKapa ();
   return;
 }
Index: trunk/Ohana/src/opihi/pantasks/pantasks.c.in
===================================================================
--- trunk/Ohana/src/opihi/pantasks/pantasks.c.in	(revision 13344)
+++ trunk/Ohana/src/opihi/pantasks/pantasks.c.in	(revision 13479)
@@ -64,6 +64,5 @@
 /* add program-dependent exit functions here */
 void cleanup () {
-  QuitImage ();
-  QuitGraph ();
+  QuitKapa ();
   QuitController ();
   return;
Index: trunk/Ohana/src/opihi/pantasks/pantasks_client.c.in
===================================================================
--- trunk/Ohana/src/opihi/pantasks/pantasks_client.c.in	(revision 13344)
+++ trunk/Ohana/src/opihi/pantasks/pantasks_client.c.in	(revision 13479)
@@ -54,6 +54,5 @@
 /* add program-dependent exit functions here */
 void cleanup () {
-  QuitImage ();
-  QuitGraph ();
+  QuitKapa ();
   return;
 }
Index: trunk/Ohana/src/opihi/pantasks/pantasks_server.c
===================================================================
--- trunk/Ohana/src/opihi/pantasks/pantasks_server.c	(revision 13344)
+++ trunk/Ohana/src/opihi/pantasks/pantasks_server.c	(revision 13479)
@@ -74,7 +74,6 @@
 /* add program-dependent exit functions here */
 void cleanup () {
-  // QuitImage ();
-  // QuitGraph ();
-  // QuitController ();
+  QuitKapa ();
+  QuitController ();
   return;
 }
