Index: trunk/psModules/src/objects/pmPSF.c
===================================================================
--- trunk/psModules/src/objects/pmPSF.c	(revision 7306)
+++ trunk/psModules/src/objects/pmPSF.c	(revision 7311)
@@ -6,6 +6,6 @@
  *  @author EAM, IfA
  *
- *  @version $Revision: 1.7 $ $Name: not supported by cvs2svn $
- *  @date $Date: 2006-04-23 01:07:51 $
+ *  @version $Revision: 1.8 $ $Name: not supported by cvs2svn $
+ *  @date $Date: 2006-06-03 01:02:08 $
  *
  *  Copyright 2004 Maui High Performance Computing Center, University of Hawaii
@@ -97,5 +97,5 @@
     // the ApTrend components are (x, y, r2rflux, flux)
     psf->ApTrend = psPolynomial4DAlloc (PS_POLYNOMIAL_ORD, 2, 2, 1, 1);
-    pmPSF_MaskApTrend (psf, PM_PSF_SKYBIAS);
+    pmPSF_MaskApTrend (psf->ApTrend, PM_PSF_SKYBIAS);
 
     if (psf->poissonErrors) {
@@ -239,76 +239,77 @@
  * to enable the specific subset of the coefficients
  **********************************************/
-bool pmPSF_MaskApTrend (pmPSF *psf, pmPSF_ApTrendOptions option)
+bool pmPSF_MaskApTrend (psPolynomial4D *trend,
+                        pmPSF_ApTrendOptions option)
 {
 
     switch (option) {
     case PM_PSF_NONE:
-        maskAllTerms (psf->ApTrend);
+        maskAllTerms (trend);
         return true;
 
     case PM_PSF_CONSTANT:
-        maskAllTerms (psf->ApTrend);
-        psf->ApTrend->mask[0][0][0][0] = 0;  // unmask constant
+        maskAllTerms (trend);
+        trend->mask[0][0][0][0] = 0;  // unmask constant
         return true;
 
     case PM_PSF_SKYBIAS:
-        maskAllTerms (psf->ApTrend);
-        psf->ApTrend->mask[0][0][0][0] = 0;  // unmask constant
-        psf->ApTrend->mask[0][0][1][0] = 0;  // unmask skybias
+        maskAllTerms (trend);
+        trend->mask[0][0][0][0] = 0;  // unmask constant
+        trend->mask[0][0][1][0] = 0;  // unmask skybias
         return true;
 
     case PM_PSF_SKYSAT:
-        maskAllTerms (psf->ApTrend);
-        psf->ApTrend->mask[0][0][0][0] = 0;  // unmask constant
-        psf->ApTrend->mask[0][0][1][0] = 0;  // unmask skybias
-        psf->ApTrend->mask[0][0][0][1] = 0;  // unmask skybias
+        maskAllTerms (trend);
+        trend->mask[0][0][0][0] = 0;  // unmask constant
+        trend->mask[0][0][1][0] = 0;  // unmask skybias
+        trend->mask[0][0][0][1] = 0;  // unmask skysat
         return true;
 
     case PM_PSF_XY_LIN:
-        maskAllTerms (psf->ApTrend);
-        psf->ApTrend->mask[0][0][0][0] = 0;  // unmask constant
-        psf->ApTrend->mask[1][0][0][0] = 0;  // unmask x
-        psf->ApTrend->mask[0][1][0][0] = 0;  // unmask y
+        maskAllTerms (trend);
+        trend->mask[0][0][0][0] = 0;  // unmask constant
+        trend->mask[1][0][0][0] = 0;  // unmask x
+        trend->mask[0][1][0][0] = 0;  // unmask y
         return true;
 
     case PM_PSF_XY_QUAD:
-        maskAllTerms (psf->ApTrend);
-        psf->ApTrend->mask[0][0][0][0] = 0;  // unmask constant
-        psf->ApTrend->mask[1][0][0][0] = 0;  // unmask x
-        psf->ApTrend->mask[2][0][0][0] = 0;  // unmask x^2
-        psf->ApTrend->mask[1][1][0][0] = 0;  // unmask x y
-        psf->ApTrend->mask[0][1][0][0] = 0;  // unmask y
-        psf->ApTrend->mask[0][2][0][0] = 0;  // unmask y^2
+        maskAllTerms (trend);
+        trend->mask[0][0][0][0] = 0;  // unmask constant
+        trend->mask[1][0][0][0] = 0;  // unmask x
+        trend->mask[2][0][0][0] = 0;  // unmask x^2
+        trend->mask[1][1][0][0] = 0;  // unmask x y
+        trend->mask[0][1][0][0] = 0;  // unmask y
+        trend->mask[0][2][0][0] = 0;  // unmask y^2
         return true;
 
     case PM_PSF_SKY_XY_LIN:
-        maskAllTerms (psf->ApTrend);
-        psf->ApTrend->mask[0][0][0][0] = 0;  // unmask constant
-        psf->ApTrend->mask[1][0][0][0] = 0;  // unmask x
-        psf->ApTrend->mask[0][1][0][0] = 0;  // unmask y
-        psf->ApTrend->mask[0][0][1][0] = 0;  // unmask skybias
+        maskAllTerms (trend);
+        trend->mask[0][0][0][0] = 0;  // unmask constant
+        trend->mask[1][0][0][0] = 0;  // unmask x
+        trend->mask[0][1][0][0] = 0;  // unmask y
+        trend->mask[0][0][1][0] = 0;  // unmask skybias
         return true;
 
     case PM_PSF_SKYSAT_XY_LIN:
-        maskAllTerms (psf->ApTrend);
-        psf->ApTrend->mask[0][0][0][0] = 0;  // unmask constant
-        psf->ApTrend->mask[1][0][0][0] = 0;  // unmask x
-        psf->ApTrend->mask[0][1][0][0] = 0;  // unmask y
-        psf->ApTrend->mask[0][0][1][0] = 0;  // unmask skybias
-        psf->ApTrend->mask[0][0][0][1] = 0;  // unmask skysat
+        maskAllTerms (trend);
+        trend->mask[0][0][0][0] = 0;  // unmask constant
+        trend->mask[1][0][0][0] = 0;  // unmask x
+        trend->mask[0][1][0][0] = 0;  // unmask y
+        trend->mask[0][0][1][0] = 0;  // unmask skybias
+        trend->mask[0][0][0][1] = 0;  // unmask skysat
         return true;
 
     case PM_PSF_ALL:
     default:
-        maskAllTerms (psf->ApTrend);
-        psf->ApTrend->mask[0][0][0][0] = 0;  // unmask constant
-        psf->ApTrend->mask[0][0][1][0] = 0;  // unmask skybias
-        psf->ApTrend->mask[0][0][0][1] = 0;  // unmask skysat
-
-        psf->ApTrend->mask[1][0][0][0] = 0;  // unmask x
-        psf->ApTrend->mask[2][0][0][0] = 0;  // unmask x^2
-        psf->ApTrend->mask[1][1][0][0] = 0;  // unmask x y
-        psf->ApTrend->mask[0][1][0][0] = 0;  // unmask y
-        psf->ApTrend->mask[0][2][0][0] = 0;  // unmask y^2
+        maskAllTerms (trend);
+        trend->mask[0][0][0][0] = 0;  // unmask constant
+        trend->mask[0][0][1][0] = 0;  // unmask skybias
+        trend->mask[0][0][0][1] = 0;  // unmask skysat
+
+        trend->mask[1][0][0][0] = 0;  // unmask x
+        trend->mask[2][0][0][0] = 0;  // unmask x^2
+        trend->mask[1][1][0][0] = 0;  // unmask x y
+        trend->mask[0][1][0][0] = 0;  // unmask y
+        trend->mask[0][2][0][0] = 0;  // unmask y^2
         return true;
     }
Index: trunk/psModules/src/objects/pmPSF.h
===================================================================
--- trunk/psModules/src/objects/pmPSF.h	(revision 7306)
+++ trunk/psModules/src/objects/pmPSF.h	(revision 7311)
@@ -31,6 +31,6 @@
     psArray *params;   ///< Model parameters (psPolynomial2D)
     psPolynomial1D *ChiTrend;  ///< Chisq vs flux fit (correction for systematic errors)
-    psPolynomial4D *ApTrend;  ///< ApResid vs (x,y,rflux) (rflux = ten(0.4*mInst)
-    pmGrowthCurve *growth;  ///< apMag vs Radius
+    psPolynomial4D *ApTrend;  ///< ApResid vs (x,y,rflux) (rflux = ten(0.4*mInst))
+    //pmGrowthCurve *growth;  ///< apMag vs Radius // move to end as gdb is confused by it here
     float ApResid;   ///< apMag - psfMag (for PSF stars)
     float dApResid;   ///< scatter of ApResid
@@ -41,4 +41,5 @@
     int nApResid;   ///< number of stars used to measure ApResid
     bool poissonErrors;
+    pmGrowthCurve *growth;  ///< apMag vs Radius
 }
 pmPSF;
@@ -100,5 +101,5 @@
 );
 
