Index: /branches/eam_branches/ohana.20150429/src/libfits/extern/gzip.c
===================================================================
--- /branches/eam_branches/ohana.20150429/src/libfits/extern/gzip.c	(revision 38339)
+++ /branches/eam_branches/ohana.20150429/src/libfits/extern/gzip.c	(revision 38340)
@@ -2,4 +2,5 @@
 # include <gfitsio.h>
 # include <zlib.h>
+# define EXTRA_VERBOSE 1
 
 /** the two functions in this file re-implement the uncompress function of zlib.  
@@ -10,35 +11,35 @@
 
 /* zlib.h -- interface of the 'zlib' general purpose compression library
-  version 1.2.3, July 18th, 2005
-
-  Copyright (C) 1995-2005 Jean-loup Gailly and Mark Adler
-
-  This software is provided 'as-is', without any express or implied
-  warranty.  In no event will the authors be held liable for any damages
-  arising from the use of this software.
-
-  Permission is granted to anyone to use this software for any purpose,
-  including commercial applications, and to alter it and redistribute it
-  freely, subject to the following restrictions:
-
-  1. The origin of this software must not be misrepresented; you must not
-     claim that you wrote the original software. If you use this software
-     in a product, an acknowledgment in the product documentation would be
-     appreciated but is not required.
-  2. Altered source versions must be plainly marked as such, and must not be
-     misrepresented as being the original software.
-  3. This notice may not be removed or altered from any source distribution.
-
-  Jean-loup Gailly        Mark Adler
-  jloup@gzip.org          madler@alumni.caltech.edu
-
-
-  The data format used by the zlib library is described by RFCs (Request for
-  Comments) 1950 to 1952 in the files http://www.ietf.org/rfc/rfc1950.txt
-  (zlib format), rfc1951.txt (deflate format) and rfc1952.txt (gzip format).
+   version 1.2.3, July 18th, 2005
+
+   Copyright (C) 1995-2005 Jean-loup Gailly and Mark Adler
+
+   This software is provided 'as-is', without any express or implied
+   warranty.  In no event will the authors be held liable for any damages
+   arising from the use of this software.
+
+   Permission is granted to anyone to use this software for any purpose,
+   including commercial applications, and to alter it and redistribute it
+   freely, subject to the following restrictions:
+
+   1. The origin of this software must not be misrepresented; you must not
+   claim that you wrote the original software. If you use this software
+   in a product, an acknowledgment in the product documentation would be
+   appreciated but is not required.
+   2. Altered source versions must be plainly marked as such, and must not be
+   misrepresented as being the original software.
+   3. This notice may not be removed or altered from any source distribution.
+
+   Jean-loup Gailly        Mark Adler
+   jloup@gzip.org          madler@alumni.caltech.edu
+
+
+   The data format used by the zlib library is described by RFCs (Request for
+   Comments) 1950 to 1952 in the files http://www.ietf.org/rfc/rfc1950.txt
+   (zlib format), rfc1951.txt (deflate format) and rfc1952.txt (gzip format).
 */
 
 /*
-     The 'zlib' compression library provides in-memory compression and
+  The 'zlib' compression library provides in-memory compression and
   decompression functions, including integrity checks of the uncompressed
   data.  This version of the library supports only one compression method
@@ -46,5 +47,5 @@
   stream interface.
 
-     Compression can be done in a single step if the buffers are large
+  Compression can be done in a single step if the buffers are large
   enough (for example if an input file is mmap'ed), or can be done by
   repeated calls of the compression function.  In the latter case, the
@@ -52,21 +53,21 @@
   (providing more output space) before each call.
 
-     The compressed data format used by default by the in-memory functions is
+  The compressed data format used by default by the in-memory functions is
   the zlib format, which is a zlib wrapper documented in RFC 1950, wrapped
   around a deflate stream, which is itself documented in RFC 1951.
 
-     The library also supports reading and writing files in gzip (.gz) format
+  The library also supports reading and writing files in gzip (.gz) format
   with an interface similar to that of stdio using the functions that start
   with "gz".  The gzip format is different from the zlib format.  gzip is a
   gzip wrapper, documented in RFC 1952, wrapped around a deflate stream.
 
-     This library can optionally read and write gzip streams in memory as well.
-
-     The zlib format was designed to be compact and fast for use in memory
+  This library can optionally read and write gzip streams in memory as well.
+
+  The zlib format was designed to be compact and fast for use in memory
   and on communications channels.  The gzip format was designed for single-
   file compression on file systems, has a larger header than zlib to maintain
   directory information, and uses a different, slower check method than zlib.
 
-     The library does not install any signal handler. The decoder checks
+  The library does not install any signal handler. The decoder checks
   the consistency of the compressed data, so the library should never
   crash even in case of corrupted input.
@@ -116,59 +117,58 @@
 }
 
-// XXX ??? is cfitsio writing data which is incompatible with zlib ???
+# define ESCAPE(RET) { fprintf (stderr, "gzip error in %s @ %s:%d\n", __func__, __FILE__, __LINE__); return (RET); }
+
+// NOTE: CFITSIO uses gzip format, not just zlib
 int gfits_uncompress (Bytef *dest, uLongf *destLen, const Bytef *source, uLong sourceLen)
 {
-    z_stream stream;
-    int i, err;
-
-    stream.next_in = (Bytef*)source;
-    stream.avail_in = (uInt)sourceLen;
-    /* Check for source > 64K on 16-bit machine: */
-    if ((uLong)stream.avail_in != sourceLen) return Z_BUF_ERROR;
-
-    stream.next_out = dest;
-    stream.avail_out = (uInt)*destLen;
-    if ((uLong)stream.avail_out != *destLen) return Z_BUF_ERROR;
-
-    stream.zalloc = Z_NULL;
-    stream.zfree  = Z_NULL;
-    stream.opaque = Z_NULL;
-
-    if (0) {
-      fprintf (stderr, "inp unc: ");
-      for (i = 0; i < sourceLen; i++) {
-	fprintf (stderr, "0x%02hhx ", source[i]);
-      }
-      fprintf (stderr, "\n");
-    }
-
-    // err = inflateInit2(&stream, -15);
-    err = inflateInit(&stream);
-    if (0) fprintf (stderr, "inp buffers unc 0: %d => %d => %d\n", stream.avail_in, stream.avail_out, (int) stream.total_out);
-
-    if (err != Z_OK) return err;
-
-    err = inflate(&stream, Z_FINISH);
-    if (0) fprintf (stderr, "out buffers unc 1: %d => %d => %d\n", stream.avail_in, stream.avail_out, (int) stream.total_out);
-    if (err != Z_STREAM_END) {
-        inflateEnd(&stream);
-        if (err == Z_NEED_DICT || (err == Z_BUF_ERROR && stream.avail_in == 0)) return Z_DATA_ERROR;
-        return err;
-    }
-    if (0) fprintf (stderr, "out buffers unc 2: %d => %d => %d\n", stream.avail_in, stream.avail_out, (int) stream.total_out);
-
-    if (0) {
-      fprintf (stderr, "out unc: ");
-      for (i = 0; i < stream.total_out; i++) {
-	fprintf (stderr, "0x%02hhx ", dest[i]);
-      }
-      fprintf (stderr, "\n");
-    }
-
-    assert (stream.total_out <= *destLen);
-    *destLen = stream.total_out;
-
-    err = inflateEnd(&stream);
-    return err;
+  z_stream stream;
+  int i, err;
+  
+  stream.next_in = (Bytef*)source;
+  stream.avail_in = (uInt)sourceLen;
+
+  /* Check for source > 64K on 16-bit machine: */
+  if ((uLong)stream.avail_in != sourceLen) ESCAPE (Z_BUF_ERROR);
+  
+  stream.next_out = dest;
+  stream.avail_out = (uInt)*destLen;
+  if ((uLong)stream.avail_out != *destLen) ESCAPE (Z_BUF_ERROR);
+  
+  stream.zalloc = Z_NULL;
+  stream.zfree  = Z_NULL;
+  stream.opaque = Z_NULL;
+  
+  if (EXTRA_VERBOSE) {
+    fprintf (stderr, "inp unc: ");
+    for (i = 0; i < sourceLen; i++) {
+      fprintf (stderr, "0x%02hhx ", source[i]);
+    }
+    fprintf (stderr, "\n");
+  }
+  
+  // a value of 32 tells inflate to use the window size used in the compression AND to look for both zlib and gzip headers
+  err = inflateInit2(&stream, 32);
+  if (err != Z_OK) ESCAPE (err);
+  
+  err = inflate(&stream, Z_FINISH);
+  if (err != Z_STREAM_END) {
+    inflateEnd(&stream);
+    if (err == Z_NEED_DICT || (err == Z_BUF_ERROR && stream.avail_in == 0)) ESCAPE (Z_DATA_ERROR);
+    ESCAPE(err);
+  }
+  
+  if (EXTRA_VERBOSE) {
+    fprintf (stderr, "out unc: ");
+    for (i = 0; i < stream.total_out; i++) {
+      fprintf (stderr, "0x%02hhx ", dest[i]);
+    }
+    fprintf (stderr, "\n");
+  }
+
+  assert (stream.total_out <= *destLen);
+  *destLen = stream.total_out;
+
+  err = inflateEnd(&stream);
+  return err;
 }
 
