Index: /trunk/psLib/src/image/psImage.c
===================================================================
--- /trunk/psLib/src/image/psImage.c	(revision 776)
+++ /trunk/psLib/src/image/psImage.c	(revision 777)
@@ -9,6 +9,6 @@
  *  @author Ross Harman, MHPCC
  *
- *  @version $Revision: 1.15 $ $Name: not supported by cvs2svn $
- *  @date $Date: 2004-05-20 01:49:47 $
+ *  @version $Revision: 1.16 $ $Name: not supported by cvs2svn $
+ *  @date $Date: 2004-05-25 20:28:46 $
  *
  *  Copyright 2004 Maui High Performance Computing Center, University of Hawaii
@@ -18,9 +18,11 @@
 /*  INCLUDE FILES                                                             */
 /******************************************************************************/
+#include <string.h>
+#include <fitsio.h>
+
 #include "psMemory.h"
 #include "psError.h"
 #include "psImage.h"
 
-#include <string.h>
 
 /*****************************************************************************/
@@ -355,5 +357,155 @@
                             int numRows, int z, char* extname, int extnum, char* filename)
 {
-
-    return NULL;
-}
+    fitsfile    *fptr=NULL;                 /*Pointer to the FITS file*/
+    int         status=0;                   /*CFITSIO file vars*/
+    int         nAxis=0;
+    int         anynull=0;
+    int         bitPix=0;                   /*Pixel type*/
+    long        nAxes[3];
+    long        firstPixel[3];              /* lower-left corner of image subset */
+    long        lastPixel[3];               /* upper-right corner of image subset */
+    long        increment[3];               /* increment for image subset */
+    char        fitsErr[80] = "";           /*CFITSIO error message string */
+    int         hduType = IMAGE_HDU;
+
+    /* Open the FITS file */
+    (void)fits_open_file(&fptr, filename, READONLY, &status);
+    if (fptr == NULL || status != 0) {
+        fits_get_errstatus(status,fitsErr);
+        psError(__func__,"Could not open file '%s'. FITS error:%s",
+                filename, fitsErr);
+        psImageFree(output);
+        return NULL;
+    }
+
+    /* find the specified extension */
+    if (extname != NULL) {
+        if (fits_movnam_hdu(fptr, hduType, extname, 0,&status) != 0) {
+            fits_get_errstatus(status, fitsErr);
+            status = 0;
+            (void)fits_close_file(fptr, &status);
+            psError(__func__,"Could not index to '%s' HDU for file %s. FITSIO ERROR:%s",
+                    extname, filename, fitsErr);
+            psImageFree(output);
+            return NULL;
+        }
+    } else {
+        if (fits_movabs_hdu(fptr, extnum, &hduType, &status) != 0) {
+            fits_get_errstatus(status, fitsErr);
+            status = 0;
+            (void)fits_close_file(fptr, &status);
+            psError(__func__,"Could not index to HDU #%d for file %s. FITSIO ERROR:%s",
+                    extnum, filename, fitsErr);
+            psImageFree(output);
+            return NULL;
+        }
+    }
+
+    /* Get the data type 'bitPix' from the FITS image */
+    if (fits_get_img_type(fptr, &bitPix, &status) != 0) {
+        fits_get_errstatus(status, fitsErr);
+        status = 0;
+        (void)fits_close_file(fptr, &status);
+        psError("Could not determine image data type of '%s'. FITSIO ERROR:%s",
+                filename, fitsErr);
+        psImageFree(output);
+        return NULL;
+    }
+
+    /* Get the dimensions 'nAxis' from the FITS image */
+    if (fits_get_img_dim(fptr, &nAxis, &status) != 0) {
+        (void)fits_get_errstatus(status, fitsErr);
+        status = 0;
+        (void)fits_close_file(fptr, &status);
+        psError("Could not determine dimensions of '%s'. FITSIO Error: %s",
+                filename,fitsErr);
+        psImageFree(output);
+        return NULL;
+    }
+
+    /* Validate the number of axis */
+    if ( (nAxis < 2) || (nAxis > 3) ) {
+        status=0;
+        (void)fits_close_file(fptr, &status);
+        psError("Dimensions of '%s' are not supported (NAXIS=%i).",
+                filename, nAxis);
+        psImageFree(output);
+        return NULL;
+    }
+
+    /* Get the Image size from the FITS file  */
+    if ( fits_get_img_size(fptr, nAxis, nAxes, &status) != 0) {
+        (void)fits_get_errstatus(status, fitsErr);
+        status = 0;
+        (void)fits_close_file(fptr, &status);
+        psError("Could not determine image size of '%s'. FITSIO Error:%s",
+                filename,fitsErr);
+        psImageFree(output);
+        return NULL;
+    }
+
+    if (numCols < 1) {
+        numCols += nAxes[0];
+    }
+    if (numRows < 1) {
+        numRows += nAxes[1];
+    }
+
+    firstPixel[0] = col+1;
+    firstPixel[1] = row+1;
+    firstPixel[2] = z+1;
+
+    lastPixel[0] = firstPixel[0] + numCols - 1;
+    lastPixel[1] = firstPixel[1] + numRows - 1;
+    lastPixel[2] = z+1;
+
+    increment[0] = 1;
+    increment[1] = 1;
+    increment[2] = 1;
+
+    // turn off the BSCALE/BZERO processing in CFITSIO
+    (void)fits_set_bscale(fptr, 1.0,0.0,&status);
+
+    switch (bitPix) {
+    case BYTE_IMG:
+        psImageRealloc(output,numCols,numRows,PS_TYPE_U8);
+        (void)fits_read_subset(fptr, TBYTE, firstPixel, lastPixel, increment,
+                               NULL, output->data.v, &anynull, &status);
+        break;
+    case SHORT_IMG:
+        psImageRealloc(output,numCols,numRows,PS_TYPE_S16);
+        (void)fits_read_subset(fptr, TSHORT, firstPixel, lastPixel, increment,
+                               NULL, output->data.v, &anynull, &status);
+        break;
+    case LONG_IMG:
+        psImageRealloc(output,numCols,numRows,PS_TYPE_S32);
+        (void)fits_read_subset(fptr, TINT, firstPixel, lastPixel, increment,
+                               NULL, output->data.v, &anynull, &status);
+        break;
+    case FLOAT_IMG:
+        psImageRealloc(output,numCols,numRows,PS_TYPE_F32);
+        (void)fits_read_subset(fptr, TFLOAT, firstPixel, lastPixel, increment,
+                               NULL, output->data.v, &anynull, &status);
+        break;
+    case DOUBLE_IMG:
+        psImageRealloc(output,numCols,numRows,PS_TYPE_F64);
+        (void)fits_read_subset(fptr, TDOUBLE, firstPixel, lastPixel, increment,
+                               NULL, output->data.v, &anynull, &status);
+        break;
+    default:
+        psError(__func__,"Unsupported bitpix value (%d) in FITS file %s.",
+                bitPix,filename);
+        psImageFree(output);
+        return NULL;
+    }
+
+    return output;
+}
+
+
+int psImageWriteSection(psImage* input, int col0,int row0,int z,
+                        char* extname, int extnum, char* filename)
+{
+
+    return 0;
+}
Index: /trunk/psLib/src/image/psImage.h
===================================================================
--- /trunk/psLib/src/image/psImage.h	(revision 776)
+++ /trunk/psLib/src/image/psImage.h	(revision 777)
@@ -9,6 +9,6 @@
  *  @author Ross Harman, MHPCC
  *
