    SUBROUTINE REASHP(CSHAPE,CFLSHP,IDVOUT,IDVSHP,A1,A2,DX,X0,BETADF,PHIDF, &
                      THETADF,CDESCR,MXNAT,NAT,IXYZ,ICOMP,MXN3,NAT3)
      USE DDPRECISION,ONLY : WP
      IMPLICIT NONE

! Arguments:

      INTEGER :: IDVOUT,IDVSHP,MXNAT,NAT,MXN3,NAT3
      CHARACTER :: CDESCR*67,CFLSHP*(*),CSHAPE*9
      INTEGER*2 ::    &
         ICOMP(MXNAT,3)
      INTEGER ::     &
         IXYZ(MXNAT,3)
      REAL(WP) ::        &
         A1(3),          &
         A2(3),          &
         BETADF(MXNAT),  &
         DX(3),          &
         PHIDF(MXNAT),   &
         THETADF(MXNAT), &
         X0(3)

! Local variables:

      INTEGER :: J,JJ

!***********************************************************************
! Given:
!    CSHAPE = 'FROM_FILE' to read list of occupied sites and compositions
!                         from file -- use this for targets composed of
!                         isotropic materials
!             'ANIFRMFIL' to read list of occupied sites, compositions,
!                         and orientations for anisotropic material from
!                         file -- use this for targets composed of general
!                         isotropic materials
!    CFLSHP = name of (unformatted) file containing target description
!    IDVSHP  = device number to use in reading this file
!    IDVOUT = device number to use for "running" output
!    MXNAT,MXN3 = dimensioning information
! Returns:
!    A1(3)  = target axis 1
!    A2(3)  = target axis 2
!    DX(3)  = dx/d , dy/d , dz/d   [where d=(dx*dy*dz)^{1/3}]
!    X0(3)  = location/d in TF of lattice site IXYZ=0 0 0
!    CDESCR = string describing target
!    NAT    = number of dipoles in target
!    NAT3   = 3*NAT
!    IXYZ(1-NAT,1-3) = lattice coordinates of dipoles
!    ICOMP(1-NAT,1-3)= composition for directions x,y,z (in Target
!                      Frame) of dipoles 1-NAT.
!    BETADF(1-NAT)=orientation angle "beta" (radians) for
!                  "Dielectric Frame" (DF) relative to Target Frame (TF)
!    PHIDF(1-NAT)=orientation angle "phi" (radians) for DF relative to T
!    THETADF(1-NAT)=orientation angle "theta" (radians) for DF relative
!                   to TF

! Note: It is expected that users may wish to modify this routine
!       to be consistent with whatever target generation software they
!       use.  However, as an example (and for test purposes) this routin
!       is supplied with code compatible with the "target.out" files whi
!       are generated by the program "calltarget"

!       Also provided below (commented out) is
!       a module of code appropriate for reading an unformatted
!       "shape.dat" file for a general target, assuming user's
!       target generation routine writes an unformatted file with
!       the same structure.


!***********************************************************************
! History:
! 91.05.02 (BTD): Added IDVOUT to argument list.
! 91.05.23 (BTD): Added A1, A2 to argument list.
! 91.11.11 (BTD): Modified for compatibility with WRITE statements in
!                 program CALLTARGET; Changed dimensioning of ICOMP from
!                 ICOMP(MXN3) to ICOMP(MXNAT,3)
! 93.09.24 (BTD): Modified to include examples of READ statements
!                 for both unformatted and formatted shape.dat files.
!                 Leave the formatted example (compatible with the
!                 "target.out" files generated by "calltarget") in effec
! 95.04.07 (BTD): Changed CDESCR*60 -> CDESCR*67 for compatibility with
!                 calling routine TARGET
! 95.05.15 (BTD): Changed  READ(DVSHP,FMT='(A60)') to ..(A67)..
!                 Added module for inhomogeneous/anisotropic targets
! 96.01.26 (BTD): Replaced IX,IY,IZ by IXYZ
! 98.02.11 (BTD): Modified to read lattice spacing DX(1-3) from
!                 "shape.dat"
! 98.03.09 (BTD): Add DX(3) to argument list
! 01.04.21 (BTD): Copied over changes made to version 5a10 on 00.11.02:
!                 Modified so now will ALWAYS expect ICOMP(x,y,z) to be
!                 included in file shape.dat
! 04.02.18 (BTD): Eliminated READ(IDVSHP,*)DX as we are not using
!                 noncubic lattices.
! 04.09.14 (BTD): Added CSHAPE,BETADF,PHIDF, and THETADF to the
!                 argument list.
!                 Add support for CSHAPE='ANIFIL' to read dipole
!                 locations, composition, and orientation angles
!                 BETADF, PHIDF, THETADF for Dielectric Frame relative
!                 to Target Frame at each occupied site.
! 07.09.11 (BTD): Changed IXYZ from INTEGER*2 to INTEGER
! 08.08.08 (BTD): Changed FRMFIL -> FROM_FILE
!                         ANIFIL -> ANIFRMFIL
! 08.08.31 (BTD): Updated for consistency with new convention for
!                 target.out files
! 08.09.12 (BTD): Removed extra read statement when called with
!                 CSHAPE=FROM_FILE
! end history

