Index: /trunk/psLib/src/dataIO/psFileUtilsErrors.dat
===================================================================
--- /trunk/psLib/src/dataIO/psFileUtilsErrors.dat	(revision 3027)
+++ /trunk/psLib/src/dataIO/psFileUtilsErrors.dat	(revision 3028)
@@ -53,3 +53,5 @@
 psFits_FITS_TYPE_UNSUPPORTED           FITS image type, BITPIX=%d, is not supported.
 psFits_READ_FAILED                     Reading FITS file failed.\nCFITSIO Error: %s
+psFits_IMAGE_UPDATE_TYPE_MISMATCH      Can not update a %s image given a %s image.
+psFits_FITS_Z_SMALL                    Current FITS HDU has %d z-planes, but z-plane %d was specified.
 #
Index: /trunk/psLib/src/dataIO/psFileUtilsErrors.h
===================================================================
--- /trunk/psLib/src/dataIO/psFileUtilsErrors.h	(revision 3027)
+++ /trunk/psLib/src/dataIO/psFileUtilsErrors.h	(revision 3028)
@@ -7,6 +7,6 @@
  *  @author Robert DeSonia, MHPCC
  *
- *  @version $Revision: 1.8 $ $Name: not supported by cvs2svn $
- *  @date $Date: 2005-01-17 20:58:21 $
+ *  @version $Revision: 1.9 $ $Name: not supported by cvs2svn $
+ *  @date $Date: 2005-01-18 03:15:03 $
  *
  *  Copyright 2004 Maui High Performance Computing Center, University of Hawaii
@@ -75,4 +75,6 @@
 #define PS_ERRORTEXT_psFits_FITS_TYPE_UNSUPPORTED "FITS image type, BITPIX=%d, is not supported."
 #define PS_ERRORTEXT_psFits_READ_FAILED "Reading FITS file failed.\nCFITSIO Error: %s"
+#define PS_ERRORTEXT_psFits_IMAGE_UPDATE_TYPE_MISMATCH "Can not update a %s image given a %s image."
+#define PS_ERRORTEXT_psFits_FITS_Z_SMALL "Current FITS HDU has %d z-planes, but z-plane %d was specified."
 //~End
 
Index: /trunk/psLib/src/dataIO/psFits.c
===================================================================
--- /trunk/psLib/src/dataIO/psFits.c	(revision 3027)
+++ /trunk/psLib/src/dataIO/psFits.c	(revision 3028)
@@ -7,6 +7,6 @@
  *  @author Robert DeSonia, MHPCC
  *
- *  @version $Revision: 1.11 $ $Name: not supported by cvs2svn $
- *  @date $Date: 2005-01-17 20:58:21 $
+ *  @version $Revision: 1.12 $ $Name: not supported by cvs2svn $
+ *  @date $Date: 2005-01-18 03:15:03 $
  *
  *  Copyright 2004 Maui High Performance Computing Center, University of Hawaii
@@ -19,4 +19,5 @@
 #include "psError.h"
 #include "psFileUtilsErrors.h"
+#include "psImageExtraction.h"
 #include "psMemory.h"
 #include "psString.h"
@@ -795,4 +796,11 @@
                             datatype);
 
+    if (output == NULL)
+    {
+        psError(PS_ERR_UNKNOWN, false,
+                "Failed to allocate a properly sized image.");
+        return false;
+    }
+
     // n.b., this assumes contiguous image buffer
     if (fits_read_subset(fits->p_fd, fitsDatatype, firstPixel, lastPixel, increment,
@@ -814,5 +822,6 @@
                       const psMetadata* header,
                       const psImage* input,
-                      int numZPlanes)
+                      int numZPlanes,
+                      char* extname)
 {
 
@@ -853,4 +862,8 @@
 
     fits_create_img(fits->p_fd, bitPix, naxis, naxes, &status);
+
+    if (extname != NULL) {
+        fits_update_key_str(fits->p_fd, "EXTNAME", (char*)extname, NULL, &status);
+    }
 
     if (bZero != 0) {        // set the bscale/bzero
@@ -896,4 +909,142 @@
     return true;
 
+}
+
+bool psFitsUpdateImage(psFits* fits,
+                       const psImage* input,
+                       psRegion region,
+                       int z)
+{
+    int status = 0;
+
+    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;
+    }
+
+    // check to see if we are positioned on an image HDU
+    int hdutype;
+    if ( fits_get_hdu_type(fits->p_fd,&hdutype, &status) != 0) {
+        char fitsErr[MAX_STRING_LENGTH];
+        (void)fits_get_errstatus(status, fitsErr);
+        psError(PS_ERR_IO, true,
+                PS_ERRORTEXT_psFits_GET_HDU_TYPE_FAILED,
+                fitsErr);
+        return NULL;
+    }
+    if (hdutype != IMAGE_HDU) {
+        psError(PS_ERR_IO, true,
+                PS_ERRORTEXT_psFits_NOT_IMAGE_TYPE);
+        return NULL;
+    }
+
+    int numCols = input->numCols;
+    int numRows = input->numRows;
+
+    // determine the FITS-equivalent parameters
+    int bitPix;
+    double bZero;
+    int dataType;
+    if (! convertPsTypeToFits(input->type.type, &bitPix, &bZero, &dataType) ) {
+        return false;
+    }
+
+    //check to see if the HDU has the same datatype
+    int fileBitpix;
+    int naxis;
+    long nAxes[3];
+    nAxes[2] = 1;
+    fits_get_img_param(fits->p_fd, 3, &fileBitpix, &naxis, nAxes, &status);
+
+    //check to see if the HDU has the same datatype
+    if (bitPix != fileBitpix) {
+        char* fitsTypeStr;
+        char* imageTypeStr;
+        PS_TYPE_NAME(fitsTypeStr,fileBitpix);
+        PS_TYPE_NAME(imageTypeStr,input->type.type);
+        psError(PS_ERR_IO, true,
+                PS_ERRORTEXT_psFits_IMAGE_UPDATE_TYPE_MISMATCH,
+                fitsTypeStr, imageTypeStr);
+        return false;
+    }
+
+    //check if the HDU has the z-plane requested
+    if (z >= nAxes[2]) {
+        psError(PS_ERR_BAD_PARAMETER_SIZE, true,
+                PS_ERRORTEXT_psFits_FITS_Z_SMALL,
+                nAxes[2],z);
+        return false;
+    }
+
+    // determine the region in the FITS file domain
+    long firstPixel[3];
+    long lastPixel[3];
+
+    firstPixel[0] = region.x0 + 1;
+    firstPixel[1] = region.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[2] = z + 1;
+
+    if (firstPixel[0] < 1 || firstPixel[0] > nAxes[0] ||
+            firstPixel[1] < 1 || firstPixel[1] > nAxes[1] ||
+            lastPixel[0] < 1 || lastPixel[0] > nAxes[0] ||
+            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,0,0,dx+1,dy+1);
+    } else {
+        subset = psMemIncrRefCounter((psImage*)input);
+    }
+
+    fits_write_subset(fits->p_fd, dataType, firstPixel, lastPixel, subset->data.V[0], &status);
+
+    if ( status != 0) {
+        char fitsErr[MAX_STRING_LENGTH];
+        (void)fits_get_errstatus(status, fitsErr);
+        psError(PS_ERR_IO, true,
+                PS_ERRORTEXT_psFits_WRITE_FAILED,
+                fits->filename, fitsErr);
+        return false;
+    }
+
+    return true;
 }
 
Index: /trunk/psLib/src/dataIO/psFits.h
===================================================================
--- /trunk/psLib/src/dataIO/psFits.h	(revision 3027)
+++ /trunk/psLib/src/dataIO/psFits.h	(revision 3028)
@@ -7,6 +7,6 @@
  *  @author Robert DeSonia, MHPCC
  *
- *  @version $Revision: 1.4 $ $Name: not supported by cvs2svn $
- *  @date $Date: 2005-01-12 22:17:01 $
+ *  @version $Revision: 1.5 $ $Name: not supported by cvs2svn $
+ *  @date $Date: 2005-01-18 03:15:03 $
  *
  *  Copyright 2004 Maui High Performance Computing Center, University of Hawaii
