      SUBROUTINE ORIENT(BETAMI,BETAMX,THETMI,THETMX,PHIMIN,PHIMAX,
     &                  MXBETA,MXTHET,MXPHI,NBETA,NTHETA,NPHI,
     &                  BETA,THETA,PHI,WGTA,WGTB)
C Arguments:
      INTEGER MXBETA,MXTHET,MXPHI,NBETA,NTHETA,NPHI
      REAL BETAMI,BETAMX,THETMI,THETMX,PHIMIN,PHIMAX
      REAL BETA(MXBETA),THETA(MXTHET),PHI(MXPHI),
     &     WGTA(MXTHET,MXPHI),WGTB(MXBETA)
C Local variables:
      INTEGER I,J
      REAL DELTA,WG
C***********************************************************************
C Given:
C        BETAMI=minimum value of beta (radians)
C        BETAMX=maximum value of beta (radians)
C        THETMI=minimum value of theta (radians)
C        THETMX=maximum value of theta (radians)
C        PHIMIN=minimum value of phi (radians)
C        PHIMAX=maximum value of phi (radians)
C        MXBETA,MXTHET,MXPHI=parameters for dimensioning of arrays
C                             BETA,THETA,PHI
C        NBETA=desired number of values of beta
C        NTHETA=desired number of values of theta
C        NPHI=desired number of values of PHI
C Returns:     
C        BETA(1-NBETA)=beta values (radians)
C        THETA(1-NTHETA)=theta values (radians)
C        PHI(1-NPHI)=phi values (radians)
C        WGTA(1-NTHETA,1-NPHI)=weighting of each orientation of target
C                              axis a1 in Lab Frame
C                              (sum of weights = 1)
C        WGTB(1-NBETA)=weighting of each rotation of target around a1
C                              (sum of weights = 1)
C            Note: it is assumed that target orientation weight function
C                  can be factored into WGTA*WGTB -- i.e., that rotations
C                  around a1 are decoupled from orientation of a1.
C
C Purpose: to generate list of desired target orientations
C
C Definitions: beta=angle of rotation of target around target axis a1
C                   (beta=0 is defined to be such that a2 lies in a1-x
C                    plane, with a2 in direction of increasing theta)
C              theta=angle between target axis a1 and x axis
C                   (0.le.theta.le.pi)
C              phi=angle of rotation of axis a1 around axis
C              (phi=0 is defined to be such that a1 lies in xy plane)
C Present version assumes:
C        beta to be uniformly distributed between BETAMI and BETAMX
C        cos(theta) to be uniformly distributed between cos(THETMI) and
C                   cos(THETMX)
C        phi to be uniformly distributed between PHIMIN and PHIMAX
C
C Values assigned to BETA and PHI are midpoints of uniform intervals in
C beta and phi
C Note that if NBETA=1, BETA is set to midpoint of range in phi
C Likewise, if NPHI=1, PHI is set to midpoint of range in phi
C
C If NTHETA=1: specify a single value of THETA
C        cos(theta)=0.5*(cos(thetami)+cos(thetamx))
C
C If NTHETA>1:
C    If NTHETA is even: 
C       divide range of cos(theta) into NTHETA equal intervals
C       set THETA to midpoints of these intervals
C       give intervals equal weights
C    If NTHETA is odd:
C       divide range of cos(theta) into NTHETA-1 equal intervals
C       set THETA to endpoints of these intervals; first value of
C       THETA is THETAMI and last value of THETA is THETAMX
C
C Note: values chosen for beta, phi are at midpoints of
C        uniform intervals
C
C B.T.Draine, Princeton Univ. Obs., 89.11.20
C History:
C 92.04.01 (BTD) Modify to allow Simpson's rule or trapezoidal rule
C                for integration over cos(theta).
C 99.03.01 (BTD) Modified to properly assign THETA values when NPHI>1
C                Code up to this time was not consistent with
C                description in UserGuide.  This problem was brought
C                to my attention by Miroslav Kocifaj
C                (Miroslav.Kocifaj@swh.sk).
C 99.03.05 (BTD) Corrected errors in assignment of THETA values.
C                Thanks to Miroslav Kocifaj for detecting these.
C 99.04.30 (BTD) Corrected errors in assignment of BETA and
C                PHI values when BETAMI.ne.0 or PHIMIN.ne.0
C                These errors (inadvertent omission of BETAMI and
C                PHIMIN from expressions assigning BETA(J) and PHI(J))
C                were introduced in 99.03.05 revision.
C                Thanks to Henriette Lemke for calling attention to
C                these errors.
C Copyright (C) 1993,1999 B.T. Draine and P.J. Flatau
C This code is covered by the GNU General Public License.
C***********************************************************************
C
C Assign values to BETA:
C
      DELTA=(BETAMX-BETAMI)/REAL(NBETA)
      DO J=1,NBETA
         BETA(J)=BETAMI+DELTA*(REAL(J)-0.5)
      ENDDO