- *  @version $Revision: 1.13 $ $Name: not supported by cvs2svn $
- *  @date $Date: 2004-05-20 01:49:47 $
+ *  @version $Revision: 1.14 $ $Name: not supported by cvs2svn $
+ *  @date $Date: 2004-05-25 20:28:46 $
  *
  *  Copyright 2004 Maui High Performance Computing Center, University of Hawaii
@@ -207,57 +207,57 @@
      *   only used if extname is NULL
      */
+    FILE* file
+    /**< the file descriptor of the FITS file */
+);
+
+/** Read an image or subimage from a FITS file specified by a filename.
+ *
+ *  return psImage*         NULL if an error, otherwise same as input psImage
+ */
+int psImageWriteSection(
+    psImage* input,
+    /**< the psImage to write */
+    int col0,
+    /**< the column index of the origin to start writing */
+    int row0,
+    /**< the row index of the origin to start writing */
+    int z,
+    /**< the z index to start writing */
+    char* extname,
+    /**< the image extension to write (this should match the EXTNAME keyword in
+     *   the extension If NULL, the extnum parameter is to be used instead
+     */
+    int extnum,
+    /**< the image extension to write (0=PHU, 1=first extension, etc.)  This is
+     *   only used if extname is NULL.
+     */
+    char* filename
+    /**< the filename of the FITS image file to write */
+);
+
+/** Read an image or subimage from a FITS file from a file descriptor.
+ *
+ *  return psImage*         NULL if an error, otherwise same as input psImage
+ */
+int psImageFWriteSection(
+    psImage* input,
+    /**< the psImage to write */
+    int col0,
+    /**< the column index of the origin to start writing */
+    int row0,
+    /**< the row index of the origin to start writing */
+    int z,
+    /**< the z index to start writing */
+    char* extname,
+    /**< the image extension to write (this should match the EXTNAME keyword in
+     *   the extension If NULL, the extnum parameter is to be used instead
+     */
+    int extnum,
+    /**< the image extension to write (0=PHU, 1=first extension, etc.)  This is
+     *   only used if extname is NULL.
+     */
     FILE* f
     /**< the file descriptor of the FITS file */
 );
 
