Index: /trunk/psLib/src/imageops/psImageMap.c
===================================================================
--- /trunk/psLib/src/imageops/psImageMap.c	(revision 25752)
+++ /trunk/psLib/src/imageops/psImageMap.c	(revision 25753)
@@ -386,2 +386,32 @@
     return result;
 }
+
+bool psImageMapCleanup (psImageMap *map) {
+
+    if ((map->map->numRows == 1) && (map->map->numCols == 1)) return true;
+
+    // find the weighted average of all pixels
+    float Sum = 0.0;
+    float Wt = 0.0;
+    for (int j = 0; j < map->map->numRows; j++) {
+        for (int i = 0; i < map->map->numCols; i++) {
+            if (!isfinite(map->map->data.F32[j][i])) continue;
+            Sum += map->map->data.F32[j][i] * map->error->data.F32[j][i];
+            Wt += map->error->data.F32[j][i];
+        }
+    }
+
+    float Mean = Sum / Wt;
+
+    // do any of the pixels in the map need to be repaired?
+    // XXX for now, we are just replacing bad pixels with the Mean
+    for (int j = 0; j < map->map->numRows; j++) {
+        for (int i = 0; i < map->map->numCols; i++) {
+            if (isfinite(map->map->data.F32[j][i]) &&
+                (map->error->data.F32[j][i] > 0.0)) continue;
+            map->map->data.F32[j][i] = Mean;
+        }
+    }
+    return true;
+}
+
Index: /trunk/psLib/src/imageops/psImageMap.h
===================================================================
--- /trunk/psLib/src/imageops/psImageMap.h	(revision 25752)
+++ /trunk/psLib/src/imageops/psImageMap.h	(revision 25753)
@@ -79,4 +79,6 @@
 psVector *psImageMapEvalVector (const psImageMap *map, const psVector *mask, psMaskType maskValue, const psVector *x, const psVector *y);
 
+bool psImageMapCleanup (psImageMap *map);
+
 /// @}
 #endif // #ifndef PS_IMAGE_MAP_H
Index: /trunk/psLib/src/imageops/psImageMapFit.c
===================================================================
--- /trunk/psLib/src/imageops/psImageMapFit.c	(revision 25752)
+++ /trunk/psLib/src/imageops/psImageMapFit.c	(revision 25753)
@@ -33,4 +33,5 @@
 #include "psStats.h"
 #include "psImageBinning.h"
+#include "psImageStructManip.h"
 #include "psImageMap.h"
 // #include "psImagePixelInterpolate.h"
@@ -752,2 +753,65 @@
 }
 
+// this function repairs an image with NAN pixels (only valid for a small-scale map -- no robust mean)
+bool psImageMapRepair (psImage *image) {
+
+    // we are going to repair the image by:
+    // 1) finding NAN pixels
+    // 2) if any of the neighbors are valid,
+    //    replace with the mean of the neighbors
+    // 3) otherwise, replace with the image mean
+
+    // copy the image so the repaired pixels do not affect the input
+    psImage *fixed = psImageCopy (NULL, image, PS_TYPE_F32);
+
+    // find the global mean
+    float mean = 0.0;
+    float npix = 0.0;
+    for (int iy = 0; iy < image->numRows; iy++) {
+	for (int ix = 0; ix < image->numCols; ix++) {
+	    if (!isfinite(image->data.F32[iy][ix])) continue;
+	    mean += image->data.F32[iy][ix];
+	    npix += 1.0;
+	}
+    }
+    mean /= npix;
+
+    // find the NAN pixels:
+    for (int iy = 0; iy < image->numRows; iy++) {
+	for (int ix = 0; ix < image->numCols; ix++) {
+	    if (isfinite(image->data.F32[iy][ix])) {
+		fixed->data.F32[iy][ix] = image->data.F32[iy][ix];
+		continue;
+	    }
+
+	    // find mean of all possible neighbors
+	    float meanLocal = 0.0;
+	    float npixLocal = 0.0;
+	    for (int jy = -1; jy <= +1; jy++) {
+		int ny = iy + jy;
+		if (ny < 0) continue;
+		if (ny >= image->numRows) continue;
+		for (int jx = -1; jx <= +1; jx++) {
+		    int nx = ix + jx;
+		    if (nx < 0) continue;
+		    if (nx >= image->numCols) continue;
+		    if (!isfinite(image->data.F32[ny][nx])) continue;
+		    meanLocal += image->data.F32[ny][nx];
+		    npixLocal += 1.0;
+		}
+	    }
+	    meanLocal = (npixLocal > 0.0) ? meanLocal / npixLocal : mean;
+	    fixed->data.F32[iy][ix] = meanLocal;
+	}
+    }
+    
+    // find the NAN pixels:
+    for (int iy = 0; iy < image->numRows; iy++) {
+	for (int ix = 0; ix < image->numCols; ix++) {
+	    image->data.F32[iy][ix] = fixed->data.F32[iy][ix];
+	}
+    }
+    psFree (fixed);
+
+    return true;
+}
Index: /trunk/psLib/src/imageops/psImageMapFit.h
===================================================================
--- /trunk/psLib/src/imageops/psImageMapFit.h	(revision 25752)
+++ /trunk/psLib/src/imageops/psImageMapFit.h	(revision 25753)
@@ -46,3 +46,5 @@
     );
 