C
C Assign values to THETA:
C
      IF(NTHETA.EQ.2*(NTHETA/2))THEN
C
C THETA is even:
C
        DELTA=(COS(THETMX)-COS(THETMI))/REAL(NTHETA)
        DO J=1,NTHETA
           THETA(J)=ACOS(COS(THETMI)+DELTA*(REAL(J)-0.5))
        ENDDO
      ELSE
C
C THETA is odd:
C
         IF(NTHETA.EQ.1)THEN
            THETA(1)=ACOS(0.5*(COS(THETMI)+COS(THETMX)))
         ELSE
            DELTA=(COS(THETMX)-COS(THETMI))/REAL(NTHETA-1)
            THETA(1)=THETMI
            THETA(NTHETA)=THETMX
            DO J=2,NTHETA-1
               THETA(J)=ACOS(COS(THETMI)+DELTA*REAL(J-1))
            ENDDO
         ENDIF
      ENDIF
C
C Assign values to PHI:
C
      DELTA=(PHIMAX-PHIMIN)/REAL(NPHI)
      DO J=1,NPHI
         PHI(J)=PHIMIN+DELTA*(REAL(J)-0.5)
      ENDDO
C
C** Specify weight factors WGTA, WGTB
C   (Note: weight function WGTA = 4*pi*P/(NTHETA*NPHI), where
C    P=(probability/solid angle) of orientation in direction THETA,PHI .
C    The orientational averaging program automatically samples uniformly
C    in cos(theta) to allow for d(solid angle)=sin(theta)d(theta)d(phi).
C
C   Present version assumes random orientations.
C
C   When NTHETA is >1 and even, we use trapezoidal integration
C   When NTHETA is >1 and odd, we use Simpson's rule integration.
C
      IF(NTHETA.EQ.1.OR.NTHETA.EQ.2*(NTHETA/2))THEN
C
C either 1 or even number of theta values: midpoints of intervals
C
         WG=1./REAL(NTHETA*NPHI)
         DO J=1,NPHI
            DO I=1,NTHETA
               WGTA(I,J)=WG
            ENDDO
         ENDDO
      ELSE
C
C odd number >1 of theta values: use Simpson's rule weighting
C
         WG=1./REAL(3*(NTHETA-1)*NPHI)
         DO J=1,NPHI
            WGTA(1,J)=WG
            WGTA(NTHETA,J)=WG
         ENDDO
         WG=4./REAL(3*(NTHETA-1)*NPHI)
         DO I=2,NTHETA,2
            DO J=1,NPHI
               WGTA(I,J)=WG
            ENDDO
         ENDDO
         IF(NTHETA.GE.5)THEN
            WG=2./REAL(3*(NTHETA-1)*NPHI)
            DO I=3,NTHETA-1,2
               DO J=1,NPHI
                  WGTA(I,J)=WG
               ENDDO
            ENDDO
         ENDIF
      ENDIF
C
C Assign weights for rotation angle beta:
C
      WG=1./REAL(NBETA)
      DO I=1,NBETA
         WGTB(I)=WG
      ENDDO
C
      RETURN
      END