-/** Read an image or subimage from a FITS file specified by a filename.
- *
- *  return psImage*         NULL if an error, otherwise same as input psImage
- */
-psImage* psImageWriteSection(
-    psImage* input,
-    /**< the psImage to write */
-    int col0,
-    /**< the column index of the origin to start writing */
-    int row0,
-    /**< the row index of the origin to start writing */
-    int z,
-    /**< the z index to start writing */
-    char* extname,
-    /**< the image extension to write (this should match the EXTNAME keyword in
-     *   the extension If NULL, the extnum parameter is to be used instead
-     */
-    int extnum,
-    /**< the image extension to write (0=PHU, 1=first extension, etc.)  This is
-     *   only used if extname is NULL.
-     */
-    char* filename
-    /**< the filename of the FITS image file to write */
-);
-
-/** Read an image or subimage from a FITS file from a file descriptor.
- *
- *  return psImage*         NULL if an error, otherwise same as input psImage
- */
-psImage* psImageFWriteSection(
-    psImage* input,
-    /**< the psImage to write */
-    int col0,
-    /**< the column index of the origin to start writing */
-    int row0,
-    /**< the row index of the origin to start writing */
-    int z,
-    /**< the z index to start writing */
-    char* extname,
-    /**< the image extension to write (this should match the EXTNAME keyword in
-     *   the extension If NULL, the extnum parameter is to be used instead
-     */
-    int extnum,
-    /**< the image extension to write (0=PHU, 1=first extension, etc.)  This is
-     *   only used if extname is NULL.
-     */
-    FILE* f
-    /**< the file descriptor of the FITS file */
-);
-
 #endif
Index: /trunk/psLib/src/mathtypes/psImage.c
===================================================================
--- /trunk/psLib/src/mathtypes/psImage.c	(revision 776)
+++ /trunk/psLib/src/mathtypes/psImage.c	(revision 777)
@@ -9,6 +9,6 @@
  *  @author Ross Harman, MHPCC
  *
- *  @version $Revision: 1.15 $ $Name: not supported by cvs2svn $
- *  @date $Date: 2004-05-20 01:49:47 $
+ *  @version $Revision: 1.16 $ $Name: not supported by cvs2svn $
+ *  @date $Date: 2004-05-25 20:28:46 $
  *
  *  Copyright 2004 Maui High Performance Computing Center, University of Hawaii
@@ -18,9 +18,11 @@
 /*  INCLUDE FILES                                                             */
 /******************************************************************************/
+#include <string.h>
+#include <fitsio.h>
+
 #include "psMemory.h"
 #include "psError.h"
 #include "psImage.h"
 
-#include <string.h>
 
 /*****************************************************************************/
