Index: /trunk/psLib/src/image/psImage.c
===================================================================
--- /trunk/psLib/src/image/psImage.c	(revision 789)
+++ /trunk/psLib/src/image/psImage.c	(revision 790)
@@ -9,6 +9,6 @@
  *  @author Ross Harman, MHPCC
  *
- *  @version $Revision: 1.16 $ $Name: not supported by cvs2svn $
- *  @date $Date: 2004-05-25 20:28:46 $
+ *  @version $Revision: 1.17 $ $Name: not supported by cvs2svn $
+ *  @date $Date: 2004-05-26 19:28:57 $
  *
  *  Copyright 2004 Maui High Performance Computing Center, University of Hawaii
@@ -20,4 +20,5 @@
 #include <string.h>
 #include <fitsio.h>
+#include <unistd.h>
 
 #include "psMemory.h"
@@ -391,5 +392,5 @@
         }
     } else {
-        if (fits_movabs_hdu(fptr, extnum, &hduType, &status) != 0) {
+        if (fits_movabs_hdu(fptr, extnum+1, &hduType, &status) != 0) {
             fits_get_errstatus(status, fitsErr);
             status = 0;
@@ -507,4 +508,153 @@
                         char* extname, int extnum, char* filename)
 {
+    int         numCols = 0;
+    int         numRows = 0;
+
+    int         status=0;               /* CFITSIO  status */
+    fitsfile    *fptr=NULL;             /* pointer to the FITS file */
+    long        nAxes[3];               /* Image axis vars */
+    long        firstPixel[3];          /* First Pixel to read */
+    long        lastPixel[3];           /* Last Pixel to read */
+    char        fitsErr[80];            /* FITSIO message string */
+    int         datatype = 0;           /* the datatype of the image */
+    int         bitPix = 0;             /* FITS bitPix value */
+    int         hduType = IMAGE_HDU;    /* the HDU type (image,table, etc.) */
+
+    /* need a valid image to write */
+    if(input==NULL) {
+        psError(__func__, "Can not write %s.  Input psImage is NULL.",
+                filename);
+        return 1;
+    }
+
+    numRows = input->numRows;
+    numCols = input->numCols;
+
+    switch (input->type.type) {
+    case PS_TYPE_U8:
+        bitPix = BYTE_IMG;
+        datatype = TBYTE;
+        break;
+    case PS_TYPE_S8:
+        bitPix = SBYTE_IMG;
+        datatype = TSBYTE;
+        break;
+    case PS_TYPE_U16:
+        bitPix = USHORT_IMG;
+        datatype = TUSHORT;
+        break;
+    case PS_TYPE_S16:
+        bitPix = SHORT_IMG;
+        datatype = TSHORT;
+        break;
+    case PS_TYPE_U32:
+        bitPix = ULONG_IMG;
+        datatype = TULONG;
+        break;
+    case PS_TYPE_S32:
+        bitPix = LONG_IMG;
+        datatype = TLONG;
+        break;
+    case PS_TYPE_F32:
+        bitPix = FLOAT_IMG;
+        datatype = TFLOAT;
+        break;
+    case PS_TYPE_F64:
+        bitPix = DOUBLE_IMG;
+        datatype = TDOUBLE;
+        break;
+    default:
+        psError(__func__, "psImage datatype (%d) not supported.  File %s not written.",
+                input->type.type,filename);
+        return 1;
+    }
+
+    /* Open the FITS file */
+    if (access(filename, F_OK) == 0) { // file exists
+        (void)fits_open_file(&fptr, filename, READWRITE, &status);
+        if (fptr == NULL || status != 0) {
+            fits_get_errstatus(status,fitsErr);
+            psError(__func__,"Could not open file '%s'. FITS error:%s",
+                    filename, fitsErr);
+            return 2;
+        }
+
+        /* 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);
+                return 3;
+            }
+        } else {
+            if (fits_movabs_hdu(fptr, extnum+1, &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);
+                return 3;
+            }
+        }
+
+    } else { // file does not exist
+
+        if (col0 != 0 || row0 != 0 || z != 0) {
+            psError(__func__,"File %s does not exist and a non-zero indexing (%d,%d,%d) was specified.",
+                    filename,col0,row0,z);
+            return 4;
+        }
+        (void)fits_create_file(&fptr,filename,&status);
+        if (fptr == NULL || status != 0) {
+            fits_get_errstatus(status,fitsErr);
+            psError(__func__,"Could not create file '%s'. FITS error:%s",
+                    filename, fitsErr);
+            return 5;
+        }
+
+        /*  create the mandatory image keywords */
+        nAxes[0] = numCols;
+        nAxes[1] = numRows;
+        if (fits_create_img(fptr, bitPix, 2, nAxes, &status) != 0) {
+            (void)fits_get_errstatus(status, fitsErr);
+            status = 0;
+            (void)fits_close_file(fptr, &status);
+            psError(__func__,"Could not create image HDU in FITS file '%s'. %s",
+                    filename, fitsErr);
+            return 5;
+        }
+
+        if (extname != NULL) {
+            /* create the extension for the Primary HDU */
+            if (fits_write_key_str(fptr, "EXTNAME", extname, "Extension Name", &status) != 0) {
+                (void)fits_get_errstatus(status, fitsErr);
+                status = 0;
+                (void)fits_close_file(fptr, &status);
+                psError(__func__,"Could not create EXTNAME keyword in FITS file '%s'. %s",
+                        filename, fitsErr);
+                return 5;
+            }
+        }
+    }
+
+    firstPixel[0] = col0+1;
+    firstPixel[1] = row0+1;
+    firstPixel[2] = z+1;
+
+    lastPixel[0] = firstPixel[0] + numCols - 1;
+    lastPixel[1] = firstPixel[1] + numRows - 1;
+    lastPixel[2] = z+1;
+
+    if ( fits_write_subset(fptr, datatype, firstPixel, lastPixel, input->data.v, &status) != 0) {
+        (void)fits_get_errstatus(status, fitsErr);
+        status = 0;
+        (void)fits_close_file(fptr, &status);
+        psError(__func__, "Could not write image data to '%s'. FITS Error: %s",
+                filename, fitsErr);
+        return 6;
+    }
 
     return 0;
Index: /trunk/psLib/src/mathtypes/psImage.c
===================================================================
--- /trunk/psLib/src/mathtypes/psImage.c	(revision 789)
+++ /trunk/psLib/src/mathtypes/psImage.c	(revision 790)
@@ -9,6 +9,6 @@
  *  @author Ross Harman, MHPCC
  *
- *  @version $Revision: 1.16 $ $Name: not supported by cvs2svn $
- *  @date $Date: 2004-05-25 20:28:46 $
+ *  @version $Revision: 1.17 $ $Name: not supported by cvs2svn $
+ *  @date $Date: 2004-05-26 19:28:57 $
  *
  *  Copyright 2004 Maui High Performance Computing Center, University of Hawaii
@@ -20,4 +20,5 @@
 #include <string.h>
 #include <fitsio.h>
+#include <unistd.h>
 
 #include "psMemory.h"
@@ -391,5 +392,5 @@
         }
     } else {
-        if (fits_movabs_hdu(fptr, extnum, &hduType, &status) != 0) {
+        if (fits_movabs_hdu(fptr, extnum+1, &hduType, &status) != 0) {
             fits_get_errstatus(status, fitsErr);
             status = 0;
@@ -507,4 +508,153 @@
                         char* extname, int extnum, char* filename)
 {
+    int         numCols = 0;
+    int         numRows = 0;
+
+    int         status=0;               /* CFITSIO  status */
+    fitsfile    *fptr=NULL;             /* pointer to the FITS file */
+    long        nAxes[3];               /* Image axis vars */
+    long        firstPixel[3];          /* First Pixel to read */
+    long        lastPixel[3];           /* Last Pixel to read */
+    char        fitsErr[80];            /* FITSIO message string */
+    int         datatype = 0;           /* the datatype of the image */
+    int         bitPix = 0;             /* FITS bitPix value */
+    int         hduType = IMAGE_HDU;    /* the HDU type (image,table, etc.) */
+
+    /* need a valid image to write */
+    if(input==NULL) {
+        psError(__func__, "Can not write %s.  Input psImage is NULL.",
+                filename);
+        return 1;
+    }
+
+    numRows = input->numRows;
+    numCols = input->numCols;
+
+    switch (input->type.type) {
+    case PS_TYPE_U8:
+        bitPix = BYTE_IMG;
+        datatype = TBYTE;
+        break;
+    case PS_TYPE_S8:
+        bitPix = SBYTE_IMG;
+        datatype = TSBYTE;
+        break;
+    case PS_TYPE_U16:
+        bitPix = USHORT_IMG;
+        datatype = TUSHORT;
+        break;
+    case PS_TYPE_S16:
+        bitPix = SHORT_IMG;
+        datatype = TSHORT;
+        break;
+    case PS_TYPE_U32:
+        bitPix = ULONG_IMG;
+        datatype = TULONG;
+        break;
+    case PS_TYPE_S32:
+        bitPix = LONG_IMG;
+        datatype = TLONG;
+        break;
+    case PS_TYPE_F32:
+        bitPix = FLOAT_IMG;
+        datatype = TFLOAT;
+        break;
+    case PS_TYPE_F64:
+        bitPix = DOUBLE_IMG;
+        datatype = TDOUBLE;
+        break;
+    default:
+        psError(__func__, "psImage datatype (%d) not supported.  File %s not written.",
+                input->type.type,filename);
+        return 1;
+    }
+
+    /* Open the FITS file */
+    if (access(filename, F_OK) == 0) { // file exists
+        (void)fits_open_file(&fptr, filename, READWRITE, &status);
+        if (fptr == NULL || status != 0) {
+            fits_get_errstatus(status,fitsErr);
+            psError(__func__,"Could not open file '%s'. FITS error:%s",
+                    filename, fitsErr);
+            return 2;
+        }
+
+        /* 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);
+                return 3;
+            }
+        } else {
+            if (fits_movabs_hdu(fptr, extnum+1, &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);
+                return 3;
+            }
+        }
+
+    } else { // file does not exist
+
+        if (col0 != 0 || row0 != 0 || z != 0) {
+            psError(__func__,"File %s does not exist and a non-zero indexing (%d,%d,%d) was specified.",
+                    filename,col0,row0,z);
+            return 4;
+        }
+        (void)fits_create_file(&fptr,filename,&status);
+        if (fptr == NULL || status != 0) {
+            fits_get_errstatus(status,fitsErr);
+            psError(__func__,"Could not create file '%s'. FITS error:%s",
+                    filename, fitsErr);
+            return 5;
+        }
+
+        /*  create the mandatory image keywords */
+        nAxes[0] = numCols;
+        nAxes[1] = numRows;
+        if (fits_create_img(fptr, bitPix, 2, nAxes, &status) != 0) {
+            (void)fits_get_errstatus(status, fitsErr);
+            status = 0;
+            (void)fits_close_file(fptr, &status);
+            psError(__func__,"Could not create image HDU in FITS file '%s'. %s",
+                    filename, fitsErr);
+            return 5;
+        }
+
+        if (extname != NULL) {
+            /* create the extension for the Primary HDU */
+            if (fits_write_key_str(fptr, "EXTNAME", extname, "Extension Name", &status) != 0) {
+                (void)fits_get_errstatus(status, fitsErr);
+                status = 0;
+                (void)fits_close_file(fptr, &status);
+                psError(__func__,"Could not create EXTNAME keyword in FITS file '%s'. %s",
+                        filename, fitsErr);
+                return 5;
+            }
+        }
+    }
+
+    firstPixel[0] = col0+1;
+    firstPixel[1] = row0+1;
+    firstPixel[2] = z+1;
+
+    lastPixel[0] = firstPixel[0] + numCols - 1;
+    lastPixel[1] = firstPixel[1] + numRows - 1;
+    lastPixel[2] = z+1;
+
+    if ( fits_write_subset(fptr, datatype, firstPixel, lastPixel, input->data.v, &status) != 0) {
+        (void)fits_get_errstatus(status, fitsErr);
+        status = 0;
+        (void)fits_close_file(fptr, &status);
+        psError(__func__, "Could not write image data to '%s'. FITS Error: %s",
+                filename, fitsErr);
+        return 6;
+    }
 
     return 0;
