Index: trunk/Ohana/src/opihi/cmd.data/rd.c
===================================================================
--- trunk/Ohana/src/opihi/cmd.data/rd.c	(revision 13479)
+++ trunk/Ohana/src/opihi/cmd.data/rd.c	(revision 15487)
@@ -5,5 +5,5 @@
   
   int i, N, status, plane, Nplane, extend, Nextend, Nskip, JustHead;
-  int ccdsel, done;
+  int ccdsel, done, Nword, IsCompressed;
   char region[512], *ccdid, *filename;
   FILE *f;
@@ -85,5 +85,9 @@
       return (FALSE);
     }
+    if (gfits_extension_is_compressed (&buf[0].header)) {
+	IsCompressed = TRUE;
+    }
   } 
+
   /* FITS extension by name */
   if (ccdsel) {
@@ -92,6 +96,6 @@
     CCDKeyword = get_variable ("CCDKEYWORD");
     if (CCDKeyword == (char *) NULL) {
-      gprint (GP_ERR, "CCDKEYWORD variable is not set; ");
-      gprint (GP_ERR, "using EXTNAME as default\n");
+      // gprint (GP_ERR, "CCDKEYWORD variable is not set; ");
+      // gprint (GP_ERR, "using EXTNAME as default\n");
       CCDKeyword = strcreate ("EXTNAME");
     }
@@ -106,5 +110,17 @@
 	return (FALSE);
       }
-      if (!gfits_scan (&buf[0].header, CCDKeyword, "%s", 1, ID)) {
+
+      // for compressed data tables, EXTNAME may be duplicated, with the first one containing the
+      // word 'COMPRESSED_IMAGE'.  in this case, check the second EXTNAME, if CCDKeyword == EXTNAME 
+      // this may have to be a more obscure test specifically for 'imcopy' data...
+      // need to check each header, since file may contain a mix
+      
+      Nword = 1;
+      IsCompressed = FALSE;
+      if (gfits_extension_is_compressed (&buf[0].header)) {
+	if (!strcmp (CCDKeyword, "EXTNAME")) Nword = 2;
+	IsCompressed = TRUE;
+      }
+      if (!gfits_scan (&buf[0].header, CCDKeyword, "%s", Nword, ID)) {
 	gprint (GP_ERR, "%s not in header\n", CCDKeyword);
 	DeleteBuffer (buf);
@@ -123,11 +139,7 @@
   /* fix up header, if needed */
   if (extend || ccdsel) {
-    static char simple[] = "SIMPLE  =                    T / Standard FITS";
-    int Ns, No;
-
-    Ns = strlen (simple);
-    No = 80 - Ns;
-    strncpy (buf[0].header.buffer, simple, Ns);
-    memset (&buf[0].header.buffer[Ns], ' ', No);
+    if (!IsCompressed) {
+      gfits_extended_to_primary (&buf[0].header, TRUE, "Standard FITS");
+    }
   } else {
       gfits_fread_header (f, &buf[0].header);
@@ -135,6 +147,7 @@
 
   /* for JustHead, we skip reading the data segment */ 
+  // XXX for compressed data, we need to convert the header to the equivalent uncompressed version
   if (JustHead) {
-    /* should be in CreateMatrix / CreateBuffer function */
+    // XXX what are we doing here exactly?
     buf[0].header.Naxes = 0;
     ALLOCATE (buf[0].matrix.buffer, char, 1);
@@ -164,7 +177,18 @@
 
   /* load matrix data */
-  sprintf (region, "-1 -1 -1 -1 %d %d", (plane - 1), plane);
-  status = gfits_fread_matrix_segment (f, &buf[0].matrix, &buf[0].header, region);
-  fclose (f);
+  if (IsCompressed) {
+    FTable ftable;
+    Header theader;
+    ftable.header = &theader;
+    gfits_copy_header (&buf[0].header, ftable.header);
+    status = gfits_fread_ftable_data (f, &ftable);  // XXX does this do more than read the bytes?
+    status = gfits_uncompress_image (&buf[0].header, &buf[0].matrix, &ftable, !(ccdsel || extend));
+    gfits_free_table (&ftable);
+    // XXX this currently does not work for a cube (we get a cube back, not a specific plane)
+  } else {
+    sprintf (region, "-1 -1 -1 -1 %d %d", (plane - 1), plane);
+    status = gfits_fread_matrix_segment (f, &buf[0].matrix, &buf[0].header, region);
+    fclose (f);
+  }
 
   if (!status) {
