Index: trunk/psModules/src/astrom/pmAstrometryDistortion.c
===================================================================
--- trunk/psModules/src/astrom/pmAstrometryDistortion.c	(revision 23975)
+++ trunk/psModules/src/astrom/pmAstrometryDistortion.c	(revision 23989)
@@ -151,8 +151,14 @@
 
             // also measure the L and M median positions as a representative coordinate
-            psVectorStats (stats, L, NULL, NULL, 0);
+            if (!psVectorStats (stats, L, NULL, NULL, 0)) {
+		psError(PS_ERR_UNKNOWN, false, "failure to measure stats");
+		goto skip;
+	    }
             grad->FP.x = stats->sampleMedian;
 
-            psVectorStats (stats, M, NULL, NULL, 0);
+            if (!psVectorStats (stats, M, NULL, NULL, 0)) {
+		psError(PS_ERR_UNKNOWN, false, "failure to measure stats");
+		goto skip;
+	    }
             grad->FP.y = stats->sampleMedian;
 
Index: trunk/psModules/src/camera/pmFPAMaskWeight.c
===================================================================
--- trunk/psModules/src/camera/pmFPAMaskWeight.c	(revision 23975)
+++ trunk/psModules/src/camera/pmFPAMaskWeight.c	(revision 23989)
@@ -482,5 +482,5 @@
         return false;
     }
-    float stdev = psStatsGetValue(stdevStats, stdevStat); // Stadard deviation of fluxes
+    float stdev = psStatsGetValue(stdevStats, stdevStat); // Standard deviation of fluxes
     psFree(stdevStats);
     psFree(noise);
Index: trunk/psModules/src/detrend/pmFringeStats.c
===================================================================
--- trunk/psModules/src/detrend/pmFringeStats.c	(revision 23975)
+++ trunk/psModules/src/detrend/pmFringeStats.c	(revision 23989)
@@ -882,5 +882,8 @@
 
     psStats *stats = psStatsAlloc(PS_STAT_SAMPLE_MEDIAN | PS_STAT_SAMPLE_QUARTILE); // Statistics
-    psVectorStats(stats, diffs, NULL, mask, 1);
+    if (!psVectorStats(stats, diffs, NULL, mask, 1)) {
+	psError(PS_ERR_UNKNOWN, false, "failure to measure stats");
+	return 0;
+    }
     float middle = stats->sampleMedian; // The middle of the distribution
     float thresh = rej * 0.74 * (stats->sampleUQ - stats->sampleLQ); // The rejection threshold
@@ -969,9 +972,15 @@
 
     // Get rid of the extreme outliers by assuming most of the points are somewhat clustered
