Index: trunk/psphot/src/psphotParseCamera.c
===================================================================
--- trunk/psphot/src/psphotParseCamera.c	(revision 6727)
+++ trunk/psphot/src/psphotParseCamera.c	(revision 6851)
@@ -4,86 +4,10 @@
 
     bool status;
-    psFits *fits = NULL;
-    psMetadata *phu = NULL;
-    psMetadata *format = NULL;
 
-    // psphot is supplied with a list of input images (may be only one image)
-    psArray *infiles = psMetadataLookupPtr(&status, config->arguments, "INPUT");
-    if (!status) psAbort (__func__, "missing INPUT entry");
-    if (infiles->n < 1) psAbort (__func__, "empty input list");
-
-    // if no camera has been specified, use the first image as a template for the rest.
-    if (config->camera == NULL) {
-	fits = psFitsOpen (infiles->data[0], "r");
-	phu = psFitsReadHeader (NULL, fits);
-	format = pmConfigCameraFormatFromHeader (config, phu);
-	psFitsClose (fits);
+    status = false;
+    pmFPAfile *input = pmFPAfileFromArgs (&status, config, "PSPHOT.INPUT", "INPUT");
+    if (!status) {
+	psAbort (__func__, "missing INPUT entry");
     }
-    // There's no point in continuing if we can't recognize what we've got
-    if (! config->camera) {
-	psErrorStackPrint(stderr, "Can't find camera configuration!\n");
-	exit(EXIT_FAILURE);
-    }
-
-    // files
-    config->files = psMetadataAlloc ();
-
-    // build the template fpa, set up the basic view
-    // XXX : 216 leaks in pmFPAConstruct / psFree (input)
-    pmFPA *input = pmFPAConstruct (config->camera);
-
-    // assign the I/O files (potentially) needed by psphot
-    // the output file is just a view to the file on config->files 
-    pmFPAfile *file = pmFPAfileDefine (config->files, format, input, "PSPHOT.INPUT");
-    if (!file) {
-	exit(EXIT_FAILURE);
-    }
-
-    for (int i = 0; i < infiles->n; i++) {
-	if (phu == NULL) {
-	    fits = psFitsOpen (infiles->data[i], "r");
-	    phu = psFitsReadHeader (NULL, fits);
-	    pmConfigValidateCameraFormat (format, phu);
-	    psFitsClose (fits);
-	}
-
-	// set the view to the corresponding entry for this phu
-	pmFPAview *view = pmFPAAddSource (input, phu, format);
-
-	// XXX is this the correct psMD to save the filename?
-	char *name = pmFPAfileNameFromRule (file->filextra, file, view);
-	psMetadataAddStr (file->names, PS_LIST_TAIL, name, 0, "", infiles->data[i]);
-
-	psFree (view);
-	psFree (name);
-	psFree (phu);
-	phu = NULL;
-    }
-
-    pmFPAfileDefine (config->files, format, input, "PSPHOT.OUTPUT");
-    pmFPAfileDefine (config->files, format, input, "PSPHOT.RESID");
-
-    // pmFPAfileDefine (config->files, format, input, "PSPHOT.PSF_INPUT");
-    // pmFPAfileDefine (config->files, format, input, "PSPHOT.PSF_OUTPUT");
-
-    // build the template fpa, set up the basic view
-    // supply the backgnd with a different camera?
-    // allow alternate format?
-    // pmFPAfileConstruct (config->files, format, config->camera, "PSPHOT.BACKSUB");
-    // pmFPAfileConstruct (config->files, format, config->camera, "PSPHOT.BACKGND");
-    // pmFPAfileConstruct (config->files, format, config->camera, "PSPHOT.BACKMDL");
-    // pmFPAfileConstruct (config->files, format, config->camera, "PSPHOT.PSF_SAMPLE");
-
-    // psphot is supplied with the output name
-    // this needs to be better: supply to all output (WRITE) files?
-    char *output = psMetadataLookupPtr(&status, config->arguments, "OUTPUT");
-
-    file = psMetadataLookupPtr (&status, config->files, "PSPHOT.OUTPUT");
-    if (!status) psAbort (__func__, "missing OUTPUT entry");
-    psMetadataAddStr (file->names, PS_LIST_TAIL, "OUTPUT", 0, "", output);
-
-    file = psMetadataLookupPtr (&status, config->files, "PSPHOT.RESID");
-    if (!status) psAbort (__func__, "missing OUTPUT entry");
-    psMetadataAddStr (file->names, PS_LIST_TAIL, "OUTPUT", 0, "", output);
 
     // recipe override values (command-line options):
@@ -96,4 +20,27 @@
     }
     psFree (iter);
+
+    // these calls bind the I/O handle to the specified fpa
+    pmFPAfileDefine (config->files, config->camera, input->fpa, "PSPHOT.OUTPUT");
+    pmFPAfileDefine (config->files, config->camera, input->fpa, "PSPHOT.RESID");
+
+    // pmFPAfileDefine (config->files, format, input, "PSPHOT.PSF_INPUT");
+    // pmFPAfileDefine (config->files, format, input, "PSPHOT.PSF_OUTPUT");
+
+    // these calls construct a new fpa for the I/O handle 
+    int DX = psMetadataLookupS32 (&status, recipe, "BACKGROUND_XBIN");
+    if (!status) {DX = 64;}
+    int DY = psMetadataLookupS32 (&status, recipe, "BACKGROUND_YBIN");
+    if (!status) {DY = 64;}
+    pmFPAfileFromFPA (config, input->fpa, DX, DY, "PSPHOT.BACKMDL");
+    pmFPAfileFromFPA (config, input->fpa,  1,  1, "PSPHOT.BACKGND");
+    pmFPAfileFromFPA (config, input->fpa,  1,  1, "PSPHOT.BACKSUB");
+
+    // pmFPAfileConstruct (config->files, format, config->camera, "PSPHOT.PSF_SAMPLE");
+
+    // psphot is supplied with the output name
+    // this needs to be better: supply to all output (WRITE) files?
+    char *output = psMetadataLookupPtr(&status, config->arguments, "OUTPUT");
+    pmFPAfileAddFileNames (config->files, "OUTPUT", output, PM_FPA_MODE_WRITE);
 
     // set default recipe values here
@@ -129,7 +76,4 @@
     # endif
 
-    psFree (input);
-    psFree (format);
-
     psTrace(__func__, 1, "Done with psphotParseCamera...\n");
     return true;