@@ -172,7 +172,19 @@
 bool psFitsWriteImage(
     psFits* fits,                      ///< the psFits object
-    const psMetadata* header,
+    const psMetadata* header,          ///< header items for the new HDU.  Can be NULL.
     const psImage* input,              ///< the image to output
-    int depth                          ///< the number of z-planes of the FITS image data cube
+    int depth,                         ///< the number of z-planes of the FITS image data cube
+    char* extname                      ///< extension name
+);
+
+/** Updates the FITS file image, given the desired region and z-plane.
+ *
+ *  @return bool        TRUE is the write was successful, otherwise FALSE.
+ */
+bool psFitsUpdateImage(
+    psFits* fits,                      ///< the psFits object
+    const psImage* input,              ///< the image to output
+    psRegion region,                   ///< the region in the FITS image to write
+    int z                              ///< the z-planes of the FITS image data cube to write
 );
 
@@ -231,8 +243,21 @@
 bool psFitsWriteTable(
     psFits* fits,                      ///< the psFits object
-    psMetadata* header,
+    psMetadata* header,                ///< header items for the new HDU.  Can be NULL.
     psArray* table,
     ///< Array of psMetadata items, which contains the output data items of each row.
-    char* extname
+    char* extname                      ///< extension name
+);
+
+/** Updates a FITS table.  The current HDU type must be either
+ *  PS_FITS_TYPE_BINARY_TABLE or PS_FITS_TYPE_ASCII_TABLE.
+ *
+ *  @return bool        TRUE if the write was successful, otherwise FALSE
+ *
+ *  @see psFitsWriteTable
+ */
+bool psFitsUpdateTable(
+    psFits* fits,                      ///< the psFits object
+    psArray* table
+    ///< Array of psMetadata items, which contains the output data items of each row.
 );
 
Index: /trunk/psLib/src/fileUtils/psFileUtilsErrors.dat
===================================================================
--- /trunk/psLib/src/fileUtils/psFileUtilsErrors.dat	(revision 3027)
+++ /trunk/psLib/src/fileUtils/psFileUtilsErrors.dat	(revision 3028)
@@ -53,3 +53,5 @@
 psFits_FITS_TYPE_UNSUPPORTED           FITS image type, BITPIX=%d, is not supported.
 psFits_READ_FAILED                     Reading FITS file failed.\nCFITSIO Error: %s
+psFits_IMAGE_UPDATE_TYPE_MISMATCH      Can not update a %s image given a %s image.
+psFits_FITS_Z_SMALL                    Current FITS HDU has %d z-planes, but z-plane %d was specified.
 #
Index: /trunk/psLib/src/fileUtils/psFileUtilsErrors.h
===================================================================
--- /trunk/psLib/src/fileUtils/psFileUtilsErrors.h	(revision 3027)
+++ /trunk/psLib/src/fileUtils/psFileUtilsErrors.h	(revision 3028)
@@ -7,6 +7,6 @@
  *  @author Robert DeSonia, MHPCC
  *
- *  @version $Revision: 1.8 $ $Name: not supported by cvs2svn $
- *  @date $Date: 2005-01-17 20:58:21 $
+ *  @version $Revision: 1.9 $ $Name: not supported by cvs2svn $
+ *  @date $Date: 2005-01-18 03:15:03 $
  *
  *  Copyright 2004 Maui High Performance Computing Center, University of Hawaii
@@ -75,4 +75,6 @@
 #define PS_ERRORTEXT_psFits_FITS_TYPE_UNSUPPORTED "FITS image type, BITPIX=%d, is not supported."
 #define PS_ERRORTEXT_psFits_READ_FAILED "Reading FITS file failed.\nCFITSIO Error: %s"
+#define PS_ERRORTEXT_psFits_IMAGE_UPDATE_TYPE_MISMATCH "Can not update a %s image given a %s image."
+#define PS_ERRORTEXT_psFits_FITS_Z_SMALL "Current FITS HDU has %d z-planes, but z-plane %d was specified."
 //~End
 
Index: /trunk/psLib/src/fileUtils/psFits.c
===================================================================
--- /trunk/psLib/src/fileUtils/psFits.c	(revision 3027)
+++ /trunk/psLib/src/fileUtils/psFits.c	(revision 3028)
@@ -7,6 +7,6 @@
  *  @author Robert DeSonia, MHPCC
  *
- *  @version $Revision: 1.11 $ $Name: not supported by cvs2svn $
- *  @date $Date: 2005-01-17 20:58:21 $
+ *  @version $Revision: 1.12 $ $Name: not supported by cvs2svn $
+ *  @date $Date: 2005-01-18 03:15:03 $
  *
  *  Copyright 2004 Maui High Performance Computing Center, University of Hawaii
@@ -19,4 +19,5 @@
 #include "psError.h"
 #include "psFileUtilsErrors.h"
+#include "psImageExtraction.h"
 #include "psMemory.h"
 #include "psString.h"
@@ -795,4 +796,11 @@
                             datatype);
 
+    if (output == NULL)
+    {
+        psError(PS_ERR_UNKNOWN, false,
+                "Failed to allocate a properly sized image.");
+        return false;
+    }
+
     // n.b., this assumes contiguous image buffer
     if (fits_read_subset(fits->p_fd, fitsDatatype, firstPixel, lastPixel, increment,
@@ -814,5 +822,6 @@
                       const psMetadata* header,
                       const psImage* input,
-                      int numZPlanes)
+                      int numZPlanes,
+                      char* extname)
 {
 
@@ -853,4 +862,8 @@
 
     fits_create_img(fits->p_fd, bitPix, naxis, naxes, &status);
+
+    if (extname != NULL) {
+        fits_update_key_str(fits->p_fd, "EXTNAME", (char*)extname, NULL, &status);
+    }
 
     if (bZero != 0) {        // set the bscale/bzero
@@ -896,4 +909,142 @@
     return true;
 
+}
+
+bool psFitsUpdateImage(psFits* fits,
+                       const psImage* input,
+                       psRegion region,
+                       int z)
+{
+    int status = 0;
+
+    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;
+    }
+
+    // check to see if we are positioned on an image HDU
+    int hdutype;
+    if ( fits_get_hdu_type(fits->p_fd,&hdutype, &status) != 0) {
+        char fitsErr[MAX_STRING_LENGTH];
+        (void)fits_get_errstatus(status, fitsErr);
+        psError(PS_ERR_IO, true,
+                PS_ERRORTEXT_psFits_GET_HDU_TYPE_FAILED,
+                fitsErr);
+        return NULL;
+    }
+    if (hdutype != IMAGE_HDU) {
+        psError(PS_ERR_IO, true,
+                PS_ERRORTEXT_psFits_NOT_IMAGE_TYPE);
+        return NULL;
+    }
+
+    int numCols = input->numCols;
+    int numRows = input->numRows;
+
+    // determine the FITS-equivalent parameters
+    int bitPix;
+    double bZero;
+    int dataType;
+    if (! convertPsTypeToFits(input->type.type, &bitPix, &bZero, &dataType) ) {
+        return false;
+    }
+
+    //check to see if the HDU has the same datatype
+    int fileBitpix;
+    int naxis;
+    long nAxes[3];
+    nAxes[2] = 1;
+    fits_get_img_param(fits->p_fd, 3, &fileBitpix, &naxis, nAxes, &status);
+
+    //check to see if the HDU has the same datatype
+    if (bitPix != fileBitpix) {
+        char* fitsTypeStr;
+        char* imageTypeStr;
+        PS_TYPE_NAME(fitsTypeStr,fileBitpix);
+        PS_TYPE_NAME(imageTypeStr,input->type.type);
+        psError(PS_ERR_IO, true,
+                PS_ERRORTEXT_psFits_IMAGE_UPDATE_TYPE_MISMATCH,
+                fitsTypeStr, imageTypeStr);
+        return false;
+    }
+
+    //check if the HDU has the z-plane requested
+    if (z >= nAxes[2]) {
+        psError(PS_ERR_BAD_PARAMETER_SIZE, true,
+                PS_ERRORTEXT_psFits_FITS_Z_SMALL,
+                nAxes[2],z);
+        return false;
+    }
+
+    // determine the region in the FITS file domain
+    long firstPixel[3];
+    long lastPixel[3];
+
+    firstPixel[0] = region.x0 + 1;
+    firstPixel[1] = region.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[2] = z + 1;
+
+    if (firstPixel[0] < 1 || firstPixel[0] > nAxes[0] ||
+            firstPixel[1] < 1 || firstPixel[1] > nAxes[1] ||
+            lastPixel[0] < 1 || lastPixel[0] > nAxes[0] ||
+            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,0,0,dx+1,dy+1);
+    } else {
+        subset = psMemIncrRefCounter((psImage*)input);
+    }
+
+    fits_write_subset(fits->p_fd, dataType, firstPixel, lastPixel, subset->data.V[0], &status);
+
+    if ( status != 0) {
+        char fitsErr[MAX_STRING_LENGTH];
+        (void)fits_get_errstatus(status, fitsErr);
+        psError(PS_ERR_IO, true,
+                PS_ERRORTEXT_psFits_WRITE_FAILED,
+                fits->filename, fitsErr);
+        return false;
+    }
+
+    return true;
 }
 
