Index: /trunk/ppImage/src/ppImageDetrendPattern.c
===================================================================
--- /trunk/ppImage/src/ppImageDetrendPattern.c	(revision 30834)
+++ /trunk/ppImage/src/ppImageDetrendPattern.c	(revision 30835)
@@ -25,15 +25,20 @@
     if (options->doPatternRow) {
         bool status;
-        pmFPAfile *input = psMetadataLookupPtr(&status, config->files, "PPIMAGE.INPUT");
-        pmFPAview *view = pmFPAviewAlloc(0); // View for local processing
-        *view = *inputView;
-        while ((cell = pmFPAviewNextCell(view, input->fpa, 1)) != NULL) {
+	pmHDU *hdu = pmHDUFromChip(chip);
+	if (psMetadataLookupBool(NULL,hdu->header,"PTRN_ROW")) {
+	  psLogMsg("ppImage", PS_LOG_INFO, "Not performing row pattern correction as it has already been done.");
+	}
+	else {
+	  pmFPAfile *input = psMetadataLookupPtr(&status, config->files, "PPIMAGE.INPUT");
+	  pmFPAview *view = pmFPAviewAlloc(0); // View for local processing
+	  *view = *inputView;
+	  while ((cell = pmFPAviewNextCell(view, input->fpa, 1)) != NULL) {
             if (!cell->process || !cell->file_exists) {
-                continue;
+	      continue;
             }
             if (!pmFPAfileIOChecks(config, view, PM_FPA_BEFORE)) {
               ESCAPE(false, "load failure for Cell");
             }
-
+	    
             if (!cell->data_exists) {
                 continue;
@@ -74,6 +79,9 @@
                 }
             }
-        }
-        psFree(view);
+
+	  }
+	  psMetadataAddBool(hdu->header, PS_LIST_TAIL, "PTRN_ROW",PS_META_REPLACE,"PATTERN.ROW correction applied",true);
+	  psFree(view);
+	}
     }
 
@@ -83,8 +91,15 @@
         pmFPAview *view = pmFPAviewAlloc(0); // View for local processing
         *view = *inputView;
-        for (int i = 0; i < chip->cells->n; i++) {
+
+	pmHDU *hdu = pmHDUFromCell(cell);
+	if (psMetadataLookupBool(NULL,hdu->header,"PTRN_CEL")) {
+	  psLogMsg("ppImage", PS_LOG_INFO, "Not performing cell pattern correction as it has already been done.");
+	}
+	else {
+	  for (int i = 0; i < chip->cells->n; i++) {
             view->cell = i;
 
             pmCell *cell = chip->cells->data[i]; // Cell of interest
+
             if (cell->readouts->n > 1) {
                 psLogMsg("ppImage", PS_LOG_INFO, "Not performing cell pattern correction on video cell.");
@@ -99,15 +114,19 @@
                 tweak->data.U8[i] = 0xFF;
             }
-        }
 
-        // Tweak the cells
-        if (!pmPatternCell(chip, tweak, options->patternCellBG, options->patternCellMean,
-                           options->maskValue, options->darkMask)) {
-            psFree(tweak);
-            psFree(view);
-            return false;
-        }
-        psFree(tweak);
-        psFree(view);
+	  }
+
+          // Tweak the cells
+          if (!pmPatternCell(chip, tweak, options->patternCellBG, options->patternCellMean,
+                             options->maskValue, options->darkMask)) {
+              psFree(tweak);
+              psFree(view);
+              return false;
+          }
+          psFree(tweak);
+          psFree(view);
+
+	  psMetadataAddBool(hdu->header, PS_LIST_TAIL, "PTRN_CEL",PS_META_REPLACE,"PATTERN.CELL correction applied",true);
+	}
     }
 
