Index: /trunk/ppstamp/src/ppstamp.h
===================================================================
--- /trunk/ppstamp/src/ppstamp.h	(revision 15387)
+++ /trunk/ppstamp/src/ppstamp.h	(revision 15388)
@@ -77,3 +77,4 @@
 psRegion *ppstampChipRegion(const pmChip *chip);
 
+extern bool ppstampMegacamWorkaround;
 #endif
Index: /trunk/ppstamp/src/ppstampMakeStamp.c
===================================================================
--- /trunk/ppstamp/src/ppstampMakeStamp.c	(revision 15387)
+++ /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
Index: /trunk/ppstamp/src/ppstampParseCamera.c
===================================================================
--- /trunk/ppstamp/src/ppstampParseCamera.c	(revision 15387)
+++ /trunk/ppstamp/src/ppstampParseCamera.c	(revision 15388)
@@ -4,4 +4,6 @@
 
 # include "ppstamp.h"
+
+bool ppstampMegacamWorkaround = false;
 
 // Set up the ppstamp output Image file
@@ -53,22 +55,10 @@
         return NULL;
     }
+
+    if (!strcmp(config->cameraName, "MEGACAM")) {
+        // workaround bug 986 and related
+        ppstampMegacamWorkaround = true;
+    }
  
-
-#ifdef notyet
-    if (psTraceGetLevel("ppstamp.config") > 0) {
-        // Get a look inside all the files.
-        psMetadataIterator *filesIter = psMetadataIteratorAlloc(config->files, PS_LIST_HEAD, NULL); // Iterator
-        psMetadataItem *item;               // Item from iteration
-        fprintf(stderr, "Files:\n");
-        while ((item = psMetadataGetAndIncrement(filesIter))) {
-            pmFPAfile *file = item->data.V; // File of interest
-            fprintf(stderr, "%s: %p %p %p (%p) %p\n", file->name,
-                    file->src, file->fpa,
-                    file->camera, file->fpa->camera, file->format);
-        }
-        psFree(filesIter);
-    }
-#endif
-
     return true;
 }
Index: /trunk/ppstamp/src/ppstampRegion.c
===================================================================
--- /trunk/ppstamp/src/ppstampRegion.c	(revision 15387)
+++ /trunk/ppstamp/src/ppstampRegion.c	(revision 15388)
@@ -3,4 +3,6 @@
 #include "pmHDU.h"
 #include "pmFPA.h"
+
+#include "ppstamp.h"
 
 // Functions to calculate the image space boundaries of a given Chip or Cell
@@ -20,17 +22,8 @@
     // This is the difference between this function and pmReadoutExtent. 
     // pmReadoutExtent ignores the col0, row0 of the readout
-
-    int col0 = 0;
-    int row0 = 0;
    
-#ifdef notdef
-    if (image->parent) {
-        // This is wrong. I'm trying to deal with the strange megacam 'spliced' images
-        // which appear to have inconsistant CELL.X0 and readout.col0
-        col0 = image->col0 - image->parent->col0;
-        row0 = image->row0 - image->parent->row0;
-    }
-#endif
-
+    int col0 = 0;   // should be  image->col0 - readout->col0
+    int row0 = 0;   //            image->row0 - readout->row0
+   
     return psRegionAlloc(col0, col0 + image->numCols,
                          row0, row0 + image->numRows);
@@ -99,4 +92,18 @@
     PS_ASSERT_PTR_NON_NULL(chip, NULL);
 
+    if (ppstampMegacamWorkaround) {
+        // There is an inconsistency in the megacam parameters. 
+        // The offset to the cells is effectively contained in two
+        // places.
+        // The CELL.X0 for the 2 cells are 0 and 1024 while
+        // readout->image.col0 = 32 and 1056
+        // This fact makes it impossible to calculate the Chip bounds 
+        // in a way consistent with say gpc1
+        // we're deferring that problem for now.
+        // Since all chips on megacam have the same bounds, just hard code
+        // the answer. See bug 986
+        return psRegionAlloc(32, 2080, 0, 4612);
+    }
+
     psArray *cells = chip->cells;       // Array of component cells
     psRegion *chipExtent = psRegionAlloc(INFINITY, 0, INFINITY, 0); // Extent of chip
