	options/check=bounds/extend
	SUBROUTINE POWDER
C
C ROUTINE TO PERFORM POWDER DIFFRACTION SCAN (IN 2-THETA)
C
	include 'single.inc'
	include 'alph.inc'
	integer icount(8)
	REAL ANGL(4),omega
	character*1 ans
	character*30 fname

C notes: 
C
c       Ierr set to 2 if open statement times out
c            set to 1 if drive goes outside softlimits
	ierr=0
	iwait=0
c 
c set up the scan
c
	write(6,10)
10	format(10x,'Powder Diffraction Scan'//)
20	write(6,25)
25	format('$     Input 2-theta start and 2-theta end: ')
29	read(5,30)i,thmin,thmax 
30	format(q,2f10.0)
	if(i .lt. 3)goto20
c
c check for valid 2-thetas
c
	if(thmin .gt. 0. .and. thmax .gt. thmin)goto40
	write(6,35)
35	format('$     Invalid values - reinput: ')
	goto29
40	do i=2,4
	  angl(i)=0.
	enddo
	angl(1)=thmin
	call softlimits(angl,1,ierr)
	if(ierr .eq. 1)goto50
	angl(1)=thmax
	call softlimits(angl,1,ierr)
	if(ierr .eq. 0)goto60
50	write(6,55)
55	format('$     2-theta values OUT OF BOUNDS - reinput: ')
	goto29
c
c input of step size (in two-theta) 
c
60	write(6,65)
65	format('$     Input 2-theta step size: ')
69	read(5,70)i,step
70	format(q,f10.0)
	if(i .eq. 0)goto60
	if(step .lt. 0. .or. step .gt. (thmax-thmin))then
	   write(6,75)
75	   format('$     Step size invalid - reinput: ')
	   goto69
	endif
c
c multiple omega?
c
	write(6,76)
76	format('$     Do you want to rock omega? ')
	read(5,77)ans
77	format(a1)
	if(ans .eq. 'y' .or. ans .eq. 'Y')then
c multiple omega
1000	  write(6,1005)
1005	  format('$     Input omega min, max, step: ')
1009	  read(5,1010)wmin,wmax,wstep
1010	  format(3f10.0)
	  if(wmin .gt. wmax)then
	 	temp=wmin
	 	wmin=wmax
		wmax=temp
	  endif
c count time per omega step
1080	  write(6,1085)
1085	  format('$     Input count time per omega step (secs): ')
1089	  read(5,1090)i,ctime
1090	  format(q,f10.0)
	  if(i .eq. 0)goto1080
	  if(ctime .lt. 1.)then
	    write(6,1095)
1095	    format('$     Count time invalid - reinput: ')
	    goto89
	  endif
	  nwstep=1+ (wmax-wmin)/wstep
	else
c  single omega
c count time per step
c
	  nwstep=1
80	  write(6,85)
85	  format('$     Input count time per step (secs): ')
89	  read(5,90)i,ctime
90	  format(q,f10.0)
	  if(i .eq. 0)goto80
	  if(ctime .lt. 1.)then
	    write(6,95)
95	    format('$     Count time invalid - reinput: ')
	    goto89
	  endif
	endif
c
c check total scan time
c
	time=ctime*nwstep*(1+(thmax-thmin)/step)/60.
	ihour=time/60
	if(ihour .gt. 0)then
	  time=time-float(ihour*60)
	  write(6,100)ihour,time
100	  format(/5x,'Total Scan Time Will Be Approx ',i2,' hours and ',
     1           f5.2,' minutes')
	else
	  write(6,105)time
105	  format(/5x,'Total Scan Time Will Be Approx ',f5.2,' minutes')
	endif
	write(6,110)
110	format('$     Is this ok? ')
	read(5,77)ans
	if(ans .ne. 'y' .and. ans .ne. 'Y')goto20
c
c request file for output
c
	write(6,200)
200	format('$     Type name for output datafile: ')
204	read(5,205)fname
205	format(a30)
	open(unit=20,file=fname,status='unknown',err=210)
	goto220
210	write(6,215)
215	format('$    Error opening file - retype filename: ')

	goto204
c
c write file header
c
220	time=ctime*nwstep
	write(20,225)thmin,step,thmax,time
225	format(4f10.3)
	close(unit=20)
c
c drive to start
c
	call drive(1,thmin,0,0,ierr)
	call drive(3,0,0,0,ierr)
	call drive(4,0,0,0,ierr)
	if(nwstep .eq. 1)then
	  call drive(2,0,0,0,ierr)
	else
	  call drive(2,wmin,0,0,ierr)
	  omega=wmin
	  iw=1
	endif
	ttheta=thmin
C
C WAIT FOR DRIVE TO START POSITION COMPLETED
	call wait
	call onbeam
	i=0
c
C START SCAN: Branch on multiple omega
c
300	i=i+1  
	icount(i)=0
	if(nwstep .eq. 1)then
c single omega
	  call count(ctime)
	  call sclwt
	  icount(i)=rdsclr()
	else
c multiple omega
360	  call count(ctime)
	  call sclwt
	  icount(i)=icount(i)+rdsclr()
	  omega=omega+iw*wstep
	  if((omega-wmin)*(wmax-omega) .ge. 0)then
	  	call drive(2,omega,0,0,ierr)
		call wait
	  	goto360
	  else
		if(iw .eq. 1)then
		  call drive(2,wmax,0,0,ierr)
		  omega=wmax
		else
		  call drive(2,wmin,0,0,ierr)
		  omega=wmin
		endif
		call wait
		iw=-1*iw
	  endif
	endif
c
c trap overflows
c
	if(icount(i) .gt. 999999999)icount(i)=999999999
c
c write data points at eight per line
c
	if(i .lt. 8)goto350
320	  open(unit=20,file=fname,status='old',access='append',err=600)
	  write(20,330)icount
330 	  format(8i10)
	  close(unit=20)
	  iwait=0
	  i=0
c
c step two-theta
c
350	ttheta=ttheta+step
	if(ttheta-thmax .gt. step)goto400
	call drive(1,ttheta,0,0,ierr)
	call wait
	goto300
c
c END SCAN - write last few steps to file
c
400	  open(unit=20,file=fname,status='old',access='append',err=700)
	  write(20,410)(icount(j),j=1,i)
410 	  format(<i>i10)
	  close(unit=20)
	  ttheta=ttheta-step
	  write(6,420)ttheta
420	  format('  SCAN COMPLETED AT TWO-THETA = ',f6.2//)
	  call ofbeam
 	  return	
c
c here if error on open file
c - wait in case temporary lock-out
600	if(iwait .gt. 30)then
		ierr=2
		type 610,ttheta
610		format(' **********DATA FILE LOCKED - DATA COLLECTION ABORTED',
     1		' AT 2-THETA = ',F6.2)
	  	call ofbeam
		return
	else
		iwait=iwait+1
        	call delay(1000)
		goto320
	endif
c
c
c here if error on open file at very end
c - wait in case temporary lock-out
700	if(iwait .gt. 30)then
		ierr=2
		type 710
710		format(' **********DATA FILE LOCKED - LAST LINE NOT WRITTEN')
		return
	else
		iwait=iwait+1
        	call delay(1000)
		goto400
	endif
	END
