      SUBROUTINE DIELEC(CDIEL,WAVE,IDVOUT,CFLEPS,CXEPS,
     &                  MXCOMP,MXWAVT,NCOMP,E1A,E2A,WVA)
      IMPLICIT NONE
C Arguments:
      INTEGER MXCOMP,MXWAVT
      CHARACTER*6 CDIEL
      CHARACTER*60 CFLEPS(MXCOMP)
      COMPLEX CXEPS(MXCOMP)
      INTEGER IDVOUT,NCOMP
      REAL WAVE
      REAL E1A(MXWAVT),E2A(MXWAVT),
     &     WVA(MXWAVT)
C Local Variables:
      LOGICAL INIT
      CHARACTER*80 CDESCR
      COMPLEX CXI
      INTEGER I,J,JJ,NWAVT,IWV,IREN,IIMN,IEPS1,IEPS2,ICOL
      REAL DUM1,DUM2,E1,E2,TEMP
      REAL XIN(10)
      EXTERNAL ERRMSG
      SAVE CXI,IEPS1,INIT,NWAVT
***********************************************************************
C Given:
C       CDIEL = 'TABLES' to read from input tables
C               'H2OICE' for H2O ice
C               'H2OLIQ' for H2O liquid
C       WAVE = wavelength (micron)
C       IDVOUT=output unit number
C       CFLEPS(1-NCOMP) = names of files containing dielectric data
C       MXCOMP,MXWAVT = dimensioning information
C       NCOMP = number of components
C       NWAV = number of wavelengths
C       E1A, E2A, WVA = scratch arrays
C Returns:
C       CXEPS(1-NCOMP) = dielectric constant for materials
C                        1-NCOMP
C NOTE:
C       If CDIEL='TABLES', then it is assumed that file(s) containing
C       the table(s) will have following format:
C line1 = description (CHARACTER*80)
C line2 = IWV, IREN, IIMN, IEPS1, IEPS2
C         where IWV = column in which wavelengths are tabulated
C               IREN = col. in which Re(N) is tabulated (0 if not)
C               IIMN = col. in which Im(N) is tabulated (0 if not)
C               IEPS1= col. in which Re(EPS) is tabulated (0 if not)
C               IEPS2= col. in which Im(EPS) is tabulated (0 if not)
C line3 = header line (will not be read)
C line4... = data, according to columns specified on line2
C wavelengths must be monotonic, either increasing or decreasing
C
C B.T.Draine, Princeton Univ. Obs.
C History:
C 90.12.04 (BTD): Rewritten to allow H2OICE and H2OLIQ options.
C 90.12.21 (BTD): Correct handling of 'TABLES' option when more than
C                 one wavelength is considered.
C 91.05.02 (BTD): Added IDVOUT to argument list for subr. INTERP
C 91.09.12 (BTD): Moved declaration of MXCOMP and MWAVT ahead of
C                 declaration of CFLEPS and CXEPS
C 93.12.06 (BTD): Added IEPS1,INIT, and NWAVT to SAVE statement
C                 (without this did not run properly on SGI Indigo)
C 96.12.16 (BTD): Corrected temperature to T=250K in output statement
C                 for H2OICE option
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 reapar.f and DDSCAT.f)
C Copyright (C) 1993,1996,1998 B.T. Draine and P.J. Flatau
C This code is covered by the GNU General Public License.
C***********************************************************************
      DATA CXI/(0.,1.)/,INIT/.TRUE./
      IF(CDIEL.EQ.'TABLES')THEN
         IF(INIT.OR.NCOMP.GT.1)THEN
            DO 1000 J=1,NCOMP
               OPEN(UNIT=3,FILE=CFLEPS(J),STATUS='OLD')
C Read header line:
               READ(3,9000)CDESCR
               WRITE(IDVOUT,9000)CDESCR
C Read line specifying columns for wavelength,Re(n),Im(n),Re(eps),Im(eps):
               READ(3,*)IWV,IREN,IIMN,IEPS1,IEPS2
               ICOL=IWV
               IF(IREN.GT.ICOL)ICOL=IREN
               IF(IIMN.GT.ICOL)ICOL=IIMN
               IF(IEPS1.GT.ICOL)ICOL=IEPS1
               IF(IEPS2.GT.ICOL)ICOL=IEPS2
C Skip header line:
               READ(3,*)
               DO 0500 I=1,MXWAVT
                  READ(3,*,END=0600)(XIN(JJ),JJ=1,ICOL)
                  WVA(I)=XIN(IWV)
                  IF(IEPS1.GT.0)THEN
                     E1A(I)=XIN(IEPS1)
                     E2A(I)=XIN(IEPS2)
                  ELSE
                     E1A(I)=XIN(IREN)
                     E2A(I)=XIN(IIMN)
                  ENDIF
                  NWAVT=I
 0500          CONTINUE
C Check whether there is unread data remaining in file
               READ(3,*,END=0600)(XIN(JJ),JJ=1,ICOL)
C If this point is reached, apparently unread data remains, so
C issue warning:
               CALL ERRMSG('WARNING','DIELEC',
     &    'parameter MXWAVT not large enough to read full dielec file')
 0600          CLOSE(3)
C
C*** Have completed reading in table for this composition.
C    Now interpolate
               CALL INTERP(WAVE,E1,E2,WVA,E1A,E2A,
     &                     IDVOUT,MXWAVT,NWAVT)
               IF(IEPS1.GT.0)THEN
                  CXEPS(J)=E1+CXI*E2
               ELSE
                  CXEPS(J)=(E1**2-E2**2)+2.*CXI*E1*E2
               ENDIF
 1000       CONTINUE
            INIT=.FALSE.
         ELSE
C*** Perform this only if NCOMP=1 and previously initialized:
            CALL INTERP(WAVE,E1,E2,WVA,E1A,E2A,IDVOUT,MXWAVT,NWAVT)
            IF(IEPS1.GT.0)THEN
               CXEPS(1)=E1+CXI*E2
            ELSE
               CXEPS(1)=(E1**2-E2**2)+2.*CXI*E1*E2
            ENDIF
         ENDIF
      ELSEIF(CDIEL.EQ.'H2OICE')THEN
         IF(NCOMP.GT.1)CALL ERRMSG('FATAL','DIELE',
     &                 'H2OICE with NCOMP>1??')
C For the moment set T=250K
         TEMP=250.
         WRITE(CDESCR,FMT='(A)')' H2O ice at T=250K'
         WRITE(IDVOUT,9000)CDESCR
         CALL REFICE(0,WAVE,TEMP,E1,E2,DUM1,DUM2)
         CXEPS(1)=E1**2-E2**2+2.*CXI*E1*E2
         RETURN
      ELSEIF(CDIEL.EQ.'H2OLIQ')THEN
         WRITE(CDESCR,FMT='(A)')' H2O liquid at T=280K'
         WRITE(IDVOUT,9000)CDESCR
         IF(NCOMP.GT.1)CALL ERRMSG('FATAL','DIELE',
     &                 'H2OLIQ with NCOMP>1??')
C For the moment set T=280K
         TEMP=280.
         CALL REFWAT(0,WAVE,TEMP,E1,E2,DUM1,DUM2)
         CXEPS(1)=E1**2-E2**2+2.*CXI*E1*E2
         RETURN
      ELSE
         CALL ERRMSG('FATAL','DIELE','unrecognized value of CDIEL')
      ENDIF
      RETURN
 9000 FORMAT(A80)
      END
