Index: trunk/psLib/src/image/psImageIO.c
===================================================================
--- trunk/psLib/src/image/psImageIO.c	(revision 824)
+++ trunk/psLib/src/image/psImageIO.c	(revision 997)
@@ -7,6 +7,6 @@
  *  @author Robert DeSonia, MHPCC
  *
- *  @version $Revision: 1.2 $ $Name: not supported by cvs2svn $
- *  @date $Date: 2004-06-01 22:42:57 $
+ *  @version $Revision: 1.3 $ $Name: not supported by cvs2svn $
+ *  @date $Date: 2004-06-11 03:45:07 $
  *
  *  Copyright 2004 Maui High Performance Computing Center, University of Hawaii
@@ -15,4 +15,6 @@
 #include <fitsio.h>
 #include <unistd.h>
+#include <stdbool.h>
+
 #include "psImageIO.h"
 #include "psError.h"
@@ -32,4 +34,12 @@
     char        fitsErr[80] = "";           /*CFITSIO error message string */
     int         hduType = IMAGE_HDU;
+    int         fitsDatatype = 0;
+    int         datatype = 0;
+
+    if (filename == NULL) {
+        psError(__func__,"Must specify filename; it can not be NULL.");
+        psImageFree(output);
+        return NULL;
+    }
 
     /* Open the FITS file */
