	SUBROUTINE RELI(ISEL)
C**********************************************************************
C
C  SUBROUTINE TO DETERMINE THE RELATIVE INTENSITIES OF X-RAY POWDER
C  DIFFRACTION LINES MEASURED USING THE DATA COLLECTION PROGRAM
C  AUTO IN QUANT MODE.
C
C  R.L. SNYDER   NYS COLLEGE OF CERAMICS AT ALFRED UNIVERSITY
C
C    AND
C
C  C.R. HUBBARD   NATIONAL BUREAU OF STANDARDS
C
C
C  DATA FILE STRUCTURE IS DEFINED IN "AUTO - A REAL TIME DIFFRACTOMETER
C  CONTROL SYSTEM", R.L. SNYDER, C.R. HUBBARD AND N. PANAGIOTOPOLOUS
C  NBS INTERNAL REPORT, 1981.
C
C  MULTIPLE REPETITIONS AND MULTIPLE MOUNTINGS RECORED IN THE SAME
C  DATA FILE ARE TREATED AS SEPARATE FILES
C
C  MULTIPLE DATA FILES CAN ALSO BE USED
C
C  OVERALL AVERAGE RELATIVE INTENSITIES AND ESD'S ARE DETERMINED
C
C*********************************************************************
C  RELATIVE INTENSITY VALUES FOR THE INTERNAL STANDARD METHOD
C  IF ENTERED, WILL BE USED TO SCALE ALL LINES FROM EACH
C  STANDARD TO THE 100% LINE.  THIS ALLOWS COMPARISON
C  OF THE STANDARD INTENSITY VALUES WITH ALL ON THE SAME
C  SCALE.  IF RELATIVE INTENSITY VALUES WERE NOT INPUT
C  TO PROGRAM AUTO THEN THEY ARE
C  COMPUTED FOR EACH LINE OF EACH STANDARD.
C
C
C  THE FOLLOWING PARAMETER STATMENT DEFINES THE DIMENSIONS FOR
C  THE QUANTATATIVE ANALYSIS SUBROUTINES.  TO REDIMENSION
C  SIMPLY CHANGE THIS STATMENT HERE AND IN EACH SUBROUTINE.
C      NF = THE NUMBER OF DATA FILES ALLOWED FOR INPUT
C      NR = THE MAXIMUM NUMBER OF REFLECTIONS WHICH CAN BE PROCESSED
C      NP = THE MAXIMUM NUMBER OF PHASES ALLOWED
C      NT = THE MAXIMUM NUMBER OF DATA POINTS FOR ANY REFLECTION
C      NS = THE MAXIMUM NUMBER OF LINES FOR THE STANDARD PHASE
C      NU = THE MAXIMUM NUMBER OF LINES FOR ALL UNKNOWN PHASES
C
	PARAMETER (NF=30, NR=100, NP=10, NT=1250, NS=5, NU=20)
	COMMON/IO/ IRD,ITYP,IPRT,ISCRT,ICOBRG,LIMPRT,IDBUG
	COMMON /FILE/ LU,ITYPE,IEND
	COMMON /FILEC/ NAME
	COMMON /RECRD1/ IFLAG(10)
	COMMON /RECR1C/ TITLE,DATE,TIME,RNFLNM
	COMMON/SVDAT1/ AINT(NR,NF),VINT(NR,NF),NREFL,IFILE
	COMMON/LABELS/ PHASE(NR),HKL(NR)
	COMMON/SVDAT2/ FRELS(NR),SRELS(NR),WT(NF),KODES(NR),STDS(NR),
     * SSTDS(NR),IOVS(NR),IPKS(NR),RELOV(2,NR),
     * RELOVS(2,NR), WTP(8,NR), CONC(NF), XMURHO(NR)
	COMMON /SVDA2C/ MAN(2,NR),KHL(2,NR),ELM(8,NR)
