Index: trunk/ppImage/src/ppImageMeasureCrosstalk.c
===================================================================
--- trunk/ppImage/src/ppImageMeasureCrosstalk.c	(revision 23988)
+++ trunk/ppImage/src/ppImageMeasureCrosstalk.c	(revision 23989)
@@ -149,5 +149,8 @@
 
 		psStatsInit (stats);
-		psVectorStats (stats, vector, NULL, NULL, 0);
+		if (!psVectorStats (stats, vector, NULL, NULL, 0)) {
+		    psError(PS_ERR_UNKNOWN, false, "failure to measure stats");
+		    return false;
+		}
 		    
 		float background = psMetadataLookupF32 (&status, cell->analysis, "XTALK.REF");
Index: trunk/ppStats/src/ppStatsFromMetadataStats.c
===================================================================
--- trunk/ppStats/src/ppStatsFromMetadataStats.c	(revision 23988)
+++ trunk/ppStats/src/ppStatsFromMetadataStats.c	(revision 23989)
@@ -38,5 +38,8 @@
 
         psStats *stats = psStatsAlloc(option);
-        psVectorStats(stats, entry->vector, NULL, NULL, 0);
+        if (!psVectorStats(stats, entry->vector, NULL, NULL, 0)) {
+	    psError(PS_ERR_UNKNOWN, false, "failure to measure stats for %s", entry->statistic);
+	    continue;
+	}
 
         double value;
Index: trunk/ppStats/src/ppStatsReadout.c
===================================================================
--- trunk/ppStats/src/ppStatsReadout.c	(revision 23988)
+++ trunk/ppStats/src/ppStatsReadout.c	(revision 23989)
@@ -92,7 +92,7 @@
         }
         if (!psVectorStats(data->stats, sampleValues, NULL, sampleMask, 1)) {
-            psWarning("Unable to perform statistics on readout %s.\n", readoutName);
-            psErrorClear();
-        }
+	    psError(PS_ERR_UNKNOWN, false, "failure to measure stats for readout %s", readoutName);
+	}
+	// XXX raise if we get a NAN? psWarning("Unable to perform statistics on readout %s.\n", readoutName);
         psFree(sampleValues);
         psFree(sampleMask);
Index: trunk/psLib/src/imageops/psImageGeomManip.c
===================================================================
--- trunk/psLib/src/imageops/psImageGeomManip.c	(revision 23988)
+++ trunk/psLib/src/imageops/psImageGeomManip.c	(revision 23989)
@@ -110,38 +110,43 @@
     out = psImageRecycle(out, outCols, outRows, in->type.type);
 
