Index: /trunk/psModules/src/astrom/Makefile.am
===================================================================
--- /trunk/psModules/src/astrom/Makefile.am	(revision 12518)
+++ /trunk/psModules/src/astrom/Makefile.am	(revision 12519)
@@ -5,4 +5,5 @@
 libpsmodulesastrom_la_SOURCES  = \
 	pmAstrometryObjects.c \
+	pmAstrometryRegions.c \
 	pmAstrometryDistortion.c \
 	pmAstrometryUtils.c \
@@ -11,4 +12,5 @@
 pkginclude_HEADERS = \
 	pmAstrometryObjects.h \
+	pmAstrometryRegions.h \
 	pmAstrometryDistortion.h \
 	pmAstrometryUtils.h \
Index: /trunk/psModules/src/astrom/pmAstrometryDistortion.c
===================================================================
--- /trunk/psModules/src/astrom/pmAstrometryDistortion.c	(revision 12518)
+++ /trunk/psModules/src/astrom/pmAstrometryDistortion.c	(revision 12519)
@@ -7,6 +7,6 @@
 *  @author EAM, IfA
 *
-*  @version $Revision: 1.16 $ $Name: not supported by cvs2svn $
-*  @date $Date: 2007-03-18 22:06:58 $
+*  @version $Revision: 1.17 $ $Name: not supported by cvs2svn $
+*  @date $Date: 2007-03-21 22:00:49 $
 *
 *  Copyright 2006 Institute for Astronomy, University of Hawaii
@@ -29,4 +29,5 @@
 #include "pmFPAExtent.h"
 #include "pmAstrometryObjects.h"
+#include "pmAstrometryRegions.h"
 #include "pmAstrometryDistortion.h"
 #include "pmKapaPlots.h"
@@ -317,15 +318,6 @@
     // reset the fromTPA terms here. choose an appropriate region based on the dimensions of
     // the complete FPA
-    psRegion *region = pmFPAExtent (fpa);
-    region->x0 *= pixelScale;
-    region->x1 *= pixelScale;
-    region->y0 *= pixelScale;
-    region->y1 *= pixelScale;
-    int xCenter = 0.5*(region->x0 + region->x1);
-    int yCenter = 0.5*(region->y0 + region->y1);
-    region->x0 -= xCenter;
-    region->x1 -= xCenter;
-    region->y0 -= yCenter;
-    region->y1 -= yCenter;
+    psRegion *region = pmAstromFPAExtent (fpa);
+
     psFree (fpa->fromTPA);
     fpa->fromTPA = psPlaneTransformInvert(NULL, fpa->toTPA, *region, 50);
Index: /trunk/psModules/src/astrom/pmAstrometryWCS.c
===================================================================
--- /trunk/psModules/src/astrom/pmAstrometryWCS.c	(revision 12518)
+++ /trunk/psModules/src/astrom/pmAstrometryWCS.c	(revision 12519)
@@ -7,6 +7,6 @@
  *  @author EAM, IfA
  *
- *  @version $Revision: 1.19 $ $Name: not supported by cvs2svn $
- *  @date $Date: 2007-03-18 22:05:43 $
+ *  @version $Revision: 1.20 $ $Name: not supported by cvs2svn $
+ *  @date $Date: 2007-03-21 22:00:49 $
  *
  *  Copyright 2006 Institute for Astronomy, University of Hawaii
@@ -23,4 +23,5 @@
 #include "pmAstrometryWCS.h"
 #include "pmAstrometryUtils.h"
+#include "pmAstrometryRegions.h"
 
 // the following functions support coordinate transformations direcly related to the FITS WCS
@@ -83,12 +84,26 @@
 
 // convert toFPA / toSky components to traditional WCS
-bool pmAstromReadBilevelMosaic (pmFPA *fpa, const psMetadata *header, double pixelScale)
+// we require the header to have NAXIS1,NAXIS2, the field of the FPA 
+// the center of the TPA/Sky projection is 0.5*(NAXIS1,NAXIS2)
+bool pmAstromReadBilevelMosaic (pmFPA *fpa, const psMetadata *header)
 {
     pmAstromWCS *wcs = pmAstromWCSfromHeader (header);
     if (!wcs) {
-        return false;
-    }
-
-    bool status = pmAstromWCSBileveltoFPA (fpa, wcs, pixelScale);
+        psError(PS_ERR_UNKNOWN, false, "failure to determine WCS terms from header");
+        return false;
+    }
+
+    bool status1 = false;
+    bool status2 = false;
+    int Nx = psMetadataLookupS32 (&status1, header, "NAXIS1");
+    int Ny = psMetadataLookupS32 (&status2, header, "NAXIS2");
+    if (!status1 || !status2) {
+	psFree (wcs);
+        psError(PS_ERR_UNKNOWN, false, "missing required FPA size in header");
+        return false;
+    }
+
+    psRegion region = psRegionSet (-0.5*Nx, +0.5*Nx, -0.5*Ny, +0.5*Ny);
+    bool status = pmAstromWCSBileveltoFPA (fpa, wcs, region);
 
     psFree (wcs);
@@ -113,6 +128,15 @@
     pmAstromWCS *wcs = pmAstromWCSBilevelMosaicFromFPA (fpa, tol);
 
