Index: trunk/psLib/src/fits/psFitsImage.c
===================================================================
--- trunk/psLib/src/fits/psFitsImage.c	(revision 10999)
+++ trunk/psLib/src/fits/psFitsImage.c	(revision 15179)
@@ -7,6 +7,6 @@
  *  @author Robert DeSonia, MHPCC
  *
- *  @version $Revision: 1.17 $ $Name: not supported by cvs2svn $
- *  @date $Date: 2007-01-09 22:38:52 $
+ *  @version $Revision: 1.18 $ $Name: not supported by cvs2svn $
+ *  @date $Date: 2007-10-03 21:27:21 $
  *
  *  Copyright 2004-2005 Maui High Performance Computing Center, University of Hawaii
@@ -38,6 +38,5 @@
 
 // Information required to read a FITS file
-typedef struct
-{
+typedef struct {
     int nAxis;                          // Number of axes
     int bitPix;                         // Bits per pixel
@@ -48,6 +47,5 @@
     int fitsDatatype;                   // cfitsio data type
     int psDatatype;                     // psLib data type
-}
-p_psFitsReadInfo;
+} p_psFitsReadInfo;
 
 static p_psFitsReadInfo *p_psFitsReadInfoAlloc(const psFits *fits, // The FITS file handle
@@ -56,5 +54,5 @@
                                               )
 {
-    PS_ASSERT_PTR_NON_NULL(fits, NULL);
+    PS_ASSERT_FITS_NON_NULL(fits, NULL);
     PS_ASSERT_INT_NONNEGATIVE(z, NULL);
 
@@ -221,4 +219,13 @@
                         )
 {
+    PS_ASSERT_FITS_NON_NULL(fits, NULL);
+    PS_ASSERT_INT_NONNEGATIVE(z, NULL);
+
+    if (psFitsCheckSingleCompressedImagePHU(fits, NULL)) {
+        // This is really what we want, not the empty PHU
+        psTrace("psLib.fits", 1,
+                "This PHU should really be a compressed image --- reading that image instead.");
+    }
+
     p_psFitsReadInfo *info = p_psFitsReadInfoAlloc(fits, region, z);
 
@@ -243,7 +250,16 @@
                               )
 {
+    PS_ASSERT_FITS_NON_NULL(fits, NULL);
+    PS_ASSERT_INT_NONNEGATIVE(z, NULL);
+
     if (output && output->parent) {
         psError(PS_ERR_IO, true, "Unable to read into a buffer for a child image.\n");
         return NULL;
+    }
+
+    if (psFitsCheckSingleCompressedImagePHU(fits, NULL)) {
+        // This is really what we want, not the empty PHU
+        psTrace("psLib.fits", 1,
+                "This PHU should really be a compressed image --- reading that image instead.");
     }
 
@@ -270,4 +286,6 @@
                       const char* extname)
 {
+    PS_ASSERT_FITS_NON_NULL(fits, false);
+    PS_ASSERT_IMAGE_NON_NULL(input, false);
     // this is equivalent to insert after the last HDU
 
@@ -283,14 +301,6 @@
                        bool after)
 {
-
-    if (!fits) {
-        psError(PS_ERR_BAD_PARAMETER_NULL, true, _("The input psFits object can not NULL."));
-        return false;
-    }
-
-    if (!input) {
-        psError(PS_ERR_BAD_PARAMETER_NULL, true, _("The input psImage was NULL.  Need a non-NULL psImage for operation to be performed."));
-        return false;
-    }
+    PS_ASSERT_FITS_NON_NULL(fits, false);
+    PS_ASSERT_IMAGE_NON_NULL(input, false);
 
     int numCols = input->numCols;       // Number of columns for image
@@ -391,17 +401,8 @@
                        int z)
 {
+    PS_ASSERT_FITS_NON_NULL(fits, false);
+    PS_ASSERT_IMAGE_NON_NULL(input, false);
+
     int status = 0;
-
-    if (fits == NULL) {
-        psError(PS_ERR_BAD_PARAMETER_NULL, true,
-                _("The input psFits object can not NULL."));
-        return false;
-    }
-
-    if (input == NULL) {
-        psError(PS_ERR_BAD_PARAMETER_NULL, true,
-                _("The input psImage was NULL.  Need a non-NULL psImage for operation to be performed."));
-        return false;
-    }
 
     // check to see if we are positioned on an image HDU
@@ -499,4 +500,6 @@
 psArray *psFitsReadImageCube(const psFits *fits, psRegion region)
 {
+    PS_ASSERT_FITS_NON_NULL(fits, NULL);
+
     int nAxis = 0;                      // Number of axes
     long nAxes[3];                      // Number of pixels on each axis
@@ -504,12 +507,12 @@
     char fitsErr[80] = "";              // CFITSIO error message string
 
-    if (fits == NULL) {
-        psError(PS_ERR_BAD_PARAMETER_NULL, true,
-                _("The input psFits object can not NULL."));
-        return NULL;
-    }
-
     // Some of this replicates what is in psFitsReadImage, so it's a little inefficient.  But it saves
     // code replication, and should be sufficient for our needs.
+
+    if (psFitsCheckSingleCompressedImagePHU(fits, NULL)) {
+        // This is really what we want, not the empty PHU
+        psTrace("psLib.fits", 1,
+                "This PHU should really be a compressed image --- reading that image instead.");
+    }
 
     if (fits_get_img_dim(fits->fd, &nAxis, &status) != 0) {
@@ -544,5 +547,7 @@
 
     // Bad dimensionality
-    psError(PS_ERR_IO, true, _("Image number of dimensions, %d, is not valid.  Only two or three dimensions supported for FITS I/O."), nAxis);
+    psError(PS_ERR_IO, true,
+            _("Image number of dimensions, %d, is not valid."
+              " Only two or three dimensions supported for FITS I/O."), nAxis);
     return NULL;
 }
@@ -550,15 +555,6 @@
 bool psFitsWriteImageCube(psFits *fits, psMetadata *header, const psArray *input, const char *extname)
 {
-    if (fits == NULL) {
-        psError(PS_ERR_BAD_PARAMETER_NULL, true,
-                _("The input psFits object can not NULL."));
-        return false;
-    }
-
-    if (input == NULL) {
-        psError(PS_ERR_BAD_PARAMETER_NULL, true,
-                _("The input psImage was NULL.  Need a non-NULL psImage for operation to be performed."));
-        return false;
-    }
+    PS_ASSERT_FITS_NON_NULL(fits, false);
+    PS_ASSERT_ARRAY_NON_NULL(input, false);
 
     if (input->n == 0) {
@@ -592,8 +588,8 @@
     }
     bool update = psMetadataAddS32(headerCopy, PS_LIST_HEAD, "NAXIS", PS_META_REPLACE, "Dimensionality", 3) &&
-                  psMetadataAddS32(headerCopy, PS_LIST_HEAD, "NAXIS1", PS_META_REPLACE, "Number of columns", numCols) &&
-                  psMetadataAddS32(headerCopy, PS_LIST_HEAD, "NAXIS2", PS_META_REPLACE, "Number of rows", numRows) &&
-                  psMetadataAddS32(headerCopy, PS_LIST_HEAD, "NAXIS3", PS_META_REPLACE, "Number of image planes",
-                                   input->n);
+        psMetadataAddS32(headerCopy, PS_LIST_HEAD, "NAXIS1", PS_META_REPLACE, "Number of columns", numCols) &&
+        psMetadataAddS32(headerCopy, PS_LIST_HEAD, "NAXIS2", PS_META_REPLACE, "Number of rows", numRows) &&
+        psMetadataAddS32(headerCopy, PS_LIST_HEAD, "NAXIS3", PS_META_REPLACE, "Number of image planes",
+                         input->n);
     if (! update) {
         psError(PS_ERR_UNKNOWN, false, _("Failed to add metadata item, %s."),
@@ -625,15 +621,6 @@
 bool psFitsUpdateImageCube(psFits *fits, const psArray *input, int x0, int y0)
 {
-    if (fits == NULL) {
-        psError(PS_ERR_BAD_PARAMETER_NULL, true,
-                _("The input psFits object can not NULL."));
-        return false;
-    }
-
-    if (input == NULL) {
-        psError(PS_ERR_BAD_PARAMETER_NULL, true,
-                _("The input psImage was NULL.  Need a non-NULL psImage for operation to be performed."));
-        return false;
-    }
+    PS_ASSERT_FITS_NON_NULL(fits, false);
+    PS_ASSERT_ARRAY_NON_NULL(input, false);
 
     if (input->n == 0) {
