Index: /trunk/psastro/src/psastro.c
===================================================================
--- /trunk/psastro/src/psastro.c	(revision 5508)
+++ /trunk/psastro/src/psastro.c	(revision 5509)
@@ -14,11 +14,11 @@
 
     // simple layout interpretation, basic WCS conversion
-    psFPA *fpa = psastroBuildFPA (header, config);
+    psFPA *fpa = psastroBuildFPA (header, rawstars);
 
     // limit fit to bright stars only 
-    psArray *subset = pspsastroSelectBrightStars (config, rawstars);
+    psFPA *subset = psastroSelectBrightStars (fpa, config);
 
     // use the header & config info to project rawstars on the focal plane
-    psastroProjectRawstars (subset, header, config);
+    psastroProjectRawstars (subset);
 
     // load the corresponding reference data (DVO command)
@@ -26,23 +26,11 @@
 
     // use the header & config info to project refstars on the focal plane
-    psastroProjectRefstars (refstars, header, config);
+    psastroProjectRefstars (refstars, subset);
 
-    // find initial offset / rotation
-    stat = pmAstromGridMatch (subset, refstars, config);
-
-    pmAstromModifyFPA (fpa, stat);
-
-    // use fit result to re-project rawstars
-    psastroProjectRawstars (fpa, subset);
-    psastroProjectRefstars (fpa, refstars);
-    
-    // use small radius to match stars
-    match = pmAstromRadiusMatch (rawstars, refstars, options);
-
-    // fit astrometric terms
-    output = pmAstromMatchedListFit (fpa, subset, refstars, match, options);
+    // fpa and subset point to the same astrometry terms
+    psastroChipAstrom (refstars, subset);
 
     // write out data (cmp file)
-    psastroWriteCMP (fpa, header, rawstars, argv[2]);
+    psastroWriteCMP (fpa, argv[2]);
 
     exit (0);
Index: /trunk/psastro/src/psastro.h
===================================================================
--- /trunk/psastro/src/psastro.h	(revision 5508)
+++ /trunk/psastro/src/psastro.h	(revision 5509)
@@ -38,5 +38,5 @@
     int colBins;                        ///< Amount of binning in x-dimension
     int rowBins;                        ///< Amount of binning in y-dimension
-    psArray *objects;			///< sources detected / measured on readout
+    psArray *stars;			///< sources detected / measured on readout
 }
 pmReadout;
Index: /trunk/psastro/src/psastroBuildFPA.c
===================================================================
--- /trunk/psastro/src/psastroBuildFPA.c	(revision 5508)
+++ /trunk/psastro/src/psastroBuildFPA.c	(revision 5509)
@@ -1,5 +1,5 @@
 # include "psastro.h"
 
-pmFPA *psastroBuildFPA (psMetadata *header, psMetadata *config) {
+pmFPA *psastroBuildFPA (psMetadata *header, psArray *stars) {
 
     // this function constructs a basic template pmFPA structure based on the information in the header
@@ -30,6 +30,6 @@
 
 	    pmCell *cell = pmCellAlloc ();
-	    cell->chip = chip; // assign parent chip (view only; don't free)
-	    cell->header = header;
+	    cell->chip = chip;      // assign parent chip (view only; don't free)
+	    cell->header = header;  // XXX EAM : extend this function to take more than header
 
 	    pmCellInterpretWCS (cell, header);
@@ -39,4 +39,6 @@
 
 		pmReadout *readout = pmReadoutAlloc ();
+		readout->stars  = stars;   // XXX EAM : need more than one stars...
+
 		cells->readouts->data[j] = readout;
 	    }
@@ -143,4 +145,6 @@
     // XXX EAM : if fpa->toSky and fpa->toTPA are already defined, then the
     //           toFPA must be modified to match the crval(i), scale(i) and crpix(i)
+    //           scale = scale(i)/scale(0) (i == chip #)
+    //           project crval1(0),crval2(0 using projection
 
     // XXX EAM : psPlaneTransformAlloc uses nTerm not nOrder (bug 581)
Index: /trunk/psastro/src/psastroUtils.c
===================================================================
--- /trunk/psastro/src/psastroUtils.c	(revision 5508)
+++ /trunk/psastro/src/psastroUtils.c	(revision 5509)
@@ -69,19 +69,34 @@
 }
 
