	SUBROUTINE CMFILE(ISP,IFIL,STATUS)
C
C	TO COMBINE FILES WITH DIFFERENT TWO-THETA RANGES, DIFFERENT
C	STEP SIZES, DIFFERENT COUNTING TIMES, AND DIFFERENT MONITOR
C	COUNTS.
C
	include 'cmpr.common'

	INTEGER STATUS,IFIL(*),NSP(M$FIL)
	
	REAL*4 SCALE(M$FIL)
	BYTE ians
                                                         
	INTEGER CLI$GET_VALUE,CLI$PRESENT
	LOGICAL GET_NUMBER,flag,GET_FNUMBER
	CHARACTER NEWEXT*80
C cleanups by Brian Toby -- removed a problem that caused points to be binned
C in the wrong slot -- this code has not been well tested.

C
	IF ((MFILE+1) .gt. M$FIL) then
	  WRITE(lo,'(a)') ' No file slots remain for the combined file'//
	1	' Delete at least one file from the working set.'
	  goto 3
	ENDIF

	CALL INFOIN(IFIL(1))
	DO I=2,ISP
	IF (fxtyp(IFIL(1)) .ne. fxtyp(IFIL(I))) then
	  WRITE(LO,'(a)') ' The files have different x axis types -- you'//
	1	' don''t really want to do this.'
	  goto 3
	ELSEIF (MOD(fytyp(IFIL(1)),64) .ne. MOD(fytyp(IFIL(I)),64)) then
	  WRITE(LO,'(a)') ' The files have different Y axis types -- do you'
	1	//' really want to do this? ([Y]/N):  '
	  read (li,'(a)') ians
	  if (ians .eq. 'N' .or. ians .eq. 'n') goto 3
	ELSEIF (INST(IFIL(1)) .ne. INST(IFIL(I)) .or.
	1        SOURCE(IFIL(1)) .ne. SOURCE(IFIL(I))) then
	  WRITE(LO,'(a)') ' The files are not from the same instrument --'
	1	//' do you really want to do this? ([Y]/N): '
	  READ (li,'(a)') ians
	  if (ians .eq. 'N' .or. ians .eq. 'n') goto 3
	ENDIF
	IF (START(IFIL(I)).LT.RMIN) RMIN=START(IFIL(I))
	IF (FINISH(IFIL(I)).GT.RMAX) RMAX=FINISH(IFIL(I))
	IF (DELTA(IFIL(I)).LT.DELR) DELR=DELTA(IFIL(I))
	ENDDO

	NPTS=((RMAX-RMIN)/DELR)+1
	IF (NPTS.GT.M$PTS) THEN
	  FACTOR=FLOAT(NPTS)/FLOAT(M$PTS)
	  DELR=FACTOR*DELR
C	ROUND DELR UP TO CONVENIENT NUMBER
	  IDELR=IFIX(200.0*DELR)+1
	  DELR=0.005*FLOAT(IDELR)
	  NPTS=((RMAX-RMIN)/DELR)+1
	  RMAX=(NPTS-1)*DELR+RMIN
	ENDIF

13	WRITE (LO,10) ISP,'#','MIN(2T)','MAX(2T)','DEL(2T)','TIME',
	1	'REFINT'
10	FORMAT(/' Values for the',i2,' files to be combined are:',/,
	1	1x,a3,5a10)
	DO I=1,ISP
	  WRITE (LO,11) ifil(i),START(IFIL(I)),FINISH(IFIL(I)),
	1	DELTA(IFIL(I)),TIME(IFIL(I)),REFINT(IFIL(I))
11	  FORMAT (1x,i3,4F10.3,F10.2)
	ENDDO

	WRITE (LO,14) 'new',RMIN,RMAX,DELR,PTIME,REFPT
14	FORMAT (/,1x,a3,4F10.3,F10.2)
	WRITE (LO,15)
15	FORMAT (/'$Are the suggested new values acceptable? ([Y]/N): ')
	READ (LI,'(a)') IANS
	IF (IANS.EQ.'N'.OR.IANS.EQ.'n') THEN
