pro rotate,a1,a2,ak1,cxe01,cxe02,beta,theta,phi,en0r,$
                       cxe01r,cxe02r,mxsca,nscat,ensc,em1,em2,aksr,$
                       em1r,em2r

;WARNING - rough translation to IDL don't use
;declare  4x4 arrays ---> index FORTRAN vs IDL
;this if for CALLTARGET routine

; Arguments:
; Local variables
;***********************************************************************
; Given:
;       A1(1-3)=axis 1 of (original, unrotated) target
;       A2(1-3)=axis 2 of (original, unrotated) target
;       AK1=magnitude of k vector
;       CXE01(1-3)=original incident polarization vector 1
;       CXE02(1-3)=original incident polarization vector 2
;       BETA=angle (radians) through which target is to be rotated
;            around target axis A1
;       THETA=angle (radians)which target axis A1 is to make with
;            Lab x-axis
;       PHI=angle (radians) between Lab x,A1 plane and Lab x,y plane
;       ENSC(1-3,1-NSCAT)=scattering directions in Lab Frame
;       EM1(1-3,1-NSCAT)=scattering polarization vector 1 in Lab Frame
;       EM2(1-3,1-NSCAT)=scattering polarization vector 2 in Lab Frame
;
; Returns:
;       EN0R(1-3)=incident propagation vector in Target Frame
;       CXE01R(1-3)=incident polariz. vector 1 in Target Frame
;       CXE02R(1-3)=incident polariz. vector 2 in Target Frame
;       AKSR(1-3,1-NSCAT)=scattering k vectors in Target Frame
;       EM1R(1-3,1-NSCAT)=scattering pol. vector 1 in Target Frame
;       EM2R(1-3,1-NSCAT)=scattering pol. vector 2 in Target Frame
;
; Purpose:
;       In the Lab Frame, we hold the propagation direction (x-axis)
;       and incident polarization vectors (CXE01 and CXE02) fixed,
;       and rotate target to an orientation specified by the three
;       angles BETA,THETA,PHI.
;       However, computations in the main program DDSCAT are carried out
;       in the Target Frame, in which the target is held fixed and the
;       propagation vectors and polarization vectors are rotated.
;       Hence, this routine computes the incident propagation vector,
;       scattering propagation vectors, and associated polarization
;       vectors in the Target Frame.
;
; B. T. Draine, Princeton Univ. Obs., 89.11.20
; History:
; 91.04.22 (BTD) Eliminated vector A1R and removed superfluous line
;                computing A1R
; 96.11.03 (BTD) Corrected comments
; end history
;
; Copyright (C) 1996, B.T. Draine and P.J. Flatau
; This code is covered by the GNU General Public License.
;***********************************************************************
;** Determine the orientation angles for original unrotated target:
;     .. Scalar Arguments ..
;      REAL ak1, beta, phi, theta
;      INTEGER mxsca, nscat
;     .. Array Arguments ..
;      COMPLEX cxe01(3), cxe01r(3), cxe02(3), cxe02r(3)
;      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)
;     .. Local Scalars ..
;      REAL beta0, phi0, sinthe, theta0
;      INTEGER i
;     .. Local Arrays ..
;      REAL rm1(3,3), rm2(3,3), rm3(3,3), vec(3)
      rm1=fltarr(3,3)
      rm2=fltarr(3,3)
      rm3=fltarr(3,3)
      vec=fltarr(3)

;     .. External Subroutines ..
;      EXTERNAL mult3, prod3, prod3c, prod3v, rot2
;     .. Intrinsic Functions ..
;      INTRINSIC acos, sin
      theta0 = acos(a1(1))
      sinthe = sin(theta0)
      IF (sinthe NE 0.) THEN begin
          phi0 = acos(a1(2)/sin(theta0))
      endif ELSE begin
;** If sin(THETA0)=0, we will arbitrarily assume PHI0=0
          phi0 = 0.
      ENDELSE
      IF (sinthe NE 0.) THEN begin
          beta0 = acos(-a2(1)/sinthe)
      endif ELSE begin
          beta0 = acos(a2(2))
      ENDELSE
;
;**** First rotate the target through angle PHI-PHI0 around x axis
;    (i.e., rotate the lab through angle PHI0-PHI around x axis)
;** obtain rotation matrix:
      vec(1) = 1.
      vec(2) = 0.
      vec(3) = 0.
      rot2,vec,phi0-phi,rm1
;
;**** Now rotate the target through angle THETA-THETA0 around axis
;     VEC = xaxis cross a1
;     (i.e., rotate the lab through angle THETA0-THETA around
;     xaxis cross a1 )
;** first compute rotation axis
      vec(1) = 0.
      vec(2) = -a1(3)
      vec(3) = a1(2)
;*** 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 begin
          vec(2) = a1(3)*a2(1) - a1(1)*a2(3)
          vec(3) = a1(1)*a2(2) - a1(2)*a2(1)
      ENDIF
      rot2,vec,theta0-theta,rm2
      mult3,rm2,rm1,rm3
;**** Now rotate the target through angle BETA-BETA0 around a1
;     (i.e., rotate the lab through angle BETA0-BETA around a1)
      rot2,a1,beta0-beta,rm1
      mult3,rm1,rm3,rm2
;
;**** RM2 is now the full rotation matrix
;
;**** Now rotate all required vectors:
; Incident propagation vector:
      vec(1) = 1.
      vec(2) = 0.
      vec(3) = 0.
      prod3,rm2,vec,en0r
; Incident polarization vectors:
      prod3c,rm2,cxe01,cxe01r
      prod3c,rm2,cxe02,cxe02r
; Scattering vectors:
      prod3v;rm2,ensc,aksr,nscat,mxsca
      for i=1, nscat do begin ;DO 10 i = 1, nscat
          aksr(1,i) = ak1*aksr(1,i)
          aksr(2,i) = ak1*aksr(2,i)
          aksr(3,i) = ak1*aksr(3,i)
      endfor    ;10 CONTINUE
; Scattering polarization vectors:
      prod3v,rm2,em1,em1r,nscat,mxsca
      prod3v,rm2,em2,em2r,nscat,mxsca
      RETURN
      END