-    #define PS_IMAGE_REBIN_CASE(TYPE) \
-case PS_TYPE_##TYPE: { \
-        ps##TYPE *outRowData; \
-        ps##TYPE *vecData = vec->data.TYPE; \
-        psImageMaskType *inRowMask = NULL; \
-        for (psS32 row = 0; row < outRows; row++) { \
-            outRowData = out->data.TYPE[row]; \
-            psS32 inCurrentRow = row * scale; \
-            psS32 inNextRow = (row + 1) * scale; \
-            for (psS32 col = 0; col < outCols; col++) { \
-                psS32 inCurrentCol = col * scale; \
-                psS32 inNextCol = (col + 1) * scale; \
-                psS32 n = 0; \
+#define PS_IMAGE_REBIN_CASE(TYPE)					\
+    case PS_TYPE_##TYPE: {						\
+        ps##TYPE *outRowData;						\
+        ps##TYPE *vecData = vec->data.TYPE;				\
+        psImageMaskType *inRowMask = NULL;				\
+        for (psS32 row = 0; row < outRows; row++) {			\
+            outRowData = out->data.TYPE[row];				\
+            psS32 inCurrentRow = row * scale;				\
+            psS32 inNextRow = (row + 1) * scale;			\
+            for (psS32 col = 0; col < outCols; col++) {			\
+                psS32 inCurrentCol = col * scale;			\
+                psS32 inNextCol = (col + 1) * scale;			\
+                psS32 n = 0;						\
                 for (psS32 inRow = inCurrentRow; inRow < inNextRow && inRow < inRows; inRow++) { \
-                    ps##TYPE* inRowData = in->data.TYPE[inRow]; \
-                    if (mask != NULL) { \
+                    ps##TYPE* inRowData = in->data.TYPE[inRow];		\
+                    if (mask != NULL) {					\
                         inRowMask = mask->data.PS_TYPE_IMAGE_MASK_DATA[inRow]; \
-                    } \
+                    }							\
                     for (psS32 inCol = inCurrentCol; inCol < inNextCol && inCol < inCols; inCol++) { \
-                        if (maskData != NULL) { \
+                        if (maskData != NULL) {				\
                             maskData[n] = (inRowMask[inCol] & maskVal); \
-                        } \
-                        vecData[n++] = inRowData[inCol]; \
-                    } \
-                } \
-                vec->n = n; \
-                if (maskVec) { \
-                    maskVec->n = n; \
-                } \
-                 psVectorStats(myStats, vec, NULL, maskVec, 0xff); /* the mask vector has only 0 or 1 */ \
-                outRowData[col] = (ps##TYPE)psStatsGetValue(myStats, statistic); \
-            } \
-        } \
-    } \
+                        }						\
+                        vecData[n++] = inRowData[inCol];		\
+                    }							\
+                }							\
+                vec->n = n;						\
+                if (maskVec) {						\
+                    maskVec->n = n;					\
+                }							\
+		if (!psVectorStats(myStats, vec, NULL, maskVec, 0xff)) { /* the mask vector has only 0 or 1 */ \
+		    psError(PS_ERR_UNKNOWN, false, "failure to measure stats"); \
+		    psFree(out);					\
+		    out = NULL;						\
+		    goto escape;					\
+		}							\
+		outRowData[col] = (ps##TYPE)psStatsGetValue(myStats, statistic); \
+	    }								\
+	}								\
+    }									\
     break;
 
@@ -161,7 +166,5 @@
             char* typeStr;
             PS_TYPE_NAME(typeStr,in->type.type);
-            psError(PS_ERR_BAD_PARAMETER_TYPE, true,
-                    _("Specified psImage type, %s, is not supported."),
-                    typeStr);
+            psError(PS_ERR_BAD_PARAMETER_TYPE, true, _("Specified psImage type, %s, is not supported."), typeStr);
             psFree(out);
             out = NULL;
@@ -169,4 +172,5 @@
     }
 
+escape:
     psFree(vec);
     psFree(maskVec);
Index: trunk/psLib/src/imageops/psImageMap.c
===================================================================
--- trunk/psLib/src/imageops/psImageMap.c	(revision 23988)
+++ trunk/psLib/src/imageops/psImageMap.c	(revision 23989)
@@ -211,18 +211,30 @@
             // XXX need to supply a mask and skip the masked pixels when calculating the centroid
             // this will not in general be properly weighted...