Index: /trunk/psLib/src/fileUtils/psFits.h
===================================================================
--- /trunk/psLib/src/fileUtils/psFits.h	(revision 3027)
+++ /trunk/psLib/src/fileUtils/psFits.h	(revision 3028)
@@ -7,6 +7,6 @@
  *  @author Robert DeSonia, MHPCC
  *
- *  @version $Revision: 1.4 $ $Name: not supported by cvs2svn $
- *  @date $Date: 2005-01-12 22:17:01 $
+ *  @version $Revision: 1.5 $ $Name: not supported by cvs2svn $
+ *  @date $Date: 2005-01-18 03:15:03 $
  *
  *  Copyright 2004 Maui High Performance Computing Center, University of Hawaii
@@ -172,7 +172,19 @@
 bool psFitsWriteImage(
     psFits* fits,                      ///< the psFits object
-    const psMetadata* header,
+    const psMetadata* header,          ///< header items for the new HDU.  Can be NULL.
     const psImage* input,              ///< the image to output
-    int depth                          ///< the number of z-planes of the FITS image data cube
+    int depth,                         ///< the number of z-planes of the FITS image data cube
+    char* extname                      ///< extension name
+);
+
+/** Updates the FITS file image, given the desired region and z-plane.
+ *
+ *  @return bool        TRUE is the write was successful, otherwise FALSE.
+ */
+bool psFitsUpdateImage(
+    psFits* fits,                      ///< the psFits object
+    const psImage* input,              ///< the image to output
+    psRegion region,                   ///< the region in the FITS image to write
+    int z                              ///< the z-planes of the FITS image data cube to write
 );
 
@@ -231,8 +243,21 @@
 bool psFitsWriteTable(
     psFits* fits,                      ///< the psFits object
-    psMetadata* header,
+    psMetadata* header,                ///< header items for the new HDU.  Can be NULL.
     psArray* table,
     ///< Array of psMetadata items, which contains the output data items of each row.
-    char* extname
+    char* extname                      ///< extension name
+);
+
+/** Updates a FITS table.  The current HDU type must be either
+ *  PS_FITS_TYPE_BINARY_TABLE or PS_FITS_TYPE_ASCII_TABLE.
+ *
+ *  @return bool        TRUE if the write was successful, otherwise FALSE
+ *
+ *  @see psFitsWriteTable
+ */
+bool psFitsUpdateTable(
+    psFits* fits,                      ///< the psFits object
+    psArray* table
+    ///< Array of psMetadata items, which contains the output data items of each row.
 );
 
Index: /trunk/psLib/src/fits/psFits.c
===================================================================
--- /trunk/psLib/src/fits/psFits.c	(revision 3027)
+++ /trunk/psLib/src/fits/psFits.c	(revision 3028)
@@ -7,6 +7,6 @@
  *  @author Robert DeSonia, MHPCC
  *
- *  @version $Revision: 1.11 $ $Name: not supported by cvs2svn $
- *  @date $Date: 2005-01-17 20:58:21 $
+ *  @version $Revision: 1.12 $ $Name: not supported by cvs2svn $
+ *  @date $Date: 2005-01-18 03:15:03 $
  *
  *  Copyright 2004 Maui High Performance Computing Center, University of Hawaii
@@ -19,4 +19,5 @@
 #include "psError.h"
 #include "psFileUtilsErrors.h"
+#include "psImageExtraction.h"
 #include "psMemory.h"
 #include "psString.h"
@@ -795,4 +796,11 @@
                             datatype);
 
+    if (output == NULL)
+    {
+        psError(PS_ERR_UNKNOWN, false,
+                "Failed to allocate a properly sized image.");
+        return false;
+    }
+
     // n.b., this assumes contiguous image buffer
     if (fits_read_subset(fits->p_fd, fitsDatatype, firstPixel, lastPixel, increment,
@@ -814,5 +822,6 @@
                       const psMetadata* header,
                       const psImage* input,
-                      int numZPlanes)
+                      int numZPlanes,
+                      char* extname)
 {
 
@@ -853,4 +862,8 @@
 
     fits_create_img(fits->p_fd, bitPix, naxis, naxes, &status);
+
+    if (extname != NULL) {
+        fits_update_key_str(fits->p_fd, "EXTNAME", (char*)extname, NULL, &status);
+    }
 
     if (bZero != 0) {        // set the bscale/bzero
@@ -896,4 +909,142 @@
     return true;
 
+}
+
+bool psFitsUpdateImage(psFits* fits,
+                       const psImage* input,
+                       psRegion region,
+                       int z)
+{
+    int status = 0;
+
+    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;
+    }
+
+    // check to see if we are positioned on an image HDU
+    int hdutype;
+    if ( fits_get_hdu_type(fits->p_fd,&hdutype, &status) != 0) {
+        char fitsErr[MAX_STRING_LENGTH];
+        (void)fits_get_errstatus(status, fitsErr);
+        psError(PS_ERR_IO, true,
+                PS_ERRORTEXT_psFits_GET_HDU_TYPE_FAILED,
+                fitsErr);
+        return NULL;
+    }
+    if (hdutype != IMAGE_HDU) {
+        psError(PS_ERR_IO, true,
+                PS_ERRORTEXT_psFits_NOT_IMAGE_TYPE);
+        return NULL;
+    }
+
+    int numCols = input->numCols;
+    int numRows = input->numRows;
+
+    // determine the FITS-equivalent parameters
+    int bitPix;
+    double bZero;
+    int dataType;
+    if (! convertPsTypeToFits(input->type.type, &bitPix, &bZero, &dataType) ) {
+        return false;
+    }
+
+    //check to see if the HDU has the same datatype
+    int fileBitpix;
+    int naxis;
+    long nAxes[3];
+    nAxes[2] = 1;
+    fits_get_img_param(fits->p_fd, 3, &fileBitpix, &naxis, nAxes, &status);
+
+    //check to see if the HDU has the same datatype
+    if (bitPix != fileBitpix) {
+        char* fitsTypeStr;
+        char* imageTypeStr;
+        PS_TYPE_NAME(fitsTypeStr,fileBitpix);
+        PS_TYPE_NAME(imageTypeStr,input->type.type);
+        psError(PS_ERR_IO, true,
+                PS_ERRORTEXT_psFits_IMAGE_UPDATE_TYPE_MISMATCH,
+                fitsTypeStr, imageTypeStr);
+        return false;
+    }
+
+    //check if the HDU has the z-plane requested
+    if (z >= nAxes[2]) {
+        psError(PS_ERR_BAD_PARAMETER_SIZE, true,
+                PS_ERRORTEXT_psFits_FITS_Z_SMALL,
+                nAxes[2],z);
+        return false;
+    }
+
+    // determine the region in the FITS file domain
+    long firstPixel[3];
+    long lastPixel[3];
+
+    firstPixel[0] = region.x0 + 1;
+    firstPixel[1] = region.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[2] = z + 1;
+
+    if (firstPixel[0] < 1 || firstPixel[0] > nAxes[0] ||
+            firstPixel[1] < 1 || firstPixel[1] > nAxes[1] ||
+            lastPixel[0] < 1 || lastPixel[0] > nAxes[0] ||
+            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,0,0,dx+1,dy+1);
+    } else {
+        subset = psMemIncrRefCounter((psImage*)input);
+    }
+
+    fits_write_subset(fits->p_fd, dataType, firstPixel, lastPixel, subset->data.V[0], &status);
+
+    if ( status != 0) {
+        char fitsErr[MAX_STRING_LENGTH];
+        (void)fits_get_errstatus(status, fitsErr);
+        psError(PS_ERR_IO, true,
+                PS_ERRORTEXT_psFits_WRITE_FAILED,
+                fits->filename, fitsErr);
+        return false;
+    }
+
+    return true;
 }
 
