Index: trunk/psastro/src/psastroBuildFPA.c
===================================================================
--- trunk/psastro/src/psastroBuildFPA.c	(revision 5510)
+++ trunk/psastro/src/psastroBuildFPA.c	(revision 5560)
@@ -18,30 +18,32 @@
     // allocate the structures
 
-    pmFPA *fpa = pmFPAAlloc ();
+    pmFPA *fpa = pmFPAAlloc (NULL, NULL);
 
-    fpa->chips = psArrayAlloc (Nchips);
+    psArrayRealloc (fpa->chips, Nchips);
     for (int i = 0; i < Nchips; i++) {
+	pmChip *chip = pmChipAlloc (fpa);
 
-	pmChip *chip = pmChipAlloc ();
-	chip->fpa = fpa; // assign parent fpa (view only; don't free)
+	psArrayRealloc (chip->cells, Ncells);
+	for (int j = 0; j < Ncells; j++) {
+	    pmCell *cell = pmCellAlloc (chip);
 
-	chip->cells = psArrayAlloc (Ncells);
-	for (int j = 0; j < Ncells; j++) {
-
-	    pmCell *cell = pmCellAlloc ();
-	    cell->chip = chip;      // assign parent chip (view only; don't free)
-	    cell->header = header;  // XXX EAM : extend this function to take more than header
-
+	    // XXX EAM : extend this function to take more than one header
+	    cell->header = header;	
 	    pmCellInterpretWCS (cell, header);
 
-	    cell->readouts = psArrayAlloc (Nreadouts);
+	    psArrayRealloc (cell->readouts, Nreadouts);
 	    for (int k = 0; k < Nreadouts; k++) {
+		pmReadout *readout = pmReadoutAlloc (cell);
 
-		pmReadout *readout = pmReadoutAlloc ();
-		readout->stars  = stars;   // XXX EAM : need more than one stars...
+		// XXX EAM : extend this function to take more than one stars...
+		psMetadataAdd (readout->analysis, PS_LIST_TAIL, "STARS.FULLSET", PS_DATA_ARRAY, "stars from analysis", stars);
 
-		cells->readouts->data[j] = readout;
+		// XXX EAM : this information should be in the header...
+		readout->col0 = readout->row0 = 0;
+		readout->colBins = readout->rowBins = 1;
+
+		cell->readouts->data[j] = readout;
 	    }
-	    chips->cells->data[j] = cell;
+	    chip->cells->data[j] = cell;
 	}
 	fpa->chips->data[i] = chip;
@@ -53,9 +55,13 @@
 bool pmCellInterpretWCS (pmCell *cell, psMetadata *header) { 
 
+    pmChip *chip;
+    pmFPA  *fpa;
+    bool status;
+    psProjectionType type;
     float crval1, crval2, crpix1, crpix2, cdelt1, cdelt2;
     float pc1_1, pc1_2, pc2_1, pc2_2;
 
     // *** interpret header data, convert to crval(i), etc
-    char *ctype = pmMetadataLookupPtr (&status, header, "CTYPE2");
+    char *ctype = psMetadataLookupPtr (&status, header, "CTYPE2");
     if (!status) {
 	psLogMsg ("psastro", 2, "warning: no WCS metadata in header\n");
@@ -85,7 +91,7 @@
 
 	// test the CROTAi varient:
-	float rotate = psMetadataLookupF32 (&status, header, "CROTA2");
+	double rotate = psMetadataLookupF32 (&status, header, "CROTA2");
 	if (status) {
-	    Lambda = cdelt2 / cdelt1;
+	    double Lambda = cdelt2 / cdelt1;
 	    pc1_1 =  cos(rotate*RAD_DEG);
 	    pc1_2 = -sin(rotate*RAD_DEG) * Lambda;
@@ -131,13 +137,15 @@
 
     // XXX EAM : these must already be set
-    chip = cell->chip;
-    fpa = chip->fpa;
+    chip = cell->parent;
+    fpa  = chip->parent;
 
     // set toChip to identity as default
-    // XXX EAM : psPlaneTransformAlloc uses nTerm not nOrder (bug 581)
-    // XXX EAM : define these in the config?
-    // int nX = psMetadataLookupS32 (&status, myHeader, "CHIP.NX");
-    // int nY = psMetadataLookupS32 (&status, myHeader, "CHIP.NY");
-    cell->toChip   = psPlaneTransformAlloc (2, 2);
+    // XXX EAM : can we actually use higher order?
+    int nX = psMetadataLookupS32 (&status, header, "PSASTRO.CHIP.NX");
+    if (!status) nX = 1;
+    int nY = psMetadataLookupS32 (&status, header, "PSASTRO.CHIP.NY");
+    if (!status) nY = 1;
+
+    cell->toChip   = psPlaneTransformAlloc (1, 1);
     cell->toChip->x->coeff[1][0] = 1;
     cell->toChip->x->mask[1][1]  = 1;
@@ -152,5 +160,6 @@
 
     // XXX EAM : psPlaneTransformAlloc uses nTerm not nOrder (bug 581)
-    chip->toFPA   = psPlaneTransformAlloc (2, 2);
+    // XXX EAM : I've fixed this in pslib eam_rel8_b2
+    chip->toFPA   = psPlaneTransformAlloc (1, 1);
     
     chip->toFPA->x->coeff[0][0] = crpix1;
@@ -164,8 +173,10 @@
     chip->toFPA->y->mask[1][1]  = 1;
 
+    chip->fromFPA = p_psPlaneTransformLinearInvert (chip->toFPA);
+
     // set toTPA to identity as default
     // XXX EAM : psPlaneDistortAlloc uses nTerm not nOrder (bug 581)
     if (fpa->toTPA == NULL) {
-	fpa->toTPA   = psPlaneDistortAlloc (2, 2, 1, 1);
+	fpa->toTPA   = psPlaneDistortAlloc (1, 1, 0, 0);
 	fpa->toTPA->x->coeff[1][0][0][0] = 1;
 	fpa->toTPA->x->mask[1][1][0][0]  = 1;
@@ -173,4 +184,5 @@
 	fpa->toTPA->y->coeff[0][1][0][0] = 1;
 	fpa->toTPA->y->mask[1][1][0][0]  = 1;
+	fpa->fromTangentPlane = psPlaneDistortInvert (fpa->toTangentPlane);
     } else {
 	psLogMsg ("psastro", 2, "warning: fpa distortion already defined\n");
@@ -178,5 +190,5 @@
 
     // center of projection is (0,0) coordinate of TPA
-    fpa->toSky = psProjectionAlloc (crval1, crval2, cdelt1, cdelt2, type);
+    fpa->toSky = psProjectionAlloc (crval1*RAD_DEG, crval2*RAD_DEG, cdelt1*RAD_DEG, cdelt2*RAD_DEG, type);
     return true;
 }
