	SUBROUTINE DIEDIT(WAVEL,*)
C----------------------------------------------------------------------------
C Subroutine to edit the current peak list 
C----------------------------------------------------------------------------
	include '[-.shared]jcpds.parms'
	include 'SEARCH.cmn'
	include 'SEARCH.par'
	REAL wavel
	INTEGER*2 I2
	CHARACTER note*20,string*7
	LOGICAL DTOTT,TTTOD,DTOPDF,PDFTOD,DTOIND,INDTOD
	logical flag,flag1,EXRtst

	CALL DSORT(ds,inten,numob)
	call TERASE(ICRT,lo,li)

	IF (wavel .le. 0.0) THEN 
	  WRITE (LO,101) numob
101	  format(' Editing in d-space mode (peak list contains',i3,
	1	' lines): ')
	ELSE
	  WRITE (LO,201) wavel,numob
201	  format(' Editing in Two-theta mode, wavelength = ',f8.5,
	1	' (peak list contains',i3,' lines): ')
	ENDIF
	num = 1

110	IF (wavel .le. 0.0 .and. numob .ge. num) THEN 
	  write (lo,760) 'd-space'
	ELSEIF (numob .ge. num) THEN
	  write (lo,760) 'angle'
	ENDIF
760	format(/,' Peak positions and intensities will be listed',
	1	' one at a time.',' Enter:',/,
	2	'   >nnn      To jump forward or backward to # nnn peak',/,
	3	'   E         To exclude the region around the current peak',/,
	4	'   -1        To delete the current peak from the list',/,
	5	'   aaa       To change the peak position to ',a,' aaa',/,
	6	'   ,iii      To change the intensity of the peak to iii',/,
	7	'   aaa,iii   To change both the position and intensity',/,
	8	'   ^Z        To leave all remaining peaks unchanged.'/)
C---------------------------------------------------------------------------	
C remove deleted peaks
111	j = 0
	DO i=1,numob
	  if (ds(i) .gt. 0 .and. inten(i) .ge. 0) then
	    j = J + 1
	    ds(j) = ds(i)
	    inten(j) = inten(i)
	  endif
	ENDDO
	numob = j
  
	DO 100 i=num,numob
C determine if the reflection is totally contained in an excluded area;
C convert D-space to 2theta at Cu Ka and at current wavelength
	  note = '(Out of Cu range)'		  
	  string = ' **.***'
	  write(string,'(f7.3)',err=701) DS(i)
701	  flag = dtott(ds(i),peak,1.5405598)
	  flag1 = flag
	  if (wavel .gt. 0.0) then
	    string = ' ***.**'
	    flag1 = dtott(ds(i),x1,wavel)
	    if (flag1) then
	      write(string,'(f7.2)',err=707) x1
	    else
	      note = '(Out of wavel range)'		  
	    endif
	  ENDIF
C if peak is not in the Ewald sphere, mark as out of range
	  if (flag .and. flag1) then
C evaluate the PDF and IND index values on the edges of the peak window
	    amax = 31416
	    x1 = min(180.,peak+abs(d2t))
	    flag = tttod(dmin,x1,1.5405598)
	    IF (FLAG) flag = DTOPDF(amax,dmin)
	    amin = -31416
	    x1 = max(0.,peak-abs(d2t))
	    flag = tttod(dmax,x1,1.5405598)
	    IF (FLAG) flag = DTOPDF(amin,dmax)

C   Find 1st bit that has not been set as excluded
	    note = ' '
	    do i2=amin,amax
	      if (.not. EXRtst(extbl,i2)) goto 707
	    ENDDO
	    note = '(Excluded)'		  
	  ENDIF

707	  WRITE (LO,102) i,note,string,inten(i)
102	  format ('$Peak #',i3,1x,a,' (',a7,',',f9.2,'): ')
	  read (li,'(a)',end=750) note
	  len = LENCH(note)
C eliminate leading spaces
	  do i2=1,len
	    if (note(1:1) .eq. ' ') then 
	      note = note(2:)
	      len = len - 1
	    endif
	  enddo
C blank line? skip to next peak
	  IF (len .eq. 0) then
	    goto 100
C goto a specific line?
	  ELSEIF (note(1:1) .eq. '>') then
	    num = i
	    read (note(2:len),'(i)',err=110) k
	    if (k .gt. 0) num = k
	    goto 111
C Eliminate the region around the current peak
	  ELSEIF (note(1:1) .eq. 'E' .or. note(1:1) .eq. 'e') then 
	    call exset(extbl,amin,amax,.true.)
	    goto 100
	  ENDIF
C no command, so try to read numbers from the line
	  read (note(:len),'(f10.0,f10.0)',err=711) d1,d2
	  IF (d1 .lt. 0) then 
	    ds(i) = -1
	  ELSEIF (wavel .gt. 0.0 .and. d1 .gt. 0) THEN 
	    flag = tttod(ds(i),d1,wavel)
	    if (.not. flag) write(lo,'(a)')
	1	' *** Invalid angle, no change made to position ***',' '
	  ELSEIF (d1 .gt. 0) then
	    ds(i) = d1
	  ENDIF
	  if (d2 .ne. 0) inten(i) = d2
100	CONTINUE
C---------------------------------------------------------------------------	
C now remove deleted peaks
750	j = 0
	DO i=1,numob
	  if (ds(i) .gt. 0 .and. inten(i) .ge. 0) then
	    j = J + 1
	    ds(j) = ds(i)
	    inten(j) = inten(i)
	  endif
	ENDDO
	numob = j
C---------------------------------------------------------------------------	
730	IF (wavel .le. 0.0) THEN 
	  write (lo,740) ' d-space'
	ELSE
	  write (lo,740) ' angle'
	ENDIF
740	Format(//' Peaks may now be added, enter the position as "aaa,iii"',
	1	' where "aaa" is the ',/,a,' and "iii" is the intensity.',
	2	' Leave a blank line after adding last peak.'/)
	DO i=numob+1,m$pks
741	  WRITE (LO,'(a,i3,a)') '$Peak #',i,': '
	  read (li,'(f10.0,f10.0)',err=703,end=713) d1,d2
	  if (d1 .le. 0) then 
	    goto 713
	  elseif (wavel .le. 0.0) THEN 
	    ds(i) = d1
	    numob = i
	    inten(i) = d2
	  ELSE
	    flag = tttod(ds(i),d1,wavel)
	    if (.not. flag) then
	      write(lo,'(a)') ' *** Illegal angle, try again ***'
	      goto 741
	    endif
	    numob = i
	    inten(i) = d2
	  ENDIF
	ENDDO
C---------------------------------------------------------------------------	
C sort the array 
713	CALL DSORT(ds,INTEN,numob)
	RETURN
C---------------------------------------------------------------------------	
703	write (lo,'(A)') ' *** Illegal entry, try again ***'
	goto 730
C---------------------------------------------------------------------------	
C error parsing the input string
711	write (lo,'(A)') ' *** Illegal command, try again ***'
	num = i
	goto 110
	END
