      SUBROUTINE REAPAR(CBINFLAG,CNETFLAG,AEFFA,
     &                  BETAMI,BETAMX,
     &                  CALPHA,CDIEL,CFLEPS,CFLPAR,CFLSHP,CMDSOL,CMDFFT,
     &                  CSHAPE,CMDTRQ,CXE01,CXE02,
     &                  DEGRAD,ETASCA,
     &                  IDVERR,IDVOUT,INIT,IOPAR,IORTH,IWRKSC,
     &                  MXBETA,MXCOMP,MXPHI,MXRAD,MXSCA,MXTHET,MXWAV,
     &                  NBETA,NCOMP,NPHI,NRAD,NSCA,NTHETA,NWAV,
     &                  IWAV0,IRAD0,IORI0,NSMELTS,
     &                  PHIMIN,PHIMAX,PHIN,
     &                  THETAN,THETMI,THETMX,TOL,
     &                  WAVEA,SHPAR,SMIND1,SMIND2,DX)
      IMPLICIT NONE

C Arguments:

      CHARACTER
     &   CALPHA*(*),CBINFLAG*(*),CDIEL*(*),CFLPAR*(*),CFLSHP*80,
     &   CMDSOL*(*),CMDFFT*(*),CMDTRQ*(*),CNETFLAG*(*),CSHAPE*(*)
      INTEGER
     &   IDVERR,IDVOUT,INIT,IOPAR,IORI0,IORTH,IRAD0,IWAV0,
     &   IWRKSC,
     &   MXBETA,MXCOMP,MXPHI,MXRAD,MXSCA,MXTHET,MXWAV,
     &   NBETA,NCOMP,NPHI,NRAD,NSCA,NSMELTS,NWAV,NTHETA
      INTEGER SMIND1(9),SMIND2(9)
      CHARACTER*60 CFLEPS(MXCOMP)
      REAL
     &   BETAMI,BETAMX,DEGRAD,ETASCA,PHIMIN,PHIMAX,THETMI,THETMX,TOL
      REAL
     &   AEFFA(MXRAD),DX(3),PHIN(MXSCA),SHPAR(6),THETAN(MXSCA),
     &   WAVEA(MXWAV)
      COMPLEX CXE01(3),CXE02(3)

C Local variables:

      CHARACTER CDIVID*3,CLINE*70,CMSGNM*70,CWHERE*70
      INTEGER J,NSCA0
      REAL
     &   AEFEND,AEFINI,DELTA,DTHETA,E1,PHI1,THETA1,THETA2,
     &   WAVEND,WAVINI

C External routines:

      EXTERNAL DIVIDE,ERRMSG,WRIMSG

