Index: trunk/archive/scripts/src/pmFPARead.c
===================================================================
--- trunk/archive/scripts/src/pmFPARead.c	(revision 4309)
+++ trunk/archive/scripts/src/pmFPARead.c	(revision 4386)
@@ -12,12 +12,14 @@
 //////////////////////////////////////////////////////////////////////////////////////////////////////////////
 
-// Copy a metadata
-static psMetadata *metadataCopy(psMetadata *md)
-{
-    psMetadata *new = psMetadataAlloc();// The metadata to return
-    psMetadataIterator *mdIter = psMetadataIteratorAlloc(md, PS_LIST_HEAD, NULL); // Iterator for the MD
+// Copy metadata elements into another metadata
+static void metadataCopy(psMetadata *to, // Metadata to which to copy
+			 psMetadata *from // Metdata from which to copy
+    )
+{
+    psMetadataIterator *mdIter = psMetadataIteratorAlloc(from, PS_LIST_HEAD, NULL); // Iterator for the MD
     psMetadataItem *item = NULL;	// Item from the metadata
     while (item = psMetadataGetAndIncrement(mdIter)) {
-	if (! psMetadataAddItem(new, item, PS_LIST_TAIL, PS_META_REPLACE)) {
+	psTrace(__func__, 9, "Adding %s to metadata...\n", item->name);
+	if (! psMetadataAddItem(to, item, PS_LIST_TAIL, PS_META_REPLACE)) {
 	    psLogMsg(__func__, PS_LOG_WARN, "Unable to add item (%s: %s) to metadata: ignored\n", item->name,
 		     item->comment);
@@ -25,6 +27,4 @@
     }
     psFree(mdIter);
-
-    return new;
 }
 
@@ -46,10 +46,5 @@
 	return NULL;
     } else {
-#if 0
-	psMetadata *newCell = metadataCopy(cellData); // A copy of the cell, to be returned
-	return newCell;
-#else
-	return psMemIncrRefCounter(cellData);
-#endif
+	return cellData;
     }
 }
@@ -62,5 +57,10 @@
     )
 {
-    if (! psFitsMoveExtName(fits, extName)) {
+    if (strncmp(extName, "PHU", 3) == 0) {
+	if (!psFitsMoveExtNum(fits, 0, false)) {
+	    psError(PS_ERR_IO, false, "Unable to find PHU in FITS file!\n");
+	    return false;
+	}
+    } else if (! psFitsMoveExtName(fits, extName)) {
 	psError(PS_ERR_IO, false, "Unable to find extension %s in FITS file!\n", extName);
 	return false;
@@ -79,5 +79,5 @@
 
     int numPlanes = 1;			// Number of planes
-    if (nAxis > 3) {
+    if (nAxis == 3) {
 	numPlanes = psMetadataLookupS32(&mdStatus, *header, "NAXIS3");
 	if (!mdStatus) {
@@ -251,18 +251,11 @@
 //////////////////////////////////////////////////////////////////////////////////////////////////////////////
 
-papFPA *pmFPARead(const psMetadata *camera, // The camera configuration
-		  psFits *fits // A FITS file
-    )
-{
+papFPA *pmFPARead(psFits *fits, // A FITS file
+		  const psMetadata *camera, // The camera configuration
+		  psDB *db		// Database
+    )
+{
+    papFPA *fpa = papFPAAlloc(camera, db); // The FPA to fill out
     bool mdStatus = true;		// Status from metadata lookups
-    // Get the configuration information out of the camera config
-    psMetadata *translation = psMetadataLookupMD(&mdStatus, camera, "TRANSLATION");
-    psMetadata *database = psMetadataLookupMD(&mdStatus, camera, "DATABASE");
-    psMetadata *defaults = psMetadataLookupMD(&mdStatus, camera, "DEFAULTS");
-    psDB *dbh = NULL;
-
-    // Generate a build tree
-    papFPA *fpa = papFPAAlloc(translation, database, defaults, dbh); // The FPA to fill out
-
     const char *phuType = psMetadataLookupString(&mdStatus, camera, "PHU"); // What is the PHU?
     const char *extType = psMetadataLookupString(&mdStatus, camera, "EXTENSIONS"); // What's in the extensions?
@@ -286,6 +279,5 @@
 		const char *extName = contentItem->name; // The name of the extension
 		papChip *chip = papChipAlloc(fpa, extName); // The chip
-		readExtension(&(chip->images), &(chip->header), fits, extName);
-
+		readExtension(&(chip->pixels), &(chip->header), fits, extName);
 		if (contentItem->type != PS_META_STR) {
 		    psLogMsg(__func__, PS_LOG_WARN, "Type of content item (%x) is not string: ignored\n",
@@ -293,4 +285,5 @@
 		} else {
 		    const char *content = contentItem->data.V; // The content of the extension
+		    psTrace(__func__, 7, "Content of %s is: %s\n", extName, content);
 		    psList *cellNames = papSplit(content, " ,"); // A list of the component cells
 		    psListIterator *cellNamesIter = psListIteratorAlloc(cellNames, PS_LIST_HEAD, NULL);
@@ -298,7 +291,8 @@
 		    while (cellName = psListGetAndIncrement(cellNamesIter)) {
 			// Get the cell data
-			papCell *cell = papCellAlloc(chip, cellName, ((psArray*)chip->images)->n); // The cell
-			cell->values = getCellData(camera, cellName);
-			portionCell(cell, chip->images, chip->header);
+			papCell *cell = papCellAlloc(chip, cellName, ((psArray*)chip->pixels)->n); // The cell
+			psMetadata *cellData = getCellData(camera, cellName);
+			metadataCopy(cell->values, cellData);
+			portionCell(cell, chip->pixels, chip->header);
 		    }
 		    psFree(cellNamesIter);
@@ -309,4 +303,5 @@
 	} else if (strncmp(extType, "CELL", 4) == 0) {
 	    // Extensions are cells; Content contains a chip name and cell type
+	    psMetadata *chipNumbers = psMetadataAlloc(); // Given a chip name, holds the chip number
 	    while (contentItem = psMetadataGetAndIncrement(contentsIter)) {
 		const char *extName = contentItem->name; // The name of the extension
@@ -324,18 +319,25 @@
 			const char *chipName = psListGet(contents, 0); // The name of the chip
 			const char *cellType = psListGet(contents, 1); // The type of cell
+			psTrace(__func__, 7, "Extension is cell of type %s, from chip %s\n", cellType,
+				chipName);
 
 			papChip *chip = psMetadataLookupChip(&mdStatus, fpa->chips, chipName); // The chip
 			if (! mdStatus && ! chip) {
-			    papChip *chip = papChipAlloc(fpa, "CHIP");
+			    chip = papChipAlloc(fpa, chipName);
 			}
 			// The cell
 			psArray *images = NULL;
 			psMetadata *header = NULL;
+			psTrace(__func__, 7, "Reading extension %s\n", extName);
 			readExtension(&images, &header, fits, extName);
-			papCell *cell = papCellAlloc(chip, extName, images->n); // The cell
-			cell->images = images;
+			psTrace(__func__, 7, "Allocating cell %s\n", cellType);
+			papCell *cell = papCellAlloc(chip, cellType, images->n); // The cell
+			cell->pixels = images;
 			cell->header = header;
-			cell->values = getCellData(camera, extName);
-			portionCell(cell, cell->images, cell->header);
+			psMetadata *cellData = getCellData(camera, cellType);
+			metadataCopy(cell->values, cellData);
+			psTrace(__func__, 7, "Portioning cell....\n");
+			portionCell(cell, cell->pixels, cell->header);
+			psTrace(__func__, 7, "Done.\n");
 		    }
 		}
@@ -344,5 +346,5 @@
 	} else if (strncmp(extType, "NONE", 4) == 0) {
 	    // No extensions; Content contains metadata, each entry is a chip with its component cells
-	    readExtension(&(fpa->images), &(fpa->header), fits, "PHU");
+	    readExtension(&(fpa->pixels), &(fpa->header), fits, "PHU");
 	    while (contentItem = psMetadataGetAndIncrement(contentsIter)) {
 		const char *chipName = contentItem->name; // The name of the chip
@@ -353,12 +355,13 @@
 		} else {
 		    const char *content = contentItem->data.V; // The content of the extension
-		    papChip *chip = papChipAlloc(fpa, "CHIP"); // The chip
+		    papChip *chip = papChipAlloc(fpa, content); // The chip
 		    psList *cellNames = papSplit(content, ", "); // Split the list of cells
 		    psListIterator *cellNamesIter = psListIteratorAlloc(cellNames, PS_LIST_HEAD, false);
 		    char *cellName = NULL; // Name of the cell
 		    while (cellName = psListGetAndIncrement(cellNamesIter)) {
-			papCell *cell = papCellAlloc(chip, "CELL", ((psArray*)fpa->images)->n); // The cell
-			cell->values = getCellData(camera, cellName);
-			portionCell(cell, fpa->images, fpa->header);
+			papCell *cell = papCellAlloc(chip, cellName, ((psArray*)fpa->pixels)->n); // The cell
+			psMetadata *cellData = getCellData(camera, cellName);
+			metadataCopy(cell->values, cellData);
+			portionCell(cell, fpa->pixels, fpa->header);
 		    }
 		    psFree(cellNamesIter);
@@ -380,5 +383,5 @@
 	if (strncmp(extType, "NONE", 4) == 0) {
 	    // There are no extensions --- only the PHU
-	    readExtension(&(fpa->images), &(fpa->header), fits, "PHU");
+	    readExtension(&(fpa->pixels), &(fpa->header), fits, "PHU");
 
 	    const char *contents = psMetadataLookupString(&mdStatus, camera, "CONTENTS");
@@ -392,7 +395,8 @@
 	    const char *cellName = NULL;
 	    while (cellName = psListGetAndIncrement(cellIter)) {
-		papCell *cell = papCellAlloc(chip, cellName, ((psArray*)fpa->images)->n); // The cell
-		cell->values = getCellData(camera, cellName);
-		portionCell(cell, fpa->images, fpa->header);
+		papCell *cell = papCellAlloc(chip, cellName, ((psArray*)fpa->pixels)->n); // The cell
+		psMetadata *cellData = getCellData(camera, cellName);
+		metadataCopy(cell->values, cellData);
+		portionCell(cell, fpa->pixels, fpa->header);
 	    }
 	    psFree(cellIter);
@@ -433,8 +437,9 @@
 		    readExtension(&images, &header, fits, extName);
 		    papCell *cell = papCellAlloc(chip, extName, images->n); // The cell
-		    cell->images = images;
+		    cell->pixels = images;
 		    cell->header = header;
-		    cell->values = getCellData(camera, cellType);
-		    portionCell(cell, cell->images, cell->header);
+		    psMetadata *cellData = getCellData(camera, cellType);
+		    metadataCopy(cell->values, cellData);
+		    portionCell(cell, cell->pixels, cell->header);
 		}
 	    } // Iterating through contents
@@ -455,10 +460,15 @@
     }
 
-    
-    // Print out the build tree
+    return fpa;
+}
+
+void pmFPAPrint(papFPA *fpa		// FPA to print
+    )
+{
+    // Print out the focal plane
     psMetadataIterator *fpaIter = psMetadataIteratorAlloc(fpa->chips, PS_LIST_HEAD, NULL); // Iterator for FPA
     psMetadataItem *fpaItem = NULL;	// Item from metadata
     psTrace("pmFPARead", 0, "FPA:\n");
-    if (fpa->images) {
+    if (fpa->pixels) {
 	psTrace("pmFPARead", 1, "---> FPA contains pixels.\n");
     }
@@ -469,5 +479,5 @@
 	    psTrace("pmFPARead", 1, "Chip: %s\n", fpaItem->name);
 	    papChip *chip = fpaItem->data.V; // The chip
-	    if (chip->images) {
+	    if (chip->pixels) {
 		psTrace("pmFPARead", 2, "---> Chip contains pixels.\n");
 	    }
@@ -481,5 +491,5 @@
 		    psTrace("pmFPARead", 2, "Cell: %s\n", chipItem->name);
 		    papCell *cell = chipItem->data.V; // The cell
-		    if (cell->images) {
+		    if (cell->pixels) {
 			psTrace("pmFPARead", 3, "---> Cell contains pixels.\n");
 		    }
@@ -494,4 +504,7 @@
 			  case PS_META_F32:
 			    psTrace("pmFPARead", 3, "%s: %f\n", cellItem->name, cellItem->data.F32);
+			    break;
+			  case PS_META_S32:
+			    psTrace("pmFPARead", 3, "%s: %d\n", cellItem->name, cellItem->data.S32);
 			    break;
 			  case PS_META_META:
@@ -527,6 +540,3 @@
     } // Iterating through FPA
     psFree(fpaIter);
-
-    return fpa;
-
-}
+}
