	SUBROUTINE DIFF11(IERR, IUNIT, NEXT, ERUNIT, ERCODE,
     &		DIFILE, ICODE, DCODE, PKCODE, IRANGE, FWCODE,
     &		ID, NID, IDATE, ITIME, OPER, NOPER, HOW, NHOW, 
     &		SMES, EMES, TEMP, FIELD, LAMBDA,
     &		NLIM, NPEAKS, PEALOC, INTS, COUNTM, PMAX )
C
	CHARACTER ID*16, IDATE*9, ITIME*8, OPER*12, HOW*32, CTEMP*48
	CHARACTER JUNK*1, PKTYPE*1, DIFILE*32
	INTEGER IERR, ERUNIT, PEALOC(1), INTS(1) 
	INTEGER DCODE, DCOD, PKCODE, FWCODE
	REAL*4 SMES, EMES, TEMP, FIELD, COUNTM, PMAX, FWHM(1)
	REAL*4 NORMAL, INTENS, LAMBDA
	INTEGER*2 JPEAKS
C
C  Read title, operator, time & date
100	NEXT=1
	DO 110,I=1,45,4
110	READ(IUNIT'NEXT) CTEMP(I:I+3)
	CTEMP(26:28)='   '
	DO 120,I=1,18
	 IF(' '.LE.CTEMP(I:I) .AND. CTEMP(I:I).LE.'~') GOTO 120
	 IERR=99	! Non ascii garbage detected in first twelve
	 RETURN		! twelve records so this is not Diffrac-11 format.
120	CONTINUE
	IERR = 0
C
C  Store title, operator, time & date
	ID = CTEMP(1:16)
	NID=16
	IDATE = CTEMP(17:25)
	ITIME = CTEMP(29:36)
	OPER = CTEMP(37:48)
	NOPERA=12	
C  How was this file created?
	HOW(1:24) = 'Diffrac-11 *DR program  '
	NHOW = 24
	READ(IUNIT'20) HOW(12:12),JUNK,PKTYPE,JUNK
C
C  Read measurement parameters
	READ(IUNIT'13) SMES
	READ(IUNIT'14) EMES
	READ(IUNIT'19) LAMBDA
	TEMP = 25.0
	FIELD = 0.0
C
C  Read number of peaks
	READ(IUNIT'21) JPEAKS
	NPEAKS = JPEAKS
C
C  Check for error conditions
	IERR = 0
	IF (NPEAKS.GE.NLIM) THEN
		NPEAKS = NLIM
		IERR = 2
	ELSE IF (NPEAKS.EQ.0) THEN
		IERR = 1			!error code
		if (ercode .eq. 1) write (erunit,6400) difile, rnow
6400	format(x,'IERR11: d/i file-',A,'no peaks found in range',i3)
		go to 9000
	ELSE IF (IRANGE.GE.2) THEN
		IERR = -5				!invalid range
		write (erunit,6300) difile, irange, rnow
6300	format(x,'IERR: d/i file-',A,'specified range',i3,' is',
     +	' higher than the available ranges',i3)
		go to 9000				!terminate
	ELSE IF (PKTYPE.EQ.'N' .AND. PKCODE.EQ.1) THEN
		IERR = -4		!error code
		if (ercode .eq. 1) write (erunit,6500) difile
6500	format(x,'IERR11: d/i file-',A,'integrated intensities not',
     +		 ' available')
		go to 9000
	ELSE IF (PKTYPE.EQ.'I' .AND. PKCODE.EQ.0) THEN
		IF (ERCODE.EQ.1) WRITE(ERUNIT,6550) DIFILE
6550		FORMAT(' IERR11: d/i file -',A,' net intensities not',
     &		' available',/,'  They will be approximated by ',/,
     &		' NET INT. = INTEG. INT. * ANGLE INCREMENT / FWHM')
	END IF
C
C  Read the peaks
C
	COUNTM=0.0
	PMAX=0.0
	NEXT = 41
	IFLAG=1
	DO 3000, I=1,NPEAKS
	READ(IUNIT'NEXT,ERR=999) PEAKL
	READ(IUNIT'NEXT,ERR=999) INTENS
	READ(IUNIT'NEXT,ERR=999) FWHM1
	NEXT = NEXT + 3
C  Store peak location
	pealoc(I) = dcod(peakl)
C  Look for biggest peak
	IF(INTENS.GT.COUNTM) THEN
		COUNTM=INTENS
		PMAX=PEAKL
	END IF
C  Store full width at half maximum?
	IF (FWCODE.EQ.1) FWHM(I) = FWHM1
3000	continue
C
C  Scale intensity as an integer from 1 to 100
	NEXT = 42
	IFLAG=2
	DO 4000, I=1,NPEAKS
	READ(IUNIT'NEXT,ERR=999) INTENS
	NEXT = NEXT + 5
	normal = intens * 100.0/ countm	!normalized intensity
	ints (I) = nint (normal)   !default integer
4000	CONTINUE
c
9000	RETURN
999	WRITE(ERUNIT,998) IFLAG,NEXT,I,NPEAKS
998	FORMAT(/,' ERROR IN PASS ',I1,' OF READING D&I FILE',/
     &   ' NEXT RECORD=',I5,'   SUBSCRIPT=',I5,'   NPEAKS=',I5)
	PRINT*,'INTENS, PEAKL, FWHM1=',INTENS,PEAKL,FWHM1
	STOP 'EITHER THE PROGRAM HAS BUGS OR THE FILE IS MESSED UP'
	END
