Index: /branches/eam_branches/ipp-dev-20210817/psastro/src/psastroLoadGlints.c
===================================================================
--- /branches/eam_branches/ipp-dev-20210817/psastro/src/psastroLoadGlints.c	(revision 41888)
+++ /branches/eam_branches/ipp-dev-20210817/psastro/src/psastroLoadGlints.c	(revision 41889)
@@ -52,5 +52,5 @@
     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");
+        psLogMsg ("psastro", PS_LOG_INFO, "Assuming old-style glint masking");
         glintCheck = 1;
     }
@@ -150,5 +150,5 @@
 
             //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(!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));
@@ -246,4 +246,6 @@
  			pmChip *chip = fpa->chips->data[nChip];
  			if (!chip) {continue;}
+                        const char *chipName = psMetadataLookupStr(NULL,chip->concepts, "CHIP.NAME");
+                        int X = chipName[2] - '0';
 
 		   	if (!psastroFindChipInXrange (fpa, nChip, x_glint->data.F32[glint_point], y_glint->data.F32[glint_point])) {
@@ -255,4 +257,5 @@
 		        psastroFindChipYedges (&yFPAs, &yFPAe, fpa, nChip);
 
+                        //For ease of reference, we adopt a frame in which coords with 0 are always lower in the relevant axis
 		        if (yFPAs > yFPAe) PS_SWAP (yFPAs, yFPAe);
 			if (yFPA0 > yFPA1) {
@@ -265,5 +268,4 @@
 		        if (yFPA1 < yFPAs) {continue;}
 
-
                         //find the y-coord positions for this chip
                         double ycFPA0, ycFPA1; 
@@ -272,17 +274,31 @@
 
                         //now calculate the proper x-coord positions given the angle, for this chip
-                        double xcFPA0, xcFPA1, angle; 
+                        double xcFPA0 = 0.0, xcFPA1 = 0.0;
+			double angle = 0.0, chip_angle = 0.0, glint_length = 0.0; 
 		  	angle = atan2(xFPA1 - xFPA0,yFPA1 - yFPA0);
                         xcFPA0 = xFPA0 + (ycFPA0-yFPA0)*tan(angle);
-                        xcFPA1 = xFPA0 + (ycFPA1-yFPA0)*tan(angle);
-			
-
-		  	double xChip0, yChip0, xChip1, yChip1, chip_angle, glint_length, xChip2,yChip2;
-		  	psastroFPAtoChip (&xChip0, &yChip0, fpa, nChip, xcFPA0, ycFPA0);
-		  	psastroFPAtoChip (&xChip1, &yChip1, fpa, nChip, xcFPA1, ycFPA1);
-		  	psastroFPAtoChip (&xChip2, &yChip2, fpa, nChip, star->FP->x,star->FP->y);
-
-		  	chip_angle = atan2(yChip1 - yChip0, xChip1 - xChip0);
- 		  	glint_length = sqrt(pow(yChip1 - yChip0,2) + pow(xChip1 - xChip0,2));
+                        xcFPA1 = xFPA0 + (ycFPA1-yFPA0)*tan(angle);	
+
+                        //use this to calculate the actual glint length as it wll be on this chip
+ 		  	glint_length = sqrt(pow(ycFPA1 - ycFPA0,2) + pow(xcFPA1 - xcFPA0,2));
+
+                        //also get the proper orientation of the glint angle, and determine the starting position on this chip. Remember that half of the focal plane is flipped
+		  	double xChip0, yChip0;
+	                if (!strcasecmp(glintType, "TOP")) {
+                          if(X<=3){
+                            chip_angle = PM_RAD_DEG * (-90. - glintAngle/PM_RAD_DEG);
+                          } else {
+                            chip_angle = PM_RAD_DEG * (90. - glintAngle/PM_RAD_DEG);
+                          }
+		  	  psastroFPAtoChip (&xChip0, &yChip0, fpa, nChip, xcFPA1, ycFPA1);
+                        }
+	                if (!strcasecmp(glintType, "BOTTOM")) {
+                          if(X<=3){
+                            chip_angle = PM_RAD_DEG * (glintAngle/PM_RAD_DEG +90.);
+                          } else {
+                            chip_angle = PM_RAD_DEG * (glintAngle/PM_RAD_DEG -90.);
+                          }
+		  	  psastroFPAtoChip (&xChip0, &yChip0, fpa, nChip, xcFPA0, ycFPA0);
+                        }
 
 		   	// select the 0th readout of the 0th cell for this chip
@@ -403,4 +419,5 @@
 		        psastroFindChipXedges (&xFPAs, &xFPAe, fpa, nChip);
 
+                        //For ease of reference, we adopt a frame in which coords with 0 are always lower in the relevant axis
 		        if (xFPAs > xFPAe) PS_SWAP (xFPAs, xFPAe);
 			if (xFPA0 > xFPA1) {
@@ -419,16 +436,23 @@
 
                         //now calculate the proper x-coord positions given the angle, for this chip
-                        double ycFPA0, ycFPA1, angle; 
+                        double ycFPA0 = 0.0, ycFPA1 = 0.0;
+			double glint_length = 0.0, angle = 0.0, chip_angle = 0.0;
 		  	angle = atan2(xFPA1 - xFPA0,yFPA1 - yFPA0);
                         ycFPA0 = yFPA0 + (xcFPA0-xFPA0)/tan(angle);
                         ycFPA1 = yFPA0 + (xcFPA1-xFPA0)/tan(angle);
 
-		  	double xChip0, yChip0, xChip1, yChip1, chip_angle, glint_length,xChip2,yChip2;
-		  	psastroFPAtoChip (&xChip0, &yChip0, fpa, nChip, xcFPA0, ycFPA0);
-		  	psastroFPAtoChip (&xChip1, &yChip1, fpa, nChip, xcFPA1, ycFPA1);
-		  	psastroFPAtoChip (&xChip2, &yChip2, fpa, nChip, star->FP->x,star->FP->y);
-
-		  	chip_angle = atan2(yChip1 - yChip0, xChip1 - xChip0);
-		  	glint_length = sqrt(pow(yChip1 - yChip0,2) + pow(xChip1 - xChip0,2));
+                        //use this to calculate the actual glint length as it wll be on this chip
+ 		  	glint_length = sqrt(pow(ycFPA1 - ycFPA0,2) + pow(xcFPA1 - xcFPA0,2));
+
+                        //also get the proper orientation of the glint angle, and determine the starting position on this chip. Remember that half of the focal plane is flipped
+		  	double xChip0, yChip0;
+	                if (!strcasecmp(glintType, "LEFT")) {
+                          chip_angle = PM_RAD_DEG * (180. - glintAngle/PM_RAD_DEG);
+  		  	  psastroFPAtoChip (&xChip0, &yChip0, fpa, nChip, xcFPA0, ycFPA0);
+                        }
+	                if (!strcasecmp(glintType, "RIGHT")) {
+                          chip_angle = PM_RAD_DEG * (glintAngle/PM_RAD_DEG +180.);
+		  	  psastroFPAtoChip (&xChip0, &yChip0, fpa, nChip, xcFPA1, ycFPA1);
+                        }
 
 		   	// select the 0th readout of the 0th cell for this chip
