Index: trunk/psModules/src/objects/pmSourceIO.h
===================================================================
--- trunk/psModules/src/objects/pmSourceIO.h	(revision 41349)
+++ trunk/psModules/src/objects/pmSourceIO.h	(revision 41367)
@@ -91,4 +91,5 @@
 bool pmSourceIO_ReadMatchedRefs (psFits *fits, pmFPA *fpa, const pmConfig *config);
 
+bool psastroZeroPointFromRecipeGlint (float *zeropt, float *exptime, float *ghostMaxMag, double *glintMaxMag, pmFPA *fpa, psMetadata *recipe);
 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 41349)
+++ trunk/psModules/src/objects/pmSourceIO_Ghosts.c	(revision 41367)
@@ -135,5 +135,13 @@
     pmFPAview *view = pmFPAviewAlloc (0);
 
-    GET_1D_POLY ("GHOST.MIRROR.RAD", mirrorRad);
+    //We need to check whether we are dealing with an old style ghost_model, or a new style model. Check if the mirror_rad polynomial exists
+    float mirCheck = 0;
+    md = psMetadataLookupMetadata (&status, ghostModel, "GHOST.MIRROR.RAD"); 
+    if (!md) { psLogMsg ("psastro", PS_LOG_INFO, "No ghost mirror_rad polynomial found. Assuming old-style ghost masking"); } 
+    if (md) {
+        GET_1D_POLY ("GHOST.MIRROR.RAD", mirrorRad);
+        mirCheck = 1;
+    }  
+
     GET_2D_POLY ("GHOST.CENTER.X", centerX);
     GET_2D_POLY ("GHOST.CENTER.Y", centerY);
@@ -143,4 +151,5 @@
     GET_1D_POLY ("GHOST.INNER.MAJOR", innerMajor);
     GET_1D_POLY ("GHOST.INNER.MINOR", innerMinor);
+
 
     // select the input astrometry data (also carries the refstars)
@@ -153,5 +162,5 @@
 
     // really error-out here?  or just skip?
-    if (!psastroZeroPointFromRecipe (&zeropt, &exptime, &MAX_MAG, fpa_ast, recipe)) {
+    if (!psastroZeroPointFromRecipeGlint (&zeropt, &exptime, &MAX_MAG,NULL, fpa_ast, recipe)) {
         psLogMsg ("psastro", PS_LOG_INFO, "failed to load zeropt data from recipe");
 	goto escape;
@@ -201,11 +210,19 @@
      	                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);
+                        if(mirCheck) {
+                            //TdB: first mirror the reference star positions (around the 0,0 pixel) using the radial offset coefficients and the ghost/star angle
+		            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);
+                        }  
+                        if(!mirCheck) {
+                            //Use the old-style ghost position determination
+                            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);
+                        }
 
  			ghost->inner.major = psPolynomial1DEval (innerMajor, rSrc);
@@ -503,2 +520,4 @@
   return true;
 }
+
+
Index: trunk/psModules/src/objects/pmSourceIO_Glints.c
===================================================================
--- trunk/psModules/src/objects/pmSourceIO_Glints.c	(revision 41349)
+++ trunk/psModules/src/objects/pmSourceIO_Glints.c	(revision 41367)
@@ -78,13 +78,19 @@
     if (!REFSTAR_MASK_GLINTS) return true;
 
-    // select the limiting magnitude
+    // select relevant keywords
     double GLINT_MAX_MAG = psMetadataLookupF32 (&status, recipe, "GLINT_MAX_MAG");
     double GLINT_LENGTH_MAG_SLOPE = psMetadataLookupF32 (&status, recipe, "GLINT_LENGTH_MAG_SLOPE");
     double GLINT_LENGTH_MAG_ZERO = psMetadataLookupF32 (&status, recipe, "GLINT_LENGTH_MAG_ZERO");
+    double glintWidth = psMetadataLookupF32 (&status, recipe, "GLINT_WIDTH");
+
+    //we will use one of the new keywords to differentiate between an old and new style glint treatment
+    float glintCheck = 0;
     double GLINT_LENGTH_POS_SLOPE = psMetadataLookupF32 (&status, recipe, "GLINT_LENGTH_POS_SLOPE");
+    if (!status) { glintCheck = 1; }
     double GLINT_LENGTH_POS_REF = psMetadataLookupF32 (&status, recipe, "GLINT_LENGTH_POS_REF");
+    double GLINT_LENGTH_POS_CUT = psMetadataLookupF32 (&status, recipe, "GLINT_LENGTH_POS_CUT");
     double GLINT_ANGLE_POS_SLOPE = psMetadataLookupF32 (&status, recipe, "GLINT_ANGLE_POS_SLOPE");
     double GLINT_ANGLE_POS_REF = psMetadataLookupF32 (&status, recipe, "GLINT_ANGLE_POS_REF");
-    double glintWidth = psMetadataLookupF32 (&status, recipe, "GLINT_WIDTH");
+
     bool GLINT_OUTPUT = psMetadataLookupBool(&status, recipe, "PSASTRO.SAVE.GLINTS");
     if (!GLINT_OUTPUT) return true;
