	SUBROUTINE QNDATA
C*********************************************************************
C
C  SUBROUTINE TO READ QUANT DATA FILES AND PROCESS DATA ONTO CPS SCALE
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  CONVERT INTENSITIES MEASURED WITH THETA COMPENSATING SLITS TO THE
C  CONSTANT VOLUME BASIS
C
C  SUBROUTINES CALLED:
C
C     AUTOLU - ATTATCHES THE LOGICAL UNIT NUMBER TO THE REQUESTED FILE
C              AND THEN READS RECORDS 1 TO 6.
C
C     QUANRD - READS THE RAW DATA FROM THE QUANT DATA FILE. CONVERTS
C              DATA TO COUNTS PER SECOND. (READS RECORDS 7 AND 8)
C
C     NETINT - INTEGRATES THE RAW INTENSITY DATA ON CPS SCALE AND CORREC
C              FOR BACKGROUND. RETURNS THE NET PEAK HEIGHT AND INTEGRATE
C              AREA.
C
C     TCSLIT - CORRECTS INTENSITY DATA FOR EFFECTS OF THETA-COMPENSATING
C              SLITS.  THAT IS, IT CONVERTS TO A CONSTANT VOLUME BASIS.
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
	PARAMETER  (NF=30,NR=100,NP=10,NT=1250)
	COMMON/IO/ IRD,ITYP,IPRT,ISCRT,ICOBRG,LIMPRT,IDBUG,LAST
	COMMON /FILE/ LU,ITYPE,IEND
	COMMON /FILEC/ NAME
	COMMON /RECRD1/ IFLAG(10)
	COMMON /RECR1C/ TITLE,DATE,TIME,RNFLNM
	COMMON /RECRD2/ XLAMBD,TAKEOF,DIVANG,RADIUS,SOLLIN,
     1 SOLLOT,RECSLT,DEADTM,STABIL,TBASE
	COMMON /RECR2C/ UNIT,TARGET,LINE,FOCAL,DIVSLT,MONOCH,DETECT
	COMMON /RECRD3/ ICALIB(6),TTHOFF,COEFF(5)
	COMMON /RECRD6/ XXWT(10),SIG(10)
	COMMON /RECR6C/ ELEMNT(10)
	COMMON /RECRD8/ FRELOV(2),SRELOV(2),WTPCPH(8)
	COMMON /RECR8C/ NAMEOV(2),JKLOV(2),ELMTPH(8)
	COMMON/RECRD7/BANG,PANG,EANG,DANG,RATE,MVCNT,TIMLIM,FIXERR,
     * LIMCNT,KODE,IOVLP,IPK,NPK,NBG,FREL,STDREL,STD,SIGSTD,
     * WTPRCT,PKTIME,BKGTIM,IOPTIM,ILOC,CONSTD
	COMMON /RECR7C/ NAM,JKL
	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)
	COMMON/RECR45/ERRMTX(15)
	COMMON/RECRD9/NPTS,X(NT),V(NT)
	COMMON/DATA/ PEAK,VPEAK,AREA,VAREA,XBKG,VBKG
	LOGICAL LAST
	CHARACTER*6 PHASE,HKL
	CHARACTER*100 TITLE
	CHARACTER*6 DATE,TIME,NAM,JKL,NAMEOV,JKLOV,MAN,KHL,RNFLNM
	CHARACTER*20 NAME
	CHARACTER*4 UNIT
	CHARACTER*2 TARGET,LINE,FOCAL,DIVSLT,MONOCH,DETECT
	CHARACTER*2 ELM, ELMTPH, ELEMNT
	CHARACTER*1 NOYES
	CHARACTER*6 T1, T2, T3, T4, T5, T6,T7,T8,T9,T10,T11
	CHARACTER*3 TLAB(2)
	CHARACTER*110 FMT
	DATA FMT/'(1X,2I2,3F6.2,1X,F4.3,1X,2A6,F4.0,F4.1,2F7.3,F9.4,F7.4,
     *1X,A3,I5,1X,I3,1X,I5,1X,I3,2F4.1,2(F6.2,1X,I2),F4.0)'/
	DATA T1/'mu/ro '/, T2/' Unk. '/, T3/' mu/ro'/, T4/'  Std.'/
	DATA T5/'Intstd'/, T6/'  Conc'/, T7/'  Ref '/
	DATA TLAB(1)/'Int'/, TLAB(2)/'Pk '/
