Index: /branches/eam_branches/ipp-20101205/psLib/src/imageops/psImageMapFit.c
===================================================================
--- /branches/eam_branches/ipp-20101205/psLib/src/imageops/psImageMapFit.c	(revision 30103)
+++ /branches/eam_branches/ipp-20101205/psLib/src/imageops/psImageMapFit.c	(revision 30104)
@@ -48,10 +48,10 @@
 
 // map defines the output image dimensions and scaling.
-bool psImageMapFit(bool *goodFit, psImageMap *map, const psVector *mask, psVectorMaskType maskValue,
+bool psImageMapFit(bool *pGoodFit, psImageMap *map, const psVector *mask, psVectorMaskType maskValue,
                    const psVector *x, const psVector *y, const psVector *f, const psVector *df)
 {
     // XXX Add Asserts
 
-    *goodFit = false;
+    *pGoodFit = false;
 
     // dimensions of the output map image
@@ -83,5 +83,7 @@
         map->map->data.F32[0][0]   = psStatsGetValue(map->stats, mean);
         map->error->data.F32[0][0] = psStatsGetValue(map->stats, stdev);
-	*goodFit = true;
+        if (isfinite(map->map->data.F32[0][0]) && isfinite( map->error->data.F32[0][0])) {
+            *pGoodFit = true;
+        }
         return true;
     }
@@ -89,10 +91,10 @@
     if (Nx == 1) {
         bool status;
-        status = psImageMapFit1DinY (goodFit, map, mask, maskValue, x, y, f, df);
+        status = psImageMapFit1DinY (pGoodFit, map, mask, maskValue, x, y, f, df);
         return status;
     }
     if (Ny == 1) {
         bool status;
-        status = psImageMapFit1DinX (goodFit, map, mask, maskValue, x, y, f, df);
+        status = psImageMapFit1DinX (pGoodFit, map, mask, maskValue, x, y, f, df);
         return status;
     }
@@ -339,10 +341,10 @@
     psFree (Empty);
 
-    *goodFit = true;
+    *pGoodFit = true;
     return true;
 }
 
 // measure residuals on each pass and clip outliers based on stats
