Index: trunk/psastro/src/psModUtils.c
===================================================================
--- trunk/psastro/src/psModUtils.c	(revision 5560)
+++ trunk/psastro/src/psModUtils.c	(revision 5565)
@@ -1,116 +1,146 @@
 # include "psastro.h"
 
-// these functions temporarily replace existing psModules code
+psPlane *psCoordReadoutToCell (psPlane *cellpix, psPlane *readpix, pmReadout *readout) {
 
-// template sample alloc/free pair:
-# if (0)
-static void psFooFree (psFoo *foo) {
+    if (cellpix == NULL) {
+	cellpix = psPlaneAlloc ();
+    }
 
-  if (foo == NULL) return;
-  return;
-}
-psFoo *psFooAlloc (int i1, int i2) {
+    cellpix->x = readpix->x*readout->colBins + readout->col0;
+    cellpix->y = readpix->y*readout->rowBins + readout->row0;
 
-  psFoo *foo = psAlloc (sizeof(psFoo));
-  psMemSetDeallocator(foo, (psFreeFunc) psFooFree);
-
-  return (foo);
-}
-# endif
-
-// pmFPA
-static void pmFPAFree (pmFPA *fpa) {
-
-  if (fpa == NULL) return;
-
-  psFree (fpa->toSky);
-  psFree (fpa->toTPA);
-  psFree (fpa->fromTPA);
-  psFree (fpa->chips);
-
-  return;
+    return (cellpix);
 }
 
-pmFPA *pmFPAAlloc (void) {
+psPlane *psCoordCellToReadout (psPlane *readpix, psPlane *cellpix, pmReadout *readout) {
 
-  pmFPA *fpa = psAlloc (sizeof(pmFPA));
-  psMemSetDeallocator(fpa, (psFreeFunc) pmFPAFree);
+    if (readpix == NULL) {
+	readpix = psPlaneAlloc ();
+    }
 
-  fpa->toSky   = NULL;
-  fpa->toTPA   = NULL;
-  fpa->fromTPA = NULL;
-  fpa->chips   = NULL;
+    readpix->x = (cellpix->x - readout->col0) / readout->colBins;
+    readpix->y = (cellpix->y - readout->row0) / readout->rowBins;
 
-  return (fpa);
+    return (readpix);
 }
 
-// pmChip
-static void pmChipFree (pmChip *chip) {
+psPlane* psCoordChipToCell_EAM(psPlane* cellCoord,
+			       const psPlane* chipCoord,
+			       const pmCell* cell)
+{
+    PS_ASSERT_PTR_NON_NULL(chipCoord, NULL);
+    PS_ASSERT_PTR_NON_NULL(cell, NULL);
+    PS_ASSERT_PTR_NON_NULL(cell->parent, NULL);
 
-  if (chip == NULL) return;
-
-  psFree (chip->toFPA);
-  psFree (chip->fromFPA);
-  psFree (chip->cells);
-
-  return;
+    // XXX EAM : why was this being done?
+    // pmCell *tmpCell = pmCellInChip(chipCoord, cell->parent);
+    PS_ASSERT_PTR_NON_NULL(cell->toChip, NULL);
+    psPlaneTransform *tmpChipToCell = p_psPlaneTransformLinearInvert(cell->toChip);
+    PS_ASSERT_PTR_NON_NULL(tmpChipToCell, NULL);
+    cellCoord = psPlaneTransformApply(cellCoord, tmpChipToCell, chipCoord);
+    psFree(tmpChipToCell);
+    return(cellCoord);
 }
 
-pmChip *pmChipAlloc (void) {
+bool psastroProjectFPA (pmFPA *fpa, char *starlist, bool toSky) {
 
-  pmChip *chip = psAlloc (sizeof(pmChip));
-  psMemSetDeallocator(chip, (psFreeFunc) pmChipFree);
+    bool status;
 
-  chip->toFPA   = NULL;
-  chip->fromFPA = NULL;
-  chip->cells   = NULL;
+    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];
+		psArray *stars = psMetadataLookupPtr (&status, readout->analysis, starlist);
+		if (toSky) {
+		    psastroProjectRawstars (stars, readout);
+		} else {
+		    psastroProjectRefstars (stars, readout);
+		}
+	    }
+	}
+    }
+    return true;
+}
+ 
+bool psastroProjectRawstars (psArray *stars, pmReadout *readout) {
 
-  return (chip);
+    pmCell *cell = readout->parent;
+    pmChip *chip = cell->parent;
+    pmFPA  *fpa  = chip->parent;
+
+    for (int i = 0; i < stars->n; i++) {
+	pmAstromObj *star = stars->data[i];
+	psCoordReadoutToCell (&star->cell, &star->pix, readout);
+	psCoordCellToChip (&star->chip, &star->cell, cell);
+	psCoordChipToFPA (&star->FP, &star->chip, chip);
+	psCoordFPAToTP (&star->TP, &star->FP, 0.0, 0.0, fpa);
+	psCoordTPToSky (&star->sky, &star->TP, fpa->projection);
+    }
+    return true;
+}
+ 
+bool psastroProjectRefstars (psArray *stars, pmReadout *readout) {
+
+    pmCell *cell = readout->parent;
+    pmChip *chip = cell->parent;
+    pmFPA  *fpa  = chip->parent;
+
+    for (int i = 0; i < stars->n; i++) {
+	pmAstromObj *star = stars->data[i];
+	psCoordSkyToTP (&star->TP, &star->sky, fpa->projection);
+	psCoordTPToFPA (&star->FP, &star->TP, 0.0, 0.0, fpa);
+	psCoordFPAToChip (&star->chip, &star->FP, chip);
+	psCoordChipToCell_EAM (&star->cell, &star->chip, cell);
+	psCoordCellToReadout (&star->pix, &star->cell, readout);
+    }
+    return true;
+}
+ 
+pmFPA *pmFPACopyAstrom (pmFPA *inFPA) {
+
+    pmFPA *fpa = pmFPAAlloc (NULL, NULL);
+
+    // copy FPA astrometry data
+    fpa->toSky   = psProjectionCopy (inFPA->toSky);
+    fpa->toTPA   = psPlaneDistortCopy (inFPA->toTPA);
+    fpa->fromTPA = psPlaneDistortCopy (inFPA->fromTPA);
+
+    psArrayRealloc (fpa->chips, inFPA->chips->n);
+    for (int i = 0; i < inFPA->chips->n; i++) {
+	pmChip *inChip = inFPA->chips->data[i];
+	pmChip *chip = pmChipAlloc (fpa);
+
+	// copy Chip astrometry data
+	chip->toFPA = psPlaneTransformCopy (inChip->toFPA);
+	chip->fromFPA = psPlaneTransformCopy (inChip->fromFPA);
+
+	psArrayRealloc (chip->cells, inChip->cells->n);
+	for (int j = 0; j < inChip->cells->n; j++) {
+	    pmCell *inCell = inChip->cells->data[j];
+	    pmCell *cell = pmCellAlloc (chip);
+
+	    // cell.header is a view on inCell.header
+	    cell->header = psMemCopy (inCell->header);
+
+	    // copy Cell astrometry data 
+	    cell->toChip = psPlaneTransformCopy (inCell->toChip);
+
+	    psArrayRealloc (cell->readouts, inCell->readouts->n);
+	    for (int k = 0; k < inCell->readouts->n; k++) {
+		pmReadout *inReadout = inCell->readouts->data[k];
+		pmReadout *readout = pmReadoutAlloc (cell);
+
+		// copy Readout data
+		*readout = *inReadout;
+
+		cell->readouts->data[k] = readout;
+	    }
+	    chip->cells->data[j] = cell;
+	}
+	fpa->chips->data[i] = chip;
+    }
+    return (fpa);
 }
 
-// pmCell
-static void pmCellFree (pmCell *cell) {
-
-  if (cell == NULL) return;
-
-  psFree (cell->toChip);
-  psFree (cell->readouts);
-
-  return;
-}
-
-pmCell *pmCellAlloc (void) {
-
-  pmCell *cell = psAlloc (sizeof(pmCell));
-  psMemSetDeallocator(cell, (psFreeFunc) pmCellFree);
-
-  cell->toChip   = NULL;
-  cell->readouts = NULL;
-
-  return (cell);
-}
-
-// pmReadout
-static void pmReadoutFree (pmReadout *readout) {
-
-  if (readout == NULL) return;
-
-  psFree (readout->stars);
-
-  return;
-}
-
-pmReadout *pmReadoutAlloc (void) {
-
-  pmReadout *readout = psAlloc (sizeof(pmReadout));
-  psMemSetDeallocator(readout, (psFreeFunc) pmReadoutFree);
-
-  readout->col0 = 0;
-  readout->row0 = 0;
-  readout->colBins = 1;
-  readout->rowBins = 1;
-  readout->stars = NULL;
-
-  return (readout);
-}
-