C
C-----INITIALIZE FILE COUNTER
C
	IFILE=0
	LINES=0
	LAST = .FALSE.
C
C-----SET UP TO READ NEXT QUANT FILE
C
100	IFILE=IFILE+1
	IF(IFILE.LE.NF)GO TO 110
101	WRITE(ITYP,102)NF
	IF(ITYP.NE.IPRT) WRITE(IPRT,102) NF
102	FORMAT(/' Limit of ',I2,' files hit - rest skipped')
	GO TO 400
C
C-----ATTATCH LOGICAL UNIT NUMBER AND READ RECORDS 1 - 6 OF QUANT FILE
C
110	CALL AUTOLU
	IF(IEND)115,120,900
115	IEND = 0
	GO TO 400
120	IREPT=IFLAG(5)
	MOUNT=IFLAG(6)
	NREFL=0
C
C-----PRINT FILE TITLE, DATE AND TIME OF COLLECTION AND FLAGS
C
	IF(LIMPRT.EQ.2 .OR. (LIMPRT.EQ.1 .AND. IFILE.GT.1)) THEN
	   WRITE(IPRT,129) TITLE
129	FORMAT(/' Title: ',A100)
	   GOTO 138
	ENDIF
	WRITE(IPRT,130) TITLE,RNFLNM,NAME,DATE,TIME,MOUNT,IREPT,IFLAG
130	FORMAT(/' Title: ',A100,' RUN File: ',A6/1H ,'File: ',A20,
     *' collected on: ',A6,2X,A6,2X,'# Mountings=',I3,2X,'# Repetions=',
     * I3,5X,'Control flags = ',10I1)
	IF(LIMPRT.EQ.1 .AND. IFILE.GT.1) GOTO 135
200	WRITE(IPRT,131) UNIT,TARGET,LINE,XLAMBD,FOCAL,TAKEOF,DIVSLT,
     * DIVANG,RADIUS,SOLLIN,SOLLOT,RECSLT,MONOCH,DETECT,DEADTM,STABIL
131	FORMAT(/' Data collected on unit ',A4,' with a ',A2,
     1' Target using the ',A2,' line with a wavelength of ',F6.4,
     2' Angstroms'/' Focal spot type = ',A2,' Take Off Angle ',
     1 '= ',F4.1,' Degrees  Divergence Slit ',
     3'of type ',A2,' with ',F6.2,' degree (or MM for TC)',
     4/' Radius of diffractometer = ',F6.2,' mm   ',
     5' Divergence of incident and receiving soller slits = ',F4.1,1X,
     6F4.1/' Receiving slit of ',F4.1,' Degrees     ',
     7' Monochrometer type = ',A2,' Detector type = ',A2,' Dead Time = '
     8,F4.2/' Instrument Stability Factor = ',F4.2,' Percent.')
	WRITE(IPRT,133)ICALIB,TTHOFF,COEFF
133	FORMAT(' Instrument Calibration Flags = ',6I1,
     1' Two Theta Offset = ',F7.3/' Calibration polynomial coefficients'
     2,' = ',5E14.7)
	IF(XXWT(1).EQ.0.0)GO TO 135
	WRITE(IPRT,134)(ELEMNT(I),XXWT(I),SIG(I),I=1,6)
134	FORMAT(' Elemental composition: ',6(A2,F6.2,1X,F5.2,1X))
	IF(XXWT(7).GT.0.)WRITE(IPRT,134)(ELEMNT(I),XXWT(I),
     1 SIG(I),I=7,10)
