C LAST UPDATE: 
C+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
C
C**************************
C**     LDM_RTOD_DP      **
C**************************
C
CD-Routine: Double Precision Version of LDM_RTOD - LDM_RTOD_DP
C This is a double precision version of LDM_RTOD
CD-end
CD-Fortran:
      LOGICAL FUNCTION LDM_RTOD_DP (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          (Double precision version)
C
C           NOTE: Single precision version LDM_RTOD 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
      DOUBLE PRECISION X
      DOUBLE PRECISION Y
      DOUBLE PRECISION Z
      DOUBLE PRECISION DSTAR2
      DOUBLE PRECISION CTF
      DOUBLE PRECISION XF
      DOUBLE PRECISION YF

CD-Parameters:
C  X       dp (R)   Reciprocal lattice X coordinate (Wonacott convention)
C  Y       dp (R)   Reciprocal lattice Y coordinate (Wonacott convention)
C  Z       dp (R)   Reciprocal lattice Z coordinate (Wonacott convention)
C  DSTAR2  dp (R)   (d*)**2 for the reflection
C  CTF     dp (R)   Crystal to film distance (mm)
C  XF      dp (W)   Detector xf coordinate (mm) (Wonacott convention)
C  YF      dp (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====== GLOBALS:
C
      INCLUDE 'ldm_derived.finc'
C
C====== FUNCTION TYPES:
C

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

C     DOUBLE PRECISION RF   !Distance along x of reflecting sphere centre 
C                           !from r.l origin
C     DOUBLE PRECISION XP   !Reciprocal lattice x coord wrt 'p' axes (see code)
C     DOUBLE PRECISION YP   !Reciprocal lattice y coord wrt 'p' axes (see code)
C     DOUBLE PRECISION ZP   !Reciprocal lattice z coord wrt 'p' axes (see code)
C     DOUBLE PRECISION XPP  !Reciprocal lattice x coord wrt 'pp' axes (see code)
C     DOUBLE PRECISION YPP  !Reciprocal lattice y coord wrt 'pp' axes (see code)
C     DOUBLE PRECISION ZPP  !Reciprocal lattice z coord wrt 'pp' axes (see code)
C     DOUBLE PRECISION PI   !pi
C     DOUBLE PRECISION GAMMA !Gamma angle
C     DOUBLE PRECISION RNU   !Nu angle
C     DOUBLE PRECISION SG    !sin(gamma)

      DATA PI/3.14159265358979/
C
C-------------------------------------------------------------------------------
C
C
C====== Check that X is negative
C
      LDM_RTOD_DP = .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====== 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_DP = .TRUE.
      RETURN
      END
