Index: trunk/archive/scripts/phase2.c
===================================================================
--- trunk/archive/scripts/phase2.c	(revision 2519)
+++ trunk/archive/scripts/phase2.c	(revision 2520)
@@ -17,23 +17,12 @@
   outroot = argv[2];
 
-  // define command-line options
-  cameraname = NULL;
-  cameraname = getargs ("-camera");  // pseudo-code, not a valid API
-  recipefile = NULL;
-  recipefile = getargs ("-recipe");  // pseudo-code, not a valid API
-
-  // base file needs to be hardwired, from environment, or based on program name
-  base   = psMetadataParseConfig (NULL, basefile, FALSE);  
-
-  // load site-specific information (such as database servers, etc)
-  sitefile = psMetadataLookup (base, "SITE");
-  site   = psMetadataParseConfig (NULL, sitefile, FALSE);  
-
-  // load only the headers from the given file input a list of headers
-  headers = psFITSReadHeaderSet (input);
+  fits = psFitsAlloc (input);
+
+  // load only the headers from the given file
+  headers = psFitsReadHeaderSet (fits);
  
   // identify camera (may be specified on command line)
   if (cameraname == NULL) {
-    cameraname = pmCameraFromHeader (headers.data[0]);
+    cameraname = pmCameraFromHeader (psListGet (headers->list, 0));
   }
   camerafile = psMetadataLookup (site, "CAMERA.%s", cameraname);
@@ -63,10 +52,21 @@
   // Is EXTNAME invarient?  (probably not; does not always exist)
 
+  /* This will work, but it is not efficient: 
+     we should work with the extensions in sequence */
   for (i = 0; i < FPA[0].chips.n; i++) {
     chip = FPA[0].chips.data[i];
 
+    if (Base == CELL) {
+      f = psFitsMoveExtName(f, extname);
+    }
+    
     for (j = 0; j < chip[0].cells.n; j++) {
       cell = chip[0].cells.data[j];
 
+      if (Base == CELL) {
+	f = psFitsMoveExtName(f, extname);
+      }
+
+      /* these need to work with the psFits pointer correctly 
       bias = load_bias_image (recipe, cell[0].metadata, cell[0].extname);
       dark = load_dark_image (recipe, cell[0].metadata, cell[0].extname);
@@ -74,4 +74,5 @@
       badpix = load_badpix_image (recipe, cell[0].metadata, cell[0].extname);
       fringe = load_fringe_image (recipe, cell[0].metadata, cell[0].extname);
+      */
 
       for (k = 0; k < cell[0].readouts.n; k++) {
@@ -79,5 +80,5 @@
 
 	// load the specific readout pixels.  put this in a function?  
-	readout[0].image = psImageReadSection (NULL, 0, 0, 0, 0, 0, cell[0].extname, k, input);
+	status = pmReadoutLoad (fits, cell, k);
 
 	kernel = make_kernel ();
@@ -299,2 +300,65 @@
 }
 
+
+bool pmConfigLoadBasic (psMetadata **site, psMetadata **base) {
+
+  // base file needs to be hardwired, from environment, or based on program name
+  base   = psMetadataParseConfig (NULL, basefile, FALSE);  
+
+  // load site-specific information (such as database servers, etc)
+  sitefile = psMetadataLookup (base, "SITE");
+  site   = psMetadataParseConfig (NULL, sitefile, FALSE);  
+
+}
+
+bool pmConfigLoadCamera (psMetadata **camera, psMetadata *site, psMetadata *header) {
+  
+  if (site == NULL) return (FALSE);
+
+  // identify camera (may be specified on command line)
+  cameraname = getargs ("-camera");  // pseudo-code, not a valid API
+
+  // if there is no source for camera info, fail
+  if ((cameraname == NULL) && (header == NULL)) {
+    return (FALSE);
+  }
+
+  // try to determine camera from header 
+  if (cameraname == NULL) {
+    cameraname = pmCameraFromHeader (header);
+  }
+
+  // find appropriate camera definition file
+  camerafile = psMetadataLookup (site, "CAMERA.%s", cameraname);
+
+  // load the camera details for the specific camera
+  *camera = psMetadataParseConfig (NULL, camerafile, FALSE);
+
+  return (TRUE);
+}
+
+bool pmConfigLoadRecipe (psMetadata **recipe, psMetadata *camera, char *script) {
+
+  char recipename[64];
+
+  if (site   == NULL) return (FALSE);
+  if (camera == NULL) return (FALSE);
+
+  // define command-line options
+  recipefile = getargs ("-recipe");  // pseudo-code, not a valid API
+
+  // recipefile is defined in the camera metadata, if not specified on command line
+  if (recipefile == NULL) {
+    sprintf (recipename, "RECIPE.%s", script);
+
+    // option 1: recipe is in the camera definition 
+    recipefile = psMetadataLookup (camera, recipename);
+
+    // option 2: recipe is in the metadata database (equiv to a detrend file)
+    // this option requires a different API (header data, etc)
+    recipefile = DetrendDatabaseLookup (recipename, site, camera, header);
+  }
+
+  *recipe = psMetadataParseConfig (NULL, recipefile, FALSE);  
+  return (TRUE);
+}