-            if (psVectorStats (map->stats, fCell, dfCell, NULL, 0)) {
+            if (!psVectorStats (map->stats, fCell, dfCell, NULL, 0)) {
+		psError(PS_ERR_UNKNOWN, false, "failure to measure stats");
+		return false;
+	    }
+
+	    // XXX ensure only one option is selected, or save both position and width
+	    map->map->data.F32[iy][ix] = psStatsGetValue (map->stats, map->stats->options);
+
+	    if (isnan(map->map->data.F32[iy][ix])) {
+                mask->data.PS_TYPE_IMAGE_MASK_DATA[iy][ix] = 1;
+	    } else {
                 mask->data.PS_TYPE_IMAGE_MASK_DATA[iy][ix] = 0;
-                // XXX ensure only one option is selected, or save both position and width
-                map->map->data.F32[iy][ix] = psStatsGetValue (map->stats, map->stats->options);
 
                 // calculate the mean position and save:
                 psStatsInit (meanStat);
-                psVectorStats (meanStat, xCell, NULL, NULL, 0);
+                if (!psVectorStats (meanStat, xCell, NULL, NULL, 0)) {
+		    psError(PS_ERR_UNKNOWN, false, "failure to measure stats");
+		    return false;
+		}
                 xCoord->data.F32[iy][ix] = psStatsGetValue (meanStat, meanStat->options);
                 psStatsInit (meanStat);
-                psVectorStats (meanStat, yCell, NULL, NULL, 0);
+                if (!psVectorStats (meanStat, yCell, NULL, NULL, 0)) {
+		    psError(PS_ERR_UNKNOWN, false, "failure to measure stats");
+		    return false;
+		}
                 yCoord->data.F32[iy][ix] = psStatsGetValue (meanStat, meanStat->options);
-            } else {
-                mask->data.PS_TYPE_IMAGE_MASK_DATA[iy][ix] = 1;
             }
 
Index: trunk/psLib/src/imageops/psImageMapFit.c
===================================================================
--- trunk/psLib/src/imageops/psImageMapFit.c	(revision 23988)
+++ trunk/psLib/src/imageops/psImageMapFit.c	(revision 23989)
@@ -73,5 +73,8 @@
 
         // XXX does ROBUST_MEDIAN work with weight?
-        psVectorStats(map->stats, f, NULL, mask, maskValue);
+        if (!psVectorStats(map->stats, f, NULL, mask, maskValue)) {
+	    psError(PS_ERR_UNKNOWN, false, "failure to measure stats");
+	    return false;
+	}
 
         map->map->data.F32[0][0]   = psStatsGetValue(map->stats, mean);
Index: trunk/psLib/src/imageops/psImagePixelExtract.c
===================================================================
--- trunk/psLib/src/imageops/psImagePixelExtract.c	(revision 23988)
+++ trunk/psLib/src/imageops/psImagePixelExtract.c	(revision 23989)
@@ -419,35 +419,40 @@
         }
 
-#define PSIMAGE_CUT_VERTICAL(TYPE) \
-    case PS_TYPE_##TYPE: { \
-            psVectorMaskType* maskVecData = NULL; \
-            for (psS32 c = col0; c < col1; c++) { \
-                ps##TYPE *imgData = input->data.TYPE[row0] + c; \
-                ps##TYPE *imgVecData = imgVec->data.TYPE; \
-                if (maskVec != NULL) { \
+#define PSIMAGE_CUT_VERTICAL(TYPE)					\
+	case PS_TYPE_##TYPE: {						\
+            psVectorMaskType* maskVecData = NULL;			\
+            for (psS32 c = col0; c < col1; c++) {			\
+                ps##TYPE *imgData = input->data.TYPE[row0] + c;		\
+                ps##TYPE *imgVecData = imgVec->data.TYPE;		\
+                if (maskVec != NULL) {					\
                     maskVecData = maskVec->data.PS_TYPE_VECTOR_MASK_DATA; \
                     maskData = &mask->data.PS_TYPE_IMAGE_MASK_DATA[row0][c]; /* XXX double check this... */ \
                     /** old entry: maskData = (psMaskType* )(mask->data.PS_TYPE_IMAGE_MASK_DATA[row0]) + c; */ \
-                } \
-                for (psS32 r = row0; r < row1; r++) { \
-                   *imgVecData = *imgData; \
-                    imgVecData ++; \
-                    imgData += inCols; \
-                    if (maskVecData != NULL) { \
-                        *maskVecData = (*maskData & maskVal); \
-                        maskVecData ++; \
-                        maskData += inCols; \
-                    } \
-                } \
-                psVectorStats(myStats, imgVec, NULL, maskVec, 0xff); \
-                *outData = psStatsGetValue(myStats, statistic); \
-                if (outPosition != NULL) { \
-                    outPosition->x = c; \
-                    outPosition->y = row0; \
-                    outPosition += delta; \
-                } \
-                outData += delta; \
-            } \
-            break; \
+                }							\
+                for (psS32 r = row0; r < row1; r++) {			\
+		    *imgVecData = *imgData;				\
+                    imgVecData ++;					\
+                    imgData += inCols;					\
+                    if (maskVecData != NULL) {				\
+                        *maskVecData = (*maskData & maskVal);		\
+                        maskVecData ++;					\
+                        maskData += inCols;				\
+                    }							\
+                }							\
+                if (!psVectorStats(myStats, imgVec, NULL, maskVec, 0xff)) { \
+		    psError(PS_ERR_UNKNOWN, false, "failure to measure stats"); \
+		    psFree(out);					\
+		    out = NULL;						\
+		    break;						\
+		}							\
+                *outData = psStatsGetValue(myStats, statistic);		\
+                if (outPosition != NULL) {				\
+                    outPosition->x = c;					\
+                    outPosition->y = row0;				\
+                    outPosition += delta;				\
+                }							\
+                outData += delta;					\
+            }								\
+            break;							\
         }
 
