Index: trunk/Ohana/src/opihi/cmd.astro/region.c
===================================================================
--- trunk/Ohana/src/opihi/cmd.astro/region.c	(revision 29759)
+++ trunk/Ohana/src/opihi/cmd.astro/region.c	(revision 29938)
@@ -55,4 +55,5 @@
   if ((argc != 4) && (argc != 5)) {
     gprint (GP_ERR, "USAGE: region Ra Dec Radius [projection] [orientation]\n");
+    gprint (GP_ERR, "  [-image] [-ew] [+ew] [-ns] [+ns] [-no-clear]\n");
     gprint (GP_ERR, " current: %f %f (%f x %f) (%s)\n", 
 	     graphmode.coords.crval1, graphmode.coords.crval2, 
Index: trunk/Ohana/src/opihi/cmd.data/Makefile
===================================================================
--- trunk/Ohana/src/opihi/cmd.data/Makefile	(revision 29759)
+++ trunk/Ohana/src/opihi/cmd.data/Makefile	(revision 29938)
@@ -36,4 +36,5 @@
 $(SRC)/cut.$(ARCH).o		\
 $(SRC)/delete.$(ARCH).o	\
+$(SRC)/densify.$(ARCH).o	\
 $(SRC)/device.$(ARCH).o	\
 $(SRC)/dimendown.$(ARCH).o	\
Index: trunk/Ohana/src/opihi/cmd.data/densify.c
===================================================================
--- trunk/Ohana/src/opihi/cmd.data/densify.c	(revision 29938)
+++ trunk/Ohana/src/opihi/cmd.data/densify.c	(revision 29938)
@@ -0,0 +1,61 @@
+# include "data.h"
+
+int densify (int argc, char **argv) {
+
+  int i, Nx, Ny, Xb, Yb, Normalize, N;
+  float Xmin, Xmax, dX, Ymin, Ymax, dY;
+  float *val;
+  Buffer *bf;
+  Vector *vx, *vy;
+  opihi_flt *x, *y;
+
+  Normalize = TRUE;
+  if ((N = get_argument (argc, argv, "-raw"))) {
+    remove_argument (N, &argc, argv);
+    Normalize = FALSE;
+  }
+
+  if (argc != 10) {
+    gprint (GP_ERR, "USAGE: densify buffer x y Xmin Xmax dX Ymin Ymax dY\n");
+    return (FALSE);
+  }
+  
+  if ((bf = SelectBuffer (argv[1], ANYBUFFER, TRUE)) == NULL) return (FALSE);
+  if ((vx = SelectVector (argv[2], OLDVECTOR, TRUE)) == NULL) return (FALSE);
+  if ((vy = SelectVector (argv[3], OLDVECTOR, TRUE)) == NULL) return (FALSE);
+
+  if (vx[0].Nelements != vy[0].Nelements) return (FALSE);
+
+  REQUIRE_VECTOR_FLT (vx, FALSE); 
+  REQUIRE_VECTOR_FLT (vy, FALSE); 
+
+  Xmin = atof (argv[4]);
+  Xmax = atof (argv[5]);
+  dX   = atof (argv[6]);
+
+  Ymin = atof (argv[7]);
+  Ymax = atof (argv[8]);
+  dY   = atof (argv[9]);
+
+  Nx = (Xmax - Xmin) / dX + 1;
+  Ny = (Ymax - Ymin) / dY + 1;
+  
+  gfits_free_matrix (&bf[0].matrix);
+  gfits_free_header (&bf[0].header);
+  CreateBuffer (bf, Nx, Ny, -32, 0.0, 1.0);
+  strcpy (bf[0].file, "(empty)");
+
+  x = vx[0].elements.Flt;
+  y = vy[0].elements.Flt;
+  val = (float *)bf[0].matrix.buffer;
+  for (i = 0; i < vx[0].Nelements; i++, x++, y++) {
+    Xb = (*x - Xmin) / dX;
+    Yb = (*y - Ymin) / dY;
+    if (Xb >= Nx) continue;
+    if (Yb >= Ny) continue;
+    if (Xb < 0) continue;
+    if (Yb < 0) continue;
+    val[Xb + Yb*Nx] ++;
+  }
+  return (TRUE);
+}
Index: trunk/Ohana/src/opihi/cmd.data/init.c
===================================================================
--- trunk/Ohana/src/opihi/cmd.data/init.c	(revision 29759)
+++ trunk/Ohana/src/opihi/cmd.data/init.c	(revision 29938)
@@ -25,4 +25,5 @@
 int dbselect         PROTO((int, char **));
 int delete           PROTO((int, char **));
+int densify          PROTO((int, char **));
 int device           PROTO((int, char **));
 int dimendown        PROTO((int, char **));
@@ -161,4 +162,5 @@
   {1, "dbselect",     dbselect,         "extract vectors from mysql database table"},
   {1, "delete",       delete,           "delete vectors or images"},
+  {1, "densify",      densify,          "create an image histogram from a set of vectors"},
   {1, "device",       device,           "set / get current graphics device"},
   {1, "dimendown",    dimendown,        "convert image to vector"},
Index: trunk/Ohana/src/opihi/cmd.data/rd.c
===================================================================
--- trunk/Ohana/src/opihi/cmd.data/rd.c	(revision 29759)
+++ trunk/Ohana/src/opihi/cmd.data/rd.c	(revision 29938)
@@ -184,5 +184,5 @@
     ftable.header[0].buffer = NULL;
     gfits_copy_header (&buf[0].header, ftable.header);
-    status = gfits_fread_ftable_data (f, &ftable);  // this just reads the bytes (not even a SWAP)
+    status = gfits_fread_ftable_data (f, &ftable, FALSE);  // this just reads the bytes (not even a SWAP)
     status = gfits_uncompress_image (&buf[0].header, &buf[0].matrix, &ftable);
     // uncompressing the image leaves the format as an extension
Index: trunk/Ohana/src/opihi/cmd.data/read_vectors.c
===================================================================
--- trunk/Ohana/src/opihi/cmd.data/read_vectors.c	(revision 29759)
+++ trunk/Ohana/src/opihi/cmd.data/read_vectors.c	(revision 29938)
@@ -160,5 +160,5 @@
 
   off_t Nbytes;
-  int i, j, k, N, Nextend, Ny, Binary, vecType;
+  int i, j, k, N, Nextend, Ny, Binary, vecType, padIfShort;
   char type[16], ID[80], *CCDKeyword;
   FTable table;
@@ -174,4 +174,10 @@
     CCDKeyword = strcreate (argv[N]);
     remove_argument (N, &argc, argv);
+  }
+
+  padIfShort = FALSE;
+  if ((N = get_argument (argc, argv, "-pad-if-short"))) {
+    remove_argument (N, &argc, argv);
+    padIfShort = TRUE;
   }
 
@@ -205,5 +211,5 @@
     }
     if (!gfits_load_header (f, &header)) ESCAPE ("error reading header for extension");
