      SUBROUTINE ROTATE(A1,A2,AK1,CXE01,CXE02,BETA,THETA,PHI,
     &                  EN0R,CXE01R,CXE02R,MXSCA,NSCAT,ENSC,EM1,EM2,
     &                  AKSR,EM1R,EM2R)
C Arguments:
      INTEGER MXSCA,NSCAT
      REAL AK1, BETA, THETA, PHI
      REAL A1(3), A2(3), AKSR(3,MXSCA),
     &     EM1(3,MXSCA), EM1R(3,MXSCA), EM2(3,MXSCA),
     &     EM2R(3,MXSCA), EN0R(3), ENSC(3,MXSCA)
      COMPLEX CXE01(3),CXE02(3),CXE01R(3),CXE02R(3)
C Local variables
      INTEGER I
      REAL BETA0, PHI0, SINTHE, THETA0
      REAL RM1(3,3),RM2(3,3),RM3(3,3),VEC(3)
C***********************************************************************
C Given:
C       A1(1-3)=axis 1 of (original, unrotated) target
C       A2(1-3)=axis 2 of (original, unrotated) target
C       AK1=magnitude of k vector
C       CXE01(1-3)=original incident polarization vector 1
C       CXE02(1-3)=original incident polarization vector 2
C       BETA=angle (radians) through which target is to be rotated
C            around target axis A1
C       THETA=angle (radians)which target axis A1 is to make with
C            Lab x-axis
C       PHI=angle (radians) between Lab x,A1 plane and Lab x,y plane
C       ENSC(1-3,1-NSCAT)=scattering directions in Lab Frame
C       EM1(1-3,1-NSCAT)=scattering polarization vector 1 in Lab Frame
C       EM2(1-3,1-NSCAT)=scattering polarization vector 2 in Lab Frame
C
C Returns:
C       EN0R(1-3)=incident propagation vector in Target Frame
C       CXE01R(1-3)=incident polariz. vector 1 in Target Frame
C       CXE02R(1-3)=incident polariz. vector 2 in Target Frame
C       AKSR(1-3,1-NSCAT)=scattering k vectors in Target Frame
C       EM1R(1-3,1-NSCAT)=scattering pol. vector 1 in Target Frame
C       EM2R(1-3,1-NSCAT)=scattering pol. vector 2 in Target Frame
C
C Purpose:
C       In the Lab Frame, we hold the propagation direction (x-axis)
C       and incident polarization vectors (CXE01 and CXE02) fixed,
C       and rotate target to an orientation specified by the three
C       angles BETA,THETA,PHI.
C       However, computations in the main program DDSCAT are carried out
C       in the Target Frame, in which the target is held fixed and the
C       propagation vectors and polarization vectors are rotated.
C       Hence, this routine computes the incident propagation vector,
C       scattering propagation vectors, and associated polarization
C       vectors in the Target Frame.
C
C B. T. Draine, Princeton Univ. Obs., 89.11.20
C History:
C 91.04.22 (BTD) Eliminated vector A1R and removed superfluous line
C                computing A1R
C 96.11.03 (BTD) Corrected comments
C end history
C
C Copyright (C) 1996, B.T. Draine and P.J. Flatau
C This code is covered by the GNU General Public License.
C***********************************************************************
C** Determine the orientation angles for original unrotated target:
      THETA0=ACOS(A1(1))
      SINTHE=SIN(THETA0)
      IF(SINTHE.NE.0.)THEN
         PHI0=ACOS(A1(2)/SIN(THETA0))
      ELSE
C** If sin(THETA0)=0, we will arbitrarily assume PHI0=0
         PHI0=0.
      ENDIF
      IF(SINTHE.NE.0.)THEN
         BETA0=ACOS(-A2(1)/SINTHE)
      ELSE
         BETA0=ACOS(A2(2))
      ENDIF
C
C**** First rotate the target through angle PHI-PHI0 around x axis
C    (i.e., rotate the lab through angle PHI0-PHI around x axis)
C** obtain rotation matrix:
      VEC(1)=1.
      VEC(2)=0.
      VEC(3)=0.
      CALL ROT2(VEC,PHI0-PHI,RM1)
C
C**** Now rotate the target through angle THETA-THETA0 around axis
C     VEC = xaxis cross a1
C     (i.e., rotate the lab through angle THETA0-THETA around
C     xaxis cross a1 )
C** first compute rotation axis
      VEC(1)=0.
      VEC(2)=-A1(3)
      VEC(3)=A1(2)
C*** For special case where a1=xaxis, we want rotation axis=a1 cross a2
      IF((VEC(2)**2+VEC(3)**2).LT.1.E-10)THEN
         VEC(2)=A1(3)*A2(1)-A1(1)*A2(3)
         VEC(3)=A1(1)*A2(2)-A1(2)*A2(1)
      ENDIF
      CALL ROT2(VEC,THETA0-THETA,RM2)
      CALL MULT3(RM2,RM1,RM3)
C**** Now rotate the target through angle BETA-BETA0 around a1
C     (i.e., rotate the lab through angle BETA0-BETA around a1)
      CALL ROT2(A1,BETA0-BETA,RM1)
      CALL MULT3(RM1,RM3,RM2)
C
C**** RM2 is now the full rotation matrix
C
C**** Now rotate all required vectors:
C Incident propagation vector:
      VEC(1)=1.
      VEC(2)=0.
      VEC(3)=0.
      CALL PROD3(RM2,VEC,EN0R)
C Incident polarization vectors:
      CALL PROD3C(RM2,CXE01,CXE01R)
      CALL PROD3C(RM2,CXE02,CXE02R)
C Scattering vectors:
      CALL PROD3V(RM2,ENSC,AKSR,NSCAT,MXSCA)
      DO 50 I=1,NSCAT
         AKSR(1,I)=AK1*AKSR(1,I)
         AKSR(2,I)=AK1*AKSR(2,I)
         AKSR(3,I)=AK1*AKSR(3,I)
   50 CONTINUE
C Scattering polarization vectors:
      CALL PROD3V(RM2,EM1,EM1R,NSCAT,MXSCA)
      CALL PROD3V(RM2,EM2,EM2R,NSCAT,MXSCA)
      RETURN
      END



