Index: /branches/eam_branch_20080620/ppMerge/src/ppMergeMask.c
===================================================================
--- /branches/eam_branch_20080620/ppMerge/src/ppMergeMask.c	(revision 18257)
+++ /branches/eam_branch_20080620/ppMerge/src/ppMergeMask.c	(revision 18258)
@@ -37,4 +37,7 @@
     psMaskType maskGrowVal = psMetadataLookupU8(NULL, config->arguments, "MASK.GROWVAL"); // Value for grown mask
 
+    bool smoothSuspect = psMetadataLookupBool(&mdok, config->arguments, "MASK.SMOOTH.SUSPECT"); // Radius to grow mask
+    float smoothScale = psMetadataLookupF32(&mdok, config->arguments, "MASK.SMOOTH.SCALE"); // Radius to grow mask
+
     psStats *statistics = psStatsAlloc(meanStat | stdevStat); // Statistics for background
 
@@ -42,4 +45,15 @@
     pmChip *outChip = pmFPAfileThisChip(config->files, view, outName); // Output chip
     psFree(outName);
+    
+    int numCells = 1;
+    if (chipStats) {
+	// count the number of active cells for this chip:
+	numCells = 0;
+	for (int i = 0; i < outChip->cells->n; i++) {
+	    pmCell *cell = outChip->cells->data[i];
+	    if (!cell->process) continue;
+	    numCells ++;
+	}
+    }
 
     // For each input file, get the statistics, which can be calculated at the chip or cell levels
@@ -58,4 +72,9 @@
         pmCell *inCell;                 // Input cell
         while ((inCell = pmFPAviewNextCell(inView, inFPA, 1))) {
+
+	    // the output FPA structure carries the information about which cells to process
+            pmCell *outCell = pmFPAfileThisCell(config->files, inView, "PPMERGE.OUTPUT.MASK"); // Output cell
+	    if (!outCell->process) continue;
+
             pmHDU *hdu = pmHDUFromCell(inCell); // HDU for cell
             if (!hdu || hdu->blankPHU) {
@@ -99,5 +118,4 @@
             }
 
-            pmCell *outCell = pmFPAfileThisCell(config->files, inView, "PPMERGE.OUTPUT.MASK"); // Output cell
             pmReadout *outRO = NULL;    // Output readout
             if (outCell->readouts && outCell->readouts->n == 1) {
@@ -111,5 +129,4 @@
             int numCols = readout->image->numCols, numRows = readout->image->numRows; // Image size
             int numPix = numCols * numRows; // Number of pixels
-            int numCells = chipStats ? readout->parent->parent->cells->n : 1; // Number of cells
             int num = PS_MIN(numPix, sample / numCells); // Number of values to add
             if (!chipStats) {
@@ -166,4 +183,9 @@
             inView->cell = -1;
             while ((inCell = pmFPAviewNextCell(inView, inFPA, 1))) {
+
+		// the output FPA structure carries the information about which cells to process
+		pmCell *outCell = pmFPAfileThisCell(config->files, inView, "PPMERGE.OUTPUT.MASK"); // Output cell
+		if (!outCell->process) continue;
+
                 pmHDU *hdu = pmHDUFromCell(inCell); // HDU for cell
                 if (!hdu || hdu->blankPHU) {
@@ -206,4 +228,8 @@
     *outView = *view;
     while ((outCell = pmFPAviewNextCell(outView, outFPA, 1))) {
+
+	// skipp inactive cells
+	if (!outCell->process) continue;
+
         pmHDU *hdu = pmHDUFromCell(outCell); // HDU for cell
         if (!hdu || hdu->blankPHU) {
@@ -216,4 +242,12 @@
         assert(outCell->readouts && outCell->readouts->n == 1);
         pmReadout *outRO = outCell->readouts->data[0]; // Output readout
+
+	if (smoothSuspect) {
+	    // XXX test output of suspect pixel image
+	    psImage *suspects = psMetadataLookupPtr(NULL, outRO->analysis, PM_MASK_ANALYSIS_SUSPECT); // Suspect img
+	    assert (suspects);
+	    psImageSmooth (suspects, smoothScale, 3); // extend smoothing region to 3-sigma
+	}
+
         if (!pmMaskIdentifyBadPixels(outRO, maskVal, maskBad, maskMode)) {
             psError(PS_ERR_UNKNOWN, false, "Unable to mask bad pixels");
@@ -359,4 +393,7 @@
     pmChip *outChip;                    // Chip of interest
     while ((outChip = pmFPAviewNextChip(view, outFPA, 1))) {
+
+        if (!outChip->process) continue;
+
         if (!pmFPAfileIOChecks(config, view, PM_FPA_BEFORE)) {
             goto PPMERGE_MASK_ERROR;
@@ -370,16 +407,18 @@
         }
 
-        psList *inChips = psListAlloc(NULL);
-        for (int i=0; i < numFiles; i++) {
-            pmFPAfile *file = pmFPAfileSelectSingle(config->files, "PPMERGE.INPUT", i); // Input file
-            pmChip *chip = pmFPAviewThisChip(view, file->fpa);
-            psListAdd(inChips, PS_LIST_TAIL, chip);
-        }
-        if (!pmConceptsAverageChips(outChip, inChips, true)) {
-            psError(PS_ERR_UNKNOWN, false, "Unable to average Chip concepts.");
-            psFree(inChips);
-            goto PPMERGE_MASK_ERROR;
-        }
-        psFree(inChips);
+	if (outChip->data_exists) {
+	    psList *inChips = psListAlloc(NULL);
+	    for (int i=0; i < numFiles; i++) {
+		pmFPAfile *file = pmFPAfileSelectSingle(config->files, "PPMERGE.INPUT", i); // Input file
+		pmChip *chip = pmFPAviewThisChip(view, file->fpa);
+		psListAdd(inChips, PS_LIST_TAIL, chip);
+	    }
+	    if (!pmConceptsAverageChips(outChip, inChips, true)) {
+		psError(PS_ERR_UNKNOWN, false, "Unable to average Chip concepts.");
+		psFree(inChips);
+		goto PPMERGE_MASK_ERROR;
+	    }
+	    psFree(inChips);
+	}
         if (!pmFPAfileIOChecks(config, view, PM_FPA_AFTER)) {
             goto PPMERGE_MASK_ERROR;