C
C-----TEST IF RELATIVE INTENSITY DATA FILE WAS READ
C
135	T8 = T5
	T9=T7
	T10=T6
	T11=T6
	IF(IFLAG(3).NE.2)GO TO 136
	T8=T1
	T9=T3
	T10=T2
	T11=T4
	FMT(49:49)='1'
	FMT(54:54)='1'
136	IF(LIMPRT.EQ.1 .AND. IFILE.GT.1) GOTO 137
	WRITE(IPRT,9)
9	FORMAT(/' Negative numbers in the following table imply',
     1' quantities which are meaningless for the current run, '
     2' or to be calculated later')
137	WRITE(IPRT,10)T8,T9,T10,T11
10	FORMAT(/' ln fl Two Theta Region Delt Phase  Line  Rel',
     1' Sig ',2A6,' Reference Sig of cnt Peak Var Intg'
     2' Var  Req Obs Bkg. Bkg Peak Pk. Max'/
     3'  # # Bkglo Peak  Bkghi  Ang Name   h k l Int Rel ',
     42A6,' Intensity Refint Typ Int. PkI Area',
     5' Area Err Err Time Pts Time Pts Tim')
138	IF(ITYPE.GE.0)GO TO 153
	IF(IFLAG(3).LE.IABS(ITYPE).AND.IFLAG(3).NE.0)GO TO 210
153	IF(IFLAG(3).EQ.ITYPE) GOTO 210
	WRITE(ITYP,140) IFLAG(3)
140	FORMAT(/' Incorrect data file read - IFLAG(3)=',I2/
     * /' Continue processing? (yes/no)')
	READ(IRD,150) NOYES
150	FORMAT(A1)
	IF(NOYES .EQ. 'Y' .OR. NOYES .EQ. 'y')GO TO 210
	GO TO 998
C
C-----READ RAW DATA FOR EACH 2-THETA REGIONS
C
210	NREFL=NREFL+1
	CALL QUANRD
	IF(IEND.LT.1) GOTO 220
C
C  CHECK THAT THE LAST DATA SET WAS COMPLETE
C
	IF(NREFL.EQ.LINES+1)GO TO 100
	GO TO 233
C
C-----DETERMINE INTEGRATED AREA AND VARIANCE FROM RAW DATA
C
220	CALL NETINT
C
C-----IF THETA COMPENSATING SLITS WERE USED THEN CORRECT INTENSITIES
C-----TO THE CONSTANT VOLUME BASIS
C
	IF(DIVSLT.EQ.'TC') CALL TCSLIT
C
C-----BEGIN CHECKS ON WHERE TO STORE THIS DATA
C
	K=NREFL
	IF(IFILE.NE.1) GOTO 230
C
C-----CHECK FOR BEGINING OF NEXT REPETION
C
	IF(NAM.EQ.PHASE(1) .AND. JKL.EQ.HKL(1)) GOTO 231
C
C-----STORE THE PHASE NAME AND LINE HKL CHARACTER STRINGS
C
	PHASE(K)=NAM
	HKL(K)  =JKL
	KODES(K)=KODE
	FRELS(K)=FREL
	SRELS(K)=STDREL
C
C  IF THIS IS THE FIRST FILE SAVE DATA FOR LATER PROCESSESSING
C
	IF(IFILE.GT.1)GO TO 280
	LINES = LINES + 1
	DO 250 I=1,2
	MAN(I,K) = NAMEOV(I)
	KHL(I,K) = JKLOV(I)
	RELOV(I,K) = FRELOV(I)
250	RELOVS(I,K) = SRELOV(I)
	DO 255 I=1,8
	ELM(I,K) = ELMTPH(I)
255	WTP(I,K) = WTPCPH(I)
	STDS(K) = STD
	SSTDS(K) = SIGSTD
	IOVS(K) = IOVLP
	IPKS(K) = IPK
	GOTO 300
