	SUBROUTINE QUANRD
C**********************************************************************
C
C  SUBROUTINE TO READ QUANT DATA FILE (NAMEXD*RAW.)
C
C  TO READ RECORDS 7, 8 AND 9 OF THE "AUTO" QUANT FILE
C     RECORD 7 = LINE DATA COLLECTION AND QUANTITATIVE PARAMETERS
C     RECORD 8 - LINE OVERLAP INFORMATION
C     RECORD 9 = RAW DATA FOR THIS LINE
C  TO CALCULATE THE VARIANCE OF THE OBS DATA ON CPS SCALE
C
C  FILE STRUCTURE DEFINED IN "AUTO - A REAL TIME DIFFRACTOMETER
C  CONTROL SYSTEM", R.L. SNYDER, C.R. HUBBARD AND N.C. PANAGIOTOPOULOS,
C  NBS INTERNAL REPORT, 1981.
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
	COMMON /FILE/ LU,ITYPE,IEND
	COMMON /FILEC/ NAME
	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 /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/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 /RECRD8/ FRELOV(2),SRELOV(2),WTPCPH(8)
	COMMON /RECR8C/ NAMEOV(2),JKLOV(2),ELMTPH(8)
	COMMON/RECRD9/NPTS,X(NT),V(NT)
	CHARACTER*100 TITLE
	CHARACTER*6 DATE,TIME,MAN,JKL,NAMEOV,JKLOV,NAM,KHL,RNFLNM
	CHARACTER*20 NAME
	CHARACTER*2 ELMTPH, ELM
	CHARACTER*4 UNIT
	CHARACTER*2 TARGET,LINE,FOCAL,DIVSLT,MONOCH,DETECT
C
C-----READ LINE PARAMETER AND QUANTITATIVE PARAMETERS RECORD
C
	IEND=0
	READ(LU,100,END=990) BANG,PANG,EANG,DANG,RATE,MVCNT,TIMLIM,FIXERR,
     * FIMCNT,NAM,JKL,KODE,IOVLP,IPK,NPK,NBG,FREL,STDREL,STD,SIGSTD,
     * WTPRCT,PKTIME,BKGTIM,IOPTIM,ILOC,CONSTD
100	FORMAT(3F7.3,F7.4,F4.1,I1,F5.1,F5.2,F7.0,2A6,3I1,2I2,F6.1,
     * F4.1,F9.4,F8.4,F8.3,2F7.2,2I1,F8.3)
C  RLS CHANGED DANG FORMAT TO F7.4 TO ALLOW FOR STEPS OF .0025 8/85
C  RLS CHANGED READ OF LIMCNT IN I5 TO FIMCNT IN F7.0
     	LIMCNT = FIMCNT
C  JDW CHANGED FORMAT FOR TIMLIM TO F5.1
C
C  IF THE PEAK ANGLE IS INPUT AS ZERO ASSIGN 1/2 THE DISTANCE BETWEEN
C  BANG AND PANG TO IT
C
	IF(PANG.EQ.0.0)PANG = BANG + (EANG - BANG) / 2.0
C
C-----READ LINE OVERLAP AND CHEMICAL INFORMATION FOR THIS LINE(PHASE)
C
	READ(LU,200,END=990) (NAMEOV(I),JKLOV(I),FRELOV(I),SRELOV(I),
     * I=1,2),(ELMTPH(J),WTPCPH(J),J=1,8)
200	FORMAT(2(2A6,F6.1,F6.2),8(A2,F8.4))
C
C-----IF THIS IS DATA FOR CALIBRATION THEN RUNFIL WILL BE CALLED. IF SO
C-----THEN THE RECORDS 7 AND 8 FOR THE FIRST FILE MUST BE SAVED
C-----(NOTE THAT THE TWO IFLAG TESTS ARE REDUNDANT. USED TO REDUCE
C-----POSSIBLE PROBLEMS DUE TO IMPROPER ASSIGNMENT OF IFLAG VALUES)
C
	IF(IFILE.NE.1 .OR. IFLAG(1).NE.2 .OR. IFLAG(3).GE.3) GOTO 280