@@ -466,7 +471,5 @@
                 char* typeStr;
                 PS_TYPE_NAME(typeStr,type);
-                psError(PS_ERR_BAD_PARAMETER_TYPE, true,
-                        _("Specified psImage type, %s, is not supported."),
-                        typeStr);
+                psError(PS_ERR_BAD_PARAMETER_TYPE, true, _("Specified psImage type, %s, is not supported."), typeStr);
                 psFree(out);
                 out = NULL;
@@ -534,5 +537,10 @@
 	    }
 
-            psVectorStats(myStats, imgVec, NULL, maskVec, 0xff);
+            if (!psVectorStats(myStats, imgVec, NULL, maskVec, 0xff)) {
+		psError(PS_ERR_UNKNOWN, false, "failure to measure stats");
+		psFree (out);
+		out = NULL;
+		break;
+	    }
             *outData = psStatsGetValue(myStats, statistic);
             if (outPosition != NULL) {
@@ -933,5 +941,10 @@
 
     for (psS32 r = 0; r < numOut; r++) {
-        psVectorStats(myStats, buffer[r], NULL, bufferMask[r], 0xff);
+        if (!psVectorStats(myStats, buffer[r], NULL, bufferMask[r], 0xff)){
+	    psError(PS_ERR_UNKNOWN, false, "failure to measure stats");
+	    psFree(out);
+	    out = NULL;
+	    break;
+	}
         outData[r] = psStatsGetValue(myStats, statistic);
     }
Index: trunk/psLib/src/imageops/psImageStats.c
===================================================================
--- trunk/psLib/src/imageops/psImageStats.c	(revision 23988)
+++ trunk/psLib/src/imageops/psImageStats.c	(revision 23989)
@@ -104,5 +104,9 @@
     }
 
-    psVectorStats(stats, junkData, NULL, junkMask, 0xff);
+    if (!psVectorStats(stats, junkData, NULL, junkMask, 0xff)) {
+	psFree(junkMask);
+	psFree(junkData);
+	return false;
+    }
 
     psFree(junkMask);
Index: trunk/psModules/src/astrom/pmAstrometryDistortion.c
===================================================================
--- trunk/psModules/src/astrom/pmAstrometryDistortion.c	(revision 23988)
+++ 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 23988)
+++ 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 23988)
+++ 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 23988)
+++ 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 23988)
+++ 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 23988)
+++ 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 23988)
+++ 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 23988)
+++ 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 23988)
+++ 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 23988)
+++ 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 23988)
+++ 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 23988)
+++ 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 23988)
+++ 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 23988)
+++ 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 23988)
+++ 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 23988)
+++ 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 23988)
+++ 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
Index: trunk/psastro/src/psastroAstromGuess.c
===================================================================
--- trunk/psastro/src/psastroAstromGuess.c	(revision 23988)
+++ trunk/psastro/src/psastroAstromGuess.c	(revision 23989)
@@ -382,6 +382,12 @@
     psStats *statsQ = psStatsAlloc (PS_STAT_SAMPLE_MEAN | PS_STAT_SAMPLE_STDEV);
 