-psArray *psastroSelectBrightStars (psMetadata *config, psArray *stars) {
-
-    stars = psArraySort (stars, psastroSortByMag);
+psArray *psastroSelectBrightStars (pmFPA *inFPA, psMetadata *config) {
+
+    pmFPA *sbFPA = pmFPACopy (fpa);
 
     // add exclusions for objects on some basis?
     int MAX_NSTARS = pmMetadataLookupS32 (&status, config, "MAX_NSTARS");
 
-    nSubset = PS_MIN (MAX_NSTARS, stars->n);
-
-    psArray *subset = psArrayAlloc (nSubset);
-
-    for (int i = 0; i < nSubset; i++) {
-	subset->data[i] = stars->data[i];
-    }
-
+    for (int i = 0; i < inFPA->chips->n; i++) {
+	pmChip *inChip = inFPA->chips->data[i];
+	pmChip *sbChip = sbFPA->chips->data[i];
+	for (int j = 0; j < Ncells; j++) {
+	    pmCell *inCell = inChip->cells->data[j];
+o	    pmCell *sbCell = sbChip->cells->data[j];
+	    for (int k = 0; k < Nreadouts; k++) {
+		pmReadout *inReadout = inCell->readouts->data[k];
+		pmReadout *sbReadout = sbCell->readouts->data[k];
+
+		inReadout->stars = psArraySort (inReadout->stars, psastroSortByMag);
+
+		nSubset = PS_MIN (MAX_NSTARS, stars->n);
+
+		psArray *subset = psArrayAlloc (nSubset);
+
+		for (int i = 0; i < nSubset; i++) {
+		    subset->data[i] = inReadout->stars->data[i];
+		}
+		sbReadout->stars = subset;
+	    }
+	}
+    }
     return (subset);
 }
@@ -133,2 +148,94 @@
 }
 
+pmFPA *pmFPACopy (pmFPA *inFPA) {
+
+    pmFPA *fpa = pmFPAAlloc ();
+
+    fpa->toSky   = psMemCopy (inFPA->toSky);
+    fpa->toTPA   = psMemCopy (inFPA->toTPA);
+    fpa->fromTPA = psMemCopy (inFPA->fromTPA);
+
+    fpa->chips = psArrayAlloc (inFPA->chips->n);
+    for (int i = 0; i < inFPA->chips->n; i++) {
+
+	pmChip *inChip = inFPA->chips->data[i];
+
+	pmChip *chip = pmChipAlloc ();
+	chip->fpa = fpa; // assign parent fpa (view only; don't free)
+
+	chip->toFPA = psMemCopy (inChip->toFPA);
+	chip->fromFPA = psMemCopy (inChip->fromFPA);
+
+	chip->cells = psArrayAlloc (inChip->cells->n);
+	for (int j = 0; j < Ncells; j++) {
+
+	    pmCell *inCell = inChip->cells->data[j];
+
+	    pmCell *cell = pmCellAlloc ();
+	    cell->chip = chip;      // assign parent chip (view only; don't free)
+
+	    cell->header = psMemCopy (inCell->header);
+	    cell->toChip = psMemCopy (inCell->toChip);
+
+	    cell->readouts = psArrayAlloc (inCell);
+	    for (int k = 0; k < Nreadouts; k++) {
+
+		pmReadout *inReadout = inCell->readouts->data[k];
+
+		pmReadout *readout = pmReadoutAlloc ();
+
+		*readout = *inReadout;
+		readout->stars = NULL;
+		
+		cell->readouts->data[k] = readout;
+	    }
+	    chip->cells->data[j] = cell;
+	}
+	fpa->chips->data[i] = chip;
+    }
+    return (fpa);
+}
+
+bool psastroProjectRawstars (pmFPA *fpa) {
+
+    for (int i = 0; i < fpa->chips->n; i++) {
+	pmChip *chip = fpa->chips->data[i];
+	for (int j = 0; j < chip->cells->n; j++) {
+	    pmCell *cell = chip->cells->data[j];
+	    for (int k = 0; k < cell->readouts->n; k++) {
+		pmReadout *readout = cell->readouts->data[k];
+		for (int m = 0; m < readout->stars->n; m++) {
+		    pmAstromObj *star = readout->data[m];
+		    // apply readout offsets
+		    // apply cell PlaneTransform
+		    // apply chip PlaneTransform
+		    psPlaneTransformApply (star-> 
+
+	    }
+	    chip->cells->data[j] = cell;
+	}
+	fpa->chips->data[i] = chip;
+    }
+    return (fpa);
+}
+ 
+bool psastroChipAstrom (psArray *refstars, pmFPA *subset) {
+
+    // do this loop over the readouts 
+
+    // find initial offset / rotation
+    stat = pmAstromGridMatch (subset, refstars, config);
+
+    pmAstromModifyFPA (fpa, stat);
+
+    // use fit result to re-project rawstars
+    psastroProjectRawstars (fpa, subset);
+    psastroProjectRefstars (fpa, refstars);
+    
+    // use small radius to match stars
+    match = pmAstromRadiusMatch (rawstars, refstars, options);
+
+    // fit astrometric terms
+    output = pmAstromMatchedListFit (fpa, subset, refstars, match, options);
+}
+