@@ -111,5 +117,5 @@
     // use the zero point and exposure time to convert to apparent mags: M_ap = M_inst + C_0 + 2.5*log(exptime)
     pmFPA *fpa_ast = astrom->fpa;
-    if (!psastroZeroPointFromRecipe (&zeropt, &exptime, NULL, fpa_ast, recipe)) {
+    if (!psastroZeroPointFromRecipeGlint (&zeropt, &exptime, NULL,&GLINT_MAX_MAG, fpa_ast, recipe)) {
         psLogMsg ("psastro", PS_LOG_INFO, "failed to load zeropt data from recipe");
         return false;
@@ -167,21 +173,22 @@
 	        }
 
+	        double glintLength = GLINT_LENGTH_MAG_SLOPE*(GLINT_LENGTH_MAG_ZERO - star->Mag);
                 double glintAngle = 0;
-                //compute the angle of the glint, which depends on position parallel to the FPX
-	        if (!strcasecmp(glintType, "TOP") || !strcasecmp(glintType, "BOTTOM") ){ 
-	          glintAngle = PM_RAD_DEG * (GLINT_ANGLE_POS_SLOPE*((GLINT_ANGLE_POS_REF - star->FP->x)/1000.));
-                }
-	        if (!strcasecmp(glintType, "LEFT") || !strcasecmp(glintType, "RIGHT") ) {
- 	          glintAngle = PM_RAD_DEG * (GLINT_ANGLE_POS_SLOPE*((GLINT_ANGLE_POS_REF - star->FP->y)/1000.));
-                }
-
-	        double glintLength = GLINT_LENGTH_MAG_SLOPE*(GLINT_LENGTH_MAG_ZERO - star->Mag);
-                //Besides brightness, the length of the glints also depends on the position of the star compared to the focal plane. But, seemingly only for stars closer than 30k pixels     
-	        if ((!strcasecmp(glintType, "TOP") || !strcasecmp(glintType, "BOTTOM")) && abs(star->FP->y) < 30000 ){ 
-                  glintLength /= GLINT_LENGTH_POS_SLOPE*(GLINT_LENGTH_POS_REF - abs(star->FP->y));
-
-                }
-	        if ((!strcasecmp(glintType, "LEFT") || !strcasecmp(glintType, "RIGHT")) && abs(star->FP->x) < 30000 ) {
-                  glintLength /= GLINT_LENGTH_POS_SLOPE*(GLINT_LENGTH_POS_REF - abs(star->FP->x));
+
+                //Besides brightness, the length of the glints also depends on the position of the star compared to the focal plane. But, seemingly only for stars closer than about 30k pixels
+                if(glintCheck) {
+	            if ((!strcasecmp(glintType, "TOP") || !strcasecmp(glintType, "BOTTOM")) && abs(star->FP->y) < GLINT_LENGTH_POS_CUT ){ 
+                      glintLength /= GLINT_LENGTH_POS_SLOPE*(GLINT_LENGTH_POS_REF - abs(star->FP->y));
+                    }
+	            if ((!strcasecmp(glintType, "LEFT") || !strcasecmp(glintType, "RIGHT")) && abs(star->FP->x) < GLINT_LENGTH_POS_CUT ) {
+                      glintLength /= GLINT_LENGTH_POS_SLOPE*(GLINT_LENGTH_POS_REF - abs(star->FP->x));
+                    }
+                    //also compute the angle of the glint, which depends on position parallel to the FPX
+	            if (!strcasecmp(glintType, "TOP") || !strcasecmp(glintType, "BOTTOM") ){ 
+	              glintAngle = PM_RAD_DEG * (GLINT_ANGLE_POS_SLOPE*((GLINT_ANGLE_POS_REF - star->FP->x)/1000.));
+                    }
+	            if (!strcasecmp(glintType, "LEFT") || !strcasecmp(glintType, "RIGHT") ) {
+ 	              glintAngle = PM_RAD_DEG * (GLINT_ANGLE_POS_SLOPE*((GLINT_ANGLE_POS_REF - star->FP->y)/1000.));
+                    }
                 }
 
@@ -230,6 +237,5 @@
 }
 
-
-bool psastroZeroPointFromRecipe (float *zeropt, float *exptime, float *ghostMaxMag, pmFPA *fpa, psMetadata *recipe) {
+bool psastroZeroPointFromRecipeGlint (float *zeropt, float *exptime, float *ghostMaxMag, double *glintMaxMag, pmFPA *fpa, psMetadata *recipe) {
 
     bool status;
@@ -279,4 +285,13 @@
             psLogMsg ("psastro", PS_LOG_INFO, "found GHOST_MAX_MAG %f",*ghostMaxMag);
         }
+        if (glintMaxMag) {
+            float MaxMag = psMetadataLookupF32 (&status, refItem->data.md, "GLINT_MAX_MAG");
+            if (status) {
+                *glintMaxMag = MaxMag ;
+	        //MEH null is a pain.. so only log if set
+                psLogMsg ("psastro", PS_LOG_INFO, "found GLINT_MAX_MAG %f",*glintMaxMag);
+            }
+        }
+
 	//MEH what zpt is set to 
         psLogMsg ("psastro", PS_LOG_INFO, "found ZEROPT  %f",*zeropt);
