Index: trunk/psastro/src/psastroLoadGlints.c
===================================================================
--- trunk/psastro/src/psastroLoadGlints.c	(revision 41348)
+++ trunk/psastro/src/psastroLoadGlints.c	(revision 41367)
@@ -26,4 +26,6 @@
     bool status;
     float zeropt, exptime;
+    psVector *x_glint = psVectorAlloc(2,PS_TYPE_F32);
+    psVector *y_glint = psVectorAlloc(2,PS_TYPE_F32);
 
     psLogMsg ("psastro", PS_LOG_INFO, "determine glint positions");
@@ -39,14 +41,25 @@
     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");
+    double pixelScale = psMetadataLookupF32 (&status, recipe, "PSASTRO.PIXEL.SCALE");
+
+    //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) {
+        psLogMsg ("psastro", PS_LOG_INFO, "Assuming old-style glint masking, given the recipe keywords");
+        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_LENGTH_MIN_FPA = psMetadataLookupF32 (&status, recipe, "GLINT_LENGTH_MIN_FPA");
+    if (!status) GLINT_LENGTH_MIN_FPA=1000.;
     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");
-    double pixelScale = psMetadataLookupF32 (&status, recipe, "PSASTRO.PIXEL.SCALE");
+
 
     // select the set of glint regions (GLINT.REGION is a MULTI of METADATA items)
@@ -70,5 +83,5 @@
 
     // really error-out here?  or just skip?
-    if (!psastroZeroPointFromRecipe (&zeropt, &exptime, NULL, fpa, recipe)) {
+    if (!psastroZeroPointFromRecipe (&zeropt, &exptime, NULL, &GLINT_MAX_MAG, fpa, recipe)) {
         psLogMsg ("psastro", PS_LOG_INFO, "failed to load zeropt data from recipe");
         return false;
@@ -79,5 +92,5 @@
     float MagOffset = zeropt + 2.5*log10(exptime);
     GLINT_MAX_MAG += MagOffset;
-    GLINT_LENGTH_MAG_ZERO += MagOffset;
+    //GLINT_LENGTH_MAG_ZERO += MagOffset;
 
     // select the raw objects for this readout (loaded in psastroExtract.c)
@@ -91,5 +104,4 @@
     psastroChipBounds (fpa);
 
-
     // find the possible glint stars, and convert the position to FPA coordinates.
     // search for stars within the glint regions
@@ -97,10 +109,10 @@
 
 	pmAstromObj *star = glintStars->data[i];
-	if (star->Mag > GLINT_MAX_MAG) continue; // XXX should not be needed...
+	if (star->Mag > GLINT_MAX_MAG) continue; 
 
 	// project glint star to the focal-plane
 	psProject (star->TP, star->sky, fpa->toSky);
 	psPlaneTransformApply (star->FP, fpa->fromTPA, star->TP);
-	fprintf (stderr, "glint: %7.2f @ %8.1f, %8.1f (%f %f) %8.1f %8.1f\n", star->Mag, star->FP->x, star->FP->y, star->sky->r * PS_DEG_RAD, star->sky->d * PS_DEG_RAD, star->TP->x,star->TP->y);
+	//fprintf (stderr, "glint: %7.2f @ %8.1f, %8.1f (%f %f) %8.1f %8.1f\n", star->Mag, star->FP->x, star->FP->y, star->sky->r * PS_DEG_RAD, star->sky->d * PS_DEG_RAD, star->TP->x,star->TP->y);
 
 	// find the GLINT.REGION this star lands in (if any)
@@ -132,22 +144,24 @@
 	    }
 
-	    double glintLength = GLINT_LENGTH_MAG_SLOPE*(GLINT_LENGTH_MAG_ZERO - star->Mag);
+	    //double glintLength = GLINT_LENGTH_MAG_SLOPE*(GLINT_LENGTH_MAG_ZERO - star->Mag);
+            //glint length should depend on the brightness on image, i.e. in instrumental mag. The same instrumental mag in different filters should likely give the same glint length.
+	    double glintLength = GLINT_LENGTH_MAG_SLOPE*(GLINT_LENGTH_MAG_ZERO - (star->Mag-MagOffset));
             double glintAngle = 0;
 
-            //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));
-            }
-            //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.));
+            //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.));
+                }
             }
 
@@ -157,7 +171,4 @@
             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);
-	    psVector *y_glint = psVectorAlloc(2,PS_TYPE_F32);
 
 	    if (!strcasecmp(glintType, "TOP") || !strcasecmp(glintType, "BOTTOM")) {
@@ -220,4 +231,6 @@
                     yFPA1 = yFPA0 + glintLength*cos(glintAngle);
 		}
+
+                if(glintLength < GLINT_LENGTH_MIN_FPA) continue;
 
 	        x_glint->data.F32[0] = xFPA0;
@@ -294,7 +307,5 @@
 		   	  psFree (glints);
 		   	}
-		   	
-		   	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);
+		   			   	psVector *glint = psVectorAlloc(5,PS_TYPE_F32);
 		   	glint->data.F32[0] = xChip0;
 		   	glint->data.F32[1] = yChip0;
@@ -369,4 +380,6 @@
                     yFPA1 = yFPA0 + glintLength*sin(glintAngle*-1.);
 		}
+
+                if(glintLength < GLINT_LENGTH_MIN_FPA) continue;
 
 	        x_glint->data.F32[0] = xFPA0;
@@ -441,6 +454,4 @@
 		   	}
 		   	
-		   	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;