C
C-----FOR THE SECOND THRU NF FILE TEST IF NAMES AND HKL'S AGREE
C
230	IF(NAM.EQ.PHASE(K) .AND. JKL.EQ.HKL(K)) GOTO 300
	IF(NAM.NE.PHASE(1) .OR. JKL.NE.HKL(1)) GOTO 240
C
C-----HAVE ALL REPETIONS BEEN PROCESSED (ALL FROM SAME FILE)?
C
231	IF(IREPT.GT.1) GOTO 260
C
C-----HAVE ALL MOUNTINGS BEEN PROCESSED (ALL FROM SAME FILE)?
C
	IF(MOUNT.GT.1) GOTO 280
	WRITE(ITYP,232) IFILE,NAM,JKL,IREPT,MOUNT
	IF(ITYP.NE.IPRT) WRITE(IPRT,232) IFILE,NAM,JKL,IREPT,MOUNT
232	FORMAT(/' **Error** IFILE=',I3,' NAME=',A6,' JKL=',A6,
     * 'IREPT=',I3,'IMOUNT=',I3)
233	IFILE = IFILE - 1
	WRITE(ITYP,234)IFILE
	IF(ITYP.NE.IPRT) WRITE(IPRT,234)IFILE
234	FORMAT(/' The rest of this physical file will be skipped',
     1' using only up to set ',I2/)
C-----RESET NREFL TO VALUE FROM PREVIOUS FILE
	NREFL = LINES
	GO TO 100
C
C-----NAMES AND/OR HKL'S DO NOT MATCH
C
240	WRITE(ITYP,241) NAM,JKL
	IF(ITYP.NE.IPRT) WRITE(IPRT,241) NAM,JKL
241	FORMAT(/' ***Error***Name=',A6,1X,'hkl=',A6,' Do not match')
	GO TO 233
C
C-----NEXT REPETITION
C
260	IREPT=IREPT-1
	GOTO 290
C
C-----NEXT MOUNTING
C
280	MOUNT=MOUNT-1
	IREPT=IFLAG(5)
290	IFILE=IFILE+1
	IF(IFILE.GT.NF)GO TO 101
	NREFL=1
	K=1
C
C-----IF FIRST REFLECTION OF A NEW FILE THEN SAVE WT%
C
300	IF(NREFL.NE.1)GO TO 303
C WT IS THE CONCENTRATION OF THE INTERNAL STANDARD OR THE MU/RO
C OF THE UNKNOWN SAMPLE TO BE ANALYSED
	WT(IFILE) = WTPRCT
303	CONTINUE
C
C  CONC IS THE CONCENTRATION OF THE REFERENCE PHASE FOR A RUNFILE PREP
C
	IF(IFLAG(3).EQ.1 .AND. KODES(K).EQ.2) CONC(IFILE) = CONSTD
C
C FOR INTENSITY RATIO METHOD XMURHO = THE MU/RO OF THE STANDARD
C
	IF(IFLAG(3).EQ.2 .AND. KODES(K).EQ.1) XMURHO(K) = CONSTD
C
C-----CHECKS WERE OK - STORE THE PEAK AND INTEGRATED INTENSITIES
C
305	IF(IPKS(K).EQ.0)GO TO 310
	AINT(K,IFILE) = PEAK
	VINT(K,IFILE) = VPEAK
	GO TO 320
310	AINT(K,IFILE) = AREA
	VINT(K,IFILE) = VAREA
C
C  PUT NEGATIVE VALUES INTO MEANINGLESS ITEMS OR QUANTITIES TO BE
C  CALCULATED LATER IN THIS PROGRAM
C
320	IF(STDS(K).NE.0.)GO TO 301
	STDS(K) = -.5
	SSTDS(K) = -.5
301	IF(FRELS(K).NE.0)GO TO 302
	FRELS(K) = -1.
	SRELS(K) = -.5
302	IF(IFLAG(3).NE.2)GO TO 352
C
C-----FOR THE INTENSITY RATIO METHOD AND IF CHEMISTRY INPUT
C-----CALCULATE THE MU/RO OF THE STD OR PHASE OF THE SAMPLE
C
	IF(IFLAG(4).EQ.1)CALL MUONRO(TEMP,ELEMNT,XXWT,TARGET)