Index: /trunk/psLib/src/fits/psFits.h
===================================================================
--- /trunk/psLib/src/fits/psFits.h	(revision 3027)
+++ /trunk/psLib/src/fits/psFits.h	(revision 3028)
@@ -7,6 +7,6 @@
  *  @author Robert DeSonia, MHPCC
  *
- *  @version $Revision: 1.4 $ $Name: not supported by cvs2svn $
- *  @date $Date: 2005-01-12 22:17:01 $
+ *  @version $Revision: 1.5 $ $Name: not supported by cvs2svn $
+ *  @date $Date: 2005-01-18 03:15:03 $
  *
  *  Copyright 2004 Maui High Performance Computing Center, University of Hawaii
@@ -172,7 +172,19 @@
 bool psFitsWriteImage(
     psFits* fits,                      ///< the psFits object
-    const psMetadata* header,
+    const psMetadata* header,          ///< header items for the new HDU.  Can be NULL.
     const psImage* input,              ///< the image to output
-    int depth                          ///< the number of z-planes of the FITS image data cube
+    int depth,                         ///< the number of z-planes of the FITS image data cube
+    char* extname                      ///< extension name
+);
+
+/** Updates the FITS file image, given the desired region and z-plane.
+ *
+ *  @return bool        TRUE is the write was successful, otherwise FALSE.
+ */
+bool psFitsUpdateImage(
+    psFits* fits,                      ///< the psFits object
+    const psImage* input,              ///< the image to output
+    psRegion region,                   ///< the region in the FITS image to write
+    int z                              ///< the z-planes of the FITS image data cube to write
 );
 
@@ -231,8 +243,21 @@
 bool psFitsWriteTable(
     psFits* fits,                      ///< the psFits object
-    psMetadata* header,
+    psMetadata* header,                ///< header items for the new HDU.  Can be NULL.
     psArray* table,
     ///< Array of psMetadata items, which contains the output data items of each row.
-    char* extname
+    char* extname                      ///< extension name
+);
+
+/** Updates a FITS table.  The current HDU type must be either
+ *  PS_FITS_TYPE_BINARY_TABLE or PS_FITS_TYPE_ASCII_TABLE.
+ *
+ *  @return bool        TRUE if the write was successful, otherwise FALSE
+ *
+ *  @see psFitsWriteTable
+ */
+bool psFitsUpdateTable(
+    psFits* fits,                      ///< the psFits object
+    psArray* table
+    ///< Array of psMetadata items, which contains the output data items of each row.
 );
 
Index: /trunk/psLib/src/image/psImageManip.c
===================================================================
--- /trunk/psLib/src/image/psImageManip.c	(revision 3027)
+++ /trunk/psLib/src/image/psImageManip.c	(revision 3028)
@@ -10,6 +10,6 @@
  *  @author Ross Harman, MHPCC
  *
- *  @version $Revision: 1.34 $ $Name: not supported by cvs2svn $
- *  @date $Date: 2005-01-03 22:02:35 $
+ *  @version $Revision: 1.35 $ $Name: not supported by cvs2svn $
+ *  @date $Date: 2005-01-18 03:15:03 $
  *
  *  Copyright 2004 Maui High Performance Computing Center, University of Hawaii
@@ -114,10 +114,10 @@
         psImageClipCase(S8)
         psImageClipCase(S16)
-        psImageClipCase(S32)                   // Not a requirement
-        //        psImageClipCase(S64)            Not a requirement
+        psImageClipCase(S32)            // Not a requirement
+        psImageClipCase(S64)            // Not a requirement
         psImageClipCase(U8)
         psImageClipCase(U16)
-        //        psImageClipCase(U32)            Not a requirement
-        //        psImageClipCase(U64)            Not a requirement
+        psImageClipCase(U32)            // Not a requirement
+        psImageClipCase(U64)            // Not a requirement
         psImageClipCase(F32)
         psImageClipCase(F64)
@@ -504,10 +504,10 @@
         //        PS_IMAGE_REBIN_CASE(U8);       Not valid since psVectorStats doesn't allow
         PS_IMAGE_REBIN_CASE(U16);
-        //        PS_IMAGE_REBIN_CASE(U32);      Not a requirement
-        //        PS_IMAGE_REBIN_CASE(U64);      Not a requirement
+        PS_IMAGE_REBIN_CASE(U32);      // Not a requirement
+        PS_IMAGE_REBIN_CASE(U64);      // Not a requirement
         PS_IMAGE_REBIN_CASE(S8);
         //        PS_IMAGE_REBIN_CASE(S16);      Not valid since psVectorStats doesn't allow
-        //        PS_IMAGE_REBIN_CASE(S32);      Not a requirement
-        //        PS_IMAGE_REBIN_CASE(S64);      Not a requirement
+        PS_IMAGE_REBIN_CASE(S32);      // Not a requirement
+        PS_IMAGE_REBIN_CASE(S64);      // Not a requirement
         PS_IMAGE_REBIN_CASE(F32);
         PS_IMAGE_REBIN_CASE(F64);
@@ -1010,10 +1010,10 @@
         PSIMAGE_SHIFT_CASE(MODE,U8);  \
         PSIMAGE_SHIFT_CASE(MODE,U16); \
-        /*        PSIMAGE_SHIFT_CASE(MODE,U32);     Not a requirement */ \
-        /*        PSIMAGE_SHIFT_CASE(MODE,U64);     Not a requirement */ \
+        PSIMAGE_SHIFT_CASE(MODE,U32);     /* Not a requirement */ \
+        PSIMAGE_SHIFT_CASE(MODE,U64);     /* Not a requirement */ \
         PSIMAGE_SHIFT_CASE(MODE,S8);  \
         PSIMAGE_SHIFT_CASE(MODE,S16); \
-        /*        PSIMAGE_SHIFT_CASE(MODE,S32);     Not a requirement */ \
-        /*        PSIMAGE_SHIFT_CASE(MODE,S64);     Not a requirement */ \
+        PSIMAGE_SHIFT_CASE(MODE,S32);    /* Not a requirement */ \
+        PSIMAGE_SHIFT_CASE(MODE,S64);    /* Not a requirement */ \
         PSIMAGE_SHIFT_CASE(MODE,F32); \
         PSIMAGE_SHIFT_CASE(MODE,F64); \
Index: /trunk/psLib/test/dataIO/tst_psFits.c
===================================================================
--- /trunk/psLib/test/dataIO/tst_psFits.c	(revision 3027)
+++ /trunk/psLib/test/dataIO/tst_psFits.c	(revision 3028)
@@ -6,6 +6,6 @@
 *  @author Robert DeSonia, MHPCC
 *
-*  @version $Revision: 1.7 $ $Name: not supported by cvs2svn $
-*  @date $Date: 2005-01-17 20:58:22 $
+*  @version $Revision: 1.8 $ $Name: not supported by cvs2svn $
+*  @date $Date: 2005-01-18 03:15:03 $
 *
 *  Copyright 2004 Maui High Performance Computing Center, University of Hawaii
@@ -17,4 +17,16 @@
 #include <unistd.h>
 #include <string.h>
