Index: trunk/ppImage/src/ppImageRebinReadout.c
===================================================================
--- trunk/ppImage/src/ppImageRebinReadout.c	(revision 7621)
+++ trunk/ppImage/src/ppImageRebinReadout.c	(revision 7629)
@@ -19,16 +19,36 @@
 
     while ((cell = pmFPAviewNextCell (view, inFile->fpa, 1)) != NULL) {
-	psLogMsg ("ppImageRebinChip", 4, "Cell %d: %x %x\n", view->cell, cell->file_exists, cell->process);
-	if (! cell->process || ! cell->file_exists) { continue; }
+        psLogMsg ("ppImageRebinChip", 4, "Cell %d: %x %x\n", view->cell, cell->file_exists, cell->process);
+        if (! cell->process || ! cell->file_exists) { continue; }
 
-	// process each of the readouts
-	while ((inReadout = pmFPAviewNextReadout (view, inFile->fpa, 1)) != NULL) {
-	    if (! inReadout->data_exists) { continue; }
+        // process each of the readouts
+        while ((inReadout = pmFPAviewNextReadout (view, inFile->fpa, 1)) != NULL) {
+            if (! inReadout->data_exists) { continue; }
 
-	    outReadout = pmFPAviewThisReadout (view, outFile->fpa);
+            outReadout = pmFPAviewThisReadout (view, outFile->fpa);
 
-	    // run the rebin code
-	    ppImageRebinReadout (outReadout, inReadout, outFile);
-	}
+            // run the rebin code
+            ppImageRebinReadout (outReadout, inReadout, outFile);
+        }
+
+        // Get the original values for the binning
+        bool mdok = true;               // Status of MD lookup
+        int xBin = psMetadataLookupS32(&mdok, cell->concepts, "CELL.XBIN"); // The binning in x
+        if (!mdok || xBin <= 0) {
+            psLogMsg(__func__, PS_LOG_WARN, "CELL.XBIN is not set --- assuming 1.\n");
+            xBin = 1;
+        }
+        int yBin = psMetadataLookupS32(&mdok, cell->concepts, "CELL.YBIN"); // The binning in y
+        if (!mdok || yBin <= 0) {
+            psLogMsg(__func__, PS_LOG_WARN, "CELL.YBIN is not set --- assuming 1.\n");
+            yBin = 1;
+        }
+
+        // Update the concepts with the new values for the binning
+        pmCell *outCell = pmFPAviewThisCell(view, outFile->fpa); // The output cell
+        psMetadataItem *binItem = psMetadataLookup(outCell->concepts, "CELL.XBIN");
+        binItem->data.S32 = xBin * outFile->xBin;
+        binItem = psMetadataLookup(outCell->concepts, "CELL.YBIN");
+        binItem->data.S32 = yBin * outFile->yBin;
     }
     return true;
@@ -53,15 +73,15 @@
     // do the rebinning by hand, mean only for test
     for (int yOut = 0; yOut < output->image->numRows; yOut++) {
-	for (int xOut = 0; xOut < output->image->numCols; xOut++) {
-	    value = 0;
-	    nPix = 0;
-	    for (int yIn = yOut * dY; (yIn < yOut * dY + dY) && (yIn < nY); yIn ++) {
-		for (int xIn = xOut * dX; (xIn < xOut * dX + dX) && (xIn < nX); xIn ++) {
-		    value += input->image->data.F32[yIn][xIn];
-		    nPix ++;
-		}
-	    }
-	    output->image->data.F32[yOut][xOut] = value / nPix;
-	}
+        for (int xOut = 0; xOut < output->image->numCols; xOut++) {
+            value = 0;
+            nPix = 0;
+            for (int yIn = yOut * dY; (yIn < yOut * dY + dY) && (yIn < nY); yIn ++) {
+                for (int xIn = xOut * dX; (xIn < xOut * dX + dX) && (xIn < nX); xIn ++) {
+                    value += input->image->data.F32[yIn][xIn];
+                    nPix ++;
+                }
+            }
+            output->image->data.F32[yOut][xOut] = value / nPix;
+        }
     }
 