@@ -37,5 +47,5 @@
     if (fptr == NULL || status != 0) {
         fits_get_errstatus(status,fitsErr);
-        psError(__func__,"Could not open file '%s'. FITS error:%s",
+        psError(__func__,"Could not open file '%s'. (%s)",
                 filename, fitsErr);
         psImageFree(output);
@@ -49,5 +59,5 @@
             status = 0;
             (void)fits_close_file(fptr, &status);
-            psError(__func__,"Could not index to '%s' HDU for file %s. FITSIO ERROR:%s",
+            psError(__func__,"Could not index to '%s' HDU for file %s. (%s)",
                     extname, filename, fitsErr);
             psImageFree(output);
@@ -59,5 +69,5 @@
             status = 0;
             (void)fits_close_file(fptr, &status);
-            psError(__func__,"Could not index to HDU #%d for file %s. FITSIO ERROR:%s",
+            psError(__func__,"Could not index to HDU #%d for file %s. (%s)",
                     extnum, filename, fitsErr);
             psImageFree(output);
@@ -67,9 +77,9 @@
 
     /* Get the data type 'bitPix' from the FITS image */
-    if (fits_get_img_type(fptr, &bitPix, &status) != 0) {
+    if (fits_get_img_equivtype(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",
+        psError("Could not determine image data type of '%s'. (%s)",
                 filename, fitsErr);
         psImageFree(output);
@@ -82,5 +92,5 @@
         status = 0;
         (void)fits_close_file(fptr, &status);
-        psError("Could not determine dimensions of '%s'. FITSIO Error: %s",
+        psError("Could not determine dimensions of '%s'. (%s)",
                 filename,fitsErr);
         psImageFree(output);
@@ -103,5 +113,5 @@
         status = 0;
         (void)fits_close_file(fptr, &status);
-        psError("Could not determine image size of '%s'. FITSIO Error:%s",
+        psError("Could not determine image size of '%s'. (%s)",
                 filename,fitsErr);
         psImageFree(output);
@@ -129,31 +139,42 @@
 
     // turn off the BSCALE/BZERO processing in CFITSIO
-    (void)fits_set_bscale(fptr, 1.0,0.0,&status);
+    // (void)fits_set_bscale(fptr, 1.0,0.0,&status);
 
     switch (bitPix) {
     case BYTE_IMG:
-        psImageRecycle(output,numCols,numRows,PS_TYPE_U8);
-        (void)fits_read_subset(fptr, TBYTE, firstPixel, lastPixel, increment,
-                               NULL, output->data.V, &anynull, &status);
+        datatype = PS_TYPE_U8;
+        fitsDatatype = TBYTE;
+        break;
+    case SBYTE_IMG:
+        datatype = PS_TYPE_S8;
+        fitsDatatype = TSBYTE;
+        break;
+    case USHORT_IMG:
+        datatype = PS_TYPE_U16;
+        fitsDatatype = TUSHORT;
         break;
     case SHORT_IMG:
-        psImageRecycle(output,numCols,numRows,PS_TYPE_S16);
-        (void)fits_read_subset(fptr, TSHORT, firstPixel, lastPixel, increment,
-                               NULL, output->data.V, &anynull, &status);
+        datatype = PS_TYPE_S16;
+        fitsDatatype = TSHORT;
+        break;
+    case ULONG_IMG:
+        datatype = PS_TYPE_U32;
+        fitsDatatype = TUINT;
         break;
     case LONG_IMG:
-        psImageRecycle(output,numCols,numRows,PS_TYPE_S32);
-        (void)fits_read_subset(fptr, TINT, firstPixel, lastPixel, increment,
-                               NULL, output->data.V, &anynull, &status);
+        datatype = PS_TYPE_S32;
+        fitsDatatype = TINT;
+        break;
+    case LONGLONG_IMG:
+        datatype = PS_TYPE_S64;
+        fitsDatatype = TLONGLONG;
         break;
     case FLOAT_IMG:
-        psImageRecycle(output,numCols,numRows,PS_TYPE_F32);
-        (void)fits_read_subset(fptr, TFLOAT, firstPixel, lastPixel, increment,
-                               NULL, output->data.V, &anynull, &status);
+        datatype = PS_TYPE_F32;
+        fitsDatatype = TFLOAT;
         break;
     case DOUBLE_IMG:
-        psImageRecycle(output,numCols,numRows,PS_TYPE_F64);
-        (void)fits_read_subset(fptr, TDOUBLE, firstPixel, lastPixel, increment,
-                               NULL, output->data.V, &anynull, &status);
+        datatype = PS_TYPE_F64;
+        fitsDatatype = TDOUBLE;
         break;
     default:
@@ -163,4 +184,17 @@
         return NULL;
     }
+    output = psImageRecycle(output,numCols,numRows,datatype);
+    if (fits_read_subset(fptr, fitsDatatype, firstPixel, lastPixel, increment,
+                         NULL, output->data.V[0], &anynull, &status) != 0) {
+        psImageFree(output);
+        (void)fits_get_errstatus(status, fitsErr);
+        status = 0;
+        (void)fits_close_file(fptr, &status);
+        psError(__func__,"Failed to read image [%s]",
+                filename, fitsErr);
+        return NULL;
+    }
+
+    (void)fits_close_file(fptr, &status);
 
     return output;
@@ -185,4 +219,5 @@
     double      bscale = 1.0;
     double      bzero = 0.0;
+    bool        createNewHDU = false;
 
     /* need a valid image to write */
@@ -203,10 +238,10 @@
     case PS_TYPE_S8:
         bitPix = BYTE_IMG;
-        bzero = -128.0;
+        bzero = INT8_MIN;
         datatype = TSBYTE;
         break;
     case PS_TYPE_U16:
         bitPix = SHORT_IMG;
-        bzero = 32768.0;
+        bzero = -1.0f*INT16_MIN;
         datatype = TUSHORT;
         break;
@@ -217,10 +252,10 @@
     case PS_TYPE_U32:
         bitPix = LONG_IMG;
-        bzero = 2147483648.0;
-        datatype = TULONG;
+        bzero = -1.0f*INT32_MIN;
+        datatype = TUINT;
         break;
     case PS_TYPE_S32:
         bitPix = LONG_IMG;
-        datatype = TLONG;
+        datatype = TINT;
         break;
     case PS_TYPE_F32:
@@ -254,14 +289,24 @@
                 status = 0;
                 (void)fits_close_file(fptr, &status);
-                psError(__func__,"Could not index to '%s' HDU for file %s. FITSIO ERROR:%s",
+                psError(__func__,"Could not index to '%s' HDU for file %s. (%s)",
                         extname, filename, fitsErr);
                 return 3;
             }
         } else {
-            if (fits_movabs_hdu(fptr, extnum+1, &hduType, &status) != 0) {
+            int numHDUs = 0;
+            fits_get_num_hdus(fptr,&numHDUs,&status);
+            if (numHDUs < extnum) {
+                status = 0;
+                (void)fits_close_file(fptr, &status);
+                psError(__func__,"extnum (%d) must not exceed number of HDUs (%d) by more than one.",
+                        extnum, numHDUs);
+                return 3;
+            } else if (numHDUs == extnum) {
+                createNewHDU = true;
+            } 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",
+                psError(__func__,"Could not index to HDU #%d for file %s. (%s)",
                         extnum, filename, fitsErr);
                 return 3;
@@ -271,21 +316,20 @@
     } 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",
+            psError(__func__,"Could not create file '%s'. (%s)",
                     filename, fitsErr);
             return 5;
         }
-
+        createNewHDU = true;
+    }
+
+    if (createNewHDU) {
         /*  create the mandatory image keywords */
-        nAxes[0] = numCols;
-        nAxes[1] = numRows;
-        if (fits_create_img(fptr, bitPix, 2, nAxes, &status) != 0) {
+        nAxes[0] = col0+numCols;
+        nAxes[1] = row0+numRows;
+        nAxes[2] = z+1;
+        if (fits_create_img(fptr, bitPix, 3, nAxes, &status) != 0) {
             (void)fits_get_errstatus(status, fitsErr);
             status = 0;
@@ -297,6 +341,6 @@
 
         // set the bscale/bzero
-        fits_write_key_dbl(fptr, "BZERO",bzero,5,"Pixel Value Offset",&status);
-        fits_write_key_dbl(fptr, "BSCALE",bscale,5,"Pixel Value Scale",&status);
+        fits_write_key_dbl(fptr, "BZERO",bzero,12,"Pixel Value Offset",&status);
+        fits_write_key_dbl(fptr, "BSCALE",bscale,12,"Pixel Value Scale",&status);
         fits_set_bscale(fptr,bscale,bzero,&status);
 
@@ -307,5 +351,5 @@
                 status = 0;
                 (void)fits_close_file(fptr, &status);
-                psError(__func__,"Could not create EXTNAME keyword in FITS file '%s'. %s",
+                psError(__func__,"Could not create EXTNAME keyword in FITS file '%s'. (%s)",
                         filename, fitsErr);
                 return 5;
@@ -322,14 +366,16 @@
     lastPixel[2] = z+1;
 
-
-    if ( fits_write_subset(fptr, datatype, firstPixel, lastPixel, input->data.V, &status) != 0) {
+    if ( fits_write_subset(fptr, datatype, firstPixel, lastPixel, input->data.V[0], &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",
+        psError(__func__, "Could not write image data to '%s'. (%s)",
                 filename, fitsErr);
         return 6;
     }
 
+    status = 0;
+    (void)fits_close_file(fptr, &status);
+
     return 0;
 }
