Index: trunk/ppSim/src/ppSimLoop.c
===================================================================
--- trunk/ppSim/src/ppSimLoop.c	(revision 13015)
+++ trunk/ppSim/src/ppSimLoop.c	(revision 13016)
@@ -234,4 +234,34 @@
 }
 
+// Generate a header containing WCS keywords
+static psMetadata *wcsHeader(float ra0, float dec0, // Boresight (radians)
+                             float pa,// Position angle (radians)
+                             float scale, // Pixel scale (radians/pix)
+                             float x0, float y0, // Position of 0,0 on the FPA
+                             int xParity, int yParity // Parity in x and y
+                             )
+{
+    psMetadata *header = psMetadataAlloc(); // Header, to return
+    pmAstromWCS *wcs = pmAstromWCSAlloc(1, 1); // WCS structure
+    wcs->toSky = psProjectionAlloc(ra0, dec0, scale * xParity, scale * yParity, PS_PROJ_TAN);
+    wcs->cdelt1 = scale * PM_DEG_RAD * xParity;
+    wcs->cdelt2 = scale * PM_DEG_RAD * yParity;
+    wcs->crpix1 = x0;
+    wcs->crpix2 = y0;
+    wcs->trans->x->coeff[1][0] = cos(pa) * wcs->cdelt1;
+    wcs->trans->x->coeff[0][1] = -sin(pa) * wcs->cdelt1;
+    wcs->trans->y->coeff[1][0] = sin(pa) * wcs->cdelt2;
+    wcs->trans->y->coeff[0][1] = cos(pa) * wcs->cdelt2;
+    // These aren't used by pmAstromWCStoHeader, but set them anyway
+    wcs->trans->x->coeff[0][0] = ra0;
+    wcs->trans->y->coeff[0][0] = dec0;
+    wcs->trans->x->coeff[1][1] = 0.0;
+    wcs->trans->y->coeff[1][1] = 0.0;
+
+    pmAstromWCStoHeader(header, wcs);
+    psFree(wcs);
+
+    return header;
+}
 
 
@@ -650,27 +680,12 @@
                              "Binning in y", binning);
 
-            // Set up WCS in header
-            pmHDU *hdu = pmHDUGetLowest(fpa, chip, cell);
-            if (!hdu->header) {
-                hdu->header = psMetadataAlloc();
-            }
-            pmAstromWCS *wcs = pmAstromWCSAlloc(1, 1); // WCS structure
-            wcs->toSky = psProjectionAlloc(ra0, dec0, scale * xParityChip, scale * yParityChip, PS_PROJ_TAN);
-            wcs->cdelt1 = scale * PM_DEG_RAD * xParityChip;
-            wcs->cdelt2 = scale * PM_DEG_RAD * yParityChip;
-            wcs->crpix1 = fpa2cell(0.0, x0Cell, xParityCell, binning, x0Chip, xParityChip);
-            wcs->crpix2 = fpa2cell(0.0, y0Cell, yParityCell, binning, y0Chip, yParityChip);
-            wcs->trans->x->coeff[1][0] = cos(pa) * wcs->cdelt1;
-            wcs->trans->x->coeff[0][1] = -sin(pa) * wcs->cdelt1;
-            wcs->trans->y->coeff[1][0] = sin(pa) * wcs->cdelt2;
-            wcs->trans->y->coeff[0][1] = cos(pa) * wcs->cdelt2;
-            // These aren't used by pmAstromWCStoHeader, but set them anyway
-            wcs->trans->x->coeff[0][0] = ra0;
-            wcs->trans->y->coeff[0][0] = dec0;
-            wcs->trans->x->coeff[1][1] = 0.0;
-            wcs->trans->y->coeff[1][1] = 0.0;
-
-            pmAstromWCStoHeader(hdu->header, wcs);
-            psFree(wcs);
+            if (cell->hdu) {
+                cell->hdu->header = wcsHeader(ra0, dec0, pa, scale * binning,
+                                              fpa2cell(0.0, x0Cell, xParityCell, binning,
+                                                       x0Chip, xParityChip),
+                                              fpa2cell(0.0, y0Cell, yParityCell, binning,
+                                                       y0Chip, yParityChip),
+                                              xParityCell * xParityChip, yParityCell * yParityChip);
+            }
 
             if (!pmFPAfileIOChecks(config, view, PM_FPA_AFTER)) {
@@ -683,4 +698,11 @@
         }
 
+        if (chip->hdu) {
+            chip->hdu->header = wcsHeader(ra0, dec0, pa, scale * binning,
+                                          fpa2cell(0.0, 0, 1, binning, x0Chip, xParityChip),
+                                          fpa2cell(0.0, 0, 1, binning, y0Chip, yParityChip),
+                                          xParityChip, yParityChip);
+        }
+
         if (!pmFPAfileIOChecks(config, view, PM_FPA_AFTER)) {
             psError(PS_ERR_IO, false, "Unable to write file.");
@@ -693,4 +715,8 @@
     }
 
+    if (chip->hdu) {
+        fpa->hdu->header = wcsHeader(ra0, dec0, pa, scale * binning, 0.0, 0.0, 1, 1);
+    }
+
     if (!pmFPAfileIOChecks(config, view, PM_FPA_AFTER)) {
         psError(PS_ERR_IO, false, "Unable to write file.");