C***********************************************************************
C Subroutine REAPAR handles the reading of input parameters from the
C "ddscat.par" file, as well as elementary processing with those input
C parameters to generate arrays.
C Original version created by P.J.Flatau, Colorado State Univ.
C Modified by B.T.Draine, Princeton Univ. Obs.
C History:
C 90.11.09 (BTD): Modified so as to remove sensitivity to errors
C                 in sign of entered value of DTHETA.
C 90.11.30 (BTD): Added test to compare CMDFFT and MXMEM
C 90.12.14 (BTD): Changed option MKRECT to RCTNGL
C 91.01.03 (BTD): Added MXBETA,MXPHI,MXTHET to argument list
C                 and added checking of NBETA,NPHI,NTHETA
C 91.05.08 (BTD): Added CALPHA to allow choice of prescription
C                 for polarizabilities
C 91.05.08 (BTD): Some rewriting done.
C 91.05.14 (BTD): Added separate options LDRXYZ and LDRAVG
C 91.05.15 (BTD): Added new option LDR000
C 91.05.23 (BTD): Added variable IWRKSC to argument list and
C                 added code to read IWRKSC
C 91.05.30 (BTD): Changed LDRXYZ->LDR100
C                 Added option LDR111
C 91.11.13 (BTD): Now three options: DRAI88, GOBR88, LATTDR
C                 (subroutine ALPHA now allows for directional
C                 dependence for LATTDR option)
C 92.05.14 (BTD): Allow for experimental option LDRISO
C 92.09.21 (BTD): Allow for option ANIELL (ellipsoid of anisotropic
C                 material); change option GRPHIT -> UNICYL (cylinder
C                 of uniaxial material)
C 93.01.07 (BTD): Allow for options TWOELL and TWOAEL (two touching
C                 ellipsoids, each either isotropic or anisotropic)
C 93.03.11 (BTD): Deleted all code associated with variable CMACHN
C                 which previously was used to identify machine/OS
C                 This information was not used or needed.
C 93.12.15 (BTD): Added code to check for compatibility of NCOMP
C                 when CSHAPE=TWOELL,THRELL,TWOAEL,or THRAEL
C 94.01.27 (BTD): Replaced SHPAR1,SHPAR2,SHPAR3 by SHPAR(1-6)
C                 Added CONELL (two concentric ellipsoids) as target
C                 option.
C 94.06.20 (PJF): Added code to handle NEWTMP
C 94.12.02 (BTD): Added options 
C                    LB1991 (Dungey-Bohren 1991 polarizability)
C                    LR1994 (Lumme-Rahola 1994 polarizability)
C 95.05.26 (BTD): Corrected construction of orthogonal polarization
C                 state for case where E01 is complex (e.g.,
C                 circular polarization)
C 95.06.14 (BTD): Added code to check that user is not requesting
C                 f_ml when illuminating target with other then
C                 linearly polarized light
C 95.06.19 (BTD): Added argument CMDSOL to allow selection of
C                 different CCG methods
C                 Added argument CMDTRQ to allow torque calculations
C                 to be skipped.
C 96.01.05 (BTD): Added shape options BLOCKS and DW1996
C 96.01.25 (BTD): Added shape option TWOSPH
C 96.01.25 (BTD): Disable code added 95.06.14 so that user can
C                 calculate f_ml even when illuminating target with
C                 elliptically polarized light.
C 96.10.18 (BTD): Changed option NEWTMP to GPFAFT (Generalized Prime
C                 Factor Algorithm for Fourier Transform).
C 96.11.06 (BTD): Added provision to 
C                 (1) produce fatal error if CXE01(1).NE.(0.,0.)
C                 (2) normalize CXE01 if necessary
C 96.11.14 (PJF): cbinflag for binary file
C 96.11.15 (PJF): netCDF netflag
C 96.11.21 (BTD): modified handling of CBINFLAG and CNETFLAG to
C                 be consistent with other option flags
C 97.11.01 (BTD): add DX to argument list
C                 read DX from ddscat.par
C                 renormalize DX (input DX not required to be
C                 normalized with DX(1)*DX(2)*DX(3)=1.)
C 98.12.06 (BTD): modified to read NSMELTS,SMELTS from ddscat.par
C                 and pass NSMELTS,SMIND1,SMIND2 as arguments to calling
C                 program DDSCAT
C 98.12.21 (BTD): changed dimension of CFLPAR from CHARACTER*40 to
C                 CHARACTER*60 to allow longer file names
C                 (also changed in DDSCAT.f and dielec.f)
C 98.12.29 (BTD): modified to support new option LYRSLB
C 99.01.26 (BTD): modified to allow first IWAV,IRAD,IORI to be
C                 specified in ddscat.par (useful for restarting
C                 jobs which did not complete)
C 99.06.29 (BTD): corrected input of SHPAR for option TWOSPH
C                 previously had input only SHPAR(1-5),
C                 now input SHPAR(1-6)
C 00.06.12 (BTD): modified to support NSPHER option
C 00.06.23 (BTD): modified to support FFTWFJ option
C                 remove MXMEM from argument list
C                 since no longer supporting TMPRTN FFT option which
C                 required additional memory
C 00.07.05 (BTD): modified to print more information about CMDFFT
C                 option
C 02.02.12 (BTD): modified to support PRISM3 option
C 03.01.29 (BTD): modified to force DX(1-3)=1.
C 03.05.21 (BTD): modified to support SPHARM option
C 03.05.22 (BTD): bug fix re SPHARM option
C 03.07.13 (BTD): changed FFTWFJ to FFTW21 in anticipation of future
C                 option FFTW3x (FFTW 2.1.x and FFTW 3.0.1 have 
C                 different interfaces, so they must be distinguished).
C 03.10.23 (BTD): removed ICTHM and IPHIM from argument list,
C                 removed code to read these values from ddscat.par
C 03.10.24 (BTD): Add parameter ETASCA to allow user to control
C                 angular resolution used for scattering calculations.
c                 Add checks against invalid input
c 04.02.26 (BTD): Modify to accept polarizability option 'GKDLDR'
c                 for Gutkowicz-Krusin & Draine (2004) lattice
c                 dispersion relation.
c 04.04.01 (BTD): Modify to support option NPSPBC (multi-sphere cluster
c                 with periodic boundary conditions)
C 04.04.05 (BTD): Remove SPHARM option: GSPHER is sufficient
C 04.04.29 (BTD): Added ANIREC option for homogeneous anisotropic 
c                 rectangular target
c 04.05.23 (BTD): Added CWHERE string to provide error information
c                 in case of malformed ddscat.par file.
c 05.03.19 (BTD): Added 2 lines to set CLFEPS(1) in case using options
c                 H2OLIQ or H2OICE (previously were left undefined,
c                 which cause problems with output statement)
c 05.10.18 (BTD): Added check to ensure that NBETA*NTHETA*NPHI < 1001
c                 if IWRKSC=1 (since current version of NAMER is limited
c                 to maximum of 1000 directions)
C end history
C Copyright (C) 1993,1994,1995,1996,1997,1998,1999,2000,2002,2003,2004,
C               2005
C               B.T. Draine and P.J. Flatau
C This code is covered by the GNU General Public License.

C***********************************************************************
C
      OPEN (UNIT=IOPAR,FILE=CFLPAR,STATUS='OLD')
C
      CWHERE='error reading line 1 of ddscat.par'
      READ(IOPAR,FMT=*,ERR=40)CLINE
      CALL WRIMSG('REAPAR',CLINE)
      CWHERE='error reading line 2 of ddscat.par'
      READ(IOPAR,FMT=*,ERR=40)CLINE
      CALL WRIMSG(' ',CLINE)

C    Specify whether torques are to be calculated:
C
C    CMDTRQ*6 = 'DOTORQ' -  calculate torque on grain
C             = 'NOTORQ' -  do not calculate torque on grain

      CWHERE='error reading CMDTRQ from ddscat.par'
      READ(IOPAR,FMT=*,ERR=40)CMDTRQ
      IF(CMDTRQ.EQ.'dotorq')CMDTRQ='DOTORQ'
      IF(CMDTRQ.EQ.'notorq')CMDTRQ='NOTORQ'
      IF(CMDTRQ.EQ.'DOTORQ')THEN
         WRITE(CMSGNM,FMT='(A,A)')CMDTRQ,' - compute torques '
         CALL WRIMSG('REAPAR',CMSGNM)
      ELSEIF(CMDTRQ.EQ.'NOTORQ')THEN
         WRITE(CMSGNM,FMT='(A,A)')CMDTRQ,' - do not compute torques '
         CALL WRIMSG('REAPAR',CMSGNM)
      ELSE
         CALL ERRMSG('FATAL','REAPAR',' wrong definition of CMDTRQ')
      ENDIF

