	dimension isum(9000,9000)
	dimension ifred(8297,8242)
	dimension ixoff(5),iyoff(5)
	real*8 hista(6)
	real*8 histc(6)
	write(6,2)
2	format('Starting program')
	open(unit=1,file='pattern',status='old')
	ixmin=0
	iymin=0
	ixmax=0
	iymax=0
1	read(1,*,end=5) ixo,iyo
	if(ixo.lt.ixmin) ixmin=ixo
	if(ixo.gt.ixmax) ixmax=ixo
	if(iyo.lt.iymin) iymin=iyo
	if(iyo.gt.iymax) iymax=iyo
	write(6,*) ixo,iyo
	noff=noff+1
	ixoff(noff)=ixo
	iyoff(noff)=iyo
	go to 1
5	close(unit=1)
	ixsize=abs(ixmin)+8297+ixmax
	if(ixsize.gt.9000) stop 'too many x pixels'
	iysize=abs(iymin)+8242+iymax
	if(iysize.gt.9000) stop 'too many y pixels'
	write(6,55) ixsize,iysize
55	format('Study size:',2i7)
	areac=8297.*8242.
	areaa=float(ixsize)*float(iysize)
	call load(ifred)
	write(6,56)
56	format('Made it through load!')
	do 60 i=1,ixsize
	do 60 j=1,iysize
	isum(i,j)=0
60	continue
C Now make the image!
	write(6,65) ixsize,iysize,ixmin,iymin
65	format('Now making image: size and mins:',2i7)
	do 100 i=1,ixsize
	do 100 j=1,iysize
	do 100 k=1,noff
C Now, for this offset, what is the value in the image?
		ixc=i+ixoff(k)+ixmin
		iyc=j+iyoff(k)+iymin
		if(ixc.lt.1) go to 100
		if(iyc.lt.1) go to 100
		if(ixc.gt.8297) go to 100
		if(iyc.gt.8242) go to 100
		isum(i,j)=isum(i,j)+ifred(ixc,iyc)
100	continue
C OK, we've done it!  Uh...now, what?
C Let's test some regions
1001	do 2000 i=1,ixsize
	do 2000 j=1,iysize
	if ((isum(i,j).gt.-1).and.(isum(i,j).lt.6)) then
	hista(isum(i,j)+1)=hista(isum(i,j)+1)+1.
	if((i.gt.abs(ixmin)).and.(i.lt.ixsize-ixmax).and.
     &     (j.gt.abs(iymin)).and.(j.lt.iysize-iymax)) then
	histc(isum(i,j)+1)=histc(isum(i,j)+1)+1.
	end if
	end if
2000	continue
	do 2001 k=1,noff+2
	ac=100.*histc(k)/areac
	aa=100.*hista(k)/areaa
	write(6,2002) k-1,ac,aa,histc(k),hista(k),areac,areaa
2002	format('Number of hits', i5, ' cen',f8.1,' all',f8.1,4f10.0)
2001	continue
	stop
C1000	format('Input starting x, end x:',$)
	read(5,*) ixs,ixe
	if((ixs.lt.1).or.(ixe.gt.9000)) stop
	write(6,1002)
1002	format('Input starting y, end y:',$)
	read(5,*) iys,iye
	if((iys.lt.1).or.(iye.gt.9000)) stop
	inmin=10
	inmax=-5
	do 1005 i=ixs,ixe
	do 1005 j=iys,iye
	if(isum(i,j).lt.inmin) inmin=isum(i,j)
	if(isum(i,j).gt.inmax) inmax=isum(i,j)
1005	continue
	write(6,1010) inmin,inmax
1010	format('Min number',i5,' Max number',i5)
	go to 1001
	end
	subroutine load(ifred)
	dimension ifred (8297,8242)
	ix=0
	do 1 i=1,2048
	ix=ix+1
	iy=0
	do 2 j=1,4096
	iy=iy+1
	ifred(ix,iy)=1
2	continue
Cwrite(6,59) ix,iy
59	format('End of 2 continue',2i7)
	do 3 j=1,50
	iy=iy+1
	ifred(ix,iy)=0
3	continue
	do 4 j=1,4096
	iy=iy+1
	ifred(ix,iy)=1
4	continue
1	continue
	write(6,55) ix,iy
55	format('At end of loop 1 in load:',2i7)
	do 5 i=1,35
	ix=ix+1
	do 555 j=1,8242
	ifred(ix,j)=0
555	continue
5	continue
	write(6,60) ix
60	format('At end of first gap:',i7)
	do 11 i=1,2048
	iy=0
	ix=ix+1
	do 12 j=1,4096
	iy=iy+1
	ifred(ix,iy)=1
12	continue
	do 13 j=1,50
	iy=iy+1
	ifred(ix,iy)=0
13	continue
	do 14 j=1,4096
	iy=iy+1
	ifred(ix,iy)=1
14	continue
11	continue
	write(6,56) ix,iy
56	format('At end of loop 2:',2i7)
	do 15 i=1,35
	ix=ix+1
	do 15 j=1,8242
	ifred(ix,j)=0
15	continue
	write(6,61) ix
61	format('At end of 2nd gap:',i7)
	do 21 i=1,2048
	iy=0
	ix=ix+1
	do 22 j=1,4096
	iy=iy+1
	ifred(ix,iy)=1
22	continue
	do 23 j=1,50
	iy=iy+1
	ifred(ix,iy)=0
23	continue
	do 24 j=1,4096
	iy=iy+1
	ifred(ix,iy)=1
24	continue
21	continue
	write(6,57) ix,iy
57	format('At end of loop 3:',2i7)
	do 25 i=1,35
	ix=ix+1
	do 25 j=1,8242
	ifred(ix,j)=0
25	continue
	write(6,71) ix
71	format('At end of 3rd gap',i7)
	do 31 i=1,2048
	iy=0
	ix=ix+1
	do 32 j=1,4096
	iy=iy+1
	ifred(ix,iy)=1
32	continue
	do 33 j=1,50
	iy=iy+1
	ifred(ix,iy)=0
33	continue
	do 34 j=1,4096
	iy=iy+1
	ifred(ix,iy)=1
34	continue
31	continue
	write(6,35) ix,iy
35	format('Done with Load:', 2i5)
	return
	end
