Index: /trunk/psModules/src/astrom/pmAstrometryWCS.c
===================================================================
--- /trunk/psModules/src/astrom/pmAstrometryWCS.c	(revision 16067)
+++ /trunk/psModules/src/astrom/pmAstrometryWCS.c	(revision 16068)
@@ -7,6 +7,6 @@
  *  @author EAM, IfA
  *
- *  @version $Revision: 1.25 $ $Name: not supported by cvs2svn $
- *  @date $Date: 2007-12-28 04:37:34 $
+ *  @version $Revision: 1.26 $ $Name: not supported by cvs2svn $
+ *  @date $Date: 2008-01-15 02:48:58 $
  *
  *  Copyright 2006 Institute for Astronomy, University of Hawaii
@@ -182,5 +182,5 @@
     psProject (FP, sky, wcs->toSky); // find the RA,DEC coord of the focal-plane coordinate
 
-    // I need the inverse of wcs->transform at this point
+    // XXX I actually need the inverse of wcs->transform at this point
     psPlaneTransformApply (Chip, wcs->trans, FP);
 
@@ -427,10 +427,30 @@
         double rY = toSky->Ys / fpa->toSky->Ys;
 
-        for (int i = 0; i <= fpa->toTPA->x->nX; i++) {
-            for (int j = 0; j <= fpa->toTPA->x->nY; j++) {
+        for (int i = 0; i <= toFPA->x->nX; i++) {
+            for (int j = 0; j <= toFPA->x->nY; j++) {
                 toFPA->x->coeff[i][j] *= rX;
                 toFPA->y->coeff[i][j] *= rY;
             }
         }
+
+	// apply the exiting fromTPA transformation to make the new toFPA consistent with the toTPA layter
+	// XXX this only works if toTPA is at most a linear transformation
+        psPlaneTransform *toFPAnew = psPlaneTransformAlloc(toFPA->x->nX, toFPA->x->nY);
+	for (int i = 0; i <= toFPA->x->nX; i++) {
+	  for (int j = 0; j <= toFPA->x->nY; j++) {
+	    double f1 = toFPA->x->coeffMask[i][j] ? 0.0 : fpa->fromTPA->x->coeff[1][0]*toFPA->x->coeff[i][j];
+	    double f2 = toFPA->y->coeffMask[i][j] ? 0.0 : fpa->fromTPA->x->coeff[0][1]*toFPA->y->coeff[i][j];
+	    toFPAnew->x->coeff[i][j] = f1 + f2;
+
+	    double g1 = toFPA->x->coeffMask[i][j] ? 0.0 : fpa->fromTPA->y->coeff[1][0]*toFPA->x->coeff[i][j];
+	    double g2 = toFPA->y->coeffMask[i][j] ? 0.0 : fpa->fromTPA->y->coeff[0][1]*toFPA->y->coeff[i][j];
+	    toFPAnew->y->coeff[i][j] = g1 + g2;
+	  }
+	}
+	toFPAnew->x->coeff[0][0] += fpa->fromTPA->x->coeff[0][0];
+	toFPAnew->y->coeff[0][0] += fpa->fromTPA->y->coeff[0][0];
+
+	psFree (toFPA);
+	toFPA = toFPAnew;
 
         // adjust reference pixel for new toSky reference coordinate
