	SUBROUTINE FILOUT(ISTD)
C
C  THIS SUBROUTINE SCANS FOR OVERLAP AND OUTPUTS THE RUN FILE
C
C  FOR INTERNAL STANDARD METHOD THE I/I(STD) COMPUTED IS REFERED
C  TO A 50:50 WT. MIXTURE OF THE CALIBRATION PHASE AND THE INTERNAL
C  INTERNAL STANDARD AND IS THEREFOR A REFERENCE INTENSITY RATIO (RIR).
C  IF THE RIR FOR YOUR INTERNAL STANDARD IS ALREADY KNOWN
C  IT MAY BE DIRECTLY INPUT TO PROGRAM AUTO ELIMINATING
C  THE NEED TO RUN STANDARDS AND CREATE A RUN FILE USING
C  THIS PROGRAM (IE. RUNFIL).
C
C  THE RUN FILE FOR EITHER INTERNAL STANDARD OR THE INTENSITY
C  RATIO METHOD WILL BE WRITTEN WITH THE LOWEST ANGLE (2 THETA)
C  LINES FIRST IN ORDER TO OPTIMIZE DIFFRACTOMETER SLEW TIME.
C
C  FOR THE INTERNAL STANDARD METHOD:
C  ALL LINES FROM THE FIRST CALIBRATION PHASE FILE WILL BE KEPT
C  ON THE RUN FILE.  FROM SUBSEQUENT FILES FOR CALIBRATION OF DIFFERENT
C  CALIBRATION PHASES ONLY THE LINES OF THE CALIBRATION PHASE WILL BE KE
C********************************************************
C  THIS IMPLIES THAT THE SAME INTERNAL STANDARD LINES
C  MUST BE USED WITH DIFFERENT PHASE TO BE ANALYSED
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)
	INTEGER IT
	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
	COMMON /RECR2C/ UNIT,TARGET,LINE,FOCAL,DIVSLT,MONOCH,DETECT,TBASE
	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/ AT(NR,NF), IT(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,TBASE,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*85 FMIOUT,FMIPRT
	CHARACTER*9 ESDHDG
	DATA FMIOUT/'(3F7.3,F7.4,F4.1,I1,F5.1,F5.2,F7.0,2A6,3I1,2I2,F6.2
     *,   F4.2,F9.4,F8.4,F8.3,2F7.2,2I1)'/
	DATA FMIPRT/'(1X,3F7.3,F7.4,F4.1,I1,F5.1,F5.2,F7.0,2A6,3I1,2I2,F
     16.2,F4.2,F9.4,F8.4,F8.3,2F7.2,2I1)'/
	DATA IY/'Y'/
	IEND=0
	MOV = 0
	MOX = 0
C-----LOGICAL UNIT NUMBER FOR CREATION OF THE RUNFILE SET TO ICOBRG
	IOUT = ICOBRG
C
C  ZERO THE ARRAYS FOR THE WEIGHTED AVERAGES
C
	DO 5 I=1,NR
	IPKS(I) = 0
	SRELS(I) = 0.0
5	SSTDS(I) = 0.0
	WRITE(ITYP,1010)
1010	FORMAT(/' Enter the name of the RUN file '/
     1,' If an extension is not typed .RUN  will be appended')
10	READ(IRD,1020)NAME
1020	FORMAT(A20)
C  it seems that LEN gives the array length not the non blank length.
C  I will therefore scan for first non blank
C       N = LEN(NAME)
       DO 12 N=1,20
       IF(NAME(N:N) .EQ. ' ')GO TO 13
12	CONTINUE
C  SAFETY DEFAULT - SET N TO 7
       N = 7
13       N = N-1
       DO 15 I=1,N
       IF(NAME(I:I) .EQ. '.')GO TO 18
15	CONTINUE
16	NAME(N+1:N+4) = '.RUN'
       GO TO 20
18	IF(NAME(N+1:N+1) .EQ. ' ')GO TO 16
	NAME(7:11) = '.RUN '
20	OPEN(UNIT=IOUT,ERR=21,FILE=NAME,STATUS='NEW',ACCESS='SEQUENTIAL',
     1 FORM='FORMATTED',IOSTAT=IOS)
	GO TO 22