@@ -355,5 +357,155 @@
                             int numRows, int z, char* extname, int extnum, char* filename)
 {
-
-    return NULL;
-}
+    fitsfile    *fptr=NULL;                 /*Pointer to the FITS file*/
+    int         status=0;                   /*CFITSIO file vars*/
+    int         nAxis=0;
+    int         anynull=0;
+    int         bitPix=0;                   /*Pixel type*/
+    long        nAxes[3];
+    long        firstPixel[3];              /* lower-left corner of image subset */
+    long        lastPixel[3];               /* upper-right corner of image subset */
+    long        increment[3];               /* increment for image subset */
+    char        fitsErr[80] = "";           /*CFITSIO error message string */
+    int         hduType = IMAGE_HDU;
+
+    /* Open the FITS file */
+    (void)fits_open_file(&fptr, filename, READONLY, &status);
+    if (fptr == NULL || status != 0) {
+        fits_get_errstatus(status,fitsErr);
+        psError(__func__,"Could not open file '%s'. FITS error:%s",
+                filename, fitsErr);
+        psImageFree(output);
+        return NULL;
+    }
+
+    /* find the specified extension */
+    if (extname != NULL) {
+        if (fits_movnam_hdu(fptr, hduType, extname, 0,&status) != 0) {
+            fits_get_errstatus(status, fitsErr);
+            status = 0;
+            (void)fits_close_file(fptr, &status);
+            psError(__func__,"Could not index to '%s' HDU for file %s. FITSIO ERROR:%s",
+                    extname, filename, fitsErr);
+            psImageFree(output);
+            return NULL;
+        }
+    } else {
+        if (fits_movabs_hdu(fptr, extnum, &hduType, &status) != 0) {
+            fits_get_errstatus(status, fitsErr);
+            status = 0;
+            (void)fits_close_file(fptr, &status);
+            psError(__func__,"Could not index to HDU #%d for file %s. FITSIO ERROR:%s",
+                    extnum, filename, fitsErr);
+            psImageFree(output);
+            return NULL;
+        }
+    }
+
+    /* Get the data type 'bitPix' from the FITS image */
+    if (fits_get_img_type(fptr, &bitPix, &status) != 0) {
+        fits_get_errstatus(status, fitsErr);
+        status = 0;
+        (void)fits_close_file(fptr, &status);
+        psError("Could not determine image data type of '%s'. FITSIO ERROR:%s",
+                filename, fitsErr);
+        psImageFree(output);
+        return NULL;
+    }
+
+    /* Get the dimensions 'nAxis' from the FITS image */
+    if (fits_get_img_dim(fptr, &nAxis, &status) != 0) {
+        (void)fits_get_errstatus(status, fitsErr);
+        status = 0;
+        (void)fits_close_file(fptr, &status);
+        psError("Could not determine dimensions of '%s'. FITSIO Error: %s",
+                filename,fitsErr);
+        psImageFree(output);
+        return NULL;
+    }
+
+    /* Validate the number of axis */
+    if ( (nAxis < 2) || (nAxis > 3) ) {
+        status=0;
+        (void)fits_close_file(fptr, &status);
+        psError("Dimensions of '%s' are not supported (NAXIS=%i).",
+                filename, nAxis);
+        psImageFree(output);
+        return NULL;
+    }
+
+    /* Get the Image size from the FITS file  */
+    if ( fits_get_img_size(fptr, nAxis, nAxes, &status) != 0) {
+        (void)fits_get_errstatus(status, fitsErr);
+        status = 0;
+        (void)fits_close_file(fptr, &status);
+        psError("Could not determine image size of '%s'. FITSIO Error:%s",
+                filename,fitsErr);
+        psImageFree(output);
+        return NULL;
+    }
+
+    if (numCols < 1) {
+        numCols += nAxes[0];
+    }
+    if (numRows < 1) {
+        numRows += nAxes[1];
+    }
+
+    firstPixel[0] = col+1;
+    firstPixel[1] = row+1;
+    firstPixel[2] = z+1;
+
+    lastPixel[0] = firstPixel[0] + numCols - 1;
+    lastPixel[1] = firstPixel[1] + numRows - 1;
+    lastPixel[2] = z+1;
+
+    increment[0] = 1;
+    increment[1] = 1;
+    increment[2] = 1;
+
+    // turn off the BSCALE/BZERO processing in CFITSIO
+    (void)fits_set_bscale(fptr, 1.0,0.0,&status);
+
+    switch (bitPix) {
+    case BYTE_IMG:
+        psImageRealloc(output,numCols,numRows,PS_TYPE_U8);
+        (void)fits_read_subset(fptr, TBYTE, firstPixel, lastPixel, increment,
+                               NULL, output->data.v, &anynull, &status);
+        break;
+    case SHORT_IMG:
+        psImageRealloc(output,numCols,numRows,PS_TYPE_S16);
+        (void)fits_read_subset(fptr, TSHORT, firstPixel, lastPixel, increment,
+                               NULL, output->data.v, &anynull, &status);
+        break;
+    case LONG_IMG:
+        psImageRealloc(output,numCols,numRows,PS_TYPE_S32);
+        (void)fits_read_subset(fptr, TINT, firstPixel, lastPixel, increment,
+                               NULL, output->data.v, &anynull, &status);
+        break;
+    case FLOAT_IMG:
+        psImageRealloc(output,numCols,numRows,PS_TYPE_F32);
+        (void)fits_read_subset(fptr, TFLOAT, firstPixel, lastPixel, increment,
+                               NULL, output->data.v, &anynull, &status);
+        break;
+    case DOUBLE_IMG:
+        psImageRealloc(output,numCols,numRows,PS_TYPE_F64);
+        (void)fits_read_subset(fptr, TDOUBLE, firstPixel, lastPixel, increment,
+                               NULL, output->data.v, &anynull, &status);
+        break;
+    default:
+        psError(__func__,"Unsupported bitpix value (%d) in FITS file %s.",
+                bitPix,filename);
+        psImageFree(output);
+        return NULL;
+    }
+
+    return output;
+}
+
+
+int psImageWriteSection(psImage* input, int col0,int row0,int z,
+                        char* extname, int extnum, char* filename)
+{
+
+    return 0;
+}
Index: /trunk/psLib/src/mathtypes/psImage.h
===================================================================
--- /trunk/psLib/src/mathtypes/psImage.h	(revision 776)
+++ /trunk/psLib/src/mathtypes/psImage.h	(revision 777)
@@ -9,6 +9,6 @@
  *  @author Ross Harman, MHPCC
  *
