Index: trunk/ppStack/src/ppStackThread.c
===================================================================
--- trunk/ppStack/src/ppStackThread.c	(revision 36855)
+++ trunk/ppStack/src/ppStackThread.c	(revision 42094)
@@ -19,4 +19,6 @@
     thread->firstScan = 0;
     thread->lastScan = 0;
+    thread->entry = -1; // this gets assigned after allocation
+    thread->status = PPSTACK_THREAD_NEW;
     return thread;
 }
@@ -124,5 +126,7 @@
             readouts->data[j] = ro;
         }
-        threads->data[i] = ppStackThreadAlloc(readouts);
+        ppStackThread *thread = ppStackThreadAlloc(readouts);
+	thread->entry = i;
+        threads->data[i] = thread;
         psFree(readouts);               // Drop reference
     }
@@ -157,6 +161,8 @@
     while (true) {
         // check for any groups which can read data
+	// NOTE: threads->n is set (in ppStackThreadDataSetup) to be 1 more than the argument to -threads
         for (int j = 0; j < threads->n; j++) {
             ppStackThread *thread = threads->data[j];
+	    // fprintf (stderr, "grab thread %d, scan: %d - %d : busy: %d read: %d status: %d\n", thread->entry, thread->firstScan, thread->lastScan, thread->busy, thread->read, thread->status);
             if (thread->read) {
                 continue;
@@ -166,4 +172,5 @@
             thread->lastScan = stack->lastScan + rows;
             stack->lastScan = thread->lastScan;
+	    thread->status = PPSTACK_THREAD_READ;
 
             psArray *readouts = thread->readouts;
@@ -172,4 +179,5 @@
 
             psTrace("ppStack", 2, "Reading data for chunk %d into group %d....\n", numChunk, j);
+            // fprintf (stderr, "Reading data for chunk %d into group %d (thread %d, scan: %d - %d)....\n", numChunk, j, thread->entry, thread->firstScan, thread->lastScan);
             for (int i = 0; i < readouts->n; i++) {
                 pmReadout *ro = readouts->data[i]; // Input readout
@@ -178,15 +186,17 @@
                 }
 
+		// fprintf (stderr, "read data for readout %d\n", i);
+
                 // override the recorded last scan
-                ro->thisImageScan  = thread->firstScan;
+                ro->thisImageScan    = thread->firstScan;
                 ro->thisVarianceScan = thread->firstScan;
-                ro->thisMaskScan   = thread->firstScan;
-                ro->lastImageScan  = thread->lastScan;
-                ro->lastMaskScan   = thread->lastScan;
+                ro->thisMaskScan     = thread->firstScan;
+                ro->lastImageScan    = thread->lastScan;
+                ro->lastMaskScan     = thread->lastScan;
                 ro->lastVarianceScan = thread->lastScan;
-                ro->forceScan      = true;
-
-                psFits *imageFits  = stack->imageFits->data[i]; // FITS file for image
-                psFits *maskFits   = stack->maskFits->data[i]; // FITS file for mask
+                ro->forceScan        = true;
+
+                psFits *imageFits    = stack->imageFits->data[i]; // FITS file for image
+                psFits *maskFits     = stack->maskFits->data[i]; // FITS file for mask
                 psFits *varianceFits = stack->varianceFits->data[i]; // FITS file for variance
 
@@ -232,4 +242,5 @@
 
             thread->read = thread->busy = true;
+	    thread->status = PPSTACK_THREAD_RUN;
             return thread;
         }
@@ -239,7 +250,10 @@
         for (int j = 0; j < threads->n; j++) {
             ppStackThread *thread = threads->data[j];
+	    // fprintf (stderr, "test thread %d, scan: %d - %d : busy: %d, read: %d, status: %d\n", thread->entry, thread->firstScan, thread->lastScan, thread->busy, thread->read, thread->status);
+
             if (thread->busy) {
                 continue;
             }
+	    thread->status = PPSTACK_THREAD_NEW;
             thread->read = false;
             wait = false;
@@ -283,4 +297,10 @@
     }
 
+    {
+        psThreadTask *task = psThreadTaskAlloc("PPSTACK_PERCENT_COMBINE", 3);
+        task->function = &ppStackReadoutPercentThread;
+        psThreadTaskAdd(task);
+        psFree(task);
+    }
     return;
 }
