     	SUBROUTINE DIADIF
C
C  PROGRAMMER:  R.S. ZHOU
C		ALFRED UNIVERSITY,  FEB-1987
C
C	THIS ROUTINE CONDUCTS THE INTERATIVE DIALOGUE
C	FOR INPUT DIFILE CONVERSION.

     	INCLUDE 'FILECONV.CBS'
     	CHARACTER TEXT*80, SAMP*80, USER*12, SYM*1, KEY*2
        
    	DIMENSION RP(10), KP(10)
     	EQUIVALENCE (SAMP, SLID), (USER, NAME)

     	LTYP = 0		!OUTPUT PEAK TYPE FLAG: ALL PEAKS
     	IPEAK = 1		!PEAK # POINTER (BEING DISPLAYED)
C
C  SCREEN THE LISTED PEAKS BY "CUTI" AND "CUTF"...
C

     	CALL SCREEN( LTYP, IPEAK )
C
C  DISPLAY THE DIALOGUE MENU...
C

1	CALL CLEARS( 0 )
     	CALL COLORS(-1)					!200
     	CALL COLORS(80)
     	TEXT = 'VAXCONV --- Dialogue Menu for D/I File Conversion'
     	CALL HEADER( TEXT )
     	CALL COLORS(1)
     	WRITE(6,2)
2	FORMAT(
     	1'  INPUT FILE:',27X,'INPUT FILE TYPE:'/
     	1' OUTPUT FILE:',26X,'OUTPUT FILE TYPE:'/
     	1' 1=SAMPLE ID:'/' 2=RUN DATE:',11X,'3=RUN TIME:',10X,'4=USER',
     	1' NAME:'/' 5=ANODE:'4X'6=KA1:'9X'7=KA2:'9X'8=KA:',
     	19X'9=KA1/KA2 RATIO:'/' 10=INTENSITY CUTOFFS(%):',16X,'11=FWHM',
     	1' CUTOFFS(D):'/' 12=OUTPUT PEAK TYPE(S) (0=ALL,1=KA1 ONLY):',
     	17X'13=USE RESIDUAL PATTERN:'/X,80('=')/' Only the numbered',
     	1' fields can be accessed to and be changed interactively.'/
     	1' e.g. INPUT> 1=Silicon internal calibration run<return>')
     	CALL COLORS(90)		!UNDERLINE ON
     	WRITE(6,3)
3	FORMAT(' TOT #TH    2-THETA(O,C)   D(O)    D(C)',
     	1'  REL.INT   AREA     FWHM    H  K  L TYPE ')
     	CALL COLORS(1)		!UNDERLINE OFF
     	WRITE(6,4)
4	FORMAT(7X,'1>'/,X,80('=')/' In the above, O=Observed,',
     	1' C=Calibrated. To edit peaks, do the followings:'/
     	1' INPUT> 20;<return> ---> show peak #20; INPUT> 20/<return>',
     	1' ---> delete peak #20'/' INPUT> 20>28.443 28.435,,,15,,0.23',
     	1' 1 1 1 ka1.....<return> ---> modify peak #20'/
     	1' INPUT> 999>65.45 65.455,,,20,,0.15 2 1 1 ka1.....<return>',
     	1' ---> add a new peak'/' INPUT> N<return> --> display next',
     	1' line;  INPUT> B<return> ---> back up.'/
     	1' Enter RE to refresh the menu; LIST, TYPE to list all peaks,',
     	1' CTRL_Z to quit.'/' Enter blank RETURN or EXIT to exit',
     	1' from the menu to start the conversion.'/,X,80('='))