- *  @version $Revision: 1.13 $ $Name: not supported by cvs2svn $
- *  @date $Date: 2004-05-20 01:49:47 $
+ *  @version $Revision: 1.14 $ $Name: not supported by cvs2svn $
+ *  @date $Date: 2004-05-25 20:28:46 $
  *
  *  Copyright 2004 Maui High Performance Computing Center, University of Hawaii
@@ -207,57 +207,57 @@
      *   only used if extname is NULL
      */
+    FILE* file
+    /**< the file descriptor of the FITS file */
+);
+
+/** Read an image or subimage from a FITS file specified by a filename.
+ *
+ *  return psImage*         NULL if an error, otherwise same as input psImage
+ */
+int psImageWriteSection(
+    psImage* input,
+    /**< the psImage to write */
+    int col0,
+    /**< the column index of the origin to start writing */
+    int row0,
+    /**< the row index of the origin to start writing */
+    int z,
+    /**< the z index to start writing */
+    char* extname,
+    /**< the image extension to write (this should match the EXTNAME keyword in
+     *   the extension If NULL, the extnum parameter is to be used instead
+     */
+    int extnum,
+    /**< the image extension to write (0=PHU, 1=first extension, etc.)  This is
+     *   only used if extname is NULL.
+     */
+    char* filename
+    /**< the filename of the FITS image file to write */
+);
+
+/** Read an image or subimage from a FITS file from a file descriptor.
+ *
+ *  return psImage*         NULL if an error, otherwise same as input psImage
+ */
+int psImageFWriteSection(
+    psImage* input,
+    /**< the psImage to write */
+    int col0,
+    /**< the column index of the origin to start writing */
+    int row0,
+    /**< the row index of the origin to start writing */
+    int z,
+    /**< the z index to start writing */
+    char* extname,
+    /**< the image extension to write (this should match the EXTNAME keyword in
+     *   the extension If NULL, the extnum parameter is to be used instead
+     */
+    int extnum,
+    /**< the image extension to write (0=PHU, 1=first extension, etc.)  This is
+     *   only used if extname is NULL.
+     */
     FILE* f
     /**< the file descriptor of the FITS file */
 );
 
-/** Read an image or subimage from a FITS file specified by a filename.
- *
- *  return psImage*         NULL if an error, otherwise same as input psImage
- */
-psImage* psImageWriteSection(
-    psImage* input,
-    /**< the psImage to write */
-    int col0,
-    /**< the column index of the origin to start writing */
-    int row0,
-    /**< the row index of the origin to start writing */
-    int z,
-    /**< the z index to start writing */
-    char* extname,
-    /**< the image extension to write (this should match the EXTNAME keyword in
-     *   the extension If NULL, the extnum parameter is to be used instead
-     */
-    int extnum,
-    /**< the image extension to write (0=PHU, 1=first extension, etc.)  This is
-     *   only used if extname is NULL.
-     */
-    char* filename
-    /**< the filename of the FITS image file to write */
-);
-
-/** Read an image or subimage from a FITS file from a file descriptor.
- *
- *  return psImage*         NULL if an error, otherwise same as input psImage
- */
-psImage* psImageFWriteSection(
-    psImage* input,
-    /**< the psImage to write */
-    int col0,
-    /**< the column index of the origin to start writing */
-    int row0,
-    /**< the row index of the origin to start writing */
-    int z,
-    /**< the z index to start writing */
-    char* extname,
-    /**< the image extension to write (this should match the EXTNAME keyword in
-     *   the extension If NULL, the extnum parameter is to be used instead
-     */
-    int extnum,
-    /**< the image extension to write (0=PHU, 1=first extension, etc.)  This is
-     *   only used if extname is NULL.
-     */
-    FILE* f
-    /**< the file descriptor of the FITS file */
-);
-
 #endif
