Index: /trunk/psastro/src/psastro.c
===================================================================
--- /trunk/psastro/src/psastro.c	(revision 6791)
+++ /trunk/psastro/src/psastro.c	(revision 6792)
@@ -16,17 +16,30 @@
     psastroDataLoop (input, config);
 
-    // simple layout interpretation, basic WCS conversion
-    pmFPA *fpa = psastroBuildFPA (header, rawstars);
+    // select astrometry stars from the psphot sources
+    // limit fit to bright stars only 
+    psastroSelectAstromStars (config);
 
-    // limit fit to bright stars only 
-    psastroSelectBrightStars (fpa, config);
+    // load the reference stars overlapping the data stars
+    psastroLoadReference (config);
 
     // fpa and subset point to the same astrometry terms
-    psastroChipAstrom (fpa, config);
+    switch (mode) {
+      case stack:
+	psastroStackAstrom (config);
+	break
+      case mosaic:
+	psastroMosaicAstrom (config);
+	break
+      case mosaic:
+	psastroMosaicAstrom (config);
+	break;
+      default:
+	break;
+    }
 
-    // write out data (cmp file)
-    psastroWriteCMP (fpa, argv[3]);
+    // perform the astrometric solution
+    psastroDataSave (config);
 
-    psLogMsg ("psphot", 3, "complete psphot run: %f sec\n", psTimerMark ("complete"));
+    psLogMsg ("psphot", 3, "complete psastro run: %f sec\n", psTimerMark ("complete"));
 
     psFree (config);
Index: /trunk/psastro/src/psastroArguments.c
===================================================================
--- /trunk/psastro/src/psastroArguments.c	(revision 6791)
+++ /trunk/psastro/src/psastroArguments.c	(revision 6792)
@@ -35,54 +35,6 @@
     psMetadataAddPtr (config->arguments, PS_LIST_TAIL, "PSASTRO.OPTIONS",  PS_DATA_METADATA, "", options);
 
-    // we load all input files onto a psArray, to be parsed later
-    psArray *input = psArrayAlloc (16);
-    input->n = 0;
-
-    // load the list of filenames the supplied file (may be a glob: "file*.fits")
-    if ((N = psArgumentGet (*argc, argv, "-file"))) {
-	glob_t globList;
-	psArgumentRemove (N, argc, argv);
-	globList.gl_offs = 0;
-	glob (argv[N], 0, NULL, &globList);
-	for (int i = 0; i < globList.gl_pathc; i++) {
-	    char *filename = psStringCopy (globList.gl_pathv[i]);
-	    psArrayAdd (input, 16, filename);
-	}
-	psArgumentRemove (N, argc, argv);
-    }
-
-    // load the list from the supplied text file
-    if ((N = psArgumentGet (*argc, argv, "-list"))) {
-	int nItems;
-	char line[1024]; // XXX limits the list lines to 1024 chars
-	char word[1024];
-	char *filename;
-
-	psArgumentRemove (N, argc, argv);
-	FILE *f = fopen (argv[N], "r");
-	if (f == NULL) {
-	    psAbort ("psphot", "unable to open specified list file");
-	}
-	while (fgets (line, 1024, f) != NULL) {
-	    nItems = sscanf (line, "%s", word);
-	    switch (nItems) {
-	      case 0:
-		break;
-	      case 1:
-		filename = psStringCopy (word);
-		psArrayAdd (input, 16, filename);
-		break;
-	      default:
-		// rigid format, no comments allowed?
-		psAbort ("psphot", "error parsing input list file");
-		break;
-	    }
-	}
-	psArgumentRemove (N, argc, argv);
-    }		
-    if (input->n < 1) usage ();
-
-    // input list gets places as an array on the config->arguements list
-    psMetadataAddPtr (config->arguments, PS_LIST_TAIL, "INPUT",  PS_DATA_ARRAY, "", input);
+    status = pmConfigFileSetsMD (config->arguments, "INPUT", "-file", "-list");
+    if (!status) { usage ();}
 
     if (*argc != 2) usage ();
Index: /trunk/psastro/src/psastroDataLoop.c
===================================================================
--- /trunk/psastro/src/psastroDataLoop.c	(revision 6791)
+++ /trunk/psastro/src/psastroDataLoop.c	(revision 6792)
@@ -1,64 +1,43 @@
 # include "psastro.h"
 
