Index: trunk/psastro/src/psastroDataSave.c
===================================================================
--- trunk/psastro/src/psastroDataSave.c	(revision 6792)
+++ trunk/psastro/src/psastroDataSave.c	(revision 7014)
@@ -1,40 +1,47 @@
 # include "psastro.h"
+// XXX leak free 2006.04.27
 
-// load the data from the files in this loop.
-// we write out the result in a second loop
-// at the end of this function, the complete stellar data is loaded
-// into the correct fpa structure locations (readout.analysis:PSPHOT.SOURCES)
+// this loop saves the photometry/astrometry data files
 bool psastroDataSave (pmConfig *config) {
 
-  // define the output files:
-    pmFPAfile *input = psMetadataLookupPtr (&status, config->files, "PSASTRO.INPUT");
-    if (!status) {
-	psErrorStackPrint(stderr, "Can't find input data!\n");
+    pmChip *chip;
+    pmCell *cell;
+    pmReadout *readout;
+
+    // select the current recipe
+    psMetadata *recipe  = psMetadataLookupPtr (NULL, config->recipes, "PSASTRO");
+    if (!recipe) {
+	psErrorStackPrint(stderr, "Can't find PSASTRO recipe!\n");
 	exit(EXIT_FAILURE);
     }
 
-    // this is the output target
-    pmFPAfileDefine (config->files, format, input, "PSASTRO.OUTPUT");
+    // select the output data sources
+    pmFPAfile *output = psMetadataLookupPtr (NULL, config->files, "PSASTRO.OUTPUT");
+    if (!output) {
+	psErrorStackPrint(stderr, "Can't find output data!\n");
+	exit(EXIT_FAILURE);
+    }
 
-    char *output = psMetadataLookupPtr(&status, config->arguments, "OUTPUT");
-    pmFPAfileAddOutputName (config->files, "OUTPUT", output, PM_FPA_MODE_WRITE);
+    // de-activate all files except PSASTRO.OUTPUT
+    pmFPAfileActivate (config->files, false, NULL);
+    pmFPAfileActivate (config->files, true, "PSASTRO.OUTPUT");
 
     pmFPAview *view = pmFPAviewAlloc (0);
 
-    // files associated with the science image
+    // open/load files as needed
     pmFPAfileIOChecks (config->files, view, PM_FPA_BEFORE);
 
-    while ((chip = pmFPAviewNextChip (view, input->fpa, 1)) != NULL) {
-        psLogMsg ("psphot", 4, "Chip %d: %x %x\n", view->chip, chip->file_exists, chip->process);
+    while ((chip = pmFPAviewNextChip (view, output->fpa, 1)) != NULL) {
+        psTrace (__func__, 4, "Chip %d: %x %x\n", view->chip, chip->file_exists, chip->process);
         if (!chip->process || !chip->file_exists) { continue; }
 	pmFPAfileIOChecks (config->files, view, PM_FPA_BEFORE);
 
-	while ((cell = pmFPAviewNextCell (view, input->fpa, 1)) != NULL) {
-            psLogMsg ("psphot", 4, "Cell %d: %x %x\n", view->cell, cell->file_exists, cell->process);
+	while ((cell = pmFPAviewNextCell (view, output->fpa, 1)) != NULL) {
+            psTrace (__func__, 4, "Cell %d: %x %x\n", view->cell, cell->file_exists, cell->process);
             if (!cell->process || !cell->file_exists) { continue; }
 	    pmFPAfileIOChecks (config->files, view, PM_FPA_BEFORE);
 
 	    // process each of the readouts
-	    while ((readout = pmFPAviewNextReadout (view, input->fpa, 1)) != NULL) {
+	    while ((readout = pmFPAviewNextReadout (view, output->fpa, 1)) != NULL) {
 		pmFPAfileIOChecks (config->files, view, PM_FPA_BEFORE);
 		if (! readout->data_exists) { continue; }
@@ -47,4 +54,6 @@
     }
     pmFPAfileIOChecks (config->files, view, PM_FPA_AFTER);
+
+    psFree (view);
     return true;
 }