-    psVectorStats (statsP, cornerPd, NULL, cornerMK, 1);
-    psVectorStats (statsQ, cornerQd, NULL, cornerMK, 1);
+    if (!psVectorStats (statsP, cornerPd, NULL, cornerMK, 1)) {
+	psError(PS_ERR_UNKNOWN, false, "failure to measure stats");
+	return false;
+    }
+    if (!psVectorStats (statsQ, cornerQd, NULL, cornerMK, 1)) {
+	psError(PS_ERR_UNKNOWN, false, "failure to measure stats");
+	return false;
+    }
 
     float angle = atan2 (map->y->coeff[1][0], map->x->coeff[1][0]);
Index: trunk/psastro/src/psastroFixChipsTest.c
===================================================================
--- trunk/psastro/src/psastroFixChipsTest.c	(revision 23988)
+++ trunk/psastro/src/psastroFixChipsTest.c	(revision 23989)
@@ -140,5 +140,8 @@
     psStats *stats;
     stats = psStatsAlloc (PS_STAT_ROBUST_MEDIAN | PS_STAT_ROBUST_STDEV);
-    psVectorStats (stats, dX, NULL, NULL, 0);
+    if (!psVectorStats (stats, dX, NULL, NULL, 0)) {
+	psError(PS_ERR_UNKNOWN, false, "failure to measure stats");
+	return false;
+    }
     Xo = stats->robustMedian;
     fprintf (stderr, "offset x: %f +/- %f\n", stats->robustMedian, stats->robustStdev);
@@ -146,5 +149,8 @@
 
     stats = psStatsAlloc (PS_STAT_ROBUST_MEDIAN | PS_STAT_ROBUST_STDEV);
-    psVectorStats (stats, dY, NULL, NULL, 0);
+    if (!psVectorStats (stats, dY, NULL, NULL, 0)) {
+	psError(PS_ERR_UNKNOWN, false, "failure to measure stats");
+	return false;
+    }
     Yo = stats->robustMedian;
     fprintf (stderr, "offset y: %f +/- %f\n", stats->robustMedian, stats->robustStdev);
@@ -168,5 +174,8 @@
     }
     stats = psStatsAlloc (PS_STAT_ROBUST_MEDIAN | PS_STAT_ROBUST_STDEV);
-    psVectorStats (stats, dT, NULL, NULL, 0);
+    if (!psVectorStats (stats, dT, NULL, NULL, 0)) {
+	psError(PS_ERR_UNKNOWN, false, "failure to measure stats");
+	return false;
+    }
     float To = stats->robustMedian;
     fprintf (stderr, "offset t: %f +/- %f\n", stats->robustMedian, stats->robustStdev);
Index: trunk/psastro/src/psastroModelAnalysis.c
===================================================================
--- trunk/psastro/src/psastroModelAnalysis.c	(revision 23988)
+++ trunk/psastro/src/psastroModelAnalysis.c	(revision 23989)
@@ -153,5 +153,8 @@
 
     psStats *stats = psStatsAlloc (PS_STAT_SAMPLE_MEDIAN | PS_STAT_SAMPLE_STDEV);
-    psVectorStats (stats, posZero, NULL, NULL, 0);
+    if (!psVectorStats (stats, posZero, NULL, NULL, 0)) {
+	psError(PS_ERR_UNKNOWN, false, "failure to measure stats");
+	return false;
+    }
 
     fprintf (outfile, "# pos zero %f +/- %f\n", stats->sampleMedian, stats->sampleStdev);