-bool psastroDataLoop (ppFile *file, ppConfig *config) {
+// 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)
+bool psastroDataLoop (pmConfig *config) {
 
-    bool status;
-    ppImageLoadDepth imageLoadDepth;
-
-    // determine the load depth
-    const char *depth = psMetadataLookupStr(&status, config->recipe, "LOAD.DEPTH");
-    if (! status || ! depth || strlen(depth) == 0) {
-        psLogMsg("psastro", PS_LOG_ERROR, "LOAD.DEPTH not specified in recipe.");
-        exit(EXIT_FAILURE);
-    }
-    imageLoadDepth = PP_LOAD_NONE;
-    if (!strcasecmp(depth, "FPA")) imageLoadDepth = PP_LOAD_FPA;
-    if (!strcasecmp(depth, "CHIP")) imageLoadDepth = PP_LOAD_CHIP;
-    if (!strcasecmp(depth, "CELL")) imageLoadDepth = PP_LOAD_CELL;
-    if (imageLoadDepth == PP_LOAD_NONE) {
-        psLogMsg(__func__, PS_LOG_ERROR, "LOAD.DEPTH in recipe is not FPA, CHIP or CELL.");
-        exit(EXIT_FAILURE);
+    pmFPAfile *input = psMetadataLookupPtr (&status, config->files, "PSASTRO.INPUT");
+    if (!status) {
+	psErrorStackPrint(stderr, "Can't find input data!\n");
+	exit(EXIT_FAILURE);
     }
 
-    if (imageLoadDepth == PP_LOAD_FPA) {
-        psTrace(__func__, 1, "data pixels for FPA...\n");
-        psastroLoadSMF(file, config->database, -1, -1);
+    pmFPAview *view = pmFPAviewAlloc (0);
+
+    // files associated with the science image
+    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);
+        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);
+            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) {
+		pmFPAfileIOChecks (config->files, view, PM_FPA_BEFORE);
+		if (! readout->data_exists) { continue; }
+
+		pmFPAfileIOChecks (config->files, view, PM_FPA_AFTER);
+	    }
+	    pmFPAfileIOChecks (config->files, view, PM_FPA_AFTER);
+	}
+	pmFPAfileIOChecks (config->files, view, PM_FPA_AFTER);
     }
-
-    for (int i = 0; i < file->fpa->chips->n; i++) {
-        pmChip *chip = file->fpa->chips->data[i]; // Chip of interest in input image
-
-        psLogMsg ("psastro", 4, "Chip %d: %x %x\n", i, chip->exists, chip->process);
-        if (! chip->process) { continue; }
-
-	if (imageLoadDepth == PP_LOAD_CHIP) {
-            psTrace(__func__, 1, "Loading pixels for chip %d...\n", i);
-	    psastroLoadSMF(file, config->database, i, -1);
-        }
-
-        for (int j = 0; j < chip->cells->n; j++) {
-            pmCell *cell = chip->cells->data[j]; // Cell of interest in input image
-
-            psLogMsg ("psastro", 4, "Cell %d: %x %x\n", j, cell->exists, cell->process);
-            if (! cell->process) { continue; }
-
-            if (imageLoadDepth == PP_LOAD_CELL) {
-                psTrace(__func__, 1, "Loading pixels for chip %d, cell %d...\n", i, j);
-		psastroLoadSMF(file, config->database, i, j);
-            }
-	    
-	    // process each of the readouts
-	    for (int k = 0; k < cell->readouts->n; k++) {
-		pmReadout *readout = cell->readouts->data[k]; // Readout of interest in input image
-
-		// run a single-model test if desired
-		psphotModelTest (readout, config->arguments, config->recipe);
-		psphotReadout (readout, config->recipe);
-		psphotOutput (readout, config->arguments);
-	    }
-        }
-    }
+    pmFPAfileIOChecks (config->files, view, PM_FPA_AFTER);
     return true;
 }
-
-/* this is currently written only for single-chip analysis */
Index: /trunk/psastro/src/psastroDataSave.c
===================================================================
--- /trunk/psastro/src/psastroDataSave.c	(revision 6792)
+++ /trunk/psastro/src/psastroDataSave.c	(revision 6792)
@@ -0,0 +1,50 @@
+# include "psastro.h"
+
+// 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)
+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");
+	exit(EXIT_FAILURE);
+    }
+
+    // this is the output target
+    pmFPAfileDefine (config->files, format, input, "PSASTRO.OUTPUT");
+
+    char *output = psMetadataLookupPtr(&status, config->arguments, "OUTPUT");
+    pmFPAfileAddOutputName (config->files, "OUTPUT", output, PM_FPA_MODE_WRITE);
+
+    pmFPAview *view = pmFPAviewAlloc (0);
+
+    // files associated with the science image
+    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);
+        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);
+            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) {
+		pmFPAfileIOChecks (config->files, view, PM_FPA_BEFORE);
+		if (! readout->data_exists) { continue; }
+
+		pmFPAfileIOChecks (config->files, view, PM_FPA_AFTER);
+	    }
+	    pmFPAfileIOChecks (config->files, view, PM_FPA_AFTER);
+	}
+	pmFPAfileIOChecks (config->files, view, PM_FPA_AFTER);
+    }
+    pmFPAfileIOChecks (config->files, view, PM_FPA_AFTER);
+    return true;
+}
Index: /trunk/psastro/src/psastroParseCamera.c
===================================================================
--- /trunk/psastro/src/psastroParseCamera.c	(revision 6791)
+++ /trunk/psastro/src/psastroParseCamera.c	(revision 6792)
@@ -1,52 +1,13 @@
 # include "psphot.h"
 
