
C LAST UPDATE: 
C+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
C
C**************************
C**     LDM_DTOR         **
C**************************
C
CD-Routine: Convert Detector to Reciprocal Lattice Coordinates - LDM_DTOR
C This routine converts ideal detector coordinates to reciprocal lattice
C coordinates.
CD-end
CD-Fortran:
      SUBROUTINE LDM_DTOR (XF, YF, DSTAR2, CTF, X, Y, Z)
CD-end
      INCLUDE 'implicit_none.finc'
C
C Purpose: Get reciprocal lattice coordinates from detector coordinates.
C
C Author:  John W. Campbell, July 1993
C
C          (No change required for cylindrical as handled in LDM_DTOQAX)
C
C Arguments:
C
      REAL XF
      REAL YF
      REAL DSTAR2
      REAL CTF
      REAL X
      REAL Y
      REAL Z

CD-Parameters:
C  XF      r (R)    Detector xf coordinate (mm) (Wonacott convention)
C  YF      r (R)    Detector yf coordinate (mm) (Wonacott convention)
C  DSTAR2  r (R)    (d*)**2 for the reflection
C  CTF     r (R)    Crystal to detector distance (mm)
C  X       r (W)    Reciprocal lattice X coordinate (Wonacott convention)
C  Y       r (W)    Reciprocal lattice Y coordinate (Wonacott convention)
C  Z       r (W)    Reciprocal lattice Z coordinate (Wonacott convention)
C
C Note: For untilted detector xf || Y, yf || Z
CD-end
C====== LOCALS:
C
      REAL XQ
      REAL YQ
      REAL ZQ
      REAL R
      REAL RP
      REAL TH
      REAL XP
      REAL YP
      REAL ZP

C      REAL XQ        !Reflection x coord wrt 'q' axes (see code)
C      REAL YQ        !Reflection y coord wrt 'q' axes (see code)
C      REAL ZQ        !Reflection z coord wrt 'q' axes (see code)
C      REAL R         !Distance of reflection on detector from crystal
C      REAL RP        !Radius of reflecting sphere (+ve)
C      REAL TH        !Bragg theta angle (radians)
C      REAL XP        !Reciprocal lattice x coord wrt 'p' axes (see code)
C      REAL YP        !Reciprocal lattice y coord wrt 'p' axes (see code)
C      REAL ZP        !Reciprocal lattice z coord wrt 'p' axes (see code)
C
C-------------------------------------------------------------------------------
C
C
C====== Get coordinates of detector position wrt axes rotated to give xq || y,
C====== yq || z and zq along x origin at centre of reflecting sphere. ('q' axes)
C
      CALL LDM_DTOQAX (XF, YF, CTF, XQ, YQ, ZQ)
C
C====== Get distance of this point from origin
C
      R = SQRT(XQ*XQ+YQ*YQ+ZQ*ZQ)
C
C====== Determine the value of theta and radius (+ve) of reflecting sphere
C     
      TH = 0.5*ACOS(ZQ/R)
      RP = SQRT(DSTAR2)/(2.0*SIN(TH))
C
C====== Get reciprocal lattice point coordinates wrt the same axes ('p' axes)
C
      XP = XQ*RP/R
      YP = YQ*RP/R
      ZP = ZQ*RP/R
C
C====== Get reciprocal lattice point coordinates in required coordinates
C
      X = ZP - RP
      Y = XP
      Z = YP
      RETURN
      END