C
C  ERROR ON OPENING THE FILE - TRY AGAIN
C
21	WRITE(ITYP,1021)NAME,IOS
1021	FORMAT(/' **Error** file name=',a11,' IOSTAT=',I10,
     * ' **try again**')
	GO TO 10
C
C-----SET THE FILE TYPE FLAGS
C-----IFLAG(1) FOR RUN FILE
C-----IFLAG(2) FOR ANALYSIS OF AN UNKNOWN
C-----IFLAG(3) FROM COMMON/RECRD1/ FOR INTERNAL STD OR I/Io METHOD
C-----IFLAG(4) FROM COMMON/RECRD1/
C-----IFLAG(5-10) TO ZERO
C
22	IFLAG(1) = 3
	IFLAG(2) = 1
	DO 24 I=5,10
24	IFLAG(I) = 0
C
C-----SET THE FORMATS FMIOUT AND FMIPRT FOR Fn.1 IF I/Io RUN FILE
C
	IF(IFLAG(3).EQ.1) GOTO 25
	FMIOUT(64:64)='1'
	FMIOUT(69:69)='1'
	FMIPRT(64:64)='1'
	FMIPRT(69:69)='1'
C
C-----ASK FOR TITLE AND OPERATOR'S NAME
C
25	WRITE(ITYP,1030)
1030	FORMAT(/' Enter the title for the RUN file')
	READ(IRD,1040)TITLE(1:80)
1040	FORMAT(A80)
	WRITE(ITYP,1050)
1050	FORMAT(/' Enter your name')
	READ(IRD,1020)TITLE(81:100)
	DATE = BL
	TIME = BL
	WRITE(ITYP,1060)
1060	FORMAT(' Enter the maximum time, in minutes, to be spent ',
     1'counting each peak ')
	READ(IRD,1070)TIMLIM
1070	FORMAT(G)
	WRITE(ITYP,1080)TIMLIM
1080	FORMAT(' Enter the percent error to which each peak should be ',
     1'counted (eg. 0.5%)'/
     2' If zero each peak will be counted for a fixed time of',
     3f6.2,' minutes')
	READ(IRD,1070)FIXERR
C
C  READ IN THE RECORD 7 AND 8 INFORMATION
C
C  THE VARIABLES ARE STORED AS FOLLOWS:
C
C  VARIABLE   LOCATION IN   LOCATION IN
C             AT ARRAY      IT ARRAY
C  BANG       AT(I,1)
C  PANG       AT(I,2)
C  EANG       AT(I,3)
C  DANG       AT(I,4)
C  RATE       AT(I,5)
C  MVCT                     IT(I,1)
C  TIMLIM     AT(I,6)
C  FIXERR     AT(I,7)
C  LIMCNT                   IT(I,2)
C  NAM                                   PHASE(I)
C  JKL                                   HKL(I)
C  KODE                     IT(I,3)
C  IOVLP                    IT(I,4)
C  IPK                      IT(I,5)
C  NPK                      IT(I,6)
C  NBG                      IT(I,7)
C  FREL       AT(I,8)
C  STDREL     AT(I,9)
C  STD        AT(I,10)
C  SIGSTD     AT(I,11)
C  WTPCT      AT(I,12)
C  PKTIME     AT(I,13)
C  BKGTIM     AT(I,14)
C  IOPTIM                   IT(I,8)
C  ILOC                     IT(I,9)
C
	REWIND ISCRT
	NUM = 0
	MUM = 0
	DO 155 I=1,ISTD
	DO 30 N=1,NR
	NUM = NUM + 1
	IF(NUM.LE.NR)GO TO 27
	WRITE(ITYP,1090)
1090	FORMAT(/' The NR dimension in subroutine FILOUT is',
     1' not large enough - Sorry!')
	CALL EXIT
27	J = NUM
	READ(ISCRT,END=990)(AT(J,K),K=1,5),IT(J,1),(AT(J,K),K=6,7),
     1 IT(J,2),PHASE(J),HKL(J),(IT(J,K),K=3,7),(AT(J,K),K=8,14),
     2 (IT(J,K),K=8,9),AT(J,15)
	IF(AT(J,1).LT.0)GO TO 40
	READ(ISCRT,END=990)(MAN(K,J),KHL(K,J),RELOV(K,J),RELOVS(K,J),
     1 K=1,2),(ELM(K,J),WTP(K,J),K=1,8)