+#include <sys/stat.h>
+#include <sys/types.h>
+
+
+#define GENIMAGE(img,c,r,TYP, valueFcn) \
+img = psImageAlloc(c,r,PS_TYPE_##TYP); \
+for (psU32 row=0;row<r;row++) { \
+    ps##TYP* imgRow = img->data.TYP[row]; \
+    for (psU32 col=0;col<c;col++) { \
+        imgRow[col] = (ps##TYP)(valueFcn); \
+    } \
+}
 
 static bool makeMulti(void);  // implicitly tests psFitsSetExtName
@@ -23,4 +35,10 @@
 const char* tableFilename = "table.fits";
 const int tableNumRows = 10;
+
+
+// N.B., the tests to Image read/write was liberally taken from the now
+// deprecated psImageReadSection/psImageWriteSection function tests.
+static psS32 testImageRead(void);
+static psS32 testImageWrite(void);
 
 static psS32 tst_psFitsAlloc( void );
@@ -41,4 +59,6 @@
                               {tst_psFitsReadHeaderSet,805, "psFitsReadHeaderSet", 0, false},
                               {tst_psFitsReadTable,809, "psFitsReadTable", 0, false},
+                              {testImageRead,567, "psFitsReadImage", 0, false},
+                              {testImageWrite,569, "psFitsWriteImage", 0, false},
                               {NULL}
                           };
@@ -95,14 +115,9 @@
             // set the pixels in the image
             psBinaryOp(image,image,"=",psScalarAlloc(lcv,PS_TYPE_F32));
-            if (! psFitsWriteImage(fitsFile,header,image,1) ) {
+            if (! psFitsWriteImage(fitsFile,header,image,1, extname) ) {
                 psError(PS_ERR_UNKNOWN, false,
                         "Could not write image.");
                 return false;
             }
-            if (! psFitsSetExtName(fitsFile,extname) ) {
-                psError(PS_ERR_UNKNOWN, false,
-                        "Could not write extension name.");
-                return false;
-            }
 
             psFree(header);
@@ -130,5 +145,5 @@
         psImage* image = psImageAlloc(16,16,PS_TYPE_F32);
 
-        if (! psFitsWriteImage(fitsFile,NULL,image,1) ) {
+        if (! psFitsWriteImage(fitsFile,NULL,image,1,"primary") ) {
             psError(PS_ERR_UNKNOWN, false,
                     "Could not write PHU image.");
@@ -873,2 +888,245 @@
     return 0;
 }
+
+psS32 testImageRead(void)
+{
+    psS32 N = 256;
+    psS32 M = 128;
+
+    /*
+        This function shall open the specified FITS file, read the specified data
+        and place the data into a psImage structure. This function shall generate
+        an error message and return NULL if any of the input parameters are out of
+        range, image file doesn't exist or image is zero or one dimensional.
+
+        Verify the returned psImage structure contains expected values, if the input
+        parameter filename specifies an available FITS file with known 2dimensional
+        data, input parameters col, row, ncol, nrow, z specify data range with the
+        FITS file. Cases should include 1x1, Nx1, 1xN, NxN and MxN sub images and
+        total FITS file image. (done in macro)
+
+        Verify the returned psImage structure is equal to the input parameter
+        'output', if specified. (done in macro)
+
+        */
+
+    /* generate FITS file to read */
+
+    #define testReadTypeSize(m, n, readM0, readN0, readM, readN, TYP, filename) \
+    { \
+        psImage* img = NULL; \
+        psImage* img2 = NULL; \
+        psImage* img3 = NULL; \
+        psImage* img4 = NULL; \
+        /*        psImagimge* img_ref = NULL; */ \
+        \
+        GENIMAGE(img,m,n,TYP,row+2*col); \
+        img2 = psImageCopy(img2,img,PS_TYPE_##TYP); \
+        GENIMAGE(img3,m,n,TYP,row+2*col); \
+        psImageClip(img3,32.0,32.0,120.0,120.0); \
+        img4 = psImageCopy(img4,img3,PS_TYPE_##TYP); \
+        remove(filename); \
+        psFits* fits = psFitsAlloc(filename); \
+        psRegion region = {0,0,0,0}; \
+        if (! psFitsWriteImage(fits, NULL, img, 2, "primary")) { \
+            psError(PS_ERR_UNKNOWN, true,"Failed to write test image %s",filename); \
+            return 1; \
+        } \
+        if (! psFitsUpdateImage(fits,img3, region, 1)) { \
+            psError(PS_ERR_UNKNOWN, true,"Failed to write test image %s",filename); \
+            return 2; \
+        } \
+        if (! psFitsWriteImage(fits,NULL, img3, 2, "extension")) { \
+            psError(PS_ERR_UNKNOWN, true,"Failed to write test image %s",filename); \
+            return 3; \
+        } \
+        if (! psFitsUpdateImage(fits,img,region, 1)) { \
+            psError(PS_ERR_UNKNOWN, true,"Failed to write test image %s",filename); \
+            return 4; \
+        } \
+        psFree(img); \
+        psFree(fits); \
+        img = NULL; \
+        psFree(img3); \
+        img3 = NULL; \
+        fits = psFitsAlloc(filename); \
+        psRegion reg = {readM0, readM, readN0, readN}; \
+        img = psFitsReadImage(img, fits, reg, 0); \
+        img3 = psFitsReadImage(img3, fits, reg, 1); \
+        if (img3 == NULL) { \
+            psError(PS_ERR_UNKNOWN, true,"Failed to read test image %s",filename); \
+            return 6; \
+        } \
+        for (psU32 row = readN0; row < readN; row++) { \
+            ps##TYP* imgRow = img->data.TYP[row-readN0]; \
+            ps##TYP* img2Row = img2->data.TYP[row]; \
+            ps##TYP* img3Row = img3->data.TYP[row-readN0]; \
+            ps##TYP* img4Row = img4->data.TYP[row]; \
+            for (psU32 col = readM0; col < readM; col++) { \
+                if (fabsf(imgRow[col-readM0]-img2Row[col]) > FLT_EPSILON) { \
+                    psError(PS_ERR_UNKNOWN, true,"Image changed in I/O operation at %d,%d,0 (%.2f vs %.2f) for %s", \
+                            col,row,(psF32)imgRow[col-readM0],(psF32)img2Row[col],filename); \
+                    return 7; \
+                } \
+                if (fabsf(img3Row[col-readM0]-img4Row[col]) > FLT_EPSILON) { \
+                    psError(PS_ERR_UNKNOWN, true,"Image changed in I/O operation at %d,%d,1 (%.2f vs %.2f) for %s", \
+                            col,row,(psF32)img3Row[col-readM0],(psF32)img4Row[col],filename); \
+                    return 8; \
+                } \
+            } \
+        } \
+        psFree(img); \
+        img = NULL; \
+        psFree(img3); \
+        img3 = NULL; \
+        psFitsMoveExtNum(fits,1, false); \
+        img3 = psFitsReadImage(img3, fits, reg, 0); \
+        img = psFitsReadImage(img, fits, reg, 1); \
+        if (img == NULL) { \
+            psError(PS_ERR_UNKNOWN, true,"Failed to read test image %s",filename); \
+            return 9; \
+        } \
+        for (psU32 row = readN0; row < readN; row++) { \
+            ps##TYP* imgRow = img->data.TYP[row-readN0]; \
+            ps##TYP* img2Row = img2->data.TYP[row]; \
+            ps##TYP* img3Row = img3->data.TYP[row-readN0]; \
+            ps##TYP* img4Row = img4->data.TYP[row]; \
+            for (psU32 col = readM0; col < readM; col++) { \
+                if (fabsf(imgRow[col-readM0]-img2Row[col]) > FLT_EPSILON) { \
+                    psError(PS_ERR_UNKNOWN, true,"Image changed in I/O operation at %d,%d,0 (%.2f vs %.2f) for %s", \
+                            col,row,(psF32)imgRow[col-readM0],(psF32)img2Row[col],filename); \
+                    return 10; \
+                } \
+                if (fabsf(img3Row[col-readM0]-img4Row[col]) > FLT_EPSILON) { \
+                    psError(PS_ERR_UNKNOWN, true,"Image changed in I/O operation at %d,%d,1 (%.2f vs %.2f) for %s", \
+                            col,row,(psF32)img3Row[col-readM0],(psF32)img4Row[col],filename); \
+                    return 11; \
+                } \
+            } \
+        } \
+        psFree(img); \
+        psFree(img2); \
+        psFree(img3); \
+        psFree(img4); \
+        psFree(fits); \
+    }
+
+    #define testReadType(TYP,filename) \
+    testReadTypeSize(1,1,0,0,0,0,TYP,"tmpImages/1x1_" filename); \
+    testReadTypeSize(M,1,M/4,0,M*3/4,0,TYP,"tmpImages/Mx1_" filename); \
+    testReadTypeSize(1,N,0,N/4,0,N*3/4,TYP,"tmpImages/1xN_" filename); \
+    testReadTypeSize(M,N,M/4,N/4,M*3/4,N*3/4,TYP,"tmpImages/MxN_" filename);
+
+    mkdir("tmpImages",0777);
+
+    testReadTypeSize(1,1,0,0,0,0,U8,"tmpImages/1x1_" "U8.fits");
+    testReadTypeSize(M,1,M/4,0,M*3/4,0,U8,"tmpImages/Mx1_" "U8.fits");
+    testReadTypeSize(1,N,0,N/4,0,N*3/4,U8,"tmpImages/1xN_" "U8.fits");
+    testReadTypeSize(M,N,M/4,N/4,M*3/4,N*3/4,U8,"tmpImages/MxN_" "U8.fits");
+
+
+    testReadType(U8,"U8.fits");
+    testReadType(S8,"S8.fits");   // Not a requirement
+    testReadType(S16,"S16.fits");
+    testReadType(U16,"U16.fits"); // Not a requirement
+    testReadType(S32,"S32.fits");
+    testReadType(U32,"U32.fits"); // Not a requirement
+    testReadType(F32,"F32.fits");
+    testReadType(F64,"F64.fits");
+
+    return 0;
+}
+
+psS32 testImageWrite(void)
+{
+    psImage* img = NULL;
+    psImage* img2 = NULL;
+    psS32 m = 64;
+    psS32 n = 96;
+
+    /*
+    This function shall write the specified section within a psImage structure
+    to a FITS file. If the specifiedfile exists, then data should overwrite the
+    section to write. If the specified file doesn't exist, it shall be created.
+    If an extenstion is specified, then a basic primary header data unit shall
+    be created.
+    */
+
+    /*
+    Verify a FITS file named filename is generated and contains expected
+    values, if the input parameter input contains known data values, input
+    parameters col, row, ncol, nrow specify a valid data region within psImage
+    structure.
+
+    Verify a FITS file named filename is generated and contains a primary
+    header data unit with extension with expected values, if the input
+    parameter input contains known data values, input parameters col, row,
+    ncol, nrow specify a valid data region within psImage structure and
+    extname and/or extnum specify an extenstion to write.
+
+    N.B. : these are done in testImageRead tests, see above.
+    */
+
+    /*
+    Verify a FITS file named filename is overwritten and contains
+    expected values, if the input parameter input contains known data values,
+    input parameters col, row, ncol, nrow specify a valid data region within
+    psImage structure.
+    */
+
+    GENIMAGE(img,m,n,F32,0);
+    GENIMAGE(img2,m,n,F32,row+2*col);
+    mkdir("tmpImages",0777);
+    remove
+        ("tmpImages/writeTest.fits");
+    psRegion region = {
+                          0,0,0,0
+                      };
+    psFits* fits = psFitsAlloc("tmpImages/writeTest.fits");
+
+    if (! psFitsWriteImage(fits, NULL, img,1,NULL)) {
+        psError(PS_ERR_UNKNOWN, true,"Couldn't write writeTest.fits.");
+        return 14;
+    }
+    if (! psFitsUpdateImage(fits, img2, region, 0)) {
+        psError(PS_ERR_UNKNOWN, true,"Couldn't update writeTest.fits.");
+        return 15;
+    }
+    psFree(img);
+    psFree(img2);
+
+    // Did it really overwrite the pixel values?  Let's read it in and see.
+    psFree(fits);
+    fits = psFitsAlloc("tmpImages/writeTest.fits");
+    img = psFitsReadImage(NULL, fits, region, 0);
+    if (img == NULL) {
+        psError(PS_ERR_UNKNOWN, true,"Could not read in writeTest.fits.");
+        return 16;
+    }
+    for (psU32 row=0;row<n;row++) {
+        psF32* imgRow = img->data.F32[row];
+        for (psU32 col=0;col<m;col++) {
+            if (fabsf(imgRow[col] - (row+2*col)) > FLT_EPSILON) {
+                psError(PS_ERR_UNKNOWN, true,"The image values were not overwritten at %d,%d (%.2f vs %.2f)",
+                        col,row,imgRow[col],(row+2*col));
+                return 17;
+            }
+        }
+    }
+
+    psFree(img);
+
+    /*
+    Verify false is returned and program execution is not stopped, if the input image
+    is null.
+    */
+    psLogMsg(__func__,PS_LOG_INFO,"Following should generate an error message because input image is null.");
+    if ( psFitsWriteImage(fits,NULL,NULL, 1, NULL) ) {
+        psError(PS_ERR_UNKNOWN, true,"psImageWriteSection did not return false when input image is NULL.");
+        return 20;
+    }
+
+    psFree(fits);
+
+    return 0;
+}
Index: /trunk/psLib/test/dataIO/verified/tst_psFits.stderr
===================================================================
--- /trunk/psLib/test/dataIO/verified/tst_psFits.stderr	(revision 3027)
+++ /trunk/psLib/test/dataIO/verified/tst_psFits.stderr	(revision 3028)
@@ -102,2 +102,24 @@
 ---> TESTPOINT PASSED (psImage{psFitsReadTable} | tst_psFits.c)
 
+/***************************** TESTPOINT ******************************************\
+*             TestFile: tst_psFits.c                                               *
+*            TestPoint: psImage{psFitsReadImage}                                   *
+*             TestType: Positive                                                   *
+\**********************************************************************************/
+
+
+---> TESTPOINT PASSED (psImage{psFitsReadImage} | tst_psFits.c)
+
+/***************************** TESTPOINT ******************************************\
+*             TestFile: tst_psFits.c                                               *
+*            TestPoint: psImage{psFitsWriteImage}                                  *
+*             TestType: Positive                                                   *
+\**********************************************************************************/
+
+<DATE><TIME>|<HOST>|I|testImageWrite
+    Following should generate an error message because input image is null.
+<DATE><TIME>|<HOST>|E|psFitsWriteImage (psFits.c:<LINENO>)
+    The input psImage was NULL.  Need a non-NULL psImage for operation to be performed.
+
+---> TESTPOINT PASSED (psImage{psFitsWriteImage} | tst_psFits.c)
+
Index: /trunk/psLib/test/fileUtils/tst_psFits.c
===================================================================
--- /trunk/psLib/test/fileUtils/tst_psFits.c	(revision 3027)
+++ /trunk/psLib/test/fileUtils/tst_psFits.c	(revision 3028)
@@ -6,6 +6,6 @@
 *  @author Robert DeSonia, MHPCC
 *
-*  @version $Revision: 1.7 $ $Name: not supported by cvs2svn $
-*  @date $Date: 2005-01-17 20:58:22 $
+*  @version $Revision: 1.8 $ $Name: not supported by cvs2svn $
+*  @date $Date: 2005-01-18 03:15:03 $
 *
 *  Copyright 2004 Maui High Performance Computing Center, University of Hawaii
@@ -17,4 +17,16 @@
 #include <unistd.h>
 #include <string.h>
+#include <sys/stat.h>
+#include <sys/types.h>
+
+
+#define GENIMAGE(img,c,r,TYP, valueFcn) \
+img = psImageAlloc(c,r,PS_TYPE_##TYP); \
+for (psU32 row=0;row<r;row++) { \
+    ps##TYP* imgRow = img->data.TYP[row]; \
+    for (psU32 col=0;col<c;col++) { \
+        imgRow[col] = (ps##TYP)(valueFcn); \
+    } \
+}
 
 static bool makeMulti(void);  // implicitly tests psFitsSetExtName
@@ -23,4 +35,10 @@
 const char* tableFilename = "table.fits";
 const int tableNumRows = 10;
+
+
+// N.B., the tests to Image read/write was liberally taken from the now
+// deprecated psImageReadSection/psImageWriteSection function tests.
+static psS32 testImageRead(void);
+static psS32 testImageWrite(void);
 
 static psS32 tst_psFitsAlloc( void );
@@ -41,4 +59,6 @@
                               {tst_psFitsReadHeaderSet,805, "psFitsReadHeaderSet", 0, false},
                               {tst_psFitsReadTable,809, "psFitsReadTable", 0, false},
+                              {testImageRead,567, "psFitsReadImage", 0, false},
+                              {testImageWrite,569, "psFitsWriteImage", 0, false},
                               {NULL}
                           };
@@ -95,14 +115,9 @@
             // set the pixels in the image
             psBinaryOp(image,image,"=",psScalarAlloc(lcv,PS_TYPE_F32));
-            if (! psFitsWriteImage(fitsFile,header,image,1) ) {
+            if (! psFitsWriteImage(fitsFile,header,image,1, extname) ) {
                 psError(PS_ERR_UNKNOWN, false,
                         "Could not write image.");
                 return false;
             }
-            if (! psFitsSetExtName(fitsFile,extname) ) {
-                psError(PS_ERR_UNKNOWN, false,
-                        "Could not write extension name.");
-                return false;
-            }
 
             psFree(header);
@@ -130,5 +145,5 @@
         psImage* image = psImageAlloc(16,16,PS_TYPE_F32);
 
-        if (! psFitsWriteImage(fitsFile,NULL,image,1) ) {
+        if (! psFitsWriteImage(fitsFile,NULL,image,1,"primary") ) {
             psError(PS_ERR_UNKNOWN, false,
                     "Could not write PHU image.");
@@ -873,2 +888,245 @@
     return 0;
 }
+
+psS32 testImageRead(void)
+{
+    psS32 N = 256;
+    psS32 M = 128;
+
+    /*
+        This function shall open the specified FITS file, read the specified data
+        and place the data into a psImage structure. This function shall generate
+        an error message and return NULL if any of the input parameters are out of
+        range, image file doesn't exist or image is zero or one dimensional.
+
+        Verify the returned psImage structure contains expected values, if the input
+        parameter filename specifies an available FITS file with known 2dimensional
+        data, input parameters col, row, ncol, nrow, z specify data range with the
+        FITS file. Cases should include 1x1, Nx1, 1xN, NxN and MxN sub images and
+        total FITS file image. (done in macro)
+
+        Verify the returned psImage structure is equal to the input parameter
+        'output', if specified. (done in macro)
+
+        */
+
+    /* generate FITS file to read */
+
+    #define testReadTypeSize(m, n, readM0, readN0, readM, readN, TYP, filename) \
+    { \
+        psImage* img = NULL; \
+        psImage* img2 = NULL; \
+        psImage* img3 = NULL; \
+        psImage* img4 = NULL; \
+        /*        psImagimge* img_ref = NULL; */ \
+        \
+        GENIMAGE(img,m,n,TYP,row+2*col); \
+        img2 = psImageCopy(img2,img,PS_TYPE_##TYP); \
+        GENIMAGE(img3,m,n,TYP,row+2*col); \
+        psImageClip(img3,32.0,32.0,120.0,120.0); \
+        img4 = psImageCopy(img4,img3,PS_TYPE_##TYP); \
+        remove(filename); \
+        psFits* fits = psFitsAlloc(filename); \
+        psRegion region = {0,0,0,0}; \
+        if (! psFitsWriteImage(fits, NULL, img, 2, "primary")) { \
+            psError(PS_ERR_UNKNOWN, true,"Failed to write test image %s",filename); \
+            return 1; \
+        } \
+        if (! psFitsUpdateImage(fits,img3, region, 1)) { \
+            psError(PS_ERR_UNKNOWN, true,"Failed to write test image %s",filename); \
+            return 2; \
+        } \
+        if (! psFitsWriteImage(fits,NULL, img3, 2, "extension")) { \
+            psError(PS_ERR_UNKNOWN, true,"Failed to write test image %s",filename); \
+            return 3; \
+        } \
+        if (! psFitsUpdateImage(fits,img,region, 1)) { \
+            psError(PS_ERR_UNKNOWN, true,"Failed to write test image %s",filename); \
+            return 4; \
+        } \
+        psFree(img); \
+        psFree(fits); \
+        img = NULL; \
+        psFree(img3); \
+        img3 = NULL; \
+        fits = psFitsAlloc(filename); \
+        psRegion reg = {readM0, readM, readN0, readN}; \
+        img = psFitsReadImage(img, fits, reg, 0); \
+        img3 = psFitsReadImage(img3, fits, reg, 1); \
+        if (img3 == NULL) { \
+            psError(PS_ERR_UNKNOWN, true,"Failed to read test image %s",filename); \
+            return 6; \
+        } \
+        for (psU32 row = readN0; row < readN; row++) { \
+            ps##TYP* imgRow = img->data.TYP[row-readN0]; \
+            ps##TYP* img2Row = img2->data.TYP[row]; \
+            ps##TYP* img3Row = img3->data.TYP[row-readN0]; \
+            ps##TYP* img4Row = img4->data.TYP[row]; \
+            for (psU32 col = readM0; col < readM; col++) { \
+                if (fabsf(imgRow[col-readM0]-img2Row[col]) > FLT_EPSILON) { \
+                    psError(PS_ERR_UNKNOWN, true,"Image changed in I/O operation at %d,%d,0 (%.2f vs %.2f) for %s", \
+                            col,row,(psF32)imgRow[col-readM0],(psF32)img2Row[col],filename); \
+                    return 7; \
+                } \
+                if (fabsf(img3Row[col-readM0]-img4Row[col]) > FLT_EPSILON) { \
+                    psError(PS_ERR_UNKNOWN, true,"Image changed in I/O operation at %d,%d,1 (%.2f vs %.2f) for %s", \
+                            col,row,(psF32)img3Row[col-readM0],(psF32)img4Row[col],filename); \
+                    return 8; \
+                } \
+            } \
+        } \
+        psFree(img); \
+        img = NULL; \
+        psFree(img3); \
+        img3 = NULL; \
+        psFitsMoveExtNum(fits,1, false); \
+        img3 = psFitsReadImage(img3, fits, reg, 0); \
+        img = psFitsReadImage(img, fits, reg, 1); \
+        if (img == NULL) { \
+            psError(PS_ERR_UNKNOWN, true,"Failed to read test image %s",filename); \
+            return 9; \
+        } \
+        for (psU32 row = readN0; row < readN; row++) { \
+            ps##TYP* imgRow = img->data.TYP[row-readN0]; \
+            ps##TYP* img2Row = img2->data.TYP[row]; \
+            ps##TYP* img3Row = img3->data.TYP[row-readN0]; \
+            ps##TYP* img4Row = img4->data.TYP[row]; \
+            for (psU32 col = readM0; col < readM; col++) { \
+                if (fabsf(imgRow[col-readM0]-img2Row[col]) > FLT_EPSILON) { \
+                    psError(PS_ERR_UNKNOWN, true,"Image changed in I/O operation at %d,%d,0 (%.2f vs %.2f) for %s", \
+                            col,row,(psF32)imgRow[col-readM0],(psF32)img2Row[col],filename); \
+                    return 10; \
+                } \
+                if (fabsf(img3Row[col-readM0]-img4Row[col]) > FLT_EPSILON) { \
+                    psError(PS_ERR_UNKNOWN, true,"Image changed in I/O operation at %d,%d,1 (%.2f vs %.2f) for %s", \
+                            col,row,(psF32)img3Row[col-readM0],(psF32)img4Row[col],filename); \
+                    return 11; \
+                } \
+            } \
+        } \
+        psFree(img); \
+        psFree(img2); \
+        psFree(img3); \
+        psFree(img4); \
+        psFree(fits); \
+    }
+
+    #define testReadType(TYP,filename) \
+    testReadTypeSize(1,1,0,0,0,0,TYP,"tmpImages/1x1_" filename); \
+    testReadTypeSize(M,1,M/4,0,M*3/4,0,TYP,"tmpImages/Mx1_" filename); \
+    testReadTypeSize(1,N,0,N/4,0,N*3/4,TYP,"tmpImages/1xN_" filename); \
+    testReadTypeSize(M,N,M/4,N/4,M*3/4,N*3/4,TYP,"tmpImages/MxN_" filename);
+
+    mkdir("tmpImages",0777);
+
+    testReadTypeSize(1,1,0,0,0,0,U8,"tmpImages/1x1_" "U8.fits");
+    testReadTypeSize(M,1,M/4,0,M*3/4,0,U8,"tmpImages/Mx1_" "U8.fits");
+    testReadTypeSize(1,N,0,N/4,0,N*3/4,U8,"tmpImages/1xN_" "U8.fits");
+    testReadTypeSize(M,N,M/4,N/4,M*3/4,N*3/4,U8,"tmpImages/MxN_" "U8.fits");
+
+
+    testReadType(U8,"U8.fits");
+    testReadType(S8,"S8.fits");   // Not a requirement
+    testReadType(S16,"S16.fits");
+    testReadType(U16,"U16.fits"); // Not a requirement
+    testReadType(S32,"S32.fits");
+    testReadType(U32,"U32.fits"); // Not a requirement
+    testReadType(F32,"F32.fits");
+    testReadType(F64,"F64.fits");
+
+    return 0;
+}
+
+psS32 testImageWrite(void)
+{
+    psImage* img = NULL;
+    psImage* img2 = NULL;
+    psS32 m = 64;
+    psS32 n = 96;
+
+    /*
+    This function shall write the specified section within a psImage structure
+    to a FITS file. If the specifiedfile exists, then data should overwrite the
+    section to write. If the specified file doesn't exist, it shall be created.
+    If an extenstion is specified, then a basic primary header data unit shall
+    be created.
+    */
+
+    /*
+    Verify a FITS file named filename is generated and contains expected
+    values, if the input parameter input contains known data values, input
+    parameters col, row, ncol, nrow specify a valid data region within psImage
+    structure.
+
+    Verify a FITS file named filename is generated and contains a primary
+    header data unit with extension with expected values, if the input
+    parameter input contains known data values, input parameters col, row,
+    ncol, nrow specify a valid data region within psImage structure and
+    extname and/or extnum specify an extenstion to write.
+
+    N.B. : these are done in testImageRead tests, see above.
+    */
+
+    /*
+    Verify a FITS file named filename is overwritten and contains
+    expected values, if the input parameter input contains known data values,
+    input parameters col, row, ncol, nrow specify a valid data region within
+    psImage structure.
+    */
+
+    GENIMAGE(img,m,n,F32,0);
+    GENIMAGE(img2,m,n,F32,row+2*col);
+    mkdir("tmpImages",0777);
+    remove
+        ("tmpImages/writeTest.fits");
+    psRegion region = {
+                          0,0,0,0
+                      };
+    psFits* fits = psFitsAlloc("tmpImages/writeTest.fits");
+
+    if (! psFitsWriteImage(fits, NULL, img,1,NULL)) {
+        psError(PS_ERR_UNKNOWN, true,"Couldn't write writeTest.fits.");
+        return 14;
+    }
+    if (! psFitsUpdateImage(fits, img2, region, 0)) {
+        psError(PS_ERR_UNKNOWN, true,"Couldn't update writeTest.fits.");
+        return 15;
+    }
+    psFree(img);
+    psFree(img2);
+
+    // Did it really overwrite the pixel values?  Let's read it in and see.
+    psFree(fits);
+    fits = psFitsAlloc("tmpImages/writeTest.fits");
+    img = psFitsReadImage(NULL, fits, region, 0);
+    if (img == NULL) {
+        psError(PS_ERR_UNKNOWN, true,"Could not read in writeTest.fits.");
+        return 16;
+    }
+    for (psU32 row=0;row<n;row++) {
+        psF32* imgRow = img->data.F32[row];
+        for (psU32 col=0;col<m;col++) {
+            if (fabsf(imgRow[col] - (row+2*col)) > FLT_EPSILON) {
+                psError(PS_ERR_UNKNOWN, true,"The image values were not overwritten at %d,%d (%.2f vs %.2f)",
+                        col,row,imgRow[col],(row+2*col));
+                return 17;
+            }
+        }
+    }
+
+    psFree(img);
+
+    /*
+    Verify false is returned and program execution is not stopped, if the input image
+    is null.
+    */
+    psLogMsg(__func__,PS_LOG_INFO,"Following should generate an error message because input image is null.");
+    if ( psFitsWriteImage(fits,NULL,NULL, 1, NULL) ) {
+        psError(PS_ERR_UNKNOWN, true,"psImageWriteSection did not return false when input image is NULL.");
+        return 20;
+    }
+
+    psFree(fits);
+
+    return 0;
+}
Index: /trunk/psLib/test/fileUtils/verified/tst_psFits.stderr
===================================================================
--- /trunk/psLib/test/fileUtils/verified/tst_psFits.stderr	(revision 3027)
+++ /trunk/psLib/test/fileUtils/verified/tst_psFits.stderr	(revision 3028)
@@ -102,2 +102,24 @@
 ---> TESTPOINT PASSED (psImage{psFitsReadTable} | tst_psFits.c)
 
+/***************************** TESTPOINT ******************************************\
+*             TestFile: tst_psFits.c                                               *
+*            TestPoint: psImage{psFitsReadImage}                                   *
+*             TestType: Positive                                                   *
+\**********************************************************************************/
+
+
+---> TESTPOINT PASSED (psImage{psFitsReadImage} | tst_psFits.c)
+
+/***************************** TESTPOINT ******************************************\
+*             TestFile: tst_psFits.c                                               *
+*            TestPoint: psImage{psFitsWriteImage}                                  *
+*             TestType: Positive                                                   *
+\**********************************************************************************/
+
+<DATE><TIME>|<HOST>|I|testImageWrite
+    Following should generate an error message because input image is null.
+<DATE><TIME>|<HOST>|E|psFitsWriteImage (psFits.c:<LINENO>)
+    The input psImage was NULL.  Need a non-NULL psImage for operation to be performed.
+
+---> TESTPOINT PASSED (psImage{psFitsWriteImage} | tst_psFits.c)
+
Index: /trunk/psLib/test/image/tst_psImageManip.c
===================================================================
--- /trunk/psLib/test/image/tst_psImageManip.c	(revision 3027)
+++ /trunk/psLib/test/image/tst_psImageManip.c	(revision 3028)
@@ -6,6 +6,6 @@
  *  @author Robert DeSonia, MHPCC
  *
- *  @version $Revision: 1.35 $ $Name: not supported by cvs2svn $
- *  @date $Date: 2005-01-03 21:58:53 $
+ *  @version $Revision: 1.36 $ $Name: not supported by cvs2svn $
+ *  @date $Date: 2005-01-18 03:15:04 $
  *
  *  Copyright 2004 Maui High Performance Computing Center, University of Hawaii
@@ -210,15 +210,4 @@
         return 4;
     }
-
-    // Verify program execution doesn't stop if the input image type is something
-    // other than U8, U16, S8, S16, F32, F64, C32, C64.
-    img = psImageAlloc(c,r,PS_TYPE_U32);
-    psLogMsg(__func__,PS_LOG_INFO,"Following should be an error (invalid type)");
-    retVal = psImageClip(img,min,-1.0f,max,-2.0f);
-    if (retVal != 0) {
-        psError(PS_ERR_UNKNOWN, true,"Expected zero return for clip of invalid image type.");
-        return 5;
-    }
-    psFree(img);
 
     return 0;
Index: /trunk/psLib/test/image/verified/tst_psImageManip.stderr
===================================================================
--- /trunk/psLib/test/image/verified/tst_psImageManip.stderr	(revision 3027)
+++ /trunk/psLib/test/image/verified/tst_psImageManip.stderr	(revision 3028)
@@ -29,8 +29,4 @@
 <DATE><TIME>|<HOST>|E|psImageClip (psImageManip.c:<LINENO>)
     Specified min value, 256, can not be greater than the specified max value, 128.
-<DATE><TIME>|<HOST>|I|testImageClip
-    Following should be an error (invalid type)
-<DATE><TIME>|<HOST>|E|psImageClip (psImageManip.c:<LINENO>)
-    Specified psImage type, psU32, is not supported.
 
 ---> TESTPOINT PASSED (psImage{psImageClip} | tst_psImageManip.c)
