Index: trunk/archive/scripts/src/phase2/papPhase2.c
===================================================================
--- trunk/archive/scripts/src/phase2/papPhase2.c	(revision 4793)
+++ trunk/archive/scripts/src/phase2/papPhase2.c	(revision 4820)
@@ -1,5 +1,13 @@
 #include <stdio.h>
+#include <string.h>
+
 #include "pslib.h"
 #include "psAdditionals.h"
+
+#include "pmSubtractBias.h"
+
+#include "pmFPA.h"
+#include "pmConfig.h"
+#include "pmFPAConstruct.h"
 
 // Phase 2 needs to:
@@ -25,4 +33,7 @@
 // 
 // Most are self-explanatory.  "-chip" says "only work on this particular chip".
+
+
+#define RECIPE "PHASE2"			// Name of the recipe to use
 
 static psMemoryId memPrintAlloc(const psMemBlock *mb)
@@ -44,13 +55,25 @@
     return 0;
 }
+static void memPrint(const psPtr ptr)
+{
+    psMemBlock *mb = ((psMemBlock*)ptr) - 1;
+    printf("Memory block %lld (%lld):\n"
+	   "\tFile %s, line %d, size %d\n"
+	   "\tPosts: %x %x %x\n",
+	   mb->id, mb->refCounter, mb->file, mb->lineno, mb->userMemorySize, mb->startblock, mb->endblock,
+	   *(void**)((int8_t *)(mb + 1) + mb->userMemorySize));
+}
 
 int main(int argc, char *argv[])
 {
-    psMemAllocateCallbackSetID(71);
-    psMemFreeCallbackSetID(71);
+#if 0
+    // Hunting memory leaks
+    psMemAllocateCallbackSetID(3539);
+    psMemFreeCallbackSetID(3539);
     psMemAllocateCallbackSet(memPrintAlloc);
     psMemFreeCallbackSet(memPrintFree);
-
-    psTraceSetLevel("psArgumentParse", 10);
+#endif
+
+//    psTraceSetLevel(".", 10);
 
     // Parse the configurations
@@ -58,11 +81,8 @@
     psMetadata *camera = NULL;          // Camera configuration
     psMetadata *recipe = NULL;          // Recipe configuration
-
-#if 0
-    if (! pmConfigRead(&site, &camera, &recipe, &argc, argv, "moduleName")) {
-        psErrorStackPrint("Can't find site configuration!\n");
+    if (! pmConfigRead(&site, &camera, &recipe, &argc, argv, RECIPE)) {
+        psErrorStackPrint(stderr, "Can't find site configuration!\n");
         exit(EXIT_FAILURE);
     }
-#endif
 
     // Parse other command-line arguments
@@ -73,14 +93,4 @@
     psMetadataAddStr(arguments, PS_LIST_TAIL, "-mask", "Name of the mask image", "");
     psMetadataAddS32(arguments, PS_LIST_TAIL, "-chip", "Chip number to process (if positive)", -1);
-
-    psMetadataAdd(arguments, PS_LIST_TAIL, "-string", PS_META_STR, "Test string", "SomeString");
-    psMetadataAdd(arguments, PS_LIST_TAIL, "-bool", PS_META_BOOL, "Test bool", false);
-#if 1
-    psMetadataAdd(arguments, PS_LIST_TAIL, "-int", PS_META_S32 | PS_META_DUPLICATE_OK, "Test integer 1", 1);
-    psMetadataAdd(arguments, PS_LIST_TAIL, "-int", PS_META_S32 | PS_META_DUPLICATE_OK, "Test integer 2", 2);
-    psMetadataAdd(arguments, PS_LIST_TAIL, "-int", PS_META_S32 | PS_META_DUPLICATE_OK, "Test integer 3", 3);
-#endif
-    psMetadataAdd(arguments, PS_LIST_TAIL, "-float", PS_META_F32, "Test float", 1.234567);
-
     if (! psArgumentParse(arguments, &argc, argv) || argc != 3) {
 	printf("\nPan-STARRS Phase 2 processing\n\n");
@@ -92,7 +102,4 @@
     const char *inputName = argv[1];	// Name of input image
     const char *outputName = argv[2];	// Name of output image
-
-    printf("Success: %s %s\n", inputName, outputName);
-
     const char *biasName = psMetadataLookupString(NULL, arguments, "-bias"); // Name of bias image
     const char *darkName = psMetadataLookupString(NULL, arguments, "-dark"); // Name of dark image
@@ -100,4 +107,5 @@
     const char *maskName = psMetadataLookupString(NULL, arguments, "-mask"); // Name of mask image
     const int chipNum = psMetadataLookupS32(NULL, arguments, "-chip"); // Chip number to work on
+    printf("Input: %s\nOutput: %s\n", inputName, outputName);
     printf("Bias: %s\n", biasName);
     printf("Dark: %s\n", darkName);
@@ -105,57 +113,49 @@
     printf("Mask: %s\n", maskName);
     printf("Chip: %d\n", chipNum);
-
-    // For testing
-    psString string = psMetadataLookupString(NULL, arguments, "-string");
-    float floating = psMetadataLookupF32(NULL, arguments, "-float");
-    bool mdok = false;
-    bool boolean = psMetadataLookupBool(&mdok, arguments, "-bool");
-    if (!mdok) printf("Urgh!\n");
-    printf("String: %s\n", string);
-    printf("Float: %f\n", floating);
-    printf("Boolean: %d\n", boolean);
+    psFree(arguments);
+
+    // Open the input
+#ifdef PRODUCTION
+    psFits *inputFile = psFitsOpen(inputName, "r"); // File handle for FITS file
+#else
+    psFits *inputFile = psFitsAlloc(inputName);	// File handle for FITS file
+#endif
+    if (! inputFile) {
+	psErrorStackPrint(stderr, "Can't open input image: %s\n", inputName);
+        exit(EXIT_FAILURE);
+    }
+    psMetadata *header = psFitsReadHeader(NULL, inputFile); // FITS header
+#if PRODUCTION
+    psDB *database = pmConfigDB(site);	// Database handle
+#else
+    psDB *database = NULL;		// Database handle
+#endif
 
 #if 0
-    psMetadataItem *intItem = psMetadataLookup(arguments, "-int");
-    if (intItem->type == PS_META_MULTI) {
-	psList *intMulti = intItem->data.V;
-	psListIterator *intIter = psListIteratorAlloc(intMulti, PS_LIST_HEAD, false);
-	psMetadataItem *intMultiItem = NULL;
-	while (intMultiItem = psListGetAndIncrement(intIter)) {
-	    printf("Integer: %d\n", intMultiItem->data.S32);
-	}
-	psFree(intIter);
-    }
-#endif
-
-
-
-
-
-#if 0
-    // Open the input
-    psFits *inputFile = psFitsOpen(inputName, "r"); // File handle for FITS file
-    if (! inputFile) {
-	psErrorStackPrint("Can't open input image: %s\n", inputName);
-        exit(EXIT_FAILURE);
-    }
-    psMetadata *header = psFitsReadHeader(NULL, inputFile); // FITS header
-    psDB *database = pmConfigDB(site);	// Database handle
-
     // Open the output and output mask
     // We do it here so that we don't process the whole lot and then find out we can't open the output file
+#ifdef PRODUCTION
     psFits *outputFile = psFitsOpen(outputName, "w");
+#else
+    psFits *outputFile = psFitsAlloc(outputName);
+#endif
     if (! outputFile) {
-	psErrorStackPrint("Can't open output image: %s\n", outputName);
+	psErrorStackPrint(stderr, "Can't open output image: %s\n", outputName);
 	exit(EXIT_FAILURE);
     }
     psString outputMaskName = psStringCopy(outputName);
-    (void)psStringAppend(outputMaskName, ".mask");
-    psFits *outputMaskFile = psFits(outputMaskName, "w");
+    (void)psStringAppend(&outputMaskName, ".mask");
+#ifdef PRODUCTION
+    psFits *outputMaskFile = psFitsOpen(outputMaskName, "w");
+#else
+    psFits *outputMaskFile = psFitsAlloc(outputMaskName);
+#endif
     if (! outputMaskFile) {
-	psErrorStackPrint("Can't open output mask image: %s\n", outputMaskName);
+	psErrorStackPrint(stderr, "Can't open output mask image: %s\n", outputMaskName);
 	exit(EXIT_FAILURE);
     }
     psFree(outputMaskName);
+#endif
+
 	
     // Get camera configuration from header if not already defined
@@ -163,22 +163,22 @@
 	camera = pmConfigCameraFromHeader(site, header);
 	if (! camera) {
-	    psErrorStackPrint("Can't find camera configuration!\n");
-	    exit(EXIT_FAILURE);
-	}
-    } else if (! pmConfigValidateCamera(camera, inputHeader)) {
-	psError("phase2", true, "%s does not seem to be from the camera.\n", inputName);
+	    psErrorStackPrint(stderr, "Can't find camera configuration!\n");
+	    exit(EXIT_FAILURE);
+	}
+   } else if (! pmConfigValidateCamera(camera, header)) {
+	psError(PS_ERR_IO, true, "%s does not seem to be from the camera.\n", inputName);
 	exit(EXIT_FAILURE);
     }
-    if (! recipe && !(recipe = pmConfigRecipeFromCamera(camera, "moduleName"))) {
-        psErrorStackPrint("Can't find recipe configuration!\n");
+    if (! recipe && !(recipe = pmConfigRecipeFromCamera(camera, RECIPE))) {
+        psErrorStackPrint(stderr, "Can't find recipe configuration!\n");
         exit(EXIT_FAILURE);
     }
 
     // Construct camera in preparation for reading
-    pmFPA *input = pmFPAConstruct(camera, db);
-    pmFPA *mask = pmFPAConstruct(camera, db);
-    pmFPA *bias = pmFPAConstruct(camera, db);
-    pmFPA *dark = pmFPAConstruct(camera, db);
-    pmFPA *flat = pmFPAConstruct(camera, db);
+    pmFPA *input = pmFPAConstruct(camera, database);
+    pmFPA *mask = pmFPAConstruct(camera, database);
+    pmFPA *bias = pmFPAConstruct(camera, database);
+    pmFPA *dark = pmFPAConstruct(camera, database);
+    pmFPA *flat = pmFPAConstruct(camera, database);
 
     // Set various tasks
@@ -236,5 +236,4 @@
 		     " assuming NONE.\n", mode);
 	}
