Index: trunk/psLib/src/fits/psFitsHeader.c
===================================================================
--- trunk/psLib/src/fits/psFitsHeader.c	(revision 17567)
+++ trunk/psLib/src/fits/psFitsHeader.c	(revision 17587)
@@ -7,6 +7,6 @@
  *  @author Robert DeSonia, MHPCC
  *
- *  @version $Revision: 1.44 $ $Name: not supported by cvs2svn $
- *  @date $Date: 2008-04-17 23:43:02 $
+ *  @version $Revision: 1.45 $ $Name: not supported by cvs2svn $
+ *  @date $Date: 2008-05-08 21:40:25 $
  *
  *  Copyright 2004-2005 Maui High Performance Computing Center, University of Hawaii
@@ -47,10 +47,14 @@
 // List of FITS header keys not to write (handled by cfitsio); NULL-terminated
 static const char *noWriteFitsKeys[] = { "SIMPLE", "XTENSION", "BITPIX", "NAXIS", "EXTNAME", "BSCALE",
-                                         "BZERO", "TFIELDS", "PCOUNT", "GCOUNT", "ZIMAGE", "ZBITPIX",
-                                         "ZCMPTYPE", "PSBITPIX", NULL };
+                                         "BZERO", "TFIELDS", "PCOUNT", "GCOUNT", "PSBITPIX", NULL };
 
 // List of the start of FITS header keys not to write (handled by cfitsio); NULL-terminated
-static const char *noWriteFitsKeyStarts[] = { "NAXIS", "TTYPE", "TFORM", "ZNAXIS", "ZTILE", "ZNAME", "ZVAL",
-                                              NULL };
+static const char *noWriteFitsKeyStarts[] = { "NAXIS", "TTYPE", "TFORM", NULL };
+
+// List of compressed FITS header keys not to write (handled by cfitsio); NULL-terminated
+static const char *noWriteCompressedKeys[] = { "ZBITPIX", "ZIMAGE", "ZBITPIX", "ZCMPTYPE", NULL };
+
+// List of the start of FITS header keys not to write (handled by cfitsio); NULL-terminated
+static const char *noWriteCompressedKeyStarts[] = { "ZNAXIS", "ZTILE", "ZNAME", "ZVAL", NULL };
 
 // List of FITS header keys that may be present if the header is considered "empty"; NULL-terminated
@@ -65,4 +69,5 @@
 
 // Translation for compressed image headers           FROM        TO
+// The "From" and "To" are appropriate for reading.
 static keywordTranslation compressTranslation[] = { { "ZSIMPLE",  "SIMPLE" },
                                                     { "ZTENSION", "XTENSION" },
@@ -94,4 +99,19 @@
 }
 
+// Compare a keyword with a list of keyword beginnings; return true if the keyword starts with one of these
+static bool keywordStartsWith(const char *keyword, // Keyword to check
+                              const char *list[] // List of keyword beginnings
+                              )
+{
+    bool writeKey = true;   // Write this keyword?
+    for (int i = 0; list[i] && writeKey; i++) {
+        if (strncmp(keyword, list[i], strlen(list[i])) == 0) {
+            return true;
+        }
+    }
+    return false;
+}
+
+
 // Translate one keyword to another.
 // This is appropriate for reading
@@ -125,6 +145,6 @@
         }
     }
-    // It translates to itself, so ignore
-    return NULL;
+    // It translates to itself
+    return keyword;
 }
 
@@ -528,33 +548,25 @@
             // image, the NAXISn haven't been changed; or after converting to F32, the BITPIX hasn't been
             // changed) so we'll take care of that for them.
-            if (keywordInList(name, noWriteFitsKeys)) {
-                if (strcmp(name, "SIMPLE") == 0 || compress == PS_FITS_COMPRESS_NONE ||
-                    (fits->options && !fits->options->conventions.compression)) {
-                    // No compression happening, so don't write keyword
+
+            // Also block out TTYPEn, NAXISn, etc --- keywords that start with a certain sequence.
+            // We want to do this when writing an image or table, since it guarantees that the NAXISn etc
+            // that go in are correct.  However, when we're writing a "blank" HDU (header only), we want
+            // to preserve NAXISn etc for reference, so we don't do this.
+
+            // Options for compression
+            if (compress != PS_FITS_COMPRESS_NONE &&
+                (!fits->options || fits->options->conventions.compression)) {
+                // Check to see if the keyword should be translated
+                name = (char*)keywordUntranslate(name, compressTranslation); // Casting away const for cfitsio
+
+                if (keywordInList(name, noWriteCompressedKeys) ||
+                    (keyStarts && keywordStartsWith(name, noWriteCompressedKeyStarts))) {
                     continue;
                 }
-                // Check to see if the keyword should be translated rather than written
-                name = (char*)keywordUntranslate(name, compressTranslation); // Casting away const for cfitsio
-                if (!name) {
-                    // Not in translation list, so don't write
-                    continue;
-                }
-            }
-
-            if (keyStarts) {
-                // Also block out TTYPEn, NAXISn, etc --- keywords that start with a certain sequence.
-                // We want to do this when writing an image or table, since it guarantees that the NAXISn etc
-                // that go in are correct.  However, when we're writing a "blank" HDU (header only), we want
-                // to preserve NAXISn etc for reference, so we don't do this.
-                bool writeKey = true;   // Write this keyword?
-                for (int i = 0; noWriteFitsKeyStarts[i] && writeKey; i++) {
-                    if (strncmp(item->name, noWriteFitsKeyStarts[i], strlen(noWriteFitsKeyStarts[i])) == 0) {
-                        writeKey = false;
-                    }
-                }
-                if (!writeKey) {
-                    // Don't write it; skip to the next key
-                    continue;
-                }
+            }
+
+            if (keywordInList(name, noWriteFitsKeys) ||
+                (keyStarts && keywordStartsWith(name, noWriteFitsKeyStarts))) {
+                continue;
             }
         }
