Index: /branches/rel10_ifa/psLib/src/fits/psFitsImage.c
===================================================================
--- /branches/rel10_ifa/psLib/src/fits/psFitsImage.c	(revision 6513)
+++ /branches/rel10_ifa/psLib/src/fits/psFitsImage.c	(revision 6514)
@@ -7,6 +7,6 @@
  *  @author Robert DeSonia, MHPCC
  *
- *  @version $Revision: 1.1.10.2 $ $Name: not supported by cvs2svn $
- *  @date $Date: 2006-03-04 01:54:42 $
+ *  @version $Revision: 1.1.10.3 $ $Name: not supported by cvs2svn $
+ *  @date $Date: 2006-03-04 02:39:39 $
  *
  *  Copyright 2004-2005 Maui High Performance Computing Center, University of Hawaii
@@ -25,5 +25,5 @@
 #include "psTrace.h"
 #include "psVector.h"
-
+#include "psFitsImage.h"
 #include "psFitsHeader.h"
 
@@ -257,5 +257,5 @@
     }
 
-    output = psImageAlloc(lastPixel[0]-firstPixel[0]+1, lastPixel[1]-firstPixel[1]+1, datatype);
+    psImage *output = psImageAlloc(lastPixel[0]-firstPixel[0]+1, lastPixel[1]-firstPixel[1]+1, datatype);
 
     // n.b., this assumes contiguous image buffer
@@ -278,4 +278,6 @@
     int nAxis = 0;                      // Number of axes
     long nAxes[3];                      // Number of pixels on each axis
+    int status = 0;                     // cfitsio status value
+    char fitsErr[80] = "";              // CFITSIO error message string
 
     if (fits == NULL) {
@@ -327,5 +329,6 @@
                       psMetadata* header,
                       const psImage* input,
-                      int numZPlanes)
+                      int numZPlanes,
+                      const char *extname)
 {
 
@@ -373,4 +376,5 @@
         psMetadataAddF64(header, PS_LIST_TAIL, "BSCALE", PS_META_REPLACE, "Pixel value scale", 1.0);
         psMetadataAddF64(header, PS_LIST_TAIL, "BZERO",  PS_META_REPLACE, "Pixel value offset", bZero);
+        psMetadataAddStr(header, PS_LIST_TAIL, "EXTNAME", PS_META_REPLACE, "Extension name", extname);
     }
 
@@ -381,6 +385,10 @@
         psFitsWriteHeader(header, (psPtr)fits);
     } else {
+        // Write the necessary headers manually
         fits_write_key_dbl(fits->fd, "BZERO", bZero, 12, "Pixel Value Offset", &status);
         fits_write_key_dbl(fits->fd, "BSCALE", 1.0, 12, "Pixel Value Scale", &status);
+        // Regrettably have to cast away "const" on extname
+        fits_write_key_str(fits->fd, "EXTNAME", (char *)extname, "Extension name", &status);
+
     }
 
@@ -438,24 +446,21 @@
     if (input->n == 0) {
         psError(PS_ERR_BAD_PARAMETER_SIZE, true, PS_ERRORTEXT_psFits_ARRAY_EMPTY);
-        return falase;
+        return false;
     }
 
     if (input->n == 1) {
         // The problem reduces to one already solved
-        return psFitsWriteImage(fits, header, input->data[0], extname);
+        return psFitsWriteImage(fits, header, input->data[0], 1, extname);
     }
 
     // Check that all images are of the same size