C
C  ZERO THE NON ESSENTIAL PARAMETERS ON THE RUN FILE
C
	DO 28 K=12,15
28	AT(J,K) = 0.0
	DO 29 K=8,9
29	  IT(J,K) = 0
	AT(J,6) = TIMLIM
	AT(J,7) = FIXERR
30	CONTINUE
40	N = MUM + 1
C
C  SUBTRACT 2 FROM NUM ONE FOR THE NEGATIVE FLAG RECORD AND THE
C  OTHER FOR THE EXTRA RECORD WHICH QUANRD HAS TO WRITE TO THE FILE
C
	NUM = NUM - 1
C
C  WHEN MORE THAN ONE REPATITION OR MOUNTING IS INCLUDED ON THE
C  THE DATA FILE, THE FIRST RECORD OF THE SECOND REPATITION IS
C  ALSO  INCLUDED AT THE END.  THIS MUST BE SKIPPED HERE.
C
	IF(PHASE(N).EQ.PHASE(NUM).AND.HKL(N).EQ.HKL(NUM))NUM=NUM-1
	MUM = NUM
C
C  NOW READ IN THE STANDARD I'S AND RELATIVE I'S
C
	READ(ISCRT,END=990)NRT,((AT(J,K),K=8,11),J=N,MUM)
	IF(IDBUG.EQ.1) WRITE(IPRT,1234)N,MUM,(PHASE(K),HKL(K),(AT(K,KK),
     * KK=8,11),K=N,MUM)
1234	FORMAT(1X,'N, MUM = ',2I5/100(1X,2A6,4F10.4/))
	IF(I.EQ.1)GO TO 155
C
C  WE NOW MUST SCAN THE NEW INPUT FILE FOR LINES WHICH WERE
C  ALREADY READ FROM A PREVIOUS FILE AND ELIMINATE THESE FROM
C  OUR LIST.  WHERE APPROPRIATE WE WILL TAKE WEIGHTED
C  AVERAGES OF THE RELI'S AND RIR'S AND SUBSTITUTE MEASURED
C  VALUES FOR ANY INPUT VALUES.
C  THE ARRAYS IPKS, RELOVS AND SSTDS ARE NOT OTHERWISE NEEDED
C  AT THIS STAGE SO THEY WILL BE USED FOR THE WEIGHTED AVERAGES
C
	K = N
50	DO 60 J=1,N-1
	IF(PHASE(K).EQ.PHASE(J).AND.HKL(K).EQ.HKL(J).AND.
     * IT(J,5).EQ.IT(K,5)) GOTO 70
60	CONTINUE
C
C  THIS LINE HAS NOT BEEN PREVIOUSLY INPUT - SAVE IT
C
	GO TO 145
C
C  THIS LINE IS ALREADY IN LIST - IF ITS A STANDARD AVERAGE THE
C  RELI'S AND DELETE IT
C
70	IF(IT(J,3).EQ.0)GO TO 100
C
C  THIS IS A PREVIOUSLY MEASURED UNKNOWN LINE - IS IT OVERLAPPED?
C
	IF(IT(J,4).EQ.0)GO TO 90
C
C  BOTH LINES ARE OVERLAPPED - SKIP THE NEW ONE
C
	IF(IT(K,4).NE.0)GO TO 120
C
C  THE NEW LINE IS NOT BUT THE OLD ONE WAS SO USE THE NEW IREL
C
	DO 80 L=8,11
80	AT(J,L) = AT(K,L)
C
C  ELIMINATE THE NEW LINE WITHOUT SAVING INFO FOR AVERAGES
C
	GO TO 120
C
C  THE FIRST LINE WAS NOT OVERLAPPED - IS THE NEW ONE?
C
90	IF(IT(K,4).NE.0)GO TO 120
C
C  TAKE WEIGHTED AVERAGE SUMS FOR RELI AND RIR
C
100	IF(IPKS(J).GT.0)GO TO 110
C
C  SAVE THE SUMS FOR THE FIRST REFLECTION
C
	VTMP = 1.0 / (AT(J,9)*AT(J,9))
	AT(J,8) = AT(J,8) * VTMP
	SRELS(J) = SRELS(J) + VTMP
	IPKS(J) = IPKS(J) + 1