+    // we need to specify the dimensions of the FPA
+    // if we have chips defined, we can do
+    psRegion *region = pmAstromFPAExtent (fpa);
+    int Nx = region->x1 - region->x0;
+    int Ny = region->y1 - region->y0;
+    psMetadataAddS32 (header, PS_LIST_TAIL, "NAXIS1", PS_META_REPLACE, "Mosaic Dimensions", Nx);
+    psMetadataAddS32 (header, PS_LIST_TAIL, "NAXIS2", PS_META_REPLACE, "Mosaic Dimensions", Ny);
+
     pmAstromWCStoHeader (header, wcs);
 
+    psFree (region);
     psFree (wcs);
     return true;
@@ -442,13 +466,12 @@
 
     // free an existing toFPA structure
+    psFree (chip->toFPA);
     chip->toFPA = toFPA;
-    // NOTE that region specifies the FP region in PIXELS covered by the chip
-    psRegion *region = pmChipExtent (chip);
-    region->x1 -= region->x0;
-    region->y1 -= region->y0;
-    region->x0 = 0;
-    region->y0 = 0;
+
+    // determine the inverse transformation: we need the chip pixels covered by this transform
+    psRegion *region = pmChipPixels (chip);
+
     psFree (chip->fromFPA);
-    chip->fromFPA = psPlaneTransformInvert(NULL, toFPA, *region, 50);
+    chip->fromFPA = psPlaneTransformInvert(NULL, chip->toFPA, *region, 50);
     psFree (region);
 
@@ -482,10 +505,10 @@
 
     // create transformation with 0,0 reference pixel and units of microns/pixel
+    psFree (chip->toFPA);
     chip->toFPA = psPlaneTransformSetCenter (NULL, wcs->trans, -wcs->crpix1, -wcs->crpix2);
-    psRegion *region = pmChipExtent (chip);
-    region->x1 -= region->x0;
-    region->y1 -= region->y0;
-    region->x0 = 0;
-    region->y0 = 0;
+
+    // determine the inverse transformation: we need the chip pixels covered by this transform
+    psRegion *region = pmChipPixels (chip);
+
     psFree (chip->fromFPA);
     chip->fromFPA = psPlaneTransformInvert(NULL, chip->toFPA, *region, 50);
@@ -496,5 +519,5 @@
 
 // convert a pmAstromWCS structure representing a bilevel mosaic into corresponding fpa elements
-bool pmAstromWCSBileveltoFPA (pmFPA *fpa, const pmAstromWCS *wcs, double pixelScale)
+bool pmAstromWCSBileveltoFPA (pmFPA *fpa, const pmAstromWCS *wcs, psRegion region)
 {
     // projection from TPA (microns) to SKY (radians)
@@ -513,24 +536,8 @@
     }
 
-    // XXX this is still wrong: the pmFPAExtent function returns the FP dimensions in pixel units
-    // relative to the 0,0 corner of chip 0,0.  for now, just recenter...
-    // XXX I need to have a function which loops over all cells, determines the pixel dimensions 
-    // for each cell, converts them to chip pixels, then uses the fpa astrometry structures
-    // to get the total dimensions of the fpa in fpa units.  equivalent to pmFPAExtent
-    psRegion *region = pmFPAExtent (fpa);
-    region->x0 *= pixelScale;
-    region->x1 *= pixelScale;
-    region->y0 *= pixelScale;
-    region->y1 *= pixelScale;
-    int xCenter = 0.5*(region->x0 + region->x1);
-    int yCenter = 0.5*(region->y0 + region->y1);
-    region->x0 -= xCenter;
-    region->x1 -= xCenter;
-    region->y0 -= yCenter;
-    region->y1 -= yCenter;
+    // the transformation used the region to define the inversion grid
+    // the region defines the FPA pixels covered by the tranformation
     psFree (fpa->fromTPA);
-    fpa->fromTPA = psPlaneTransformInvert(NULL, fpa->toTPA, *region, 50);
-    psFree (region);
-
+    fpa->fromTPA = psPlaneTransformInvert(NULL, fpa->toTPA, region, 50);
     return true;
 }
Index: /trunk/psModules/src/astrom/pmAstrometryWCS.h
===================================================================
--- /trunk/psModules/src/astrom/pmAstrometryWCS.h	(revision 12518)
+++ /trunk/psModules/src/astrom/pmAstrometryWCS.h	(revision 12519)
@@ -4,6 +4,6 @@
  * @author EAM, IfA
  *
- * @version $Revision: 1.10 $ $Name: not supported by cvs2svn $
- * @date $Date: 2007-03-18 22:06:09 $
+ * @version $Revision: 1.11 $ $Name: not supported by cvs2svn $
+ * @date $Date: 2007-03-21 22:00:49 $
  * Copyright 2006 Institute for Astronomy, University of Hawaii
  */
@@ -39,5 +39,5 @@
 // convert from wcs terms to chip->toFPA, fpa->toSky,toTPA terms
 bool pmAstromWCSBileveltoChip (pmChip *chip, const pmAstromWCS *wcs);
-bool pmAstromWCSBileveltoFPA (pmFPA *fpa, const pmAstromWCS *wcs, double pixelScale);
+bool pmAstromWCSBileveltoFPA (pmFPA *fpa, const pmAstromWCS *wcs, psRegion region);
 
 // convert from chip->toFPA, fpa->toSky,toTPA terms to wcs terms
@@ -57,5 +57,5 @@
 
 bool pmAstromReadBilevelChip (pmChip *chip, const psMetadata *header);
-bool pmAstromReadBilevelMosaic (pmFPA *fpa, const psMetadata *header, double pixelScale);
+bool pmAstromReadBilevelMosaic (pmFPA *fpa, const psMetadata *header);
 
 bool pmAstromWriteBilevelChip (psMetadata *header, const pmChip *chip, double tol);
