Index: trunk/psLib/src/fits/psFitsTable.c
===================================================================
--- trunk/psLib/src/fits/psFitsTable.c	(revision 6767)
+++ trunk/psLib/src/fits/psFitsTable.c	(revision 7230)
@@ -7,10 +7,11 @@
  *  @author Robert DeSonia, MHPCC
  *
- *  @version $Revision: 1.7 $ $Name: not supported by cvs2svn $
- *  @date $Date: 2006-04-04 19:52:42 $
+ *  @version $Revision: 1.8 $ $Name: not supported by cvs2svn $
+ *  @date $Date: 2006-05-27 01:50:33 $
  *
  *  Copyright 2004-2005 Maui High Performance Computing Center, University of Hawaii
  */
 
+#include "config.h"
 #include "psFits.h"
 #include "string.h"
@@ -23,5 +24,6 @@
 #include "psTrace.h"
 #include "psVector.h"
-#include "config.h"
+#include "psFitsTable.h"
+#include "psFitsHeader.h"
 
 #define MAX_STRING_LENGTH 256  // maximum length string for FITS routines
@@ -603,7 +605,13 @@
 
 bool psFitsWriteTable(psFits* fits,
-                      const psMetadata* header,
-                      const psArray* table)
+                      psMetadata* header,
+                      const psArray* table,
+                      const char *extname)
 {
+    psFitsMoveLast(fits);
+    return psFitsInsertTable(fits, header, table, extname, true);
+
+    #if 0
+
     int status = 0;
 
@@ -710,12 +718,5 @@
     }
 
-    psString extname = NULL;
-    if (header != NULL) {
-        extname = psMetadataLookupStr(NULL, header, "EXTNAME");
-        if ( extname == NULL) {
-            extname = psMetadataLookupStr(NULL, header, "HDUNAME");
-        }
-    }
-
+    // Create the table
     fits_create_tbl(fits->fd,
                     BINARY_TBL,
@@ -727,7 +728,12 @@
                     extname, // extension name
                     &status);
-
     psFree(columnNames);
     psFree(columnTypes);
+
+    // Write header
+    if (header && !psFitsWriteHeader(header, fits)) {
+        psError(PS_ERR_IO, false, "Unable to write FITS header.\n");
+        return false;
+    }
 
     // fill in the table elements with data
@@ -810,9 +816,11 @@
 
     return true;
+    #endif
 }
 
 bool psFitsInsertTable(psFits* fits,
-                       const psMetadata* header,
+                       psMetadata* header,
                        const psArray* table,
+                       const char *extname,
                        bool after)
 {
@@ -921,35 +929,47 @@
     }
 
-    psString extname = NULL;
-    if (header != NULL) {
-        extname = psMetadataLookupStr(NULL, header, "EXTNAME");
-        if ( extname == NULL) {
-            extname = psMetadataLookupStr(NULL, header, "HDUNAME");
-        }
-    }
-
-    if (! after) {
-        if (psFitsGetExtNum(fits) == 0) {
-            // set status to signal fits_insert_img to insert a new primary HDU
-            status = PREPEND_PRIMARY;
-        } else {
-            // move back one to perform an insert after the previous HDU
-            psFitsMoveExtNum(fits, -1, true);
-        }
-    }
-
-    fits_insert_btbl(fits->fd,
-                     table->n, // number of rows in table
-                     columns->n, // number of columns in table
-                     (char**)columnNames->data, // names of the columns
-                     (char**)columnTypes->data, // format of the columns
-                     NULL, // physical unit of columns
-                     extname, // extension name
-                     0,
-                     &status);
-
+    // Create the table HDU
+    int hdus = psFitsGetSize(fits);     // Number of HDUs in file
+    if (hdus == 0) {
+        // We're creating the first extension
+        fits_create_tbl(fits->fd,
+                        BINARY_TBL,
+                        table->n, // number of rows in table
+                        columns->n, // number of columns in table
+                        (char**)columnNames->data, // names of the columns
+                        (char**)columnTypes->data, // format of the columns
+                        NULL, // physical unit of columns
+                        (char*)extname, // extension name; casting away const because cfitsio is horrible
+                        &status);
+    } else {
+        if (!after) {
+            if (psFitsGetExtNum(fits) == 0) {
+                // We're creating a replacement primary HDU.
+                // Set status to signal fits_insert_img to insert a new primary HDU
+                status = PREPEND_PRIMARY;
+            } else {
+                // Move back one to perform an insert after the previous HDU
+                psFitsMoveExtNum(fits, -1, true);
+            }
+        }
+        // Insert the table
+        fits_insert_btbl(fits->fd,
+                         table->n, // number of rows in table
+                         columns->n, // number of columns in table
+                         (char**)columnNames->data, // names of the columns
+                         (char**)columnTypes->data, // format of the columns
+                         NULL, // physical unit of columns
+                         (char*)extname, // extension name; casting away const because cfitsio is horrible
+                         0,
+                         &status);
+    }
     psFree(columnNames);
     psFree(columnTypes);
 
+    // Write header
+    if (header && !psFitsWriteHeader(header, fits)) {
+        psError(PS_ERR_IO, false, "Unable to write FITS header.\n");
+        return false;
+    }
 
     // fill in the table elements with data