-ppFile *psastroParseCamera (ppConfig *config) {
+bool *psastroParseCamera (pmConfig *config) {
 
     ppFile *input = ppFileAlloc ();
 
-    input->filename = psMetadataLookupStr(NULL, config->arguments, "INPUT_FILE");
-    psMemCopy (input->filename); // keep for external use
-
-    // Open the input image
-    psLogMsg("psphot", PS_LOG_INFO, "Opening input image: %s\n", input->filename);
-    input->fits = psFitsOpen (input->filename, "r"); // File handle for FITS file
-    if (! input->fits) {
-        psErrorStackPrint(stderr, "Can't open input image: %s\n", input->filename);
-        exit(EXIT_FAILURE);
+    // the input image(s) are required arguments; they define the camera
+    input = pmFPAfileAddSource (config, "INPUT", "PSASTRO.INPUT", true);
+    if (input == NULL) {
+	psAbort (__func__, "missing INPUT entry");
     }
-    input->phu = psFitsReadHeader(NULL, input->fits); // FITS header
-
-    // Get camera configuration from header if not already defined
-    if (! config->camera) {
-        config->camera = pmConfigCameraFromHeader(config->site, input->phu);
-        if (! config->camera) {
-             // There's no point in continuing if we can't recognize what we've got
-            psErrorStackPrint(stderr, "Can't find camera configuration!\n");
-            exit(EXIT_FAILURE);
-        }
-   } else if (! pmConfigValidateCamera(config->camera, input->phu)) {
-       // There's no point in continuing if what we've got doesn't match what we've been told
-        psError(PS_ERR_IO, true, "%s does not seem to be from the specified camera.\n",
-                input->filename);
-        exit(EXIT_FAILURE);
-    }
-
-    // Determine the correct recipe to use (from camera or from user)
-    // if config->recipe is not NULL, it is a user-supplied recipe
-    if (!config->recipe) {
-	config->recipe = pmConfigRecipeFromCamera(config->camera, PSPHOT_RECIPE);
-	if (config->recipe == NULL) {
-	    psErrorStackPrint(stderr, "Can't find recipe configuration!\n");
-	    exit(EXIT_FAILURE);
-	}
-    }
-
-    // recipe override values (command-line options):
-    psMetadataItem *item = NULL;
-    psMetadataIterator *iter = psMetadataIteratorAlloc (config->options, PS_LIST_HEAD, NULL);
-    while ((item = psMetadataGetAndIncrement (iter)) != NULL) {
-	psMetadataAddItem (config->recipe, item, PS_LIST_TAIL, PS_META_REPLACE);
-    }
-    psFree (iter);
 
     // set default recipe values here
@@ -54,31 +15,5 @@
     psMetadataAddStr (recipe, PS_LIST_TAIL, "BREAK_POINT", PS_META_NO_REPLACE, "default fitting mode", "NONE");
 
-    // Chip selection: if we are using a single chip, select it for each FPA
-    // Chip numbers to work on: -chip 1,2,3,5,8,10
-    # if (0)
-    char *chips = psMetadataLookupStr(NULL, config->arguments, "CHIP_SELECTIONS"); 
-    if (chips != NULL) {
-	char *p = chips;
-	while (strlen(p)) {
-	    char *q = strchr (p, ',');
-	    if (q == NULL) { 
-		q = p + strlen(p);
-	    } else {
-		*q = 0;
-		q = q + 1;
-	    }
-	    int chipNum = atoi(p);
-	    // XXX EAM : extend this to allow an array of selected chips (by name)
-	    if (! pmFPASelectChip(input->fpa, chipNum)) {
-		psErrorStackPrint(stderr, "Chip number %d doesn't exist in camera.\n", chipNum);
-		exit(EXIT_FAILURE);
-	    }
-	    psLogMsg("psphot", PS_LOG_INFO, "Operating only on chip %d\n", chipNum);
-	    p = q;
-        }
-    }
-    # endif
-
-    psTrace(__func__, 1, "Done with psphotParseCamera...\n");
-    return input;
+    psTrace(__func__, 1, "Done with psastroParseCamera...\n");
+    return true;
 }
