Index: /branches/pap_branch_070920/psLib/src/fits/psFitsHeader.c
===================================================================
--- /branches/pap_branch_070920/psLib/src/fits/psFitsHeader.c	(revision 14984)
+++ /branches/pap_branch_070920/psLib/src/fits/psFitsHeader.c	(revision 14985)
@@ -7,6 +7,6 @@
  *  @author Robert DeSonia, MHPCC
  *
- *  @version $Revision: 1.34 $ $Name: not supported by cvs2svn $
- *  @date $Date: 2007-08-10 02:23:42 $
+ *  @version $Revision: 1.34.4.1 $ $Name: not supported by cvs2svn $
+ *  @date $Date: 2007-09-22 03:08:05 $
  *
  *  Copyright 2004-2005 Maui High Performance Computing Center, University of Hawaii
@@ -19,4 +19,5 @@
 #include <unistd.h>
 
+#include "psAssert.h"
 #include "psFits.h"
 #include "string.h"
@@ -32,18 +33,68 @@
 
 #define MAX_STRING_LENGTH 256  // maximum length string for FITS routines
+#define NUM_EMPTY_KEYS 8                // Number of keywords before header is considered practically empty
 
 // list of FITS header keys to ignore; NULL-terminated
-static char* ignoreFitsKeys[] = {"", NULL};
+static const char* ignoreFitsKeys[] = {"", NULL};
 
 // List of FITS header keys that may be duplicated; NULL-terminated
-static char *duplicateFitsKeys[] = {"COMMENT", "HIERARCH", "HISTORY", NULL};
+static const char *duplicateFitsKeys[] = {"COMMENT", "HIERARCH", "HISTORY", NULL};
 
 // List of FITS header keys not to write (handled by cfitsio); NULL-terminated
-static char *noWriteFitsKeys[] = {"SIMPLE", "XTENSION", "BITPIX", "NAXIS", "EXTNAME", "BSCALE", "BZERO",
-                                  "TFIELDS", NULL};
+static const char *noWriteFitsKeys[] = {"SIMPLE", "XTENSION", "BITPIX", "NAXIS", "EXTNAME", "BSCALE", "BZERO",
+                                        "TFIELDS", NULL};
+
 // List of the start of FITS header keys not to write (handled by cfitsio); NULL-terminated
