Index: trunk/psLib/src/fits/psFitsHeader.c
===================================================================
--- trunk/psLib/src/fits/psFitsHeader.c	(revision 16665)
+++ trunk/psLib/src/fits/psFitsHeader.c	(revision 16822)
@@ -7,6 +7,6 @@
  *  @author Robert DeSonia, MHPCC
  *
- *  @version $Revision: 1.40 $ $Name: not supported by cvs2svn $
- *  @date $Date: 2008-02-26 23:42:21 $
+ *  @version $Revision: 1.41 $ $Name: not supported by cvs2svn $
+ *  @date $Date: 2008-03-05 02:19:28 $
  *
  *  Copyright 2004-2005 Maui High Performance Computing Center, University of Hawaii
@@ -93,4 +93,6 @@
 }
 
+// Translate one keyword to another.
+// This is appropriate for reading
 static const char *keywordTranslate(const char *keyword, // Keyword to check
                                     keywordTranslation translation[] // Translation list
@@ -108,4 +110,20 @@
     // It translates to itself
     return keyword;
+}
+
+// Translate back the other way.
+// This is appropriate for writing.
+static const char *keywordUntranslate(const char *keyword, // Keyword to check
+                                      keywordTranslation translation[] // Translation list
+    )
+{
+    for (keywordTranslation *trans = translation; (*trans).to; ++trans) {
+        if (strcmp(keyword, (*trans).to) == 0) {
+            // Translate it
+            return (*trans).from;
+        }
+    }
+    // It translates to itself, so ignore
+    return NULL;
 }
 
@@ -461,27 +479,31 @@
     int status = 0;                     // Status of cfitsio calls
     bool simple = true;                 // If SIMPLE is T, then the file should conform to the FITS standard