-    {
-        psImage *image = input->data[0];// First image off the array
-        int numCols = image->numCols;   // Number of columns
-        int numRows = image->numRows;   // Number of rows
-        for (int i = 1; i < input->n; i++)
-        {
-            image = input->data[i];
-            if (image->numCols != numCols || image->numRows != numRows) {
-                psError(PS_ERR_BAD_PARAMETER_SIZE, true, PS_ERRORTEXT_psFits_ARRAY_SIZE_DIFFER);
-                return false;
-            }
+    psImage *testImage = input->data[0];// First image off the array
+    int numCols = testImage->numCols;   // Number of columns
+    int numRows = testImage->numRows;   // Number of rows
+    for (int i = 1; i < input->n; i++) {
+        testImage = input->data[i];
+        if (testImage->numCols != numCols || testImage->numRows != numRows) {
+            psError(PS_ERR_BAD_PARAMETER_SIZE, true, PS_ERRORTEXT_psFits_ARRAY_SIZE_DIFFER);
+            return false;
         }
     }
@@ -472,30 +477,35 @@
                   psMetadataAddS32(headerCopy, PS_LIST_HEAD, "NAXIS2", PS_META_REPLACE, "Number of rows", numRows) &&
                   psMetadataAddS32(headerCopy, PS_LIST_HEAD, "NAXIS3", PS_META_REPLACE, "Number of image planes",
-                                   images->n);
+                                   input->n);
     if (! update) {
-        psError(PS_ERR_UNKNOWN, false, psFits_METADATA_ADD_FAILED, "NAXIS, NAXIS1, NAXIS2, NAXIS3");
+        psError(PS_ERR_UNKNOWN, false, PS_ERRORTEXT_psFits_METADATA_ADD_FAILED,
+                "NAXIS, NAXIS1, NAXIS2, NAXIS3");
         psFree(headerCopy);
         return false;
     }
 
-    // Now we can safely write the images out
-    for (int i = 0; i < images->n; i++) {
-        if (! psFitsWriteImage(fits, headerCopy, images->data[i], i, extname)) {
+    // Now we can safely write the images out.
+    // The first is an psFitsImageWrite to create the extension.
+    // The next are psFitsImageUpdate to write into the extension.
+    if (! psFitsWriteImage(fits, headerCopy, input->data[0], input->n, extname)) {
+        psError(PS_ERR_UNKNOWN, false, PS_ERRORTEXT_psFits_WRITE_PLANE_FAILED, 0);
+        psFree(headerCopy);
+        return false;
+    }
+    psFree(headerCopy);                 // Free, or drop reference
+
+    for (int i = 1; i < input->n; i++) {
+        if (! psFitsUpdateImage(fits, input->data[i], 0, 0, i)) {
             psError(PS_ERR_UNKNOWN, false, PS_ERRORTEXT_psFits_WRITE_PLANE_FAILED, i);
-            psFree(headerCopy);
             return false;
         }
     }
 
-    psFree(headerCopy);                 // Free, or drop reference
-
     return true;
 }
 
-
-
 bool psFitsUpdateImage(psFits* fits,
                        const psImage* input,
-                       psRegion region,
+                       int x0, int y0,
                        int z)
 {
@@ -572,18 +582,10 @@
     long lastPixel[3];
 
-    firstPixel[0] = region.x0 + 1;
-    firstPixel[1] = region.y0 + 1;
+    firstPixel[0] = x0 + 1;
+    firstPixel[1] = y0 + 1;
     firstPixel[2] = z + 1;
 
-    if (region.x1 > 0) {
-        lastPixel[0] = region.x1;
-    } else {
-        lastPixel[0] = nAxes[0] + region.x1; // n.b., region.x1 < 0
-    }
-    if (region.y1 > 0) {
-        lastPixel[1] = region.y1;
-    } else {
-        lastPixel[1] = nAxes[1] + region.y1; // n.b., region.y1 < 0
-    }
+    lastPixel[0] = firstPixel[0] + input->numCols;
+    lastPixel[1] = firstPixel[1] + input->numRows;
     lastPixel[2] = z + 1;
 
@@ -594,5 +596,5 @@
         psError(PS_ERR_BAD_PARAMETER_VALUE, true,
                 "Specified region [%d:%d,%d:%d], is not valid given the %dx%d FITS image.",
-                region.y0,region.y1-1,region.x0,region.x1-1);
+                x0, x0 + input->numCols, y0, y0 + input->numRows, nAxes[0], nAxes[1]);
         return false;
 
@@ -633,2 +635,30 @@
 }
 
+bool psFitsUpdateImageCube(psFits *fits, const psArray *input, int x0, int y0)
+{
+    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;
+    }
+
+    if (input->n == 0) {
+        psError(PS_ERR_BAD_PARAMETER_SIZE, true, PS_ERRORTEXT_psFits_ARRAY_EMPTY);
+        return false;
+    }
+
+    for (int i = 0; i < input->n; i++) {
+        if (! psFitsUpdateImage(fits, input->data[i], x0, y0, i)) {
+            psError(PS_ERR_UNKNOWN, false, PS_ERRORTEXT_psFits_UPDATE_PLANE_FAILED, i);
+            return false;
+        }
+    }
+
+    return true;
+}