C*****NOTE: CONTRARY TO AUTO WRITEUP RELOVS IS NOT THE SIGMA OF THE
C*****OVERLAPPED LINES REL I BUT INSTEAD IT IS THE RIR OF THAT LINE
	COMMON /RNFIL/ KIND(NR), NUNK, L,IWT
	COMMON /RNFILC/ AVL, BL, ALL, ESDHDG(2)
	DIMENSION MAXI(NP), SUMMAX(NP), RMS(NR)
	CHARACTER*6 PHASE,HKL,BL,ALL
	CHARACTER*100 TITLE
	CHARACTER*6 DATE,TIME,MAN,KHL,RNFLNM
	CHARACTER*20 NAME
	CHARACTER*7 AVL
	CHARACTER*2 ELM
	CHARACTER*7 METHOD(2)
	CHARACTER*9 ESDHDG
	DATA METHOD/'INTEGRT','PEAK HT'/
C
C  IDENTIFY WHICH REFLECTIONS BELONG TO WHICH PHASE
C
	NUNK = 0
	L = 0
	IREF = 0
	DO 15 I=1,NREFL
15	KIND(I) = 0
	DO 30 I=1,NREFL
	IF(KODES(I).EQ.2)IREF=1
	IF(KODES(I).EQ.3)L=I
	IF(KODES(I).NE.1)GO TO 30
C
C  KIND(I) = 0 MEANS A STANDARD OR REFERENCE LINE
C          = 1 MEANS I IS A LINE FROM UNKNOWN #1
C          = N MEANS I IS A LINE FROM UNKNOWN #N (10 MAX)
C          = NUNK+1 MEANS A STANDARD AFTER THE NEXT LOOP
C      L   = THE NUMBER OF THE REFERENCE LINE FOR INT. RATIO METHOD
C
	IF(KIND(I).NE.0)GO TO 30
	NUNK = NUNK + 1
	DO 20 J=1,NREFL
	IF(PHASE(I).NE.PHASE(J))GO TO 20
	KIND(J) = NUNK
20	CONTINUE
30	CONTINUE
	MUNK = 0
	IF(ISEL.EQ.3)GO TO 35
C
C  ZERO THE AVERAGE ARRAYS
C
C
C  FIND WHICH REFLECTION WILL BE USED AS THE SCALE REFLECTION FOR
C  EACH STANDARD INPUT
C  NOTE: THE INTERNAL STANDARDS KEYS ARE KEPT IN MAXI(NUNK+1)
C
	WRITE(IPRT,1100)
1100	FORMAT(/' Relative intensity analysis:'/
     1 ' The following line(s) will be used in scaling'//
     * ' ', 'ln  Phase  h k l    Method')
	MUNK = NUNK + 1
	IF(IREF.EQ.0)MUNK = MUNK - 1
35	DO 500 I=1,MUNK
	MAXI(I) = 0
500	SUMMAX(I) = 0.0
	DO 550 I=1,NREFL
	IF(KIND(I).EQ.0)KIND(I) = MUNK
C
C  A SCALE LINE MAY NOT BE OVERLAPPED
C
	IF(IOVS(I).NE.0)GO TO 550
	PKSUM = 0.0
	DO 510 J=1,IFILE
	PKSUM = PKSUM + AINT(I,J)
510	CONTINUE
	MAX = KIND(I)
	IF(SUMMAX(MAX).GE.PKSUM)GO TO 550
	SUMMAX(MAX) = PKSUM
	MAXI(MAX) = I
550	CONTINUE
	DO 560 I=1,MUNK
	SUMMAX(I) = SUMMAX(I) / IFILE
	IMTH = IPKS(MAXI(I)) + 1
	WRITE(IPRT,1110)MAXI(I),PHASE(MAXI(I)),HKL(MAXI(I)),METHOD(IMTH)
1110	FORMAT(' ',I2,2(1X,A6),3X,A7)
560	CONTINUE
C
C-----RETURN IF RELATIVE INTENSITIES INPUT ARE TO BE USED
C
	IF(ISEL.EQ.3)GO TO 999
	DO 570 I=1,NREFL
C
C  SET THE RELATIVE INTESITIES OF THE SCALE REFLECTIONS
C
	IF(KODES(I).LT.2)GO TO 570
C
C  THIS IS THE REFERENCE LINE FOR THE INTENSITY RATIO METHOD
C  SET ITS RELATIVE INTENSITY TO 100. AND SIGMA TO ALMOST 0.0
C
	FRELS(I) = 100.
	SRELS(I) = .1E-15