Index: trunk/psastro/src/psastroModelFitBoresite.c
===================================================================
--- trunk/psastro/src/psastroModelFitBoresite.c	(revision 23988)
+++ trunk/psastro/src/psastroModelFitBoresite.c	(revision 23989)
@@ -62,10 +62,16 @@
 
     // center (Xo) = mean(Xo), RX = range / 2
-    psVectorStats (stats, Xo, NULL, NULL, 0);
+    if (!psVectorStats (stats, Xo, NULL, NULL, 0)) {
+	psError(PS_ERR_UNKNOWN, false, "failure to measure stats");
+	return NULL;
+    }
     params->data.F32[PAR_X0] = stats->sampleMean;
     params->data.F32[PAR_RX] = (stats->max - stats->min) / 2.0;
 
     // center (Yo) = mean(Yo), RY = range / 2
-    psVectorStats (stats, Yo, NULL, NULL, 0);
+    if (!psVectorStats (stats, Yo, NULL, NULL, 0)) {
+	psError(PS_ERR_UNKNOWN, false, "failure to measure stats");
+	return NULL;
+    }
     params->data.F32[PAR_Y0] = stats->sampleMean;
     params->data.F32[PAR_RY] = (stats->max - stats->min) / 2.0;
Index: trunk/psastro/src/psastroZeroPoint.c
===================================================================
--- trunk/psastro/src/psastroZeroPoint.c	(revision 23988)
+++ trunk/psastro/src/psastroZeroPoint.c	(revision 23989)
@@ -126,5 +126,8 @@
     // this analysis has too few data points to use the robust median method
     psStats *stats = psStatsAlloc (PS_STAT_CLIPPED_MEAN | PS_STAT_CLIPPED_STDEV);
-    psVectorStats (stats, dMag, NULL, NULL, 0);
+    if (!psVectorStats (stats, dMag, NULL, NULL, 0)) {
+	psError(PS_ERR_UNKNOWN, false, "failure to measure stats");
+	return false;
+    }
     fprintf (stderr, "zero point %f +/- %f using %d stars; transparency %f\n", stats->clippedMean, stats->clippedStdev, Npts, zeropt - stats->clippedMean);
 
