Index: /trunk/archive/scripts/src/phase2/papPhase2.c
===================================================================
--- /trunk/archive/scripts/src/phase2/papPhase2.c	(revision 5461)
+++ /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
 		}
 
Index: /trunk/archive/scripts/src/phase2/phase2.config
===================================================================
--- /trunk/archive/scripts/src/phase2/phase2.config	(revision 5461)
+++ /trunk/archive/scripts/src/phase2/phase2.config	(revision 5462)
@@ -3,8 +3,8 @@
 # List of tasks to perform
 MASK		BOOL	FALSE		# Mask bad pixels
-NONLIN		BOOL	FALSE		# Non-linearity correction
+NONLIN		BOOL	TRUE		# Non-linearity correction
 BIAS		BOOL	FALSE		# Bias subtraction
 DARK		BOOL	FALSE		# Dark subtraction
-OVERSCAN	BOOL	TRUE		# Overscan subtraction
+OVERSCAN	BOOL	FALSE		# Overscan subtraction
 FLAT		BOOL	FALSE		# Flat-field normalisation
 FRINGE		BOOL	FALSE		# Fringe subtraction
@@ -58,6 +58,7 @@
 # Overscan subtraction
 OVERSCAN.MODE		STR	INDIVIDUAL	# NONE | INDIVIDUAL | ALL
-OVERSCAN.FIT		STR	SPLINE		# NONE | POLYNOMIAL | SPLINE
-OVERSCAN.ORDER		S32	10		# Order of polynomial fit
+#OVERSCAN.FIT		STR	SPLINE		# NONE | POLYNOMIAL | SPLINE
+OVERSCAN.FIT		STR	POLYNOMIAL	# NONE | POLYNOMIAL | SPLINE
+OVERSCAN.ORDER		S32	5		# Order of polynomial fit
 OVERSCAN.BIN		S32	0		# Number of pixels per bin
 OVERSCAN.STAT		STR	MEAN		# MEAN | MEDIAN