-    if (!gfits_fread_ftable_data (f, &table)) ESCAPE ("error reading table for extension");
+    if (!gfits_fread_ftable_data (f, &table, padIfShort)) ESCAPE ("error reading table for extension");
 
   } else {
@@ -236,5 +242,5 @@
 	continue;
       }
-      if (!gfits_fread_ftable_data (f, &table)) ESCAPE ("error reading table for extension");
+      if (!gfits_fread_ftable_data (f, &table, padIfShort)) ESCAPE ("error reading table for extension");
       break;
     }
Index: trunk/Ohana/src/opihi/cmd.data/resize.c
===================================================================
--- trunk/Ohana/src/opihi/cmd.data/resize.c	(revision 29759)
+++ trunk/Ohana/src/opihi/cmd.data/resize.c	(revision 29938)
@@ -17,4 +17,10 @@
   if (!GetImage (NULL, &kapa, name)) return (FALSE);
   FREE (name);
+
+  if ((N = get_argument (argc, argv, "-by-image"))) {
+    remove_argument (N, &argc, argv);
+    KiiResizeByImage (kapa);
+    return (TRUE);
+  }
 
   if (argc != 3) {
Index: trunk/Ohana/src/opihi/cmd.data/section.c
===================================================================
--- trunk/Ohana/src/opihi/cmd.data/section.c	(revision 29759)
+++ trunk/Ohana/src/opihi/cmd.data/section.c	(revision 29938)
@@ -1,5 +1,5 @@
 # include "data.h"
 
-enum {NONE, LIST, UP, DOWN, TOP, BOTTOM, TOOL, BG};
+enum {NONE, LIST, UP, DOWN, TOP, BOTTOM, TOOL, BG, IMAGE};
 
 int section (int argc, char **argv) {
@@ -49,4 +49,9 @@
     remove_argument (N, &argc, argv);
     action = BG;
+  }
+
+  if ((N = get_argument (argc, argv, "-image"))) {
+    remove_argument (N, &argc, argv);
+    action = IMAGE;
   }
 
@@ -130,4 +135,14 @@
   } 
   
