Index: trunk/psLib/src/fits/psFitsHeader.c
===================================================================
--- trunk/psLib/src/fits/psFitsHeader.c	(revision 23376)
+++ trunk/psLib/src/fits/psFitsHeader.c	(revision 25002)
@@ -147,5 +147,4 @@
     return keyword;
 }
-
 
 bool psFitsCheckCompressedImagePHU(const psFits *fits, psMetadata *header)
@@ -497,11 +496,14 @@
 static bool fitsWriteHeader(psFits *fits, // The FITS file handle
                             const psMetadata *output, // Metadata that is to be output into the FITS file
-                            bool keyStarts // Write out the key starts?
+                            bool keyStarts, // Write out the key starts?
+                            bool phuImage   // Are we writing a PHU image?
                            )
 {
     int status = 0;                     // Status of cfitsio calls
+    int extnum = psFitsGetExtNum(fits); // Number of extension
     bool simple = true;                 // If SIMPLE is T, then the file should conform to the FITS standard
     psFitsCompressionType compress = psFitsCompressionGetType(fits); // Compression type
-    if (psFitsGetExtNum(fits) == 0) {
+    if (extnum == 0) {
+
         // We allow the user to write SIMPLE, but it must be boolean
         psMetadataItem *simpleItem = psMetadataLookup(output, "SIMPLE"); // SIMPLE in the header
@@ -526,5 +528,4 @@
     bool compressing = ((!fits->options || fits->options->conventions.compression) &&
                         compress != PS_FITS_COMPRESS_NONE) ? true : false; // Are we compressing?
-
     if (compressing) {
         psMetadataItem *simpleItem = psMetadataLookup(output, "SIMPLE"); // SIMPLE in the header
@@ -538,7 +539,14 @@
             }
         }
-        //        int value = simple;             // Temporary holder for boolean
-        //        fits_update_key(fits->fd, TLOGICAL, "ZSIMPLE", &value,
-        //                        "Uncompressed file's conformance to FITS standard", &status);
+        if (simple && phuImage && extnum == 1) {
+            // ZSIMPLE is required for decompression with funpack, etc.  For funpack to work, ZSIMPLE needs to
+            // go early in the FITS header (otherwise we get "Extension doesn't start with SIMPLE or XTENSION
+            // keyword.").  We put it after ZIMAGE by reading ZIMAGE (which sets the insertion pointer) and
+            // then inserting ZSIMPLE.
+            char comment[FLEN_CARD];    // Comment for ZIMAGE; unused
+            int value;                  // Value for ZIMAGE; unused
+            fits_read_key(fits->fd, TLOGICAL, "ZIMAGE", &value, comment, &status);
+            fits_insert_key_log(fits->fd, "ZSIMPLE", simple, "Uncompressed file's conforms to FITS", &status);
+        }
     }
 
@@ -699,5 +707,17 @@
     PS_ASSERT_METADATA_NON_NULL(output, false);
 
-    return fitsWriteHeader(fits, output, true);
+    return fitsWriteHeader(fits, output, true, false);
+}
+
+bool psFitsWriteHeaderImage(psFits *fits,
+                            const psMetadata *output,
+                            bool phuImage
+                            )
+{
+    PS_ASSERT_FITS_NON_NULL(fits, false);
+    PS_ASSERT_FITS_WRITABLE(fits, false);
+    PS_ASSERT_METADATA_NON_NULL(output, false);
+
+    return fitsWriteHeader(fits, output, true, phuImage);
 }
 
@@ -733,5 +753,5 @@
     }
 
-    if (output && !fitsWriteHeader(fits, output, false)) {
+    if (output && !fitsWriteHeader(fits, output, false, false)) {
         psError(PS_ERR_IO, false, "Unable to write FITS header.\n");
         return false;
