Index: /trunk/psLib/src/fits/psFitsHeader.c
===================================================================
--- /trunk/psLib/src/fits/psFitsHeader.c	(revision 7226)
+++ /trunk/psLib/src/fits/psFitsHeader.c	(revision 7227)
@@ -7,6 +7,6 @@
  *  @author Robert DeSonia, MHPCC
  *
- *  @version $Revision: 1.9 $ $Name: not supported by cvs2svn $
- *  @date $Date: 2006-05-26 02:48:47 $
+ *  @version $Revision: 1.10 $ $Name: not supported by cvs2svn $
+ *  @date $Date: 2006-05-26 03:24:49 $
  *
  *  Copyright 2004-2005 Maui High Performance Computing Center, University of Hawaii
@@ -218,8 +218,11 @@
 }
 
-bool psFitsWriteHeader(psMetadata* output,
-                       psFits* fits)
-{
-
+
+// Do the work of writing out the header.
+// Doesn't check to see if a new HDU needs to be created
+bool p_psFitsWriteHeader(psMetadata *output, // Metadata that is to be output into the FITS file
+                         psFits *fits   // The FITS file handle
+                        )
+{
     if (!fits) {
         psError(PS_ERR_BAD_PARAMETER_NULL, true, PS_ERRORTEXT_psFits_NULL);
@@ -230,19 +233,4 @@
         psError(PS_ERR_BAD_PARAMETER_NULL, true, PS_ERRORTEXT_psFits_METADATA_NULL);
         return false;
-    }
-
-    int status = 0;                     // Status of cfitsio calls
-
-    // Check to see if there are any extant HDUs
-    int hdus = psFitsGetSize(fits);     // Number of HDUs
-    if (hdus == 0) {
-        // Need to create a dummy image HDU for the primary HDU
-        fits_create_img(fits->fd, 16, 0, NULL, &status);
-        if (status) {
-            char fitsErr[MAX_STRING_LENGTH];
-            (void)fits_get_errstatus(status, fitsErr);
-            psError(PS_ERR_IO, true, "Unable to create primary header.\n%s\n", fitsErr);
-            return false;
-        }
     }
 
@@ -250,4 +238,5 @@
     psListIterator* iter = psListIteratorAlloc(output->list, PS_LIST_HEAD, true); // Iterator
     psMetadataItem* item;               // Item from iteration
+    int status = 0;                     // Status of cfitsio calls
     while ((item = psListGetAndIncrement(iter))) {
         // Check to see if the item should be ignored
@@ -325,4 +314,34 @@
 }
 
+bool psFitsWriteHeader(psMetadata* output,
+                       psFits* fits)
+{
+    if (!fits) {
+        psError(PS_ERR_BAD_PARAMETER_NULL, true, PS_ERRORTEXT_psFits_NULL);
+        return false;
+    }
+
+    if (!output) {
+        psError(PS_ERR_BAD_PARAMETER_NULL, true, PS_ERRORTEXT_psFits_METADATA_NULL);
+        return false;
+    }
+
+    // Check to see if there are any extant HDUs
+    int hdus = psFitsGetSize(fits);     // Number of HDUs
+    if (hdus == 0) {
+        // Need to create a dummy image HDU for the primary HDU
+        int status = 0;                 // Status of cfitsio
+        fits_create_img(fits->fd, 16, 0, NULL, &status);
+        if (status) {
+            char fitsErr[MAX_STRING_LENGTH];
+            (void)fits_get_errstatus(status, fitsErr);
+            psError(PS_ERR_IO, true, "Unable to create primary header.\n%s\n", fitsErr);
+            return false;
+        }
+    }
+
+    return p_psFitsWriteHeader(output, fits);
+}
+
 bool psFitsHeaderValidate(psMetadata *header)
 {
Index: /trunk/psLib/src/fits/psFitsHeader.h
===================================================================
--- /trunk/psLib/src/fits/psFitsHeader.h	(revision 7226)
+++ /trunk/psLib/src/fits/psFitsHeader.h	(revision 7227)
@@ -7,6 +7,6 @@
  *  @author Robert DeSonia, MHPCC
  *
- *  @version $Revision: 1.4 $ $Name: not supported by cvs2svn $
- *  @date $Date: 2006-05-22 22:39:07 $
+ *  @version $Revision: 1.5 $ $Name: not supported by cvs2svn $
+ *  @date $Date: 2006-05-26 03:24:49 $
  *
  *  Copyright 2004-2005 Maui High Performance Computing Center, University of Hawaii
@@ -42,4 +42,14 @@
 
 /** Writes the values of the metadata to the current HDU header.
+ *  Doesn't check if the header has to be created, so not for general public use.
+ *
+ * @return bool         if TRUE, the write was successful, otherwise FALSE.
+ */
+bool p_psFitsWriteHeader(
+    psMetadata* output,                 ///< the psMetadata data in which to write
+    psFits* fits                        ///< the psFits object
+);
+
+/** Writes the values of the metadata to an HDU header.
  *
  *  @return bool        if TRUE, the write was successful, otherwise FALSE.
Index: /trunk/psLib/src/fits/psFitsImage.c
===================================================================
--- /trunk/psLib/src/fits/psFitsImage.c	(revision 7226)
+++ /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,
Index: /trunk/psLib/test/fits/tst_psFits.c
===================================================================
--- /trunk/psLib/test/fits/tst_psFits.c	(revision 7226)
+++ /trunk/psLib/test/fits/tst_psFits.c	(revision 7227)
@@ -6,6 +6,6 @@
 *  @author Robert DeSonia, MHPCC
 *
-*  @version $Revision: 1.20 $ $Name: not supported by cvs2svn $
-*  @date $Date: 2006-04-20 01:13:11 $
+*  @version $Revision: 1.21 $ $Name: not supported by cvs2svn $
+*  @date $Date: 2006-05-26 03:24:37 $
 *
 *  Copyright 2004-2005 Maui High Performance Computing Center, University of Hawaii
@@ -119,6 +119,6 @@
 
         // set the pixels in the image
-        psBinaryOp(image,image,"=",psScalarAlloc(lcv,PS_TYPE_F32));
-        if (! psFitsWriteImage(fitsFile,header,image,1,extname) ) {
+        psImageInit(image, lcv);
+        if (! psFitsWriteImage(fitsFile,header,image,0,extname) ) {
             psError(PS_ERR_UNKNOWN, false,
                     "Could not write image.");