-    psVectorStats(median, science->f, NULL, NULL, 0);
+    if (!psVectorStats(median, science->f, NULL, NULL, 0)) {
+	psError(PS_ERR_UNKNOWN, false, "failure to measure stats");
+	return NULL;
+    }
     scale->coeff->data.F32[0] = median->sampleMedian;
     for (int i = 0; i < fringes->n; i++) {
         pmFringeStats *fringe = fringes->data[i]; // The fringe of interest
-        psVectorStats(median, fringe->f, NULL, NULL, 0);
+        if (!psVectorStats(median, fringe->f, NULL, NULL, 0)) {
+	    psError(PS_ERR_UNKNOWN, false, "failure to measure stats");
+	    return NULL;
+	}
         scale->coeff->data.F32[0] -= median->sampleMedian;
         scale->coeff->data.F32[i] = 0.0;
Index: trunk/psModules/src/detrend/pmOverscan.c
===================================================================
--- trunk/psModules/src/detrend/pmOverscan.c	(revision 23975)
+++ trunk/psModules/src/detrend/pmOverscan.c	(revision 23989)
@@ -74,5 +74,8 @@
             mask->data.PS_TYPE_VECTOR_MASK_DATA[i] = 0;
             ordinate->data.F32[i] = 2.0*(float)i/(float)pixels->n - 1.0; // Scale to [-1,1]
-            psVectorStats(myStats, values, NULL, NULL, 0);
+            if (!psVectorStats(myStats, values, NULL, NULL, 0)) {
+		psError(PS_ERR_UNKNOWN, false, "failure to measure stats");
+		return false;
+	    }
             reduced->data.F32[i] = psStatsGetValue(myStats, statistic);
         } else if (overscanOpts->fitType == PM_FIT_NONE) {
@@ -275,5 +278,8 @@
 	psFree(iter);
 
-	(void)psVectorStats(stats, pixels, NULL, NULL, 0);
+	if (!psVectorStats(stats, pixels, NULL, NULL, 0)) {
+	    psError(PS_ERR_UNKNOWN, false, "failure to measure stats");
+	    return false;
+	}
 	psFree(pixels);
 	double reduced = psStatsGetValue(stats, statistic); // Result of statistics
@@ -349,5 +355,8 @@
 	  psString comment = NULL;    // Comment to add
 	  psStats *vectorStats = psStatsAlloc (PS_STAT_SAMPLE_MEAN | PS_STAT_SAMPLE_STDEV);
-	  psVectorStats (vectorStats, reduced, NULL, NULL, 0);
+	  if (!psVectorStats (vectorStats, reduced, NULL, NULL, 0)) {
+	      psError(PS_ERR_UNKNOWN, false, "failure to measure stats");
+	      return false;
+	  }
 	  psStringAppend(&comment, "Mean Overscan value: %f", vectorStats->sampleMean);
 	  psMetadataAddStr(hdu->header, PS_LIST_TAIL, "HISTORY", PS_META_DUPLICATE_OK, comment, "");
@@ -412,5 +421,8 @@
 	  psString comment = NULL;    // Comment to add
 	  psStats *vectorStats = psStatsAlloc (PS_STAT_SAMPLE_MEAN | PS_STAT_SAMPLE_STDEV);
-	  psVectorStats (vectorStats, reduced, NULL, NULL, 0);
+	  if (!psVectorStats (vectorStats, reduced, NULL, NULL, 0)) {
+	      psError(PS_ERR_UNKNOWN, false, "failure to measure stats");
+	      return false;
+	  }
 	  psStringAppend(&comment, "Mean Overscan value: %f", vectorStats->sampleMean);
 	  psMetadataAddStr(hdu->header, PS_LIST_TAIL, "HISTORY", PS_META_DUPLICATE_OK, comment, "");
Index: trunk/psModules/src/detrend/pmRemnance.c
===================================================================
--- trunk/psModules/src/detrend/pmRemnance.c	(revision 23975)
+++ trunk/psModules/src/detrend/pmRemnance.c	(revision 23989)
@@ -66,6 +66,8 @@
             values->n = numValues;
             if (!psVectorStats(stats, values, NULL, NULL, 0)) {
-                // Can't do anything about it
-                psErrorClear();
+		psError(PS_ERR_UNKNOWN, false, "failure to measure stats");
+		return false;
+	    }
+	    if (isnan(stats->sampleMedian)) {
                 maxMask = max;
                 continue;
Index: trunk/psModules/src/detrend/pmShutterCorrection.c
===================================================================
--- trunk/psModules/src/detrend/pmShutterCorrection.c	(revision 23975)
+++ trunk/psModules/src/detrend/pmShutterCorrection.c	(revision 23989)
@@ -350,10 +350,17 @@
     psStats *rawStats = psStatsAlloc (PS_STAT_SAMPLE_MEDIAN | PS_STAT_SAMPLE_STDEV);
     psStats *resStats = psStatsAlloc (PS_STAT_SAMPLE_MEDIAN | PS_STAT_SAMPLE_STDEV);
-    psVectorStats (rawStats, counts, NULL, NULL, 0);
-    psVectorStats (resStats, resid, NULL, NULL, 0);
+    if (!psVectorStats (rawStats, counts, NULL, NULL, 0)) {
+	psError(PS_ERR_UNKNOWN, false, "failure to measure stats");
+	return NULL;
+    }
+    if (!psVectorStats (resStats, resid, NULL, NULL, 0)) {
+	psError(PS_ERR_UNKNOWN, false, "failure to measure stats");
+	return NULL;
+    }
 
     // XXX temporary hard-wired minimum stdev improvement factor
     psTrace("psModules.detrend", 3, "raw scatter %f vs res scatter %f\n", rawStats->sampleStdev, resStats->sampleStdev);
     if (rawStats->sampleStdev / resStats->sampleStdev < 1.5) corr->valid = false;
+    if (isnan(rawStats->sampleStdev) || isnan(resStats->sampleStdev)) corr->valid = false;
 
     psFree (rawStats);
Index: trunk/psModules/src/extras/pmVisual.c
===================================================================
--- trunk/psModules/src/extras/pmVisual.c	(revision 23975)
+++ trunk/psModules/src/extras/pmVisual.c	(revision 23989)
@@ -281,6 +281,12 @@
     psStats *statsX = psStatsAlloc(PS_STAT_SAMPLE_MEDIAN | PS_STAT_SAMPLE_STDEV);
     psStats *statsY = psStatsAlloc(PS_STAT_SAMPLE_MEDIAN | PS_STAT_SAMPLE_STDEV);
-    psVectorStats (statsX, xVec, NULL, NULL, 0);
-    psVectorStats (statsY, yVec, NULL, NULL, 0);
+    if (!psVectorStats (statsX, xVec, NULL, NULL, 0)) {
+	psError(PS_ERR_UNKNOWN, false, "failure to measure stats");
+	return false;
+    }
+    if (!psVectorStats (statsY, yVec, NULL, NULL, 0)) {
+	psError(PS_ERR_UNKNOWN, false, "failure to measure stats");
+	return false;
+    }
 
     float xhi  = statsX->sampleMedian + 3 *statsX->sampleStdev;
Index: trunk/psModules/src/imcombine/pmImageCombine.c
===================================================================
--- trunk/psModules/src/imcombine/pmImageCombine.c	(revision 23975)
+++ trunk/psModules/src/imcombine/pmImageCombine.c	(revision 23989)
@@ -132,4 +132,8 @@
         // Combine all the pixels, using the specified stat.
         if (!psVectorStats(stats, pixelData, pixelErrors, pixelMasks, 0xff)) {
+	    psError(PS_ERR_UNKNOWN, false, "failure to measure stats");
+	    return false;
+	}
+	if (isnan(stats->sampleMean)) {
             combine->data.F32[y][x] = NAN;
             psFree(buffer);
@@ -137,5 +141,5 @@
         }
         float combinedPixel = stats->sampleMean; // Value of the combination
-
+	
         if (iter == 0) {
             // Save the value produced with no rejection, since it may be useful later
@@ -364,5 +368,7 @@
     // Get the median
     psStats *stats = psStatsAlloc(PS_STAT_SAMPLE_MEDIAN);
-    psVectorStats(stats, pixels, NULL, mask, 1);
+    if (!psVectorStats(stats, pixels, NULL, mask, 1)) {
+	psError(PS_ERR_UNKNOWN, false, "failure to measure stats");
+    }
     float median = stats->sampleMedian;
     psFree(stats);
Index: trunk/psModules/src/imcombine/pmReadoutCombine.c
===================================================================
--- trunk/psModules/src/imcombine/pmReadoutCombine.c	(revision 23975)
+++ trunk/psModules/src/imcombine/pmReadoutCombine.c	(revision 23989)
@@ -384,23 +384,27 @@
             // Combination
             if (!psVectorStats(stats, pixels, errors, mask, 1)) {
-                // Can't do much about it, but it's not worth worrying about
-                psErrorClear();
+		psError(PS_ERR_UNKNOWN, false, "error in pixel stats");
+		return false;
+	    }
+	    
+	    outputImage[yOut][xOut] = psStatsGetValue(stats, params->combine);
+
+	    if (isnan(outputImage[yOut][xOut])) {
                 outputImage[yOut][xOut] = NAN;
                 outputMask[yOut][xOut] = params->blank;
+                sigma->data.F32[yOut][xOut] = NAN;
                 if (params->variances) {
                     outputVariance[yOut][xOut] = NAN;
                 }
-                sigma->data.F32[yOut][xOut] = NAN;
-            } else {
-                outputImage[yOut][xOut] = psStatsGetValue(stats, params->combine);
-                outputMask[yOut][xOut] = isfinite(outputImage[yOut][xOut]) ? 0 : params->blank;
-                if (params->variances) {
-                    float stdev = psStatsGetValue(stats, combineStdev);
-                    outputVariance[yOut][xOut] = PS_SQR(stdev); // Variance
-                    // XXXX this is not the correct formal error.
-                    // also, the weighted mean is not obviously the correct thing here
-                }
-                sigma->data.F32[yOut][xOut] = psStatsGetValue(stats, combineStdev);
-            }
+		continue;
+	    }
+	    outputMask[yOut][xOut] = 0;
+	    sigma->data.F32[yOut][xOut] = psStatsGetValue(stats, combineStdev);
+	    if (params->variances) {
+		float stdev = psStatsGetValue(stats, combineStdev);
+		outputVariance[yOut][xOut] = PS_SQR(stdev); // Variance
+		// XXXX this is not the correct formal error.
+		// also, the weighted mean is not obviously the correct thing here
+	    }
         }
     }