C    Define method used for iterative solution of complex linear equations
C
C    CMDSOL*6 = 'PETRKP' -  Petravic & Kuo-Petravic method
C             = 'PBCGST' -  PIM BiConjugate Gradient with Stabilization

      CWHERE='error reading CMDSOL from ddscat.par'
      READ(IOPAR,FMT=*,ERR=40)CMDSOL
      IF(CMDSOL.EQ.'petrkp')CMDSOL='PETRKP'
      IF(CMDSOL.EQ.'pbcgst')CMDSOL='PBCGST'
      IF(CMDSOL.EQ.'PETRKP'.OR.
     &   CMDSOL.EQ.'PBCGST')THEN
         WRITE(CMSGNM,FMT='(A,A)')CMDSOL,' - CCG Method  '
         CALL WRIMSG('REAPAR',CMSGNM)
      ELSE
         CALL ERRMSG('FATAL','REAPAR',' wrong definition of CMDSOL')
      ENDIF

C    Define FFT method:

C    CMDFFT*6 = 'BRENNR' -  Use Brenner's FOURX FFT routine [no longer used]
C             = 'TMPRTN' -  Use Temperton's FFT routine [no longer used]
C             = 'GPFAFT' -  GPFA code of Temperton
C             = 'FFTW21' -  FFTW 2.1.x code of Frigo & Johnson
C             = 'CONVEX' -  Use native Convex veclib routine C3DFFT.

      CWHERE='error reading CMDFFT from ddscat.par'
      READ(IOPAR,FMT=*,ERR=40)CMDFFT
      IF(CMDFFT.EQ.'CONVEX')THEN
         WRITE(CMSGNM,FMT='(A,A,A)')CMDFFT,' - using native Convex ',
     &                    '3d FFT'
         CALL WRIMSG('REAPAR',CMSGNM)
      ELSEIF(CMDFFT.EQ.'FFTW21')THEN
         WRITE(CMSGNM,FMT='(A,A,A)')CMDFFT,' - using FFTW 2.1.x ',
     &                    'package from Frigo & Johnson'
         CALL WRIMSG('REAPAR',CMSGNM)
      ELSEIF(CMDFFT.EQ.'GPFAFT')THEN
         WRITE(CMSGNM,FMT='(A,A,A)')CMDFFT,' - using GPFA package ',
     &                    'from Clive Temperton'
         CALL WRIMSG('REAPAR',CMSGNM)
      ELSE
         WRITE(CMSGNM,FMT='(A,A)')'DDSCAT6.1 only supports FFT options',
     &      ' FFTW21, GPFAFT, and CONVEX'
         CALL WRIMSG('REAPAR',CMSGNM)
         CALL ERRMSG('FATAL','REAPAR',' wrong definition of CMDFFT')
      ENDIF

C***********************************************************************

C    Define prescription for computing polarizabilities:

c    CALPHA*6 = 'LATTDR' - Lattice Dispersion Relation of
c                          Draine & Goodman (1993)
c               'GKDLDR' - Lattice Dispersion Relation of
c                          Gutkowicz-Krusin & Draine (2004)

      CWHERE='error reading CALPHA from ddscat.par'
      READ(IOPAR,FMT=*,ERR=40)CALPHA
      IF(CALPHA.EQ.'LATTDR')THEN
         CALL WRIMSG('REAPAR',
     &      'LATTDR - Draine & Goodman (1993) LDR for alpha')
      ELSEIF(CALPHA.EQ.'GKDLDR')THEN
         CALL WRIMSG('REAPAR',
     &      'GKDLDR - Gutkowicz-Krusin & Draine (2004) LDR for  alpha')
      ELSE
         CALL ERRMSG('FATAL','REAPAR',' wrong definition of CALPHA')
      ENDIF

C binary file flag

      CWHERE='error reading CBINFLAG from ddscat.par'
      READ(IOPAR,FMT=*,ERR=40)CBINFLAG
      IF(CBINFLAG.EQ.'allbin')CBINFLAG='ALLBIN'
      IF(CBINFLAG.EQ.'oribin')CBINFLAG='ORIBIN'
      IF(CBINFLAG.EQ.'notbin')CBINFLAG='NOTBIN'
      IF(CBINFLAG.EQ.'ALLBIN'
     &   .OR.CBINFLAG.EQ.'ORIBIN'
     &   .OR.CBINFLAG.EQ.'NOTBIN')THEN
         WRITE(CMSGNM,FMT='(A,A)')CBINFLAG,
     &                            ' - Unformatted binary dump option'
         CALL WRIMSG('REAPAR',CMSGNM)
      ELSE
         CALL ERRMSG('FATAL','REAPAR',' Wrong definition of CBINFLAG')
      ENDIF

C NETCDF file flag

      CWHERE='error reading CNETFLAG from ddscat.par'
      READ(IOPAR,FMT=*,ERR=40)CNETFLAG
      IF(CNETFLAG.EQ.'allcdf')CNETFLAG='ALLCDF'
      IF(CNETFLAG.EQ.'oricdf')CNETFLAG='ORICDF'
      IF(CNETFLAG.EQ.'notcdf')CNETFLAG='NOTCDF'
      IF(CNETFLAG.EQ.'ALLCDF'  
     &   .OR.CNETFLAG.EQ.'ORICDF' 
     &   .OR.CNETFLAG.EQ.'NOTCDF')THEN
         WRITE(CMSGNM,FMT='(A,A)')CNETFLAG,
     &                            ' - netCDF file option'
         CALL WRIMSG('REAPAR',CMSGNM)
      ELSE
         CALL ERRMSG('FATAL','REAPAR',' Wrong definition of CNETFLAG')
      ENDIF

C***********************************************************************

