Index: branches/eam_branches/ipp-dev-20210817/psModules/src/detrend/pmPattern.c
===================================================================
--- branches/eam_branches/ipp-dev-20210817/psModules/src/detrend/pmPattern.c	(revision 41836)
+++ branches/eam_branches/ipp-dev-20210817/psModules/src/detrend/pmPattern.c	(revision 41838)
@@ -164,6 +164,6 @@
                   psImageMaskType maskVal, psImageMaskType maskBad) {
 
-//  bool status = pmPatternRowBinned(ro, order, iter, rej, thresh, clipMean, clipStdev, maskVal, maskBad);
-    bool status = pmPatternRowUnbinned(ro, order, iter, rej, thresh, clipMean, clipStdev, maskVal, maskBad);
+  bool status = pmPatternRowBinned(ro, order, iter, rej, thresh, clipMean, clipStdev, maskVal, maskBad);
+  // bool status = pmPatternRowUnbinned(ro, order, iter, rej, thresh, clipMean, clipStdev, maskVal, maskBad);
     return status;
 }
@@ -480,15 +480,17 @@
     psRandom *rng = psRandomAlloc(PS_RANDOM_TAUS); // Random number generator
     if (!psImageBackground(stats, NULL, ro->image, ro->mask, maskVal, rng)) {
-        psWarning("Unable to calculate statistics on readout; masking entire readout.");
+        psWarning("Unable to calculate statistics on readout; skipping pattern correction.");
         psErrorClear();
         psFree(stats);
         psFree(rng);
-        psImageInit(image, NAN);
-        if (mask) {
-            psBinaryOp(mask, mask, "|", psScalarAlloc(maskBad, PS_TYPE_IMAGE_MASK));
-        }
-        if (ro->variance) {
-            psImageInit(image, NAN);
-        }
+	
+	// EAM 20211011 : we used to mask cells which fail the above, but this seems excessive
+        // psImageInit(image, NAN);
+        // if (mask) {
+        //     psBinaryOp(mask, mask, "|", psScalarAlloc(maskBad, PS_TYPE_IMAGE_MASK));
+        // }
+        // if (ro->variance) {
+        //     psImageInit(image, NAN);
+        // }
         return true;
     }
@@ -520,19 +522,23 @@
     // so smaller by a factor of 1.4 than what we predict, which is not very large
 
-    # define READNOISE 10
+    // retrieve noise and gain from 'concepts' if possible
+# define READNOISE 10 /* arbitrary number */
     float sigma = sqrt(stats->robustMedian + PS_SQR(READNOISE));
-    float delta = PS_MIN (thresh * sigma, 40);
+    float delta = PS_MIN (thresh * sigma, 40); // XXX EAM : arbitrary number
     float lower = stats->robustMedian - delta; // Lower bound for data
     float upper = stats->robustMedian + delta; // Upper bound for data
     float background = stats->robustMedian;
-# endif    
+
+    // find the nominal signal amplitude (check the ghost and/or crosstalk recipe file)
+    float nominalAmplitude = 20; // XXX EAM : arbitrary number
+
+    // if the noise from the background is too large 
+    float significance = nominalAmplitude / sigma;
+    if (significance < 0.5) return true; // XXX EAM : arbitrary number
     
     pmCell *cell = ro->parent;
     const char *cellName = psMetadataLookupStr(NULL, cell->concepts, "CELL.NAME"); // Name of cell
-    fprintf (stderr, "pattern row background %s: %f - %f - %f\n", cellName,lower, background, upper);
-
-
-    // XXX add code to skip fit if background stdev is large compared to expected signal
-    // XXX add a constraint to the fit so the |amplitude| < x (x ~ 20 - 25)
+    fprintf (stderr, "pattern row background %s: %f - %f - %f\n", cellName, lower, background, upper);
+# endif    
 
     psFree(stats);
@@ -637,5 +643,6 @@
         if (num < validNmin) {
             // Not enough points to fit
-            patternMaskRow(ro, y, maskBad);
+	    // EAM 20211011 : we used to mask rows which could not be fit, but this is excessive.  just skip.
+	    // patternMaskRow(ro, y, maskBad);
 	    // Ignore this row in our subsequent fits, because the fit failed.
 	    yaxisMask->data.PS_TYPE_VECTOR_MASK_DATA[y] = 0xFF;
@@ -646,5 +653,6 @@
             psWarning("Unable to fit polynomial to row %d", y);
             psErrorClear();
-            patternMaskRow(ro, y, maskBad);
+	    // EAM 20211011 : we used to mask rows which could not be fit, but this is excessive.  just skip.
+            // patternMaskRow(ro, y, maskBad);
 	    // Ignore this row in our subsequent fits, because the fit failed.
 	    yaxisMask->data.PS_TYPE_VECTOR_MASK_DATA[y] = 0xFF;
@@ -661,5 +669,6 @@
             psWarning("Unable to evaluate polynomial for row %d", y);
             psErrorClear();
-            patternMaskRow(ro, y, maskBad);
+	    // EAM 20211011 : we used to mask rows which could not be fit, but this is excessive.  just skip.
+            // patternMaskRow(ro, y, maskBad);
 	    yaxisMask->data.PS_TYPE_VECTOR_MASK_DATA[y] = 0xFF;
             continue;