-	psFree(mode);
 	psString fit = psMetadataLookupString(NULL, recipe, "OVERSCAN.FIT");
 	if (strcasecmp(fit, "POLYNOMIAL") == 0) {
@@ -246,8 +245,7 @@
 		     " assuming NONE.\n", fit);
 	}
-	psFree(fit);
 	overscanBins = psMetadataLookupS32(NULL, recipe, "OVERSCAN.BIN");
 	if (overscanBins <= 0) {
-	    psErrorStackPrint("OVERSCAN.BIN (%d) is non-positive --- assuming 1.\n", overscanBins);
+	    psErrorStackPrint(stderr, "OVERSCAN.BIN (%d) is non-positive --- assuming 1.\n", overscanBins);
 	    overscanBins = 1;
 	}
@@ -258,5 +256,5 @@
 	    overscanStats = psStatsAlloc(PS_STAT_SAMPLE_MEDIAN);
 	} else {
-	    psErrorStackPrint("OVERSCAN.STAT (%s) is not one of MEAN, MEDIAN: assuming MEAN\n", stat);
+	    psErrorStackPrint(stderr, "OVERSCAN.STAT (%s) is not one of MEAN, MEDIAN: assuming MEAN\n", stat);
 	    overscanStats = psStatsAlloc(PS_STAT_SAMPLE_MEAN);	    
 	}
