Index: /trunk/Ohana/src/kapa2/include/prototypes.h
===================================================================
--- /trunk/Ohana/src/kapa2/include/prototypes.h	(revision 42939)
+++ /trunk/Ohana/src/kapa2/include/prototypes.h	(revision 42940)
@@ -80,4 +80,5 @@
 int           DefineSection       PROTO((int sock));
 int           DefineSectionByImage PROTO((int sock));
+int           DefineSectionCenterByImage PROTO((int sock));
 int           SetFont             PROTO((int sock));
 int           EraseCurrentPlot    PROTO((void));
Index: /trunk/Ohana/src/kapa2/src/CheckPipe.c
===================================================================
--- /trunk/Ohana/src/kapa2/src/CheckPipe.c	(revision 42939)
+++ /trunk/Ohana/src/kapa2/src/CheckPipe.c	(revision 42940)
@@ -245,4 +245,10 @@
   }
   
+  if (!strcmp (word, "CSEC")) {
+    status = DefineSectionCenterByImage (sock);
+    KiiSendCommand (sock, 4, "DONE");
+    FINISHED (TRUE);
+  }
+  
   if (!strcmp (word, "MSEC")) {
     status = MoveSection (sock);
Index: /trunk/Ohana/src/kapa2/src/DefineSection.c
===================================================================
--- /trunk/Ohana/src/kapa2/src/DefineSection.c	(revision 42939)
+++ /trunk/Ohana/src/kapa2/src/DefineSection.c	(revision 42940)
@@ -9,5 +9,15 @@
   Section *section;
 
+  // XXX the background must be set using KapaSectionBG
   KiiScanMessage (sock, "%s %lf %lf %lf %lf %d", name, &x, &y, &dx, &dy, &bg);
+
+  // truncate section to 0.0 - 1.0
+  x = MAX(MIN(x,1.0),0.0);
+  y = MAX(MIN(y,1.0),0.0);
+
+  double xmax = MAX(MIN(x + dx, 1.0), 0.0);
+  double ymax = MAX(MIN(y + dy, 1.0), 0.0);
+  dx = xmax - x;
+  dy = ymax - y;
 
   MoveSection = FALSE;
@@ -15,5 +25,6 @@
   N = GetSectionByName (name);
   if (N < 0) {
-    section = AddSection (name, x, y, dx, dy, bg);
+    // new section is added with transparent background
+    section = AddSection (name, x, y, dx, dy, -1);
     MoveSection = TRUE;
   } else {
@@ -30,5 +41,6 @@
     section[0].dx = dx;
     section[0].dy = dy;
-    section[0].bg = bg;
+    // do not change bg (must use KapaSectionBG)
+    // section[0].bg = bg;
   }
 
@@ -56,4 +68,8 @@
 
   N = GetSectionByName (name);
+  if (N < 0)  {
+    fprintf (stderr, "section does not yet exist\n");
+    return (TRUE);
+  }
   section = GetSectionByNumber (N);
   image = section->image;
@@ -82,5 +98,5 @@
   section[0].x = x;
   section[0].y = y;
-  section[0].bg = bg;
+  // section[0].bg = bg;
   section[0].dx = dx / graphic[0].dx;
   section[0].dy = dy / graphic[0].dy;
@@ -96,2 +112,82 @@
   return (TRUE);
 }