C
C  INSERT THE PARAMETERS...
C
     	CALL COLORS(-1)			!REVERSE VIDEO ON

     	CALL PUTEXT( FILINP, 25, 2, 13 )
     	CALL PUTEXT( FILOUT, 25, 3, 13 )
     	CALL PUTEXT( FILTYP(IFLI), 24, 2, 57 )
     	CALL PUTEXT( FILTYP(IFLO), 24, 3, 57 )
     	CALL PUTEXT( SAMP, 68, 4, 13 )

     	CALL CODDAT( TEXT(1:9), DATM ,IFLI)
     	CALL PUTEXT( TEXT(1:9), 9, 5, 12  )
     	CALL CODTIM( TEXT(1:8), TIMM )
     	CALL PUTEXT( TEXT(1:8), 8, 5, 34  )
     	CALL PUTEXT( USER, 12, 5, 56 )
       
        IF(IELE.LT.1 .OR. IELE.GT.6) THEN  !S.Z.
        IELE=4                             !S.Z.
        ANODE(4)='Cu'                      !S.Z.
        ENDIF                              !S.Z.
 
     	CALL PUTEXT( ANODE(IELE),  2, 6, 9 )
     	CALL PUTFFL( WAVE(1,IELE), 1, 7.4, 6, 19 )
     	CALL PUTFFL( WAVE(2,IELE), 1, 7.4, 6, 34 )
     	CALL PUTFFL( WAVE(3,IELE), 1, 7.4, 6, 48 )
     	CALL PUTFFL( WAVE(5,IELE), 1, 4.1, 6, 73 )

     	CALL PUTFFL( CUTI(1), 1, 5.1, 7, 25 )
     	CALL PUTFFL( CUTI(2), 1, 5.1, 7, 33 )
     	CALL PUTFFL( CUTF(1), 1, 5.3, 7, 60 )
     	CALL PUTFFL( CUTF(2), 1, 5.3, 7, 68 )
     	CALL PUTIFL( LTYP, 1, 2, 8, 43 )


     	IF( JRES.EQ.1 ) CALL PUTEXT( 'NO ', 3, 8, 74 )
     	IF( JRES.EQ.2 ) CALL PUTEXT( 'YES', 3, 8, 74 )


     	CALL PUTIFL( IPEAK, 1, 3, 13, 5 )
     	CALL PUTFFL( F2TH(IPEAK), 1, 7.3, 13, 10 )
     	CALL PUTFFL( C2TH(IPEAK), 1, 7.3, 13, 18 )
     	CALL PUTFFL( FDSP(IPEAK), 1, 7.4, 13, 26 )
     	CALL PUTFFL( CDSP(IPEAK), 1, 7.4, 13, 34 )
     	CALL PUTFFL( RELA(IPEAK), 1, 5.1, 13, 42 )
     	CALL PUTGFL( AREA(IPEAK,JRES), 1, 11.4, 13, 48 )
     	CALL PUTFFL( FWHM(IPEAK), 1, 5.3, 13, 60 )
     	CALL PUTIFL( PHKL(1,IPEAK), 3, 3, 13, 66 )
     	CALL PUTEXT( PEAK(IPEAK), 4, 13, 76 )
     	CALL COLORS(80)
     	CALL PUTIFL( NPKS, 1, 3, 13, 1 )

C
C  START THE DIALOGUE CYCLE...
C
111	CALL COLORS(1)
    	CALL PUTEXT( 'INPUT> ', 7, 23, 1 )
    	IERR = 1				!ERROR FLAG ON
    	READ(5,100,ERR=9999,END=9999) TEXT