570	CONTINUE
C
C  WE HAVE NOW OBTAINED THE MAXIMUM INTENSITY VALUES FOR THE
C  INTERNAL STANDARD AND THE UNKNOWNS.
C  COMPUTE THE RELATIVE INTENSITIES
C
	DO 300 I=1,MUNK
	DO 200 J=1,NREFL
	IF(KIND(J).NE.I)GO TO 200
	IF(L.EQ.J)GO TO 200
	II = MAXI(I)
	S1 = 0.0
	S2 = 0.0
	S3 = 0.0
	WRITE(IPRT,2020) ESDHDG
2020	FORMAT(/' File analyte  line Irel=100  line  relative ',A9,
     1'  Root mean  Number of'/' ','  #   Phase  h k l   Name',
     1'   h k l Intensity ', A9,  ' square dev. measurments')
	IF(IOVS(J).EQ.0)GO TO 90
	IF(ISEL.NE.4)GO TO 85
	WRITE(IPRT,2025)
2025	FORMAT(' ','********** Warning - overlapped line relative',
     1' intensity may be incorrect')
	GO TO 90
85	WRITE(IPRT,2050)PHASE(J),HKL(J)
2050	FORMAT(/' Relative intensity of overlapped line ',2A6,
     1' will not be computed - input value will be used')
	GO TO 200
90	CONTINUE
	DO 100 K=1,IFILE
C
C  IF A ZERO INTENSITY GOT ON THE FILE SKIP IT
C
	IF(AINT(J,K).LE.0.0)GO TO 100
	COMTMP=AINT(J,K)/AINT(II,K) * 100.
	VTMP = 1.0
	IF(IWT.NE.1) VTMP=COMTMP*COMTMP*(VINT(J,K)/(AINT(J,K)*AINT(J,K))+
     * VINT(II,K)/(AINT(II,K)*AINT(II,K)))
	VV = SQRT(VTMP)
C
C  SAVE THE SUMS TO TAKE WEIGHTED AVERAGES AND RMS SIGMAS
C
C  WE USE 1/SIGMA**2 AS WEIGHTS (WHERE SIGMA IS FROM COUNTING STATS)
C
	COM = COMTMP / VTMP
	S2 = S2 + COM
	COM = COMTMP*COM
	S1 = S1 + COM
	S3 = S3 + 1.0/VTMP
	WRITE(IPRT,2200)K,PHASE(J),HKL(J),PHASE(II),HKL(II),
     1COMTMP,VV
2200	FORMAT(' ',I3,2(2X,A6,1X,A6),2F10.3)
100	CONTINUE
190	FRELS(J) =   S2 / S3
	SRELS(J) = 0.0
	IF(IWT.NE.1) SRELS(J) = SQRT(1.0 / S3)
	RMS(J) = 0.0
	IF(IFILE.EQ.1)GO TO 200
	T = (S1/S3 - FRELS(J)*FRELS(J))/(IFILE-1)
	IF(T.GT.0.0) RMS(J) = SQRT(T)
C-----FOR Irel = 100 LINE SET RMS TO AVOID ROUND OFF PROBLEMS
	IF(J.EQ.II) RMS(J) = 0.0
	IF(IOVS(J).GT.0.AND.ISEL.GT.2)WRITE(IPRT,2025)
	WRITE(IPRT,110)PHASE(J),HKL(J),AVL,BL,FRELS(J),
     1 SRELS(J),RMS(J),IFILE
110	FORMAT(' ',5X,A6,1X,A6,1X,A7,1X,A6,F10.3,F10.3,1X,F10.3,I7)
200	CONTINUE
300	CONTINUE
	WRITE(IPRT,2300)
2300	FORMAT(/' Summary of relative intensities')
	DO 450 I=1,MUNK
	WRITE(IPRT,2310)
2310	FORMAT(/' ',5X,'Phase   h k l   Method      Intensity   e.s.d.'
     * '  rms dev  #')
	DO 400 J=1,NREFL
	IF(KIND(J).NE.I)GO TO 400
	IMTH = IPKS(J)+1
	WRITE(IPRT,2320)PHASE(J),HKL(J),METHOD(IMTH),FRELS(J),SRELS(J),
     * RMS(J),IFILE
2320	FORMAT(' ',5X,A6,1X,A6,3X,A7,F12.3,2F9.3,2X,I3)
400	CONTINUE
450	CONTINUE
999	RETURN
	END