+  if (argc == 4) {
+    /* set section */
+    section.name = argv[1];
+    section.x = atof (argv[2]);
+    section.y = atof (argv[3]);
+    section.bg = background;
+    KapaSetSectionByImage (kapa, &section);
+    return (TRUE);
+  }
+
   if (argc == 6) {
     /* set section */
@@ -142,4 +157,5 @@
   }
   gprint (GP_ERR, "USAGE: section name [x y dx dy]\n");
+  gprint (GP_ERR, "USAGE: section name [-image x y] : width based on current image\n");
   gprint (GP_ERR, "USAGE: section name [-list] [-up] [-down] [-top] [-bottom]\n");
   return (FALSE);
Index: trunk/Ohana/src/opihi/cmd.data/vgauss.c
===================================================================
--- trunk/Ohana/src/opihi/cmd.data/vgauss.c	(revision 29759)
+++ trunk/Ohana/src/opihi/cmd.data/vgauss.c	(revision 29938)
@@ -45,4 +45,6 @@
   CastVector (yvec, OPIHI_FLT);
   CastVector (svec, OPIHI_FLT);
+  // XXX Cast is failing.
+    
 
   Npts = xvec[0].Nelements;
Index: trunk/Ohana/src/opihi/dvo/images.c
===================================================================
--- trunk/Ohana/src/opihi/dvo/images.c	(revision 29759)
+++ trunk/Ohana/src/opihi/dvo/images.c	(revision 29938)
@@ -11,9 +11,9 @@
 int images (int argc, char **argv) {
 
-  off_t i, Nimage;
+  off_t i, Nimage, Nmosaic;
   int j, status, InPic, leftside, *plist, TimeSelect, ByName;
   int WITH_MOSAIC, SOLO_MOSAIC, HIDDEN;
   time_t tzero, tend;
-  int N, NPTS, n, npts, Npts, kapa;
+  int N, NPTS, n, npts, Npts, kapa, *foundMosaic;
   Vector Xvec, Yvec;
   double r[8], d[8], x[8], y[8], Rmin, Rmax, Rmid, trange, Radius;
@@ -35,4 +35,5 @@
 
   SOLO_MOSAIC = FALSE;
+  foundMosaic = NULL;
   if ((N = get_argument (argc, argv, "-mosaic"))) {
     remove_argument (N, &argc, argv);
@@ -132,4 +133,9 @@
   BuildChipMatch (image, Nimage);
 
+  if (SOLO_MOSAIC && photcode) {
+    ALLOCATE(foundMosaic, int, Nimage);
+    memset(foundMosaic, 0, Nimage*sizeof(int));
+  }
+
   Rmin = graphmode.coords.crval1 - 180.0;
   Rmax = graphmode.coords.crval1 + 180.0;
@@ -137,4 +143,5 @@
   
   int DistortImage = wordhash ("-DIS");
+  int ChipImage    = wordhash ("-WRP");
   int TriangleUp   = wordhash ("TRP-");
   int TriangleDn   = wordhash ("TRM-");
@@ -150,5 +157,6 @@
     if (ByName && strncmp (image[i].name, name, strlen(name))) continue;
     if (TimeSelect && ((image[i].tzero < tzero) || (image[i].tzero+image[i].trate*image[i].NY > tzero + trange))) continue;
-    if (!FindMosaicForImage (image, Nimage, i)) continue;
+    if (!(Nmosaic = FindMosaicForImage (image, Nimage, i))) continue;
+    Nmosaic --; // XXX kind of a hack: FindMosaicForImage returns 0 or the mosaic seq number + 1
     if (photcode) {
       if ( photcodeEquiv && (photcode[0].code != GetPhotcodeEquivCodebyCode(image[i].photcode))) continue;
@@ -161,4 +169,31 @@
 
     typehash = wordhash (&image[i].coords.ctype[4]);
+
+    if (photcode && SOLO_MOSAIC) {
+      // mosaic (DIS) images are not currently given a photcode : plot these via the WRP entries
+      /* DIS images represent a field, not a chip */
+      if (typehash != ChipImage) continue;
+      if (foundMosaic[Nmosaic]) continue;
+      x[0] = -0.5*image[Nmosaic].NX; y[0] = -0.5*image[Nmosaic].NY;
+      x[1] = +0.5*image[Nmosaic].NX; y[1] = -0.5*image[Nmosaic].NY;
+      x[2] = +0.5*image[Nmosaic].NX; y[2] = +0.5*image[Nmosaic].NY;
+      x[3] = -0.5*image[Nmosaic].NX; y[3] = +0.5*image[Nmosaic].NY;
+      for (j = 0; j < Npts; j++) {
+	status = XY_to_RD (&r[j], &d[j], x[j], y[j], &image[Nmosaic].coords);
+	if (!status) break;
+	r[j] = ohana_normalize_angle (r[j]);
+	while (r[j] < Rmin) { r[j] += 360.0; }
+	while (r[j] > Rmax) { r[j] -= 360.0; }
+	if (j == 0) {
+	  leftside = (r[j] < Rmid);
+	} 
+	if (j > 0) { 
+	  if ( leftside && (r[j] > Rmid + 90)) { r[j] -= 360.0; }
+	  if (!leftside && (r[j] < Rmid - 90)) { r[j] += 360.0; }
+	}
+      }
+      foundMosaic[Nmosaic] = TRUE;
+      goto plot_points;
+    }
 
     /* DIS images represent a field, not a chip */
@@ -270,4 +305,6 @@
     if (Npts == 0) continue;
 
+  plot_points:
+
     status = FALSE;
     for (j = 0; j < Npts; j++) {
@@ -320,4 +357,5 @@
   free (Yvec.elements.Flt);
   FreeImages (image);
+  FREE (foundMosaic);
   return (TRUE);
 
Index: trunk/Ohana/src/opihi/dvo/imlist.c
===================================================================
--- trunk/Ohana/src/opihi/dvo/imlist.c	(revision 29759)
+++ trunk/Ohana/src/opihi/dvo/imlist.c	(revision 29938)
@@ -104,6 +104,6 @@
       XY_to_RD (&r, &d, 0.5*image[i].NX, 0.5*image[i].NY, &image[i].coords);
     }
-    gprint (GP_LOG, "%3lld %s %8.4f %8.4f %f %5d %2d %4.2f %5.3f %5.3f\n", 
-	    (long long) i, image[i].name, r, d, t, image[i].nstar, image[i].photcode, image[i].secz, image[i].Mcal, image[i].dMcal);
+    gprint (GP_LOG, "%3lld %s %8lld %8.4f %8.4f %f %5d %2d %4.2f %5.3f %5.3f\n", 
+	    (long long) i, image[i].name, (long long) image[i].imageID, r, d, t, image[i].nstar, image[i].photcode, image[i].secz, image[i].Mcal, image[i].dMcal);
   }
 
Index: trunk/Ohana/src/opihi/lib.data/starfuncs.c
===================================================================
--- trunk/Ohana/src/opihi/lib.data/starfuncs.c	(revision 29759)
+++ trunk/Ohana/src/opihi/lib.data/starfuncs.c	(revision 29938)
@@ -5,5 +5,5 @@
   double *ring;
   double x, y, x2, y2, xy, I, sky, FWHMx, FWHMy, value, mag, Sxy;
-  int i, j, n, Npix2, Nring, Nmax;
+  int i, j, n, Radius, Nring, Nmax;
   double Npts, gain, dsky2, dmag, peak, offset;
   char *string;
@@ -19,6 +19,6 @@
   Nborder = MIN (1000, Nborder);
   
-  Npix2 = (int)(0.5*Npix);
-  Npix = 2 * Npix2 + 1;
+  Radius = (int)(0.5*Npix);
+  Npix = 2 * Radius + 1;
   Nring = 4*Nborder*(Nborder + Npix);
   ALLOCATE (ring, double, Nring);
@@ -27,11 +27,11 @@
   n = 0;  
   for (j = 0; j < Nborder; j++) {
-    for (i = X - Npix2 - Nborder; i < X + Npix2 + Nborder + 1; i++, n+=2) {
-      ring[n]   = gfits_get_matrix_value (matrix, i, (int)(Y - Npix2 - j));
-      ring[n+1] = gfits_get_matrix_value (matrix, i, (int)(Y + Npix2 + j));
-    }
-    for (i = Y - Npix2; i < Y + Npix2 + 1; i++, n+=2) {
-      ring[n]   = gfits_get_matrix_value (matrix, (int)(X - Npix2 - j), i);
-      ring[n+1] = gfits_get_matrix_value (matrix, (int)(X + Npix2 + j), i);
+    for (i = X - Radius - Nborder; i < X + Radius + Nborder + 1; i++, n+=2) {
+      ring[n]   = gfits_get_matrix_value (matrix, i, (int)(Y - Radius - j));
+      ring[n+1] = gfits_get_matrix_value (matrix, i, (int)(Y + Radius + j));
+    }
+    for (i = Y - Radius; i < Y + Radius + 1; i++, n+=2) {
+      ring[n]   = gfits_get_matrix_value (matrix, (int)(X - Radius - j), i);
+      ring[n+1] = gfits_get_matrix_value (matrix, (int)(X + Radius + j), i);
     }
   }
@@ -50,6 +50,7 @@
   Npts = Nmax = 0;
   x = y = x2 = y2 = xy = I = 0;
-  for (i = X - Npix2; i < X + Npix2 + 1; i++) {
-    for (j = Y - Npix2; j < Y + Npix2 + 1; j++) {
+  for (i = X - Radius; i < X + Radius + 1; i++) {
+    for (j = Y - Radius; j < Y + Radius + 1; j++) {
+      if (hypot((i-X), (j-Y)) > Radius) continue;
       value = gfits_get_matrix_value (matrix, i, j);
       offset = value - sky;
@@ -92,4 +93,5 @@
   set_variable ("Zpk", peak);
   set_int_variable ("Nsat", Nmax);
+  set_int_variable ("Npts", Npts);
   
   gprint (GP_LOG, "%f %f %f %f %f %f %f %f\n", x, y, FWHMx, FWHMy, sky, I, mag, dmag);
