C********************************************************************
C
C  PROGRAM TO CREATE RUN FILES FROM STANDARD FILES PREPARED BY AUTO
C  IN EITHER INTERNAL STANDARD OR INTENSITY RATIO MODE.
C  THE RUN FILE IS THE SET OF DATA DIRECTING THE QUANTITATIVE ANALYSIS
C  OF AN UNKNOWN.  IT CONTAINS THE STANDARD INTENSITY VALUES PROCESSED
C  BY THIS PROGRAM (IE RUNFIL) AND ALL OTHER DATA NECESSARY TO
C  CONDUCT A QUANTITATIVE ANALYSIS USING THE INTERNAL STANDARD
C  OR INTENSITY RATIO METHOD.
C
C  THIS PROGRAM IS CURRENTLY DIMENSIONED FOR UP TO 30 FILES
C  WHERE EACH SAMPLE REPATITION AND MOUNTING IS CONSIDERED
C  TO BE A SEPERATE FILE
C  IN ADDITION A MAXIMUM OF 25 TOTAL LINES FOR THE
C  ENTIRE ANALYSIS ARE ALLOWED TO BE MEASURED
C  UP TO 10 UNKNOWNS PHASES CAN BE ANALYSED
C
C
C          WRITTEN BY
C
C  R. L. SNYDER   NYS COLLEGE OF CERAMICS
C                 ALFRED UNIVERSITY
C                 ALFRED, NEW YORK
C
C and
C
C  C.R. HUBBARD   NATIONAL BUREAU OF STANDARDS
C                 A221 MATL
C                 WASHINGTON D.C. 20234
C
C  DATA FILE STRUCTRE IS DEFINED IN "AUTO - A REAL TIME DIFFRACTOMETER
C  CONTROL SYSTEM" R.L. SNYDER, C.R.HUBBARD AND N. PANAGIOTOPOLOUS
C  NBS 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      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 /RECRD2/ XLAMBD,TAKEOF,DIVANG,RADIUS,SOLLIN,
     1 SOLLOT,RECSLT,DEADTM,STABIL,TBASE
	COMMON /RECR2C/ UNIT,TARGET,LINE,FOCAL,DIVSLT,MONOCH,DETECT
	COMMON /RECRD6/ XWT(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/DATA/ PEAK,VPEAK,AREA,VAREA,XBKG,VBKG
	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 /RNFIL/ KIND(NR),NUNK,L,IWT
	COMMON /RNFILC/ AVL, BL, ALL, ESDHDG(2)
	CHARACTER*6 PHASE,HKL,BL,ALL
	CHARACTER*100 TITLE
	CHARACTER*6 DATE,TIME,MAN,JKL,NAMEOV,JKLOV,NAM,KHL,RNFLNM
	CHARACTER*20 NAME
	CHARACTER*2 ELMTPH, ELM, ELEMNT
	CHARACTER*4 UNIT
	CHARACTER*2 TARGET,LINE,FOCAL,DIVSLT,MONOCH,DETECT
	CHARACTER*7 AVL
	CHARACTER*8 DAT, TIM
	CHARACTER*1 NOYES,IY
	CHARACTER*9 ESDHDG
       CHARACTER FILNAM*40
	ESDHDG(1) = ' Standard'
	ESDHDG(2) = 'Deviation'
	IY='Y'
	AVL='Wt Mean'
	BL='      '
	ALL='  all '
C
C-----LOGICAL UNIT# ASSIGNMENTS
C     IRD IS FOR CRT TERMINAL INPUT OR CARD READER INPUT
C     ITYP IS FOR CRT TERMINAL OUTPUT (REQUESTING USER RESPONSES)
C     IPRT IS FOR 132 COLUMN PRINTER OUTPUT (FINAL RESULTS)
C     LU   IS FOR AUTO DATA FILE INPUT (OPENED BY QUANT82 ROUTINES)
C     ICOBRG IS FOR COPLAND-BRAGG BINARY FILE (SEE QUANT AND COBRAG)
C     ISCRT  IS FOR TEMPORARY SCRATCH FILE IN RUNFIL
C
	IRD  = 5
	ITYP = 5
	IPRT = 5
	LU    = 10
	ICOBRG= 30
	ISCRT = 40
C-----SET THE DEBUG PRINT OUT FLAG TO OFF(=0).  ON=1
	IDBUG = 0
	CALL DATIM(DAT,TIM)
C
C  IGNORE ANY ERRORS ON OPENING THE SCRATCH FILE
C
100	WRITE(ITYP,1000)DAT,TIM
1000	FORMAT(/' ',16X,'NBS*QUANT84-RUNFIL',5X,'Revision Date:',
     & ' 29 FEB 1984'/
     * /' ',2X,'Calculates Irel and/or RIR and/or I/Io and/or '
     * ' Quantitative Analysis RUN files'/
     */' ',20X,'Written by R. L. Snyder and C. R. Hubbard  '/
     */' ', 24X,'Run on ',A8,5X,'at ',A8//' Select the option for the',
     3 ' type of RUN desired:'/
     45X,'1 - Create a RUN file for the Internal Standard Method'/
     55X,'2 - Create a RUN file for the Intensity Ratio Method'/
     65X,'3 - Compute RIR''s from the rel I''s on the files'/
     75X,'4 - Compute Relative Intensities'/
     85X,'5 - Compute RIR''s from computed rel I''s')
	READ(IRD,*)ISEL
	ITYPE = ISEL
        WRITE(ITYP,10)
10	FORMAT(/' Select Output Mode Desired:  (Output is 132 characte',
     1'rs wide)'/'       1 	- causes output to be put on the inter',
     2'active terminal.'/'       newname 	- causes output to go ',
     3'to the named file.'/'       <CR> 	- causes output to go ',
     4'to a file named RUNFIL.LIS.'/' Enter the output destination: '$)
       READ(IRD,20) FILNAM
20	FORMAT(A40)
       IF(FILNAM(1:1) .EQ. '1') GO TO 30
       IPRT = 15
       IF(FILNAM(1:1) .EQ. ' ') THEN
		OPEN(UNIT=IPRT,FILE='RUNFIL.LIS',STATUS='NEW',
     1 ACCESS='SEQUENTIAL',FORM='FORMATTED')
       		GO TO 30
       ENDIF
	OPEN(UNIT=IPRT,FILE=FILNAM,STATUS='NEW',ACCESS='SEQUENTIAL',
     1 FORM='FORMATTED')
30	IF(ITYP.NE.IPRT) WRITE(IPRT,1000) DAT,TIM
	IF(ITYPE.EQ.5.OR.ITYPE.EQ.3)ITYPE = 1
C-----LIMPRT CONTROL RAW DATA FILE PRINTING AND HEADINGS
C           =0 FOR FULL LISTING
C           =1 FOR HEADING ON FIRST FILE ONLY
C           =2 FOR BRIEFEST RAW DATA LISTING (FILE TITLE ONLY)
	WRITE(ITYP,11)
11	FORMAT(/' Select a print control option (I1):'/
     & ' 0 = Full listing of data files'/
     & ' 1 = Heading on first data file only'/
     & ' 2 = Data file titles only')
	READ(IRD,12) LIMPRT
12	FORMAT(I1)
	IF(LIMPRT.LT.0 .OR. LIMPRT.GT.3) LIMPRT=0
C
C  OPEN LOGICAL UNIT ISCRT AS A TEMPORARY SCRATCH FILE
C
	IF(ISEL.EQ.1 .OR. ISEL.EQ.2) OPEN(UNIT=ISCRT,ERR=100,
     1 STATUS='UNKNOWN',ACCESS='SEQUENTIAL',FORM='UNFORMATTED')
C
C-----ALLOW FOR USE OF UNIT WEIGHTS OR COUNTING STATISTIC WEIGHTS
C-----IN FORMING AVERAGES OF I-REL AND RIR VALUES AS WELL AS RMS
C-----DEVIATIONS.
C
110	WRITE(ITYP,1020)
1020	FORMAT(/' ','Enter weighting choice:'/
     1       ' 0 for counting statistical weights'/
     2       ' 1 for unit weights')
	READ(IRD,*) IWT
	IF( IWT .NE.0 .AND. IWT .NE.1) THEN
	  WRITE(ITYP,1030) IWT
1030	FORMAT(/' ','Input option of ',I3,' not equal to 0 or 1')
	  GOTO 110
	ENDIF
	IF(IWT.EQ.1) THEN
	   ESDHDG(1) = '   UNIT  '
	   ESDHDG(2) = ' WEIGHT  '
	   AVL = '   MEAN'
	ENDIF
C
C  THIS SECTION OF THE MAIN PROGRAM PROCESSES THE CALIBRATION FILES FOR
C  INTERNAL STANDARD OR INTENSITY RATIO METHODS OF QUANT
C
C  ISTD = THE NUMBER OF CALIBRATION PHASES TO BE ANALYSED
C         IE THE NUMBER OF DIFFERENT PHASES INPUT NOT COUNTING THE
C            INTERNAL STANDARD.
C  IREFL = THE NUMBER OF UNIQUE LINES READ IN FOR ALL
C          CALIBRATION, INTERNAL STANDARD AND/OR REFERENCE PHASES
C
	ISTD = 0
200	ISTD = ISTD + 1
	WRITE(ITYP,1050)ISTD
1050	FORMAT(/' ','Input all the files for calibration phase number',i3/
     1' ','Enter a blank for the requested file name',
     2' after the last file for this phase has been processed')
C
C  READ ALL FILES FOR THE FIRST CALIBRATION PHASE AND CALCULATE INTENSIT
C
	CALL QNDATA
C
C  CALCULATE THE AVERAGE RELATIVE INTENSITY OF EACH LINE
C
	CALL RELI(ISEL)
C
C  COMPUTE THE AVERAGE STANDARD INTENSITY FOR THIS STANDARD
C
220	IF(ISEL.EQ.4)GO TO 999
	CALL RIR(ISEL)
	IF(ISEL.GT.2)GO TO 999
C
C  WRITE THE RELATIVE INTENSITIES AND THE RIR'S FOR THE CALIBRATION PHAS
C  TO THE SCRATCH FILE CONTAINING THE RECORD 7 & 8 INFO
C  FIRST PUT A NEGATIVE FLAG ON SCRATCH FILE
C
	BANG = -1.0
	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
C NOW OUTPUT RELATIVE I'S AND RIR'S
300	WRITE(ISCRT)NREFL,(FRELS(I),SRELS(I),STDS(I),SSTDS(I),I=1,NREFL)
C
C  ONE STANDARD HAS BEEN INPUT - SEE IF THERE ARE ANY MORE
C
	WRITE(ITYP,1060)
1060	FORMAT(/' ','Are files for another calibration phase to be ',
     1'entered? (y/n)')
	READ(IRD,1015)NOYES
1015	FORMAT(A1)
	IF(NOYES.EQ.IY)GO TO 200
C
C  OUTPUT THE RUN FILE
750	CALL FILOUT(ISTD)
	CLOSE(UNIT=ISCRT,STATUS='DELETE')
C
C-----ALL DONE
C
999	CALL EXIT
	END