C    Define shape:
C
C    CSHAPE*6 = 'FRMFIL'     read shape data from file
C             = 'RCTNGL'     make rectangular solid
C             = 'ELLIPS'     make ellipsoidal solid
C             = 'CYLNDR'     make cylindrical solid
c             = 'CYLCAP'     make cylinder with hemispherical end-caps
C             = 'HEXGON'     make hexagonal prism
C             = 'TETRAH'     make regular tetrahedron
C             = 'UNICYL'     make cylinder of unixaxial material
C             = 'ANIELL'     make ellipsoid of anisotropic material
C             = 'TWOELL'     make two touching ellipsoids of materials 1,2
C             = 'TWOAEL'     make two touching anisotropic ellipsoids of
C                            materials 1-6
C             = 'THRELL'     make three touching isotropic ellipsoids of
C                            materials 1,2,3
C             = 'THRAEL'     make three touching anisotropic ellipsoids of
C                            materials 1-9
C             = 'CONELL'     make two concentric ellipsoids of
C                            materials 1,2
C             = 'BLOCKS'     make target out of cubic blocks defined by
C                            data in file 'blocks.par'
C             = 'DW1996'     make 13-cube target used by Draine &
C                            Weingartner 1996
C             = 'TWOSPH'     make two touching spheroids with symmetry axes at
C                            specified angle
C             = 'LYRSLB'     make layered slab target, with up to 4 separate
C                            layers
C             = 'NSPHER'     make multisphere target = union of volumes of
C                            N spheres
C             = 'PRISM3'     make triangular prism target
C             = 'GSPHER'     make gaussian sphere target
C             = 'NSPPBC'     make multisphere target with periodic
C                            boundary conditions
C             = 'ANIREC'     make homogeneous anisotropic rectangular 
C                            target

      CWHERE='error reading CSHAPE from ddscat.par'
      READ(IOPAR,FMT=*,ERR=40)CSHAPE
      IF(    CSHAPE.EQ.'FRMFIL'.OR.CSHAPE.EQ.'RCTNGL'
     &   .OR.CSHAPE.EQ.'ELLIPS'.OR.CSHAPE.EQ.'CYLNDR'
     &   .OR.CSHAPE.EQ.'HEXGON'.OR.CSHAPE.EQ.'TETRAH'
     &   .OR.CSHAPE.EQ.'UNICYL'.OR.CSHAPE.EQ.'ANIELL'
     &   .OR.CSHAPE.EQ.'TWOELL'.OR.CSHAPE.EQ.'TWOAEL'
     &   .OR.CSHAPE.EQ.'THRELL'.OR.CSHAPE.EQ.'THRAEL'
     &   .OR.CSHAPE.EQ.'CONELL'.OR.CSHAPE.EQ.'BLOCKS'
     &   .OR.CSHAPE.EQ.'DW1996'.OR.CSHAPE.EQ.'LYRSLB'
     &   .OR.CSHAPE.EQ.'TWOSPH'.OR.CSHAPE.EQ.'NSPHER'
     &   .OR.CSHAPE.EQ.'PRISM3'.OR.CSHAPE.EQ.'GSPHER'
     &   .OR.CSHAPE.EQ.'NSPPBC'.OR.CSHAPE.EQ.'ANIREC'
     &   .OR.CSHAPE.EQ.'CYLCAP')THEN

         WRITE(CMSGNM,FMT='(A,A)')CSHAPE,' - Shape definition '
         CALL WRIMSG('REAPAR',CMSGNM)
      ELSE
         CALL ERRMSG('FATAL','REAPAR',' Wrong shape directive ')
      ENDIF

C Read shape parameters
C Need 3 shape parameters for all options except 
C      FRMFIL (0 shape parameters: 
C                here skip a line, later read target from file)
C      TETRAH, DW1996 (1 shape parameter)
C      CYLNDR, CYLCAP, HEXGON, UNICYL (2 shape parameters)
C      TWOSPH (6 shape parameters)
C      CONELL (6 shape parameters)
C      LYRSLB (6 shape parameters)
C      NSPHER (2 shape parameters + 1 filename)
C      PRISM3 (4 shape parameters)
c      GSPHER (6 shape parameters)

      IF(CSHAPE.EQ.'FRMFIL')THEN
         READ(IOPAR,FMT=*,ERR=40)

c targets needing 1 shape parameter:

      ELSEIF(
     &   CSHAPE.EQ.'TETRAH'.OR.
     &   CSHAPE.EQ.'DW1996')THEN
         CWHERE='error reading 1 shape parameter in ddscat.par'
         READ(IOPAR,FMT=*,ERR=40)SHPAR(1)

c targets needing 2 shape parameters:

      ELSEIF(
     &   CSHAPE.EQ.'CYLNDR'.OR.
     &   CSHAPE.EQ.'CYLCAP'.OR.
     &   CSHAPE.EQ.'HEXGON'.OR.
     &   CSHAPE.EQ.'UNICYL')THEN
         CWHERE='error reading 2 shape parameters in ddscat.par'
         READ(IOPAR,FMT=*,ERR=40)SHPAR(1),SHPAR(2)

c targets needing 4 shape parameters

      ELSEIF(CSHAPE.EQ.'PRISM3')THEN
         CWHERE='error reading 4 shape parameters in ddscat.par'
         READ(IOPAR,FMT=*,ERR=40)SHPAR(1),SHPAR(2),SHPAR(3),SHPAR(4)

c targets needing 6 shape parameters:

      ELSEIF(
     &   CSHAPE.EQ.'TWOSPH'.OR.
     &   CSHAPE.EQ.'CONELL'.OR.
     &   CSHAPE.EQ.'LYRSLB'.OR.
     &   CSHAPE.EQ.'GSPHER')THEN
         CWHERE='error reading 6 shape parameters in ddscat.par'
         READ(IOPAR,FMT=*,ERR=40)SHPAR(1),SHPAR(2),SHPAR(3),
     &                           SHPAR(4),SHPAR(5),SHPAR(6)