-bool psImageMapClipFit(bool *goodFit, psImageMap *map, psStats *stats, psVector *inMask, psVectorMaskType maskValue,
+bool psImageMapClipFit(bool *pGoodFit, psImageMap *map, psStats *stats, psVector *inMask, psVectorMaskType maskValue,
                        const psVector *x, const psVector *y, const psVector *f, const psVector *df)
 {
@@ -354,5 +356,5 @@
     psAssert(f, "impossible");
 
-    *goodFit = false;
+    *pGoodFit = false;
 
     // the user supplies one of various stats option pairs,
@@ -398,5 +400,5 @@
         psTrace("psLib.imageops", 6, "Loop iteration %d.  Calling psImageMapFit()\n", N);
         psS32 Nkeep = 0;
-        if (!psImageMapFit(goodFit, map, mask, maskValue, x, y, f, df)) {
+        if (!psImageMapFit(pGoodFit, map, mask, maskValue, x, y, f, df)) {
             psError(PS_ERR_UNKNOWN, false, "Could not fit image map.\n");
             psFree(resid);
@@ -404,5 +406,5 @@
             return false;
         }
-	if (!goodFit) {
+	if (!*pGoodFit) {
 	    psWarning ("bad fit to image map, try something else");
 	    return true;
@@ -463,10 +465,10 @@
     psFree(resid);
     if (!inMask) psFree (mask);
-    *goodFit = true; // XXX probably don't need to set this (set by psImageMapFit)
+    *pGoodFit = true; // XXX probably don't need to set this (set by psImageMapFit)
     return true;
 }
 
 // map defines the output image dimensions and scaling.
-bool psImageMapFit1DinY(bool *goodFit, psImageMap *map, const psVector *mask, psVectorMaskType maskValue,
+bool psImageMapFit1DinY(bool *pGoodFit, psImageMap *map, const psVector *mask, psVectorMaskType maskValue,
                         const psVector *x, const psVector *y, const psVector *f, const psVector *df)
 {
@@ -474,5 +476,5 @@
     assert (map->binning->nXruff == 1);
 
-    *goodFit = false;
+    *pGoodFit = false;
 
     // dimensions of the output map image
@@ -613,10 +615,10 @@
     psFree (Empty);
 
-    *goodFit = true;
+    *pGoodFit = true;
     return true;
 }
 
 // map defines the output image dimensions and scaling.
-bool psImageMapFit1DinX(bool *goodFit, psImageMap *map, const psVector *mask, psVectorMaskType maskValue,
+bool psImageMapFit1DinX(bool *pGoodFit, psImageMap *map, const psVector *mask, psVectorMaskType maskValue,
                         const psVector *x, const psVector *y, const psVector *f, const psVector *df)
 {
@@ -624,5 +626,5 @@
     assert (map->binning->nYruff == 1);
 
-    *goodFit = false;
+    *pGoodFit = false;
 
     // dimensions of the output map image
@@ -763,5 +765,5 @@
     psFree (Empty);
 
-    *goodFit = true;
+    *pGoodFit = true;
     return true;
 }
Index: /branches/eam_branches/ipp-20101205/psLib/src/imageops/psImageMapFit.h
===================================================================
--- /branches/eam_branches/ipp-20101205/psLib/src/imageops/psImageMapFit.h	(revision 30103)
+++ /branches/eam_branches/ipp-20101205/psLib/src/imageops/psImageMapFit.h	(revision 30104)
@@ -8,5 +8,5 @@
 
 // fit the image map to a set of points
-bool psImageMapFit(bool *goodFit, 
+bool psImageMapFit(bool *pGoodFit, 
 		   psImageMap *map,
                    const psVector *mask,
@@ -19,5 +19,5 @@
 
 // fit the image map to a set of points
-bool psImageMapClipFit(bool *goodFit, 
+bool psImageMapClipFit(bool *pGoodFit, 
 		       psImageMap *map,
                        psStats *stats,
@@ -30,5 +30,5 @@
     );
 
-bool psImageMapFit1DinY(bool *goodFit, 
+bool psImageMapFit1DinY(bool *pGoodFit, 
 			psImageMap *map,
                         const psVector *mask,
@@ -40,5 +40,5 @@
     );
 
-bool psImageMapFit1DinX(bool *goodFit, 
+bool psImageMapFit1DinX(bool *pGoodFit, 
 			psImageMap *map,
                         const psVector *mask,
Index: /branches/eam_branches/ipp-20101205/psLib/src/math/psStats.c
===================================================================
--- /branches/eam_branches/ipp-20101205/psLib/src/math/psStats.c	(revision 30103)
+++ /branches/eam_branches/ipp-20101205/psLib/src/math/psStats.c	(revision 30104)
@@ -827,5 +827,5 @@
         // values; nearly bi-modal distribution).  if so, keep only points within 5? 10?
         // bins of that excess bin:
-        int nMaxBin = 0;
+        int nMaxBin = histogram->nums->data.F32[0];
         int iMaxBin = 0;
         for (long i = 1; i < histogram->nums->n; i++) {
@@ -843,6 +843,6 @@
                 if (mask->data.PS_TYPE_VECTOR_MASK_DATA[i] & maskVal) continue;
                 bool invalid = false;
-                invalid |= (myVector->data.F32[i] <= minKeep);
-                invalid |= (myVector->data.F32[i] >= maxKeep);
+                invalid |= (myVector->data.F32[i] < minKeep);
+                invalid |= (myVector->data.F32[i] > maxKeep);
                 invalid |= (!isfinite(myVector->data.F32[i]));
                 if (!invalid) continue;
@@ -852,5 +852,6 @@
 
             if (nInvalid) {
-              psTrace(TRACE, 6, "data is concentrated in a single bin, masking %d extreme outliers and retrying\n", nInvalid);
+              psTrace(TRACE, 6, "data is concentrated in a single bin (%d = %f - %f), masking %d extreme outliers and retrying\n", 
+		      iMaxBin, histogram->bounds->data.F32[iMaxBin], histogram->bounds->data.F32[iMaxBin+1], nInvalid);
               psFree(histogram);
               psFree(cumulative);
@@ -1108,6 +1109,16 @@
     // If the mean is NAN, then generate a warning and set the stdev to NAN.
     if (isnan(stats->robustMedian)) {
-        stats->fittedStdev = NAN;
-        stats->fittedStdev = NAN;
+	stats->fittedMean = NAN;
+	stats->fittedStdev = NAN;
+	stats->results |= PS_STAT_FITTED_MEAN;
+	stats->results |= PS_STAT_FITTED_STDEV;
+        return true;
+    }
+
+    if (stats->robustStdev <= FLT_EPSILON) {
+	stats->fittedMean = stats->robustMedian;
+	stats->fittedStdev = stats->robustStdev;
+	stats->results |= PS_STAT_FITTED_MEAN;
+	stats->results |= PS_STAT_FITTED_STDEV;
         return true;
     }
@@ -1289,7 +1300,16 @@
     // If the mean is NAN, then generate a warning and set the stdev to NAN.
     if (isnan(stats->robustMedian)) {
-        stats->fittedStdev = NAN;
-        stats->fittedStdev = NAN;
-        psTrace(TRACE, 4, "---- %s() end ----\n", __func__);
+	stats->fittedMean = NAN;
+	stats->fittedStdev = NAN;
+	stats->results |= PS_STAT_FITTED_MEAN_V2;
+	stats->results |= PS_STAT_FITTED_STDEV_V2;
+        return true;
+    }
+
+    if (stats->robustStdev <= FLT_EPSILON) {
+	stats->fittedMean = stats->robustMedian;
+	stats->fittedStdev = stats->robustStdev;
+	stats->results |= PS_STAT_FITTED_MEAN_V2;
+	stats->results |= PS_STAT_FITTED_STDEV_V2;
         return true;
     }
@@ -1486,7 +1506,16 @@
     // If the mean is NAN, then generate a warning and set the stdev to NAN.
     if (isnan(stats->robustMedian)) {
-        stats->fittedStdev = NAN;
-        stats->fittedStdev = NAN;
-        psTrace(TRACE, 4, "---- %s() end ----\n", __func__);
+	stats->fittedMean = NAN;
+	stats->fittedStdev = NAN;
+	stats->results |= PS_STAT_FITTED_MEAN_V3;
+	stats->results |= PS_STAT_FITTED_STDEV_V3;
+        return true;
+    }
+
+    if (stats->robustStdev <= FLT_EPSILON) {
+	stats->fittedMean = stats->robustMedian;
+	stats->fittedStdev = stats->robustStdev;
+	stats->results |= PS_STAT_FITTED_MEAN_V3;
+	stats->results |= PS_STAT_FITTED_STDEV_V3;
         return true;
     }
@@ -1782,5 +1811,19 @@
 
     // If the mean is NAN, then generate a warning and set the stdev to NAN.
-    if (isnan(stats->robustMedian)) goto escape;
+    if (isnan(stats->robustMedian)) {
+	stats->fittedMean = NAN;
+	stats->fittedStdev = NAN;
+	stats->results |= PS_STAT_FITTED_MEAN_V4;
+	stats->results |= PS_STAT_FITTED_STDEV_V4;
+        return true;
+    }
+
+    if (stats->robustStdev <= FLT_EPSILON) {
+	stats->fittedMean = stats->robustMedian;
+	stats->fittedStdev = stats->robustStdev;
+	stats->results |= PS_STAT_FITTED_MEAN_V4;
+	stats->results |= PS_STAT_FITTED_STDEV_V4;
+        return true;
+    }
 
     float guessStdev = stats->robustStdev;  // pass the guess sigma
Index: /branches/eam_branches/ipp-20101205/psLib/src/types/psMetadata.c
===================================================================
--- /branches/eam_branches/ipp-20101205/psLib/src/types/psMetadata.c	(revision 30103)
+++ /branches/eam_branches/ipp-20101205/psLib/src/types/psMetadata.c	(revision 30104)
@@ -1371,5 +1371,5 @@
             break;
         case PS_DATA_S64:
-            fprintf(fd, "%jd\n", item->data.S64);
+            fprintf(fd, "%" PRId64 "\n", item->data.S64);
             break;
         case PS_DATA_U8:
@@ -1383,5 +1383,5 @@
             break;
         case PS_DATA_U64:
-            fprintf(fd, "%ju\n", item->data.U64);
+            fprintf(fd, "%" PRIu64 "\n", item->data.U64);
             break;
         case PS_DATA_F32:
@@ -1458,5 +1458,5 @@
               fprintf(fd, "U64  ");
               for (int i = 0; i < vector->n; i++) {
-                  fprintf(fd, "%ju ", vector->data.U64[i]);
+                  fprintf(fd, "%" PRIu64, vector->data.U64[i]);
               }
               fprintf(fd, "\n");
@@ -1486,5 +1486,5 @@
               fprintf(fd, "S64  ");
               for (int i = 0; i < vector->n; i++) {
-                  fprintf(fd, "%jd ", vector->data.S64[i]);
+                  fprintf(fd, "%" PRId64, vector->data.S64[i]);
               }
               fprintf(fd, "\n");
