	SUBROUTINE ADFILE(IFILE1,IFILE2,IDO,*)
C
C	TO ADD OR SUBTRACT FILES WITH THE SAME VALUES
C	OF RMIN, RMAX, AND DELR
C
	include 'cmpr.common'

	INTEGER STATUS,CLI$GET_VALUE,CLI$PRESENT
	LOGICAL GET_NUMBER,flag,GET_FNUMBER
	LOGICAL*1 SCALEF,CLIPF,MINF,IGNOREF,OFFSTF
	BYTE IANS
	CHARACTER newext*80
C check the upper and lower data limits, step size, axes types before continuing
	IF (fxtyp(ifile1) .ne. fxtyp(ifile2)) then 
	  WRITE(LO,'(a)') ' The files have different x axis types -- you'// 
	1	' don''t really want to do this.'
	  RETURN 1
	ELSEIF (START(IFILE1) .NE. START(IFILE2) .or. 
	1	FINISH(IFILE1) .NE. FINISH(IFILE2)) THEN
	  WRITE(LO,130) 'limits'
130	  FORMAT(/,' ERROR: The two files have different ',a,/
	1	' use the INTERPOLATE command to make them compatible before ',
	2	'using this command.')
	  RETURN 1
	ELSEIF (DELTA(IFILE1) .NE. DELTA(IFILE2)) THEN
	  WRITE(LO,130) 'steps'
	  RETURN 1
	ELSEIF (DELTA(IFILE1) .EQ. 0.0) THEN
	  WRITE(LO,'(a)') ' WARNING: The files have uneven step sizes '
C check step sizes
	  DO I=1,NPTS
	    IF (BX(I,IFILE1) .ne. BX(I,IFILE2)) THEN
	      WRITE(LO,130) 'step'
	      RETURN 1
	    ENDIF
	  ENDDO
	ELSEIF (MOD(fytyp(ifile1),64) .ne. MOD(fytyp(ifile2),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') RETURN 1
	ENDIF
C------------------------------------------------------------------------------
	CALL INFOIN(IFILE1)
C............. Parse the qualifiers from the command string............

	SCALEF = .false.
	CLIPF = .false.
	OFFSTF = .false.
	MINF = .false.
	IGNOREF = .false.
	SCALE = 1.0
	OFFST = 0.

C SCALE qualifier
	IF (CLI$PRESENT('SCALE')) THEN 
	  FLAG = GET_FNUMBER('SCALE',SCALE)
	  if (.not. FLAG) SCALEF = .true.
	ENDIF

C OFFSET qualifier
	IF (CLI$PRESENT('OFFSET')) THEN 
	  OFFSTF = .true.
	  FLAG = GET_FNUMBER('OFFSET',OFFST)
	  if (.not. FLAG) OFFSTF = .true.
	ENDIF

C TITLE qualifier
	TITLE = ' '
	IF (CLI$PRESENT('OFFSET')) STATUS = CLI$GET_VALUE('TITLE',TITLE,len)

C MINIMUM qualifier
	IF (CLI$PRESENT('MINIMUM')) THEN 
	  MINF = .true.
	ENDIF

	IF (IDO .le. -1) THEN
C CLIP qualifier
	  IF (CLI$PRESENT('CLIP')) THEN 
	    CLIPF = .true.
	  ENDIF

C IGNORE qualifier
	  IF (CLI$PRESENT('IGNORE')) THEN 
	    IGNOREF = .true.
	  ENDIF
	ENDIF
C parse for qualifiers is all done, (and about time!).

	IF (.NOT. (DEFAULT .or. SCALEF)) THEN
	  WRITE(LO,62) IFILE2
62	  FORMAT('$ENTER SCALE FACTOR [APPLIED TO FILE #',i2,
	1	' (Default:1.0)]: ')
	  READ (LI,64,end=999) SCALE
64	  FORMAT(F10.0)
	  IF(SCALE.EQ.0.0) SCALE=1.0
	ENDIF

	SCALE = IDO*SCALE
	YMIN = A(1) + SCALE*B(1,IFILE2)
	DO 55 I=1,NPTS
	  A(I) = A(I)+SCALE*B(I,IFILE2)
	  IF (A(I).LT.YMIN) YMIN=A(I)
55	CONTINUE

	IF (.not. (CLIPF .or. MINF .or. IGNOREF .or. DEFAULT)) then 
	  IF (IDO .eq. -1 .and. YMIN .LT. 0.0) THEN 
	    WRITE (LO,66)
66	    FORMAT(' THERE ARE THREE OPTIONS FOR NEGATIVE INTENSITIES:'
	1 ,/,10X,' 0 = LEAVE THEM ALONE (default)',/,10X,
	2 ' 1 = SET THEM TO ZERO',/,10X,
	3 ' 2 = SUBTRACT THE MINIMUM FROM ALL POINTS',/,'$ENTER OPTION: ')
	    READ (LI,FMT='(I)',end=999) IOPT
	    IF (IOPT .EQ. 1) CLIPF = .true.
	    IF (IOPT .EQ. 2) MINF = .true.
	  ELSEIF (.not. DEFAULT) THEN
	    WRITE (LO,56)
56	    FORMAT('$DO YOU WANT SUBTRACT THE MINIMUM FROM ALL POINTS?',
	1	' (Default=N): ')
	    READ (LI,FMT='(a1)',end=999) IANS
	    IF (IANS .eq. 'Y' .or. IANS .eq. 'y') MINF = .true.
	  ENDIF
	ENDIF

	IF (CLIPF) THEN
	  DO I=1,NPTS
	    IF (A(I) .LT. 0.0) A(I)=0.0
	  ENDDO
	  YMIN=0.0
	ELSEIF (MINF) THEN
	  DO I=1,NPTS
	   A(I) = A(I) - YMIN
	  ENDDO
	  YMIN = 0.0
	ENDIF

	IF (PROMPT .and. (.not. OFFSTF)) THEN
	  WRITE (LO,82)
82	  FORMAT ('$ENTER AN INCREMENT TO ADD TO ALL POINTS (Default:0): ')
	  READ (LI,fmt='(F10.0)',end=999) OFFST 
	ENDIF
	IF (OFFST .NE. 0) THEN
	  DO I=1,NPTS
	    A(I)=A(I)+OFFST
	  ENDDO
	ENDIF

	IF (IDO.EQ.1) THEN
	  acfnam = newext(acfnam,'.SUM')
	ELSE
	  acfnam = newext(acfnam,'.DIF')
	ENDIF

	LEN = LENCH(TITLE)
	if (len .LE. 0 .AND. PROMPT) THEN 
	  write(LO,101) IFILE1,IFILE2,ATITLE(IFILE1)(:79),ATITLE(IFILE2)(:79)
101	  FORMAT(/,' Titles for files',i3,' and',i3,' are:',
	1	2(/,1x,a),//,' Enter the title for the new file:')
	  READ(LI,fmt='(a)') TITLE
	endif
	CALL INFOUT

999	RETURN
	END
