      SUBROUTINE TARANIREC(A1,A2,XV,YV,ZV,DX,CDESCR,IOSHP,MXNAT,NAT,
     &                     IXYZ,ICOMP)
      IMPLICIT NONE

C Arguments:

      REAL XV,YV,ZV
      INTEGER IOSHP,MXNAT,NAT
      CHARACTER CDESCR*67
      INTEGER*2 ICOMP(MXNAT,3),IXYZ(MXNAT,3)
      REAL A1(3),A2(3),DX(3)

C Local variables:

      INTEGER JA,JX,JY,JZ,NX,NY,NZ
      CHARACTER CMSGNM*70

C External subroutines:

      EXTERNAL ERRMSG

C Intrinsic functions:
c intrinsic function: do not need to explicitly type:      INTEGER IFIX
      INTRINSIC IFIX
C***********************************************************************
C Subroutine TARANIREC
C Purpose: to construct rectangular target with anisotropic dielectric
C          tensor.
C          Dielectric tensor is assumed to be diagonalized in target
C          frame, with 
C          material 1 for x-direction
C          material 2 for y-direction
C          material 3 for z-direction
C          Note: Must specify NCOMP=3 in ddscat.par, and provide
C                file names for three materials
C Input:
C        XV=(x-length)/d    (d=lattice spacing)
C        YV=(y-length)/d
C        ZV=(z-length)/d
C        DX(1-3)=(dx,dy,dz)/d where dx,dy,dz=lattice spacings in x,y,z
C                directions, and d=(dx*dy*dz)**(1/3)=effective lattice
C                spacing
C        IOSHP=device number for "target.out" file
C             =-1 to suppress printing of "target.out"
C        MXNAT=dimensioning information (max number of atoms)
C Output:
C        A1(1-3)=unit vector (1,0,0) defining target axis 1 in Target Frame
C        A2(1-3)=unit vector (0,1,0) defining target axis 2 in Target Frame
C        NAT=number of atoms in target
C        IXYZ(1-NAT,1-3)=x/d,y/d,z/d for atoms of target
C
C B.T.Draine, Princeton Univ. Obs.
C History:
C 91.01.05 (BTD): Change I4 -> I7 when printing NAT.
C 91.04.22 (BTD): Added XV,YV,ZV and A1,A2 to WRITE(IOSHP,FMT=9020)
C 93.03.12 (BTD): Specified CDESCR*67, and now use it.
C 95.07.12 (BTD): Changed definition of axes A1,A2.  They used to be
C                 longest,next longest dimention.  They are now
C                 fixed to always be (1,0,0) and (0,1,0).
C 96.01.26 (BTD): Replaced IX,IY,IZ by IXYZ
C 97.12.26 (BTD): Added DX(1-3) to argument list to support nonuniform
C                 lattice spacing.
C 98.02.11 (BTD): Modify to generate target on noncubic lattice.
C 00.11.02 (BTD): Add ICOMP to argument list, set ICOMP=1,
C                 write ICOMP to target.out
C 04.04.29 (BTD): Created from TARREC with only minor modifications 
C end history
C
C Copyright (C) 1993,1995,1996,1997,1998,2000,2004
C               B.T. Draine and P.J. Flatau
C This code is covered by the GNU General Public License.
C***********************************************************************
C***********************************************************************
      NX=IFIX(XV/DX(1)+0.5)
      NY=IFIX(YV/DX(2)+0.5)
      NZ=IFIX(ZV/DX(3)+0.5)
      WRITE(CMSGNM,FMT='(A,3I4)')
     &      ' Anisotropic rectangular prism; NX,NY,NZ=',NX,NY,NZ
      WRITE(CDESCR,FMT='(A,3I4)')
     &      ' Anisotropic rectangular prism; NX,NY,NZ=',NX,NY,NZ

C Specify target axes A1 and A2
C Convention: A1 will be (1,0,0) in target frame
C             A2 will be (0,1,0) in target frame

      DO JA=1,3
         A1(JA)=0.
         A2(JA)=0.
      ENDDO
      A1(1)=1.
      A2(2)=1.
C               
C Now populate lattice:
      NAT=0
      DO JZ=1,NZ
         DO JY=1,NY
            DO JX=1,NX
               NAT=NAT+1
               IF(NAT.GT.MXNAT)THEN
                  CALL ERRMSG('FATAL','TARREC',' NAT.GT.MXNAT')
               ENDIF
               IXYZ(NAT,1)=JX
               IXYZ(NAT,2)=JY
               IXYZ(NAT,3)=JZ
            ENDDO
         ENDDO
      ENDDO
      IF(NAT.GT.MXNAT)THEN
         CALL ERRMSG('FATAL','TARREC',' NAT.GT.MXNAT ')
      ENDIF

C homogeneous, anisotropic composition:

      DO JX=1,3
         DO JA=1,NAT
            ICOMP(JA,JX)=JX
         ENDDO
      ENDDO

      WRITE(CMSGNM,FMT='(A, I7, A)')
     &      '  Anisotropic rectangular prism of NAT=',NAT,' dipoles'
      IF(IOSHP.GE.0)THEN
         OPEN(UNIT=IOSHP,FILE='target.out',STATUS='UNKNOWN')
         WRITE(IOSHP,FMT=9020)XV,YV,ZV,NAT,A1,A2,DX
         DO JA=1,NAT
            WRITE(IOSHP,FMT=9030)JA,IXYZ(JA,1),IXYZ(JA,2),IXYZ(JA,3),
     &                           ICOMP(JA,1),ICOMP(JA,2),ICOMP(JA,3)
         ENDDO
         CLOSE (UNIT=IOSHP)
      ENDIF
      RETURN
 9020 FORMAT (' >TARREC anisotropic rectangular prism; AX,AY,AZ=',3F8.4,
     &/,I7,' = NAT',/,
     &3F9.4,' = A_1 vector',/,
     &3F9.4,' = A_2 vector',/,
     &3F9.6,' = lattice spacings (d_x,d_y,d_z)/d',/,
     &'     JA  IX  IY  IZ ICOMP(x,y,z)')
 9030 FORMAT(I7,3I4,3I2)
      END