+
+// define the section, but do not create a graph or image 
+int DefineSectionCenterByImage (int sock) {
+  
+  int N;
+  char name[128];
+  double x, y, dx, dy;
+  int dXm, dXp, dYm, dYp;
+  double x0, y0, x1, y1, expand;
+  Section *section;
+  Graphic *graphic;
+  KapaImageWidget *image;
+
+  // x,y is the center of the image, adjust section to contain the image
+  KiiScanMessage (sock, "%s %lf %lf", name, &x, &y);
+
+  N = GetSectionByName (name);
+  if (N < 0)  {
+    fprintf (stderr, "section does not yet exist\n");
+    return (TRUE);
+  }
+  section = GetSectionByNumber (N);
+  image = section->image;
+  if (!image) {
+    fprintf (stderr, "no image to define section\n");
+    return (TRUE);
+  }
+  SetActiveSectionByNumber (N);
+
+  graphic = GetGraphic ();
+
+  GetGraphBoundary (section, &x0, &y0, &x1, &y1, &dXm, &dXp, &dYm, &dYp);
+
+  expand = 1.0;
+  if (image[0].picture.expand > 0) {
+    expand = image[0].picture.expand;
+  }
+  if (image[0].picture.expand < 0) {
+    expand = 1.0 / fabs((double)image[0].picture.expand);
+  }
+
+  // pixel dimensions of the imaging region + boundary
+  dx = MIN (image[0].image[0].matrix.Naxis[0]*expand + x1, graphic[0].dx);
+  dy = MIN (image[0].image[0].matrix.Naxis[1]*expand + y1, graphic[0].dy);
+
+  // dx,dy cannot be larger than 1.0
+
+  // section[0].bg = bg;
+  section[0].dx = dx / graphic[0].dx;
+  section[0].dy = dy / graphic[0].dy;
+
+  // translate the section center (x,y) to the section corner
+
+  // desired center of section in pixels
+  double Xc_pix = x*graphic->dx;
+  double Yc_pix = y*graphic->dy;
+
+  // this is the size if the full image fits, saturate on graphic boundary
+  double Xs_pix = MAX(MIN(Xc_pix - 0.5*dx, graphic->dx), 0.0);
+  double Xe_pix = MAX(MIN(Xc_pix + 0.5*dx, graphic->dx), 0.0);
+
+  section[0].x  = Xs_pix / graphic->dx;
+  section[0].dx = (Xe_pix - Xs_pix) / graphic->dx;
+  
+  double Ys_pix = MAX(MIN(Yc_pix - 0.5*dy, graphic->dy), 0.0);
+  double Ye_pix = MAX(MIN(Yc_pix + 0.5*dy, graphic->dy), 0.0);
+
+  section[0].y  = Ys_pix / graphic->dy;
+  section[0].dy = (Ye_pix - Ys_pix) / graphic->dy;
+
+  // if (section[0].x != x)   MoveSection = TRUE;
+  // if (section[0].y != y)   MoveSection = TRUE;
+  // if (section[0].dx != dx) MoveSection = TRUE;
+  // if (section[0].dy != dy) MoveSection = TRUE;
+
+  SetSectionSizes (section);
+  Refresh ();
+
+  return (TRUE);
+}
Index: /trunk/Ohana/src/kapa2/src/SetGraphSize.c
===================================================================
--- /trunk/Ohana/src/kapa2/src/SetGraphSize.c	(revision 42939)
+++ /trunk/Ohana/src/kapa2/src/SetGraphSize.c	(revision 42940)
@@ -19,4 +19,7 @@
   graphic = GetGraphic ();
 
+  // graphic->dx,dy : size of the full window
+  // (x0,y0) : distance of (LL) corner of box relative to section corner
+  // (x1,y1) : distance of (UR) corner of box relative to section corner
   double X0 = graphic[0].dx * section[0].x  + x0;
   double Y0 = graphic[0].dy * section[0].y  + y0;
Index: /trunk/Ohana/src/kapa2/src/bDrawImage.c
===================================================================
--- /trunk/Ohana/src/kapa2/src/bDrawImage.c	(revision 42939)
+++ /trunk/Ohana/src/kapa2/src/bDrawImage.c	(revision 42940)
@@ -59,16 +59,32 @@
   if (Xs < 0) {
     fprintf (stderr, "image display boundaries out of range of window (invalid condition) : fix Kapa\n");
+    fprintf (stderr, "Xs,Ys: %d,%d\n", Xs, Ys);
+    fprintf (stderr, "dx,dy: %d,%d\n", dx, dy);
+    fprintf (stderr, "picture.x,y: %d,%d\n", image[0].picture.x, image[0].picture.y);
+    fprintf (stderr, "picture.dx,dy: %d,%d\n", image[0].picture.dx, image[0].picture.dy);
     abort();
   }
   if (buffer[0].Nx < Xs + dx) {
     fprintf (stderr, "image display boundaries out of range of window (invalid condition) : fix Kapa\n");
+    fprintf (stderr, "Xs,Ys: %d,%d\n", Xs, Ys);
+    fprintf (stderr, "dx,dy: %d,%d\n", dx, dy);
+    fprintf (stderr, "picture.x,y: %d,%d\n", image[0].picture.x, image[0].picture.y);
+    fprintf (stderr, "picture.dx,dy: %d,%d\n", image[0].picture.dx, image[0].picture.dy);
     abort();
   }
   if (Ys < 0) {
     fprintf (stderr, "image display boundaries out of range of window (invalid condition) : fix Kapa\n");
+    fprintf (stderr, "Xs,Ys: %d,%d\n", Xs, Ys);
+    fprintf (stderr, "dx,dy: %d,%d\n", dx, dy);
+    fprintf (stderr, "picture.x,y: %d,%d\n", image[0].picture.x, image[0].picture.y);
+    fprintf (stderr, "picture.dx,dy: %d,%d\n", image[0].picture.dx, image[0].picture.dy);
     abort();
   }
   if (buffer[0].Ny < Ys + dy) {
     fprintf (stderr, "image display boundaries out of range of window (invalid condition) : fix Kapa\n");
+    fprintf (stderr, "Xs,Ys: %d,%d\n", Xs, Ys);
+    fprintf (stderr, "dx,dy: %d,%d\n", dx, dy);
+    fprintf (stderr, "picture.x,y: %d,%d\n", image[0].picture.x, image[0].picture.y);
+    fprintf (stderr, "picture.dx,dy: %d,%d\n", image[0].picture.dx, image[0].picture.dy);
     abort();
   }
