Index: /trunk/psphot/src/psphotImageMedian.c
===================================================================
--- /trunk/psphot/src/psphotImageMedian.c	(revision 10552)
+++ /trunk/psphot/src/psphotImageMedian.c	(revision 10553)
@@ -40,5 +40,5 @@
     }
     psStatsOptions statsOption = psStatsOptionFromString (statsName);
-    if (!(statsOption & (PS_STAT_SAMPLE_MEAN | PS_STAT_SAMPLE_MEDIAN | PS_STAT_ROBUST_MEDIAN | PS_STAT_ROBUST_QUARTILE | PS_STAT_CLIPPED_MEAN | PS_STAT_FITTED_MEAN))) {
+    if (!(statsOption & (PS_STAT_SAMPLE_MEAN | PS_STAT_SAMPLE_MEDIAN | PS_STAT_ROBUST_MEDIAN | PS_STAT_ROBUST_QUARTILE | PS_STAT_CLIPPED_MEAN | PS_STAT_FITTED_MEAN | PS_STAT_FITTED_MEAN_V2))) {
 	statsOption = PS_STAT_FITTED_MEAN;
     }
@@ -141,9 +141,12 @@
 		stats->options |= PS_STAT_ROBUST_MEDIAN;
 		if (!psImageBackground(stats, subset, submask, 0xff, rng)) {
-		    psAbort(PS_FILE_LINE, "Failed to estimate background using ROBUST_MEDIAN for "
-			    "(%dx%d, (row0,col0) = (%d,%d)",
-			    subset->numRows, subset->numCols, subset->row0, subset->col0);
+		    psLogMsg ("psphot", PS_LOG_WARN, "Failed to estimate background using ROBUST_MEDIAN for "
+			       "(%dx%d, (row0,col0) = (%d,%d)",
+			       subset->numRows, subset->numCols, subset->row0, subset->col0);
+		    modelData[iy][ix] = NAN;
+		} else {
+		    modelData[iy][ix] = psStatsGetValue (stats, PS_STAT_ROBUST_MEDIAN);
 		}
-		modelData[iy][ix] = psStatsGetValue (stats, PS_STAT_ROBUST_MEDIAN);
+		psErrorClear(); // drop errors caused by this condition
 		stats->options = currentOptions;
 	    }
@@ -158,4 +161,41 @@
             // backMdl->data.F32[iy][ix] = stats->clippedMean;
         }
+    }
+
+    // patch over bad regions (use average of 8 possible neighbor pixels)
+    float Count = 0;
+    float Value = 0;
+    for (int iy = 0; iy < ny; iy++) {
+        for (int ix = 0; ix < nx; ix++) {
+	    if (!isnan(modelData[iy][ix])) {
+		Value += modelData[iy][ix];
+		Count += 1;
+		continue;
+	    }
+	    float value = 0;
+	    float count = 0;
+	    for (int jy = iy - 1; jy <= iy + 1; jy++) {
+		if (jy <   0) continue;
+		if (jy >= ny) continue;
+		for (int jx = ix - 1; jx <= ix + 1; jx++) {
+		    if (!jx && !jy) continue;
+		    if (jx   <   0) continue;
+		    if (jx   >= nx) continue;
+		    value += modelData[jy][jx];
+		    count += 1.0;
+		}
+	    }
+	    if (count > 0) modelData[iy][ix] = value / count;
+	    
+	}
+    }
+    Value = Value / Count;
+
+    // patch over remaining bad regions (use global average)
+    for (int iy = 0; iy < ny; iy++) {
+        for (int ix = 0; ix < nx; ix++) {
+	    if (!isnan(modelData[iy][ix])) continue;
+	    modelData[iy][ix] = Value;
+	}
     }
     psLogMsg ("psphot", 3, "build median image: %f sec\n", psTimerMark ("psphot"));
