Index: trunk/psastro/src/psastroLoadGhosts.c
===================================================================
--- trunk/psastro/src/psastroLoadGhosts.c	(revision 41342)
+++ trunk/psastro/src/psastroLoadGhosts.c	(revision 41348)
@@ -105,4 +105,5 @@
     psPolynomial2D *centerX = NULL;
     psPolynomial2D *centerY = NULL;
+    psPolynomial1D *mirrorRad = NULL;
     psPolynomial1D *outerMajor = NULL;
     psPolynomial1D *outerMinor = NULL;
@@ -110,7 +111,7 @@
     psPolynomial1D *innerMinor = NULL;
 
-
     pmFPAview *view = pmFPAviewAlloc (0);
 
+    GET_1D_POLY ("GHOST.MIRROR.RAD", mirrorRad);
     GET_2D_POLY ("GHOST.CENTER.X", centerX);
     GET_2D_POLY ("GHOST.CENTER.Y", centerY);
@@ -178,9 +179,15 @@
 		    ghost->srcFP->y = ref->FP->y;
 
-		    // XXX it is stupid that this takes -X_fpa,-Y_fpa --> the analysis script is reporting the guess FPA position, and the fit is using that...
-		    ghost->FP->x = -ref->FP->x + psPolynomial2DEval(centerX, -ref->FP->x, -ref->FP->y);
-		    ghost->FP->y = -ref->FP->y + psPolynomial2DEval(centerY, -ref->FP->x, -ref->FP->y);
-
-		    float rSrc = hypot (ref->FP->x, ref->FP->y);
+                    //TdB: first mirror the reference star positions (around the 0,0 pixel) using the radial offset coefficients and the ghost/star angle
+		    double rSrc = hypot (ref->FP->x, ref->FP->y);
+     	            double theta0 = atan2(ref->FP->y,ref->FP->x);
+
+		    double ghost_offset_rad = psPolynomial1DEval (mirrorRad, rSrc);
+		    double ghost_x_fpa_mirror = ref->FP->x + ((ref->FP->x*-1.)/abs(ref->FP->x)*abs(cos(theta0)*ghost_offset_rad));
+		    double ghost_y_fpa_mirror = ref->FP->y + ((ref->FP->y*-1.)/abs(ref->FP->y)*abs(sin(theta0)*ghost_offset_rad));
+
+		    // Now use the mirrored position together with the 2D ghost center fitting to get the actual ghost position in FPA coords 
+		    ghost->FP->x = ghost_x_fpa_mirror + psPolynomial2DEval(centerX, ghost_x_fpa_mirror, ghost_y_fpa_mirror);
+		    ghost->FP->y = ghost_y_fpa_mirror + psPolynomial2DEval(centerY, ghost_x_fpa_mirror, ghost_y_fpa_mirror);
 
 		    ghost->inner.major = psPolynomial1DEval (innerMajor, rSrc);
@@ -203,28 +210,4 @@
 		    ghostChip = psastroFindChip (&ghost->chip->x, &ghost->chip->y, fpa, ghost->FP->x, ghost->FP->y);
 		    // fprintf (stderr, "-> model chip position: %f, %f\n", ghost->chip->x, ghost->chip->y);
-
-	            psPlane coordFPA,ptCH2;
-    	            psPlane coordCell,ptFP2;
-	            coordCell.x = ghost->chip->x;
-	            coordCell.y = ghost->chip->y;
-                    psPlaneTransformApply(&coordFPA,ghostChip->toFPA,&coordCell);
-	            ptCH2.x = ghost->chip->x;
-               	    ptCH2.y = ghost->chip->y;
-                    psPlaneTransformApply(&ptFP2,ghostChip->toFPA,&ptCH2);
-
-	            psPlane ptCH, ptFP;
-	            ptFP.x = ghost->FP->x;
-               	    ptFP.y = ghost->FP->y;
-                    psPlaneTransformApply (&ptCH, ghostChip->fromFPA, &ptFP);
-
-                    psastroUpdateChipToFPA (fpa, ghostChip);
-	            psPlane ptCH3, ptFP3;
-	            ptCH3.x = ghost->chip->x;
-               	    ptCH3.y = ghost->chip->y;
-                    psPlaneTransformApply(&ptFP3,ghostChip->toFPA,&ptCH3);
-
-
-
-                    psLogMsg ("psastro", 3, "Ghost refpos: %f %f %s pos: %f %f %f %f %f %f %f %f %f %f %f %f mag: %f inshape: %f %f %f outshape: %f %f %f \n", ref->FP->x,ref->FP->y,psMetadataLookupStr(NULL,chip->concepts,"CHIP.NAME"), ghost->FP->x,ghost->FP->y,coordFPA.x,coordFPA.y,ghost->chip->x, ghost->chip->y,ptCH.x,ptCH.y,ptFP2.x,ptFP2.y,ptFP3.x,ptFP3.y, ghost->Mag,ghost->inner.major,ghost->inner.minor,ghost->inner.theta/PM_RAD_DEG,ghost->outer.major,ghost->outer.minor,ghost->outer.theta/PM_RAD_DEG);
 
 		    if (ghostChip) {
@@ -257,7 +240,5 @@
 			      ghost->outer.major,ghost->outer.minor,ghost->outer.theta);
 		    }		      
-		      
-
-			    
+		      			    
 		    if (!ghostChip) goto skip;
 		    if (!ghostChip->cells) goto skip;
