Index: trunk/ppstamp/src/ppstampMakeStamp.c
===================================================================
--- trunk/ppstamp/src/ppstampMakeStamp.c	(revision 15363)
+++ trunk/ppstamp/src/ppstampMakeStamp.c	(revision 15388)
@@ -4,5 +4,4 @@
 
 #include "ppstamp.h"
-#include "pmFPAAstrometry.h"
 #include "pmAstrometryUtils.h"
 
@@ -25,7 +24,19 @@
     PS_ASSERT_PTR_NON_NULL(hdu->header, 1)
 
-    outChip->toFPA   = psPlaneTransformSetCenter(NULL, inChip->toFPA, roi->x0, roi->y0);
+    // Change the reference pixel to account for the change in origin between the stamp and
+    // the original image
+    outChip->toFPA   = psPlaneTransformSetCenter(NULL, inChip->toFPA, (int) roi->x0, (int) roi->y0);
 
     outChip->fromFPA = psPlaneTransformInvert(NULL, outChip->toFPA, *roi, 50);
+
+    // remove these keys which may have been copied from the input header
+    // XXX pmAstromWriteWCS should do this since it's the one that's inserting
+    // the PC00* style keywords
+    if (psMetadataLookup(outHDU->header, "CD1_1")) {
+        psMetadataRemoveKey(outHDU->header, "CD1_1");
+        psMetadataRemoveKey(outHDU->header, "CD1_2");
+        psMetadataRemoveKey(outHDU->header, "CD2_1");
+        psMetadataRemoveKey(outHDU->header, "CD2_2");
+    }
 
     if (!pmAstromWriteWCS(outHDU->header, outFPA, outChip, WCS_NONLIN_TOL)) {
@@ -61,4 +72,5 @@
         outHDU->header = psMetadataAlloc();
     }
+
 
     // If input had WCS convert it for stamp
@@ -182,6 +194,16 @@
             break;
         }
+
+        psRegion extractRegion = options->roi;
+
+        // Close your eyes while I hack around bug 986
+        if (ppstampMegacamWorkaround) {
+            // the coordinates of the mosaic are shifted 32 pixels from the chip
+            extractRegion.x0 -= 32;
+            extractRegion.x1 -= 32;
+        }
     
-        psImage *subsetImage = psImageSubset(readout->image, options->roi);
+        psImage *subsetImage = psImageSubset(readout->image, extractRegion);
+
         if (subsetImage) {
             // make a copy since we're going to change the image's internals
