Index: /branches/pap_branch_070920/psLib/src/fits/psFits.c
===================================================================
--- /branches/pap_branch_070920/psLib/src/fits/psFits.c	(revision 14983)
+++ /branches/pap_branch_070920/psLib/src/fits/psFits.c	(revision 14984)
@@ -7,6 +7,6 @@
  *  @author Robert DeSonia, MHPCC
  *
- *  @version $Revision: 1.71 $ $Name: not supported by cvs2svn $
- *  @date $Date: 2007-09-18 03:01:17 $
+ *  @version $Revision: 1.71.2.1 $ $Name: not supported by cvs2svn $
+ *  @date $Date: 2007-09-22 03:06:16 $
  *
  *  Copyright 2004-2005 Maui High Performance Computing Center, University of Hawaii
@@ -65,5 +65,5 @@
             return false;
         }
-	fits->fd = NULL;
+        fits->fd = NULL;
     }
     return true;
@@ -74,5 +74,5 @@
     if (!fits) return;
     if (fits->fd) {
-	fitsClose(fits);
+        fitsClose(fits);
     }
     psFree (fits->extword);
@@ -182,4 +182,33 @@
 }
 
+psErrorCode p_psFitsError(const char* filename,
+                          unsigned int lineno,
+                          const char* func,
+                          int status,
+                          bool new,
+                          const char *errorMsg,
+                          ...
+                          )
+{
+    if (status == 0) {
+        return PS_ERR_NONE;
+    }
+
+    va_list ap;                         // Variable arguments
+    va_start(ap, errorMsg);
+    psString msg = NULL;                // Message to pass to psError
+    psStringAppendV(&msg, errorMsg, ap);
+    va_end(ap);
+
+    char cfitsioMsg[MAX_STRING_LENGTH];   // Error message from cfitsio
+    (void)fits_get_errstatus(status, cfitsioMsg);
+
+    psStringAppend(&msg, "[CFITSIO error: %s]", cfitsioMsg);
+
+    psErrorCode code = p_psError(filename, lineno, func, PS_ERR_IO, new, msg); // Error code
+    psFree(msg);
+    return code;
+}
+
 static void psFitsOptionsFree(psFitsOptions *opt)
 {
@@ -227,6 +256,6 @@
 // a user-defined word in place of EXTNAME
 bool p_psFitsMoveExtName_UserKey(const psFits *fits,
-				 const char *extname,
-				 const char *extword)
+                                 const char *extname,
+                                 const char *extword)
 {
     PS_ASSERT_PTR_NON_NULL(fits,    false);
@@ -242,27 +271,27 @@
     // NOTE: fits_* return 0 for success
     for (int i = 1; true; i++) {
-	// are we able to read the next HDU?
-
-	int status = 0;
-	if (fits_movabs_hdu(fits->fd, i, &hdutype, &status)) {
-	    char fitsErr[MAX_STRING_LENGTH];
-	    fits_get_errstatus(status, fitsErr);
-	    psError(PS_ERR_LOCATION_INVALID, true,
-		    _("Could not find HDU with %s = '%s'. CFITSIO Error: %s"),
-		    extword, extname, fitsErr);
-	    return false;
-	}
-	// is there a keyword called 'extword'? (read as string regardless of type)
-	status = 0;
-	if (fits_read_keyword(fits->fd, (char *)extword, name, NULL, &status)) {
-	    continue;
-	}
-	// if this was read as a string, we will have leading and trailing single-quotes
-	// try both for comparison
-	
-	// do we have the right hdu (names match)?
-	if (!strcmp (name, extname) || !strcmp (name, extstring)) {
-	    return true;
-	}
+        // are we able to read the next HDU?
+
+        int status = 0;
+        if (fits_movabs_hdu(fits->fd, i, &hdutype, &status)) {
+            char fitsErr[MAX_STRING_LENGTH];
+            fits_get_errstatus(status, fitsErr);
+            psError(PS_ERR_LOCATION_INVALID, true,
+                    _("Could not find HDU with %s = '%s'. CFITSIO Error: %s"),
+                    extword, extname, fitsErr);
+            return false;
+        }
+        // is there a keyword called 'extword'? (read as string regardless of type)
+        status = 0;
+        if (fits_read_keyword(fits->fd, (char *)extword, name, NULL, &status)) {
+            continue;
+        }
+        // if this was read as a string, we will have leading and trailing single-quotes
+        // try both for comparison
+
+        // do we have the right hdu (names match)?
+        if (!strcmp (name, extname) || !strcmp (name, extstring)) {
+            return true;
+        }
     }
     psAbort("we should not reach here");
@@ -290,6 +319,6 @@
 
     if (fits->extword != NULL) {
-	bool result = p_psFitsMoveExtName_UserKey(fits, extname, fits->extword);
-	return (result);
+        bool result = p_psFitsMoveExtName_UserKey(fits, extname, fits->extword);
+        return (result);
     }
 
@@ -389,5 +418,5 @@
         psError(PS_ERR_BAD_PARAMETER_NULL, true,
                 _("Header keyword %s is not found"), extword);
-	return NULL;
+        return NULL;
     }
     return psStringCopy(name);
