Index: trunk/ppBackground/src/ppBackgroundStackLoop.c
===================================================================
--- trunk/ppBackground/src/ppBackgroundStackLoop.c	(revision 36642)
+++ trunk/ppBackground/src/ppBackgroundStackLoop.c	(revision 36649)
@@ -9,4 +9,7 @@
 
 #include "ppBackgroundStack.h"
+
+#define WCS_TOLERANCE 0.001             // Tolerance for WCS
+
 
 bool ppBackgroundStackLoop(ppBackgroundStackData *data // Run-time data
@@ -120,16 +123,8 @@
       if (tp->y > data->y_max) { data->y_max = tp->y; }
 
-/*       data->x_min -= data->ra_min; */
-/*       data->x_max -= data->ra_min; */
-/*       data->y_min -= data->dec_min; */
-/*       data->y_max -= data->dec_min; */
-      
-      
       psStats *stats = psStatsAlloc(PS_STAT_ROBUST_MEDIAN | PS_STAT_ROBUST_STDEV);
       psImageBinning *binning = psImageBinningAlloc();
       binning->nXruff = 13; // Number of samples
       binning->nYruff = 13; 
-      //    binning->nXfine = ceil(data->ra_max - data->ra_min) + 1; // This is the range we're looking at
-      //    binning->nYfine = ceil(data->dec_max - data->dec_min) + 1;
       binning->nXfine = ceil(data->x_max - data->x_min) + 1;
       binning->nYfine = ceil(data->y_max - data->y_min) + 1;
@@ -146,12 +141,4 @@
       P_PSIMAGE_SET_ROW0(sizeImage, data->y_min);
       data->modelMap = psImageMapAlloc(sizeImage,binning,stats);
-/*       psFree(sizeImage); */
-/*       data->modelMap = psImageMapNoImageAlloc( binning,stats); */
-/*       P_PSIMAGE_SET_COL0(data->modelMap->map, (data->x_min - binning->nXskip) / binning->nXbin); */
-/*       P_PSIMAGE_SET_ROW0(data->modelMap->map, (data->y_min - binning->nYskip) / binning->nYbin); */
-
-      // force col0/row0
-/*       data->modelMap->map->col0 = data->modelMap->binning->nXskip; */
-/*       data->modelMap->map->row0 = data->modelMap->binning->nYskip; */
       
       // PART 2:
@@ -173,5 +160,5 @@
 
       // This is where an iterative solution loop would likely start.
-      for (int iterator = 0; iterator < 2; iterator++) {
+      for (int iterator = 0; iterator < 4; iterator++) {
 	// Construct the offset information
 	printf("Model fit!\n");
@@ -310,4 +297,45 @@
 	    psFree(fp);
 	    psFree(tp);
+
+	    // Copy WCS (from ppStackUpdateHeader)
+	    pmHDU *inHDU = pmHDUFromCell(readout->parent);
+	    model->parent->hdu = pmHDUAlloc(NULL);
+	    corr->parent->hdu = pmHDUAlloc(NULL);
+	    pmHDU *modHDU= pmHDUFromCell(model->parent);
+	    pmHDU *corHDU= pmHDUFromCell(corr->parent);
+
+	    if (!modHDU || !inHDU) {
+	      psWarning("Unable to find HDU at FPA level to copy wcs!");
+	    }
+	    else {
+	      if (!pmAstromReadWCS(stack_model->fpa,model_cell->parent,inHDU->header,1.0)) {
+		psErrorClear();
+		psWarning("Unable to read WCS astrometry from input FPA!");
+	      }
+	      else {
+		if (!modHDU->header) {
+		  modHDU->header = psMetadataAlloc();
+		}
+		if (!pmAstromWriteWCS(modHDU->header, stack_model->fpa,model_cell->parent, WCS_TOLERANCE)) {
+		  psErrorClear();
+		  psWarning("Unable to read WCS astrometry from input FPA!");
+		}
+	      }
+	      if (!pmAstromReadWCS(stack_corr->fpa,corr_cell->parent,inHDU->header,1.0)) {
+		psErrorClear();
+		psWarning("Unable to read WCS astrometry from input FPA!");
+	      }
+	      else {
+		if (!corHDU->header) {
+		  corHDU->header = psMetadataAlloc();
+		}
+		if (!pmAstromWriteWCS(corHDU->header, stack_corr->fpa,corr_cell->parent, WCS_TOLERANCE)) {
+		  psErrorClear();
+		  psWarning("Unable to read WCS astrometry from input FPA!");
+		}
+	      }
+	    } // End WCS saving.
+
+	    
 	  } // Close readout
 	  printf("    I'm done with that readout\n");
@@ -339,4 +367,5 @@
       psFree(view);
       psFree(data->modelMap);
+      psFree(sizeImage);
     }
 		