c targets needing 2 shape parameters and file name:

      ELSEIF(CSHAPE.EQ.'NSPHER')THEN
         CWHERE=
     &   'error reading 2 shape parameters and filename in ddscat.par'
         READ(IOPAR,FMT=*,ERR=40)SHPAR(1),SHPAR(2),CFLSHP

c targets needing 3 shape parameters and file name:

      ELSEIF(CSHAPE.EQ.'NSPPBC')THEN
         CWHERE=
     &   'error reading 3 shape parameters and filename in ddscat.par'
         READ(IOPAR,FMT=*,ERR=40)SHPAR(1),SHPAR(2),SHPAR(3),CFLSHP

c targets needing 3 shape parameters:

      ELSE
         CWHERE='error reading 3 shape parameters in ddscat.par'
         READ(IOPAR,FMT=*,ERR=40)SHPAR(1),SHPAR(2),SHPAR(3)
      ENDIF

C***********************************************************************
C following code disabled 03.01.29
C (retain for use in future noncubic version)
C
C   Obtain lattice anisotropy parameters DX(1-3)
C   For cubic lattice, DX(1)=DX(2)=DX(3)
C   Note that we do not require here that DX(1)*DX(2)*DX(3)=1 upon
C   input; DX is renormalized here before being returned to DDSCAT
C

C      READ(IOPAR,FMT=*,ERR=40)DX(1),DX(2),DX(3)
C      WRITE(CMSGNM,FMT='(F8.3,F8.3,F8.3,A)')DX,
C     &   ' = relative lattice spacings dx,dy,dz'
C      CALL WRIMSG('REAPAR',CMSGNM)

C      DELTA=(DX(1)*DX(2)*DX(3))**(1./3.)
C      DX(1)=DX(1)/DELTA
C      DX(2)=DX(2)/DELTA
C      DX(3)=DX(3)/DELTA
C      WRITE(CMSGNM,FMT='(F8.3,F8.3,F8.3,A)')DX,
C     &   ' = normalized lattice spacings dx,dy,dz'
C      CALL WRIMSG('REAPAR',CMSGNM)

C and replaced by following:

      DX(1)=1.
      DX(2)=1.
      DX(3)=1.

C-----------------------------------------------------------------------
C***********************************************************************

C   Obtain names of file(s) containing dielectric function(s)
C   NCOMP = number of different dielectric functions
C   CFLEPS(1-NCOMP) = names of files containing dielectric functions
C
      CWHERE='error reading NCOMP in ddscat.par'
      READ(IOPAR,FMT=*,ERR=40)NCOMP

C Check that MXCOMP is large enough 

      IF(NCOMP.GT.MXCOMP)
     &   CALL ERRMSG('FATAL','REAPAR',
     &   'NCOMP > MXCOMP: must increase MXCOMP in DDSCAT')

C*** Check that NCOMP=2 if CSHAPE=UNICYL *******************************
C                     3           ANIELL
C                     2           TWOELL
C                     3           THRELL
C                     6           TWOAEL
C                     9           THRAEL
C                     2           CONELL
C                     2           TWOSPH
C                     2-4         LYRSLB

      IF(CSHAPE.EQ.'UNICYL'.AND.NCOMP.NE.2)
     &   CALL ERRMSG('FATAL','REAPAR',
     &   'NCOMP must be 2 for option UNICYL')
      IF(CSHAPE.EQ.'ANIELL'.AND.NCOMP.NE.3)
     &   CALL ERRMSG('FATAL','REAPAR',
     &   'NCOMP must be 3 for option ANIELL')
      IF(CSHAPE.EQ.'TWOELL'.AND.NCOMP.NE.2)
     &   CALL ERRMSG('FATAL','REAPAR',
     &   'NCOMP must be 2 for option TWOELL')
      IF(CSHAPE.EQ.'THRELL'.AND.NCOMP.NE.3)
     &   CALL ERRMSG('FATAL','REAPAR',
     &   'NCOMP must be 3 for option THRELL')
      IF(CSHAPE.EQ.'TWOAEL'.AND.NCOMP.NE.6)
     &   CALL ERRMSG('FATAL','REAPAR',
     &   'NCOMP must be 6 for option TWOAEL')
      IF(CSHAPE.EQ.'THRAEL'.AND.NCOMP.NE.9)
     &   CALL ERRMSG('FATAL','REAPAR',
     &   'NCOMP must be 9 for option THRAEL')
      IF(CSHAPE.EQ.'CONELL'.AND.NCOMP.NE.2)
     &   CALL ERRMSG('FATAL','REAPAR',
     &   'NCOMP must be 2 for option CONELL')
      IF(CSHAPE.EQ.'TWOSPH'.AND.NCOMP.NE.2)
     &   CALL ERRMSG('FATAL','REAPAR',
     &   'NCOMP must be 2 for option TWOSPH')
      IF(CSHAPE.EQ.'LYRSLB')THEN
         IF(SHPAR(5).LE.0.)THEN
            J=2
         ELSE
            IF(SHPAR(6).LE.0.)THEN
               J=3
            ELSE
               J=4
            ENDIF
         ENDIF
         IF(J.NE.NCOMP)
     &      CALL ERRMSG('FATAL','REAPAR',
     &      'option LYRSLB; NCOMP incompatible with SHPAR(4-6)')
      ENDIF