-bool pmPSF_MaskApTrend (pmPSF *psf, pmPSF_ApTrendOptions option);
+bool pmPSF_MaskApTrend (psPolynomial4D *trend, pmPSF_ApTrendOptions option);
 
 # endif
Index: trunk/psModules/src/objects/pmPSF_IO.c
===================================================================
--- trunk/psModules/src/objects/pmPSF_IO.c	(revision 7306)
+++ trunk/psModules/src/objects/pmPSF_IO.c	(revision 7311)
@@ -6,6 +6,6 @@
  *  @author EAM, IfA
  *
- *  @version $Revision: 1.2 $ $Name: not supported by cvs2svn $
- *  @date $Date: 2006-04-17 18:01:05 $
+ *  @version $Revision: 1.3 $ $Name: not supported by cvs2svn $
+ *  @date $Date: 2006-06-03 01:02:08 $
  *
  *  Copyright 2004 Maui High Performance Computing Center, University of Hawaii
@@ -287,5 +287,5 @@
 {
 
-    int Nfail;
+    unsigned int Nfail;
     char *filename;
 
Index: trunk/psModules/src/objects/pmPSFtry.c
===================================================================
--- trunk/psModules/src/objects/pmPSFtry.c	(revision 7306)
+++ trunk/psModules/src/objects/pmPSFtry.c	(revision 7311)
@@ -5,6 +5,6 @@
  *  @author EAM, IfA
  *
- *  @version $Revision: 1.10 $ $Name: not supported by cvs2svn $
- *  @date $Date: 2006-06-02 02:16:05 $
+ *  @version $Revision: 1.11 $ $Name: not supported by cvs2svn $
+ *  @date $Date: 2006-06-03 01:02:08 $
  *
  *  Copyright 2004 Maui High Performance Computing Center, University of Hawaii
@@ -263,5 +263,5 @@
     psLogMsg ("pmPSFtryMetric", 4, "fit stats: %f +/- %f\n", stats->sampleMedian, stats->sampleStdev);
 
-    pmPSF_MaskApTrend (psfTry->psf, PM_PSF_SKYBIAS);
+    pmPSF_MaskApTrend (psfTry->psf->ApTrend, PM_PSF_SKYBIAS);
     psfTry->psf->ApTrend->coeff[0][0][0][0] = poly->coeff[0];
     psfTry->psf->ApTrend->coeff[0][0][1][0] = 0;
Index: trunk/psModules/src/objects/pmPSFtry.h
===================================================================
--- trunk/psModules/src/objects/pmPSFtry.h	(revision 7306)
+++ trunk/psModules/src/objects/pmPSFtry.h	(revision 7311)
@@ -6,4 +6,6 @@
  *  @author EAM, IfA
  *
+ *  @version $Revision: 1.6 $ $Name: not supported by cvs2svn $
+ *  @date $Date: 2006-06-03 01:02:08 $
  *
  *  Copyright 2004 Maui High Performance Computing Center, University of Hawaii
@@ -76,10 +78,5 @@
  * 
  */
-pmPSFtry *pmPSFtryAlloc(
-    psArray *stars,                     ///< Add comment.
-    char *modelName,                     ///< Add comment.
-    bool poissonErrors   // use poissonian or constant errors?
-);
-
+pmPSFtry *pmPSFtryAlloc (psArray *sources, char *modelName, bool poissonErrors);
 
 /** pmPSFtryModel()
@@ -90,11 +87,5 @@
  * 
  */
-pmPSFtry *pmPSFtryModel(
-    psArray *sources,                   ///< Add comment.
-    char *modelName,                    ///< Add comment.
-    float radius,                     ///< Add comment.
-    bool poissonErrors   // use poissonian or constant errors?
-);
-
+pmPSFtry *pmPSFtryModel (psArray *sources, char *modelName, float RADIUS, bool poissonErrors);
 
 /** pmPSFtryMetric()
Index: trunk/psModules/src/objects/pmPeaks.c
===================================================================
--- trunk/psModules/src/objects/pmPeaks.c	(revision 7306)
+++ trunk/psModules/src/objects/pmPeaks.c	(revision 7311)
@@ -6,6 +6,6 @@
  *  @author EAM, IfA: significant modifications.
  *
- *  @version $Revision: 1.3 $ $Name: not supported by cvs2svn $
- *  @date $Date: 2006-04-21 21:26:44 $
+ *  @version $Revision: 1.4 $ $Name: not supported by cvs2svn $
+ *  @date $Date: 2006-06-03 01:02:08 $
  *
  *  Copyright 2004 Maui High Performance Computing Center, University of Hawaii
@@ -94,4 +94,7 @@
 pmPeakAlloc(): Allocate the pmPeak data structure and set appropriate members.
 *****************************************************************************/
+static void peakFree(pmPeak *tmp)
+{} // used by pmIsPeak()
+
 pmPeak *pmPeakAlloc(psS32 x,
                     psS32 y,
@@ -100,5 +103,7 @@
 {
     psTrace(__func__, 3, "---- %s() begin ----\n", __func__);
+    static int id = 1;
     pmPeak *tmp = (pmPeak *) psAlloc(sizeof(pmPeak));
+    *(int *)&tmp->id = id++;
     tmp->x = x;
     tmp->y = y;
@@ -106,6 +111,13 @@
     tmp->type = type;
 
+    psMemSetDeallocator(tmp, (psFreeFunc) peakFree);
+
     psTrace(__func__, 3, "---- %s() end ----\n", __func__);
     return(tmp);
+}
+
+bool pmIsPeak(const psPtr ptr)
+{
+    return (psMemGetDeallocator(ptr) == (psFreeFunc)peakFree);
 }
 
Index: trunk/psModules/src/objects/pmPeaks.h
===================================================================
--- trunk/psModules/src/objects/pmPeaks.h	(revision 7306)
+++ trunk/psModules/src/objects/pmPeaks.h	(revision 7311)
@@ -10,6 +10,6 @@
  *  @author GLG, MHPCC
  *
- *  @version $Revision: 1.2 $ $Name: not supported by cvs2svn $
- *  @date $Date: 2006-04-17 18:01:05 $
+ *  @version $Revision: 1.3 $ $Name: not supported by cvs2svn $
+ *  @date $Date: 2006-06-03 01:02:08 $
  *
  *  Copyright 2004 Maui High Performance Computing Center, University of Hawaii
@@ -47,4 +47,5 @@
 typedef struct
 {
+    const int id;   ///< Unique ID for object
     int x;                              ///< X-coordinate of peak pixel.
     int y;                              ///< Y-coordinate of peak pixel.
@@ -66,4 +67,5 @@
 );
 
+bool pmIsPeak(const psPtr ptr);
 
 /** pmFindVectorPeaks()
Index: trunk/psModules/src/objects/pmSource.c
===================================================================
--- trunk/psModules/src/objects/pmSource.c	(revision 7306)
+++ trunk/psModules/src/objects/pmSource.c	(revision 7311)
@@ -6,6 +6,6 @@
  *  @author EAM, IfA: significant modifications.
  *
- *  @version $Revision: 1.5 $ $Name: not supported by cvs2svn $
- *  @date $Date: 2006-06-02 02:16:05 $
+ *  @version $Revision: 1.6 $ $Name: not supported by cvs2svn $
+ *  @date $Date: 2006-06-03 01:02:08 $
  *
  *  Copyright 2004 Maui High Performance Computing Center, University of Hawaii
@@ -46,5 +46,7 @@
 {
     psTrace(__func__, 3, "---- %s() begin ----\n", __func__);
+    static int id = 1;
     pmSource *tmp = (pmSource *) psAlloc(sizeof(pmSource));
+    *(int *)&tmp->id = id++;
     tmp->peak = NULL;
     tmp->pixels = NULL;
@@ -69,4 +71,9 @@
 }
 
+bool pmIsSource(const psPtr ptr)
+{
+    return (psMemGetDeallocator(ptr) == (psFreeFunc)sourceFree);
+}
+
 // x,y are defined in the parent image coords of readout->image
 bool pmSourceDefinePixels(pmSource *mySource,
@@ -82,7 +89,7 @@
     srcRegion = psRegionForImage (readout->image, srcRegion);
 
-    mySource->pixels = psImageSubset(readout->image, srcRegion);
-    mySource->weight = psImageSubset(readout->weight, srcRegion);
-    mySource->mask   = psImageSubset(readout->mask,  srcRegion);
+    mySource->pixels = psMemIncrRefCounter(psImageSubset(readout->image, srcRegion));
+    mySource->weight = psMemIncrRefCounter(psImageSubset(readout->weight, srcRegion));
+    mySource->mask   = psMemIncrRefCounter(psImageSubset(readout->mask,  srcRegion));
     mySource->region = srcRegion;
 
@@ -123,7 +130,7 @@
         psFree (mySource->mask);
 
-        mySource->pixels = psImageSubset(readout->image,  newRegion);
-        mySource->weight = psImageSubset(readout->weight, newRegion);
-        mySource->mask   = psImageSubset(readout->mask,   newRegion);
+        mySource->pixels = psMemIncrRefCounter(psImageSubset(readout->image,  newRegion));
+        mySource->weight = psMemIncrRefCounter(psImageSubset(readout->weight, newRegion));
+        mySource->mask   = psMemIncrRefCounter(psImageSubset(readout->mask,   newRegion));
         mySource->region = newRegion;
     }
Index: trunk/psModules/src/objects/pmSource.h
===================================================================
--- trunk/psModules/src/objects/pmSource.h	(revision 7306)
+++ trunk/psModules/src/objects/pmSource.h	(revision 7311)
@@ -3,6 +3,6 @@
  *  @author EAM, IfA; GLG, MHPCC
  *
- *  @version $Revision: 1.3 $ $Name: not supported by cvs2svn $
- *  @date $Date: 2006-04-19 20:29:57 $
+ *  @version $Revision: 1.4 $ $Name: not supported by cvs2svn $
+ *  @date $Date: 2006-06-03 01:02:08 $
  *
  *  Copyright 2004 Maui High Performance Computing Center, University of Hawaii
@@ -73,4 +73,5 @@
 typedef struct
 {
+    const int id;   ///< Unique ID for object
     pmPeak *peak;   ///< Description of peak pixel.
     psImage *pixels;   ///< Rectangular region including object pixels.
@@ -114,4 +115,5 @@
 pmSource  *pmSourceAlloc();
 
+bool pmIsSource(const psPtr ptr);
 
 /** pmSourceDefinePixels()
Index: trunk/psModules/src/objects/pmSourceFitModel.c
===================================================================
--- trunk/psModules/src/objects/pmSourceFitModel.c	(revision 7306)
+++ trunk/psModules/src/objects/pmSourceFitModel.c	(revision 7311)
@@ -6,6 +6,6 @@
  *  @author GLG, MHPCC
  *
- *  @version $Revision: 1.4 $ $Name: not supported by cvs2svn $
- *  @date $Date: 2006-04-23 01:07:51 $
+ *  @version $Revision: 1.5 $ $Name: not supported by cvs2svn $
+ *  @date $Date: 2006-06-03 01:02:08 $
  *
  *  Copyright 2004 Maui High Performance Computing Center, University of Hawaii
@@ -515,4 +515,7 @@
     psTrace (__func__, 5, "fitting function\n");
     fitStatus = psMinimizeLMChi2(myMin, covar, params, constrain, x, y, yErr, pmModelFitSet);
+    if (!fitStatus) {
+        psError(PS_ERR_UNKNOWN, false, "Failed to fit model\n");
+    }
 
     // parameter errors from the covariance matrix
Index: trunk/psModules/src/objects/pmSourceIO.c
===================================================================
--- trunk/psModules/src/objects/pmSourceIO.c	(revision 7306)
+++ trunk/psModules/src/objects/pmSourceIO.c	(revision 7311)
@@ -3,6 +3,6 @@
  *  @author EAM, IfA
  *
- *  @version $Revision: 1.5 $ $Name: not supported by cvs2svn $
- *  @date $Date: 2006-06-03 00:40:28 $
+ *  @version $Revision: 1.6 $ $Name: not supported by cvs2svn $
+ *  @date $Date: 2006-06-03 01:02:08 $
  *
  *  Copyright 2004 Maui High Performance Computing Center, University of Hawaii
@@ -57,5 +57,5 @@
 }
 
-// Given a FITS file pointer, read the table of object data
+// Given a FITS file pointer, write the table of object data
 bool pmFPAviewWriteObjects (const pmFPAview *view, pmFPAfile *file)
 {
@@ -64,9 +64,13 @@
 
     if (view->chip == -1) {
-        pmFPAWriteObjects (fpa, view, file);
+        if (!pmFPAWriteObjects (fpa, view, file)) {
+            psError(PS_ERR_IO, false, "Failed to write objects from fpa");
+            return false;
+        }
         return true;
     }
 
     if (view->chip >= fpa->chips->n) {
+        psError(PS_ERR_UNKNOWN, false, "Writing chip == %d (>= chips->n == %d)", view->chip, fpa->chips->n);
         return false;
     }
@@ -74,9 +78,14 @@
 
     if (view->cell == -1) {
-        pmChipWriteObjects (chip, view, file);
+        if (!pmChipWriteObjects (chip, view, file)) {
+            psError(PS_ERR_IO, false, "Failed to write objects from chip");
+            return false;
+        }
         return true;
     }
 
     if (view->cell >= chip->cells->n) {
+        psError(PS_ERR_UNKNOWN, false, "Writing cell == %d (>= cells->n == %d)",
+                view->cell, chip->cells->n);
         return false;
     }
@@ -84,14 +93,24 @@
 
     if (view->readout == -1) {
-        pmCellWriteObjects (cell, view, file);
+        if (!pmCellWriteObjects (cell, view, file)) {
+            psError(PS_ERR_IO, false, "Failed to write objects from cell");
+            return false;
+        }
+
         return true;
     }
 
     if (view->readout >= cell->readouts->n) {
+        psError(PS_ERR_UNKNOWN, false, "Writing readout == %d (>= readouts->n == %d)",
+                view->readout, cell->readouts->n);
         return false;
     }
     pmReadout *readout = cell->readouts->data[view->readout];
 
-    pmReadoutWriteObjects (readout, view, file);
+    if (!pmReadoutWriteObjects (readout, view, file)) {
+        psError(PS_ERR_IO, false, "Failed to write objects from readout %d", view->readout);
+        return false;
+    }
+
     return true;
 }
@@ -104,5 +123,8 @@
 
         pmChip *chip = fpa->chips->data[i];
-        pmChipWriteObjects (chip, view, file);
+        if (!pmChipWriteObjects (chip, view, file)) {
+            psError(PS_ERR_IO, false, "Failed to write %dth chip", i);
+            return false;
+        }
     }
     return true;
@@ -116,5 +138,8 @@
 
         pmCell *cell = chip->cells->data[i];
-        pmCellWriteObjects (cell, view, file);
+        if (!pmCellWriteObjects (cell, view, file)) {
+            psError(PS_ERR_IO, false, "Failed to write %dth cell", i);
+            return false;
+        }
     }
     return true;
@@ -128,5 +153,8 @@
 
         pmReadout *readout = cell->readouts->data[i];
-        pmReadoutWriteObjects (readout, view, file);
+        if (!pmReadoutWriteObjects (readout, view, file)) {
+            psError(PS_ERR_IO, false, "Failed to write %dth readout", i);
+            return false;
+        }
     }
     return true;
@@ -181,7 +209,12 @@
         psMetadataCopy (outhead, updates);
 
-        pmSourcesWriteCMP (sources, filename, outhead);
+        bool status = pmSourcesWriteCMP (sources, filename, outhead);
         psFree (outhead);
         psFree (filename);
+
+        if (!status) {
+            psError(PS_ERR_IO, false, "Failed to write CMP file\n");
+            return false;
+        }
         break;
 
Index: trunk/psModules/src/objects/pmSourceIO_CMP.c
===================================================================
--- trunk/psModules/src/objects/pmSourceIO_CMP.c	(revision 7306)
+++ trunk/psModules/src/objects/pmSourceIO_CMP.c	(revision 7311)
@@ -3,6 +3,6 @@
  *  @author EAM, IfA
  *
- *  @version $Revision: 1.6 $ $Name: not supported by cvs2svn $
- *  @date $Date: 2006-06-03 00:40:46 $
+ *  @version $Revision: 1.7 $ $Name: not supported by cvs2svn $
+ *  @date $Date: 2006-06-03 01:02:08 $
  *
  *  Copyright 2004 Maui High Performance Computing Center, University of Hawaii
@@ -69,11 +69,29 @@
 
     psFits *fits = psFitsOpen (filename, "w");
-    psFitsWriteBlank(fits, header);
-    psFitsClose (fits);
+    if (fits == NULL) {
+        psError(PS_ERR_IO, false, "can't open output file for write %s\n", filename);
+        return false;
+    }
+    if (!psFitsWriteBlank(fits, header)) {
+        psError(PS_ERR_IO, false, "Writing header to %s\n", filename);
+        (void)psFitsClose(fits);
+        return false;
+    }
+    if (!psFitsClose(fits)) {
+        const psErrorCode code = psErrorCodeLast();
+
+        if (code == PS_ERR_BAD_FITS) {
+            psErrorClear();
+        } else {
+            psError(PS_ERR_IO, false, "Closing %s\n", filename);
+            return false;
+        }
+    }
 
     // re-open, add data to end of file
     FILE *f = fopen (filename, "a+");
     if (f == NULL) {
-        psLogMsg ("WriteSourceOBJ", 3, "can't open output file for output %s\n", filename);
+        psLogMsg ("WriteSourceOBJ", 3, "can't reopen output file for append %s\n", filename);
+        psError(PS_ERR_IO, false, "can't open output file for output %s\n", filename);
         return false;
     }