! Copyright (C) 1993,1995,1996,1998,2001,2004,2007,2008
!               B.T. Draine and P.J. Flatau
! This code is covered by the GNU General Public License.
!***********************************************************************
      IF(CSHAPE=='FROM_FILE'.OR.CSHAPE=='from_file') THEN

! Enable following module of code if "shape.dat" file has been written
! in formatted form consistent with the "target.out" files generated by
! e.g., tarcel.f (inhomoeneous target, with ICOMP in file)

! Note: it is assumed that arrays THETADF,PHIDF,BETADF are
!       initialized to zero prior to calling this routine.

         OPEN(UNIT=IDVSHP,FILE=CFLSHP,STATUS='OLD',FORM='FORMATTED')
         READ(IDVSHP,FMT='(A67)')CDESCR
         READ(IDVSHP,*)NAT
         READ(IDVSHP,*)A1
         READ(IDVSHP,*)A2
         READ(IDVSHP,*)DX
         READ(IDVSHP,*)X0
         READ(IDVSHP,*)
         DO J=1,NAT
            READ(IDVSHP,*)JJ,IXYZ(J,1),IXYZ(J,2),IXYZ(J,3),ICOMP(J,1), &
                         ICOMP(J,2),ICOMP(J,3)
         ENDDO
         CLOSE(IDVSHP)

      ELSEIF(CSHAPE=='ANIFRMFIL'.OR.CSHAPE=='anifrmfil')THEN

         OPEN(UNIT=IDVSHP,FILE=CFLSHP,STATUS='OLD',FORM='FORMATTED')
         READ(IDVSHP,FMT='(A67)')CDESCR
         READ(IDVSHP,*)NAT
         READ(IDVSHP,*)A1
         READ(IDVSHP,*)A2
         READ(IDVSHP,*)DX
         READ(IDVSHP,*)X0
         READ(IDVSHP,*)
         DO J=1,NAT
            READ(IDVSHP,*)JJ,IXYZ(J,1),IXYZ(J,2),IXYZ(J,3),ICOMP(J,1), &
                 ICOMP(J,2),ICOMP(J,3),THETADF(J),PHIDF(J),BETADF(J)
         ENDDO
         CLOSE(IDVSHP)
      ELSE
         WRITE(IDVOUT,*)'Fatal error in REASHP: ',              &
                        'do not understand target option ',CSHAPE
         STOP
      ENDIF

!***********************************************************************

! Enable following module of code if "shape.dat" file has been written
! in unformatted form consistent with these read statements:

!      INTEGER JX,JY,JZ
!      OPEN(UNIT=IDVSHP,FILE=CFLSHP,STATUS='OLD',FORM='UNFORMATTED')
!      READ(IDVSHP)CDESCR,NAT,A1,A2,DX,X0
!      READ(IDVSHP)((IXYZ(JX,JY),JX=1,NAT),JY=1,3),
!     &            ((ICOMP(J,JJ),J=1,NAT),JJ=1,3),
!     &            (THETADF(J),J=1,NAT),
!     &            (PHIDF(J),J=1,NAT),
!                  (BETADF(J),J=1,NAT)
!      CLOSE(IDVSHP)

!***********************************************************************

      NAT3=3*NAT
      RETURN
    END SUBROUTINE REASHP
