Index: trunk/psModules/src/detrend/pmPattern.c
===================================================================
--- trunk/psModules/src/detrend/pmPattern.c	(revision 42379)
+++ trunk/psModules/src/detrend/pmPattern.c	(revision 42889)
@@ -239,4 +239,5 @@
 # define READNOISE 10 /* arbitrary number */
     float sigma = sqrt(stats->robustMedian + PS_SQR(READNOISE));
+    if(isnan(sigma)) sigma=20.;
     float delta = PS_MIN (thresh * sigma, 2*nominalAmplitude); 
     float lower = stats->robustMedian - delta; // Lower bound for data
@@ -246,7 +247,13 @@
     // if the noise from the background is too large 
     float significance = nominalAmplitude / sigma;
+    significance = abs(significance);
    
     // XXX EAM : arbitrary number
     if (isfinite(nominalAmplitude) && (significance < 1.0/6.0)) {
+      psLogMsg("ppImage", PS_LOG_INFO, "Skipping row pattern correction for %s, %s, stats: %f - %f - %f : %f %f %f\n", chipName, cellName, lower, background, upper, sigma, nominalAmplitude, significance);
+      return true; 
+    }
+    //Add some safeties in case of bad fits
+    if (isnan(significance) || (background < 0.0)) {
       psLogMsg("ppImage", PS_LOG_INFO, "Skipping row pattern correction for %s, %s, stats: %f - %f - %f : %f %f %f\n", chipName, cellName, lower, background, upper, sigma, nominalAmplitude, significance);
       return true; 
@@ -554,4 +561,5 @@
 # define READNOISE 10 /* arbitrary number */
     float sigma = sqrt(stats->robustMedian + PS_SQR(READNOISE));
+    if(isnan(sigma)) sigma=20.; // XXX EAM : somewhat arbitrary number
     float delta = PS_MIN (thresh * sigma, 5*nominalAmplitude); 
     float lower = stats->robustMedian - delta; // Lower bound for data
@@ -561,5 +569,6 @@
     // if the noise from the background is too large 
     float significance = nominalAmplitude / sigma;
-   
+    significance = abs(significance);
+      
     // XXX EAM : arbitrary number
     if (isfinite(nominalAmplitude) && (significance < 1.0/6.0)) {
@@ -569,4 +578,12 @@
       return true; 
     }
+    //Add some safeties in case of bad fits
+    if (isnan(significance) || (background < 0.0)) {
+      psLogMsg("ppImage", PS_LOG_INFO, "Skipping row pattern correction due to bad fit for %s, %s, stats: %f - %f - %f : %f %f %f\n", chipName, cellName, lower, background, upper, sigma, nominalAmplitude, significance);
+      psFree(stats);
+      psFree(rng);
+      return true; 
+    }
+    
     psLogMsg("ppImage", PS_LOG_INFO, "Performing row pattern correction for %s, %s, stats: %f - %f - %f : %f %f %f\n", chipName, cellName, lower, background, upper, sigma, nominalAmplitude, significance);
 # endif    