C
C-----CALIBRATION (IFLAG(2)=2) OR ANALYSIS (IFLAG(2)=1)?
C
	IF(IFLAG(2).EQ.2)GO TO 351
C IF CHEM NOT ENTERED USE THE U/P ALREADY IN WT(1) 	!RLS 4/20/85
       IF(IFLAG(4) .EQ. 0)GO TO 352			!RLS 4/20/85
	WT(IFILE) = TEMP
	GO TO 352
351       IF(IFLAG(4) .EQ. 0)GO TO 352			!RLS 4/20/85
	XMURHO(K) = TEMP
	WT(IFILE) = -1.0
C
C-----FIXERR = RELATIVE ERROR FOR PK OR INT INTENSITY DATA
C
352	IT = IPKS(K) + 1
	GO TO(355,358),IT
355	FIXER = 100. *  SQRT(VAREA)/ AREA
	GO TO 360
358	FIXER = 100. *  SQRT(VPEAK) / PEAK
C
C-----SKIP PRINTING IF IN LIMITED PRINT MODE
C
360	IF(LIMPRT.EQ.2)GO TO 210
C CONVERT PEAK AND AREA VALUES TO INTEGER TO SAVE TABLE SPACE
	JJP = PEAK
	JJPV = VPEAK
	JJA = AREA
	JJAV = VAREA
C-----WTSTD IS SET ONLY FOR INTERNAL STANDARD LINES
	WTSTD = -1.0
	IF(IFLAG(3).EQ.1 .AND. KODES(K).EQ.2) WTSTD =WT(IFILE)
	IF(IFLAG(3).EQ.2 .AND. KODES(K).EQ.1) WTSTD = XMURHO(K)
C-----CONCRF IS WT FRACTION OF CALIBRATION PHASE IN CALIBRATION SAMPLE
	CONCRF = -1.0
	IF(KODES(K).EQ.1) CONCRF = CONC(IFILE)
	WRITE(IPRT,FMT)NREFL,IFILE,BANG,PANG,EANG,DANG,PHASE(K),
     1 HKL(K),FRELS(K),SRELS(K),WTSTD,CONCRF,STDS(K),
     2 SSTDS(K),TLAB(IT),JJP,JJPV,JJA,JJAV,FIXERR,FIXER,
     3 BKGTIM,NBG,PKTIME,NPK,TIMLIM
	IF(IOVS(K).EQ.0)GO TO 210
	DO 450 I=1,IOVS(K)
	IF(IFLAG(4).EQ.1)GO TO 430
410	WRITE(IPRT,420)MAN(I,K),KHL(I,K),RELOV(I,K),RELOVS(I,K)
420	FORMAT(' ',5X, 'Overlapped with',8X,2A6,F4.0,F4.1)
	GO TO 450
430	DO 435 J=8,1,-1
	IF(WTP(J,K).NE.0.0)GO TO 440
435	CONTINUE
	GO TO 410
440	JJ=J
	WRITE(IPRT,445)MAN(I,K),KHL(I,K),RELOV(I,K),RELOVS(I,K),
     1 (ELM(J,K),WTP(J,K),J=1,JJ)
445	FORMAT(' ',5X,'Overlapped with',8X,2A6,F4.0,F4.1,
     2 2X,'Chemistry = ',8(A2,1X,F5.3,1X))
450	CONTINUE
	GOTO 210
C
C-----ALL DATA HAS BEEN READ, PROCESSED AND STORED INTO ARRAYS
C
400	IFILE=IFILE-1
	NREFL=NREFL-1
	IEND = 0
	GO TO 999
C
C-----UNEXPECTED END-OF-FILE ENCOUNTERED
C
900	WRITE(ITYP,902) IFILE,NAME
902	FORMAT(/' Unexpected EOF in file#',I3,' file name=',A6)
	GO TO 999
998	IEND = 1
999	RETURN
	END