Index: trunk/psphot/src/psphotApResid.c
===================================================================
--- trunk/psphot/src/psphotApResid.c	(revision 23988)
+++ trunk/psphot/src/psphotApResid.c	(revision 23989)
@@ -355,12 +355,15 @@
 
         if (j > 2) {
-            bool status = true;
-            status &= psVectorStats (statsS, dASubset, NULL, mkSubset, 0xff);
-            status &= psVectorStats (statsM, dMSubset, NULL, mkSubset, 0xff);
-            if (!status) { psErrorClear (); }
+            if (!psVectorStats (statsS, dASubset, NULL, mkSubset, 0xff)) {
+		psError(PS_ERR_UNKNOWN, false, "failure to measure stats");
+		return false;
+	    }
+            if (!psVectorStats (statsM, dMSubset, NULL, mkSubset, 0xff)) {
+		psError(PS_ERR_UNKNOWN, false, "failure to measure stats");
+		return false;
+	    }
             dSo->data.F32[i] = statsS->robustStdev;
             dMo->data.F32[i] = statsM->sampleMean;
             dRo->data.F32[i] = statsS->robustStdev / statsM->sampleMean;
-            //      fprintf (stderr, "%d (%d) : sys: %f, phot: %f, rat: %f\n", i, j, dSo->data.F32[i], dMo->data.F32[i], dRo->data.F32[i]);
         } else {
             dSo->data.F32[i] = NAN;
@@ -375,6 +378,6 @@
     psStats *stats = psStatsAlloc (PS_STAT_SAMPLE_MEDIAN);
     if (!psVectorStats (stats, dRo, NULL, NULL, 0)) {
-        // XXX better testing of raised error
-        psErrorClear();
+	psError(PS_ERR_UNKNOWN, false, "failure to measure stats");
+	return false;
     }
 
Index: trunk/psphot/src/psphotChoosePSF.c
===================================================================
--- trunk/psphot/src/psphotChoosePSF.c	(revision 23988)
+++ trunk/psphot/src/psphotChoosePSF.c	(revision 23989)
@@ -361,5 +361,9 @@
 
     psStats *stats = psStatsAlloc (PS_STAT_SAMPLE_MEAN | PS_STAT_SAMPLE_STDEV | PS_STAT_SAMPLE_QUARTILE);
-    psVectorStats (stats, fwhmMajor, NULL, NULL, 0);
+    if (!psVectorStats (stats, fwhmMajor, NULL, NULL, 0)) {
+        psError(PS_ERR_UNKNOWN, false, "failure to measure stats for FWHM MAJOR");
+        return false;
+    }
+
     psMetadataAddF32 (recipe, PS_LIST_TAIL, "FWHM_MAJ",   PS_META_REPLACE, "PSF FWHM Major axis (mean)", stats->sampleMean);
     psMetadataAddF32 (recipe, PS_LIST_TAIL, "FW_MJ_SG",   PS_META_REPLACE, "PSF FWHM Major axis (sigma)", stats->sampleStdev);
@@ -367,5 +371,8 @@
     psMetadataAddF32 (recipe, PS_LIST_TAIL, "FW_MJ_UQ",   PS_META_REPLACE, "PSF FWHM Major axis (upper quartile)", stats->sampleUQ);
 
-    psVectorStats (stats, fwhmMinor, NULL, NULL, 0);
+    if (!psVectorStats (stats, fwhmMinor, NULL, NULL, 0)) {
+        psError(PS_ERR_UNKNOWN, false, "failure to measure stats for FWHM MINOR");
+        return false;
+    }
     psMetadataAddF32 (recipe, PS_LIST_TAIL, "FWHM_MIN",   PS_META_REPLACE, "PSF FWHM Minor axis (mean)", stats->sampleMean);
     psMetadataAddF32 (recipe, PS_LIST_TAIL, "FW_MN_SG",   PS_META_REPLACE, "PSF FWHM Minor axis (sigma)", stats->sampleStdev);
Index: trunk/psphot/src/psphotDiagnosticPlots.c
===================================================================
--- trunk/psphot/src/psphotDiagnosticPlots.c	(revision 23988)
+++ trunk/psphot/src/psphotDiagnosticPlots.c	(revision 23989)
@@ -40,5 +40,8 @@
 
     psStats *stats = psStatsAlloc (PS_STAT_MAX | PS_STAT_MIN);
-    psVectorStats (stats, values, NULL, NULL, 0);
+    if (!psVectorStats (stats, values, NULL, NULL, 0)) {
+	psError(PS_ERR_UNKNOWN, false, "failure to measure stats for histogram");
+        return false;
+    }
 
     psHistogram *histogram = psHistogramAlloc (stats->min, stats->max, 1000);
@@ -63,5 +66,8 @@
     psStatsInit (stats);
     stats->options = PS_STAT_MAX | PS_STAT_MIN;
-    psVectorStats (stats, histogram->nums, NULL, NULL, 0);
+    if (!psVectorStats (stats, histogram->nums, NULL, NULL, 0)) {
+	psError(PS_ERR_UNKNOWN, false, "failure to measure stats for histogram");
+        return false;
+    }
 
     // scale the plot to hold the histogram
@@ -100,5 +106,8 @@
     psStatsInit (stats);
     stats->options = PS_STAT_MAX | PS_STAT_MIN;
-    psVectorStats (stats, histogram->nums, NULL, NULL, 0);
+    if (!psVectorStats (stats, histogram->nums, NULL, NULL, 0)) {
+	psError(PS_ERR_UNKNOWN, false, "failure to measure stats for histogram");
+        return false;
+    }
 
     // scale the plot to hold the histogram
Index: trunk/psphot/src/psphotImageQuality.c
===================================================================
--- trunk/psphot/src/psphotImageQuality.c	(revision 23988)
+++ trunk/psphot/src/psphotImageQuality.c	(revision 23989)
@@ -88,4 +88,5 @@
                      "Number of stars used for IQ measurements", M2->n);
 
