Index: trunk/archive/scripts/src/phase2/pmFPARead.c
===================================================================
--- trunk/archive/scripts/src/phase2/pmFPARead.c	(revision 5105)
+++ trunk/archive/scripts/src/phase2/pmFPARead.c	(revision 5371)
@@ -17,9 +17,9 @@
 
 // Read a FITS extension into a chip
-static bool readExtension(p_pmHDU *hdu,	// HDU to read
+static bool readExtension(pmPixelData *pixelData, // Pixel data into which to read
 			  psFits *fits	// The FITS file from which to read
     )
 {
-    const char *extName = hdu->extname;	// Extension name
+    const char *extName = pixelData->extname; // Extension name
 
     psTrace(__func__, 7, "Moving to extension %s...\n", extName);
@@ -74,4 +74,13 @@
 	if (image->type.type != PS_TYPE_F32) {
 	    pixels->data[i] = psImageCopy(NULL, image, PS_TYPE_F32);
+
+#ifndef PRODUCTION
+	    // XXX: Temporary fix for writing images, until psFits gets cleaned up
+	    psMetadataItem *bitpixItem = psMetadataLookup(header, "BITPIX");
+	    bitpixItem->data.S32 = -32;
+	    psMetadataRemove(header, 0, "BZERO");
+	    psMetadataRemove(header, 0, "BSCALE");
+#endif
+
 	    psFree(image);
 	} else {
@@ -86,58 +95,32 @@
 
     // Update the HDU with the new data
-    hdu->pixels = pixels;
-    hdu->header = header;
+    pixelData->images = pixels;
+    pixelData->header = header;
+
+    // XXX: Insert mask and weight reading here
 
     return true;
 }
 
-
 // Portion out an image into the cell
-static bool portionCell(pmCell *cell,	// The cell that gets its bits
-			psArray *images // Array of images from which the readouts are assigned
+static bool generateReadouts(pmCell *cell, // The cell that gets its bits
+			     pmPixelData *data // Pixel data, containing image, mask, weights
     )
 {
-    bool mdStatus = false;		// Status of MD lookup
-
-    // Get the trim and bias sections
-    psMetadataItem *mdItem = psMetadataLookup(cell->concepts, "CELL.TRIMSEC");
-    psRegion *trimRegion = mdItem->data.V;
-    mdItem = psMetadataLookup(cell->concepts, "CELL.BIASSEC");
-    psList *biasRegionList = mdItem->data.V;
-
+    psArray *images = data->images;	// Array of images (each of which is a readout)
+    psArray *masks = data->masks;	// Array of masks (one for each readout)
     // Iterate over each of the image planes
     for (int i = 0; i < images->n; i++) {
 	psImage *image = images->data[i]; // The i-th plane
-
-	// Convert from FITS standard to PS standard
-	// We don't touch the x1 and y1 values because they aren't included by psImageSubset.
-	//trimRegion->x0 -= 1;
-	//trimRegion->y0 -= 1;
-
-	psImage *trimImage = psImageSubset(image, *trimRegion); // Image from trim section
-
-	psList *biasImages = psListAlloc(NULL);	// List of images from bias sections
-	psListIterator *biasRegionIter = psListIteratorAlloc(biasRegionList, PS_LIST_HEAD, true); // Iterator
-	psRegion *biasRegion = NULL;	// Bias region from list
-	while (biasRegion = psListGetAndIncrement(biasRegionIter)) {
-	    // Convert from FITS standard to PS standard
-	    // We don't touch the x1 and y1 values because they aren't included by psImageSubset.
-	    //biasRegion->x0 -= 1;
-	    //biasRegion->y0 -= 1;
-	    
-	    psImage *biasImage = psImageSubset(image, *biasRegion); // Image from bias section
-	    psListAdd(biasImages, PS_LIST_TAIL, biasImage);
-	    psFree(biasImage);
-	}
-	psFree(biasRegionIter);
-
-	pmReadout *readout = pmReadoutAlloc(cell, trimImage, biasImages, 0,0,0,0,1,1);
+	psImage *mask = NULL;		// The mask
+	if (masks) {
+	    mask = masks->data[i];
+	}
+
+	pmReadout *readout = pmReadoutAlloc(cell, image, mask, 0,0,0,0,1,1);
 	psFree(readout);		// This seems silly, but the alloc registers it with the cell, so we
 					// don't have to do anything with it.  Perhaps we should change
 					// pmReadoutAlloc to pmReadoutAdd?
-	psFree(trimImage);
-	psFree(biasImages);
-    }
-    psFree(biasRegionList);
+    }
 
     return true;
@@ -154,5 +137,5 @@
     )
 {
-    p_pmHDU *hdu = NULL;		// Current HDU from FITS file
+    pmPixelData *pixelData = NULL;	// Pixel data from FITS file
 
     // Read the PHU, if required
@@ -169,9 +152,9 @@
     // Read in....
     psTrace(__func__, 1, "Working on FPA...\n");
-    if (fpa->private) {
-	hdu = fpa->private;
-	psTrace(__func__, 3, "Reading pixels from extension %s into FPA.\n", hdu->extname);
-	if (! readExtension(hdu, fits)) {
-	    psError(PS_ERR_IO, false, "Unable to read pixels from extension %s\n", hdu->extname);
+    if (fpa->data) {
+	pixelData= fpa->data;
+	psTrace(__func__, 3, "Reading pixels from extension %s into FPA.\n", pixelData->extname);
+	if (! readExtension(pixelData, fits)) {
+	    psError(PS_ERR_IO, false, "Unable to read pixels from extension %s\n", pixelData->extname);
 	    return false;
 	}
@@ -191,9 +174,9 @@
 	psTrace(__func__, 2, "Reading in chip %d...\n", i);
 
-	if (chip->private) {
-	    hdu = chip->private;
-	    psTrace(__func__, 3, "Reading pixels from extension %s into chip %d.\n", hdu->extname, i);
-	    if (! readExtension(hdu, fits)) {
-		psError(PS_ERR_IO, false, "Unable to read pixels from extension %s\n", hdu->extname);
+	if (chip->data) {
+	    pixelData = chip->data;
+	    psTrace(__func__, 3, "Reading pixels from extension %s into chip %d.\n", pixelData->extname, i);
+	    if (! readExtension(pixelData, fits)) {
+		psError(PS_ERR_IO, false, "Unable to read pixels from extension %s\n", pixelData->extname);
 		return false;
 	    }
@@ -213,9 +196,11 @@
 	    psTrace(__func__, 3, "Reading in cell %d...\n", j);
 
-	    if (cell->private) {
-		hdu = cell->private;
-		psTrace(__func__, 5, "Reading pixels from extension %s into cell %d.\n", hdu->extname, j);
-		if (! readExtension(hdu, fits)) {
-		    psError(PS_ERR_IO, false, "Unable to read pixels from extension %s\n", hdu->extname);
+	    if (cell->data) {
+		pixelData = cell->data;
+		psTrace(__func__, 5, "Reading pixels from extension %s into cell %d.\n", pixelData->extname,
+			j);
+		if (! readExtension(pixelData, fits)) {
+		    psError(PS_ERR_IO, false, "Unable to read pixels from extension %s\n",
+			    pixelData->extname);
 		    return false;
 		}
@@ -225,5 +210,5 @@
 	    psTrace(__func__, 5, "Allocating readouts for chip %d cell %d...\n",
 		    i, j);
-	    portionCell(cell, hdu->pixels);
+	    generateReadouts(cell, pixelData);
 	}
     }
