Index: trunk/ppImage/src/ppImageDetrendPattern.c
===================================================================
--- trunk/ppImage/src/ppImageDetrendPattern.c	(revision 30860)
+++ trunk/ppImage/src/ppImageDetrendPattern.c	(revision 31066)
@@ -23,4 +23,16 @@
     assert(inputView->readout == -1);
 
+    // PATTERN.ROW is selected by the recipe.  If it is selected, we search for the table
+    // PATTERN.ROW.SUBSET.  If this is found in our format file, we use that version;
+    // otherwise, we use the table provided in the recipe file "ppImage.config".  Within that
+    // table, we select the entry that matches our CHIP.NAME.  This will be either a boolean or
+    // a string of bits.  If it is a boolean, it specified whether or not to correct the entire
+    // chip; if it is a string, the bits specify which cells to correct (sequence is order of
+    // CELLS in the format:CHIPS metadata table)
+
+    // We also check the chip header for the boolean 'PTRN_ROW' : if this is true, we have
+    // already applied this correct to this data, so we simply skip the correction for this
+    // chip.
+
     if (options->doPatternRow) {
         bool status;
@@ -28,15 +40,16 @@
 	if (psMetadataLookupBool(NULL,hdu->header,"PTRN_ROW")) {
 	  psLogMsg("ppImage", PS_LOG_INFO, "Not performing row pattern correction as it has already been done.");
+	  goto pattern_cell;
 	}
-	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) {
+
+	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");
+		ESCAPE(false, "load failure for Cell");
             }
 	    
@@ -56,8 +69,8 @@
             if (!doPattern) continue;
 
+	    // A very detailed log message. 
             const char *chipName = psMetadataLookupStr(NULL, chip->concepts, "CHIP.NAME");
             const char *cellName = psMetadataLookupStr(NULL, cell->concepts, "CELL.NAME");
-            psLogMsg("ppImage", PS_LOG_INFO, "Performing row pattern correction for %s, %s\n",
-                     chipName, cellName);
+            psLogMsg("ppImage", PS_LOG_INFO, "Performing row pattern correction for %s, %s\n", chipName, cellName);
 
             // process each of the readouts
@@ -80,9 +93,12 @@
             }
 
-	  }
-	  psMetadataAddBool(hdu->header, PS_LIST_TAIL, "PTRN_ROW",PS_META_REPLACE,"PATTERN.ROW correction applied",true);
-	  psFree(view);
 	}
+	psMetadataAddBool(hdu->header, PS_LIST_TAIL, "PTRN_ROW",PS_META_REPLACE,"PATTERN.ROW correction applied",true);
+	psFree(view);
     }
+
+ pattern_cell:
+
+    // see the comment for PATTERN.ROW; the same rules apply for PATTERN.CELL
 
     if (options->doPatternCell) {
@@ -95,7 +111,8 @@
 	if (psMetadataLookupBool(NULL,hdu->header,"PTRN_CEL")) {
 	  psLogMsg("ppImage", PS_LOG_INFO, "Not performing cell pattern correction as it has already been done.");
+	  goto pattern_done;
 	}
-	else {
-	  for (int i = 0; i < chip->cells->n; i++) {
+
+	for (int i = 0; i < chip->cells->n; i++) {
             view->cell = i;
 
@@ -115,20 +132,20 @@
             }
 
-	  }
+	}
 
-          // 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);
-	}
+	psMetadataAddBool(hdu->header, PS_LIST_TAIL, "PTRN_CEL",PS_META_REPLACE,"PATTERN.CELL correction applied",true);
     }
 
+ pattern_done:
     return(true);
 }