Index: trunk/psModules/src/imcombine/pmSubtraction.c
===================================================================
--- trunk/psModules/src/imcombine/pmSubtraction.c	(revision 23975)
+++ trunk/psModules/src/imcombine/pmSubtraction.c	(revision 23989)
@@ -822,4 +822,10 @@
     psFree(mask);
 
+    // XXX raise an error?
+    if (isnan(stats->sampleMean)) {
+        psFree(stats);
+        return -1;
+    }
+
     double mean, rms;                 // Mean and RMS of deviations
     if (numStamps < MIN_SAMPLE_STATS) {
Index: trunk/psModules/src/imcombine/pmSubtractionMatch.c
===================================================================
--- trunk/psModules/src/imcombine/pmSubtractionMatch.c	(revision 23975)
+++ trunk/psModules/src/imcombine/pmSubtractionMatch.c	(revision 23989)
@@ -830,12 +830,14 @@
     psFree(mask);
 
+    // XXX raise an error here or not?
+    if (isnan(stats->robustMedian)) {
+        psFree(stats);
+        return PM_SUBTRACTION_MODE_ERR;
+    }
+
     psLogMsg("psModules.imcombine", PS_LOG_INFO, "Median width ratio: %lf", stats->robustMedian);
     pmSubtractionMode mode = (stats->robustMedian <= 1.0 ? PM_SUBTRACTION_MODE_1 : PM_SUBTRACTION_MODE_2);
     psFree(stats);
 
-    // XXX EAM : I think Paul left some test code in here.  I've commented these lines out
-    // return PM_SUBTRACTION_MODE_2;
-    // exit(1);
-
     return mode;
 }
