Index: trunk/psastro/src/psastroChipAstrom.c
===================================================================
--- trunk/psastro/src/psastroChipAstrom.c	(revision 6176)
+++ trunk/psastro/src/psastroChipAstrom.c	(revision 7014)
@@ -1,73 +1,87 @@
 # include "psastro.h"
 
-// measure per-chip astrometry terms 
-// this function requires a chip from a well-formed pmFPA 
-// with PSASTRO.OBJECTS on the readouts
-bool psastroChipAstrom (pmChip *chip, psMetadata *config) {
+bool psastroChipAstrom (pmConfig *config, psArray *refs) {
 
     bool status;
-    psArray *match;
-    pmAstromStats stats;
+    pmChip *chip = NULL;
+    pmCell *cell = NULL;
+    pmReadout *readout = NULL;
 
-    // cells->n > 1 is not well-defined
-    if (chip->cells->n > 1) {
-	psLogMsg ("pmSourcesReadCMP", 3, "undefined behavior for nCells > 1");
-	return false;
+    // select the current recipe
+    psMetadata *recipe  = psMetadataLookupPtr (NULL, config->recipes, "PSASTRO");
+    if (!recipe) {
+	psErrorStackPrint(stderr, "Can't find PSASTRO recipe!\n");
+	exit(EXIT_FAILURE);
     }
-    pmCell *cell = chip->cells->data[0];
 
-    // readouts->n > 1 is not well-defined
-    if (cell->readouts->n > 1) {
-	psLogMsg ("pmSourcesReadCMP", 3, "undefined behavior for nReadouts > 1");
-	return false;
+    // select the input data sources
+    pmFPAfile *input = psMetadataLookupPtr (NULL, config->files, "PSASTRO.INPUT");
+    if (!input) {
+	psErrorStackPrint(stderr, "Can't find input data!\n");
+	exit(EXIT_FAILURE);
     }
-    pmReadout *readout = cell->readouts->data[0];
-    psMetadata *header = pmReadoutGetHeader (readout);
 
-    // load the corresponding reference data (DVO command)
-    // XXX EAM : this needs to be improved, perhaps moved to a higher level
-    psArray *refstars = psastroLoadReference (header, config);
+    double plateScale = psMetadataLookupF32 (&status, recipe, "PSASTRO.PLATE.SCALE");
+    if (!status) plateScale = 1.0;
 
-    // we require PASTRO.OBJECTS (pmAstromObj) on the analysis for this readout
-    psArray *rawstars = psMetadataLookupPtr (&status, readout->analysis, "PSASTRO.OBJECTS");
+    pmFPAview *view = pmFPAviewAlloc (0);
+    pmFPA *fpa = input->fpa;
 
-    // project the rawstars to the current best guess astrometry
-    psastroProjectRawstars (rawstars, readout);
+    while ((chip = pmFPAviewNextChip (view, 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; }
+	
+	while ((cell = pmFPAviewNextCell (view, 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; }
 
-    // use the header & config info to project refstars onto the focal plane
-    psastroProjectRefstars (refstars, readout);
+	    // process each of the readouts
+	    while ((readout = pmFPAviewNextReadout (view, fpa, 1)) != NULL) {
+		if (! readout->data_exists) { continue; }
 
-    // testWriteRaw ("ref.inp", refstars);
-    // testWriteRaw ("raw.inp", rawstars);
+		// select the raw objects for this readout
+		psArray *rawstars = psMetadataLookupPtr (NULL, readout->analysis, "PSASTRO.OBJECTS");
+		if (rawstars == NULL) { continue; }
 
-    // fprintf (stderr, "rawstars:\n");
-    // psastroDumpStars (rawstars);
-    // fprintf (stderr, "refstars:\n");
-    // psastroDumpStars (refstars);
+		// the refstars is a subset within range of this chip
+		psArray *refstars = psArrayAlloc (100);
 
-    // find initial offset / rotation
-    stats = pmAstromGridMatch (rawstars, refstars, config);
+		// select the reference objects within range of this readout
+		// project the reference objects to this chip
+		for (int i = 0; i < refs->n; i++) {
+		    pmAstromObj *ref = refs->data[i];
+	
+		    p_psProject (ref->TP, ref->sky, fpa->projection);
+		    psPlaneDistortApply (ref->FP, fpa->fromTangentPlane, ref->TP, 0.0, 0.0);
+		    psPlaneTransformApply (ref->chip, chip->fromFPA, ref->FP);
 
-    // adjust the chip.toFPA terms only
-    pmAstromGridApply (chip->toFPA, stats);
-    chip->fromFPA = p_psPlaneTransformLinearInvert (chip->toFPA);
+		    // XXX what is the X,Y range of the selected chip?
+		    // XXX for the moment, force these to be good for Megacam
+		    if (ref->chip->x < -500) continue;
+		    if (ref->chip->x > 2500) continue;
+		    if (ref->chip->y < -500) continue;
+		    if (ref->chip->y > 5000) continue;
+		    
+		    psArrayAdd (refstars, 100, ref);
+		}
+		psastroOneChip (fpa, chip, refstars, rawstars, recipe);
+		psFree (refstars);
 
-    // use fit result to re-project rawstars
-    psastroProjectRawstars (rawstars, readout);
-    psastroProjectRefstars (refstars, readout);
-
-    // testWriteRaw ("ref.dat", refstars);
-    // testWriteRaw ("raw.dat", rawstars);
-    
-    // use small radius to match stars
-    match = pmAstromRadiusMatch (rawstars, refstars, config);
-
-    // improved fit for astrometric terms
-    pmAstromMatchFit (chip->toFPA, rawstars, refstars, match, config);
-    chip->fromFPA = p_psPlaneTransformLinearInvert (chip->toFPA);
-
-    // per-chip astrometry has been updated
-    pmAstromWriteWCS (chip->toFPA, fpa->toSky, header);
-
+		// read WCS data from the corresponding header
+		pmHDU *hdu = pmFPAviewThisHDU (view, fpa);
+		pmAstromWriteWCS (chip->toFPA, fpa->toSky, hdu->header, plateScale);
+	    }
+	}
+    }
+    psFree (view);
     return true;
 }
+
+/* coordinate frame hierachy
+   pixels (on a given readout)
+   cell
+   chip
+   FP (focal plane)
+   TP (tangent plane)
+   sky (ra, dec)
+*/