C***********************************************************************

      READ(IOPAR,FMT=*,ERR=40)CDIEL
      IF(CDIEL.EQ.'TABLES')THEN
         CWHERE='error reading filename(s) for diel.fn. in ddscat.par'
         DO J=1,NCOMP
             READ(IOPAR,FMT=*,ERR=40)CFLEPS(J)
         ENDDO
      ELSEIF(CDIEL.EQ.'H2OICE'.OR.CDIEL.EQ.'H2OLIQ')THEN
         IF(NCOMP.GT.1)CALL ERRMSG('FATAL','REAPAR',
     &                              'H2OICE/LIQ with NCOMP>1?')
         IF(CDIEL.EQ.'H2OICE')CFLEPS(1)=
     &      'Using option H2OICE: H2O ice at T=250K'
         IF(CDIEL.EQ.'H2OLIQ')CFLEPS(1)=
     &      'Using option H2OLIQ: liquid H2O at T=280K'
      ELSE
         CALL ERRMSG('FATAL','REAPAR','Unrecognized option for CDIEL')
      ENDIF

C***********************************************************************

C    Define INITIALIZATION:

C    INIT = 0 to start with |X0>=0
C           1 to obtain |X0> from 4th-order expansion in polarizability
C           2 to read |X0> from file solvp.in

      READ(IOPAR,FMT=*,ERR=40)CLINE
      CALL WRIMSG(' ',CLINE)
      CWHERE='error reading INIT in ddscat.par'
      READ(IOPAR,FMT=*,ERR=40)INIT
      IF(INIT.EQ.0)THEN
         CALL WRIMSG('REAPAR',
     &               'INIT=0 to start with |X0>=0 (CCG method)')
      ELSEIF(INIT.EQ.1)THEN
         CALL WRIMSG('REAPAR',
     &         'INIT=1 to obtain |X0> from 4th-order expansion (CCG)')
      ELSEIF(INIT.EQ.2)THEN
         CALL WRIMSG('REAPAR',
     &        'INIT=2 to read |X0> from file solvp.in')
      ELSE
         CALL ERRMSG('FATAL','REAPAR',' Wrong value of INIT')
      ENDIF

C***********************************************************************

C    Define error tolerance:
C    TOL= maximum acceptable value of |Ax-E|/|E|

      CWHERE='error reading TOL in ddscat.par'
      READ(IOPAR,FMT=*,ERR=40)TOL
      IF(TOL.GT.1.E-15.AND.TOL.LT.1.E-1)THEN
         WRITE(CMSGNM,FMT='(1PE10.3,A)')TOL,
     &         ' = TOL = max. acceptable normalized residue |Ax-E|/|E|'
         CALL WRIMSG('REAPAR',CMSGNM)
      ELSE
         CALL ERRMSG('WARNING','REAPAR',' Strange value of TOL ')
      ENDIF

C***********************************************************************

C    Define angular resolution used for calculating radiation force,
C    <cos>, <cos^2>, and radiation torque (if CMTORQ='DOTORQ')

c     ETASCA = parameter controlling number of scattering angles
c            = 1 is a good choice, but can reduce this if higher
c                accuracy is required for angular averages

c     number of scattering angles will be approximately
c            6*[(3+x)/ETASCA]^2

      READ(IOPAR,FMT=*,ERR=40)CLINE
      CALL WRIMSG(' ',CLINE)
      CWHERE='error reading ETASCA in ddscat.par'
      READ(IOPAR,FMT=*,ERR=40)ETASCA
      IF(ETASCA.GT.1.E-3)THEN
         WRITE(CMSGNM,FMT='(F7.4,A)')ETASCA,
     &    ' = ETASCA (parameter controlling number of scattering angles'
         CALL WRIMSG('REAPAR',CMSGNM)
      ELSE
         WRITE(CMSGNM,FMT='(1PE10.3,A)')ETASCA,
     &    ' is not an appropriate value for ETASCA: check ddscat.par'
         CALL ERRMSG('FATAL','REAPAR',CMSGNM)
      ENDIF

C***********************************************************************

C    Define WAVELENGTH :

C    WAVINI = first wavelength (physical units)
C    WAVEND = last wavelength (physical units)
C    NWAV   = number of wavelengths
C    CDIVID = 'LIN', 'LOG', or 'INV' for equal increments in
C             wavelength, log(wavelength), or frequency

      READ(IOPAR,FMT=*,ERR=40)CLINE
      CALL WRIMSG(' ',CLINE)
      CWHERE=
     &   'error reading firstwave, lastwave, number, how in ddscat.par'
      READ(IOPAR,FMT=*,ERR=40)WAVINI,WAVEND,NWAV,CDIVID
      IF(CDIVID.NE.'LIN'.AND.
     &   CDIVID.NE.'LOG'.AND.
     &   CDIVID.NE.'INV')THEN
         CALL ERRMSG('FATAL','REAPAR',
     &               ' CDIVID for wavelengths must be LIN,LOG, or INV')
      ENDIF
      WRITE(CMSGNM,FMT='(I3,A,F7.4,A,F7.4)')
     &      NWAV,' wavelengths from ',WAVINI,' to ',WAVEND
      CALL WRIMSG('REAPAR',CMSGNM)
      CALL DIVIDE(CDIVID,WAVINI,WAVEND,NWAV,MXWAV,WAVEA)

C***********************************************************************

C    Define AEFF = radius of equal-volume sphere (physical units)

      READ(IOPAR,FMT=*,ERR=40)CLINE
      CALL WRIMSG(' ',CLINE)
      CWHERE=
     &   'error reading firstaeff, lastaeff, number, how in ddscat.par'
      READ(IOPAR,FMT=*)AEFINI,AEFEND,NRAD,CDIVID
      IF(CDIVID.NE.'LIN'.AND.
     &   CDIVID.NE.'LOG'.AND.
     &   CDIVID.NE.'INV')THEN
         CALL ERRMSG('FATAL','REAPAR',
     &               ' CDIVID for radii must be LIN,LOG, or INV')
      ENDIF
      WRITE(CMSGNM,FMT='(I3,A,F7.4,A,F7.4)')
     &      NRAD,' eff. radii from ',AEFINI,' to ',AEFEND
      CALL WRIMSG('REAPAR',CMSGNM)
      CALL DIVIDE(CDIVID,AEFINI,AEFEND,NRAD,MXRAD,AEFFA)

