Index: trunk/psModules/src/astrom/pmAstrometryModel.c
===================================================================
--- trunk/psModules/src/astrom/pmAstrometryModel.c	(revision 18601)
+++ trunk/psModules/src/astrom/pmAstrometryModel.c	(revision 19506)
@@ -9,6 +9,6 @@
  *
  *  @author EAM, IfA
- *  @version $Revision: 1.4 $ $Name: not supported by cvs2svn $
- *  @date $Date: 2008-07-17 22:38:15 $
+ *  @version $Revision: 1.5 $ $Name: not supported by cvs2svn $
+ *  @date $Date: 2008-09-12 01:04:49 $
  *
  *  Copyright 2007 Institute for Astronomy, University of Hawaii
@@ -250,6 +250,4 @@
 bool pmAstromModelWriteFP (pmFPAfile *file) {
 
-    bool status;
-
     psMetadata *header = psMetadataAlloc();
     psMetadataAddStr(header, PS_LIST_TAIL, "COORD",    PS_META_REPLACE, "name of this layer",   "FOCAL_PLANE");
@@ -260,13 +258,8 @@
     psArray *model = psArrayAllocEmpty (1);
 
-    // XXX is this or the tpa region correct?
-    psRegion *region = pmAstromFPAExtent (file->fpa);
-
-    // rotate the toTPA to have 0.0 posangle
-    float posangle = psMetadataLookupF32 (&status, file->fpa->concepts, "FPA.POSANGLE");
-
-    // the to/from TPA transform currently has rotation of posangle; remove it & create the toTPA version
-    psPlaneTransform *fromTPA = psPlaneTransformRotate (NULL, file->fpa->fromTPA, -posangle);
-    psPlaneTransform *toTPA   = psPlaneTransformInvert(NULL, fromTPA, *region, 50);
+    // region over which the fromTPA projection is valid
+    psRegion *region = pmAstromFPInTP (file->fpa);
+
+    psPlaneTransform *toTPA   = file->fpa->toTPA;
 
     for (int i = 0; i <= toTPA->x->nX; i++) {
@@ -315,5 +308,6 @@
     bool status;
 
-    // get the boresite model parameters
+    // get the boresite model parameters.  these track the position of the boresite
+    // as a function of the rotator angle
     float Xo = psMetadataLookupF32 (&status, file->fpa->concepts, "FPA.BORE.X0");
     float Yo = psMetadataLookupF32 (&status, file->fpa->concepts, "FPA.BORE.Y0");
@@ -322,4 +316,6 @@
     float To = psMetadataLookupF32 (&status, file->fpa->concepts, "FPA.BORE.T0");
     float Po = psMetadataLookupF32 (&status, file->fpa->concepts, "FPA.BORE.P0");
+
+    // the PosZero is the offset between the reported and actual POSANGLE values
     float PosZero = psMetadataLookupF32 (&status, file->fpa->concepts, "FPA.POS_ZERO");  /// XXX be consistent with degrees v radians
     char *refChip = psMetadataLookupStr (&status, file->fpa->concepts, "FPA.REF.CHIP");
@@ -341,7 +337,4 @@
     psMetadataAddF32(row,    PS_LIST_TAIL, "MINY",     PS_META_REPLACE, "range", region->y0);
     psMetadataAddF32(row,    PS_LIST_TAIL, "MAXY",     PS_META_REPLACE, "range", region->y1);
-
-    // psMetadataAddF32(row,    PS_LIST_TAIL, "XREF",     PS_META_REPLACE, "", file->fpa->toSky->R  * PS_DEG_RAD);
-    // psMetadataAddF32(row,    PS_LIST_TAIL, "YREF",     PS_META_REPLACE, "", file->fpa->toSky->D  * PS_DEG_RAD);
 
     psMetadataAddF32(row,    PS_LIST_TAIL, "XSCALE",   PS_META_REPLACE, "", file->fpa->toSky->Xs * PS_DEG_RAD);
@@ -353,6 +346,7 @@
     psMetadataAddF32(row,    PS_LIST_TAIL, "BORE_T0",  PS_META_REPLACE, "boresite parameter", To);
     psMetadataAddF32(row,    PS_LIST_TAIL, "BORE_P0",  PS_META_REPLACE, "boresite parameter", Po);
-    psMetadataAddF32(row,    PS_LIST_TAIL, "POS_ZERO", PS_META_REPLACE, "boresite parameter", PosZero);
-    psMetadataAddStr(row,    PS_LIST_TAIL, "REF_CHIP", PS_META_REPLACE, "boresite parameter", refChip);
+
+    psMetadataAddF32(row,    PS_LIST_TAIL, "POS_ZERO", PS_META_REPLACE, "POSANGLE offset (degrees)", PosZero);
+    psMetadataAddStr(row,    PS_LIST_TAIL, "REF_CHIP", PS_META_REPLACE, "reference chip for model", refChip);
 
     psArrayAdd (model, 100, row);
@@ -454,5 +448,5 @@
 bool pmAstromModelReadPHU (pmFPAfile *file) {
 
-    fprintf (stderr, "not sure we need to read PHU\n");
+    // not necessary to read the PHU
     return true;
 }
@@ -500,5 +494,5 @@
     }
 
-    // XXX do I need anything from the header?
+    // load the header
     psMetadata *header = psFitsReadHeader(NULL, file->fits); // The FITS header
     if (!header) psAbort("cannot read model header");
@@ -507,5 +501,5 @@
     psArray *model = psFitsReadTable (file->fits);
     if (!model) psAbort("cannot read model");
-    fprintf (stderr, "read %ld rows from FP\n", model->n);
+    psLogMsg ("psModules.astrom", 4, "read %ld rows from FP\n", model->n);
 
     // parse the model entries
@@ -535,11 +529,4 @@
         int iy = psMetadataLookupS32(&status, row, "YORDER");  REQUIRE (status, "missing YORDER");
 
-        // XXX do I need to use this or can i rely on the camera concepts?
-        // minX = psMetadataLookupF32(&status, row, "MINX");
-        // maxX = psMetadataLookupF32(&status, row, "MAXX");
-        // minY = psMetadataLookupF32(&status, row, "MINY");
-        // maxY = psMetadataLookupF32(&status, row, "MAXY");
-
-        // XXX need to include mask values
         chip->toFPA->x->coeff[ix][iy]    = psMetadataLookupF32(&status, row, "POLY_X");
         chip->toFPA->y->coeff[ix][iy]    = psMetadataLookupF32(&status, row, "POLY_Y");
@@ -584,5 +571,5 @@
     // read the complete model data at one shot
     psArray *model = psFitsReadTable (file->fits);
-    fprintf (stderr, "read %ld rows from FP\n", model->n);
+    psLogMsg ("psModules.astrom", 4, "read %ld rows from FP\n", model->n);
 
     // parse the model
@@ -644,5 +631,5 @@
 
     psArray *model = psFitsReadTable (file->fits);
-    fprintf (stderr, "read %ld rows from TP\n", model->n);
+    psLogMsg ("psModules.astrom", 4, "read %ld rows from TP\n", model->n);
     if (model->n != 1) psAbort("invalid number of rows in TP model (%ld)", model->n);
 
@@ -679,8 +666,12 @@
 
     psArray *model = psFitsReadTable (file->fits);
-    fprintf (stderr, "read %ld rows from SKY\n", model->n);
+    psLogMsg ("psModules.astrom", 4, "read %ld rows from SKY\n", model->n);
     if (model->n != 1) psAbort("invalid number of rows in SKY model (%ld)", model->n);
 
     // XXX not much information of interest in this table...
+
+    // generate a template projection for comparisons
+    psFree (file->fpa->toSky);
+    file->fpa->toSky = psProjectionAlloc (0.0, 0.0, PS_RAD_DEG/3600.0, PS_RAD_DEG/3600.0, PS_PROJ_DIS);
 
     psFree (model);
@@ -698,5 +689,4 @@
     double DEC = psMetadataLookupF64 (&status, concepts, "FPA.DEC"); REQUIRE (status, "missing FPA.DEC");
     double POS = PM_RAD_DEG * psMetadataLookupF64 (&status, concepts, "FPA.POSANGLE"); REQUIRE (status, "missing FPA.POSANGLE");
-    // double ROT = psMetadataLookupF64 (&status, concepts, "FPA.ROTANGLE"); REQUIRE (status, "missing FPA.ROTANGLE");
 
     // get projection scale; center is supplied
@@ -721,4 +711,5 @@
 
     // apply true posangle = -(POS - POS_ZERO)
+    psLogMsg ("psModules.astrom", 4, "Position Angle: %f, Model Position Angle Zero Point: %f\n", POS, PosZero);
     psPlaneTransform *fromTPA = psPlaneTransformRotate (NULL, file->fpa->fromTPA, (POS - PosZero));
     psFree (file->fpa->fromTPA);
@@ -728,8 +719,10 @@
     psFree (file->fpa->toTPA);
     file->fpa->toTPA = psPlaneTransformInvert(NULL, file->fpa->fromTPA, *region, 50);
+    psFree (region);
 
     // current position of the nominal boresite in refChip coordinates
     double X = Xo + RX*cos(POS - To)*cos(Po) + RY*sin(POS - To)*sin(Po);
     double Y = Yo + RY*sin(POS - To)*cos(Po) - RX*cos(POS - To)*sin(Po);
+    psLogMsg ("psModules.astrom", 4, "Boresite coords on reference chip: %f, %f\n", X, Y);
 
     // get reference chip from name
@@ -742,8 +735,12 @@
     psSphere *boreSky = psSphereAlloc();
 
-    // find the true RA,DEC coord of the mirror of the reported boresite
-    boreCH->x = -X;
-    boreCH->y = -Y;
+    // find the FP coord of the reported boresite location
+    boreCH->x = X;
+    boreCH->y = Y;
     psPlaneTransformApply (boreFP, chip->toFPA, boreCH);
+
+    // find the true RA,DEC coord of the mirror of the reported boresite FP location
+    boreFP->x = -boreFP->x;
+    boreFP->y = -boreFP->y;
     psPlaneTransformApply (boreTP, file->fpa->toTPA, boreFP);
     psDeproject (boreSky, boreTP, file->fpa->toSky);
@@ -761,5 +758,4 @@
     psFree (boreSky);
 
-    psFree (region);
     return (true);
 }