C
C  IF THIS IS A STANDARD LINE THERE IS NO RIR
C
	IF(IT(J,3).EQ.2)GO TO 110
	VTMP = 1.0 / (AT(J,11) * AT(J,11))
	AT(J,10) = AT(J,10) * VTMP
	SSTDS(J) = SSTDS(J) + VTMP
C
C  SAVE THE SUMS FOR THE NEW REFLECTION
C
110	VTMP = 1.0 / (AT(K,9) * AT(K,9))
	AT(J,8) = AT(J,8) + AT(K,8) * VTMP
	SRELS(J) = SRELS(J) + VTMP
	IPKS(J) = IPKS(J) + 1
	IF(IT(J,3).EQ.2)GO TO 120
	VTMP = 1.0 / (AT(K,11) * AT(K,11))
	AT(J,10) = AT(J,10) + AT(K,10) * VTMP
	SSTDS(J) = SSTDS(J) + VTMP
C
C  ELIMINATE THE NEW REFLECTION FROM THE LIST
C
120	IF(K.EQ.MUM)GO TO 140
	DO 135 J=K,MUM
	DO 125 L=1,15
125	AT(J,L) = AT(J+1,L)
	DO 130 L=1,9
130	IT(J,L) = IT(J+1,L)
	PHASE(J) = PHASE(J+1)
	HKL(J) = HKL(J+1)
	DO 132 L=1,2
	MAN(L,J) = MAN(L,J+1)
	KHL(L,J) = KHL(L,J+1)
	RELOV(K,J) = RELOV(K,J+1)
132	RELOVS(K,J) = RELOVS(K,J+1)
	DO 133 L=1,8
	ELM(L,J) = ELM(L,J+1)
133	WTP(L,J) = WTP(L,J+1)
135	CONTINUE
140	NUM = NUM - 1
	MUM = MUM - 1
	GO TO 150
145	K = K + 1
150	IF(K.LE.MUM)GO TO 50
155	CONTINUE
	NREFL = MUM
C
C  COMPUTE THE WEIGHTED AVERAGES FOR ANY MULTIPLY DETERMINED
C  STANDARDS
C
	DO 158 I=1,NREFL
	IF(IPKS(I).EQ.0)GO TO 158
	AT(I,8) = AT(I,8) / SRELS(I)
	AT(I,9) = SQRT(1.0 / SRELS(I))
	IF(IT(I,3).EQ.2)GO TO 158
	AT(I,10) = AT(I,10) / SSTDS(I)
	AT(I,11) = SQRT(1.0 / SSTDS(I))
158	CONTINUE
	NREFL = MUM
C
C  CHECK THAT THE SAME SCALE REFLECTION WAS USED IN EACH MEASURMENT
C  OF THE UNKNOWNS AND INTERNAL STANDARD
C
C
	DO 170 I=1,NREFL
C
C  SCALE REFLECTIONS HAVE SIGMA IREL = 0.0
C
	IF(AT(I,9).NE.0.0)GO TO 170
C
C  WE NOW HAVE A SCALE REFLECTION SEE IF ANY OTHER REFLECTION
C  FROM THIS PHASE WAS ALSO USED AS A SCALE
C
	DO 165 J=1,NREFL
	IF(J.EQ.I)GO TO 165
	IF(PHASE(I).NE.PHASE(J))GO TO 165
	IF(HKL(I).NE.HKL(J))GO TO 165
	WRITE(ITYP,1095)PHASE(I),HKL(I),HKL(J)
1095	    FORMAT(//' ******************Bad News - Chariots comming - B',
     1'ad News**************************'/' Phase ',A6,' was measured',
     2' in two seperate standards and a different scale reflection',
     3' was used in determining the relative intensities'/
     4' Reflection ',A6,' one time and ',A6,' the next',
     5' the relatve intensity ratios are not on the same scale'/
     6' You havent got a prayer unless you hand compute the RIR''s',
     7' and edit them into the RUN file!!'/' *******************',
     8'*********************************************************')