C***********************************************************************

C    Define incident polarizations (in Lab Frame)
C    It is assumed that incident radiation is along x axis in Lab Frame

      READ(IOPAR,FMT=*,ERR=40)CLINE
      CALL WRIMSG(' ',CLINE)

C Read complex polarization vector CXE01=e01 (normalize if necessary).

      CWHERE='error reading CXE01 in ddscat.par'
      READ(IOPAR,FMT=*,ERR=40)CXE01
      E1=REAL(SQRT(CXE01(1)*CONJG(CXE01(1))))
      IF(E1.NE.0.)THEN
         CALL ERRMSG('FATAL','REAPAR',' CXE01(1) must be zero!')
      ENDIF
C
C Normalize:
C
      E1=SQRT(REAL(CXE01(2)*CONJG(CXE01(2))+CXE01(3)*CONJG(CXE01(3))))
      CXE01(2)=CXE01(2)/E1
      CXE01(3)=CXE01(3)/E1
C
C Construct orthogonal normalized polarization vector CXE02=e02
C using xhat cross e01* = e02
C
      CXE02(1)=(0.,0.)
      CXE02(2)=-CONJG(CXE01(3))
      CXE02(3)=CONJG(CXE01(2))
C
C IORTH=1 to calculate only for single polarization
C      =2 to also calculate for orthogonal polarization
C
      READ(IOPAR,FMT=*,ERR=40)IORTH
C
C***********************************************************************

C    Specify whether or not to write ".sca" files

C IWRKSC=0 to NOT write ".sca" file for each target orientation
C       =1 to write ".sca" file for each target orientation

      CWHERE='error reading IWRKSC in ddscat.par'
      READ(IOPAR,FMT=*,ERR=40)IWRKSC

C***********************************************************************

C    Read information determining target rotations

      READ(IOPAR,FMT=*,ERR=40)CLINE
      CALL WRIMSG(' ',CLINE)
      CWHERE='error reading BETAMI,BETAMX,NBETA in ddscat.par'
      READ(IOPAR,FMT=*)BETAMI,BETAMX,NBETA
      CWHERE='error reading THETMI,THETMX,NTHETA in ddscat.par'
      READ(IOPAR,FMT=*)THETMI,THETMX,NTHETA
      CWHERE='error reading PHIMIN,PHIMAX,NPHI in ddscat.par'
      READ(IOPAR,FMT=*)PHIMIN,PHIMAX,NPHI

c check that user has not requested more than 1000 orientations and
c IWRKSC=1

      IF(IWRKSC.GT.0.AND.(NBETA*NTHETA*NPHI).GT.1000)THEN
         CALL ERRMSG('FATAL','REAPAR',
     &   'error: if IWRKSC=1, NBETA*NTHETA*NPHI must be .le. 1000')
      ENDIF

      READ(IOPAR,FMT=*,ERR=40)CLINE
      CALL WRIMSG(' ',CLINE)
      CWHERE='error reading IWAV0,IRAD0,IORI0 in ddscat.par'
      READ(IOPAR,FMT=*)IWAV0,IRAD0,IORI0

C*** Check that values of NBETA,NTHETA,NPHI are OK:

      IF(NBETA.GT.MXBETA)
     &   CALL ERRMSG('FATAL','REAPAR',' NBETA>MXBETA: must recompile')
      IF(NPHI.GT.MXPHI)
     &   CALL ERRMSG('FATAL','REAPAR',' NPHI>MXPHI: must recompile')
      IF(NTHETA.GT.MXTHET)
     &   CALL ERRMSG('FATAL','REAPAR',' NTHETA>MXTHET: must recompile')

C***  Check that IWAV0,IRAD0,IORI0 are OK:

      IF(IWAV0+1.GT.NWAV)
     &   CALL ERRMSG('FATAL','REAPAR','IWAV0+1 > NWAV')
      IF(IRAD0+1.GT.NRAD)
     &   CALL ERRMSG('FATAL','REAPAR','IRAD0+1 > NRAD')
      IF(IORI0+1.GT.NBETA*NTHETA*NPHI)
     &   CALL ERRMSG('FATAL','REAPAR','IORI0+1 > NBETA*NTHETA*NPHI')

C If NPHI>1, then set IORTH=2 regardless of value input.

      IF(IORTH.EQ.1.AND.NPHI.GT.1)THEN
         IORTH=2
         CALL WRIMSG('REAPAR',
     &               'Set IORTH=2 since NPHI>1 ')
      ENDIF
C
      IF(IORTH.EQ.1)THEN
         CALL WRIMSG('REAPAR',
     &               'Calculate only for single polarization ')
      ELSEIF(IORTH.EQ.2)THEN
         CALL WRIMSG('REAPAR',
     &               'Do orthogonal polarization for each case ')
      ELSE
         CALL ERRMSG('FATAL','REAPAR',' WRONG VALUE OF IORTH ')
      ENDIF
      WRITE(CMSGNM,FMT='(2F7.2,A,I4)')BETAMI,BETAMX,
     &  ' Range of BETA values ; NBETA =',NBETA
      CALL WRIMSG('REAPAR',CMSGNM)
      WRITE(CMSGNM,FMT='(2F7.2,A,I4)')THETMI,THETMX,
     &  ' Range of THETA values; NTHETA=',NTHETA
      CALL WRIMSG('REAPAR',CMSGNM)
      WRITE(CMSGNM,FMT='(2F7.2,A,I4)')PHIMIN,PHIMAX,
     &  ' Range of PHI values ;   NPHI =',NPHI
      CALL WRIMSG('REAPAR',CMSGNM)

