Index: trunk/ppMerge/src/ppMergeLoop_Threaded.c
===================================================================
--- trunk/ppMerge/src/ppMergeLoop_Threaded.c	(revision 18839)
+++ trunk/ppMerge/src/ppMergeLoop_Threaded.c	(revision 18862)
@@ -11,4 +11,7 @@
 
 #include "ppMerge.h"
+
+// XXX this function is now sufficiently different for the major types, it would make sense to just 
+// split it into three: BASIC, SHUTTER, DARK
 
 bool ppMergeLoop(pmConfig *config)
@@ -145,6 +148,8 @@
 
             float shutterRef = NAN;     // Reference shutter correction
+            pmReadout *pattern = NULL;
             if (type == PPMERGE_TYPE_SHUTTER) {
                 shutterRef = pmShutterCorrectionReference(shutters->data[cellNum]);
+		pattern = pmReadoutAlloc(NULL);
             }
 
@@ -159,4 +164,5 @@
 	    }
 
+	    ppMergeFileGroup *fileGroup = NULL;
 	    psArray *fileGroups = psArrayAlloc (nThreads + 1);
 
@@ -170,5 +176,5 @@
 		}
 
-		ppMergeFileGroup *fileGroup = ppMergeFileGroupAlloc();
+		fileGroup = ppMergeFileGroupAlloc();
 		fileGroup->readouts = readouts;
 		fileGroup->read = false;
@@ -185,10 +191,23 @@
 	      case PPMERGE_TYPE_FRINGE: 
 		psAssert (fileGroups->n > 0, "no valid file groups defined");
-		ppMergeFileGroup *fileGroup = fileGroups->data[0];
+		fileGroup = fileGroups->data[0];
 		if (!pmReadoutCombinePrepare(outRO, fileGroup->readouts, combination)) {
 		    goto ERROR;
 		}
 		break;
-
+	      case PPMERGE_TYPE_DARK:
+		psAssert (fileGroups->n > 0, "no valid file groups defined");
+		fileGroup = fileGroups->data[0];
+		if (!pmDarkCombinePrepare(outCell, fileGroup->readouts, darkOrdinates, darkNorm)) {
+		    goto ERROR;
+		}
+		break;
+	      case PPMERGE_TYPE_SHUTTER:
+		psAssert (fileGroups->n > 0, "no valid file groups defined");
+		fileGroup = fileGroups->data[0];
+		if (!pmShutterCorrectionGeneratePrepare(outRO, pattern, fileGroup->readouts, maskVal)) {
+		    goto ERROR;
+		}
+		break;
 	      default:
 		fprintf (stderr, "not yet ready");
@@ -197,9 +216,9 @@
 
             // Read input data by chunks