165	   CONTINUE
170	   CONTINUE
C  AT THIS POINT ALL OF THE VALUES FOR THE FIRST PHASE NAMED
C  ON ANY RECORD HAVE BEEN AVERAGED OVER ALL OBSERVATIONS
C  AND, WHERE AVAILABLE, EXPERIMENTAL VALUES OF IREL HAVE
C  BEEN INSERTED.  IN THE OUTPUT LOOP BELOW WE WILL SCAN
C  THE OVERLAPPED PHASES DECLARED IN THE RECORD 8 INFORMATION
C  TO SEE IF ANY OF THESE WERE RUN UN-OVERLAPPED AND AGAIN
C  THE EXPERIMENTAL VALUES, WHEN AVAILABLE, WILL BE USED.
C
C
C  WE NOW MUST SORT THE LINES ON PEAK ANGLE
C
	DO 200 I=1,NREFL-1
	DO 200 J=I+1,NREFL
	IF(AT(I,2).LE.AT(J,2))GO TO 200
	DO 180 K=1,15
	A = AT(I,K)
	AT(I,K) = AT(J,K)
	AT(J,K) = A
180	CONTINUE
	DO 185 K=1,7
	II = IT(I,K)
	IT(I,K) = IT(J,K)
	IT(J,K) = II
185	CONTINUE
	DATE = PHASE(I)
	PHASE(I) = PHASE(J)
	PHASE(J) = DATE
	DATE = HKL(I)
	HKL(I) = HKL(J)
	HKL(J) = DATE
	DO 190 K=1,2
	DATE = MAN(K,I)
	MAN(K,I) = MAN(K,J)
	MAN(K,J) = DATE
	DATE = KHL(K,I)
	KHL(K,I) = KHL(K,J)
	KHL(K,J) = DATE
190	CONTINUE
	DO 195 K=1,8
	TARGET = ELM(K,I)
	ELM(K,I) = ELM(K,J)
	ELM(K,J) = TARGET
	A = WTP(K,I)
	WTP(K,I) = WTP(K,J)
	WTP(K,J) = A
195	CONTINUE
200	CONTINUE
	CALL DATIM(DAT,TIM)
C
C-----WRITE RECORD 1 - PROBLEM SPECIFICATIONS
C
	WRITE(IOUT,1000)    TITLE,DATE,TIME,IFLAG
1000	FORMAT(A100,2A6,10I1)
	WRITE(IPRT,1001) NAME,TITLE,DATE,TIME,IFLAG
1001	FORMAT(///' Listing of the output RUN file:',a11//
     1 1X,A100,2A6,10I1)
C
C  OUTPUT THE DATA RECORDS FOR EACH LINE
C
	IFLG = 0
	JFLG = 0
	I = 0
250	I = I + 1
	IF(I.GT.NREFL)GO TO 999
C-----TEST IF THE NEXT LINE HAS ALREADY BEEN OUTPUT
	LN = I
	IF(PHASE(LN).EQ.'      ') GOTO 250
C-----IF THIS IS THE LAST LINE OVERLAP IS IMPOSSIBLE
	IF(I.EQ.NREFL)GO TO 380
C-----SET POINTER TO POSSIBLE OVERLAP REFLECTION
260	LM = I + 1
	IF(LM.GT.NREFL) GOTO 380
C-----TEST IF THIS LINE HAS ALREADY BEEN OUTPUT
	IF(PHASE(LM).EQ.'      ') GOTO 260
C
C  IS THIS AN OVERLAPPED LINE BEING SET BY THIS PROGRAM?
C
	IF(IFLG.GT.0)GO TO 270
C
C  TEST IF OVERLAP FLAG IS SET FOR THIS LINE
C
	IF(IT(I,4).GT.0)GO TO 340
C
C  THE OVERLAP FLAG IS NOT SET SO WE MUST CHECK RESOLUTION
C
C
C  IF BOTH LINES WERE MEASURED IN INTEGRATE MODE
C  AND BACKGROUND POINTS ARE IDENTICAL FORCE AN OVERLAP CONDITION
C
270	IF(AT(LN,1).EQ.AT(LM,1).AND.AT(LN,3).EQ.AT(LM,3)
     1 .AND. IT(LN,5).EQ.0 .AND. IT(LM,5).EQ.0)GO TO 315
	TEM = AT(LM,2) - AT(LN,2)
