Index: trunk/psLib/src/image/psImageIO.c
===================================================================
--- trunk/psLib/src/image/psImageIO.c	(revision 1406)
+++ trunk/psLib/src/image/psImageIO.c	(revision 1407)
@@ -1,2 +1,3 @@
+
 /** @file  psImageIO.c
  *
@@ -7,6 +8,6 @@
  *  @author Robert DeSonia, MHPCC
  *
- *  @version $Revision: 1.8 $ $Name: not supported by cvs2svn $
- *  @date $Date: 2004-08-06 22:34:05 $
+ *  @version $Revision: 1.9 $ $Name: not supported by cvs2svn $
+ *  @date $Date: 2004-08-07 00:06:06 $
  *
  *  Copyright 2004 Maui High Performance Computing Center, University of Hawaii
@@ -21,23 +22,25 @@
 #include "psMemory.h"
 
-psImage* psImageReadSection(psImage* output, int col, int row, int numCols,
-                            int numRows, int z, char* extname, int extnum, char* filename)
+psImage *psImageReadSection(psImage * output,
+                            int col,
+                            int row,
+                            int numCols, int numRows, int z, char *extname, int extnum, char *filename)
 {
-    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;
-    int         fitsDatatype = 0;
-    int         datatype = 0;
+    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;
+    int fitsDatatype = 0;
+    int datatype = 0;
 
     if (filename == NULL) {
-        psError(__func__,"Must specify filename; it can not be NULL.");
+        psError(__func__, "Must specify filename; it can not be NULL.");
         psFree(output);
         return NULL;
@@ -47,7 +50,6 @@
     (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'. (%s)",
-                filename, fitsErr);
+        fits_get_errstatus(status, fitsErr);
+        psError(__func__, "Could not open file '%s'. (%s)", filename, fitsErr);
         psFree(output);
         return NULL;
@@ -56,20 +58,18 @@
     /* find the specified extension */
     if (extname != NULL) {
-        if (fits_movnam_hdu(fptr, hduType, extname, 0,&status) != 0) {
+        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. (%s)",
-                    extname, filename, fitsErr);
+            psError(__func__, "Could not index to '%s' HDU for file %s. (%s)", extname, filename, fitsErr);
             psFree(output);
             return NULL;
         }
     } else {
-        if (fits_movabs_hdu(fptr, extnum+1, &hduType, &status) != 0) {
+        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. (%s)",
-                    extnum, filename, fitsErr);
+            psError(__func__, "Could not index to HDU #%d for file %s. (%s)", extnum, filename, fitsErr);
             psFree(output);
             return NULL;
@@ -82,6 +82,5 @@
         status = 0;
         (void)fits_close_file(fptr, &status);
-        psError("Could not determine image data type of '%s'. (%s)",
-                filename, fitsErr);
+        psError("Could not determine image data type of '%s'. (%s)", filename, fitsErr);
         psFree(output);
         return NULL;
@@ -93,6 +92,5 @@
         status = 0;
         (void)fits_close_file(fptr, &status);
-        psError("Could not determine dimensions of '%s'. (%s)",
-                filename,fitsErr);
+        psError("Could not determine dimensions of '%s'. (%s)", filename, fitsErr);
         psFree(output);
         return NULL;
@@ -100,20 +98,18 @@
 
     /* 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);
-        psFree(output);
-        return NULL;
-    }
-
-    /* Get the Image size from the FITS file  */
-    if ( fits_get_img_size(fptr, nAxis, nAxes, &status) != 0) {
+    if ((nAxis < 2) || (nAxis > 3)) {
+        status = 0;
+        (void)fits_close_file(fptr, &status);
+        psError("Dimensions of '%s' are not supported (NAXIS=%i).", filename, nAxis);
+        psFree(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'. (%s)",
-                filename,fitsErr);
+        psError("Could not determine image size of '%s'. (%s)", filename, fitsErr);
         psFree(output);
         return NULL;
@@ -127,11 +123,11 @@
     }
 
-    firstPixel[0] = col+1;
-    firstPixel[1] = row+1;
-    firstPixel[2] = z+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;
+    lastPixel[2] = z + 1;
 
     increment[0] = 1;
@@ -139,6 +135,8 @@
     increment[2] = 1;
 
-    // turn off the BSCALE/BZERO processing in CFITSIO
-    // (void)fits_set_bscale(fptr, 1.0,0.0,&status);
+    // turn off the BSCALE/BZERO processing in
+    // CFITSIO
+    // (void)fits_set_bscale(fptr,
+    // 1.0,0.0,&status);
 
     switch (bitPix) {
@@ -180,18 +178,17 @@
         break;
     default:
-        psError(__func__,"Unsupported bitpix value (%d) in FITS file %s.",
-                bitPix,filename);
-        psFree(output);
-        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) {
+        psError(__func__, "Unsupported bitpix value (%d) in FITS file %s.", bitPix, filename);
+        psFree(output);
+        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) {
         psFree(output);
         (void)fits_get_errstatus(status, fitsErr);
         status = 0;
         (void)fits_close_file(fptr, &status);
-        psError(__func__,"Failed to read image [%s]",
-                filename, fitsErr);
+        psError(__func__, "Failed to read image [%s]", filename, fitsErr);
         return NULL;
     }
@@ -202,28 +199,26 @@
 }
 
-
-bool psImageWriteSection(psImage* input, int col0,int row0,int z,
-                         char* extname, int extnum, char* filename)
+bool psImageWriteSection(psImage * input, int col0, int row0, int z, 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.) */
-    double      bscale = 1.0;
-    double      bzero = 0.0;
-    bool        createNewHDU = false;
+    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.) */
+    double bscale = 1.0;
+    double bzero = 0.0;
+    bool createNewHDU = false;
 
     /* need a valid image to write */
