Index: trunk/psLib/src/fits/psFitsHeader.c
===================================================================
--- trunk/psLib/src/fits/psFitsHeader.c	(revision 24987)
+++ 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;
Index: trunk/psLib/src/fits/psFitsHeader.h
===================================================================
--- trunk/psLib/src/fits/psFitsHeader.h	(revision 24987)
+++ trunk/psLib/src/fits/psFitsHeader.h	(revision 25002)
@@ -66,4 +66,14 @@
 );
 
+/** Write a header for an image
+ *  Principal difference with psFitsWriteHeader is this allows writing ZSIMPLE for uncompressing a single image
+ */
+bool psFitsWriteHeaderImage(
+    psFits *fits,                       ///< FITS file
+    const psMetadata *output,           ///< Header to output
+    bool phuImage                       ///< Is this image supposed to be in the PHU?
+    );
+
+
 /** Writes a "blank" --- a header only, with no image or table.
  *
Index: trunk/psLib/src/fits/psFitsImage.c
===================================================================
--- trunk/psLib/src/fits/psFitsImage.c	(revision 24987)
+++ trunk/psLib/src/fits/psFitsImage.c	(revision 25002)
@@ -616,4 +616,6 @@
     }
 
+    bool createPHU = false;             // Are we creating a PHU?
+
     // Create the image HDU
     int hdus = psFitsGetSize(fits);     // Number of HDUs in file
@@ -621,4 +623,5 @@
         // We're creating the first image
         fits_create_img(fits->fd, bitPix, naxis, naxes, &status);
+        createPHU = true;
     } else {
         if (!after) {
@@ -627,4 +630,5 @@
                 // Set status to signal fits_insert_img to insert a new primary HDU
                 status = PREPEND_PRIMARY;
+                createPHU = true;
             } else {
                 // Move back one to perform an insert after the previous HDU
@@ -641,5 +645,5 @@
 
     // write the header, if any.
-    if (header && !psFitsWriteHeader(fits, header)) {
+    if (header && !psFitsWriteHeaderImage(fits, header, createPHU)) {
         psError(PS_ERR_IO, false, "Unable to write FITS header.\n");
         success = false;