C Convert from degrees to radians

      BETAMI=BETAMI/DEGRAD
      BETAMX=BETAMX/DEGRAD
      THETMI=THETMI/DEGRAD
      THETMX=THETMX/DEGRAD
      PHIMIN=PHIMIN/DEGRAD
      PHIMAX=PHIMAX/DEGRAD

C***********************************************************************

C    Specify elements of scattering matrix to be printed out

      READ(IOPAR,FMT=*,ERR=40)CLINE
      CALL WRIMSG(' ',CLINE)
      CWHERE='error reading NSMELTS in ddscat.par'
      READ(IOPAR,FMT=*,ERR=40)NSMELTS

C NSMELTS = number of elements of scattering matrix element to be
C           printed out.  Must be no greater than 9
C           if NSMELTS is zero, then 6 "default" elements are output

      IF(NSMELTS.GT.9)THEN
         WRITE(IDVOUT,FMT='(A,I2,A)')'Error:',NSMELTS,
     &      ' > 9 is not permitted by output routines'
         CALL ERRMSG('FATAL','REAPAR',
     &               'NSMELTS > 9 is not allowed in ddscat.par')
      ENDIF
      IF(NSMELTS.LE.0)THEN
         SMIND1(1)=11
         SMIND1(2)=21
         SMIND1(3)=31
         SMIND1(4)=41
         SMIND1(5)=12
         SMIND1(6)=13
         NSMELTS=6
         READ(IOPAR,FMT=*)
      ELSE
         CWHERE=
     &      'error reading indices ij of S_ij elements in ddscat.par'
         READ(IOPAR,FMT=*,ERR=40)(SMIND1(J),J=1,NSMELTS)
      ENDIF
      DO J=1,NSMELTS
         SMIND2(J)=SMIND1(J)-10*(SMIND1(J)/10)
         SMIND1(J)=SMIND1(J)/10
      ENDDO
      
C***********************************************************************

C    Specify scattering directions to be calculated
C    THETAN,PHIN = Direction of n from n0 in calculation of the
C                  scattering matrix; cos(THETAN) is n0 \dot n
C                  and PHIN is azimuthal angle of n from Lab xy plane
C                  Note: THETA1, THETA2, and PHI for each scattering
C                  plane are entered in degrees, and immediately 
C                  converted to radians.  Arbitrary number
C                  of scattering planes may be considered.

      NSCA=0
      READ(IOPAR,FMT=*,ERR=40)CLINE
      CALL WRIMSG(' ',CLINE)
      CWHERE='error reading scattering directions in ddscat.par'
 22   READ(IOPAR,FMT=*,ERR=40,END=25)PHI1,THETA1,THETA2,DTHETA
      WRITE(CMSGNM,FMT='(3F7.1,A)')PHI1,THETA1,THETA2,
     &  ' = phi, theta_min, theta_max for scattering plane'
      CALL WRIMSG('REAPAR',CMSGNM)
C
C Convert to radians:
C
      PHI1=PHI1/DEGRAD
      THETA1=THETA1/DEGRAD
      THETA2=THETA2/DEGRAD
      DTHETA=DTHETA/DEGRAD
C
C Allow for possibility that user did not enter correct sign for
C DTHETA (if I did it, others will too...)
C
      IF(THETA2.LT.THETA1)DTHETA=-ABS(DTHETA)
C
C compute theta values for this scattering plane
C
      NSCA0=1
      DELTA=THETA2-THETA1
      IF(DELTA.NE.0)NSCA0=1+INT(DELTA/DTHETA+0.5)
C
C Check that there is enough storage space:
C
      IF((NSCA+NSCA0).GT.MXSCA)THEN
         WRITE(IDVOUT,FMT='(I6,I6,A)')MXSCA,NSCA0,
     &         ' =MXSCA,NSCA0 (dirs in this scatt. plane)'
         CALL ERRMSG('FATAL','REAPAR',
     &               'MXSCA too small in DDSCAT')
      ENDIF
      THETAN(NSCA+1)=THETA1
      IF(NSCA0.GT.2)THEN
         DO J=2,NSCA0-1
            THETAN(NSCA+J)=THETA1+(J-1)*DTHETA
         ENDDO
      ENDIF
      THETAN(NSCA+NSCA0)=THETA2
      DO J=1,NSCA0
         PHIN(NSCA+J)=PHI1
      ENDDO
      NSCA=NSCA+NSCA0
      WRITE(CMSGNM,FMT='(I4,A)')NSCA0,
     &  ' = number of scattering angles in this scattering plane'
      CALL WRIMSG('REAPAR',CMSGNM)
      GOTO 22
 25   CONTINUE
C
C Check that if user has requested computation of f_ml, user has
C also requested linearly-polarized incident radiation.
C 96.01.25 (BTD) We have disabled this "protection".  So long as user
C is aware that when using elliptical incident polarization the
C f_ml are not the "usual" ones with index l referring to linearly
C polarized incident modes, there is not reason not to allow user
C to calculate f_ml for the the scattered radiation.
C
C      IF(NSCA.GT.0)THEN
C         DO 26 J=1,3
C            IF(AIMAG(CXE01(J)).NE.0.)CALL ERRMSG('FATAL','REAPAR',
C     &      'pol. vector E01 is complex: DDSCAT cannot compute f_ml, so 
C     &do not ask it to!')
C 26      CONTINUE
C      ENDIF
C
      CLOSE (IOPAR)
      RETURN
   40 CONTINUE
      CALL WRIMSG('REAPAR',CWHERE)
      CALL ERRMSG('FATAL','REAPAR',' Error reading ddscat.par file')
      RETURN
      END