Index: /trunk/archive/scripts/src/phase2/pmConfig.c
===================================================================
--- /trunk/archive/scripts/src/phase2/pmConfig.c	(revision 5461)
+++ /trunk/archive/scripts/src/phase2/pmConfig.c	(revision 5462)
@@ -141,5 +141,5 @@
 	psMetadataItem *traceItem = NULL; // Item from MD iteration
 	while (traceItem = psMetadataGetAndIncrement(traceIter)) {
-	    if (traceItem->type != PS_META_S32) {
+	    if (traceItem->type != PS_DATA_S32) {
 		psLogMsg(__func__, PS_LOG_WARN, "The level for trace component %s is not of type S32 (%x)\n",
 			 traceItem->name, traceItem->type);
@@ -181,5 +181,5 @@
 	// Check to see if the rule works
 	switch (ruleItem->type) {
-	  case PS_META_STR:
+	  case PS_DATA_STRING:
 	    psTrace(__func__, 8, "Matching %s: '%s' vs '%s'\n", ruleItem->name,
 		    ruleItem->data.V, headerItem->data.V);
@@ -247,7 +247,7 @@
 		cameraItem->comment);
 	psMetadata *camera = NULL;	// The camera metadata
-	if (cameraItem->type == PS_META_META) {
+	if (cameraItem->type == PS_DATA_METADATA) {
 	    camera = psMemIncrRefCounter(cameraItem->data.md);
-	} else if (cameraItem->type == PS_META_STR) {
+	} else if (cameraItem->type == PS_DATA_STRING) {
 	    psTrace(__func__, 5, "Reading camera configuration for %s...\n", cameraItem->name);
 	    int badLines = 0;		// Number of bad lines in reading camera configuration
Index: /trunk/archive/scripts/src/phase2/pmFPA.c
===================================================================
--- /trunk/archive/scripts/src/phase2/pmFPA.c	(revision 5461)
+++ /trunk/archive/scripts/src/phase2/pmFPA.c	(revision 5462)
@@ -202,5 +202,7 @@
     pmReadout *readout = psAlloc(sizeof(pmReadout));
     psMemSetDeallocator(readout, (psFreeFunc)p_pmReadoutFree);
-    cell->readouts = psArrayAdd(cell->readouts, 0, readout);
+    if (cell) {
+	cell->readouts = psArrayAdd(cell->readouts, 0, readout);
+    }
     
     // Set the components
Index: /trunk/archive/scripts/src/phase2/pmFPA.h
===================================================================
--- /trunk/archive/scripts/src/phase2/pmFPA.h	(revision 5461)
+++ /trunk/archive/scripts/src/phase2/pmFPA.h	(revision 5462)
@@ -6,6 +6,6 @@
 
 // Temporary metadata types
-#define PS_META_CHIP PS_META_UNKNOWN
-#define PS_META_CELL PS_META_UNKNOWN
+//#define PS_META_CHIP PS_DATA_UNKNOWN
+//#define PS_META_CELL PS_DATA_UNKNOWN
 
 typedef struct {
Index: /trunk/archive/scripts/src/phase2/pmFPAConceptsGet.c
===================================================================
--- /trunk/archive/scripts/src/phase2/pmFPAConceptsGet.c	(revision 5461)
+++ /trunk/archive/scripts/src/phase2/pmFPAConceptsGet.c	(revision 5462)
@@ -100,5 +100,5 @@
     psMetadataItem *defItem = psMetadataLookup(defaults, concept);
     if (defItem) {
-	if (defItem->type == PS_META_META) {
+	if (defItem->type == PS_DATA_METADATA) {
 	    // A dependent default
 	    psTrace(__func__, 7, "Evaluating dependent default....\n");
@@ -122,5 +122,5 @@
 		return NULL;
 	    }
-	    if (depItem->type != PS_META_STR) {
+	    if (depItem->type != PS_DATA_STRING) {
 		psError(PS_ERR_IO, true, "Value of %s is not of type string, as required for dependency"
 			" --- ignored.\n", dependsOn);
@@ -270,12 +270,12 @@
     if (item) {
 	switch (item->type) {
-	  case PS_META_F32:
+	  case PS_DATA_F32:
 	    value = item->data.F32;
 	    break;
-	  case PS_META_F64:
+	  case PS_DATA_F64:
 	    // Assume it's OK to truncate to floating point from double
 	    value = (float)item->data.F64;
 	    break;
-	  case PS_META_S32:
+	  case PS_DATA_S32:
 	    // Promote to float
 	    value = (float)item->data.S32;
@@ -370,5 +370,5 @@
     if (item) {
 	switch (item->type) {
-	  case PS_META_STR:
+	  case PS_DATA_STRING:
 	    value = item->data.V;
 	    break;
@@ -383,5 +383,5 @@
     psTrace(__func__, 7, "Adding %s (%s): %s\n", name, comment, value);
 
-    psMetadataAdd(concepts, PS_LIST_TAIL, name, PS_META_STR, comment, value);
+    psMetadataAdd(concepts, PS_LIST_TAIL, name, PS_DATA_STRING, comment, value);
 }
 
@@ -428,5 +428,5 @@
 		ra = raItem->data.F64;
 		break;
-	      case PS_META_STR:
+	      case PS_DATA_STRING:
 		// Sexagesimal format
 		{
@@ -480,5 +480,5 @@
 	}
 
-	psMetadataAdd(fpa->concepts, PS_LIST_TAIL, "FPA.RA", PS_META_F64, 
+	psMetadataAdd(fpa->concepts, PS_LIST_TAIL, "FPA.RA", PS_DATA_F64, 
 		      "Right Ascension of the boresight (radians)", ra);
 
@@ -497,5 +497,5 @@
 		dec = decItem->data.F64;
 		break;
-	      case PS_META_STR:
+	      case PS_DATA_STRING:
 		// Sexagesimal format
 		{
@@ -549,5 +549,5 @@
 	}
 
-	psMetadataAdd(fpa->concepts, PS_LIST_TAIL, "FPA.DEC", PS_META_F64, 
+	psMetadataAdd(fpa->concepts, PS_LIST_TAIL, "FPA.DEC", PS_DATA_F64, 
 		      "Declination of the boresight (radians)", dec);
 
@@ -633,5 +633,5 @@
 	    psError(PS_ERR_IO, false, "Couldn't find CELL.TRIMSEC.\n");
 	    *trimsec = psRegionSet(0.0, 0.0, 0.0, 0.0);
-	} else if (secItem->type != PS_META_STR) {
+	} else if (secItem->type != PS_DATA_STRING) {
 	    psError(PS_ERR_IO, true, "CELL.TRIMSEC is not of type STR (%x)\n", secItem->type);
 	    *trimsec = psRegionSet(0.0, 0.0, 0.0, 0.0);
@@ -643,5 +643,5 @@
 		psError(PS_ERR_IO, false, "Couldn't find CELL.TRIMSEC.SOURCE.\n");
 		*trimsec = psRegionSet(0.0, 0.0, 0.0, 0.0);
-	    } else if (sourceItem->type != PS_META_STR) {
+	    } else if (sourceItem->type != PS_DATA_STRING) {
 		psError(PS_ERR_IO, true, "CELL.TRIMSEC.SOURCE is not of type STR (%x)\n", sourceItem->type);
 		*trimsec = psRegionSet(0.0, 0.0, 0.0, 0.0);
@@ -681,5 +681,5 @@
 	} // Looking up CELL.TRIMSEC
 
-	psMetadataAdd(cell->concepts, PS_LIST_TAIL, "CELL.TRIMSEC", PS_META_UNKNOWN,
+	psMetadataAdd(cell->concepts, PS_LIST_TAIL, "CELL.TRIMSEC", PS_DATA_UNKNOWN,
 		      "Trim section", trimsec);
 	psFree(trimsec);
@@ -693,5 +693,5 @@
 	if (! secItem) {
 	    psError(PS_ERR_IO, false, "Couldn't find CELL.BIASSEC.\n");
-	} else if (secItem->type != PS_META_STR) {
+	} else if (secItem->type != PS_DATA_STRING) {
 	    psError(PS_ERR_IO, true, "CELL.BIASSEC is not of type STR (%x)\n", secItem->type);
 	} else {
@@ -701,5 +701,5 @@
 	    if (! sourceItem) {
 		psError(PS_ERR_IO, false, "Couldn't find CELL.BIASSEC.SOURCE.\n");
-	    } else if (sourceItem->type != PS_META_STR) {
+	    } else if (sourceItem->type != PS_DATA_STRING) {
 		psError(PS_ERR_IO, true, "CELL.BIASSEC.SOURCE is not of type STR (%x)\n", sourceItem->type);
 	    } else {
@@ -752,5 +752,5 @@
 	} // Looking up CELL.BIASSEC
 
-	psMetadataAdd(cell->concepts, PS_LIST_TAIL, "CELL.BIASSEC", PS_META_LIST, "Bias sections", biassecs);
+	psMetadataAdd(cell->concepts, PS_LIST_TAIL, "CELL.BIASSEC", PS_DATA_LIST, "Bias sections", biassecs);
 	psFree(biassecs);
     }
@@ -762,5 +762,5 @@
 	if (! binItem) {
 	    psError(PS_ERR_IO, false, "Couldn't find CELL.XBIN.\n");
-	} else if (binItem->type == PS_META_STR) {
+	} else if (binItem->type == PS_DATA_STRING) {
 	    psString binString = binItem->data.V; // The string containing the binning
 	    if (sscanf(binString, "%d %*d", &xBin) != 1 &&
@@ -784,5 +784,5 @@
 	if (! binItem) {
 	    psError(PS_ERR_IO, false, "Couldn't find CELL.YBIN.\n");
-	} else if (binItem->type == PS_META_STR) {
+	} else if (binItem->type == PS_DATA_STRING) {
 	    psString binString = binItem->data.V; // The string containing the binning
 	    if (sscanf(binString, "%*d %d", &yBin) != 1 &&
@@ -809,5 +809,5 @@
 	if (! sysItem) {
 	    psError(PS_ERR_IO, true, "Couldn't find CELL.TIMESYS --- assuming UTC.\n");
-	} else if (sysItem->type != PS_META_STR) {
+	} else if (sysItem->type != PS_DATA_STRING) {
 	    psError(PS_ERR_IO, true, "CELL.TIMESYS isn't of type STRING --- assuming UTC.\n");
 	} else {
@@ -839,5 +839,5 @@
 		if (mdok && strlen(timeFormat) > 0) {
 		    switch (timeItem->type) {
-		      case PS_META_STR:
+		      case PS_DATA_STRING:
 			{
 			    psString timeString = timeItem->data.V;	// String with the time
@@ -887,5 +887,5 @@
 					    psError(PS_ERR_IO, false, "Unable to find time header: %s\n",
 						    timeName);
-					} else if (timeItem->type == PS_META_STR) {
+					} else if (timeItem->type == PS_DATA_STRING) {
 					    // Time is a string, in the usual way:
 					    psStringAppend(&dateString, "T%s", timeItem->data.V);
@@ -965,5 +965,5 @@
 	} // Getting CELL.TIME
 
-	psMetadataAdd(cell->concepts, PS_LIST_TAIL, "CELL.TIME", PS_META_UNKNOWN, "Time of exposure", time);
+	psMetadataAdd(cell->concepts, PS_LIST_TAIL, "CELL.TIME", PS_DATA_UNKNOWN, "Time of exposure", time);
 	psFree(time);
     }
Index: /trunk/archive/scripts/src/phase2/pmFPAConceptsSet.c
===================================================================
--- /trunk/archive/scripts/src/phase2/pmFPAConceptsSet.c	(revision 5461)
+++ /trunk/archive/scripts/src/phase2/pmFPAConceptsSet.c	(revision 5462)
@@ -68,6 +68,6 @@
 	}
 	return (item1->data.F64 == item2->data.F64) ? true : false;
-      case PS_META_STR:
-	if (item2->type != PS_META_STR) {
+      case PS_DATA_STRING:
+	if (item2->type != PS_DATA_STRING) {
 	    return false;
 	}
@@ -112,14 +112,14 @@
 	// XXX: Need to expand range of types
 	switch (concept->type) {
-	  case PS_META_STR:
+	  case PS_DATA_STRING:
 	    headerItem = psMetadataItemAllocStr(keyword, concept->comment, concept->data.V);
 	    break;
-	  case PS_META_S32:
+	  case PS_DATA_S32:
 	    headerItem = psMetadataItemAllocS32(keyword, concept->comment, concept->data.S32);
 	    break;
-	  case PS_META_F32:
+	  case PS_DATA_F32:
 	    headerItem = psMetadataItemAllocF32(keyword, concept->comment, concept->data.F32);
 	    break;
-	  case PS_META_F64:
+	  case PS_DATA_F64:
 	    headerItem = psMetadataItemAllocF64(keyword, concept->comment, concept->data.F64);
 	    break;
@@ -183,5 +183,5 @@
     psMetadataItem *defItem = psMetadataLookup(defaults, concept->name);
     if (defItem) {
-	if (defItem->type == PS_META_META) {
+	if (defItem->type == PS_DATA_METADATA) {
 	    // A dependent default
 	    psTrace(__func__, 7, "Evaluating dependent default....\n");
@@ -205,5 +205,5 @@
 		return false;
 	    }
-	    if (depItem->type != PS_META_STR) {
+	    if (depItem->type != PS_DATA_STRING) {
 		psError(PS_ERR_IO, true, "Value of %s is not of type string, as required for dependency"
 			" --- ignored.\n", dependsOn);
@@ -555,5 +555,5 @@
 	if (! sourceItem) {
 	    psError(PS_ERR_IO, false, "Couldn't find CELL.TRIMSEC.SOURCE.\n");
-	} else if (sourceItem->type != PS_META_STR) {
+	} else if (sourceItem->type != PS_DATA_STRING) {
 	    psError(PS_ERR_IO, true, "CELL.TRIMSEC.SOURCE is not of type STR (%x)\n", sourceItem->type);
 	} else {
@@ -596,5 +596,5 @@
 	if (! sourceItem) {
 	    psError(PS_ERR_IO, false, "Couldn't find CELL.BIASSEC.SOURCE.\n");
-	} else if (sourceItem->type != PS_META_STR) {
+	} else if (sourceItem->type != PS_DATA_STRING) {
 	    psError(PS_ERR_IO, true, "CELL.BIASSEC.SOURCE is not of type STR (%x)\n", sourceItem->type);
 	} else {
@@ -668,5 +668,5 @@
 			    psRegion *biassec = psListGetAndIncrement(biassecsIter);
 			    psString biassecString = psRegionToString(*biassec);
-			    psMetadataAdd(header, PS_LIST_TAIL, keyword, PS_META_STR | PS_META_REPLACE,
+			    psMetadataAdd(header, PS_LIST_TAIL, keyword, PS_DATA_STRING | PS_META_REPLACE,
 					  "Bias section", biassecString);
 			    psFree(biassecString);
Index: /trunk/archive/scripts/src/phase2/pmFPAConstruct.c
===================================================================
--- /trunk/archive/scripts/src/phase2/pmFPAConstruct.c	(revision 5461)
+++ /trunk/archive/scripts/src/phase2/pmFPAConstruct.c	(revision 5462)
@@ -37,7 +37,7 @@
     psMetadataItem *item = NULL;	// Item from iteration
     while (item = psMetadataGetAndIncrement(iter)) {
-	if (item->type == PS_META_MULTI || item->type == PS_META_META) {
-	    psLogMsg(__func__, PS_LOG_WARN, "PS_META_MULTI and PS_META_META are not supported in a cell "
-		     "definition --- %s ignored.\n", item->name);
+	if (item->type == PS_DATA_METADATA_MULTI || item->type == PS_DATA_METADATA) {
+	    psLogMsg(__func__, PS_LOG_WARN, "PS_DATA_METADATA_MULTI and PS_DATA_METADATA are not supported "
+		     "in a cell definition --- %s ignored.\n", item->name);
 	    continue;
 	}
@@ -89,5 +89,5 @@
 		pmChip *chip = pmChipAlloc(fpa, extName); // The chip
 		chip->data = pmPixelDataAlloc(extName); // Prepare chip to receive FITS data
-		if (contentItem->type != PS_META_STR) {
+		if (contentItem->type != PS_DATA_STRING) {
 		    psLogMsg(__func__, PS_LOG_WARN, "Type of content item (%x) is not string: ignored\n",
 			     contentItem->type);
@@ -117,5 +117,5 @@
 		psTrace(__func__, 1, "Getting %s....\n", extName);
 
-		if (contentItem->type != PS_META_STR) {
+		if (contentItem->type != PS_DATA_STRING) {
 		    psLogMsg(__func__, PS_LOG_WARN, "Type of content item (%x) is not string: ignored\n",
 			     contentItem->type);
@@ -136,5 +136,5 @@
 		    if (! chipItem) {
 			chip = pmChipAlloc(fpa, chipName);
-			psMetadataAdd(chips, PS_LIST_TAIL, chipName, PS_META_CHIP, "", chip);
+			psMetadataAdd(chips, PS_LIST_TAIL, chipName, PS_DATA_UNKNOWN, "", chip);
 		    } else {
 			chip = psMemIncrRefCounter(chipItem->data.V);
@@ -161,5 +161,5 @@
 		psString chipName = contentItem->name; // The name of the chip
 
-		if (contentItem->type != PS_META_STR) {
+		if (contentItem->type != PS_DATA_STRING) {
 		    psLogMsg(__func__, PS_LOG_WARN, "Type of content item (%x) is not string: ignored\n",
 			     contentItem->type);
@@ -234,5 +234,5 @@
 		psString extName = contentItem->name; // The name of the extension
 		// Content is a cell type
-		if (contentItem->type != PS_META_STR) {
+		if (contentItem->type != PS_DATA_STRING) {
 		    psLogMsg(__func__, PS_LOG_WARN,
 			     "CONTENT metadata for extension %s is not of type string, but %x --- ignored\n",
Index: /trunk/archive/scripts/src/phase2/pmFlatField.c
===================================================================
--- /trunk/archive/scripts/src/phase2/pmFlatField.c	(revision 5461)
+++ /trunk/archive/scripts/src/phase2/pmFlatField.c	(revision 5462)
@@ -18,15 +18,19 @@
  *  @author Ross Harman, MHPCC
  *
- *  @version $Revision: 1.1 $ $Name: not supported by cvs2svn $
- *  @date $Date: 2005-09-23 02:58:30 $
+ *  @version $Revision: 1.2 $ $Name: not supported by cvs2svn $
+ *  @date $Date: 2005-11-03 01:30:32 $
  *
  *  Copyright 2004 Maui High Performance Computing Center, University of Hawaii
  */
 
-#include <stdio.h>
+#if HAVE_CONFIG_H
+#include <config.h>
+#endif
+
+#include<stdio.h>
+#include<math.h>
 #include <string.h>
+
 #include "pslib.h"
-
-#include "pmFPA.h"
 #include "pmFlatField.h"
 #include "pmMaskBadPixels.h"
@@ -34,6 +38,8 @@
 
 
-bool pmFlatField(pmReadout *in, const pmReadout *flat)
+bool pmFlatField(pmReadout *in, pmReadout *mask, const pmReadout *flat)
 {
+    // XXX: Not sure if this is correct.  Must consult with IfA.
+    PS_ASSERT_PTR_NON_NULL(mask, false);
     int i = 0;
     int j = 0;
@@ -67,17 +73,16 @@
                  PS_ERRORTEXT_pmFlatField_NULL_FLAT_IMAGE);
         return false;
-    } else if(in->mask == NULL) {
-        in->mask = psImageAlloc(inImage->numCols, inImage->numRows, PS_TYPE_MASK);
-        memset(in->mask->data.V[0], 0, inImage->numCols*inImage->numRows*PSELEMTYPE_SIZEOF(PS_TYPE_MASK));
     }
-    inMask = in->mask;
+    inMask = mask->image;
 
     // Check input image and its mask are not larger than flat image
-    if(inImage->numRows>flatImage->numRows || inImage->numCols>flatImage->numCols) {
+
+    if (inImage->numRows>flatImage->numRows || inImage->numCols>flatImage->numCols) {
         psError( PS_ERR_BAD_PARAMETER_SIZE, true,
                  PS_ERRORTEXT_pmFlatField_SIZE_INPUT_IMAGE,
                  inImage->numRows, inImage->numCols, flatImage->numRows, flatImage->numCols);
         return false;
-    } else if(inMask->numRows>flatImage->numRows || inMask->numCols > flatImage->numCols) {
+    }
+    if (inMask->numRows > flatImage->numRows || inMask->numCols > flatImage->numCols) {
         psError( PS_ERR_BAD_PARAMETER_SIZE, true,
                  PS_ERRORTEXT_pmFlatField_SIZE_MASK_IMAGE,
Index: /trunk/archive/scripts/src/phase2/pmFlatField.h
===================================================================
--- /trunk/archive/scripts/src/phase2/pmFlatField.h	(revision 5461)
+++ /trunk/archive/scripts/src/phase2/pmFlatField.h	(revision 5462)
@@ -18,9 +18,13 @@
  *  @author Ross Harman, MHPCC
  *
- *  @version $Revision: 1.1 $ $Name: not supported by cvs2svn $
- *  @date $Date: 2005-09-23 02:58:30 $
+ *  @version $Revision: 1.2 $ $Name: not supported by cvs2svn $
+ *  @date $Date: 2005-11-03 01:30:32 $
  *
  *  Copyright 2004 Maui High Performance Computing Center, University of Hawaii
  */
+
+#include "pslib.h"
+#include "pmFPA.h" // #include "pmAstrometry.h"
+
 
 /** Execute flat field module.
@@ -31,9 +35,7 @@
  *  @return  bool: True or false for success or failure
  */
-
-#include "pmFPA.h"
-
 bool pmFlatField(
-    pmReadout *in,          ///< Redout with input image and mask
+    pmReadout *in,          ///< Readout with input image
+    pmReadout *mask,        ///< Input image mask
     const pmReadout *flat   ///< Readout with flat image
 );
Index: /trunk/archive/scripts/src/phase2/pmFlatFieldErrors.h
===================================================================
--- /trunk/archive/scripts/src/phase2/pmFlatFieldErrors.h	(revision 5461)
+++ /trunk/archive/scripts/src/phase2/pmFlatFieldErrors.h	(revision 5462)
@@ -7,6 +7,6 @@
  *  @author Ross Harman, MHPCC
  *
- *  @version $Revision: 1.1 $ $Name: not supported by cvs2svn $
- *  @date $Date: 2005-09-23 02:59:05 $
+ *  @version $Revision: 1.2 $ $Name: not supported by cvs2svn $
+ *  @date $Date: 2005-11-03 01:30:32 $
  *
  *  Copyright 2004 Maui High Performance Computing Center, University of Hawaii
Index: /trunk/archive/scripts/src/phase2/pmSubtractBias.c
===================================================================
--- /trunk/archive/scripts/src/phase2/pmSubtractBias.c	(revision 5461)
+++ /trunk/archive/scripts/src/phase2/pmSubtractBias.c	(revision 5462)
@@ -6,6 +6,6 @@
  *  @author GLG, MHPCC
  *
- *  @version $Revision: 1.1 $ $Name: not supported by cvs2svn $
- *  @date $Date: 2005-09-23 02:58:30 $
+ *  @version $Revision: 1.2 $ $Name: not supported by cvs2svn $
+ *  @date $Date: 2005-11-03 01:30:32 $
  *
  *  Copyright 2004 Maui High Performance Computing Center, University of Hawaii
@@ -22,9 +22,38 @@
 #define PM_SUBTRACT_BIAS_SPLINE_ORDER 3
 
+// XXX: put these in psConstants.h
+void PS_POLY1D_PRINT(psPolynomial1D *poly)
+{
+    printf("-------------- PS_POLY1D_PRINT() --------------\n");
+    printf("poly->nX is %d\n", poly->nX);
+    for (psS32 i = 0 ; i < (1 + poly->nX) ; i++) {
+        printf("poly->coeff[%d] is %f\n", i, poly->coeff[i]);
+    }
+}
+
+void PS_PRINT_SPLINE(psSpline1D *mySpline)
+{
+    printf("-------------- PS_PRINT_SPLINE() --------------\n");
+    printf("mySpline->n is %d\n", mySpline->n);
+    for (psS32 i = 0 ; i < mySpline->n ; i++) {
+        PS_POLY1D_PRINT(mySpline->spline[i]);
+    }
+    PS_VECTOR_PRINT_F32(mySpline->knots);
+}
+
+#define PS_IMAGE_PRINT_F32_HIDEF(NAME) \
+printf("======== printing %s ========\n", #NAME); \
+for (int i = 0 ; i < (NAME)->numRows ; i++) { \
+    for (int j = 0 ; j < (NAME)->numCols ; j++) { \
+        printf("%.5f ", (NAME)->data.F32[i][j]); \
+    } \
+    printf("\n"); \
+}\
+
 /******************************************************************************
 psSubtractFrame(): this routine will take as input a readout for the input
 image and a readout for the bias image.  The bias image is subtracted in
 place from the input image.
- *****************************************************************************/
+*****************************************************************************/
 static pmReadout *SubtractFrame(pmReadout *in,
                                 const pmReadout *bias)
@@ -143,4 +172,6 @@
     return(opt);
 }
+
+
 
 /******************************************************************************
@@ -161,4 +192,5 @@
     psTrace(".psModule.pmSubtracBias.ScaleOverscanVector", 4,
             "---- ScaleOverscanVector() begin (%d -> %d) ----\n", overscanVector->n, n);
+    //    PS_VECTOR_PRINT_F32(overscanVector);
 
     if (NULL == overscanVector) {
@@ -182,5 +214,5 @@
     psF32 x;
     psS32 i;
-
+    printf("Got here\n");
     if (fit == PM_FIT_POLYNOMIAL) {
         // Fit a polynomial to the old overscan vector.
@@ -204,4 +236,5 @@
         // Fit a spline to the old overscan vector.
         mySpline = (psSpline1D *) fitSpec;
+        // XXX: Does it make any sense to have a psSpline argument?
         if (mySpline == NULL) {
             mustFreeSpline = 1;
@@ -210,12 +243,14 @@
         //
         // NOTE: Since the X arg in the psVectorFitSpline1D() function is NULL,
-        // splines enpoints will be from 0.0 to overscanVector->n-1.  Must scale
+        // splines endpoints will be from 0.0 to overscanVector->n-1.  Must scale
         // properly when doing the spline eval.
         //
-        mySpline = psVectorFitSpline1D(mySpline, NULL, overscanVector, NULL);
+        //        mySpline = psVectorFitSpline1D(mySpline, NULL, overscanVector, NULL);
+        mySpline = psVectorFitSpline1D(NULL, overscanVector);
         if (mySpline == NULL) {
             psError(PS_ERR_UNKNOWN, false, "ScaleOverscanVector()(2): Could not fit a spline to the psVector.\n");
             return(NULL);
         }
+        //        PS_PRINT_SPLINE(mySpline);
 
         // For each element of the new vector, convert the x-ordinate to that
@@ -230,4 +265,6 @@
             psFree(mySpline);
         }
+        //        PS_VECTOR_PRINT_F32(newVec);
+
 
     } else {
@@ -376,4 +413,5 @@
     tmpOverscan = (psListElem *) overscans->head;
     while (NULL != tmpOverscan) {
+        //        PS_IMAGE_PRINT_F32_HIDEF(in->image);
         myOverscanImage = (psImage *) tmpOverscan->data;
 
@@ -557,6 +595,8 @@
                 }
             } else if (fit == PM_FIT_SPLINE) {
+                // XXX: This makes no sense
+                // XXX: must free mySpline?
                 mySpline = (psSpline1D *) fitSpec;
-                mySpline = psVectorFitSpline1D(mySpline, NULL, overscanVector, NULL);
+                mySpline = psVectorFitSpline1D(NULL, overscanVector);
                 if (mySpline == NULL) {
                     psError(PS_ERR_UNKNOWN, false, "Could not fit a spline to overscan vector.  Returning in image.\n");
Index: /trunk/archive/scripts/src/phase2/pmSubtractBias.h
===================================================================
--- /trunk/archive/scripts/src/phase2/pmSubtractBias.h	(revision 5461)
+++ /trunk/archive/scripts/src/phase2/pmSubtractBias.h	(revision 5462)
@@ -6,6 +6,6 @@
  *  @author GLG, MHPCC
  *
- *  @version $Revision: 1.1 $ $Name: not supported by cvs2svn $
- *  @date $Date: 2005-09-23 02:58:30 $
+ *  @version $Revision: 1.2 $ $Name: not supported by cvs2svn $
+ *  @date $Date: 2005-11-03 01:30:32 $
  *
  *  Copyright 2004 Maui High Performance Computing Center, University of Hawaii
@@ -23,5 +23,6 @@
 #include<math.h>
 #include "pslib.h"
-#include "pmFPA.h"
+
+#include "pmFPA.h"   //#include "pmAstrometry.h"
 
 typedef enum {
Index: /trunk/archive/scripts/src/phase2/psAdditionals.c
===================================================================
--- /trunk/archive/scripts/src/phase2/psAdditionals.c	(revision 5461)
+++ /trunk/archive/scripts/src/phase2/psAdditionals.c	(revision 5462)
@@ -15,10 +15,10 @@
 	    psError(PS_ERR_IO, true, "Couldn't find %s in the metadata.\n");
 	}
-    } else if (item->type != PS_META_META) {
+    } else if (item->type != PS_DATA_METADATA) {
 	// The value at the key isn't metadata
 	if (status) {
 	    *status = false;
 	} else {
-	    psLogMsg(__func__, PS_LOG_WARN, "%s isn't of type PS_META_META, as expected.\n");
+	    psLogMsg(__func__, PS_LOG_WARN, "%s isn't of type PS_DATA_METADATA, as expected.\n");
 	}
 	value = NULL;
@@ -45,10 +45,10 @@
 	    psError(PS_ERR_IO, true, "Couldn't find %s in the metadata.\n");
 	}
-    } else if (item->type != PS_META_STR) {
+    } else if (item->type != PS_DATA_STRING) {
 	// The value at the key isn't of the desired type
 	if (status) {
 	    *status = false;
 	} else {
-	    psLogMsg(__func__, PS_LOG_WARN, "%s isn't of type PS_META_STR, as expected.\n");
+	    psLogMsg(__func__, PS_LOG_WARN, "%s isn't of type PS_DATA_STRING, as expected.\n");
 	}
 	value = NULL;
@@ -79,8 +79,8 @@
 	printf(": ");
 	switch (item->type) {
-	  case PS_META_STR:
+	  case PS_DATA_STRING:
 	    printf("%s", item->data.V);
 	    break;
-	  case PS_META_BOOL:
+	  case PS_DATA_BOOL:
 	    if (item->data.B) {
 		printf("True");
@@ -89,14 +89,14 @@
 	    }
 	    break;
-	  case PS_META_S32:
+	  case PS_DATA_S32:
 	    printf("%d", item->data.S32);
 	    break;
-	  case PS_META_F32:
+	  case PS_DATA_F32:
 	    printf("%f", item->data.F32);
 	    break;
-	  case PS_META_F64:
+	  case PS_DATA_F64:
 	    printf("%f", item->data.F64);
 	    break;
-	  case PS_META_META:
+	  case PS_DATA_METADATA:
 	    printf("\n");
 	    psMetadataPrint(item->data.V, level + 1);
@@ -200,18 +200,18 @@
     switch(item->type) {
 	// Only doing a representative set of types
-      case PS_META_S32:
+      case PS_DATA_S32:
 	newItem = psMetadataItemAlloc(item->name, item->type, item->comment, atoi(argv[argnum]));
 	psArgumentRemove(argnum, argc, argv);
 	break;
-      case PS_META_F32:
+      case PS_DATA_F32:
 	newItem = psMetadataItemAlloc(item->name, item->type, item->comment, atof(argv[argnum]));
 	psArgumentRemove(argnum, argc, argv);
 	break;
-      case PS_META_BOOL:
+      case PS_DATA_BOOL:
 	// Turn option on; no optional argument to remove
 	newItem = psMetadataItemAlloc(item->name, item->type, item->comment, true);
 	break;
 	// XXX: Include the other numerical types
-      case PS_META_STR:
+      case PS_DATA_STRING:
 	{
 	    //psString string = psStringCopy(argv[argnum]);	// Get the argument into PS memory management
@@ -250,6 +250,6 @@
 	if (argItem) {
 	    psArgumentRemove(i, argc, argv); // Remove the switch
-	    if (argItem->type != PS_META_MULTI) {
-		if (argItem->type != PS_META_BOOL && *argc < i + 1) {
+	    if (argItem->type != PS_DATA_METADATA_MULTI) {
+		if (argItem->type != PS_DATA_BOOL && *argc < i + 1) {
 		    psError(PS_ERR_IO, true, "Required argument for %s is missing.\n", argItem->name);
 		    // XXX: Cleanup before returning
@@ -318,19 +318,19 @@
 	switch (oldItem->type) {
 	    // Only doing a representative set of types
-	  case PS_META_S32:
+	  case PS_DATA_S32:
 	    oldItem->data.S32 = newItem->data.S32;
 	    break;
-	  case PS_META_F32:
+	  case PS_DATA_F32:
 	    oldItem->data.F32 = newItem->data.F32;
 	    break;
-	  case PS_META_BOOL:
+	  case PS_DATA_BOOL:
 	    oldItem->data.B = newItem->data.B;
 	    break;
 	    // XXX: Include the other numerical types
-	  case PS_META_STR:
+	  case PS_DATA_STRING:
 	    psFree(oldItem->data.V);
 	    oldItem->data.V = psMemIncrRefCounter(newItem->data.V);
 	    break;
-	  case PS_META_MULTI:
+	  case PS_DATA_METADATA_MULTI:
 	    {
 		psList *newMulti = psMemIncrRefCounter(newItem->data.V);	// The new list of MULTI
@@ -347,12 +347,12 @@
 		    switch (oldMultiItem->type) {
 			// Only doing a representative set of types
-		      case PS_META_S32:
+		      case PS_DATA_S32:
 			oldItem->data.S32 = newItem->data.S32;
 			break;
-		      case PS_META_F32:
+		      case PS_DATA_F32:
 			oldItem->data.F32 = newItem->data.F32;
 			break;
 			// XXX: Include the other numerical types
-		      case PS_META_STR:
+		      case PS_DATA_STRING:
 			psFree(oldItem->data.V);
 			oldItem->data.V = psMemIncrRefCounter(newItem->data.V);
@@ -385,15 +385,15 @@
     switch (arg->type) {
 	// Only doing a representative set of types
-      case PS_META_S32:
+      case PS_DATA_S32:
 	return arg->data.S32 >= 0 ? (int)log10f((float)arg->data.S32) + 1 :
 	    (int)log10f(-(float)arg->data.S32) + 2;
 	// XXX: Other numerical types
-      case PS_META_F32:
+      case PS_DATA_F32:
 	return arg->data.F32 >= 0 ? 12 : 13; // -d.dddddde?dd
-      case PS_META_F64:
+      case PS_DATA_F64:
 	return arg->data.F64 >= 0 ? 12 : 13; // -d.dddddde?dd
-      case PS_META_BOOL:
+      case PS_DATA_BOOL:
 	return arg->data.B ? 4 : 5;
-      case PS_META_STR:
+      case PS_DATA_STRING:
 	return strlen(arg->data.V);
       default:
@@ -450,15 +450,15 @@
 	switch (argItem->type) {
 	    // Only doing a representative set of types
-	  case PS_META_S32:
+	  case PS_DATA_S32:
 	    printf("%d", argItem->data.S32);
 	    break;
 	    // XXX: Other numerical types
-	  case PS_META_F32:
+	  case PS_DATA_F32:
 	    printf("%.6e", argItem->data.F32);
 	    break;
-	  case PS_META_F64:
+	  case PS_DATA_F64:
 	    printf("%.6e", argItem->data.F64);
 	    break;
-	  case PS_META_BOOL:
+	  case PS_DATA_BOOL:
 	    if (argItem->data.B) {
 		printf("TRUE");
@@ -467,5 +467,5 @@
 	    }
 	    break;
-	  case PS_META_STR:
+	  case PS_DATA_STRING:
 	    printf("%s", argItem->data.V);
 	    break;