Index: trunk/psastro/src/psastroLoadGlints.c
===================================================================
--- trunk/psastro/src/psastroLoadGlints.c	(revision 41342)
+++ trunk/psastro/src/psastroLoadGlints.c	(revision 41348)
@@ -152,4 +152,9 @@
             }
 
+            //do a rudimentary check of whether the glint enters the pixel FPA
+            if (!strcasecmp(glintType, "TOP")    && ((star->FP->y - glintLength) > 20000.))  continue;
+            if (!strcasecmp(glintType, "BOTTOM") && ((star->FP->y + glintLength) < -20000.))  continue;
+            if (!strcasecmp(glintType, "LEFT")   && ((star->FP->x + glintLength) < -20000.))  continue;
+            if (!strcasecmp(glintType, "RIGHT")  && ((star->FP->x - glintLength) > 20000.))  continue;
 
 	    psVector *x_glint = psVectorAlloc(2,PS_TYPE_F32);
@@ -179,4 +184,5 @@
 		        double yFPAs, yFPAe;
 		        psastroFindChipYedges (&yFPAs, &yFPAe, fpa, nChip);
+		        if (yFPAs > yFPAe) PS_SWAP (yFPAs, yFPAe);
 
   		        xFPA0 = star->FP->x;
@@ -204,4 +210,5 @@
 		        double yFPAs, yFPAe;
 		        psastroFindChipYedges (&yFPAs, &yFPAe, fpa, nChip);
+		        if (yFPAs > yFPAe) PS_SWAP (yFPAs, yFPAe);
 
   		        xFPA0 = star->FP->x;
@@ -218,4 +225,5 @@
 	        x_glint->data.F32[1] = xFPA1;
 	        y_glint->data.F32[1] = yFPA1;
+
 
                 //we need to loop over each corner to select the chips that can have the glint on it
@@ -243,4 +251,5 @@
 		        if (yFPA0 > yFPAe) continue;
 		        if (yFPA1 < yFPAs) continue;
+
 
                         //find the y-coord positions for this chip
@@ -322,10 +331,11 @@
 
 		        // FPA coordinates of intersections with chip edges 
-		        double yFPAs, yFPAe;
-		        psastroFindChipYedges (&yFPAs, &yFPAe, fpa, nChip);
-
-  		        xFPA0 = yFPAe;
+		        double xFPAs, xFPAe;
+		        psastroFindChipXedges (&xFPAs, &xFPAe, fpa, nChip);
+		        if (xFPAs > xFPAe) PS_SWAP (xFPAs, xFPAe);
+
+  		        xFPA0 = xFPAe;
 		        yFPA0 = star->FP->y;
-                        glintLength-= (star->FP->x-yFPAe);
+                        glintLength-= (star->FP->x-xFPAe);
                     }
 
@@ -347,10 +357,11 @@
 
 		        // FPA coordinates of intersections with chip edges 
-		        double yFPAs, yFPAe;
-		        psastroFindChipYedges (&yFPAs, &yFPAe, fpa, nChip);
-
-  		        xFPA0 = yFPAs;
+		        double xFPAs, xFPAe;
+		        psastroFindChipXedges (&xFPAs, &xFPAe, fpa, nChip);
+		        if (xFPAs > xFPAe) PS_SWAP (xFPAs, xFPAe);
+
+  		        xFPA0 = xFPAs;
 		        yFPA0 = star->FP->y;
-                        glintLength-= (yFPAs-star->FP->x);
+                        glintLength-= (xFPAs-star->FP->x);
                     }
 
@@ -431,4 +442,5 @@
 		   	
 		   	fprintf (stderr, "glint %s : %d %f,%f to %f,%f (%f %f %f)\n", glintType, nChip, xChip0, yChip0, xChip1, yChip1, glint_length, glintWidth, chip_angle);
+
 		   	psVector *glint = psVectorAlloc(5,PS_TYPE_F32);
 		   	glint->data.F32[0] = xChip0;
Index: trunk/psastro/src/psastroMaskUpdates.c
===================================================================
--- trunk/psastro/src/psastroMaskUpdates.c	(revision 41342)
+++ trunk/psastro/src/psastroMaskUpdates.c	(revision 41348)
@@ -424,6 +424,6 @@
                         psastroGhost *ghost = ghosts->data[i];
                         //offset the ghost angles with the astrometric rotation angle
-                        ghost->inner.theta -= (ROT_PARITY * ROTANGLE);
-                        ghost->outer.theta -= (ROT_PARITY * ROTANGLE);
+                        ghost->inner.theta = ghost->inner.theta - AST_T0;
+                        ghost->outer.theta = ghost->outer.theta - AST_T0;
                         // XXX bright vs faint ghost bits? (OR with SUSPECT)
                         psastroMaskEllipticalAnnulus (readoutMask->mask, ghostMaskValue, ghost->chip->x, ghost->chip->y, ghost->inner, ghost->outer);
@@ -444,4 +444,5 @@
                         //psastroMaskRectangle (readoutMask->mask, glintMaskValue, glint->x0, glint->y0, glint->x1, glint->y1);
 
+                        psLogMsg ("psastro", 3, "glint: %d %f %f %f %f %f\n",i, glint->data.F32[0], glint->data.F32[1],glint->data.F32[2], glint->data.F32[3], glint->data.F32[4]);
 		        psastroMaskBox (readoutMask->mask, glintMaskValue, glint->data.F32[0], glint->data.F32[1],
 				    glint->data.F32[2], glint->data.F32[3], glint->data.F32[4]);