@@ -182,9 +182,9 @@
 
   /* Check for source > 64K on 16-bit machine: */
-  if ((uLong)stream.avail_in != sourceLen) return Z_BUF_ERROR;
+  if ((uLong)stream.avail_in != sourceLen) ESCAPE (Z_BUF_ERROR);
 
   stream.next_out = dest;
   stream.avail_out = (uInt)*destLen; // allocated space
-  if ((uLong)stream.avail_out != *destLen) return Z_BUF_ERROR;
+  if ((uLong)stream.avail_out != *destLen) ESCAPE (Z_BUF_ERROR);
 
   stream.zalloc = Z_NULL;
@@ -192,5 +192,5 @@
   stream.opaque = Z_NULL;
 
-  if (0) {
+  if (EXTRA_VERBOSE) {
     fprintf (stderr, "inp cmp: ");
     for (i = 0; i < sourceLen; i++) {
@@ -202,21 +202,19 @@
   // the '1' is the compression level: make this a user argument
   // NOTE: cfitsio uses a fixed value of 1
-  err = deflateInit(&stream, 1);
+  // I am using deflateInit2 because cfitsio expects gzip, not just zlib
+  err = deflateInit2(&stream, 1, Z_DEFLATED, 31, 8, Z_DEFAULT_STRATEGY);
   if (0) fprintf (stderr, "inp buffers cmp 0: %d => %d => %d\n", stream.avail_in, stream.avail_out, (int) stream.total_out);
 
-  if (err != Z_OK) return err;
+  if (err != Z_OK) ESCAPE(err);
 
   // XXX this is written to do the compression in a single pass.  it could be re-done to have 
   // the compression occur in a series of steps
   err = deflate(&stream, Z_FINISH);
-  if (0) fprintf (stderr, "out buffers cmp 1: %d => %d => %d\n", stream.avail_in, stream.avail_out, (int) stream.total_out);
-
   if (err != Z_STREAM_END) {
     err = deflateEnd(&stream);
-    return Z_BUF_ERROR;
-  }
-  if (0) fprintf (stderr, "out buffers cmp 2: %d => %d => %d\n", stream.avail_in, stream.avail_out, (int) stream.total_out);
-
-  if (0) {
+    ESCAPE (Z_BUF_ERROR);
+  }
+
+  if (EXTRA_VERBOSE) {
     fprintf (stderr, "out cmp: ");
     for (i = 0; i < stream.total_out; i++) {
Index: /branches/eam_branches/ohana.20150429/src/libfits/include/gfitsio.h
===================================================================
--- /branches/eam_branches/ohana.20150429/src/libfits/include/gfitsio.h	(revision 38339)
+++ /branches/eam_branches/ohana.20150429/src/libfits/include/gfitsio.h	(revision 38340)
@@ -205,5 +205,6 @@
 
 int     gfits_byteswap_zdata   	       PROTO((char *zdata, int Nzdata, int bitpix));
-int     gfits_extension_is_compressed  PROTO((Header *header));
+int     gfits_extension_is_compressed_image  PROTO((Header *header));
+int     gfits_extension_is_compressed_table  PROTO((Header *header));
 int     gfits_uncompressed_data_pixsize PROTO((char *cmptype, int out_bitpix, char **optname, char **optvalue, int Noptions));
 int     gfits_vartable_heap_pixsize    PROTO((char format));
Index: /branches/eam_branches/ohana.20150429/src/libfits/matrix/F_compress_M.c
===================================================================
--- /branches/eam_branches/ohana.20150429/src/libfits/matrix/F_compress_M.c	(revision 38339)
+++ /branches/eam_branches/ohana.20150429/src/libfits/matrix/F_compress_M.c	(revision 38340)
@@ -139,6 +139,7 @@
   int tile_pixsize = gfits_uncompressed_data_pixsize (zcmptype, header[0].bitpix, NULL, NULL, 0);
 
+  int Nzdata_alloc = raw_pixsize*max_tile_size + 100;
   ALLOCATE (raw,   char, raw_pixsize*max_tile_size);
-  ALLOCATE (zdata, char, raw_pixsize*max_tile_size);
+  ALLOCATE (zdata, char, Nzdata_alloc);
 
   // init the otile[] counters
@@ -155,9 +156,13 @@
     if (!gfits_collect_data (matrix, raw, Nraw, raw_pixsize, otile, oblank, ztile, zblank, zscale, zzero)) ESCAPE(A);
 
+    if (!strcasecmp(zcmptype, "GZIP_1")) {
+      if (!gfits_byteswap_zdata (raw, Nraw, raw_pixsize)) ESCAPE(A);
+    }
+
     // XXX the tile must not be > 2GB
    
     // optname, optvalue = NULL, Noptions = 0
 
-    int Nzdata = raw_pixsize*max_tile_size; // available space, replaced with actual output size on compression
+    int Nzdata = Nzdata_alloc; // available space, replaced with actual output size on compression
     if (!gfits_compress_data (zdata, &Nzdata, zcmptype, NULL, NULL, 0, raw, Nraw, tile_pixsize)) ESCAPE(A);
 
@@ -298,4 +303,7 @@
 	SETUP_INSIZE (int, 4);
 	break;
+      case 8:
+	SETUP_INSIZE (double, 8);
+	break;
       default:
 	abort();
Index: /branches/eam_branches/ohana.20150429/src/libfits/matrix/F_compress_data.c
===================================================================
--- /branches/eam_branches/ohana.20150429/src/libfits/matrix/F_compress_data.c	(revision 38339)
+++ /branches/eam_branches/ohana.20150429/src/libfits/matrix/F_compress_data.c	(revision 38340)
@@ -27,4 +27,6 @@
 int gfits_compress (Bytef *dest, uLongf *destLen, const Bytef *source, uLong sourceLen);
 
+# define ESCAPE(RET) { fprintf (stderr, "gzip error in %s @ %s:%d\n", __func__, __FILE__, __LINE__); return (RET); }
+
 int gfits_compress_data (char *zdata, int *Nzdata, char *cmptype, 
 			 char **optname, char **optvalue, int Nopt, 
@@ -53,8 +55,5 @@
     status = gfits_compress ((Bytef *) zdata, &destLen, (Bytef *) rawdata, Nbytes);
     *Nzdata = destLen;
-    if (status != Z_OK) {
-      fprintf (stderr, "error in compress (GZIP)\n");
-      return (FALSE);
-    }
+    if (status != Z_OK) ESCAPE(FALSE);
     return (TRUE);
   }
Index: /branches/eam_branches/ohana.20150429/src/libfits/matrix/F_compress_utils.c
===================================================================
--- /branches/eam_branches/ohana.20150429/src/libfits/matrix/F_compress_utils.c	(revision 38339)
+++ /branches/eam_branches/ohana.20150429/src/libfits/matrix/F_compress_utils.c	(revision 38340)
@@ -131,5 +131,5 @@
 }
 
-int gfits_extension_is_compressed (Header *header) {
+int gfits_extension_is_compressed_image (Header *header) {
 
     int has_extension, has_extname, has_zimage, zimage;
@@ -146,4 +146,12 @@
     }
 
+    return (FALSE);
+}
+
+int gfits_extension_is_compressed_table (Header *header) {
+
+    int has_ztable, ztable;
+    has_ztable  = gfits_scan_alt (header, "ZTABLE", "%t", 1, &ztable);
+    if (has_ztable && ztable) return (TRUE);
     return (FALSE);
 }
Index: /branches/eam_branches/ohana.20150429/src/libfits/matrix/F_uncompress_M.c
===================================================================
--- /branches/eam_branches/ohana.20150429/src/libfits/matrix/F_uncompress_M.c	(revision 38339)
+++ /branches/eam_branches/ohana.20150429/src/libfits/matrix/F_uncompress_M.c	(revision 38340)
@@ -249,5 +249,7 @@
   // size of a pixel in the output from the decompression routine
   odata_pixsize = gfits_uncompressed_data_pixsize (cmptype, header[0].bitpix, optname, optvalue, Noptions);
-  ALLOCATE (out, char, odata_pixsize*max_tile_size);
+
+  int Nout_alloc = odata_pixsize*max_tile_size;
+  ALLOCATE (out, char, Nout_alloc);
 
   off_t row;
@@ -258,5 +260,6 @@
 
     // expected output size for this tile
-    Nout = gfits_tile_size (matrix, otile, ztile);
+    // Nout = gfits_tile_size (matrix, otile, ztile);
+    Nout = Nout_alloc;
 
     zdata = gfits_varlength_column_pointer (ftable, &zdef, row, &Nzdata);
@@ -273,4 +276,8 @@
     // XXX the tile must not be > 2GB
     if (!gfits_uncompress_data ((char *)zdata, Nzdata, cmptype, optname, optvalue, Noptions, out, &Nout, odata_pixsize)) ESCAPE;
+
+    if (!strcasecmp(cmptype, "GZIP_1")) {
+      if (!gfits_byteswap_zdata (out, Nout, odata_pixsize)) ESCAPE;
+    }
 
     // copy the uncompressed pixels into their correct locations 	    
@@ -387,4 +394,7 @@
 	SETUP_INSIZE (int, 4);
 	break;
+      case 8:
+	SETUP_INSIZE (double, 8);
+	break;
       default:
 	abort();
Index: /branches/eam_branches/ohana.20150429/src/libfits/matrix/F_uncompress_data.c
===================================================================
--- /branches/eam_branches/ohana.20150429/src/libfits/matrix/F_uncompress_data.c	(revision 38339)
+++ /branches/eam_branches/ohana.20150429/src/libfits/matrix/F_uncompress_data.c	(revision 38340)
@@ -26,4 +26,6 @@
 int gfits_uncompress (Bytef *dest, uLongf *destLen, const Bytef *source, uLong sourceLen);
 
+# define ESCAPE(RET) { fprintf (stderr, "gzip error in %s @ %s:%d\n", __func__, __FILE__, __LINE__); return (RET); }
+
 int gfits_uncompress_data (char *zdata, int Nzdata, char *cmptype, char **optname, char **optvalue, int Nopt, char *outdata, int *Nout, int out_pixsize) {
 
@@ -49,8 +51,5 @@
     // XXX shouldn't we validate the result : we think we know the size
     status = gfits_uncompress ((Bytef *) outdata, &tNout, (Bytef *) zdata, Nzdata);
-    if (status != Z_OK) {
-      fprintf (stderr, "error in uncompress (GZIP)\n");
-      return (FALSE);
-    }
+    if (status != Z_OK) ESCAPE(FALSE);
     *Nout = tNout / out_pixsize;
 
Index: /branches/eam_branches/ohana.20150429/src/libfits/table/F_uncompress_T.c
===================================================================
--- /branches/eam_branches/ohana.20150429/src/libfits/table/F_uncompress_T.c	(revision 38339)
+++ /branches/eam_branches/ohana.20150429/src/libfits/table/F_uncompress_T.c	(revision 38340)
@@ -98,5 +98,6 @@
 
   // allocate the intermediate storage buffers
-  ALLOCATE (raw,   char, max_width*ztilelen);
+  int Nraw_alloc = max_width*ztilelen + 100;
+  ALLOCATE (raw,   char, Nraw_alloc);
   ALLOCATE (zdata, char, max_width*ztilelen);
 
@@ -120,6 +121,5 @@
     
       int Nrows = (row == Ntile - 1) ? ztilelast : ztilelen;
-      int Nraw = Nrows*fields[i].Nvalues; // number of pixels
-
+      int Nraw = Nraw_alloc; // expected number of pixel: Nrows*fields[i].Nvalues
       if (!gfits_uncompress_data (zdata, Nzdata, fields[i].zctype, NULL, NULL, 0, raw, &Nraw, fields[i].pixsize)) ESCAPE(A);
 
Index: /branches/eam_branches/ohana.20150429/src/libfits/test/compress.sh
===================================================================
--- /branches/eam_branches/ohana.20150429/src/libfits/test/compress.sh	(revision 38339)
+++ /branches/eam_branches/ohana.20150429/src/libfits/test/compress.sh	(revision 38340)
@@ -1,13 +1,43 @@
+
+macro testall
+
+  foreach bitpix 8 16 32 -32 -64
+    echo ===== bitpix = $bitpix =====
+    test1 $bitpix
+  end
+end
 
 macro test1
- mcreate z 500 500
+ if ($0 != 2)
+   echo "USAGE: test_image (bitpix)"
+   break
+ end
+
+ mcreate z 8 8
  set x = xramp(z)
- wd x test.raw.fits -bitpix -32 -bzero 0 -bscale 1
- wd x test.cmp.fits -bitpix -32 -bzero 0 -bscale 1 -compress
+ wd x test.raw.fits -bitpix $1 -bzero 0 -bscale 1
+ wd x test.cmp.fits -bitpix $1 -bzero 0 -bscale 1 -compress
  rd b test.cmp.fits -x 0
  set d = x - b
  stat d
- exec fpack -g -S test.raw.fits > test.fpk.fits
- exec funpack -S test.cmp.fits > test.fun.fits
+
+ if ($bitpix == -64)
+   echo "*** bitpix $bitpix fails fpack / funpack ***"
+ else
+   exec fpack -g -S test.raw.fits > test.fpk.fits
+   echo "load fpack version"
+   rd c test.fpk.fits -x 0
+   set d = x - c
+   stat d
+ end
+
+ if ($bitpix == -64)
+   echo "*** bitpix $bitpix fails fpack / funpack ***"
+ else
+   exec funpack -S test.cmp.fits > test.fun.fits
+   rd e test.fun.fits
+   set d = x - e
+   stat d
+ end
 end
 
@@ -25,10 +55,68 @@
 macro test3
 
+ $NPT = 10
+ create x 0 $NPT
+ set y = x^2
+ create ID 1 {$NPT + 1} -int
+
+ write -fits test test.raw.tbl x y ID
+ write -fits test test.cmp.tbl x y ID -compress-mode NONE
+
+ foreach f x y ID
+   set $f\_raw = $f
+   delete $f
+ end
+
+ echo "===== raw ====="
+ data test.raw.tbl
+ read -fits test x y ID
+
+ foreach f x y ID
+   set dv = $f - $f\_raw
+   vstat dv
+ end
+ delete -q x y ID
+
+ echo "===== cmp ====="
+ data test.cmp.tbl
+ read -fits test x y ID
+
+ foreach f x y ID
+   set dv = $f - $f\_raw
+   vstat dv
+ end
+end
+
+macro test4
+
  create x 0 100
  set y = x^2
+ create ID 1 101 -int
 
- create ID 1 101 -int
  write -fits test test.raw.tbl x y ID
+ write -fits test test.cmp.tbl x y ID -compress
 
- write -fits test test.cmp.tbl x y ID -compress
+ foreach f x y ID
+   set $f\_raw = $f
+   delete $f
+ end
+
+ echo "===== raw ====="
+ data test.raw.tbl
+ read -fits test x y ID
+
+ foreach f x y ID
+   set dv = $f - $f\_raw
+   vstat dv
+ end
+ delete -q x y ID
+
+ echo "===== cmp ====="
+ data test.cmp.tbl
+ read -fits test x y ID
+
+ foreach f x y ID
+   set dv = $f - $f\_raw
+   vstat dv
+ end
 end
Index: /branches/eam_branches/ohana.20150429/src/libfits/test/imagecomp.c
===================================================================
--- /branches/eam_branches/ohana.20150429/src/libfits/test/imagecomp.c	(revision 38339)
+++ /branches/eam_branches/ohana.20150429/src/libfits/test/imagecomp.c	(revision 38340)
@@ -3,5 +3,5 @@
 # include "tap_ohana.h"
 
-int test_compress (char *zcmptype);
+int test_compress (int bitpix, char *zcmptype);
 
 int main (int argc, char **argv) {
@@ -11,6 +11,15 @@
   diag ("libfits imagecomp.c tests");
 
-  test_compress ("NONE");
-  test_compress ("GZIP_1");
+  test_compress (  8, "NONE");
+  test_compress ( 16, "NONE");
+  test_compress ( 32, "NONE");
+  test_compress (-32, "NONE");
+  test_compress (-64, "NONE");
+
+  test_compress (  8, "GZIP_1");
+  test_compress ( 16, "GZIP_1");
+  test_compress ( 32, "GZIP_1");
+  test_compress (-32, "GZIP_1");
+  test_compress (-64, "GZIP_1");
 
   exit (0);
@@ -20,5 +29,5 @@
 # define NY 100
 
-int test_compress (char *zcmptype) { // make a table, compress, uncompress, compare : use compression zcmptype
+int test_compress (int bitpix, char *zcmptype) { // make a table, compress, uncompress, compare : use compression zcmptype
 
   Header rawheader;
@@ -31,5 +40,5 @@
   ftable.header = &theader;
 
-  diag ("--- starting test_compress with zcmptype %s ---", zcmptype);
+  diag ("--- starting test_compress with bitpix %d, zcmptype %s ---", bitpix, zcmptype);
   ok (gfits_init_header (&rawheader), "init'ed the raw header");
   ok (gfits_init_matrix (&rawmatrix), "init'ed the raw matrix");
@@ -38,5 +47,5 @@
 
   /* assign the necessary internal values */
-  rawheader.bitpix   = -32;
+  rawheader.bitpix   = bitpix;
   rawheader.Naxes = 2;
   rawheader.Naxis[0] = NX;
@@ -46,10 +55,22 @@
   ok (gfits_create_header (&rawheader),          "created header");
   ok (gfits_create_matrix (&rawheader, &rawmatrix), "created matrix");
-
-  float *rawdata = (float *) rawmatrix.buffer;
+  
+  char   *rawdata_char   = (char   *) rawmatrix.buffer;
+  short  *rawdata_short  = (short  *) rawmatrix.buffer;
+  int    *rawdata_int    = (int    *) rawmatrix.buffer;
+  float  *rawdata_float  = (float  *) rawmatrix.buffer;
+  double *rawdata_double = (double *) rawmatrix.buffer;
+  
   int ix, iy;
   for (ix = 0; ix < NX; ix++) {
     for (iy = 0; iy < NY; iy++) {
-      rawdata[ix + NX*iy] = ix * iy*iy;
+      switch (bitpix) {
+	case   8: rawdata_char  [ix + NX*iy] = ix + iy*iy; break;
+	case  16: rawdata_short [ix + NX*iy] = ix + iy*iy; break;
+	case  32: rawdata_int   [ix + NX*iy] = ix + iy*iy; break;
+	case -32: rawdata_float [ix + NX*iy] = ix + iy*iy; break;
+	case -64: rawdata_double[ix + NX*iy] = ix + iy*iy; break;
+	default: myAbort ("bad bitpix value");
+      }
     }
   }     
@@ -64,8 +85,20 @@
   int Nbad = 0;
 
-  float *outdata = (float *) outmatrix.buffer;
+  char   *outdata_char   = (char   *) outmatrix.buffer;
+  short  *outdata_short  = (short  *) outmatrix.buffer;
+  int    *outdata_int    = (int    *) outmatrix.buffer;
+  float  *outdata_float  = (float  *) outmatrix.buffer;
+  double *outdata_double = (double *) outmatrix.buffer;
+
   for (ix = 0; ix < NX; ix++) {
     for (iy = 0; iy < NY; iy++) {
-      if (rawdata[ix + NX*iy] != outdata[ix + NX*iy]) Nbad ++;
+      switch (bitpix) {
+	case   8: if (rawdata_char  [ix + NX*iy] != outdata_char  [ix + NX*iy]) Nbad ++; break;
+	case  16: if (rawdata_short [ix + NX*iy] != outdata_short [ix + NX*iy]) Nbad ++; break;
+	case  32: if (rawdata_int   [ix + NX*iy] != outdata_int   [ix + NX*iy]) Nbad ++; break;
+	case -32: if (rawdata_float [ix + NX*iy] != outdata_float [ix + NX*iy]) Nbad ++; break;
+	case -64: if (rawdata_double[ix + NX*iy] != outdata_double[ix + NX*iy]) Nbad ++; break;
+	default: myAbort ("bad bitpix value");
+      }
     }
   }     
Index: /branches/eam_branches/ohana.20150429/src/libfits/test/tablecomp.c
===================================================================
--- /branches/eam_branches/ohana.20150429/src/libfits/test/tablecomp.c	(revision 38339)
+++ /branches/eam_branches/ohana.20150429/src/libfits/test/tablecomp.c	(revision 38340)
@@ -82,9 +82,9 @@
   off_t Nrow;
 
-  short  *SEQ_t = gfits_get_bintable_column_data (outheader, outtable, "SEQ", type, &Nrow, &Ncol); ok (!strcmp (type, "short"),  "read short  table column"); 
-  int     *ID_t = gfits_get_bintable_column_data (outheader, outtable,  "ID", type, &Nrow, &Ncol); ok (!strcmp (type, "int"),    "read int    table column"); 
-  float    *X_t = gfits_get_bintable_column_data (outheader, outtable,   "X", type, &Nrow, &Ncol); ok (!strcmp (type, "float"),  "read float  table column"); 
-  double   *Y_t = gfits_get_bintable_column_data (outheader, outtable,   "Y", type, &Nrow, &Ncol); ok (!strcmp (type, "double"), "read double table column"); 
-    
+  short  *SEQ_t = gfits_get_bintable_column_data (outheader, outtable, "SEQ", type, &Nrow, &Ncol); ok (!strcmp (type, "short"),  "read short  table column"); ok (Nrow == Nval, "right number of rows"); ok (Ncol == 1, "right number of cols");
+  int     *ID_t = gfits_get_bintable_column_data (outheader, outtable,  "ID", type, &Nrow, &Ncol); ok (!strcmp (type, "int"),    "read int    table column"); ok (Nrow == Nval, "right number of rows"); ok (Ncol == 1, "right number of cols");
+  float    *X_t = gfits_get_bintable_column_data (outheader, outtable,   "X", type, &Nrow, &Ncol); ok (!strcmp (type, "float"),  "read float  table column"); ok (Nrow == Nval, "right number of rows"); ok (Ncol == 1, "right number of cols");
+  double   *Y_t = gfits_get_bintable_column_data (outheader, outtable,   "Y", type, &Nrow, &Ncol); ok (!strcmp (type, "double"), "read double table column"); ok (Nrow == Nval, "right number of rows"); ok (Ncol == 1, "right number of cols");
+
   // count mismatched values
   int Nseq = 0;
Index: /branches/eam_branches/ohana.20150429/src/opihi/cmd.data/rd.c
===================================================================
--- /branches/eam_branches/ohana.20150429/src/opihi/cmd.data/rd.c	(revision 38339)
+++ /branches/eam_branches/ohana.20150429/src/opihi/cmd.data/rd.c	(revision 38340)
@@ -85,5 +85,5 @@
       return (FALSE);
     }
-    if (gfits_extension_is_compressed (&buf[0].header)) {
+    if (gfits_extension_is_compressed_image (&buf[0].header)) {
 	IsCompressed = TRUE;
     }
@@ -118,5 +118,5 @@
       Nword = 1;
       IsCompressed = FALSE;
-      if (gfits_extension_is_compressed (&buf[0].header)) {
+      if (gfits_extension_is_compressed_image (&buf[0].header)) {
 	if (!strcmp (CCDKeyword, "EXTNAME")) Nword = 1;
 	IsCompressed = TRUE;
@@ -194,17 +194,5 @@
 
     if (!gfits_fread_ftable_data (f, &ftable, FALSE)) { fprintf (stderr, "problem reading compressed image table data\n"); fclose (f); return FALSE; }
-    fprintf (stderr, "rd 1: ");
-    for (i = 0; i < 32; i++) {
-      fprintf (stderr, "0x%0hhx ", ftable.buffer[i]);
-    }
-    fprintf (stderr, "\n");
-  
     if (!gfits_byteswap_varlength_column (&ftable, 1)) { fprintf (stderr, "problem doing byteswap on compressed image metadata column\n"); fclose (f); gfits_free_table (&ftable); return FALSE; }
-    fprintf (stderr, "rd 2: ");
-    for (i = 0; i < 32; i++) {
-      fprintf (stderr, "0x%0hhx ", ftable.buffer[i]);
-    }
-    fprintf (stderr, "\n");
-
     if (!gfits_uncompress_image (&buf[0].header, &buf[0].matrix, &ftable)) { fprintf (stderr, "problem uncompressing the image data\n"); fclose (f); gfits_free_table (&ftable); return FALSE; }
 
Index: /branches/eam_branches/ohana.20150429/src/opihi/cmd.data/read_vectors.c
===================================================================
--- /branches/eam_branches/ohana.20150429/src/opihi/cmd.data/read_vectors.c	(revision 38339)
+++ /branches/eam_branches/ohana.20150429/src/opihi/cmd.data/read_vectors.c	(revision 38340)
@@ -412,4 +412,6 @@
   table.header = &header;
 
+  int IsCompressed = FALSE;
+
   /* load appropriate extension (if extname is a number, use count) */
   if (Nextend > -1) {
@@ -430,9 +432,23 @@
     if (!gfits_load_header (f, &header)) ESCAPE ("error reading header for extension %d\n", Nextend);
 
-    if (getSizes) {
+    IsCompressed = gfits_extension_is_compressed_table (&header);
+
+    if (getSizes && !IsCompressed) {
       read_table_sizes (&header);
       if (CCDKeyword != NULL) free (CCDKeyword); 
       gfits_free_header (&header); 
       return TRUE;
+    }
+    if (IsCompressed) {
+      if (getSizes) {
+	gprint (GP_ERR, "%s[%s] is compressed: -sizes is invalid\n", filename, extname);
+	gfits_free_header (&header); 
+	return FALSE;
+      }
+      if ((start > 0) || (Nrows > -1)) {
+	gprint (GP_ERR, "%s[%s] is compressed: must read entire table\n", filename, extname);
+	gfits_free_header (&header); 
+	return FALSE;
+      }
     }
 
@@ -483,5 +499,7 @@
       }
 
-      if (getSizes) {
+      IsCompressed = gfits_extension_is_compressed_table (&header);
+
+      if (getSizes && !IsCompressed) {
 	read_table_sizes (&header);
 	if (CCDKeyword != NULL) free (CCDKeyword); 
@@ -489,4 +507,16 @@
 	return TRUE;
       }
+      if (IsCompressed) {
+	if (getSizes) {
+	  gprint (GP_ERR, "%s[%s] is compressed: -sizes is invalid\n", filename, extname);
+	  gfits_free_header (&header); 
+	  return FALSE;
+	}
+	if ((start > 0) || (Nrows > -1)) {
+	  gprint (GP_ERR, "%s[%s] is compressed: must read entire table\n", filename, extname);
+	  gfits_free_header (&header); 
+	  return FALSE;
+	}
+      }
 
       if (Nrows == -1) {
@@ -503,7 +533,4 @@
 
       if (!gfits_fread_ftable_range (f, padIfShort, &table, start, Nrows)) ESCAPE ("error reading table for extension %d\n", Nextend);
-
-      // if (!gfits_fread_ftable_data (f, &table, padIfShort)) ESCAPE ("error reading table for extension\n");
-
       break;
     }
@@ -518,4 +545,19 @@
   Binary = !strcmp (type, "BINTABLE");
   Ny = header.Naxis[1];
+
+  Header *outheader = &header;
+  FTable *outtable  = &table;
+
+  Header rawheader;
+  FTable rawtable;
+  if (IsCompressed) {
+    rawtable.header = &rawheader;
+    if (!gfits_uncompress_table (&table, &rawtable)) ESCAPE ("failed to uncompress table");
+    outheader = &rawheader;
+    outtable  = &rawtable;
+    gfits_free_header (&header);
+    gfits_free_table (&table);
+    Ny = rawheader.Naxis[1];
+  }
 
   /* find columns which match requested vectors */
@@ -527,9 +569,9 @@
     Nval = 0;
     if (Binary) {
-      if (!gfits_get_bintable_column_type (&header, argv[i], type, &Nval)) ESCAPE ("requested field not found");
-      if (!gfits_get_bintable_column (&header, &table, argv[i], &data)) ESCAPE ("error reading data from specified field");
+      if (!gfits_get_bintable_column_type (outheader, argv[i], type, &Nval)) ESCAPE ("requested field not found");
+      if (!gfits_get_bintable_column (outheader, outtable, argv[i], &data)) ESCAPE ("error reading data from specified field");
     } else {
-      if (!gfits_get_table_column_type (&header, argv[i], type)) ESCAPE ("requested field not found");
-      if (!gfits_get_table_column (&header, &table, argv[i], &data)) ESCAPE ("error reading data from specified field");
+      if (!gfits_get_table_column_type (outheader, argv[i], type)) ESCAPE ("requested field not found");
+      if (!gfits_get_table_column (outheader, outtable, argv[i], &data)) ESCAPE ("error reading data from specified field");
     }
     if (Nval == 0) ESCAPE ("no data for field in table");
@@ -594,6 +636,6 @@
   }
   if (CCDKeyword != NULL) free (CCDKeyword);
-  gfits_free_table (&table);
-  gfits_free_header (&header);
+  gfits_free_table (outtable);
+  gfits_free_header (outheader);
   return (TRUE);
 }
Index: /branches/eam_branches/ohana.20150429/src/opihi/cmd.data/wd.c
===================================================================
--- /branches/eam_branches/ohana.20150429/src/opihi/cmd.data/wd.c	(revision 38339)
+++ /branches/eam_branches/ohana.20150429/src/opihi/cmd.data/wd.c	(revision 38340)
@@ -205,19 +205,6 @@
     ftable.header = &theader;
 
-    int i;
     gfits_compress_image (&temp_header, &temp_matrix, &ftable, NULL, CompressMode);
-    fprintf (stderr, "wd 1: ");
-    for (i = 0; i < 32; i++) {
-      fprintf (stderr, "0x%0hhx ", ftable.buffer[i]);
-    }
-    fprintf (stderr, "\n");
-
     gfits_byteswap_varlength_column (&ftable, 1);
-    fprintf (stderr, "wd 2: ");
-    for (i = 0; i < 32; i++) {
-      fprintf (stderr, "0x%0hhx ", ftable.buffer[i]);
-    }
-    fprintf (stderr, "\n");
-    
     gfits_fwrite_Theader (f, &theader);
     gfits_fwrite_table  (f, &ftable);
Index: /branches/eam_branches/ohana.20150429/src/opihi/cmd.data/write_vectors.c
===================================================================
--- /branches/eam_branches/ohana.20150429/src/opihi/cmd.data/write_vectors.c	(revision 38339)
+++ /branches/eam_branches/ohana.20150429/src/opihi/cmd.data/write_vectors.c	(revision 38340)
@@ -63,8 +63,12 @@
   }
 
-  int compress = FALSE;
-  if ((N = get_argument (argc, argv, "-compress"))) {
-    remove_argument (N, &argc, argv);
-    compress = TRUE;
+  char *compress = NULL;
+  if ((N = get_argument (argc, argv, "-compress-mode"))) {
+    remove_argument (N, &argc, argv);
+    compress = strcreate(argv[N]);
+    remove_argument (N, &argc, argv);
+  } else if ((N = get_argument (argc, argv, "-compress"))) {
+    remove_argument (N, &argc, argv);
+    compress = strcreate("GZIP_1");
     if (!FITS) {
       fprintf (stderr, "NOTE: write_vectors -compress has no effect on non-FITS\n");
Index: /branches/eam_branches/ohana.20150429/src/opihi/include/dvomath.h
===================================================================
--- /branches/eam_branches/ohana.20150429/src/opihi/include/dvomath.h	(revision 38339)
+++ /branches/eam_branches/ohana.20150429/src/opihi/include/dvomath.h	(revision 38340)
@@ -167,5 +167,5 @@
 
 /* vector IO functions */
-int           WriteVectorTableFITS  PROTO((char *filename, char *extname, Vector **vec, int Nvec, int append, int compress, char *format));
+int           WriteVectorTableFITS  PROTO((char *filename, char *extname, Vector **vec, int Nvec, int append, char *compress, char *format));
 Vector      **ReadVectorTableFITS   PROTO((char *filename, char *extname, int *Nvec));
 
Index: /branches/eam_branches/ohana.20150429/src/opihi/lib.shell/VectorIO.c
===================================================================
--- /branches/eam_branches/ohana.20150429/src/opihi/lib.shell/VectorIO.c	(revision 38339)
+++ /branches/eam_branches/ohana.20150429/src/opihi/lib.shell/VectorIO.c	(revision 38340)
@@ -72,5 +72,5 @@
   
 // write a set of vectors to a FITS file (vectors names become fits column names)
-int WriteVectorTableFITS (char *filename, char *extname, Vector **vec, int Nvec, int append, int compress, char *format) {
+int WriteVectorTableFITS (char *filename, char *extname, Vector **vec, int Nvec, int append, char *compress, char *format) {
   
   Header rawheader;
@@ -106,5 +106,5 @@
   if (compress) {
     cmptable.header = &cmpheader;
-    if (!gfits_compress_table (&rawtable, &cmptable, 10, "GZIP_1")) goto escape;
+    if (!gfits_compress_table (&rawtable, &cmptable, 10, compress)) goto escape;
     outtable = &cmptable;
     outheader = &cmpheader;