C-----IF FIRST PEAK IS IN INTEGRATE MODE GOTO 310
	IF(IT(I,5).EQ.0)GO TO 310
C-----LINE LN IS PK HEIGHT MODE.  TEST FOR POSSIBLE SERIOUS OVERLAP
	IF(TEM.GT.0.50)GO TO 380
C-----IF SECOND PEAK IS IN INTEGRATE MODE THEN ASK IF PK HT TO BE SKIPPE
	IF(IT(LM,5).NE.0) GOTO 280
	WRITE(ITYP,1063) PHASE(LN),HKL(LN),AT(LN,2),PHASE(LM),HKL(LM),
     * AT(LM,2)
1063	FORMAT(/' The line ',2A6,F8.3,' is pk height mode and'/
     *       ' ','The line ',2A6,F8.3,' is integrate mode'/
     * ' ','Do you wish to collect peak height as well as integrated'
     * ' data? (Y/N)')
	READ(IRD,1110) NOYES
	IF(NOYES.EQ.IY .OR. NOYES.EQ.'y') GOTO 380
	PHASE(LN)='      '
	GOTO 250
C
C  IF PEAK COUNT MODE AND PEAKS ARE CLOSER THAN .25 DEGREES
C  PRINT A WARNING BUT DO NOT FORCE AN OVERLAP CONDITION
C
280	IF(TEM.GT.0.25) GOTO 380
	WRITE(ITYP,1065)PHASE(LN),HKL(LN),AT(LN,2),IT(LN,5),
     1 PHASE(LM),HKL(LM),AT(LM,2),IT(LM,5)
1065	FORMAT(/' ******************WARNING**************'/
     1' The following two lines are within 0.25 degrees of',
     2' each other in peak count mode'
     3' you may wish to integrate the cluster',
     4/2(1X,2A6,F8.3,' IPK=',I1))
	GO TO 380
C
C  WE ARE IN INTEGRATE PEAK COUNT MODE CHECK IF THE TWO REFLECTIONS
C  ARE WITHIN 1.0 DEGREES OF EACH OTHER
C
310	IF(TEM.GT.1.0)GO TO 380
C  THEY ARE WITHIN 1 DEGREE OF EACH OTHER ASK IF AN
C  OVERLAP CONDITION SHOULD BE SET
C
C
C  CHECK IF THE OVERLAP FLAG IS SET ON THE SECOND LINE
C
	IF(IT(LM,4).EQ.0)GO TO 313
	DO 311 J=1,IT(LM,4)
	IF(PHASE(LN).EQ.MAN(J,LM).AND.HKL(LN).EQ.KHL(J,LM))GO TO 312
311	CONTINUE
C
C  THIS LINE IS NOT INCLUDED IN THE OVERLAP INFO - ASK IF OVERLAP
C  IS DESIRED
C
	JFLG = 1
	L = 2
	GO TO 313
C
C  THIS LINE IS ALREADY DECLARED AS OVERLAPPED
C
312	L = J
	GO TO 333
C
C-----CHECK IF THE SECOND LINE IS PEAK HT MODE WHILE THE FIRST IS INTEGR
C
313	IF(IT(LM,5).EQ.0) GOTO 314
C
C-----SECOND LINES IS PEAK MODE.  DO NOT CONSIDER AS OVERLAP POSSIBILITY
C-----DO NEED TO CHECK FOLLOWING PEAK (GOTO 270)
C-----FIRST ASK IF PEAK HEIGHT LINE NEEDS TO BE MEASURED AT ALL SINCE
C-----AN INTEGRATION IS BEING DONE WHICH INCLUDES IT
C
	WRITE(ITYP,1096) PHASE(LN),HKL(LN),AT(LN,2),PHASE(LM),HKL(LM),
     * AT(LM,2)
1096	FORMAT(/' The line ',2A6,F8.3,' is specified integrate mode and'/
     *        ' The line ',2A6,F8.3,' is specified peak height mode'/
     * ' Do you wish to collect peak height as well as integrated data'
     * ' (yes/no)?')
	READ(IRD,1110) NOYES
	IF(NOYES.NE.IY .or. noyes.eq.'y')  PHASE(LM)='      '
	LM = LM + 1
	IF(LM.GT.NREFL) GOTO 380
	GOTO 270
