Index: trunk/archive/scripts/src/phase2/papPhase2.c
===================================================================
--- trunk/archive/scripts/src/phase2/papPhase2.c	(revision 5104)
+++ trunk/archive/scripts/src/phase2/papPhase2.c	(revision 5371)
@@ -45,5 +45,5 @@
 static psMemId memPrintAlloc(const psMemBlock *mb)
 {
-    printf("Allocated memory block %lld (%lld):\n"
+    printf("Allocated memory block %ld (%ld):\n"
 	   "\tFile %s, line %d, size %d\n"
 	   "\tPosts: %x %x %x\n",
@@ -54,5 +54,5 @@
 static psMemId memPrintFree(const psMemBlock *mb)
 {
-    printf("Freed memory block %lld (%lld):\n"
+    printf("Freed memory block %ld (%ld):\n"
 	   "\tFile %s, line %d, size %d\n"
 	   "\tPosts: %x %x %x\n",
@@ -64,5 +64,5 @@
 {
     psMemBlock *mb = ((psMemBlock*)ptr) - 1;
-    printf("Memory block %lld (%lld):\n"
+    printf("Memory block %ld (%ld):\n"
 	   "\tFile %s, line %d, size %d\n"
 	   "\tPosts: %x %x %x\n",
@@ -75,7 +75,7 @@
 #if 0
     // Hunting memory leaks
-    psMemAllocateCallbackSetID(23289);
-    psMemFreeCallbackSetID(23289);
-    psMemAllocateCallbackSet(memPrintAlloc);
+    psMemAllocCallbackSetID(22367);
+    psMemFreeCallbackSetID(22367);
+    psMemAllocCallbackSet(memPrintAlloc);
     psMemFreeCallbackSet(memPrintFree);
 #endif
@@ -198,7 +198,8 @@
     bool doAstrom = false;		// Astrometry
     pmOverscanAxis overscanMode = PM_OVERSCAN_NONE; // Axis for overscan
-    pmFit overscanFit = PM_FIT_NONE;	// Fit type for overscan
+    pmFit overscanFitType = PM_FIT_NONE; // Fit type for overscan
     int overscanBins = 1;		// Number of pixels per bin for overscan
     psStats *overscanStats = NULL;	// Statistics for overscan
+    void *overscanFit = NULL;		// Overscan fit (polynomial or spline)
 
     if (psMetadataLookupBool(NULL, recipe, "MASK")) {
@@ -243,7 +244,11 @@
 	psString fit = psMetadataLookupString(NULL, recipe, "OVERSCAN.FIT");
 	if (strcasecmp(fit, "POLYNOMIAL") == 0) {
-	    overscanFit = PM_FIT_POLYNOMIAL;
+	    overscanFitType = PM_FIT_POLYNOMIAL;
+	    int order = psMetadataLookupS32(NULL, recipe, "OVERSCAN.ORDER"); // Order of polynomial fit
+	    overscanFit = psPolynomial1DAlloc(order, PS_POLYNOMIAL_ORD);
 	} else if (strcasecmp(fit, "SPLINE") == 0) {
-	    overscanFit = PM_FIT_SPLINE;
+	    overscanFitType = PM_FIT_SPLINE;
+	    int order = psMetadataLookupS32(NULL, recipe, "OVERSCAN.ORDER"); // Order of polynomial fit
+//	    overscanFit = psSpline1DAlloc(order, PS_POLYNOMIAL_ORD);
 	} else if (strcasecmp(fit, "NONE") != 0) {
 	    psLogMsg("phase2", PS_LOG_WARN, "OVERSCAN.FIT (%s) is not one of NONE, POLYNOMIAL, or SPLINE:"
@@ -297,5 +302,5 @@
 #endif
 
-    pmFPAPrint(input);
+    //pmFPAPrint(input);
 
     // Load the calibration frames, if required
@@ -475,5 +480,4 @@
 		pmReadout *inputReadout = inputReadouts->data[k]; // Readout of interest in input image
 		psImage *inputImage = inputReadout->image; // The actual image
-		psList *inputOverscans = inputReadout->overscans; // List of overscan bias regions
 
 		// Mask bad pixels
@@ -656,25 +660,24 @@
 		}
 
-		printf("mode: %d\n", overscanMode);
-
 		if (doBias || doOverscan || doDark) {
-		    void *overscanResult = NULL; // Result of overscan fit
-#ifdef PRODUCTION
-		    (void)pmSubtractBias(inputReadout, overscanResult, inputOverscans, overscanMode,
-					 overscanStats, overscanBins, overscanFit, pedestal);
-#else
-		    (void)pmSubtractBias(inputReadout, overscanResult, inputOverscans, overscanMode,
-					 overscanStats, overscanBins, overscanFit, biasReadout);
-#endif
-		    
+		    psList *inputOverscans = pmReadoutGetBias(inputReadout); // List of overscan bias regions
+#ifdef PRODUCTION
+		    (void)pmSubtractBias(inputReadout, overscanFit, inputOverscans, overscanMode,
+					 overscanStats, overscanBins, overscanFitType, pedestal);
+#else
+		    (void)pmSubtractBias(inputReadout, overscanFit, inputOverscans, overscanMode,
+					 overscanStats, overscanBins, overscanFitType, biasReadout);
+#endif
+		    psFree(inputOverscans);
+
 		    // Output overscan fit results, if required
 		    if (doOverscan) {
-			switch (overscanFit) {
+			switch (overscanFitType) {
 			  case PM_FIT_POLYNOMIAL:
 			    {
-				psPolynomial1D *poly = (psPolynomial1D*)overscanResult;	// The polynomial
+				psPolynomial1D *poly = (psPolynomial1D*)overscanFit;	// The polynomial
 				psString coeffs = NULL;	// String containing the coefficients
 				for (int i = 0; i < poly->n; i++) {
-				    psStringAppend(&coeffs, "%.2f ", poly->coeff[i]);
+				    psStringAppend(&coeffs, "%e ", poly->coeff[i]);
 				}
 				psLogMsg("phase2", PS_LOG_INFO, "Overscan polynomial coefficients:\n%s\n",
@@ -685,5 +688,5 @@
 			  case PM_FIT_SPLINE:
 			    {
-				psSpline1D *spline = (psSpline1D*)overscanResult; // The spline
+				psSpline1D *spline = (psSpline1D*)overscanFit; // The spline
 				psString coeffs = NULL;	// String containing the coefficients
 				for (int i = 0; i < spline->n; i++) {
@@ -691,5 +694,5 @@
 				    psStringAppend(&coeffs, "%d: ", i);
 				    for (int j = 0; j < poly->n; j++) {
-					psStringAppend(&coeffs, "%.2f ", poly->coeff[i]);
+					psStringAppend(&coeffs, "%e ", poly->coeff[i]);
 				    }
 				    psStringAppend(&coeffs, "\n");
@@ -755,4 +758,5 @@
     psFree(dark);
     psFree(flat);
+    psFree(overscanFit);
     psFree(overscanStats);
 
@@ -764,8 +768,10 @@
 #if 1
     for (int i = 0; i < nLeaks; i++) {
-        printf("Memory leak detection: memBlock %lld (%lld)\n"
-	       "\tFile %s, line %d, size %d\n",
-	       leaks[i]->id, leaks[i]->refCounter, leaks[i]->file, leaks[i]->lineno,
-	       leaks[i]->userMemorySize);
+	psMemBlock *mb = leaks[i];
+	printf("Memory leak %ld (%ld):\n"
+	       "\tFile %s, line %d, size %d\n"
+	       "\tPosts: %x %x %x\n",
+	       mb->id, mb->refCounter, mb->file, mb->lineno, mb->userMemorySize, mb->startblock, mb->endblock,
+	       *(void**)((int8_t *)(mb + 1) + mb->userMemorySize));
     }
 #endif