-    if(input==NULL) {
-        psError(__func__, "Can not write %s.  Input psImage is NULL.",
-                filename);
+    if (input == NULL) {
+        psError(__func__, "Can not write %s.  Input psImage is NULL.", filename);
         return false;
     }
@@ -244,5 +239,5 @@
     case PS_TYPE_U16:
         bitPix = SHORT_IMG;
-        bzero = -1.0f*INT16_MIN;
+        bzero = -1.0f * INT16_MIN;
         datatype = TUSHORT;
         break;
@@ -253,5 +248,5 @@
     case PS_TYPE_U32:
         bitPix = LONG_IMG;
-        bzero = -1.0f*INT32_MIN;
+        bzero = -1.0f * INT32_MIN;
         datatype = TUINT;
         break;
@@ -269,16 +264,16 @@
         break;
     default:
-        psError(__func__, "psImage datatype (%d) not supported.  File %s not written.",
-                input->type.type,filename);
+        psError(__func__,
+                "psImage datatype (%d) not supported.  File %s not written.", input->type.type, filename);
         return false;
     }
 
     /* Open the FITS file */
-    if (access(filename, F_OK) == 0) { // file exists
+    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);
+            fits_get_errstatus(status, fitsErr);
+            psError(__func__, "Could not open file '%s'. FITS error:%s", filename, fitsErr);
             return false;
         }
@@ -286,42 +281,42 @@
         /* find the specified extension */
         if (extname != NULL) {
-            if (fits_movnam_hdu(fptr, hduType, extname, 0,&status) != 0) {
+            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. (%s)",
-                        extname, filename, fitsErr);
+                psError(__func__,
+                        "Could not index to '%s' HDU for file %s. (%s)", extname, filename, fitsErr);
                 return false;
             }
         } else {
             int numHDUs = 0;
-            fits_get_num_hdus(fptr,&numHDUs,&status);
+
+            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);
+                psError(__func__,
+                        "extnum (%d) must not exceed number of HDUs (%d) by more than one.", extnum, numHDUs);
                 return false;
-            } 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. (%s)",
-                                extnum, filename, fitsErr);
-                        return false;
-                    }
-        }
-
-    } else { // file does not exist
-
-        (void)fits_create_file(&fptr,filename,&status);
+            } 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. (%s)", extnum, filename, fitsErr);
+                return false;
+            }
+        }
+
+    } else {                               // file
+        // does
+        // not
+        // exist
+
+        (void)fits_create_file(&fptr, filename, &status);
         if (fptr == NULL || status != 0) {
-            fits_get_errstatus(status,fitsErr);
-            psError(__func__,"Could not create file '%s'. (%s)",
-                    filename, fitsErr);
+            fits_get_errstatus(status, fitsErr);
+            psError(__func__, "Could not create file '%s'. (%s)", filename, fitsErr);
             return false;
         }
@@ -330,21 +325,19 @@
 
     if (createNewHDU) {
-        /*  create the mandatory image keywords */
-        nAxes[0] = col0+numCols;
-        nAxes[1] = row0+numRows;
-        nAxes[2] = z+1;
+        /* create the mandatory image keywords */
+        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;
             (void)fits_close_file(fptr, &status);
-            psError(__func__,"Could not create image HDU in FITS file '%s'. %s",
-                    filename, fitsErr);
+            psError(__func__, "Could not create image HDU in FITS file '%s'. %s", filename, fitsErr);
             return false;
         }
-
         // set the bscale/bzero
-        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);
+        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);
 
         if (extname != NULL) {
@@ -354,6 +347,6 @@
                 status = 0;
                 (void)fits_close_file(fptr, &status);
-                psError(__func__,"Could not create EXTNAME keyword in FITS file '%s'. (%s)",
-                        filename, fitsErr);
+                psError(__func__,
+                        "Could not create EXTNAME keyword in FITS file '%s'. (%s)", filename, fitsErr);
                 return false;
             }
@@ -361,18 +354,17 @@
     }
 
-    firstPixel[0] = col0+1;
-    firstPixel[1] = row0+1;
-    firstPixel[2] = z+1;
+    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[0], &status) != 0) {
+    lastPixel[2] = z + 1;
+
+    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'. (%s)",
-                filename, fitsErr);
+        psError(__func__, "Could not write image data to '%s'. (%s)", filename, fitsErr);
         return false;
     }
