Index: trunk/archive/scripts/src/papFocalPlane.c
===================================================================
--- trunk/archive/scripts/src/papFocalPlane.c	(revision 4092)
+++ trunk/archive/scripts/src/papFocalPlane.c	(revision 4309)
@@ -1,6 +1,8 @@
 #include <stdio.h>
+#include <string.h>
 #include "pslib.h"
 #include "papFocalPlane.h"
-#include "pmRead.h"
+#include "psAdditionals.h"
+#include "pmFPARead.h"
 
 // I'm sure this code, especially the DB lookup, leaks memory something chronic....
@@ -8,7 +10,7 @@
 
 // Look for a value already cached
-static psMetadataItem *getValueFromCache(const psFPA *fpa, // The FPA that contains the chip
-					 const psChip *chip, // The chip that contains the cell
-					 const psCell *cell, // The cell
+static psMetadataItem *getValueFromCache(const papFPA *fpa, // The FPA that contains the chip
+					 const papChip *chip, // The chip that contains the cell
+					 const papCell *cell, // The cell
 					 const char *valueName // Name of value
     )
@@ -31,7 +33,7 @@
 
 // Put a value that we've found into the cache
-static void setValueInCache(psFPA *fpa, // The FPA that contains the chip
-			    psChip *chip, // The chip that contains the cell
-			    psCell *cell, // The cell
+static void setValueInCache(papFPA *fpa, // The FPA that contains the chip
+			    papChip *chip, // The chip that contains the cell
+			    papCell *cell, // The cell
 			    const char *valueName, // Name of value
 			    const psMetadataItem *value	// Item containing the value
@@ -56,12 +58,14 @@
 
 
-static psMetadataItem *getValueFromHeader(const psFPA *fpa, // The FPA that contains the chip
-					  const psChip *chip, // The chip that contains the cell
-					  const psCell *cell, // The cell
+static psMetadataItem *getValueFromHeader(papFPA *fpa, // The FPA that contains the chip
+					  papChip *chip, // The chip that contains the cell
+					  papCell *cell, // The cell
 					  const char *valueName // Name of value
     )
 {
+    bool mdStatus = true;		// Status of MD lookup
+
     // Look for how to translate the concept into a FITS header name
-    const char *header = psMetadataLookupString(fpa->fits, valueName);
+    const char *header = psMetadataLookupString(&mdStatus, fpa->fits, valueName);
     if (strlen(header) > 0) {
 	// We have a FITS header to look up --- search each level
@@ -70,5 +74,5 @@
 	    if (cellItem) {
 		// XXX: Need to clean up before returning
-		setValueInCache(NULL, NULL, cell, cellItem);
+		setValueInCache(NULL, NULL, cell, valueName, cellItem);
 		return cellItem;
 	    }
@@ -79,5 +83,5 @@
 	    if (chipItem) {
 		// XXX: Need to clean up before returning
-		setValueInCache(NULL, chip, cell, chipItem);
+		setValueInCache(NULL, chip, cell, valueName, chipItem);
 		return chipItem;
 	    }
@@ -88,5 +92,5 @@
 	    if (fpaItem) {
 		// XXX: Need to clean up before returning
-		setValueInCache(fpa, chip, cell, fpaItem);
+		setValueInCache(fpa, chip, cell, valueName, fpaItem);
 		return fpaItem;
 	    }
@@ -100,13 +104,13 @@
 
 // Look for a default
-static psMetadataItem *getValueFromDefault(const psFPA *fpa, // The FPA that contains the chip
-					   const psChip *chip, // The chip that contains the cell
-					   const psCell *cell, // The cell
+static psMetadataItem *getValueFromDefault(papFPA *fpa, // The FPA that contains the chip
+					   papChip *chip, // The chip that contains the cell
+					   papCell *cell, // The cell
 					   const char *valueName // Name of value
     )
 {
-    psMetadataItem *defItem = psMetadataLookup(fpa->defaults, valueName);
+    psMetadataItem *defItem = psMetadataLookup((psMetadata*)fpa->defaults, valueName);
     if (defItem) {
-	setValueInCache(fpa, chip, cell, defItem);
+	setValueInCache(fpa, chip, cell, valueName, defItem);
     }
     return defItem;			// defItem is either NULL or points to what was desired
@@ -115,7 +119,7 @@
 
 // Look for a database lookup
-static psMetadataItem *getValueFromDB(const psFPA *fpa, // The FPA that contains the chip
-				      const psChip *chip, // The chip that contains the cell
-				      const psCell *cell, // The cell
+static psMetadataItem *getValueFromDB(papFPA *fpa, // The FPA that contains the chip
+				      papChip *chip, // The chip that contains the cell
+				      papCell *cell, // The cell
 				      const char *valueName // Name of value
     )
@@ -123,11 +127,11 @@
     if (fpa->db) {
 	// The database has been initialised
-
-	psMetadata *dbLookup = psMetadataLookupMD(fpa->database, valueName);
+	bool mdStatus = true;		// Status of MD lookup
+	psMetadata *dbLookup = psMetadataLookupMD(&mdStatus, fpa->database, valueName);
 	if (dbLookup) {
-	    const char *tableName = psMetadataLookupString(dbLookup, "TABLE"); // Name of the table
-	    const char *colName = psMetadataLookupString(dbLookup, "COLUMN"); // Name of the column
-	    const char *givenCols = psMetadataLookupString(dbLookup, "GIVENDBCOL");	// Name of "where" columns
-	    const char *givenPS = psMetadataLookupString(dbLookup, "GIVENPS"); // Values for "where" columns
+	    const char *tableName = psMetadataLookupString(&mdStatus, dbLookup, "TABLE"); // Name of the table
+	    const char *colName = psMetadataLookupString(&mdStatus, dbLookup, "COLUMN"); // Name of the column
+	    const char *givenCols = psMetadataLookupString(&mdStatus, dbLookup, "GIVENDBCOL");	// Name of "where" columns
+	    const char *givenPS = psMetadataLookupString(&mdStatus, dbLookup, "GIVENPS"); // Values for "where" columns
 	    
 	    // Now, need to get the "given"s
@@ -135,5 +139,5 @@
 		psList *cols = papSplit(givenCols, ",;"); // List of column names
 		psList *values = papSplit(givenPS, ",;"); // List of value names for the columns
-		psMetadata *selection = psMetadataAlloc(NULL); // The stuff to select in the DB
+		psMetadata *selection = psMetadataAlloc(); // The stuff to select in the DB
 		if (cols->size != values->size) {
 		    psLogMsg(__func__, PS_LOG_WARN, "The GIVENDBCOL and GIVENPS entries for %s do not have "
@@ -159,5 +163,5 @@
 			    }
 			    if (! item) {
-				psLogMsg(__func__, PS_LOG_ERR, "Unable to find the value name %s for DB "
+				psLogMsg(__func__, PS_LOG_ERROR, "Unable to find the value name %s for DB "
 					 " lookup on %s --- ignored.\n", name, valueName);
 			    } else {
@@ -199,5 +203,5 @@
 }
 
-psMetadataItem *psCellGetValue(const psFPA *fpa, // The FPA that contains the cell
+psMetadataItem *psCellGetValue(papFPA *fpa, // The FPA that contains the cell
 			       const char *chipName, // The name of the chip
 			       const char *cellName, // The name of the cell
@@ -205,6 +209,7 @@
 			       )
 {
-    psChip *chip = psMetadataLookupChip(fpa, chipName);	// The chip
-    psCell *cell = psMetadataLookupCell(chip, cellName); // The cell
+    bool mdStatus = true;		// Status of MD lookup
+    papChip *chip = psMetadataLookupChip(&mdStatus, fpa->chips, chipName); // The chip
+    papCell *cell = psMetadataLookupCell(&mdStatus, chip->cells, cellName); // The cell
 
     // Try cache, headers, database, defaults in order
@@ -223,5 +228,5 @@
 }
 
-float psCellGetValueF32(const psFPA *fpa, // The FPA that contains the cell
+float psCellGetValueF32(papFPA *fpa, // The FPA that contains the cell
 			const char *chipName, // The name of the chip
 			const char *cellName, // The name of the cell
@@ -244,5 +249,5 @@
 }
 
-int psCellGetValueS32(const psFPA *fpa, // The FPA that contains the cell
+int psCellGetValueS32(papFPA *fpa, // The FPA that contains the cell
 		      const char *chipName, // The name of the chip
 		      const char *cellName, // The name of the cell
@@ -250,5 +255,5 @@
     )
 {
-    int value = NAN;			// Result
+    int value = 0;			// Result
     psMetadataItem *item = psCellGetValue(fpa, chipName, cellName, valueName); // The item
     if (! item) {
@@ -266,5 +271,5 @@
 
 
-double psCellGetValueF64(const psFPA *fpa, // The FPA that contains the cell
+double psCellGetValueF64(papFPA *fpa, // The FPA that contains the cell
 			 const char *chipName, // The name of the chip
 			 const char *cellName, // The name of the cell
@@ -288,5 +293,5 @@
 
 
-psString psCellGetValueString(const psFPA *fpa, // The FPA that contains the cell
+psString psCellGetValueString(papFPA *fpa, // The FPA that contains the cell
 			      const char *chipName, // The name of the chip
 			      const char *cellName, // The name of the cell
@@ -308,2 +313,136 @@
     return value;
 }
+
+//////////////////////////////////////////////////////////////////////////////////////////////////////////////
+// Allocators
+//////////////////////////////////////////////////////////////////////////////////////////////////////////////
+
+papFPA *papFPAAlloc(psMetadata *fits, // FITS translation info
+		    psMetadata *database, // Database lookup info
+		    psMetadata *defaults, // Defaults info
+		    psDB *db		// Database handle
+    )
+{
+    papFPA *fpa = psAlloc(sizeof(papFPA));// The FPA
+    psMemSetDeallocator(fpa, (psFreeFcn)p_papFPAFree);
+
+    // Fill in the various components
+    fpa->fits = psMemIncrRefCounter(fits);
+    fpa->database = psMemIncrRefCounter(database);
+    fpa->defaults = psMemIncrRefCounter(defaults);
+
+    fpa->values = psMetadataAlloc();
+    fpa->header = NULL;
+    fpa->db = psMemIncrRefCounter(db);
+
+    fpa->chips = psMetadataAlloc();
+    fpa->images = NULL;
+
+    return fpa;
+}
+
+void p_papFPAFree(papFPA *fpa)
+{
+    psFree((psPtr)fpa->fits);
+    psFree((psPtr)fpa->database);
+    psFree((psPtr)fpa->defaults);
+
+    psFree(fpa->values);
+    psFree(fpa->header);
+    if (fpa->db) {
+	psDBCleanup((psDB*)fpa->db);
+    }
+
+    psFree(fpa->chips);
+    psFree(fpa->images);
+}
+
+papChip *papChipAlloc(papFPA *fpa,		// FPA to which the chip belongs
+		    const char *name	// Name of the chip
+    )
+{
+    papChip *chip = psAlloc(sizeof(papChip)); // The chip
+    psMemSetDeallocator(chip, (psFreeFcn)p_papChipFree);
+    psMetadataAdd(fpa->chips, PS_LIST_TAIL, name, PS_META_CHIP, "Added on allocation of chip", chip);
+
+    chip->values = psMetadataAlloc();
+    chip->header = NULL;
+
+    chip->cells = psMetadataAlloc();
+    chip->images = NULL;
+
+    return chip;
+}
+
+void p_papChipFree(papChip *chip)
+{
+    psFree(chip->values);
+    psFree(chip->header);
+    psFree(chip->cells);
+    psFree(chip->images);
+}
+
+papCell *papCellAlloc(papChip *chip,	// Chip to which the cell belongs
+		    const char *name,	// Name of the cell
+		    int nReadouts	// Number of readouts contained
+    )
+{
+    papCell *cell = psAlloc(sizeof(papCell)); // The cell
+    psMemSetDeallocator(cell, (psFreeFcn)p_papCellFree);
+    psMetadataAdd(chip->cells, PS_LIST_TAIL, name, PS_META_CELL, "Added on allocation of cell", cell);
+
+    cell->values = psMetadataAlloc();
+    cell->header = NULL;
+
+    cell->readouts = psArrayAlloc(nReadouts);
+    cell->images = NULL;
+
+    return cell;
+}
+
+void p_papCellFree(papCell *cell)
+{
+    psFree(cell->values);
+    psFree(cell->header);
+    psFree(cell->readouts);
+    psFree(cell->images);
+}
+
+papReadout *papReadoutAlloc(papCell *cell, // Cell to which the readout belongs
+			    int readoutNum, // Number of the readout
+			    psImage *image, // The pixels
+			    psList *overscans, // The overscan images
+			    int col0, int row0, int colParity, int rowParity, int colBin, int rowBin // Data
+    )
+{
+    papReadout *readout = psAlloc(sizeof(papReadout));
+    psMemSetDeallocator(readout, (psFreeFcn)p_papReadoutFree);
+    psArray *readouts = cell->readouts;
+    if (readoutNum >= readouts->nalloc) {
+	readouts = psArrayRealloc(readouts, readoutNum);
+	cell->readouts = readouts;
+    }
+    readouts->data[readoutNum] = readout;
+    
+    // Set the components
+    readout->image = image;
+    readout->overscans = overscans;
+    
+    readout->values = psMetadataAlloc();
+    
+    *(int*)&readout->col0 = col0;
+    *(int*)&readout->row0 = row0;
+    *(int*)&readout->colParity = colParity;
+    *(int*)&readout->rowParity = rowParity;
+    *(int*)&readout->colBins = colBin;
+    *(int*)&readout->rowBins = rowBin;
+
+    return readout;
+}
+
+void p_papReadoutFree(papReadout *readout)
+{
+    psFree(readout->image);
+    psFree(readout->overscans);
+    psFree(readout->values);
+}
