Index: trunk/psLib/src/fits/psFitsImage.c
===================================================================
--- trunk/psLib/src/fits/psFitsImage.c	(revision 5511)
+++ trunk/psLib/src/fits/psFitsImage.c	(revision 6767)
@@ -7,6 +7,6 @@
  *  @author Robert DeSonia, MHPCC
  *
- *  @version $Revision: 1.1 $ $Name: not supported by cvs2svn $
- *  @date $Date: 2005-11-14 22:18:30 $
+ *  @version $Revision: 1.2 $ $Name: not supported by cvs2svn $
+ *  @date $Date: 2006-04-04 19:52:42 $
  *
  *  Copyright 2004-2005 Maui High Performance Computing Center, University of Hawaii
@@ -16,4 +16,5 @@
 
 #include "psFits.h"
+#include "psFitsImage.h"
 #include "string.h"
 #include "psError.h"
@@ -292,5 +293,19 @@
                       psMetadata* header,
                       const psImage* input,
-                      int numZPlanes)
+                      int numZPlanes,
+                      const char* extname)
+{
+    // this is equivalent to insert after the last HDU
+
+    psFitsMoveLast(fits);
+    return psFitsInsertImage(fits,header,input,numZPlanes,extname,true);
+}
+
+bool psFitsInsertImage(psFits* fits,
+                       psMetadata* header,
+                       const psImage* input,
+                       int numZPlanes,
+                       const char* extname,
+                       bool after)
 {
 
@@ -330,5 +345,23 @@
     }
 
-    fits_create_img(fits->fd, bitPix, naxis, naxes, &status);
+    int chdu = psFitsGetExtNum(fits);
+    int hdus = psFitsGetSize(fits);
+    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;
+        fits_create_img(fits->fd, bitPix, naxis, naxes, &status);
+
+    } else {
+        fits_insert_img(fits->fd, bitPix, naxis, naxes, &status);
+    }
 
     if (bZero != 0) {        // set the bscale/bzero
@@ -339,6 +372,17 @@
 
     // write the header, if any.
+    if (extname != NULL) {
+        psFitsSetExtName(fits,extname);
+    }
+
     if (header != NULL) {
-        psFitsWriteHeader(header, (psPtr)fits);
+        if (extname != NULL) {
+            psMetadataAddStr(header,PS_LIST_TAIL,"EXTNAME",
+                             PS_META_REPLACE,
+                             "Extenion Name",
+                             extname);
+        }
+
+        psFitsWriteHeader(header, fits);
     }
 
@@ -378,5 +422,6 @@
 bool psFitsUpdateImage(psFits* fits,
                        const psImage* input,
-                       psRegion region,
+                       int x0,
+                       int y0,
                        int z)
 {
@@ -453,18 +498,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] = x0 + numCols;
+    lastPixel[1] = y0 + numRows;
     lastPixel[2] = z + 1;
 
@@ -474,31 +511,12 @@
             lastPixel[1] < 1 || lastPixel[1] > nAxes[1]) {
         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);
-        return false;
-
-    }
-
-    int dx = lastPixel[0] - firstPixel[0];
-    int dy = lastPixel[1] - firstPixel[1];
-    if (dx > numCols ||
-            dy > numRows) {
-        psError(PS_ERR_BAD_PARAMETER_VALUE, true,
-                "The region [%d:%d,%d:%d], is not valid given the input %dx%d image.",
-                firstPixel[1]-1,lastPixel[1]-1,
-                firstPixel[0]-1,lastPixel[0]-1,
-                numCols, numRows);
-        return false;
-    }
-
-    psImage* subset;
-    if (dx != numCols || dy != numRows) {
-        // the input image needs to be subsetted
-        subset = psImageSubset((psImage*)input, psRegionSet(0,dx+1,0,dy+1));
-    } else {
-        subset = psMemIncrRefCounter((psImage*)input);
-    }
-
-    fits_write_subset(fits->fd, dataType, firstPixel, lastPixel, subset->data.V[0], &status);
+                "Input image [size of %ix%i] at position (%i,%i) does not all lay in the %ix%i FITS image.",
+                numCols, numRows,
+                x0, y0,
+                nAxes[0], nAxes[1]);
+        return false;
+    }
+
+    fits_write_subset(fits->fd, dataType, firstPixel, lastPixel, input->data.V[0], &status);
 
     if ( status != 0) {