100	FORMAT(A)

    	IERR = 0
    	IF( TEXT.EQ.' ' ) GOTO 999
     	NCHS = NALPHA( TEXT, 80 )
     	NCH  = NBLANK( TEXT(1:NCHS), NCHS )
     	IF( NCH.GT.0 ) THEN
     	    TEXT = TEXT(NCH+1:)
     	    NCHS = NCHS - NCH
     	ENDIF
    	SYM = TEXT(1:1)
     	KEY = TEXT(1:2)
     	NCH = NALPHA( SYM, -1 )
     	NCH = NALPHA( KEY, -2 )
    	IF( KEY.EQ.'EX' ) GOTO 999
     	IERR = 1
    	IF( KEY.EQ.'QU' ) GOTO 9999
    	IF( KEY.EQ.'RE' ) GOTO 1	!REFRESH THE MENU
    	IF( KEY.EQ.'LI'.OR. KEY.EQ.'TY' ) THEN
     	    IF( NPKS.EQ.0 ) GOTO 444			!NO PEAKS LISTED
     	    CALL SHOWPS					!SHOW ALL PEAKS
     	    GOTO 1					!REFRESH THE MENU
     	ENDIF
     	CALL CURSOR( 23, 1 )
    	CALL CLEARS( 2 )
     	IF( NCHS.GT.1 ) GOTO 101
     	IF( SYM.EQ.'N' ) GOTO 4321
     	IF( SYM.EQ.'B' ) GOTO 4322
     	GOTO 888
C
C  DECODE THE ENTRY NUMBER...
C
105	FORMAT(I)
101	IF( NCHS.LT.4 ) GOTO 110
     	DECODE( 3, 105, TEXT(1:3), ERR=110 ) INDX
     	GOTO 130
110	IF( NCHS.LT.3 ) GOTO 120
     	DECODE( 2, 105, KEY, ERR=120 ) INDX
    	GOTO 130
120	DECODE( 1, 105, SYM, ERR=888 ) INDX

130    	IF( INDX.LT.1 ) GOTO 777

    	NPS = INDEX( TEXT(1:4), '>' )
    	IF( NPS.GT.0 ) GOTO 1234	!PEAK EDIT
    	NPS = INDEX( TEXT(1:4), ';' )
    	IF( NPS.GT.0 ) GOTO 4320	!PEAK LIST
    	NPS = INDEX( TEXT(1:4), '/' )
    	IF( NPS.GT.0 ) GOTO 400		!PEAK DELETE
    	NPS = INDEX( TEXT(1:3), '=' )
    	IF( NPS.EQ.0 ) GOTO 888		!WRONG ACCESS
    	NCH = NCHS - NPS
     	IF( NCH.EQ.0 ) GOTO 111
     	CALL COLORS(-1)			!REVERSE VIDEO
    	CALL COLORS(80)			!INTENSE PRINT
    	NPS = NPS + 1
    	GOTO (50,51,52,53,54,55,55,55,55,56,56,55,57), INDX
C
C  ERROR TRANSFER...
C
777	CALL COLORS(-1)
     	CALL PUTEXT('_ERROR: entry value out of range. Entry ignored!',
     	1 48, 24, 1 )
    	GOTO 111
666	CALL COLORS(-1)
     	CALL PUTEXT('_ERROR: shown radiation source is not modifiable.',
     	1 49, 24, 1 )
     	GOTO 111
888    	CALL COLORS(-1)
     	CALL PUTEXT('_ERROR: invalid entry. Check spelling.',
     	1 38, 24, 1 )
    	GOTO 111
C
C  GET THE SAMPLE ID...
C
50	SAMP = TEXT(NPS:NCHS)
     	CALL PUTEXT( SAMP, 68, 4, 13 )
     	GOTO 111
C
C  GET THE DATE...
C
51	NCH = NALPHA( TEXT(1:NCHS), -NCHS )	!CONVERT TO UPPERCASES
     	DUMY = DATCOD( TEXT(NPS:NCHS) )
     	IF( DUMY.EQ.0.0 ) GOTO 888
     	DATM = DUMY
     	CALL CODDAT( TEXT(1:9), DATM )
     	CALL PUTEXT( TEXT(1:9), 9, 5, 12  )
     	GOTO 111
C
C  GET THE TIME...
C
52	NCH = NALPHA( TEXT(1:NCHS), -NCHS )	!CONVERT TO UPPERCASES
     	DUMY = TIMCOD( TEXT(NPS:NCHS) )
     	IF( DUMY.EQ.0.0 ) GOTO 888
     	TIMM = DUMY
     	CALL CODTIM( TEXT(1:8), TIMM )
     	CALL PUTEXT( TEXT(1:8), 8, 5, 34  )
     	GOTO 111