Index: /branches/pap_branch_070920/psLib/src/fits/psFits.h
===================================================================
--- /branches/pap_branch_070920/psLib/src/fits/psFits.h	(revision 14983)
+++ /branches/pap_branch_070920/psLib/src/fits/psFits.h	(revision 14984)
@@ -4,6 +4,6 @@
  * @author Robert DeSonia, MHPCC
  *
- * @version $Revision: 1.31 $ $Name: not supported by cvs2svn $
- * @date $Date: 2007-09-18 02:56:36 $
+ * @version $Revision: 1.31.2.1 $ $Name: not supported by cvs2svn $
+ * @date $Date: 2007-09-22 03:06:16 $
  * Copyright 2004-2005 Maui High Performance Computing Center, University of Hawaii
  */
@@ -21,4 +21,6 @@
 #include "psMetadata.h"
 #include "psImage.h"
+
+#include "psErrorCodes.h"
 
 /** FITS HDU type.
@@ -51,7 +53,7 @@
 typedef struct
 {
-    fitsfile* fd;			///< the CFITSIO fits files handle.
-    bool writable;			///< Is the file writable?
-    char *extword;			///< user-specified word to name extensions (NULL implies EXTNAME)
+    fitsfile* fd;                       ///< the CFITSIO fits files handle.
+    bool writable;                      ///< Is the file writable?
+    char *extword;                      ///< user-specified word to name extensions (NULL implies EXTNAME)
 }
 psFits;
@@ -84,5 +86,31 @@
 );
 
-/** Creates a new FITS options struct 
+
+/// Generate an error including the cfitsio error string
+#ifdef DOXYGEN
+psErrorCode psFitsError(
+    int status,                         ///< cfitsio status value
+    bool new,                           ///< new error?
+    const char *errorMsg,               ///< printf-style format of header line
+    ...                                 ///< any parameters required in format
+    );
+#else // ifdef DOXYGEN
+psErrorCode p_psFitsError(
+    const char* filename,               ///< file name
+    unsigned int lineno,                ///< line number in file
+    const char* func,                   ///< function name
+    int status,                         ///< cfitsio status value
+    bool new,                           ///< new error?
+    const char *errorMsg,               ///< printf-style format of header line
+    ...                                 ///< any parameters required in format
+    ) PS_ATTR_FORMAT(printf, 6, 7);
+#ifndef SWIG
+#define psFitsError(status,new,...) \
+      p_psFitsError(__FILE__,__LINE__,__func__,status,new,__VA_ARGS__)
+#endif // ifndef SWIG
+#endif // ifdef DOXYGEN
+
+
+/** Creates a new FITS options struct
  *
  *  @return psFitsOptions or NULL on failure
@@ -146,6 +174,6 @@
 // a user-defined word in place of EXTNAME
 bool p_psFitsMoveExtName_UserKey(const psFits *fits,
-				 const char *extname,
-				 const char *extword);
+                                 const char *extname,
+                                 const char *extword);
 
 /** Moves the FITS HDU to the specified extension name.
