Index: /branches/eam_branches/ohana.20150429/src/dvopsps/src/insert_detections_dvopsps_catalog.c
===================================================================
--- /branches/eam_branches/ohana.20150429/src/dvopsps/src/insert_detections_dvopsps_catalog.c	(revision 38439)
+++ /branches/eam_branches/ohana.20150429/src/dvopsps/src/insert_detections_dvopsps_catalog.c	(revision 38440)
@@ -215,5 +215,9 @@
       fprintf (stderr, "%s\n", mysql_error(mysql));
       fprintf (stderr, "Nbuffer: %d\n", buffer->Nbuffer);
-      if (DEBUG) fprintf (stderr, "%s\n", buffer->buffer);
+      if (DEBUG) {
+	FILE *f = fopen ("dvopsps.dump.sql", "w");
+	fprintf (f, "%s\n", buffer->buffer);
+	fclose (f);
+      }
       status = FALSE;
     }
Index: /branches/eam_branches/ohana.20150429/src/libfits/Makefile
===================================================================
--- /branches/eam_branches/ohana.20150429/src/libfits/Makefile	(revision 38439)
+++ /branches/eam_branches/ohana.20150429/src/libfits/Makefile	(revision 38440)
@@ -63,5 +63,6 @@
 $(MATR)/F_matrix.$(ARCH).o                      $(MATR)/F_compress_utils.$(ARCH).o \
 $(MATR)/F_compress_M.$(ARCH).o   		$(MATR)/F_compress_data.$(ARCH).o  \
-$(MATR)/F_uncompress_M.$(ARCH).o                $(MATR)/F_uncompress_data.$(ARCH).o
+$(MATR)/F_uncompress_M.$(ARCH).o                $(MATR)/F_uncompress_data.$(ARCH).o \
+$(MATR)/F_swap_raw.$(ARCH).o
 
 TABLE_OBJ = \
Index: /branches/eam_branches/ohana.20150429/src/libfits/include/gfitsio.h
===================================================================
--- /branches/eam_branches/ohana.20150429/src/libfits/include/gfitsio.h	(revision 38439)
+++ /branches/eam_branches/ohana.20150429/src/libfits/include/gfitsio.h	(revision 38440)
@@ -197,5 +197,7 @@
 int     gfits_compress_image 	       PROTO((Header *header, Matrix *matrix, FTable *ftable, int *Ztile, char *zcmptype));
 int     gfits_collect_data   	       PROTO((Matrix *matrix, char *raw, int Nraw, int raw_pixsize, int *otile, int oblank, int *ztile, int zblank, float zscale, float zzero));
+
 int     gfits_copy_keywords_compress   PROTO((Header *srchead, Header *tgthead));
+int     gfits_swap_raw   	       PROTO((Matrix *matrix));
 
 off_t   gfits_tile_size                PROTO((Matrix *matrix, int *otile, int *ztile));
Index: /branches/eam_branches/ohana.20150429/src/libfits/matrix/F_swap_raw.c
===================================================================
--- /branches/eam_branches/ohana.20150429/src/libfits/matrix/F_swap_raw.c	(revision 38440)
+++ /branches/eam_branches/ohana.20150429/src/libfits/matrix/F_swap_raw.c	(revision 38440)
@@ -0,0 +1,46 @@
+# include <ohana.h>
+# include <gfitsio.h>
+# define DOSWAP(A,B) { char tmp = A; A = B; B = tmp; }
+
+/*********************** fits convert format ***********************************/
+/* this function is safe in the number of axes (can even be 0) */
+int gfits_swap_raw (Matrix *matrix) {
+
+  off_t i;
+
+  int bytes_per_pixel = (abs(matrix[0].bitpix) / 8);
+
+  off_t Npixels = gfits_npix_matrix (matrix);
+
+  char *byte = matrix[0].buffer;
+
+  switch (bytes_per_pixel) {
+    case 1:
+      break;
+    case 2:
+      for (i = 0; i < Npixels; i++) {
+	DOSWAP(byte[0], byte[1]);
+	byte += 2;
+      }
+      break;
+    case 4:
+      for (i = 0; i < Npixels; i++) {
+	DOSWAP(byte[0], byte[3]);
+	DOSWAP(byte[1], byte[2]);
+	byte += 4;
+      }
+      break;
+    case 8:
+      for (i = 0; i < Npixels; i++) {
+	DOSWAP(byte[0], byte[7]);
+	DOSWAP(byte[1], byte[6]);
+	DOSWAP(byte[2], byte[5]);
+	DOSWAP(byte[3], byte[4]);
+	byte += 8;
+      }
+      break;
+    default:
+      myAbort ("invalid bytes_per_pixel");
+  }
+  return TRUE;
+}
Index: /branches/eam_branches/ohana.20150429/src/opihi/cmd.data/Makefile
===================================================================
--- /branches/eam_branches/ohana.20150429/src/opihi/cmd.data/Makefile	(revision 38439)
+++ /branches/eam_branches/ohana.20150429/src/opihi/cmd.data/Makefile	(revision 38440)
@@ -91,4 +91,5 @@
 $(SRC)/mget.$(ARCH).o		\
 $(SRC)/mget3d.$(ARCH).o		\
