Index: trunk/psModules/src/objects/pmSourceIO.c
===================================================================
--- trunk/psModules/src/objects/pmSourceIO.c	(revision 41339)
+++ trunk/psModules/src/objects/pmSourceIO.c	(revision 41348)
@@ -810,4 +810,5 @@
     pmSourceIO_WriteMatchedRefs (file->fits, file->fpa, config);
     pmSourceIO_WriteGlints (file->fits, file->fpa, config);
+    pmSourceIO_WriteGhosts (file->fits, file->fpa, config);
     return true;
 }
Index: trunk/psModules/src/objects/pmSourceIO.h
===================================================================
--- trunk/psModules/src/objects/pmSourceIO.h	(revision 41339)
+++ trunk/psModules/src/objects/pmSourceIO.h	(revision 41348)
@@ -92,4 +92,5 @@
 
 bool pmSourceIO_WriteGlints (psFits *fits, pmFPA *fpa, pmConfig *config);
+bool pmSourceIO_WriteGhosts (psFits *fits, pmFPA *fpa, pmConfig *config);
 
 /// @}
Index: trunk/psModules/src/objects/pmSourceIO_Ghosts.c
===================================================================
--- trunk/psModules/src/objects/pmSourceIO_Ghosts.c	(revision 41339)
+++ trunk/psModules/src/objects/pmSourceIO_Ghosts.c	(revision 41348)
@@ -104,4 +104,5 @@
     psPolynomial2D *centerX = NULL;
     psPolynomial2D *centerY = NULL;
+    psPolynomial1D *mirrorRad = NULL;
     psPolynomial1D *outerMajor = NULL;
     psPolynomial1D *outerMinor = NULL;
@@ -110,4 +111,6 @@
     psMetadata *ghostModel = NULL;
 
+    psLogMsg ("psastro", PS_LOG_INFO, "writing ghost positions");
+
     // select the current recipe
     psMetadata *recipe  = psMetadataLookupPtr (&status, config->recipes, PSASTRO_RECIPE);
@@ -132,4 +135,5 @@
     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);
@@ -187,5 +191,4 @@
  		    for (int i = 0; i < refstars->n; i++) {
  			pmAstromObj *ref = refstars->data[i];
- 			psTrace("psastro.ghost",5,"Begin ghost %d/%ld: MAX_MAG: %g; ref_mag: %g @ (%.10g,%.10g)",i,refstars->n,MAX_MAG,ref->Mag,ref->sky->r * 180 / M_PI,ref->sky->d * 180.0 / M_PI);
  			if (ref->Mag > MAX_MAG) continue;
 
@@ -194,9 +197,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);
@@ -278,5 +287,4 @@
     	psFree (ghostModel);
     	psFree (view);
-    	return true;
 
         if (table->n == 0) {
Index: trunk/psModules/src/objects/pmSourceIO_Glints.c
===================================================================
--- trunk/psModules/src/objects/pmSourceIO_Glints.c	(revision 41339)
+++ trunk/psModules/src/objects/pmSourceIO_Glints.c	(revision 41348)
@@ -66,4 +66,6 @@
     float zeropt, exptime;
 
+    psLogMsg ("psastro", PS_LOG_INFO, "writing glint positions");
+
     // select the current recipe
     psMetadata *recipe  = psMetadataLookupPtr (&status, config->recipes, PSASTRO_RECIPE);
@@ -184,9 +186,9 @@
                 }
 
-                //do a rudimentary check of whether the glint enters the pixel FPA
-                if (!strcasecmp(glintType, "TOP")    && ((star->FP->y - glintLength) > 20500.))  continue;
-                if (!strcasecmp(glintType, "BOTTOM") && ((star->FP->y + glintLength) < -20500.))  continue;
-                if (!strcasecmp(glintType, "LEFT")   && ((star->FP->x + glintLength) < -20500.))  continue;
-                if (!strcasecmp(glintType, "RIGHT")  && ((star->FP->x - glintLength) > 20500.))  continue;
+                //do a rudimentary check of whether the glint enters the pixel FPA, and exclude very short ones
+                if (!strcasecmp(glintType, "TOP")    && ((star->FP->y - glintLength) > 19500.))  continue;
+                if (!strcasecmp(glintType, "BOTTOM") && ((star->FP->y + glintLength) < -19500.))  continue;
+                if (!strcasecmp(glintType, "LEFT")   && ((star->FP->x + glintLength) < -19500.))  continue;
+                if (!strcasecmp(glintType, "RIGHT")  && ((star->FP->x - glintLength) > 19500.))  continue;
             
                 //save the glint positions