C
C-----BOTH LINES ARE INTEGRATE MODE AND POSSIBLE OVERLAP CASE
C
314	WRITE(ITYP,1100)PHASE(LN),HKL(LN),AT(LN,2),IT(LN,5),
     1 PHASE(LM),HKL(LM),AT(LM,2),IT(LM,5)
1100	FORMAT(/' The following two lines are within 1 degree',
     1 ' of each other'/2(1X,2A6,F8.3,' IPK=',I1)/
     * ' Should they be treated as overlapping? (y/n)')
	READ(IRD,1110)NOYES
1110	FORMAT(A1)
	IF(NOYES.NE.IY)GO TO 380
C
C  THE TWO REFLECTIONS ARE OVERLAPPED
315	WRITE(IPRT,1120)PHASE(LN),HKL(LN),PHASE(LM),HKL(LM)
	IF(ITYP.NE.IPRT) WRITE(ITYP,1120) PHASE(LN),HKL(LN),PHASE(LM),
     * HKL(LM)
1120	FORMAT(' Overlap condition being set for reflections:',
     12(2X,2A6))
C  SET THE INTEGRATION SCAN LIMITS FROM LOWEST ANGLE TO HIGHEST
C  LEAVE THE PEAK ANGLE SET FOR FIRST (LOWEST ANGLE) LINE
C
320	   KK = LM
	IF(IFLG.EQ.1)KK = LN
	IF(JFLG.EQ.1)GO TO 335
	IT(KK,4) = IT(KK,4) + 1
	IF(IT(KK,4).LE.3)GO TO 330
C
C  THERE ARE TWO MANY OVERLAPPED REFLECTIONS
C
325	WRITE(ITYP,1130)
	IF(IPRT.NE.ITYP) WRITE(IPRT,1130)
1130	FORMAT(/' There are more than 2 reflections overlapping',
     1 ' this line'/' All others will be skipped')
	IT(LN,1) = IT(LN,1) - 1
	GO TO 250
C
C  SAVE THE I'TH REFLECTION AS OVERLAP INFO ON I+1
C
330	IF(AT(LN,1).LT.AT(LM,1))AT(LM,1) = AT(LN,1)
	IF(AT(LN,3).GT.AT(LM,3))AT(LM,3) = AT(LN,3)
	IF(JFLG.EQ.1)GO TO 333
	L = 1
	IF(IFLG.EQ.1)GO TO 335
333	RELOV(L,LM) = AT(LN,8)
	RELOVS(L,LM) = AT(LN,9)
	MAN(L,LM) = PHASE(LN)
	KHL(L,LM) = HKL(LN)
	PHASE(LN)='      '
	IF(IFLG.EQ.0)GO TO 334
	IFLG = 0
	GO TO 250
334	IFLG = 1
	GO TO 250
335	L = 2
	RELOV(L,LN) = AT(LM,8)
	RELOVS(L,LN) = AT(LM,9)
	MAN(L,LN) = PHASE(LM)
	KHL(L,LN) = HKL(LM)
	PHASE(LM)='      '
	GO TO 380
C
C  GET THE STANDARD I AND RELATIVE I FOR THE OVERLAPPING
C  PHASE AND STORE IN THE PROPER ARRAY POSITIONS
C
340	L = 1
345	DO 350 J=1,NREFL
	IF(PHASE(J).EQ.MAN(L,I).AND.HKL(J).EQ.KHL(L,I))
     1 GO TO 351
350	CONTINUE
	WRITE(ITYP,1122)MAN(L,I),KHL(L,I),RELOV(L,I)
	IF(ITYP.NE.IPRT) WRITE(IPRT,1122)MAN(L,I),KHL(L,I),RELOV(L,I)
1122	FORMAT(' A standard determination for overlapped',
     1' line ',2A6,' was not requested by you'/
     1' The input reli of ',F6.2,' will be used------',
     2'be warned this must be on the same scale as your',
     3' other reli''s-----------')
	GO TO 378