+$(SRC)/mslice.$(ARCH).o		\
 $(SRC)/minterpolate.$(ARCH).o	\
 $(SRC)/medimage.$(ARCH).o	\
Index: /branches/eam_branches/ohana.20150429/src/opihi/cmd.data/init.c
===================================================================
--- /branches/eam_branches/ohana.20150429/src/opihi/cmd.data/init.c	(revision 38439)
+++ /branches/eam_branches/ohana.20150429/src/opihi/cmd.data/init.c	(revision 38440)
@@ -82,4 +82,5 @@
 int mget             PROTO((int, char **));
 int mget3d           PROTO((int, char **));
+int mslice           PROTO((int, char **));
 int minterp          PROTO((int, char **));
 int medimage_command PROTO((int, char **));
@@ -248,4 +249,5 @@
   {1, "mget",         mget,             "extract a vector from an image"},
   {1, "mget3d",       mget3d,           "extract a vector from a 3D image"},
+  {1, "mslice",       mslice,           "extract an image plane from a 3D image"},
   {1, "imget",        mget,             "extract a vector from an image"},
   {1, "minterp",      minterp,          "interpolate image pixels"},
Index: /branches/eam_branches/ohana.20150429/src/opihi/cmd.data/mslice.c
===================================================================
--- /branches/eam_branches/ohana.20150429/src/opihi/cmd.data/mslice.c	(revision 38440)
+++ /branches/eam_branches/ohana.20150429/src/opihi/cmd.data/mslice.c	(revision 38440)
@@ -0,0 +1,46 @@
+# include "data.h"
+
+int mslice (int argc, char **argv) {
+  
+  int i;
+  Buffer *in, *out;
+
+  if (argc != 4) {
+    gprint (GP_ERR, "USAGE: mget <input> <output> plane\n");
+    return (FALSE);
+  }
+
+  int plane = atoi(argv[3]);
+
+  if ((in = SelectBuffer (argv[1], OLDBUFFER, TRUE)) == NULL) return (FALSE);
+  if (in[0].matrix.Naxes < 3) {
+    gprint (GP_ERR, "buffer is not 3D\n");
+    return FALSE;
+  }
+  if ((out = SelectBuffer (argv[2], ANYBUFFER, TRUE)) == NULL) return (FALSE);
+
+  int Nx = in[0].matrix.Naxis[0];
+  int Ny = in[0].matrix.Naxis[1];
+  int Nz = in[0].matrix.Naxis[2];
+
+  int invalid = FALSE;
+  invalid = invalid || (plane < 0);
+  invalid = invalid || (plane >= Nz);
+  if (invalid) {
+    gprint (GP_ERR, "plane %d out of range\n", plane);
+    return (FALSE);
+  }
+
+  /* I should encapsulate this in a create_default_buffer */
+  gfits_free_matrix (&out[0].matrix);
+  gfits_free_header (&out[0].header);
+  CreateBuffer (out, Nx, Ny, -32, 1.0, 0.0);
+
+  float *inF  = (float *) in[0].matrix.buffer + plane*Nx*Ny;
+  float *outF = (float *) out[0].matrix.buffer;
+
+  for (i = 0; i < Nx*Ny; i++, inF ++, outF++) {
+    *outF = *inF;
+  }
+  return (TRUE);
+}
Index: /branches/eam_branches/ohana.20150429/src/opihi/cmd.data/rd.c
===================================================================
--- /branches/eam_branches/ohana.20150429/src/opihi/cmd.data/rd.c	(revision 38439)
+++ /branches/eam_branches/ohana.20150429/src/opihi/cmd.data/rd.c	(revision 38440)
@@ -19,4 +19,10 @@
     remove_argument (N, &argc, argv);
     JustHead = TRUE;
+  }
+
+  int SwapRaw = FALSE;
+  if ((N = get_argument (argc, argv, "-swap-raw"))) {
+    remove_argument (N, &argc, argv);
+    SwapRaw = TRUE;
   }
 
@@ -176,5 +182,5 @@
   if (plane >= 0) {
     // we are requesting a specific plane (-1 : all data)
-    int tooFar = Nz ? (plane >= Nz) : (plane > Nz);
+    int tooFar = (Nz > 0) ? (plane >= Nz) : (plane > 0);
     if (tooFar) {
       gprint (GP_ERR, "-plane is too large: %d total planes\n", Nz);
@@ -249,4 +255,8 @@
   gfits_scan (&buf[0].header, "BLANK", "%d", 1, &blank);
 
+  if (SwapRaw) {
+    gfits_swap_raw (&buf[0].matrix);
+  }
+
   /** now - convert the matrix values to floats for internal use **/
   gfits_convert_format (&buf[0].header, &buf[0].matrix, -32, 1.0, 0.0, blank, gfits_get_unsign_mode());
