
CD-Section: Calculate an Individual Spot Position
C A routine is available to calculate the predicted detector position
C for an individual Laue reflection. 
CD-end

C LAST UPDATE: 
C+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
C
C**************************
C**     LRL_CALC_POS     **
C**************************
C
CD-Routine: Calculate Individual Reflection Position - LRL_CALC_POS
C This routine calculates the predicted detector position for an
C individual reflection. Distortion corrections are applied.
CD-end
CD-Fortran:
      SUBROUTINE LRL_CALC_POS (IPACK, IPLATE, X_CEN, Y_CEN, 
     +                         COSOM, SINOM,
     +                         AMAT, H, K, L, XFD, YFD, IERR)
CD-end
      INCLUDE 'implicit_none.finc'
C
C Purpose: Calculate the position of a Laue spot on the detector
C
C Author:  John W. Campbell (based on code from GENLAUE)
C
C Arguments:
C
      INTEGER IPACK
      INTEGER IPLATE
      REAL X_CEN
      REAL Y_CEN
      REAL COSOM
      REAL SINOM
      REAL AMAT(3,3)
      INTEGER H
      INTEGER K
      INTEGER L
      REAL XFD
      REAL YFD
      INTEGER IERR
CD-Parameters:
C IPACK     i (R)  Pack number
C IPLATE    i (R)  Plate number
C X_CEN     r (R)  Corrected x centre value (raster units)
C Y_CEN     r (R)  Corrected y centre value (raster units)
C COSOM     r (R)  cos (omega)
C SINOM     r (R)  sin (omega)
C AMAT(3,3) r (R)  A matrix calculated using current LDM parameters
C H         i (R)  Reflection h index
C K         i (R)  Reflection k index
C L         i (R)  Reflection l index
C XFD       r (W)  Predicted x position (raster units) (distortion 
C                  corrected)
C YFD       r (W)  Predicted y position (raster units) (distortion 
C                  corrected)
C IERR      i (W)  = 0 OK
C                  =-1, Invalid pack (outside program limits)
C                  =-2, Invalid plate (outside program limits)
C                  = 1 could not be recorded with current detector position
CD-end
C====== Parameter statements
C 

C
C====== GLOBALS:
C
C
C====== FUNCTION TYPES:
C
      LOGICAL LDM_RTOD
C
C====== LOCALS:
C
      INTEGER IFLAG, ND, KERR
      REAL CTOF
      REAL SPINDLE
      REAL SPIN(2,2)
      REAL X, Y, Z
      REAL XF, YF
      REAL X1, Y1, Z1
      REAL DSTAR2
      REAL DEG_TO_RAD
      REAL MM_RAST_X, MM_RAST_Y
      CHARACTER*2 ERRSTR

      DATA DEG_TO_RAD/0.0174533/
C
C-------------------------------------------------------------------------------
C
      XFD = 0.0
      YFD = 0.0
C
C====== Check pack/plate number
C
      CALL LDM_CHK_GPL (IPACK, IPLATE, IERR, ERRSTR)
      IF (IERR.NE.0) RETURN
C
C====== Get LDM parameters
C
      CALL LDM_GET_CTOF (IPACK, IPLATE, CTOF, IFLAG, ND, KERR, ERRSTR)
      CALL LDM_GET_SPIN (IPACK, SPINDLE, IFLAG, ND, KERR, ERRSTR)
      CALL LDM_MMRAST (IPACK, IPLATE, MM_RAST_X, MM_RAST_Y)

C
C====== Set up spindle rotation matrix
C
      SPIN(1,1) = COS(SPINDLE*DEG_TO_RAD)
      SPIN(1,2) = -SIN(SPINDLE*DEG_TO_RAD)
      SPIN(2,1) = -SPIN(1,2)
      SPIN(2,2) = SPIN(1,1)
      X1 = AMAT(1,1)*H + AMAT(1,2)*K + AMAT(1,3)*L
      Y1 = AMAT(2,1)*H + AMAT(2,2)*K + AMAT(2,3)*L
      Z1 = AMAT(3,1)*H + AMAT(3,2)*K + AMAT(3,3)*L

      DSTAR2 = X1*X1 + Y1*Y1 + Z1*Z1
C
C====== Apply spindle rotation
C
      X = SPIN(1,1)*X1 + SPIN(1,2)*Y1
      Y = SPIN(2,1)*X1 + SPIN(2,2)*Y1
      Z = Z1
 
      IF (LDM_RTOD(X, Y, Z, DSTAR2, CTOF, XF, YF)) THEN
         CALL LDM_CORR (IPACK, IPLATE, COSOM, SINOM, CTOF, XF, YF,
     +                  XFD, YFD)
         IERR = 0
         XFD = X_CEN + XFD*MM_RAST_X
         YFD = Y_CEN + YFD*MM_RAST_Y
      ELSE
         IERR = 1
      END IF
 
      RETURN
      END
