Index: trunk/ppBackground/src/ppBackgroundStackCamera.c
===================================================================
--- trunk/ppBackground/src/ppBackgroundStackCamera.c	(revision 36582)
+++ trunk/ppBackground/src/ppBackgroundStackCamera.c	(revision 36615)
@@ -33,4 +33,6 @@
     pmConfig *config = data->config;    // Because I'm reusing code.
     int u,v;
+    size_t A,P;
+    double RR = -9999.0 ,DD = -99999.0,rr = 9999.0,dd = 99999.0;
     // FIX Figure out what stacks we have to deal with.
     psString stackName = data->stacks->data[0];
@@ -49,4 +51,10 @@
     }
     psArrayAdd(data->stack_data,data->stack_data->n,psMemIncrRefCounter(stack));
+    pmFPAfileActivate(config->files, false, NULL);
+
+    // You know what? Let's just fucking lie to config->files.
+    psMetadata *config_files = config->files;
+    config->files = NULL;
+    config->files = psMetadataAlloc();
     
     // Read over the input background models.    
@@ -62,18 +70,31 @@
 	return(false);
       }
-
+      
+      // Pull out the information for this exposure
       psMetadata *input = item->data.md; // the input metadata of interest
-
       psString smfFileName = psMetadataLookupStr(NULL, input, "astrom");
       psMetadata *modelContent = psMetadataLookupMetadata(NULL, input, "models");
+
+      // Allocate the model metadata object
+      psMetadata *Bmodel = psMetadataAlloc();
+      
       // Read the smf file from this item
       fileArguments("astrom",smfFileName,"",config);
       pmFPAfile *smfFile = pmFPAfileDefineFromArgs(&status, config, "PSWARP.ASTROM","astrom");
-
+      
+      // taking from pswarpLoadAstrometry.c
+      smfFile->type = PM_FPA_FILE_WCS;
       // Read the SMF data
       pmFPAview *view = pmFPAviewAlloc(0); // Pointer into FPA hierarchy
+
       if (!pmFPAfileIOChecks(config, view, PM_FPA_BEFORE)) {
 	return NULL;
       }
+      if (!pmFPAfileRead(smfFile,view,config)) {
+	psError(PS_ERR_IO, false, "failed READ at FPA %s", smfFile->name);
+	psFree(view);
+	return false;
+      }
+      
       printf("CZW: Item %d\n",i);
       // find the FPA phu
@@ -93,4 +114,8 @@
 	}
       }
+
+      psMemStats(0,&A,&P);
+      fprintf(stderr,"fpa %ld %ld\n",A,P);
+
       pmChip *chip;                       // Chip from FPA
       while ((chip = pmFPAviewNextChip(view, smfFile->fpa, 1))) {
@@ -106,4 +131,7 @@
 	  psWarning("More than one cell present for chip %d", view->chip);
 	}
+
+	psMemStats(0,&A,&P);
+	fprintf(stderr,"chip %ld %ld\n",A,P);
 
 	// read WCS data from the corresponding header
@@ -128,5 +156,5 @@
 	}
 
-	// FIX Load model data for this chip
+	//  Load model data for this chip
 	psString modelFileName = pmConfigConvertFilename(psMetadataLookupStr(NULL, modelContent, chipName),
 							 config, PM_FPA_MODE_READ, false);
@@ -175,18 +203,27 @@
 	    if (tp->y < data->dec_min) { data->dec_min = tp->y; }
 	    else if (tp->y > data->dec_max) { data->dec_max = tp->y; }
-	  }
-	}
-
+
+	    if (sky->r < rr) { rr = sky->r; }
+	    else if (sky->r > RR) { RR = sky->r; }
+	    if (sky->r < dd) { dd = sky->d; }
+	    else if (sky->d > DD) { DD = sky->d; }
+	    
+	  }
+	}
+
+	// Allocate the model data for this chip
+	psMetadata *this_model = psMetadataAlloc();
+		
 	// Attach vectors to teh structure of the chip