+bool psImageMapRepair (psImage *image);
+
 #endif
Index: /trunk/psLib/src/imageops/psImageMaskOps.c
===================================================================
--- /trunk/psLib/src/imageops/psImageMaskOps.c	(revision 25752)
+++ /trunk/psLib/src/imageops/psImageMaskOps.c	(revision 25753)
@@ -223,4 +223,41 @@
     psError(PS_ERR_BAD_PARAMETER_VALUE,true,
 	    "The logical operation specified is incorrect\n");
+    return;
+}
+
+// perform the mask operation on the image pixels
+void psImageMaskPixels(psImage *image,
+                       const char *op,
+                       psImageMaskType maskValue)
+{
+    if (image == NULL) {
+        psError(PS_ERR_BAD_PARAMETER_NULL, true, "Invalid image input.  Image is NULL.\n");
+        return;
+    }
+
+# define MASK_IT_IMAGE(OP) \
+    for (int iy = 0; iy < image->numRows; iy++) { \
+        for (int ix = 0; ix < image->numCols; ix++) { \
+	    image->data.PS_TYPE_IMAGE_MASK_DATA[iy][ix] OP maskValue; \
+	} }
+
+    if ( !strncmp(op, "&", 2) || !strncmp(op, "AND", 5) ) {
+      MASK_IT_IMAGE (&=);
+      return;
+    } 
+    if ( !strncmp(op, "|", 2) || !strncmp(op, "OR", 5) ) {
+      MASK_IT_IMAGE (|=);
+      return;
+    } 
+    if ( !strncmp(op, "=", 2) || !strncmp(op, "EQUAL", 5) ) {
+      MASK_IT_IMAGE (=);
+      return;
+    } 
+    if ( !strncmp(op, "^", 2) || !strncmp(op, "XOR", 5) ) {
+      MASK_IT_IMAGE (^=);
+      return;
+    } 
+
+    psError(PS_ERR_BAD_PARAMETER_VALUE, true, "The logical operation specified is incorrect\n");
     return;
 }
Index: /trunk/psLib/src/imageops/psImageMaskOps.h
===================================================================
--- /trunk/psLib/src/imageops/psImageMaskOps.h	(revision 25752)
+++ /trunk/psLib/src/imageops/psImageMaskOps.h	(revision 25753)
@@ -22,4 +22,13 @@
 #include "psStats.h"
 #include "psPixels.h"
+
+/** Perform the mask opertion on all image pixels
+ *
+ *  The pixels are set by combining the existing pixel value and the given maskValue
+ *  with a logical operation.  The allowed operations are =, AND, OR, and XOR.
+ */
+void psImageMaskPixels(psImage *image,
+                       const char *op,
+                       psImageMaskType maskValue);
 
 /** Sets the bits inside the region, ignoring pixels outside.
