Index: trunk/archive/scripts/src/phase2/papPhase2.c
===================================================================
--- trunk/archive/scripts/src/phase2/papPhase2.c	(revision 5371)
+++ trunk/archive/scripts/src/phase2/papPhase2.c	(revision 5462)
@@ -250,5 +250,6 @@
 	    overscanFitType = PM_FIT_SPLINE;
 	    int order = psMetadataLookupS32(NULL, recipe, "OVERSCAN.ORDER"); // Order of polynomial fit
-//	    overscanFit = psSpline1DAlloc(order, PS_POLYNOMIAL_ORD);
+	    overscanFit = NULL;
+//	    overscanFit = psSpline1DAlloc();
 	} else if (strcasecmp(fit, "NONE") != 0) {
 	    psLogMsg("phase2", PS_LOG_WARN, "OVERSCAN.FIT (%s) is not one of NONE, POLYNOMIAL, or SPLINE:"
@@ -292,4 +293,5 @@
 
     // Read in the input pixels
+    psLogMsg("phase2", PS_LOG_INFO, "Opening input image: %s\n", inputName);
     if (! pmFPARead(input, inputFile, header, database)) {
 	psErrorStackPrint(stderr, "Unable to populate camera from FITS file: %s\n", inputName);
@@ -316,9 +318,10 @@
 	    exit(EXIT_FAILURE);
 	}
+	psLogMsg("phase2", PS_LOG_INFO, "Opening bias image: %s\n", biasName);
+	if (! pmFPARead(bias, biasFile, biasHeader, database)) {
+	    psErrorStackPrint(stderr, "Unable to populate bias camera from fits FITS: %s\n", biasName);
+	    exit(EXIT_FAILURE);
+	}
 	psFree(biasHeader);
-	if (! pmFPARead(bias, biasFile, NULL, database)) {
-	    psErrorStackPrint(stderr, "Unable to populate bias camera from fits FITS: %s\n", biasName);
-	    exit(EXIT_FAILURE);
-	}
 #ifdef PRODUCTION
 	psFitsClose(biasFile);
@@ -339,9 +342,10 @@
 	    exit(EXIT_FAILURE);
 	}
+	psLogMsg("phase2", PS_LOG_INFO, "Opening dark image: %s\n", darkName);
+	if (! pmFPARead(dark, darkFile, darkHeader, database)) {
+	    psErrorStackPrint(stderr, "Unable to populate dark camera from fits FITS: %s\n", darkName);
+	    exit(EXIT_FAILURE);
+	}
 	psFree(darkHeader);
-	if (! pmFPARead(dark, darkFile, NULL, database)) {
-	    psErrorStackPrint(stderr, "Unable to populate dark camera from fits FITS: %s\n", darkName);
-	    exit(EXIT_FAILURE);
-	}
 #ifdef PRODUCTION
 	psFitsClose(darkFile);
@@ -362,9 +366,11 @@
 	    exit(EXIT_FAILURE);
 	}
+	psLogMsg("phase2", PS_LOG_INFO, "Opening mask image: %s\n", maskName);
+	if (! pmFPARead(mask, maskFile, maskHeader, database)) {
+	    psErrorStackPrint(stderr, "Unable to populate mask camera from fits FITS: %s\n", maskName);
+	    exit(EXIT_FAILURE);
+	}
 	psFree(maskHeader);
-	if (! pmFPARead(mask, maskFile, NULL, database)) {
-	    psErrorStackPrint(stderr, "Unable to populate mask camera from fits FITS: %s\n", maskName);
-	    exit(EXIT_FAILURE);
-	}
+
 #ifdef PRODUCTION
 	psFitsClose(maskFile);
@@ -380,4 +386,8 @@
 	psFits *flatFile = psFitsAlloc(flatName);
 #endif
+	if (! flatFile) {
+	    psErrorStackPrint(stderr, "Can't open flat image: %s\n", flatName);
+	    exit(EXIT_FAILURE);
+	}
 	psMetadata *flatHeader = psFitsReadHeader(NULL, flatFile); // FITS header for flat
 	if (! pmConfigValidateCamera(camera, flatHeader)) {
@@ -385,9 +395,10 @@
 	    exit(EXIT_FAILURE);
 	}
+	psLogMsg("phase2", PS_LOG_INFO, "Opening flat image: %s\n", flatName);
+	if (! pmFPARead(flat, flatFile, flatHeader, database)) {
+	    psErrorStackPrint(stderr, "Unable to populate flat camera from fits FITS: %s\n", flatName);
+	    exit(EXIT_FAILURE);
+	}
 	psFree(flatHeader);
-	if (! pmFPARead(flat, flatFile, NULL, database)) {
-	    psErrorStackPrint(stderr, "Unable to populate flat camera from fits FITS: %s\n", flatName);
-	    exit(EXIT_FAILURE);
-	}
 #ifdef PRODUCTION
 	psFitsClose(flatFile);
