C+
C NAME:
C	centroid_rotate
C MODIFICATION HISTORY:
C	July, 2003 A. Smith (UCSD/CASS)
C-
	subroutine rotate_2d(xlen, ylen, xlen2, ylen2, image, rotimage2)
	implicit none

	integer*4	image(xlen,ylen), rotimage(xlen,ylen), rotimagecnt(xlen,ylen), rotimage2(xlen2,ylen2)
	integer*4	xlen, ylen,xlen2, ylen2, xbound, ybound, ii, jj, i, j
	real*8		theta, x, y, xnew, ynew

	print*, 'Fortran in...'


	xbound=xlen+1
	ybound=ylen+1

	do i=1,xlen
	 do j=1,ylen

	   x=float(i)-(xlen*0.5D0)+0.5D0
	   y=float(j)-(ylen*0.5D0)+0.5D0

	   xnew=nint( x*dcosd(theta)+y*dsind(theta)+100.5D0 )
	   ynew=nint( -x*dsind(theta)+y*dcosd(theta)+100.5D0 )

	   if(xnew.gt.0.and.xnew.lt.xbound.and.ynew.gt.0.and.ynew.lt.ybound)then
	     rotimage(xnew,ynew)=rotimage(xnew,ynew)+image(i,j)
	     rotimagecnt(xnew,ynew)=rotimagecnt(xnew,ynew)
	   endif
	 enddo
	enddo

	do i=1,200
	 do j=1,200
	   if(rotimagecnt(i,j).gt.0) rotimage(i,j)=rotimage(i,j)/rotimagecnt(i,j)
	 enddo
	enddo
	
	do i=1,xlen2
	 do j=1,ylen2
	  ii=i*2
	  jj=j*2
	  rotimage2(i,j)=rotimage(ii,jj)+rotimage(ii-1,jj)+rotimage(ii-1,jj-1)+rotimage(ii,jj-1)
	 enddo
	enddo

	print*, 'Fortran out!!!'

	end