17	  FORMAT ('$Enter a value for ',a,' [',f11.3,']: ')
16	  WRITE(lo,17) 'MIN(2T)',RMIN
	  read (li,'(Q,f10.0)',end=3,err=16) i,xsave
	  if (i .gt. 0) RMIN = XSAVE
	  WRITE(lo,17) 'MAX(2T)',RMAX
	  read (li,'(Q,f10.0)',end=3,err=16) i,xsave
	  if (i .gt. 0) RMAX = XSAVE
	  WRITE(lo,17) 'DEL(2T)',DELR
	  read (li,'(Q,f10.0)',end=3,err=16) i,xsave
	  if (i .gt. 0) DELR = XSAVE
	  WRITE(lo,17) 'TIME',PTIME
	  read (li,'(Q,f10.0)',end=3,err=16) i,xsave
	  if (i .gt. 0) PTIME = XSAVE
	  WRITE(lo,17) 'REFINT',REFPT
	  read (li,'(Q,f10.0)',end=3,err=16) i,xsave
	  if (i .gt. 0) REFPT = XSAVE
	  GO TO 13
	ENDIF
	IF (DELR.GT.0.0) THEN
	  NPTS=(RMAX-RMIN)/DELR + 1
	  IF (NPTS.GT.M$PTS) THEN
	    WRITE (LO,19) NPTS,M$PTS
19	    FORMAT(' THE NUMBER OF DATA POINTS,'I5
	1	', EXCEEDS DIMENSION OF ',I6/)
	    GOTO 16
	  ENDIF
	ENDIF

	DO I=1,ISP
	  IFILE=IFIL(I)
C+++ added to avoid divide by zero errors +++ (BHT)
	  SCALE(IFILE) = 1.
	  IF (PTIME .ne. 0 .and. TIME(IFILE) .ne. 0) 
	1	SCALE(IFILE) = SCALE(IFILE) * (PTIME/TIME(IFILE))
	  IF (REFPT  .ne. 0 .and. REFINT(IFILE) .ne. 0) 
	1	SCALE(IFILE) = SCALE(IFILE) * (REFPT/REFINT(IFILE))
C++++++++++++++++++++++++++++++++++++++++++++ (BHT)
	  NSP(IFILE)=1
	ENDDO

C	THERE ARE NOW TWO SITUATIONS:
C	(1) DELR=0.0, WHICH MEANS THAT X VALUES CAN BE ANYTHING.  I HAVE
C		DECIDED TO SIMPLY DETERMINE THE NUMBER OF UNIQUE X VALUES
C		WITHIN THE FILES BEING SUMMED BY MAKING A BIG ARRAY OF ALL
C		THE POSSIBLE X VALUES, THEN SORT THEM AND ELIMINATE THE
C		REDUNDANCIES.  THIS MEANS THAT THE RESULTING FILE WILL NOT
C		BE CORRUPTED BY THE 'SMOOTHING' CAUSED BY INTERPOLATING
C		BETWEEN POINTS.
	IF (DELR.EQ.0.0) THEN
	  NP=0
	  DO 40 I=1,ISP
	    IFILE=IFIL(I)
	    IF (START(IFILE).GT.RMAX.OR.FINISH(IFILE).LT.RMIN) GO TO 40
	    DO 35 J=1,NPOINT(IFILE)
	      X=BX(J,IFILE)
	      IF (X .le. RMIN .and. X .gt. RMAX) THEN
C Is this point already in the unsorted list?
	        DO K=1,NP
		  IF (AX(K) .eq. X) GOTO 35	        
		ENDDO
		IF (NP+1 .GT. M$PTS) THEN
		  WRITE (LO,'(A)') 
	1	' The number of points in combined file has exceeded'
	1	//' the program dimensions.',
	1	' Please choose a different set of pattern parameters.',' '
		  GO TO 13
	        ENDIF
	        NP=NP+1
		AX(NP) = X
	      ENDIF
35	    CONTINUE
40	  CONTINUE

C	SORT THE A ARRAY AND PLACE UNIQUE VALUES IN AX ARRAY
	  CALL XXSORT(AX,NP)
	  NPTS=NP
C now zero out A and the AESD arrays (BHT)
	  DO I=1,NPTS
	    A(I) = 0.0
	    AESD(I) = 0.0
	  ENDDO
C	  AND JUST IN CASE,
	  RMIN=AX(1)
	  RMAX=AX(NPTS)
	  DO J=1,NPTS
	    X = AX(J)
	    FRACON=0.0
	    DO 79 I=1,ISP
	      IFILE=IFIL(I)
	      IF (START(IFILE).GT.X.OR.FINISH(IFILE).LT.X) GO TO 79