-    psMetadataItem *simpleItem = psMetadataLookup(output, "SIMPLE"); // SIMPLE in the header
-    if (simpleItem) {
+    if (psFitsGetExtNum(fits) == 0) {
         // We allow the user to write SIMPLE, but it must be boolean
-        if (simpleItem->type != PS_DATA_BOOL) {
-            psError(PS_ERR_BAD_PARAMETER_TYPE, true, "SIMPLE in a FITS header must be of boolean type: "
-                    "not %x --- assuming TRUE.\n", simpleItem->type);
-            int value = false;          // Temporary holder for boolean
-            fits_update_key(fits->fd, TLOGICAL, "SIMPLE", &value,
-                            "File does not conform to FITS standard", &status);
-            simple = false;
-        } else if (!simpleItem->data.B) {
-            simple = false;
-            int value = false;          // Temporary holder for boolean
-            fits_update_key(fits->fd, TLOGICAL, "SIMPLE", &value,
-                            "File does not conform to FITS standard", &status);
-        }
-        // SIMPLE = T is taken care of by cfitsio.
+        psMetadataItem *simpleItem = psMetadataLookup(output, "SIMPLE"); // SIMPLE in the header
+        if (simpleItem) {
+            if (simpleItem->type != PS_DATA_BOOL) {
+                psError(PS_ERR_BAD_PARAMETER_TYPE, true, "SIMPLE in a FITS header must be of boolean type: "
+                        "not %x --- assuming TRUE.\n", simpleItem->type);
+                int value = false;          // Temporary holder for boolean
+                fits_update_key(fits->fd, TLOGICAL, "SIMPLE", &value,
+                                "File does not conform to FITS standard", &status);
+                simple = false;
+            } else if (!simpleItem->data.B) {
+                simple = false;
+                int value = false;          // Temporary holder for boolean
+                fits_update_key(fits->fd, TLOGICAL, "SIMPLE", &value,
+                                "File does not conform to FITS standard", &status);
+            }
+            // SIMPLE = T is taken care of by cfitsio.
+        }
     }
 
     // Traverse the metadata list and add each key.
+    psFitsCompressionType compress = psFitsCompressionGetType(fits); // Compression type
     psListIterator* iter = psListIteratorAlloc(output->list, PS_LIST_HEAD, true); // Iterator
     psMetadataItem* item;               // Item from iteration
     while ((item = psListGetAndIncrement(iter))) {
+        char *name = item->name;        // Keyword name to use when writing out
         // Check to see if the item should be ignored
         if (simple) {
@@ -490,8 +512,18 @@
             // 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(item->name, noWriteFitsKeys)) {
-                // Don't write it; skip to the next key
-                continue;
-            }
+            if (keywordInList(name, noWriteFitsKeys)) {
+                if ((fits->options && !fits->options->conventions.compression) ||
+                    compress == PS_FITS_COMPRESS_NONE) {
+                    // No compression happening, so don't write keyword
+                    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.
@@ -512,5 +544,5 @@
         }
 
-        if (strcmp(item->name, "COMMENT") == 0) {
+        if (strcmp(name, "COMMENT") == 0) {
             if (item->type != PS_DATA_STRING) {
                 psWarning("COMMENT header is not of type STRING (%x) --- ignored.", item->type);
@@ -518,5 +550,5 @@
                 fits_write_comment(fits->fd, item->data.str, &status);
             }
-        } else if (strcmp(item->name,  "HISTORY") == 0) {
+        } else if (strcmp(name,  "HISTORY") == 0) {
             if (item->type != PS_DATA_STRING) {
                 psWarning("COMMENT header is not of type STRING (%x) --- ignored.", item->type);
@@ -529,39 +561,39 @@
             case PS_DATA_BOOL: {
                     int value = item->data.B;
-                    fits_update_key(fits->fd, TLOGICAL, item->name, &value, item->comment, &status);
+                    fits_update_key(fits->fd, TLOGICAL, name, &value, item->comment, &status);
                     break;
                 }
             case PS_DATA_S8:
-                fits_update_key(fits->fd, TBYTE, item->name, &item->data.S8, item->comment, &status);
+                fits_update_key(fits->fd, TBYTE, name, &item->data.S8, item->comment, &status);
                 break;
             case PS_DATA_S16:
-                fits_update_key(fits->fd, TSHORT, item->name, &item->data.S16, item->comment, &status);
+                fits_update_key(fits->fd, TSHORT, name, &item->data.S16, item->comment, &status);
                 break;
             case PS_DATA_S32:
-                fits_update_key(fits->fd, TINT, item->name, &item->data.S32, item->comment, &status);
+                fits_update_key(fits->fd, TINT, name, &item->data.S32, item->comment, &status);
                 break;
             case PS_DATA_U8: {
                     unsigned short int temp = item->data.U8;
-                    fits_update_key(fits->fd, TUSHORT, item->name, &temp, item->comment, &status);
+                    fits_update_key(fits->fd, TUSHORT, name, &temp, item->comment, &status);
                 }
                 break;
             case PS_DATA_U16:
-                fits_update_key(fits->fd, TUSHORT, item->name, &item->data.U16, item->comment, &status);
+                fits_update_key(fits->fd, TUSHORT, name, &item->data.U16, item->comment, &status);
                 break;
             case PS_DATA_U32:
-                fits_update_key(fits->fd, TUINT, item->name, &item->data.U32, item->comment, &status);
+                fits_update_key(fits->fd, TUINT, name, &item->data.U32, item->comment, &status);
                 break;
             case PS_DATA_F32: {
                     int infCheck = 0;         // Result of isinf()
                     if (isnan(item->data.F32)) {
-                        fits_update_key(fits->fd, TSTRING, item->name, "NaN", item->comment, &status);
+                        fits_update_key(fits->fd, TSTRING, name, "NaN", item->comment, &status);
                     } else if ((infCheck = isinf(item->data.F32)) != 0) {
                         if (infCheck == 1) {
-                            fits_update_key(fits->fd, TSTRING, item->name, "Inf", item->comment, &status);
+                            fits_update_key(fits->fd, TSTRING, name, "Inf", item->comment, &status);
                         } else {
-                            fits_update_key(fits->fd, TSTRING, item->name, "-Inf", item->comment, &status);
+                            fits_update_key(fits->fd, TSTRING, name, "-Inf", item->comment, &status);
                         }
                     } else {
-                        fits_update_key(fits->fd, TFLOAT, item->name, &item->data.F32, item->comment,
+                        fits_update_key(fits->fd, TFLOAT, name, &item->data.F32, item->comment,
                                         &status);
                     }
@@ -571,13 +603,13 @@
                     int infCheck = 0;         // Result of isinf()
                     if (isnan(item->data.F64)) {
-                        fits_update_key(fits->fd, TSTRING, item->name, "NaN", item->comment, &status);
+                        fits_update_key(fits->fd, TSTRING, name, "NaN", item->comment, &status);
                     } else if ((infCheck = isinf(item->data.F64)) != 0) {
                         if (infCheck == 1) {
-                            fits_update_key(fits->fd, TSTRING, item->name, "Inf", item->comment, &status);
+                            fits_update_key(fits->fd, TSTRING, name, "Inf", item->comment, &status);
                         } else {
-                            fits_update_key(fits->fd, TSTRING, item->name, "-Inf", item->comment, &status);
+                            fits_update_key(fits->fd, TSTRING, name, "-Inf", item->comment, &status);
                         }
                     } else {
-                        fits_update_key(fits->fd, TDOUBLE, item->name, &item->data.F64, item->comment,
+                        fits_update_key(fits->fd, TDOUBLE, name, &item->data.F64, item->comment,
                                         &status);
                     }
@@ -585,5 +617,5 @@
                 }
             case PS_DATA_STRING:
-                fits_update_key(fits->fd, TSTRING, item->name, item->data.V, item->comment, &status);
+                fits_update_key(fits->fd, TSTRING, name, item->data.V, item->comment, &status);
                 break;
             default:  // all other META types are ignored