-static char *noWriteFitsKeyStarts[] = {"NAXIS", "TTYPE", "TFORM", NULL};
-
-// List of FITS header keys to be written with fits_write_comment
+static const char *noWriteFitsKeyStarts[] = {"NAXIS", "TTYPE", "TFORM", NULL};
+
+// List of FITS header keys that may be present if the header is considered "empty"; NULL-terminated
+static const char *emptyKeys[] = { "SIMPLE", "BITPIX", "NAXIS", "EXTEND", "COMMENT", "CHECKSUM", "DATASUM",
+                                   NULL };
+
+// Compare a keyword with a list of keywords; return true if it's in the list
+static bool keywordInList(const char *keyword, // Keyword to check
+                          const char **list // List of keywords
+    )
+{
+    for (const char **check = list; *check; *check++) {
+        if (strcmp(keyword, *check) == 0) {
+            return true;
+        }
+    }
+    return false;
+}
+
+
+bool psFitsEmptyPHU(const psFits *fits, const psMetadata *header)
+{
+    PS_ASSERT_PTR_NON_NULL(fits, false);
+    PS_ASSERT_METADATA_NON_NULL(header, false);
+
+    int hduNum = 0;                     // Which HDU we're at
+    fits_get_hdu_num(fits->fd, &hduNum);  /* Get the current output HDU position */
+
+    if (hduNum != 1) {
+        // It's not the PHU, so it can't be an empty PHU!
+        return false;
+    }
+
+    if (header->list->n == 0) {
+        // There's nothing in the list
+        return true;
+    }
+
+    // Check if it's got any keywords of potential interest
+    psMetadataIterator *iter = psMetadataIteratorAlloc(header, PS_LIST_HEAD, NULL); // Iterator
+    psMetadataItem *item;               // Item from iteration
+    while ((item = psMetadataGetAndIncrement(iter))) {
+        if (!keywordInList(item->name, emptyKeys)) {
+            psFree(iter);
+            return false;
+        }
+    }
+
+    return true;
+}
+
 
 psMetadata* psFitsReadHeader(psMetadata* out,
@@ -74,30 +125,13 @@
 
         // Check to see if the keyword should be ignored
-        bool ignoreKey = false;         // Ignore this keyword?
-        for (int i = 0; ignoreFitsKeys[i] && !ignoreKey; i++) {
-            if (strcmp(keyName, ignoreFitsKeys[i]) == 0) {
-                ignoreKey = true;
-            }
-        }
-        if (ignoreKey) {
+        if (keywordInList(keyName, ignoreFitsKeys)) {
             // We're done here; skip to the next key
             continue;
         }
 
-#if 0
-        // This doesn't seem to be necessary
-        if (strncmp(keyName, "HIERARCH ", 9) == 0) {
-            char temp[MAX_STRING_LENGTH];
-            strcpy(temp, &keyName[9]);
-            strcpy(keyName, temp);
-        }
-#endif
-
         // Check to see if the keyword should be duplicated
         int dupFlag = 0;                // Duplicate flag
-        for (int i = 0; duplicateFitsKeys[i] && !dupFlag; i++) {
-            if (strcmp(keyName, duplicateFitsKeys[i]) == 0) {
-                dupFlag = PS_META_DUPLICATE_OK;
-            }
+        if (keywordInList(keyName, duplicateFitsKeys)) {
+            dupFlag = PS_META_DUPLICATE_OK;
         }
 
@@ -279,11 +313,5 @@
             // image, the NAXISn haven't been changed; or after converting to F32, the BITPIX hasn't been
             // changed) so we'll take care of that for them.
-            bool writeKey = true;           // Should we write this keyword?
-            for (int i = 0; noWriteFitsKeys[i] && writeKey; i++) {
-                if (strcmp(item->name, noWriteFitsKeys[i]) == 0) {
-                    writeKey = false;
-                }
-            }
-            if (!writeKey) {
+            if (keywordInList(item->name, noWriteFitsKeys)) {
                 // Don't write it; skip to the next key
                 continue;
@@ -294,4 +322,5 @@
                 // that go in are correct.  However, when we're writing a "blank" HDU (header only), we want
                 // to preserve NAXISn etc for reference, so we don't do this.
+                bool writeKey = true;   // Write this keyword?
                 for (int i = 0; noWriteFitsKeyStarts[i] && writeKey; i++) {
                     if (strncmp(item->name, noWriteFitsKeyStarts[i], strlen(noWriteFitsKeyStarts[i])) == 0) {
Index: /branches/pap_branch_070920/psLib/src/fits/psFitsHeader.h
===================================================================
--- /branches/pap_branch_070920/psLib/src/fits/psFitsHeader.h	(revision 14984)
+++ /branches/pap_branch_070920/psLib/src/fits/psFitsHeader.h	(revision 14985)
@@ -4,6 +4,6 @@
  * @author Robert DeSonia, MHPCC
  *
- * @version $Revision: 1.10 $ $Name: not supported by cvs2svn $
- * @date $Date: 2007-01-23 22:47:23 $
+ * @version $Revision: 1.10.14.1 $ $Name: not supported by cvs2svn $
+ * @date $Date: 2007-09-22 03:08:05 $
  *
  * Copyright 2004-2005 Maui High Performance Computing Center, University of Hawaii
@@ -18,4 +18,10 @@
 #include "psFits.h"
 #include "psMetadata.h"
+
+/// Determines whether the current HDU is an empty PHU
+bool psFitsEmptyPHU(const psFits *fits, ///< FITS file pointer
+                    const psMetadata *header ///< Header
+    );
+
 
 /** Reads the header of the current HDU.
