Index: branches/pap/ppImage/src/ppImageDetrendFringe.c
===================================================================
--- branches/pap/ppImage/src/ppImageDetrendFringe.c	(revision 23948)
+++ branches/pap/ppImage/src/ppImageDetrendFringe.c	(revision 25027)
@@ -11,15 +11,9 @@
     PS_ASSERT_PTR_NON_NULL(options, false);
 
-    // Reference fringe measurements
-    psArray *references = psMemIncrRefCounter(psMetadataLookupPtr(NULL, fringe->analysis,
-                                                                  "FRINGE.MEASUREMENTS"));
+    // Reference fringe measurements (stored on the reference cell->analysis)
+    psArray *references = psMemIncrRefCounter(psMetadataLookupPtr(NULL, fringe->analysis, "FRINGE.MEASUREMENTS"));
     if (!references) {
-        references = pmFringesParse(fringe); // Reference fringes
-        if (!references) {
-            psError(PS_ERR_IO, false, "Unable to find fringe references.\n");
-            return false;
-        }
-        psMetadataAdd(fringe->analysis, PS_LIST_TAIL, "FRINGE.MEASUREMENTS", PS_DATA_UNKNOWN,
-                      "Fringe measurements", references);
+	psError(PS_ERR_IO, false, "Unable to find fringe references.\n");
+	return false;
     }
 
@@ -75,6 +69,5 @@
     }
 
-    psMetadataAdd(readout->parent->analysis, PS_LIST_TAIL, scienceFringes,
-                  PS_DATA_UNKNOWN | PS_META_REPLACE, "Fringe measurements", measurements);
+    psMetadataAdd(readout->parent->analysis, PS_LIST_TAIL, scienceFringes, PS_DATA_UNKNOWN | PS_META_REPLACE, "Fringe measurements", measurements);
     psFree(measurements);
     psFree(scienceFringes);
@@ -92,10 +85,12 @@
     psArray *fringes = psArrayAlloc(cells->n); // Fringes, to return
     for (int i = 0; i < cells->n; i++) {
+        fringes->data[i] = NULL;
+
         pmCell *cell = cells->data[i];  // Cell of interest
 
 	// XXX for now, skip the video cells (cell->readouts->n > 1)
 	if (cell->readouts->n > 1) {
-	  psWarning ("Skipping Video Cell for ppImageDetrendFringe.c:getFringes");
-	  continue;
+	    psWarning ("Skipping Video Cell (%d) for ppImageDetrendFringe.c:getFringes", i);
+	    continue;
 	}
 
@@ -197,4 +192,6 @@
     }
     assert(fringes->readouts->n == solution->nFringeFrames);
+
+    psImageMaskType maskVal = options->flatMask;
 
     bool mdok;                          // Status of MD lookup
@@ -262,9 +259,19 @@
         }
 
+	// subtract fringe and update mask if fringe value is NAN
+	for (int iy = 0; iy < readout->image->numRows; iy++) {
+	    for (int ix = 0; ix < readout->image->numCols; ix++) {
+  	        readout->image->data.F32[iy][ix] -= sumFringe->data.F32[iy][ix];
+		if (!isfinite(sumFringe->data.F32[iy][ix]) && readout->mask) {
+		    readout->mask->data.PS_TYPE_IMAGE_MASK_DATA[iy][ix] |= maskVal;
+		} 
+	    }
+	}
+
         // XXX: Make generic, so subregions may be subtracted as well
-        if (!psBinaryOp(readout->image, readout->image, "-", sumFringe)) {
-            psError(PS_ERR_UNEXPECTED_NULL, false, "Unable to subtract fringe.\n");
-            return false;
-        }
+        // if (!psBinaryOp(readout->image, readout->image, "-", sumFringe)) {
+        //     psError(PS_ERR_UNEXPECTED_NULL, false, "Unable to subtract fringe.\n");
+        //     return false;
+        // }
 
 	// measure residual fringe amplitude. results go to FRINGE.RESIDUALS