Index: trunk/psModules/src/objects/models/pmModel_SGAUSS.c
===================================================================
--- trunk/psModules/src/objects/models/pmModel_SGAUSS.c	(revision 23975)
+++ trunk/psModules/src/objects/models/pmModel_SGAUSS.c	(revision 23989)
@@ -325,5 +325,8 @@
 
     psStats *stats = psStatsAlloc (PS_STAT_SAMPLE_MEDIAN);
-    psVectorStats (stats, contour, NULL, NULL, 0);
+    if (!psVectorStats (stats, contour, NULL, NULL, 0)) {
+	psError(PS_ERR_UNKNOWN, false, "failure to measure stats");
+	return false;
+    }
     value = stats->sampleMedian;
 
Index: trunk/psModules/src/objects/pmGrowthCurveGenerate.c
===================================================================
--- trunk/psModules/src/objects/pmGrowthCurveGenerate.c	(revision 23975)
+++ trunk/psModules/src/objects/pmGrowthCurveGenerate.c	(revision 23989)
@@ -89,5 +89,8 @@
 	psVectorAppend (values, growth->fitMag);
     }
-    psVectorStats (stats, values, NULL, NULL, 0);
+    if (!psVectorStats (stats, values, NULL, NULL, 0)) {
+	psError(PS_ERR_UNKNOWN, false, "failure to measure stats");
+	return false;
+    }
     psf->growth->fitMag = stats->sampleMedian;
 