@@ -270,5 +268,6 @@
 	}
     }
-	 
+
+#if 0
     // Chip selection
     if (chipNum >= 0) {
@@ -276,5 +275,5 @@
 	    ! pmFPASelectChip(dark, chipNum) || ! pmFPASelectChip(flat, chipNum) ||
 	    ! pmFPASelectChip(mask, chipNum)) {
-	    psErrorStackPrint("Chip number %d doesn't exist in camera.\n", chipNum);
+	    psErrorStackPrint(stderr, "Chip number %d doesn't exist in camera.\n", chipNum);
 	    exit(EXIT_FAILURE);
 	}
@@ -284,8 +283,13 @@
     // Read in the input pixels
     if (! pmFPARead(input, inputFile)) {
-	psErrorStackPrint("Unable to populate camera from FITS file: %s\n", inputName);
+	psErrorStackPrint(stderr, "Unable to populate camera from FITS file: %s\n", inputName);
 	exit(EXIT_FAILURE);
     }
+#ifdef PRODUCTION
     psFitsClose(inputFile);
+#else
+    psFree(inputFile);
+#endif
+
 
     // Load the calibration frames, if required
@@ -299,5 +303,5 @@
 	psFree(biasHeader);
 	if (! pmFPARead(bias, biasFile)) {
-	    psErrorStackPrint("Unable to populate bias camera from fits FITS: %s\n", biasName);
+	    psErrorStackPrint(stderr, "Unable to populate bias camera from fits FITS: %s\n", biasName);
 	    exit(EXIT_FAILURE);
 	}
@@ -314,5 +318,5 @@
 	psFree(darkHeader);
 	if (! pmFPARead(dark, darkFile)) {
-	    psErrorStackPrint("Unable to populate dark camera from fits FITS: %s\n", darkName);
+	    psErrorStackPrint(stderr, "Unable to populate dark camera from fits FITS: %s\n", darkName);
 	    exit(EXIT_FAILURE);
 	}