C
C  FOR STANDARD FILE PROCESSESING BY PROGRAM RUNFIL WRITE A
C  SCRATCH FILE OF THE RECORDS 7 & 8
C
	 WRITE(ISCRT)BANG,PANG,EANG,DANG,RATE,MVCNT,TIMLIM,FIXERR,
     * LIMCNT,NAM,JKL,KODE,IOVLP,IPK,NPK,NBG,FREL,STDREL,STD,SIGSTD,
     * WTPRCT,PKTIME,BKGTIM,IOPTIM,ILOC,CONSTD
	WRITE(ISCRT)    (NAMEOV(I),JKLOV(I),FRELOV(I),SRELOV(I),I=1,2),
     * (ELMTPH(J),WTPCPH(J),J=1,8)
C
C-----CALCULATE THE NUMBER OF DATA POINTS AND RECORDS FOR THIS LINE
C
280	IF(IPK.GT.0) GOTO 320
C-----INTEGRATE MODE
c  RLS 8/85 allow for round off error when using .0025 step width
	NPTS = IFIX((EANG-BANG)/DANG+1.5)
	GOTO 330
C-----PEAK DATA COLLECTION MODE USED
320	NPTS = NBG + NPK
C-----UPPER ANGLE BACKGROUND COLLECTED? IF SO ADD NBG
        IF(EANG.GT.0.001) NPTS = NPTS + NBG
C-----CALCULATE # OF RECORDS - 16 POINTS PER ASCII RECORD
330	IF(NPTS.LE.NT) GOTO 340
C-----# OF DATA POINTS WILL EXCEED DIMENSIONS
	WRITE(ITYP,322) NPTS, NT
322	FORMAT(/' ',I6,' Data points will exceed array dimension NT=',I5)
	GOTO 990
340	NREC = (NPTS-1)/16+1
C
C-----READ RAW INTENSITY DATA AND STORE INTO X(I)
C
	DO 380 I=1,NREC
	J=16*(I-1)
	READ(LU,360,END=990) (X(J+K),K=1,16)
360	FORMAT(16X,16F7.0)
380	CONTINUE
C
C-----CONVERT RAW DATA TO COUNTS PER SECOND(CPS)
C-----HANDLE ANY X(I)=NEGATIVE #. (=TENTH OF A SECOND TO REACH LIMCNT CO
C-----CALCULATE THE VARIANCE OF X(I)
C
	DO 460 I=1,NPTS
	IF(X(I).LT.0.0) GOTO 420
	SECNDS = PKTIME
	IF(I.LE.NBG .OR. (EANG.GT.0.001 .AND. I.GT.(NPTS-NBG)))SECNDS=
     *BKGTIM
C-----THE VALUE 1.0E-04 IS DUE TO STABIL BEING IN PERCENT
	V(I) = (X(I)+1.0E-04*STABIL*STABIL*X(I)*X(I))/(SECNDS*SECNDS)
	X(I) = X(I)/SECNDS
	GOTO 440
C-----X(I) IS -10*SECONDS REQUIRED TO REACH LIMCNT COUNTS
420	V(I) = (1.0/(TBASE*X(I)))**2*(LIMCNT+1.0E-04*STABIL*STABIL*LIMCNT*
     * LIMCNT)
	X(I) = -(1.0/TBASE)*LIMCNT/X(I)
C
C-----CORRECT FOR DEAD TIME
C
440	FACTOR = 1.0-X(I)*DEADTM*1.0E-06
	X(I) = X(I)/FACTOR
C
C-----ASSUME ZERO ERROR IN DEADTM AND THAT X(I)*DEADTM IS SMALL
C
	V(I)=V(I)/(FACTOR*FACTOR)
460	CONTINUE
	GOTO 999
C-----END OF FILE ENCOUNTERED - SET THE FLAG
990	IEND = 1
	CLOSE(UNIT=LU,STATUS='KEEP')
999	RETURN
	END
