Index: trunk/psLib/src/fits/psFitsTable.c
===================================================================
--- trunk/psLib/src/fits/psFitsTable.c	(revision 28511)
+++ trunk/psLib/src/fits/psFitsTable.c	(revision 28580)
@@ -69,8 +69,7 @@
 }
 
-
-// Check the FITS file in preparation for reading a table
-static bool readTableCheck(const psFits *fits // FITS file
-                           )
+// Check if the FITS file is an empty table
+static bool emptyTableCheck(const psFits *fits // FITS file
+                            )
 {
     PS_ASSERT_FITS_NON_NULL(fits, false);
@@ -81,9 +80,9 @@
     }
 
-    // check that we are positioned on a table HDU
     int status = 0;                     // CFITSIO status
     int hdutype;                        // Type of HDU
     fits_get_hdu_type(fits->fd, &hdutype, &status);
-    if (psFitsError(status, true, "Could not determine the HDU type.")) {
+    if (status) {
+        psFitsError(status, true, "Could not determine the HDU type.");
         return false;
     }
@@ -99,6 +98,30 @@
             return false;
         }
-    } else if (hdutype != ASCII_TBL && hdutype != BINARY_TBL) {
-        psError(PS_ERR_BAD_FITS, true, _("Current FITS HDU is not a table."));
+        return true;
+    }
+
+    return false;
+}
+
+// Check the FITS file in preparation for reading a table
+static bool readTableCheck(const psFits *fits // FITS file
+                           )
+{
+    PS_ASSERT_FITS_NON_NULL(fits, false);
+
+    if (psFitsGetExtNum(fits) == 0 && !psFitsMoveExtNum(fits, 1, false)) {
+        psError(PS_ERR_IO, false, "Unable to move to first extension to read table.");
+        return false;
+    }
+
+    // check that we are positioned on a table HDU
+    int status = 0;                     // CFITSIO status
+    int hdutype;                        // Type of HDU
+    fits_get_hdu_type(fits->fd, &hdutype, &status);
+    if (status) {
+        psFitsError(status, true, "Could not determine the HDU type.");
+        return false;
+    }
+    if (hdutype != ASCII_TBL && hdutype != BINARY_TBL) {
         return false;
     }
@@ -114,4 +137,8 @@
 
     if (!readTableCheck(fits)) {
+        if (emptyTableCheck(fits)) {
+            return psMetadataAlloc();
+        }
+        psError(PS_ERR_BAD_FITS, true, "Extension is not a table");
         return NULL;
     }
@@ -123,5 +150,5 @@
     fits_get_num_rows(fits->fd, &numRows, &status);
     fits_get_num_cols(fits->fd, &numCols, &status);
-    if (status != 0) {
+    if (status) {
         psFitsError(status, true, "Failed to determine the size of the current HDU table.");
         return NULL;
@@ -236,4 +263,8 @@
 
     if (!readTableCheck(fits)) {
+        if (emptyTableCheck(fits)) {
+            return psArrayAlloc(0);
+        }
+        psError(PS_ERR_BAD_FITS, true, "Extension is not a table");
         return NULL;
     }
@@ -283,4 +314,5 @@
 
     if (!readTableCheck(fits)) {
+        psError(PS_ERR_BAD_FITS, true, "Extension is not a table");
         return NULL;
     }
@@ -327,4 +359,8 @@
 
     if (!readTableCheck(fits)) {
+        if (emptyTableCheck(fits)) {
+            return psArrayAlloc(0);
+        }
+        psError(PS_ERR_BAD_FITS, true, "Extension is not a table");
         return NULL;
     }
@@ -736,4 +772,5 @@
 
     if (!readTableCheck(fits)) {
+        psError(PS_ERR_BAD_FITS, true, "Extension is not a table");
         return NULL;
     }
@@ -785,4 +822,8 @@
 
     if (!readTableCheck(fits)) {
+        if (emptyTableCheck(fits)) {
+            return psMetadataAlloc();
+        }
+        psError(PS_ERR_BAD_FITS, true, "Extension is not a table");
         return NULL;
     }
@@ -794,5 +835,6 @@
     fits_get_num_rows(fits->fd, &numRows, &status);
     fits_get_num_cols(fits->fd, &numCols, &status);
-    if (psFitsError(status, true, "Failed to determine the size of the current HDU table.")) {
+    if (status) {
+        psFitsError(status, true, "Failed to determine the size of the current HDU table.");
         return NULL;
     }
@@ -800,5 +842,6 @@
     int hdutype;                        // Type of HDU: need to distinguish ASCII and binary tables
     fits_get_hdu_type(fits->fd, &hdutype, &status);
-    if (psFitsError(status, true, "Could not determine the HDU type.")) {
+    if (status) {
+        psFitsError(status, true, "Could not determine the HDU type.");
         return false;
     }
@@ -818,5 +861,6 @@
         long repeat;                     // Number of repeats
         fits_get_eqcoltype(fits->fd, col, &cfitsioType, &repeat, NULL, &status);
-        if (psFitsError(status, true, "Could not determine the column data for %s", name)) {
+        if (status) {
+            psFitsError(status, true, "Could not determine the column data for %s", name);
             psFree(table);
             return false;
@@ -837,5 +881,6 @@
             }
             fits_read_col_str(fits->fd, col, 1, 1, numRows, "", (char**)array->data, NULL, &status);
-            if (psFitsError(status, true, "Failed to read column %s", name)) {
+            if (status) {
+                psFitsError(status, true, "Failed to read column %s", name);
                 psFree(array);
                 psFree(table);
@@ -854,5 +899,6 @@
             fits_read_col(fits->fd, cfitsioType, col, 1, 1, numRows, NULL,
                           vector->data.U8, NULL, &status);
-            if (psFitsError(status, true, "Failed to read column %s", name)) {
+            if (status) {
+                psFitsError(status, true, "Failed to read column %s", name);
                 psFree(vector);
                 psFree(table);
@@ -965,6 +1011,7 @@
     psFree(names);
     psFree(types);
-    if (psFitsError(status, true, "Unable to create FITS table with %d columns and %ld rows",
-                    numCols, numRows)) {
+    if (status) {
+        psFitsError(status, true, "Unable to create FITS table with %d columns and %ld rows",
+                    numCols, numRows);
         psFree(columns);
         return false;
@@ -1018,5 +1065,6 @@
         }
         // Check error status from writing column
-        if (psFitsError(status, true, "Unable to write column %ld of FITS table", col)) {
+        if (status) {
+            psFitsError(status, true, "Unable to write column %ld of FITS table", col);
             psFree(columns);
             return false;
@@ -1029,5 +1077,6 @@
     // hurt.
     ffrdef(fits->fd, &status);
-    if (psFitsError(status, true, "Could not re-scan HDU.")) {
+    if (status) {
+        psFitsError(status, true, "Could not re-scan HDU.");
         return false;
     }