+// XXX make this a recipe option
 #if (USE_SAMPLE)
     psStats *stats = psStatsAlloc (PS_STAT_SAMPLE_MEAN | PS_STAT_SAMPLE_STDEV | PS_STAT_SAMPLE_QUARTILE);
Index: trunk/psphot/src/psphotMakeResiduals.c
===================================================================
--- trunk/psphot/src/psphotMakeResiduals.c	(revision 23988)
+++ trunk/psphot/src/psphotMakeResiduals.c	(revision 23989)
@@ -200,6 +200,15 @@
             // measure the robust median to determine a baseline reference value
             *fluxClip = *fluxClipDef;
-            psVectorStats (fluxClip, fluxes, NULL, fmasks, fmaskVal);
-            psErrorClear();             // clear (ignore) any outstanding errors
+            if (!psVectorStats (fluxClip, fluxes, NULL, fmasks, fmaskVal)) {
+		psError(PSPHOT_ERR_CONFIG, false, "Error calculating residual stats");
+		return false;
+	    }
+	    if (isnan(fluxClip->robustMedian)) {
+                resid->Ro->data.F32[oy][ox] = 0.0;
+                resid->Rx->data.F32[oy][ox] = 0.0;
+                resid->Ry->data.F32[oy][ox] = 0.0;
+                resid->mask->data.PM_TYPE_RESID_MASK_DATA[oy][ox] = badMask;
+                continue;
+	    }
 
             // mark input pixels which are more than N sigma from the median
@@ -220,10 +229,19 @@
                 // measure the desired statistic on the unclipped pixels
                 *fluxStats = *fluxStatsDef;
-                psVectorStats (fluxStats, fluxes, NULL, fmasks, fmaskVal);
-                psErrorClear();         // clear (ignore) any outstanding errors
+                if (!psVectorStats (fluxStats, fluxes, NULL, fmasks, fmaskVal)) {
+		    psError(PSPHOT_ERR_CONFIG, false, "Error calculating residual stats");
+		    return false;
+		}
 
                 resid->Ro->data.F32[oy][ox] = psStatsGetValue(fluxStats, statOption);
                 resid->Rx->data.F32[oy][ox] = resid->Ry->data.F32[oy][ox] = 0.0;
-                //resid->variance->data.F32[oy][ox] = fluxStats->sampleStdev;
+
+		if (isnan(resid->Ro->data.F32[oy][ox])) {
+		    resid->Ro->data.F32[oy][ox] = 0.0;
+		    resid->Rx->data.F32[oy][ox] = 0.0;
+		    resid->Ry->data.F32[oy][ox] = 0.0;
+		    resid->mask->data.PM_TYPE_RESID_MASK_DATA[oy][ox] = badMask;
+		    continue;
+		}
 
                 if (fabs(resid->Ro->data.F32[oy][ox]) < pixelSN*fluxStats->sampleStdev/sqrt(nKeep)) {
Index: trunk/psphot/src/psphotRoughClass.c
===================================================================
--- trunk/psphot/src/psphotRoughClass.c	(revision 23988)
+++ trunk/psphot/src/psphotRoughClass.c	(revision 23989)
@@ -91,5 +91,5 @@
 	return false;
     }
-    if (!psfClump.X || !psfClump.Y) {
+    if (!psfClump.X || !psfClump.Y || isnan(psfClump.X) || isnan(psfClump.Y)) {
 	psLogMsg ("psphot", 4, "Failed to find a valid PSF clump for region %f,%f - %f,%f\n", region->x0, region->y0, region->x1, region->y1);
 	return false;