C
C  AN INDEPENDANT DETERMINATION OF IREL WAS MADE
C  WE WILL USE THIS VALUE ON THE OVERLAP POSITION OF LINE I
C  AND ELIMINATE THE INDEPENDANT MEASURE (LINE J) FROM THE LIST
C
351	RELOV(L,I) = AT(J,8)
	RELOVS(L,I) = AT(J,9)
	IF(J.EQ.NREFL)GO TO 362
	DO 360 K=J,NREFL
	DO 352 LL=1,14
352	AT(K,LL) = AT(K+1,LL)
	DO 354 LL=1,9
354	IT(K,LL) = IT(K+1,LL)
	PHASE(K) = PHASE(K+1)
	HKL(K) = HKL(K+1)
	DO 356 LL=1,2
	MAN(LL,K) = MAN(LL,K+1)
	KHL(LL,K) = KHL(LL,K+1)
	RELOV(LL,K) = RELOV(LL,K+1)
	RELOVS(LL,K) = RELOVS(LL,K+1)
356	CONTINUE
	DO 358 LL=1,8
	ELM(LL,K) = ELM(LL,K+1)
358	WTP(LL,K) = WTP(LL,K+1)
360	CONTINUE
362	NREFL = NREFL - 1
378	IF(L.EQ.2)GO TO 380
	IF(IT(I,4).EQ.1)GO TO 380
	L = 2
	GO TO 345
C
C  SUM THE NUMBER OF UNOVERLAPPED STANDARD LINES
C
380	IF(IT(LN,3).EQ.2.AND.IT(LN,4).EQ.0)MOV = MOV + 1
	MOX = MOX + 1
C
C  OUTPUT RECORDS 7 AND 8
C
       fimcnt = it(ln,2)
	WRITE(IOUT,FMIOUT)(AT(LN,J),J=1,5),IT(LN,1),
     1 (AT(LN,J),J=6,7),FIMCNT,PHASE(LN),HKL(LN),
     2 (IT(LN,J),J=3,7),(AT(LN,J),J=8,14),
     3 (IT(LN,J),J=8,9)
	WRITE(IPRT,FMIPRT)(AT(LN,J),J=1,5),IT(LN,1),
     1 (AT(LN,J),J=6,7),FIMCNT,PHASE(LN),HKL(LN),
     2 (IT(LN,J),J=3,7),(AT(LN,J),J=8,14),
     3 (IT(LN,J),J=8,9)
C
C-----WRITE LINE OVERLAP AND CHEMICAL INFORMATION FOR THIS LINE(PHASE)
C
	WRITE(IOUT,1220)(MAN(J,LN),KHL(J,LN),RELOV(J,LN),
     1 RELOVS(J,LN),J=1,2),(ELM(J,LN),WTP(J,LN),J=1,8)
1220	FORMAT(2(2A6,F6.2,F6.2),8(A2,F8.4))
	WRITE(IPRT,1230)(MAN(J,LN),KHL(J,LN),RELOV(J,LN),
     1 RELOVS(J,LN),J=1,2),(ELM(J,LN),WTP(J,LN),J=1,8)
1230	FORMAT(1X,2(2A6,F6.2,F6.2),8(A2,F8.4))
	JFLG = 0
	IFLG = 0
	GO TO 250
990	WRITE(IPRT,1250)
	IF(ITYP.NE.IPRT) WRITE(ITYP,1250)
1250	FORMAT(/' Unexpected end of file on scratch file ISCRT')
999	ENDFILE(IOUT)
	IF(IFLAG(3).EQ.2)MOV = MOX
	WRITE(IPRT,1260)MOX,MOV
	IF(IPRT.NE.ITYP) WRITE(ITYP,1260)MOX,MOV
1260	FORMAT(//1X,I5,' Lines output to the runfile with',I5,
     1' unoverlapped standard lines')
	IF(iflag(3) .eq.1 .and. MOV.EQ.0)WRITE(IPRT,1270)
	IF(iflag(3).eq.1.and.MOV.EQ.0 .AND. ITYP.NE.IPRT)
     1 WRITE(ITYP,1270)
1270	FORMAT(/' You must have at least one fully resolved ',
     1'standard line - so this run file is hopeless - Sorry!')
	CLOSE(UNIT=IOUT,STATUS='KEEP')
	RETURN
	END