@@ -499,5 +510,5 @@
 		    } else {
 			switch (dataItem->type) {
-			  case PS_META_VEC:
+			  case PS_DATA_VECTOR:
 			    {
 				// These are the polynomial coefficients
@@ -518,5 +529,5 @@
 			    }
 			    break;
-			  case PS_META_STR:
+			  case PS_DATA_STRING:
 			    {
 				// This is a filename
@@ -536,5 +547,5 @@
 			    }
 			    break;
-			  case PS_META_META:
+			  case PS_DATA_METADATA:
 			    {
 				// This is a menu
@@ -550,5 +561,5 @@
 					psLogMsg("phase2", PS_LOG_WARN, "Unable to find value of concept %s "
 						 "for non-linearity correction --- ignored.\n", concept);
-				    } else if (conceptValueItem->type != PS_META_STR) {
+				    } else if (conceptValueItem->type != PS_DATA_STRING) {
 					psLogMsg("phase2", PS_LOG_WARN, "Type for concept %s isn't STRING, as"
 						 " expected for non-linearity correction --- ignored.\n",
@@ -561,5 +572,5 @@
 					    psLogMsg("phase2", PS_LOG_WARN, "Unable to find %s in NONLIN.DATA"
 						     " --- ignored.\n", conceptValue);
-					} else if (optionItem->type == PS_META_VEC) {
+					} else if (optionItem->type == PS_DATA_VECTOR) {
 					    // These are the polynomial coefficients
 					    psVector *coeff = optionItem->data.V; // The coefficient vector
@@ -578,5 +589,5 @@
 					    psFree(coeff);
 					    psFree(correction);
-					} else if (optionItem->type == PS_META_STR) {
+					} else if (optionItem->type == PS_DATA_STRING) {
 					    // This is a filename
 					    psString tableName = optionItem->data.V; // The filename
@@ -673,45 +684,64 @@
 		    // Output overscan fit results, if required
 		    if (doOverscan) {
-			switch (overscanFitType) {
-			  case PM_FIT_POLYNOMIAL:
-			    {
-				psPolynomial1D *poly = (psPolynomial1D*)overscanFit;	// The polynomial
-				psString coeffs = NULL;	// String containing the coefficients
-				for (int i = 0; i < poly->n; i++) {
-				    psStringAppend(&coeffs, "%e ", poly->coeff[i]);
-				}
-				psLogMsg("phase2", PS_LOG_INFO, "Overscan polynomial coefficients:\n%s\n",
-					 coeffs);
-				psFree(coeffs);
-			    }
-			    break;
-			  case PM_FIT_SPLINE:
-			    {
-				psSpline1D *spline = (psSpline1D*)overscanFit; // The spline
-				psString coeffs = NULL;	// String containing the coefficients
-				for (int i = 0; i < spline->n; i++) {
-				    psPolynomial1D *poly = spline->spline[i]; // i-th polynomial
-				    psStringAppend(&coeffs, "%d: ", i);
-				    for (int j = 0; j < poly->n; j++) {
+			if (! overscanFit) {
+			    psLogMsg("phase2", PS_LOG_WARN, "No fit generated!\n");
+			} else {
+			    switch (overscanFitType) {
+			      case PM_FIT_POLYNOMIAL:
+				{
+				    psPolynomial1D *poly = (psPolynomial1D*)overscanFit; // The polynomial
+				    psString coeffs = NULL;	// String containing the coefficients
+				    for (int i = 0; i < poly->nX; i++) {
 					psStringAppend(&coeffs, "%e ", poly->coeff[i]);
 				    }
-				    psStringAppend(&coeffs, "\n");
+				    psLogMsg("phase2", PS_LOG_INFO, "Overscan polynomial coefficients:\n%s\n",
+					     coeffs);
+				    psFree(coeffs);
 				}
-				psLogMsg("phase2", PS_LOG_INFO, "Overscan spline coefficients:\n%s\n",
-					 coeffs);
-				psFree(coeffs);
+				break;
+			      case PM_FIT_SPLINE:
+				{
+				    psSpline1D *spline = (psSpline1D*)overscanFit; // The spline
+				    psString coeffs = NULL;	// String containing the coefficients
+				    for (int i = 0; i < spline->n; i++) {
+					psPolynomial1D *poly = spline->spline[i]; // i-th polynomial
+					psStringAppend(&coeffs, "%d: ", i);
+					for (int j = 0; j < poly->nX; j++) {
+					    psStringAppend(&coeffs, "%e ", poly->coeff[i]);
+					}
+					psStringAppend(&coeffs, "\n");
+				    }
+				    psLogMsg("phase2", PS_LOG_INFO, "Overscan spline coefficients:\n%s\n",
+					     coeffs);
+				    psFree(coeffs);
+				}
+				break;
+			      case PM_FIT_NONE:
+				break;
+			      default:
+				psAbort(__func__, "Should never get here!!!\n");
 			    }
-			    break;
-			  case PM_FIT_NONE:
-			    break;
-			  default:
-			    psAbort(__func__, "Should never get here!!!\n");
 			}
 		    }
 		}
 
+		// Trim, so that flat, fringe etc computations are faster.
+		psRegion *trimRegion = psMetadataLookupPtr(NULL, inputCell->concepts, "CELL.TRIMSEC");
+		psImage *trimmed = psImageSubset(inputReadout->image, *trimRegion);
+		psFree(inputReadout->image);
+		inputReadout->image = trimmed;
+
 		// Flat-field correction
 		if (doFlat) {
-		    (void)pmFlatField(inputReadout, flatReadout);
+		    psLogMsg("phase2", PS_LOG_INFO, "Performing flat field.\n");
+#ifndef PRODUCTION
+		    psImage *dummyImage = psImageAlloc(inputReadout->image->numCols,
+						       inputReadout->image->numRows,
+						       PS_TYPE_U8);
+		    pmReadout *dummyMask = pmReadoutAlloc(NULL, dummyImage, NULL, 0, 0, 1, 1, 1, 1);
+		    (void)pmFlatField(inputReadout, dummyMask, flatReadout);
+		    psFree(dummyMask);
+		    psFree(dummyImage);
+#endif
 		}
 
