Index: trunk/ppMerge/src/ppMergeLoop_Threaded.c
===================================================================
--- trunk/ppMerge/src/ppMergeLoop_Threaded.c	(revision 18757)
+++ trunk/ppMerge/src/ppMergeLoop_Threaded.c	(revision 18839)
@@ -36,5 +36,4 @@
 
     // General combination parameters
-    int rows = psMetadataLookupS32(NULL, arguments, "ROWS"); // Number of rows to read per chunk
     int iter = psMetadataLookupS32(NULL, arguments, "ITER"); // Number of rejection iterations
     float rej = psMetadataLookupF32(NULL, arguments, "REJ"); // Rejection level
@@ -175,57 +174,72 @@
 		fileGroup->read = false;
 		fileGroup->busy = false;
+		fileGroup->lastScan = 0;
+		fileGroup->firstScan = 0;
 		fileGroups->data[i] = fileGroup;
             }
 
+	    // call the init functions
+	    switch (type) {
+	      case PPMERGE_TYPE_BIAS:
+	      case PPMERGE_TYPE_FLAT:
+	      case PPMERGE_TYPE_FRINGE: 
+		psAssert (fileGroups->n > 0, "no valid file groups defined");
+		ppMergeFileGroup *fileGroup = fileGroups->data[0];
+		if (!pmReadoutCombinePrepare(outRO, fileGroup->readouts, combination)) {
+		    goto ERROR;
+		}
+		break;
+
+	      default:
+		fprintf (stderr, "not yet ready");
+		goto ERROR;
+	    }
+
             // Read input data by chunks
+	    // psTimerStart ("ppMergeLoop");
             for (int numChunk = 0; true; numChunk++) {
-		
+
+		bool status = false;
 		ppMergeFileGroup *fileGroup = ppMergeReadChunk (&status, fileGroups, config, numChunk);
 		if (!status) goto ERROR;
 		if (!fileGroup) break;
 
+		psThreadJob *job = NULL;
+
                 switch (type) {
                   case PPMERGE_TYPE_SHUTTER:
-		    if (nThreads) {
-			// allocate a job
-			psThreadJob *job = psThreadJobAlloc ("PPMERGE_SHUTTER_CORRECTION", 0);
-
-			// 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));
-
-			psThreadJobAddPending (job);
-		    } else {
-			if (!pmShutterCorrectionGenerate(outRO, NULL, fileGroup->readouts, shutterRef, shutters->data[cellNum], iter, rej, maskVal)) {
-			    goto ERROR;
-			}
-			fileGroup->busy = false;
+		    // allocate a job
+		    job = psThreadJobAlloc ("PPMERGE_SHUTTER_CORRECTION");
+
+		    // 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)
+		    if (!psThreadJobAddPending (job)) {
+			goto ERROR;
 		    }
                     break;
                   case PPMERGE_TYPE_DARK:
-		    if (nThreads) {
-			// allocate a job
-			psThreadJob *job = psThreadJobAlloc ("PPMERGE_DARK_COMBINE", 0);
-
-			// construct the arguments for this job
-			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));
-
-			psThreadJobAddPending (job);
-		    } else {
-			if (!pmDarkCombine(outCell, fileGroup->readouts, darkOrdinates, darkNorm, iter, rej, maskVal)) {
-			    goto ERROR;
-			}
-			fileGroup->busy = false;
+		    // allocate a job
+		    job = psThreadJobAlloc ("PPMERGE_DARK_COMBINE");
+
+		    // construct the arguments for this job
+		    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);
+		    if (!psThreadJobAddPending (job)) {			
+			goto ERROR;
 		    }
                     break;
@@ -233,21 +247,17 @@
                   case PPMERGE_TYPE_FLAT:
                   case PPMERGE_TYPE_FRINGE:
-		    if (nThreads) {
-			// allocate a job
-			psThreadJob *job = psThreadJobAlloc ("PPMERGE_READOUT_COMBINE", 0);
-
-			// construct the arguments for this job
-			psArrayAdd (job->args, 1, outRO);
-			psArrayAdd (job->args, 1, fileGroup);
-			psArrayAdd (job->args, 1, zeros);
-			psArrayAdd (job->args, 1, scales);
-			psArrayAdd (job->args, 1, combination);
-
-			psThreadJobAddPending (job);
-		    } else {
-			if (!pmReadoutCombine(outRO, fileGroup->readouts, zeros, scales, combination)) {
-			    goto ERROR;
-			}
-			fileGroup->busy = false;
+		    // allocate a job
+		    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, zeros);
+		    psArrayAdd (job->args, 1, scales);
+		    psArrayAdd (job->args, 1, combination);
+
+		    // call: pmReadoutCombine(outRO, fileGroup->readouts, zeros, scales, combination);
+		    if (!psThreadJobAddPending (job)) {
+			goto ERROR;
 		    }
                     break;
@@ -258,24 +268,23 @@
 
 	    // wait for the threads to finish and manage results
-	    if (nThreads) {
-		// wait here for the threaded jobs to finish
-		if (!psThreadPoolWait ()) {
-		    psError(PS_ERR_UNKNOWN, false, "Unable to interpolate image.");
-		    return false;
-		}
-		fprintf (stderr, "success for threaded jobs\n");
-
-		// we don't care about the results, just dump the done queue jobs
-		psThreadJob *job = NULL;
-		while ((job = psThreadJobGetDone()) != NULL) {
-		    psFree (job);
-		}
+	    if (!psThreadPoolWait ()) {
+		psError(PS_ERR_UNKNOWN, false, "Unable to combine images.");
+		return false;
 	    }
+
+	    // we don't care about the results, just dump the done queue jobs
+	    psThreadJob *job = NULL;
+	    while ((job = psThreadJobGetDone()) != NULL) {
+		psFree (job);
+	    }
+
+            psFree(fileGroups);
 
             // Get list of cells for concepts averaging
             psList *inCells = psListAlloc(NULL); // List of cells
             for (int i = 0; i < numFiles; i++) {
-                pmReadout *readout = readouts->data[i]; // Readout of interest
-                psListAdd(inCells, PS_LIST_TAIL, readout->parent);
+		pmFPAfile *input = pmFPAfileSelectSingle(config->files, "PPMERGE.INPUT", i);
+		pmCell *inCell = pmFPAviewThisCell(view, input->fpa); // Input cell
+                psListAdd(inCells, PS_LIST_TAIL, inCell);
             }
             if (!pmConceptsAverageCells(outCell, inCells, NULL, NULL, true)) {
@@ -286,6 +295,5 @@
             }
             psFree(inCells);
-
-            psFree(fileGroups);
+	    // fprintf (stdout, "done ppMergeLoop for cell : %f\n", psTimerMark ("ppMergeLoop"));
 
             // Plug supplementary images into their own FPAs