-	    // psTimerStart ("ppMergeLoop");
+	    psTimerStart ("ppMergeLoop");
             for (int numChunk = 0; true; numChunk++) {
 
 		bool status = false;
-		ppMergeFileGroup *fileGroup = ppMergeReadChunk (&status, fileGroups, config, numChunk);
+		fileGroup = ppMergeReadChunk (&status, fileGroups, config, numChunk);
 		if (!status) goto ERROR;
 		if (!fileGroup) break;
@@ -208,18 +227,18 @@
 
                 switch (type) {
-                  case PPMERGE_TYPE_SHUTTER:
+                  case PPMERGE_TYPE_BIAS:
+                  case PPMERGE_TYPE_FLAT:
+                  case PPMERGE_TYPE_FRINGE:
 		    // allocate a job
-		    job = psThreadJobAlloc ("PPMERGE_SHUTTER_CORRECTION");
+		    job = psThreadJobAlloc ("PPMERGE_READOUT_COMBINE");
 
 		    // construct the arguments for this job
 		    psArrayAdd (job->args, 1, outRO);
 		    psArrayAdd (job->args, 1, fileGroup);
-		    psArrayAdd (job->args, 1, psScalarAlloc(shutterRef, PS_TYPE_F32));
-		    psArrayAdd (job->args, 1, shutters->data[cellNum]);
-		    psArrayAdd (job->args, 1, psScalarAlloc(iter, PS_TYPE_S32));
-		    psArrayAdd (job->args, 1, psScalarAlloc(rej, PS_TYPE_F32));
-		    psArrayAdd (job->args, 1, psScalarAlloc(maskVal, PS_TYPE_U8));
-
-		    // call: pmShutterCorrectionGenerate(outRO, NULL, fileGroup->readouts, shutterRef, shutters->data[cellNum], iter, rej, maskVal)
+		    psArrayAdd (job->args, 1, zeros);
+		    psArrayAdd (job->args, 1, scales);
+		    psArrayAdd (job->args, 1, combination);
+
+		    // call: pmReadoutCombine(outRO, fileGroup->readouts, zeros, scales, combination);
 		    if (!psThreadJobAddPending (job)) {
 			goto ERROR;
@@ -233,29 +252,28 @@
 		    psArrayAdd (job->args, 1, outCell);
 		    psArrayAdd (job->args, 1, fileGroup);
-		    psArrayAdd (job->args, 1, darkOrdinates);
-		    psArrayAdd (job->args, 1, darkNorm);
 		    psArrayAdd (job->args, 1, psScalarAlloc(iter, PS_TYPE_S32));
 		    psArrayAdd (job->args, 1, psScalarAlloc(rej, PS_TYPE_F32));
 		    psArrayAdd (job->args, 1, psScalarAlloc(maskVal, PS_TYPE_U8));
 
-		    // call: pmDarkCombine(outCell, fileGroup->readouts, darkOrdinates, darkNorm, iter, rej, maskVal);
+		    // call: pmDarkCombine(outCell, fileGroup->readouts, iter, rej, maskVal);
 		    if (!psThreadJobAddPending (job)) {			
 			goto ERROR;
 		    }
                     break;
-                  case PPMERGE_TYPE_BIAS:
-                  case PPMERGE_TYPE_FLAT:
-                  case PPMERGE_TYPE_FRINGE:
+                  case PPMERGE_TYPE_SHUTTER:
 		    // allocate a job
-		    job = psThreadJobAlloc ("PPMERGE_READOUT_COMBINE");
+		    job = psThreadJobAlloc ("PPMERGE_SHUTTER_CORRECTION");
 
 		    // construct the arguments for this job
 		    psArrayAdd (job->args, 1, outRO);
+		    psArrayAdd (job->args, 1, pattern);
 		    psArrayAdd (job->args, 1, fileGroup);
-		    psArrayAdd (job->args, 1, zeros);
-		    psArrayAdd (job->args, 1, scales);
-		    psArrayAdd (job->args, 1, combination);
-
-		    // call: pmReadoutCombine(outRO, fileGroup->readouts, zeros, scales, combination);
+		    psArrayAdd (job->args, 1, psScalarAlloc(shutterRef, PS_TYPE_F32));
+		    psArrayAdd (job->args, 1, shutters->data[cellNum]);
+		    psArrayAdd (job->args, 1, psScalarAlloc(iter, PS_TYPE_S32));
+		    psArrayAdd (job->args, 1, psScalarAlloc(rej, PS_TYPE_F32));
+		    psArrayAdd (job->args, 1, psScalarAlloc(maskVal, PS_TYPE_U8));
+
+		    // call: pmShutterCorrectionGenerate(outRO, pattern, fileGroup->readouts, shutterRef, shutters->data[cellNum], iter, rej, maskVal)
 		    if (!psThreadJobAddPending (job)) {
 			goto ERROR;
@@ -281,4 +299,7 @@
             psFree(fileGroups);
 
+	    // XXX eventually need to keep both the shutter and the pattern, as we do with dark 
+            psFree(pattern);
+
             // Get list of cells for concepts averaging
             psList *inCells = psListAlloc(NULL); // List of cells
@@ -295,5 +316,5 @@
             }
             psFree(inCells);
-	    // fprintf (stdout, "done ppMergeLoop for cell : %f\n", psTimerMark ("ppMergeLoop"));
+	    fprintf (stdout, "done ppMergeLoop for cell : %f\n", psTimerMark ("ppMergeLoop"));
 
             // Plug supplementary images into their own FPAs