-	psMetadataAddImage(chip->analysis,PS_LIST_TAIL,
+	psMetadataAddImage(this_model,PS_LIST_TAIL,
 			   "bkg image", 0,
 			   "ota space X vector", image);
-	psMetadataAddImage(chip->analysis,PS_LIST_TAIL,
+	psMetadataAddImage(this_model,PS_LIST_TAIL,
 			   "bkg ra", 0,
 			   "ota space ra vector", raim);
-	psMetadataAddImage(chip->analysis,PS_LIST_TAIL,
+	psMetadataAddImage(this_model,PS_LIST_TAIL,
 			   "bkg dec", 0,
 			   "ota space dec vector", decim);
-	psMetadataAddImage(chip->analysis,PS_LIST_TAIL,
+	psMetadataAddImage(this_model,PS_LIST_TAIL,
 			   "bkg calibrated data", 0,
 			   "ota space corrected data", model);
@@ -194,11 +231,15 @@
 	// Define default background model parameters, using the assumption:
 	// observed = camera + offset + scale * astrophysical
-	psMetadataAddF32(chip->analysis,PS_LIST_TAIL,
+	psMetadataAddF32(this_model,PS_LIST_TAIL,
 			 "bkg offset", PS_META_REPLACE,
 			 "background offset for this exposure/ota pair", 0.0);
-	psMetadataAddF32(chip->analysis,PS_LIST_TAIL,
+	psMetadataAddF32(this_model,PS_LIST_TAIL,
 			 "bkg scale", PS_META_REPLACE,
 			 "background scale parameter for this exposure/ota pair", 1.0);
-	// FIX Free model data for this chip
+	// Add this model to the current model
+	psMetadataAddMetadata(Bmodel,PS_LIST_TAIL,
+			      chipName, PS_META_REPLACE,
+			      "model data for this exposure/ota", this_model);
+	// Free model data for this chip
 	psFree(modelFileName);
 	psFree(modelFits);
@@ -212,5 +253,6 @@
 	psFree(decim);
 	psFree(model);
-
+	psFree(this_model);
+	
 	// Check to see if we've loaded or allocated an OTA solution container for this chip
 	if (!psMetadataLookupPtr(NULL,data->OTA_solutions,chipName)) { // No solution metadata entry exists for this chipName
@@ -235,32 +277,61 @@
 	  }
 	}
-	
-	// FIX Hopefully this isn't going to destroy everything.
-	// Chip
+
+	
+	psMemStats(0,&A,&P);
+	fprintf(stderr,"chip2 %ld %ld\n",A,P);
+	
 /* 	if (!pmFPAfileIOChecks(config, view, PM_FPA_AFTER)) { */
 /* 	  psError(psErrorCodeLast(), false, "Error saving data to files."); */
 /* 	  return false; */
 /* 	} */
-      }
-      // FPA
-/*       if (!pmFPAfileIOChecks(config, view, PM_FPA_AFTER)) { */
-/* 	psError(psErrorCodeLast(), false, "Error saving data to files."); */
-/* 	return false; */
-/*       } */
-
-      // Add this smf to the smf array.
-      psArrayAdd(data->smfs,data->smfs->n,psMemIncrRefCounter(smfFile));
-      psFree(smfFile);
-    }
-
+      } // end chip loop
+
+      // Add the set of models to the datastructure
+      psMetadataAddMetadata(data->models, PS_LIST_TAIL,
+			    smfFile->origname, PS_META_REPLACE,
+			    "model data for this exposure", Bmodel);
+      psMetadataAddS16(data->models,PS_LIST_TAIL,
+		       "N", PS_META_REPLACE,
+		       "counter", i);
+      // data->models->name->XY__->{image/ra/dec/calibrated/offset/scale}
+      // data->models->counter->XY__->{image/ra/dec/calibrated/offset/scale}
+
+      
+      
+      if (!pmFPAfileIOChecks(config, view, PM_FPA_AFTER)) {
+	psError(psErrorCodeLast(), false, "Error saving data to files.");
+	return false;
+      }
+      pmFPAfileActivate(config->files, false, NULL);
+      psFree(modelContent);
+      //      psFree(smfFile);
+      psFree(smfFileName);
+      psFree(view);
+
+      psFree(config->files);
+      config->files = psMetadataAlloc();
+      // 
+      
+      //
+    } // end smf loop
+
+    // Unlie now.
+    psFree(config->files);
+    config->files = config_files;
+    
     // Allocate the modelMap for the region we're covering.
 
     psStats *stats = psStatsAlloc(PS_STAT_ROBUST_MEDIAN | PS_STAT_ROBUST_STDEV);
     psImageBinning *binning = psImageBinningAlloc();
-    binning->nXruff = 100; // Number of samples
-    binning->nYruff = 100; 
+    binning->nXruff = 33; // Number of samples
+    binning->nYruff = 33; 
     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->nXskip = floor(data->ra_min);
+    binning->nYskip = floor(data->dec_min);
+    psImageBinningSetScale(binning,PS_IMAGE_BINNING_CENTER);
+    printf("Sizes: sky: %f %f -> %f %f :: tp: %f %f -> %f %f :: map: %d %d\n",
+	   rr,dd,RR,DD,data->ra_min,data->dec_min,data->ra_max,data->dec_max,binning->nXfine,binning->nYfine);
     data->modelMap = psImageMapNoImageAlloc( binning,stats);
     