@@ -329,5 +333,5 @@
 	psFree(maskHeader);
 	if (! pmFPARead(mask, maskFile)) {
-	    psErrorStackPrint("Unable to populate mask camera from fits FITS: %s\n", maskName);
+	    psErrorStackPrint(stderr, "Unable to populate mask camera from fits FITS: %s\n", maskName);
 	    exit(EXIT_FAILURE);
 	}
@@ -344,5 +348,5 @@
 	psFree(flatHeader);
 	if (! pmFPARead(flat, flatFile)) {
-	    psErrorStackPrint("Unable to populate flat camera from fits FITS: %s\n", flatName);
+	    psErrorStackPrint(stderr, "Unable to populate flat camera from fits FITS: %s\n", flatName);
 	    exit(EXIT_FAILURE);
 	}
@@ -414,5 +418,5 @@
 		darkTime = pmReadoutGetDarkTime(darkReadout);
 		if (darkTime <= 0.0) {
-		    psErrorStackPrint("DARKTIME for dark image (%f) is non-positive.\n", darkTime);
+		    psErrorStackPrint(stderr, "DARKTIME for dark image (%f) is non-positive.\n", darkTime);
 		    exit(EXIT_FAILURE);
 		}
@@ -469,6 +473,6 @@
 			    psLookupTable *table = psLookupTableAlloc(name, "%f %f", 0);
 			    if (psLookupTableRead(table) <= 0) {
-				psErrorStackPrint("Unable to read non-linearity correction file %s --- "
-						  "ignored\n", tableName);
+				psErrorStackPrint(stderr, "Unable to read non-linearity correction file %s "
+						  "--- ignored\n", tableName);
 			    } else {
 				(void)pmNonLinearityLookup(inputReadout, table);
@@ -523,6 +527,6 @@
 					psLookupTable *table = psLookupTableAlloc(tableName, "%f %f", 0);
 					if ((int numLines = psLookupTableRead(table)) <= 0) {
-					    psErrorStackPrint("Unable to read non-linearity correction "
-							      "file %s --- ignored\n", tableName);
+					    psErrorStackPrint(stderr, "Unable to read non-linearity "
+							      "correction file %s --- ignored\n", tableName);
 					} else {
 					    (void)pmNonLinearityLookup(inputReadout, table);
@@ -553,5 +557,6 @@
 		    float inputTime = pmReadoutGetDarkTime(inputReadout); // Dark time for input image
 		    if (inputTime <= 0) {
-			psErrorStackPrint("DARKTIME for input image (%f) is non-positive.\n", inputTime);
+			psErrorStackPrint(stderr, "DARKTIME for input image (%f) is non-positive.\n",
+					  inputTime);
 			exit(EXIT_FAILURE);
 		    }
@@ -583,5 +588,6 @@
 			overscanMode = PM_OVERSCAN_COLUMNS;
 		    } else {
-			psErrorStackPrint("CELL.READDIR (%d) is not 1 or 2 --- assuming 1.\n", readdir);
+			psErrorStackPrint(stderr, "CELL.READDIR (%d) is not 1 or 2 --- assuming 1.\n",
+					  readdir);
 			overscanMode = PM_OVERSCAN_ROWS;
 		    }
@@ -655,7 +661,18 @@
     pmFPAWrite(outputFile, input);
     pmFPAWriteMask(outputMaskFile, input);
+#ifdef PRODUCTION
     psFitsClose(outputFile);
     psFitsClose(outputMaskFile);
-
+#else
+    psFree(outputFile);
+    psFree(outputMaskFile);
+#endif
+
+#endif
+
+    psFree(site);
+    psFree(header);
+    psFree(camera);
+    psFree(recipe);
     psFree(input);
     psFree(mask);
@@ -663,18 +680,21 @@
     psFree(dark);
     psFree(flat);
-
-#endif
-
-    
+    psFree(overscanStats);
+    psFree(inputFile);
+
+#if 1
     psMemCheckCorruption(true);
-    psFree(arguments);
     psMemBlock **leaks = NULL;          // List of leaks
     int nLeaks = psMemCheckLeaks(0, &leaks, NULL, false); // Number of leaks
     printf("%d leaks found.\n", nLeaks);
+#if 1
     for (int i = 0; i < nLeaks; i++) {
-        printf("Memory leak detection: memBlock %lld\n"
+        printf("Memory leak detection: memBlock %lld (%lld)\n"
 	       "\tFile %s, line %d, size %d\n",
-	       leaks[i]->id, leaks[i]->file, leaks[i]->lineno, leaks[i]->userMemorySize);
-    }
+	       leaks[i]->id, leaks[i]->refCounter, leaks[i]->file, leaks[i]->lineno,
+	       leaks[i]->userMemorySize);
+    }
+#endif
+#endif
 
 }
