Index: trunk/psLib/src/fits/psFitsImage.c
===================================================================
--- trunk/psLib/src/fits/psFitsImage.c	(revision 7224)
+++ trunk/psLib/src/fits/psFitsImage.c	(revision 7227)
@@ -7,6 +7,6 @@
  *  @author Robert DeSonia, MHPCC
  *
- *  @version $Revision: 1.6 $ $Name: not supported by cvs2svn $
- *  @date $Date: 2006-05-26 00:48:20 $
+ *  @version $Revision: 1.7 $ $Name: not supported by cvs2svn $
+ *  @date $Date: 2006-05-26 03:24:49 $
  *
  *  Copyright 2004-2005 Maui High Performance Computing Center, University of Hawaii
@@ -296,31 +296,28 @@
 {
 
-    if (fits == NULL) {
-        psError(PS_ERR_BAD_PARAMETER_NULL, true,
-                PS_ERRORTEXT_psFits_NULL);
-        return false;
-    }
-
-    if (input == NULL) {
-        psError(PS_ERR_BAD_PARAMETER_NULL, true,
-                PS_ERRORTEXT_psFits_IMAGE_NULL);
-        return false;
-    }
-    int numCols = input->numCols;
-    int numRows = input->numRows;
-
-    int status = 0;
+    if (!fits) {
+        psError(PS_ERR_BAD_PARAMETER_NULL, true, PS_ERRORTEXT_psFits_NULL);
+        return false;
+    }
+
+    if (!input) {
+        psError(PS_ERR_BAD_PARAMETER_NULL, true, PS_ERRORTEXT_psFits_IMAGE_NULL);
+        return false;
+    }
+
+    int numCols = input->numCols;       // Number of columns for image
+    int numRows = input->numRows;       // Number of rows for image
+    int status = 0;                     // Status from cfitsio
 
     // determine the FITS-equivalent parameters
-    int bitPix;
-    double bZero;
-    int dataType;
+    int bitPix;                         // Bits per pixel
+    double bZero;                       // Zero offset
+    int dataType;                       // cfitsio data type
     if (! convertPsTypeToFits(input->type.type, &bitPix, &bZero, &dataType) ) {
         return false;
     }
 
-    int naxis = 3;
-    long naxes[3];
-
+    int naxis = 3;                      // Number of axes
+    long naxes[3];                      // Length of each axis
     naxes[0] = numCols;
     naxes[1] = numRows;
@@ -331,21 +328,28 @@
     }
 
-    int chdu = psFitsGetExtNum(fits);   // Current HDU number
+    // Create the image HDU
     int hdus = psFitsGetSize(fits);     // Number of HDUs in file
-    if (! after) {
-        if (chdu == 0) {
-            // set status to signal fits_insert_img to insert a new primary HDU
-            status = PREPEND_PRIMARY;
-        } else if (hdus > 0) {
-            // move back one to perform an insert after the previous HDU
-            psFitsMoveExtNum(fits, -1, true);
-        }
-    }
-
     if (hdus == 0) {
-        status = 0;
+        // We're creating the first image
         fits_create_img(fits->fd, bitPix, naxis, naxes, &status);
     } else {
+        if (!after) {
+            int chdu = psFitsGetExtNum(fits);   // Current HDU number
+            if (chdu == 0) {
+                // We're creating a replacement primary HDU.
+                // Set status to signal fits_insert_img to insert a new primary HDU
+                status = PREPEND_PRIMARY;
+            } else {
+                // Move back one to perform an insert after the previous HDU
+                psFitsMoveExtNum(fits, -1, true);
+            }
+        }
+        // Insert after the current position
         fits_insert_img(fits->fd, bitPix, naxis, naxes, &status);
+    }
+
+    if (header && !p_psFitsWriteHeader(header, fits)) {
+        psError(PS_ERR_IO, false, "Unable to write FITS header.\n");
+        return false;
     }
 
@@ -361,7 +365,4 @@
     }
 
-    if (header) {
-        psFitsWriteHeader(header, fits);
-    }
     if (input->parent == NULL) { // if no parent, assume that the image data is contiguous
         fits_write_img(fits->fd,