C
C  GET THE USER NAME...
C
53	USER = TEXT(NPS:NCHS)
     	CALL PUTEXT( USER, 12, 5, 56 )
     	GOTO 111
C
C  GET THE ANODE...
C
54	KEY = TEXT(NPS:(NPS+1))
     	NCH = NALPHA( KEY, -2 )			!CONVERT TO UPPERCASES
     	IF( KEY.EQ.ANODE(IELE) ) GOTO 111
     	DO I=1, 5
     	   IF( KEY.EQ.ANODE(I) ) GOTO 200
     	ENDDO
     	IELE = 6
     	ANODE(IELE) = KEY
     	CALL PUTEXT( ANODE(IELE), 2, 6, 9 )
     	CALL COLORS(-1)
     	CALL PUTEXT('_Please enter the correct values
     	1 for field 6,7,8,9.', 51, 24, 1 )
     	GOTO 111
C
C  DISPLAY NEW RADIATION...
C
200	IELE = I
     	CALL PUTEXT( ANODE(IELE), 2, 6, 9 )
     	CALL PUTFFL( WAVE(1,IELE), 1, 7.4, 6, 19 )
     	CALL PUTFFL( WAVE(2,IELE), 1, 7.4, 6, 34 )
     	CALL PUTFFL( WAVE(3,IELE), 1, 7.4, 6, 48 )
     	CALL PUTFFL( WAVE(5,IELE), 1, 4.1, 6, 73 )
     	IF( NPKS.EQ.0 ) GOTO 111
     	CALL LAMDAP	!RECALCULATE THE PEAK POSITIONS
     	GOTO 501
C
C  GET THE RADIATIONS AND PEAK TYPE...
C
55	IF( TEXT(NCHS:NCHS) .EQ. ',' ) NCHS = NCHS - 1
    	NCH = NCHS - NPS + 1
    	IF( NCH.EQ.0 ) GOTO 111
    	CALL COMMAS( TEXT(NPS:NCHS), NCH, 1, RP, KP, IDUM )
    	IF( NCH.EQ.0 ) GOTO 888		!ERROR INPUT

     	IF( INDX.LT.10 ) THEN
     	    IF( IELE.NE.6 ) GOTO 666
     	    IF( RP(1).LT.0.01 .OR. RP(1).GE.10.0 ) GOTO 777
     	    IF( INDX.EQ.6 ) THEN
     		WAVE(1,IELE) = RP(1)
     		CALL PUTFFL( RP(1), 1, 7.4, 6, 19 )
     	    ENDIF
     	    IF( INDX.EQ.7 ) THEN
     		WAVE(2,IELE) = RP(1)
     		CALL PUTFFL( RP(1), 1, 7.4, 6, 34 )
     	    ENDIF
     	    IF( INDX.EQ.8 ) THEN
     		WAVE(3,IELE) = RP(1)
     		CALL PUTFFL( RP(1), 1, 7.4, 6, 48 )
     	    ENDIF
     	    IF( INDX.EQ.9 ) THEN
     		WAVE(5,IELE) = RP(1)
     		CALL PUTFFL( RP(1), 1, 4.1, 6, 73 )
     	    ENDIF
     	    IF( NPKS.EQ.0 ) GOTO 111
     	    CALL LAMDAP			!RECALCULATE THE PEAK POSITIONS
     	    GOTO 501
     	ELSE
     	    IF( RP(1).LT.0 .OR. RP(1).GT.2 ) GOTO 777
     		IDUM = LTYP
     		LTYP = RP(1)
     		IF( IDUM.EQ.LTYP ) GOTO 111
     		CALL PUTIFL( LTYP,1, 2, 8, 43 )
     		IF( NPKS.EQ.0 ) GOTO 111
     		CALL SCREEN( LTYP, IPEAK )
     		GOTO 501
     	ENDIF
     	GOTO 111
C
C  GET THE RESIDUAL PATTERN FLAG...
C
57	IF( IRES.EQ.0 ) GOTO 555
     	NCH = JRES
     	JRES = 1
     	IF( TEXT(NPS:NPS).EQ.'Y'.OR.TEXT(NPS:NPS).EQ.'y' ) JRES= 2
     	IF( NCH.EQ.JRES ) GOTO 111
     	IF( JRES.EQ.1 ) CALL PUTEXT( 'NO ', 3, 8, 74 )
     	IF( JRES.EQ.2 ) CALL PUTEXT( 'YES', 3, 8, 74 )
     	IF( NPKS.EQ.0 ) GOTO 111
     	CALL SCREEN( LTYP, IPEAK )
     	GOTO 501

555	CALL COLORS(-1)
     	CALL PUTEXT('_ERROR: input file does not contain
     	1 a residual pattern.', 55, 24, 1 )
     	GOTO 111
C
C  GET THE INTENSITY/FWHM CUTOFFS...
C
56	IF( TEXT(NCHS:NCHS) .EQ. ',' ) NCHS = NCHS - 1
    	NCH = NCHS - NPS + 1
    	IF( NCH.EQ.0 ) GOTO 111
    	CALL COMMAS( TEXT(NPS:NCHS), NCH, 2, RP, KP, IDUM )
    	IF( NCH.EQ.0 ) GOTO 888		!ERROR INPUT

     	IF( INDX.EQ.10 ) THEN
     	    IF( KP(1).EQ.1 ) THEN
     	    	IF( RP(1).LT.0.1 .OR. RP(1).GT.50.0 ) GOTO 777
     	    	CUTI(1) = RP(1)
     	        CALL PUTFFL( CUTI(1), 1, 5.1, 7, 25 )
     	    ENDIF
     	    IF( KP(2).EQ.1 ) THEN
     	    	IF( RP(2).LT.50.0 .OR. RP(2).GT.100. ) GOTO 777
     	    	CUTI(2) = RP(2)
     	        CALL PUTFFL( CUTI(2), 1, 5.1, 7, 33 )
     	    ENDIF
     	ENDIF
     	IF( INDX.EQ.11 ) THEN
     	    IF( KP(1).EQ.1 ) THEN
     	    	IF( RP(1).LT.0.01 .OR. RP(1).GT.0.50 ) GOTO 777
     	    	CUTF(1) = RP(1)
     	        CALL PUTFFL( CUTF(1), 1, 5.3, 7, 60 )
     	    ENDIF
     	    IF( KP(2).EQ.1 ) THEN
     	    	IF( RP(2).LT.0.50 .OR. RP(2).GT.9.99) GOTO 777
     	    	CUTF(2) = RP(2)
     	        CALL PUTFFL( CUTF(2), 1, 5.3, 7, 68 )
     	    ENDIF
     	ENDIF
     	IF( NPKS.EQ.0 ) GOTO 111
     	CALL SCREEN( LTYP, IPEAK )
     	GOTO 501
C
C  SCAN THE PEAK LIST...
C
4320	IF( NPKS.EQ.0 ) GOTO 444
     	IF( INDX.GT.NPKS ) GOTO 777
     	IF( INDX.EQ.IPEAK ) GOTO 111
     	IPEAK = INDX
     	GOTO 501
4321	IF( NPKS.EQ.0 ) GOTO 444
     	IPEAK = IPEAK + 2
4322	IF( NPKS.EQ.0 ) GOTO 444
     	IPEAK = IPEAK - 1
     	IF( IPEAK.EQ.0 ) IPEAK = NPKS
     	IF( IPEAK.GT.NPKS ) IPEAK = 1
     	GOTO 501
444	CALL COLORS(-1)
     	CALL PUTEXT('_ERROR: number of peak recorded = 0 ', 36, 24, 1 )
     	GOTO 111
222	CALL COLORS(-1)
     	CALL PUTEXT('_ERROR: duplicated entry detected.
     	1 Entry ignored.', 49, 24, 1 )
     	GOTO 111
C
C	---------------------------------------
C	     P E A K    L I N E    E D I T
C	---------------------------------------
C
C  DECODE THE ENTRIES...
C
1234	IF( TEXT(NCHS:NCHS) .EQ. ',' ) NCHS = NCHS - 1
    	NCH = NCHS - NPS
    	IF( NCH.EQ.0 ) GOTO 111
    	NPS = NPS + 1
    	CALL COMMAS( TEXT(NPS:NCHS), NCH, 10, RP, KP, IDUM )
    	IF( NCH.EQ.0 ) GOTO 888		!ERROR INPUT
C
C  MODIFY AN EXISTING PEAK OR ADD A NEW PEAK...
C
     	IF( INDX.GT.NPKS ) THEN			!ADD A NEW PEAK
     	    IF( NPKS+1.GT.MNK ) GOTO 333	!LIMIT HIT
     	    INDX = NPKS + 1
     	ENDIF
C
C  GET THE PEAK TYPE...
C
     	PEAK(INDX) = TEXT(IDUM: )
     	NCH = NALPHA( PEAK(INDX), 4 )	!CONVERT TO UPPERCASES
     	NCH = 1
     	IF( PEAK(INDX).EQ.'KA2' ) NCH = 2
     	IF( PEAK(INDX).EQ.'KA' )  NCH = 3
     	IF( PEAK(INDX).EQ.'KB1' ) NCH = 4
     	IF( NCH.EQ.1 ) PEAK(INDX) = 'KA1'
C
C  STORE THE VALID NUMBERS...
C
     	IF( KP(1).EQ.1 ) THEN
     	    IF( RP(1).LT.1.0 .OR. RP(1).GT.179.0 ) GOTO 777
     	    DO I=1, NPKS
     		IF( ABS(F2TH(I)-RP(1)).LT.0.001 ) GOTO 222
     	    ENDDO
     	    F2TH(INDX) = RP(1)
     	    FDSP(INDX) = 0.5*WAVE(NCH,IELE)/SIN(DTOR*F2TH(INDX))
     	    IF( KP(2).EQ.0 .AND. KP(4).EQ.0 ) THEN
     	    	C2TH(INDX) = RP(1)
     	    	CDSP(INDX) = FDSP(INDX)
     	    ENDIF
     	ENDIF
     	IF( KP(2).EQ.1 ) THEN
     	    IF( RP(2).LT.1.0 .OR. RP(2).GT.179.0 ) GOTO 777
     	    DO I=1, NPKS
     		IF( ABS(C2TH(I)-RP(2)).LT.0.001 ) GOTO 222
     	    ENDDO
     	    C2TH(INDX) = RP(2)
     	    CDSP(INDX) = 0.5*WAVE(NCH,IELE)/SIN(DTOR*C2TH(INDX))
     	    IF( KP(1).EQ.0 .AND. KP(3).EQ.0 ) THEN
     	    	F2TH(INDX) = RP(2)
     	        FDSP(INDX) = CDSP(INDX)
     	    ENDIF
     	ENDIF
     	IF( KP(1).EQ.0 .AND. KP(3).EQ.1 ) THEN
     	    IF( RP(3).LE.0.0 .OR. RP(3).GT.99.9999 ) GOTO 777
     	    DO I=1, NPKS
     		IF( ABS(FDSP(I)-RP(3)).LT.0.0001 ) GOTO 222
     	    ENDDO
     	    FDSP(INDX) = RP(3)
     	    F2TH(INDX) = RTOD*ASIN(0.5*WAVE(NCH,IELE)/FDSP(INDX))
     	    IF( KP(2).EQ.0 .AND. KP(4).EQ.0 ) THEN
     	    	CDSP(INDX) = RP(3)
     	    	C2TH(INDX) = F2TH(INDX)
     	    ENDIF
     	ENDIF
     	IF( KP(2).EQ.0 .AND. KP(4).EQ.1 ) THEN
     	    IF( RP(4).LE.0.0 .OR. RP(4).GT.99.9999 ) GOTO 777
     	    DO I=1, NPKS
     		IF( ABS(CDSP(I)-RP(4)).LT.0.0001 ) GOTO 222
     	    ENDDO
     	    CDSP(INDX) = RP(4)
     	    C2TH(INDX) = RTOD*ASIN(0.5*WAVE(NCH,IELE)/CDSP(INDX))
     	    IF( KP(1).EQ.0 .AND. KP(3).EQ.0 ) THEN
     	        FDSP(INDX) = RP(4)
     	    	F2TH(INDX) = C2TH(INDX)
     	    ENDIF
     	ENDIF
     	IF( KP(5).EQ.1 ) THEN
     	    IF( RP(5).LT.CUTI(1) .OR. RP(5).GT.CUTI(2) ) GOTO 777
     	    RELA(INDX) = RP(5)
     	    IF( INDX.GT.1 ) THEN
     	AREA(INDX,JRES) = AREA(INDX-1,JRES)*RELA(INDX)/RELA(INDX-1)
     	    ELSE
     	IF( NPKS.LT.2 ) AREA(1,JRES) = RELA(1)
     	IF( NPKS.GT.1 ) AREA(1,JRES) = AREA(2,JRES)*RELA(1)/RELA(2)
     	    ENDIF
     	    PHGH(INDX) = AREA(INDX,JRES)
     	ENDIF
     	IF( KP(6).EQ.1 ) THEN
     	    IF( RP(6).LT.1.0 ) GOTO 777
     	    AREA(INDX,JRES) = RP(6)
     	    PHGH(INDX) = RP(6)
     	    IF( INDX.GT.1 ) THEN
     	    RELA(INDX) = RELA(INDX-1)*AREA(INDX,JRES)/AREA(INDX-1,JRES)
     	    ELSE
     	    IF( NPKS.LT.2 ) RELA(1)= AREA(1,JRES)
     	    IF( NPKS.GT.1 ) RELA(1)= RELA(2)*AREA(1,JRES)/AREA(2,JRES)
     	    ENDIF
     	    IF(RELA(INDX).LT.CUTF(1).OR.RELA(INDX).GT.CUTF(2)) GOTO 777
     	ENDIF
     	IF( INDX.GT.NPKS ) FWHM(INDX) = 0.1	!DEFAULT
     	IF( KP(7).EQ.1 ) THEN
     	    IF( RP(7).LT.CUTF(1) .OR. RP(7).GT.CUTF(2) ) GOTO 777
     	    FWHM(INDX) = RP(7)
     	ENDIF
     	IF( INDX.GT.NPKS ) PHKL(1,INDX) = 0	!DEFAULT
     	IF( KP(8).EQ.1 ) THEN
     	    IF( ABS(RP(8)).GT.99.0 ) GOTO 777
     	    PHKL(1,INDX) = RP(8)
     	ENDIF
     	IF( INDX.GT.NPKS ) PHKL(2,INDX) = 0	!DEFAULT
     	IF( KP(9).EQ.1 ) THEN
     	    IF( ABS(RP(9)).GT.99.0 ) GOTO 777
     	    PHKL(2,INDX) = RP(9)
     	ENDIF
     	IF( INDX.GT.NPKS ) PHKL(3,INDX) = 0	!DEFAULT
     	IF( KP(10).EQ.1 ) THEN
     	    IF( ABS(RP(10)).GT.99.0 ) GOTO 777
     	    PHKL(3,INDX) = RP(10)
     	ENDIF
     	IF( INDX.GT.NPKS ) NPKS = INDX
     	IPEAK = INDX
     	GOTO 500

333	CALL COLORS(-1)
     	CALL PUTEXT('_ERROR: max number of peaks hit.
     	1 No more peak to add.', 53, 24, 1 )
     	GOTO 111
C
C  DELETE A VALID PEAK...
C
400	IF( NPKS.EQ.0 ) GOTO 444
     	IF( INDX.GT.NPKS ) GOTO 777
     	NPKS = NPKS - 1
     	DO I= INDX, NPKS		!REPACK THE ARRAY
     	   F2TH(I) = F2TH(I+1)
     	   C2TH(I) = C2TH(I+1)
     	   FDSP(I) = FDSP(I+1)
     	   CDSP(I) = CDSP(I+1)
     	   RELA(I) = RELA(I+1)
     	   AREA(I,JRES) = AREA(I+1,JRES)
     	   PHGH(I) = PHGH(I+1)
     	   FWHM(I) = FWHM(I+1)
     	   PHKL(1,I) = PHKL(1,I+1)
     	   PHKL(2,I) = PHKL(2,I+1)
     	   PHKL(3,I) = PHKL(3,I+1)
     	   PEAK(I) = PEAK(I+1)
     	ENDDO
     	IF( NPKS.EQ.0 ) THEN
     	    F2TH(1) = 0.0
     	    C2TH(1) = 0.0
     	    FDSP(1) = 0.0
     	    CDSP(1) = 0.0
     	    RELA(1) = 0.0
     	    AREA(1,JRES) = 0.0
     	    PHGH(1) = 0.0
     	    FWHM(1) = 0.0
     	    PHKL(1,1) = 0
     	    PHKL(2,1) = 0
     	    PHKL(3,1) = 0
     	    PEAK(1) = ' '
     	ENDIF
     	IF( IPEAK.GT.NPKS ) IPEAK = NPKS
     	IF( IPEAK.EQ.0 ) IPEAK = 1
C
C  SCREEN THE PEAKS...
C
500	IF( NPKS.GT.0 ) CALL SCREEN( LTYP, IPEAK )
C
C  DISPLAY THE NEW LINE...
C
501	IF( IPEAK.GT.NPKS ) IPEAK = NPKS
     	CALL COLORS(-1)			!REVERSE VIDEO
     	CALL PUTIFL( IPEAK, 1, 3, 13, 5 )
     	CALL PUTFFL( F2TH(IPEAK), 1, 7.3, 13, 10 )
     	CALL PUTFFL( C2TH(IPEAK), 1, 7.3, 13, 18 )
     	CALL PUTFFL( FDSP(IPEAK), 1, 7.4, 13, 26 )
     	CALL PUTFFL( CDSP(IPEAK), 1, 7.4, 13, 34 )
     	CALL PUTFFL( RELA(IPEAK), 1, 5.1, 13, 42 )
     	CALL PUTGFL( AREA(IPEAK,JRES), 1, 11.4, 13, 48 )
     	CALL PUTFFL( FWHM(IPEAK), 1, 5.3, 13, 60 )
     	CALL PUTIFL( PHKL(1,IPEAK), 3, 3, 13, 66 )
     	CALL PUTEXT( PEAK(IPEAK), 4, 13, 76 )
     	CALL COLORS(80)
     	CALL PUTIFL( NPKS,  1, 3, 13, 1 )
     	GOTO 111
C
C  READY TO GET OUT...
C
999	IERR = 1
     	IF( NPKS.EQ.0 ) GOTO 9999	!NO PEAK LEFT
     	IERR= 0
     	NRNG = 1
     	NREP = 1
C
C  FIGURE OUT THE CORRECT ANGULAR RANGE...
C
     	DO I=1, 14
     	    DANG(I) = BANG(I)
     	ENDDO
     	IF( DANG(3).EQ.0.0 ) DANG(3) = 0.01	!ARBITRARY VALUE
     	DANG(1)= DANG(1)+DANG(3)*INT((F2TH(1)-DANG(1))/DANG(3)-1.0)
     	DANG(2)= DANG(1)+DANG(3)*INT((F2TH(NPKS)-DANG(1))/DANG(3)+1.0)
9999	RETURN
     	END
