
CD-Section: Special Reciprocal Lattice to Detector Conversions
C Some routines are available for converting between reciprocal lattice
C and detector coordinates. Where appropriate, these routines should be
C used as they would be modified to cope with any new requirements for
C a detector arrangement. The currently cope with a normal or tilted
C detector.
CD-end

C LAST UPDATE: 
C+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
C
C**************************
C**     LDM_RTOD         **
C**************************
C
CD-Routine: Convert Reciprocal Lattice to Detector Coordinates - LDM_RTOD
C This routine will convert from reciprocal lattice to ideal detector 
C xf, yf coordinates. It also returns a flag to indicate whether the 
C reflection would be recorded on the detector at all assuming that it is 
C sufficiently large.
CD-end
CD-Fortran:
      LOGICAL FUNCTION LDM_RTOD (X, Y, Z, DSTAR2, CTF, XF, YF)
CD-end
      INCLUDE 'implicit_none.finc'
C
C Purpose: Get detector coordinates from reciprocal lattice coordinates.
C
C          (Single precision version)
C
C           NOTE: Double precision version LDM_RTOD_DP will also need to be 
C                 modified in an analogous manner to this routine if it 
C                 is changed.
C
C Author:  John W. Campbell, July 1993, July 1997
C          Cylindrical camera option Wilkinson & Lehmann
C
C Arguments:
C
      REAL X
      REAL Y
      REAL Z
      REAL DSTAR2
      REAL CTF
      REAL XF
      REAL YF

CD-Parameters:
C  X        r (R)    Reciprocal lattice X coordinate (Wonacott convention)
C  Y        r (R)    Reciprocal lattice Y coordinate (Wonacott convention)
C  Z        r (R)    Reciprocal lattice Z coordinate (Wonacott convention)
C  DSTAR2   r (R)    (d*)**2 for the reflection
C  CTF      r (R)    Crystal to film distance (mm)
C  XF       r (W)    Detector xf coordinate (mm) (Wonacott convention)
C  YF       r (W)    Detector yf coordinate (mm) (Wonacott convention)
C
C Return:  Returns .TRUE. if reflection would be recorded on the detector
C          (if sufficiently large) or .FALSE. if it would not. Check to
C          avoid divide overflow incorporated.
C
C Note: For untilted detector xf || Y, yf || Z
CD-end
C
C
C====== GLOBALS:
C
      INCLUDE 'ldm_derived.finc'
C
C====== FUNCTION TYPES:
C

C
C====== LOCALS:
C
      REAL RF
      REAL XP
      REAL YP
      REAL ZP
      REAL XPP
      REAL YPP
      REAL ZPP
      REAL PI
      REAL GAMMA
      REAL RNU
      REAL SG

C     REAL RF   !Distance along x of reflecting sphere centre 
C               !from r.l origin
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     REAL XPP  !Reciprocal lattice x coord wrt 'pp' axes (see code)
C     REAL YPP  !Reciprocal lattice y coord wrt 'pp' axes (see code)
C     REAL ZPP  !Reciprocal lattice z coord wrt 'pp' axes (see code)
C     REAL PI   !pi
C     REAL GAMMA !Gamma angle
C     REAL RNU   !Nu angle
C     REAL SG    !sin(gamma)

      DATA PI/3.14159265/
C
C-------------------------------------------------------------------------------
C
C
C====== Check that X is negative
C
      LDM_RTOD = .FALSE.
      XF = 0.0
      YF = 0.0
      IF (X.GT.0.0) RETURN
C
C====== Get distance (along X) of centre of reflecting sphere from r.l. origin
C
      RF = DSTAR2/(2.0*X)
C
C====== Get r.l. point with respect to axes xp || y, yp || z, zp || x with
C====== origin at centre of reflecting sphere ('p' axes)
C
      XP = Y
      YP = Z
      ZP = X - RF

      IF (IDGEOM.EQ.1) THEN
C
C====== Flat
C
C
C====== Get coordinates of r.l. point wrt axes rotated by detector angle
C====== to give xpp || xf, yp || yf and zp normal to the detector with origin 
C====== at centre of reflecting sphere ('pp' axes)
C
         XPP = XP*COS_DTILT - ZP*SIN_DTILT
         YPP = YP
         ZPP = ZP*COS_DTILT + XP*SIN_DTILT
         IF (ZPP.LT.0.00001) RETURN
C
C====== Now get coordinates on the detector (i.e. at distance 'ctof')
C
         XF = CTF*XPP/ZPP
         YF = CTF*YPP/ZPP
C
C====== Cylindrical
C
      ELSE
	RNU = ASIN(-Z/RF)
        SG = -Y/(COS(RNU)*RF)
        IF (SG.GT.1.0) SG = 1.0
        IF (SG.LT.-1.0) SG = -1.0
	GAMMA = ASIN(SG)
        IF(ZP.LT.0.0) THEN
          IF (Y.LT.0.0) THEN
            GAMMA = -PI - GAMMA
          ELSE
            GAMMA = PI - GAMMA
          END IF
        END IF
	XF = CTF*GAMMA
	YF = CTF*TAN(RNU)
      END IF
      LDM_RTOD = .TRUE.
      RETURN
      END