C	FOR DELR=0.0, THE INTENSITIES CAN SIMPLY BE SUMMED AT THE
C		APPROPRIATE X VALUES.
	      DO N=NSP(IFILE),NPOINT(IFILE)
		IF (ABS(X-BX(N,IFILE)).LE.0.0001) THEN
		  FRACON=FRACON+1.0
		  A(J)=A(J)+SCALE(IFILE)*B(N,IFILE)
		  BES=SCALE(IFILE)*BESD(NP,IFILE)
		  AESD(J)=AESD(J)+BES*BES
		  NSP(IFILE)=N+1
D	      WRITE (LO,666) J,X,IFILE,NP,BX(NP,IFILE),DX
D666	      FORMAT (' J,X =',I5,F7.2,' IFILE,NP,BX,DX =',2I5,2F7.2)
		  GO TO 79
		ENDIF
	      ENDDO
79	    CONTINUE
	    IF (FRACON.NE.0.0) THEN
	      A(J)=A(J)/FRACON
	      AESD(J)=SQRT(AESD(J))/FRACON
	    ENDIF
	  ENDDO
	ELSE
C++++++++++++++++++++
C	(2) DELR.GT.0.0, WHICH MEANS THAT THE X VALUES WILL BE IN EQUAL
C		INCREMENTS ALONG X.  REBINNING WILL BE DONE UNLESS 
C		THE ORIGINAL FILES ALL HAVE THE SAME DELR VALUES AND 
C		THE STARTING ANGLES JUST HAPPEN TO COINCIDE WITH X VALUES 
C		IN THE SUMMED FILE.
	  DO I=1,NPTS
	    AX(I) = RMIN + (I-1) * DELR
	    A(I)=0.0
	    AESD(I)=0.0
	  ENDDO

C	   THE PROCESS HERE IS TRICKIER BECAUSE SOME FILES MAY
C	   HAVE INDIVIDUAL DELR VALUES WHICH ARE LESS THAN THAT FOR
C	   SUMMED FILE AND I WANT TO INCLUDE THE INTENSITY INFORMATION
C	   FROM ALL OF THIS HIGHER RESOLUTION DATA. 
C	   Note that if one uses DELR less than
C	   the individual DELR values, one may end up with zero data 
C	   points -- no interpolation is done
	  DO J=1,NPTS
	    X = AX(J)
	    FRACON=0.0
	    DO 89 I=1,ISP
	      IFILE=IFIL(I)
	      IF (START(IFILE).GT.X.OR.FINISH(IFILE).LT.X) GO TO 89
	      DO N=NSP(IFILE),NPOINT(IFILE)
	        DX = X - BX(N,IFILE)
C does the current point lay in this bin?
	        IF (BX(N,IFILE) .GT. X - DELR/2. .and. 
	1		BX(N,IFILE) .LT. X + DELR/2.) THEN
C N.B. This weights each point equally in the summation -- should 
C      really be weighted by ESD's
		  A(J) = A(J) + SCALE(IFILE)*B(N,IFILE)
		  BES = SCALE(IFILE) * BESD(N,IFILE)
		  AESD(J)=AESD(J) + BES*BES
	          FRACON=FRACON+1.0
C don't reuse this point
	          NSP(IFILE) = N
D	          WRITE (LO,666) J,X,IFILE,N,BX(N,IFILE),DX
	        ENDIF
	      ENDDO
89	    CONTINUE
	    IF (FRACON.NE.0.0) THEN
	      A(J)=A(J)/FRACON
	      AESD(J)=SQRT(AESD(J))/FRACON
	    ENDIF
	  ENDDO
	ENDIF

	ACFNAM = NEWEXT(ACFNAM,'.CMB')
	WRITE (LO,'(a)') ' ENTER THE TITLE FOR THE COMBINED FILE:'
	READ (LI,'(A)') TITLE
	NS=1
	NF=NPTS
	STR=RMIN
	FIN=RMAX
	CSTR=RMIN
	CFIN=RMAX
	CALL INFOUT
	STATUS = .true.
	RETURN
C error condition
3	STATUS = .false.
	RETURN
	END
C----------------------------------------------------------------------
	SUBROUTINE XXSORT (XX,NP)
C
C  SORTS XX ARRAY FROM LOW TO HIGH
C
	LOGICAL NSWAP
	DIMENSION XX(NP)
	N=NP-1
100	NSWAP = .false.
	DO 300 K=1,N
	  IF (XX(K).GT.XX(K+1)) THEN
	    TEMP=XX(K+1)
	    XX(K+1)=XX(K)
	    XX(K)=TEMP
	    NSWAP = .true.
	  ENDIF
300	CONTINUE
	IF (NSWAP) GO TO 100
	RETURN
	END
