Index: trunk/external/atnf/PKSIO/SDFITSwriter.cc
===================================================================
--- trunk/external/atnf/PKSIO/SDFITSwriter.cc	(revision 1399)
+++ trunk/external/atnf/PKSIO/SDFITSwriter.cc	(revision 1452)
@@ -2,5 +2,5 @@
 //# SDFITSwriter.cc: ATNF CFITSIO interface class for SDFITS output.
 //#---------------------------------------------------------------------------
-//# Copyright (C) 2000-2007
+//# Copyright (C) 2000-2008
 //# Mark Calabretta, ATNF
 //#
@@ -27,18 +27,17 @@
 //#                        AUSTRALIA
 //#
-//# $Id: SDFITSwriter.cc,v 19.12 2007/11/12 03:37:56 cal103 Exp $
+//# $Id: SDFITSwriter.cc,v 19.15 2008-11-17 06:58:58 cal103 Exp $
 //#---------------------------------------------------------------------------
 //# Original: 2000/07/24, Mark Calabretta, ATNF
 //#---------------------------------------------------------------------------
 
+#include <atnf/PKSIO/MBrecord.h>
+#include <atnf/PKSIO/PKSmsg.h>
+#include <atnf/PKSIO/SDFITSwriter.h>
+
+#include <casa/iostream.h>
+
 #include <algorithm>
 #include <math.h>
-
-// AIPS++ includes.
-#include <casa/iostream.h>
-
-// ATNF includes.
-#include <atnf/PKSIO/PKSMBrecord.h>
-#include <atnf/PKSIO/SDFITSwriter.h>
 
 using namespace std;
@@ -55,5 +54,8 @@
 {
   // Default constructor.
-  cSDptr = 0;
+  cSDptr = 0x0;
+
+  // By default, messages are written to stderr.
+  initMsg();
 }
 
@@ -86,4 +88,12 @@
         int    extraSysCal)
 {
+  if (cSDptr) {
+    logMsg("ERROR: Output file already open, close it first.");
+    return 1;
+  }
+
+  // Clear the message stack.
+  clearMsg();
+
   // Prepend an '!' to the output name to force it to be overwritten.
   char sdname[80];
@@ -94,4 +104,6 @@
   cStatus = 0;
   if (fits_create_file(&cSDptr, sdname, &cStatus)) {
+    sprintf(cMsg, "ERROR: Failed to create SDFITS file\n       %s", sdName);
+    logMsg(cMsg);
     return cStatus;
   }
@@ -141,4 +153,5 @@
   // Write required primary header keywords.
   if (fits_write_imghdr(cSDptr, 8, 0, 0, &cStatus)) {
+    logMsg("ERROR: Failed to write required primary header keywords.");
     return cStatus;
   }
@@ -160,6 +173,6 @@
   char version[7];
   char date[11];
-  sscanf("$Revision: 19.12 $", "%*s%s", version);
-  sscanf("$Date: 2007/11/12 03:37:56 $", "%*s%s", date);
+  sscanf("$Revision: 19.15 $", "%*s%s", version);
+  sscanf("$Date: 2008-11-17 06:58:58 $", "%*s%s", date);
   sprintf(text, "SDFITSwriter (v%s, %s)", version, date);
   fits_write_key_str(cSDptr, "ORIGIN", text, "output class", &cStatus);
@@ -171,4 +184,10 @@
   fits_write_comment(cSDptr, text, &cStatus);
 
+  if (cStatus) {
+    logMsg("ERROR: Failed in writing primary header.");
+    return cStatus;
+  }
+
+
   // Create an SDFITS extension.
   long nrow = 0;
@@ -176,4 +195,5 @@
   if (fits_create_tbl(cSDptr, BINARY_TBL, nrow, ncol, NULL, NULL, NULL,
       "SINGLE DISH", &cStatus)) {
+    logMsg("ERROR: Failed to create a binary table extension.");
     return 1;
   }
@@ -524,4 +544,8 @@
   }
 
+  if (cStatus) {
+    logMsg("ERROR: Failed in writing binary table header.");
+  }
+
   return cStatus;
 }
@@ -531,5 +555,5 @@
 // Write a record to the SDFITS file.
 
-int SDFITSwriter::write(PKSMBrecord &mbrec)
+int SDFITSwriter::write(MBrecord &mbrec)
 {
   char *cptr;
@@ -740,4 +764,8 @@
   }
 
+  if (cStatus) {
+    logMsg("ERROR: Failed in writing binary table entry.");
+  }
+
   return cStatus;
 }
@@ -751,17 +779,13 @@
 
 {
-  fits_write_history(cSDptr, text, &cStatus);
+  if (!cSDptr) {
+    return 1;
+  }
+
+  if (fits_write_history(cSDptr, text, &cStatus)) {
+    logMsg("ERROR: Failed in writing HISTORY records.");
+  }
 
   return cStatus;
-}
-
-//-------------------------------------------------- SDFITSwriter::reportError
-
-// Print the error message corresponding to the input status value and all the
-// messages on the CFITSIO error stack to stderr.
-
-void SDFITSwriter::reportError()
-{
-  fits_report_error(stderr, cStatus);
 }
 
@@ -774,5 +798,8 @@
   if (cSDptr) {
     cStatus = 0;
-    fits_close_file(cSDptr, &cStatus);
+    if (fits_close_file(cSDptr, &cStatus)) {
+      logMsg("ERROR: Failed to close file.");
+    }
+
     cSDptr = 0;
   }
@@ -787,6 +814,28 @@
   if (cSDptr) {
     cStatus = 0;
-    fits_delete_file(cSDptr, &cStatus);
+    if (fits_delete_file(cSDptr, &cStatus)) {
+      logMsg("ERROR: Failed to close and delete file.");
+    }
+
     cSDptr = 0;
   }
 }
+
+//------------------------------------------------------- SDFITSwriter::logMsg
+
+// Log a message.  If the current CFITSIO status value is non-zero, also log
+// the corresponding error message and dump the CFITSIO message stack.
+
+void SDFITSwriter::logMsg(const char *msg)
+{
+  PKSmsg::logMsg(msg);
+
+  if (cStatus) {
+    fits_get_errstatus(cStatus, cMsg);
+    PKSmsg::logMsg(cMsg);
+
+    while (fits_read_errmsg(cMsg)) {
+      PKSmsg::logMsg(cMsg);
+    }
+  }
+}