@@ -104,5 +107,8 @@
 	    psVectorAppend (values, growth->apMag->data.F32[i]);
 	}
-	psVectorStats (stats, values, NULL, NULL, 0);
+	if (!psVectorStats (stats, values, NULL, NULL, 0)) {
+	    psError(PS_ERR_UNKNOWN, false, "failure to measure stats");
+	    return false;
+	}
 	psf->growth->apMag->data.F32[i] = stats->sampleMedian;
     }
Index: trunk/psModules/src/objects/pmPSFtry.c
===================================================================
--- trunk/psModules/src/objects/pmPSFtry.c	(revision 23975)
+++ trunk/psModules/src/objects/pmPSFtry.c	(revision 23989)
@@ -211,5 +211,5 @@
 
     if (Next == 0) {
-        psError(PS_ERR_UNKNOWN, true, "No sources with good extended fits from which to determine PSF.");
+        psError(PS_ERR_UNKNOWN, false, "No sources with good extended fits from which to determine PSF.");
         psFree(psfTry);
         return NULL;
@@ -282,5 +282,5 @@
 
     if (Npsf == 0) {
-        psError(PS_ERR_UNKNOWN, true, "No sources with good PSF fits after model is built.");
+        psError(PS_ERR_UNKNOWN, false, "No sources with good PSF fits after model is built.");
         psFree(psfTry);
         return NULL;
@@ -643,5 +643,5 @@
         }
         if (entryMin == -1) {
-            psError (PS_ERR_UNKNOWN, true, "failed to find image map for shape params");
+            psError (PS_ERR_UNKNOWN, false, "failed to find image map for shape params");
             return false;
         }
@@ -958,13 +958,22 @@
     float dEsquare = 0.0;
     psStatsInit (stats);
-    psVectorStats (stats, e0res, NULL, mask, maskValue);
+    if (!psVectorStats (stats, e0res, NULL, mask, maskValue)) {
+	psError(PS_ERR_UNKNOWN, false, "failure to measure stats");
+	return false;
+    }
     dEsquare += PS_SQR(psStatsGetValue(stats, stdevOpt));
 
     psStatsInit (stats);
-    psVectorStats (stats, e1res, NULL, mask, maskValue);
+    if (!psVectorStats (stats, e1res, NULL, mask, maskValue)) {
+	psError(PS_ERR_UNKNOWN, false, "failure to measure stats");
+	return false;
+    }
     dEsquare += PS_SQR(psStatsGetValue(stats, stdevOpt));
 
     psStatsInit (stats);
-    psVectorStats (stats, e2res, NULL, mask, maskValue);
+    if (!psVectorStats (stats, e2res, NULL, mask, maskValue)) {
+	psError(PS_ERR_UNKNOWN, false, "failure to measure stats");
+	return false;
+    }
     dEsquare += PS_SQR(psStatsGetValue(stats, stdevOpt));
 
@@ -1018,13 +1027,20 @@
         float dEsquare = 0.0;
         psStatsInit (statsS);
-        psVectorStats (statsS, dE0subset, NULL, mkSubset, 0xff);
+        if (!psVectorStats (statsS, dE0subset, NULL, mkSubset, 0xff)) {
+	}
         dEsquare += PS_SQR(psStatsGetValue(statsS, stdevOpt));
 
         psStatsInit (statsS);
-        psVectorStats (statsS, dE1subset, NULL, mkSubset, 0xff);
-        dEsquare += PS_SQR(psStatsGetValue(statsS, stdevOpt));
+        if (!psVectorStats (statsS, dE1subset, NULL, mkSubset, 0xff)) {
+	    psError(PS_ERR_UNKNOWN, false, "failure to measure stats");
+	    return false;
+	}        
+	dEsquare += PS_SQR(psStatsGetValue(statsS, stdevOpt));
 
         psStatsInit (statsS);
-        psVectorStats (statsS, dE2subset, NULL, mkSubset, 0xff);
+        if (!psVectorStats (statsS, dE2subset, NULL, mkSubset, 0xff)) {
+	    psError(PS_ERR_UNKNOWN, false, "failure to measure stats");
+	    return false;
+	}
         dEsquare += PS_SQR(psStatsGetValue(statsS, stdevOpt));
 
Index: trunk/psModules/src/objects/pmSource.c
===================================================================
--- trunk/psModules/src/objects/pmSource.c	(revision 23975)
+++ trunk/psModules/src/objects/pmSource.c	(revision 23989)
@@ -479,9 +479,15 @@
         stats = psStatsAlloc (PS_STAT_CLIPPED_MEAN | PS_STAT_CLIPPED_STDEV);
 
-        psVectorStats (stats, tmpSx, NULL, NULL, 0);
+        if (!psVectorStats (stats, tmpSx, NULL, NULL, 0)) {
+	    psError(PS_ERR_UNKNOWN, false, "failed to measure Sx stats");
+            return (emptyClump);
+	}
         psfClump.X  = stats->clippedMean;
         psfClump.dX = stats->clippedStdev;
 
-        psVectorStats (stats, tmpSy, NULL, NULL, 0);
+        if (!psVectorStats (stats, tmpSy, NULL, NULL, 0)) {
+	    psError(PS_ERR_UNKNOWN, false, "failed to measure Sy stats");
+            return (emptyClump);
+	}
         psfClump.Y  = stats->clippedMean;
         psfClump.dY = stats->clippedStdev;
@@ -636,6 +642,8 @@
 
         if (!psVectorStats (stats, starsn_moments, NULL, NULL, 0)) {
-            // Don't care about this error
-            psErrorClear();
+	    psError(PS_ERR_UNKNOWN, false, "failed to measure SN / moments stats");
+	    psFree (stats);
+	    psFree (starsn_peaks);
+	    return false;
         }
         psLogMsg ("pmObjects", 3, "SN range (moments): %f - %f\n", stats->min, stats->max);
@@ -648,6 +656,8 @@
         stats = psStatsAlloc (PS_STAT_MIN | PS_STAT_MAX);
         if (!psVectorStats (stats, starsn_peaks, NULL, NULL, 0)) {
-            // Don't care about this error
-            psErrorClear();
+	    psError(PS_ERR_UNKNOWN, false, "failed to measure SN / moments stats");
+	    psFree (stats);
+	    psFree (starsn_peaks);
+	    return false;
         }
         psLogMsg ("psModules.objects", 3, "SN range (peaks)  : %f - %f (%ld)\n",
Index: trunk/psModules/src/objects/pmSourceFitModel.c
===================================================================
--- trunk/psModules/src/objects/pmSourceFitModel.c	(revision 23975)
+++ trunk/psModules/src/objects/pmSourceFitModel.c	(revision 23989)
@@ -172,10 +172,10 @@
     fitStatus = psMinimizeLMChi2(myMin, covar, params, constraint, x, y, yErr, model->modelFunc);
     for (int i = 0; i < dparams->n; i++) {
-        if (psTraceGetLevel("psModules.objects") >= 4) {
-            fprintf (stderr, "%f ", params->data.F32[i]);
-        }
         if ((constraint->paramMask != NULL) && constraint->paramMask->data.PS_TYPE_VECTOR_MASK_DATA[i])
             continue;
         dparams->data.F32[i] = sqrt(covar->data.F32[i][i]);
+        if (psTraceGetLevel("psModules.objects") >= 4) {
+            fprintf (stderr, "%f +/- %f\n", params->data.F32[i], dparams->data.F32[i]);
+        }
     }
     psTrace ("psModules.objects", 4, "niter: %d, chisq: %f", myMin->iter, myMin->value);
Index: trunk/psModules/src/objects/pmSourceMatch.c
===================================================================
--- trunk/psModules/src/objects/pmSourceMatch.c	(revision 23975)
+++ trunk/psModules/src/objects/pmSourceMatch.c	(revision 23989)
@@ -473,4 +473,10 @@
         return -1;
     }
+    // XXX handle this case better:
+    if (isnan(stats->clippedMean))  {
+        psError(PS_ERR_UNKNOWN, false, "Unable to perform statistics on transparencies.");
+        psFree(stats);
+        return -1;
+    }
 
     float thresh = stats->clippedMean + photoLevel * stats->clippedStdev; // Threshold for clouds
