subroutine rgtran(new)
!   This subroutine transfers old data to the new grid
!   for maxdim = 1,2, or 3
use types
use amr
type(ADAPTIVE_MESH),pointer    :: new

type(ADAPTIVE_MESH),pointer    :: old
real(SP), dimension(3)         :: ti,tf,si,sf,sitemp,sftemp,dx
integer(I4B), dimension(3)     :: nxt,nxs,tni,tnf,sni,snf
integer(I4B) i,j

! ti,tf     : target coordinates (cube corners)
! si,sf     : source coordinates (cube corners)
! dx        : coordinate differentials (same for both old and new)
! nxt,nxs   : number of points for target and source
! tni,tnf   : final target indices (cube corners)
! sni,snf   : final source indices (cube corners)

!any overlap betwen new child and old structure?

old=>lp(new%level)%ml

DO WHILE (associated(old))

si = old%grmin
sf = old%grmax
ti = new%grmin
tf = new%grmax
nxt = new%gnr
nxs = old%gnr
do i=1,3
   dx(i) = (tf(i) - ti(i))/real(nxt(i) - 1)
enddo

IF (.not. ((sf(1) .lt. ti(1) + 0.5 * dx(1)) .or. &
           (si(1) .gt. tf(1) - 0.5 * dx(1))) ) THEN
   tni(1) = int( real(nxt(1) - 1) * (si(1) - ti(1))/ &
                 (tf(1) - ti(1)) + 1.5)
   tnf(1) = int( real(nxt(1) - 1) * (sf(1) - ti(1))/ &
                 (tf(1) - ti(1)) + 1.5)
   if (tni(1) .lt. 1) tni(1) = 1
   if (tnf(1) .gt. nxt(1)) tnf(1) = nxt(1)
   sitemp(1) = ti(1) + real(tni(1) - 1) * dx(1)
   sftemp(1) = ti(1) + real(tnf(1) - 1) * dx(1)
   sni(1) = int( real(nxs(1) - 1) * (sitemp(1) - si(1))/ &
                 (sf(1) - si(1)) + 1.5)
   snf(1) = int( real(nxs(1) - 1) * (sftemp(1) - si(1))/ &
                 (sf(1) - si(1)) + 1.5)
   if (maxdim .eq. 1) then
      do i=1,n_gfcn
      do j=1,ntlev(i)
         new%grid%gfcn(i)%tlev(j)%data(tni(1):tnf(1),1,1) = &
         old%grid%gfcn(i)%tlev(j)%data(sni(1):snf(1),1,1)
      enddo
      enddo
   else
IF (.not. ((sf(2) .lt. ti(2) + 0.5 * dx(2)) .or. &
           (si(2) .gt. tf(2) - 0.5 * dx(2))) ) THEN
   tni(2) = int( real(nxt(2) - 1) * (si(2) - ti(2))/ &
                 (tf(2) - ti(2)) + 1.5)
   tnf(2) = int( real(nxt(2) - 1) * (sf(2) - ti(2))/ &
                 (tf(2) - ti(2)) + 1.5)
   if (tni(2) .lt. 1) tni(2) = 1
   if (tnf(2) .gt. nxt(2)) tnf(2) = nxt(2)
   sitemp(2) = ti(2) + real(tni(2) - 1) * dx(2)
   sftemp(2) = ti(2) + real(tnf(2) - 1) * dx(2)
   sni(2) = int( real(nxs(2) - 1) * (sitemp(2) - si(2))/ &
                 (sf(2) - si(2)) + 1.5)
   snf(2) = int( real(nxs(2) - 1) * (sftemp(2) - si(2))/ &
                 (sf(2) - si(2)) + 1.5)
   if (maxdim .eq. 2) then
      do i=1,n_gfcn
      do j=1,ntlev(i)
    new%grid%gfcn(i)%tlev(j)%data(tni(1):tnf(1),tni(2):tnf(2),1) = &
    old%grid%gfcn(i)%tlev(j)%data(sni(1):snf(1),sni(2):snf(2),1)
      enddo
      enddo
   else
if(maxdim .ne. 3) then
   write(*,*) 'rgtran: alg error 1'
   stop
endif
IF (.not. ((sf(3) .lt. ti(3) + 0.5 * dx(3)) .or. &
           (si(3) .gt. tf(3) - 0.5 * dx(3))) ) THEN
   tni(3) = int( real(nxt(3) - 1) * (si(3) - ti(3))/ &
                 (tf(3) - ti(3)) + 1.5)
   tnf(3) = int( real(nxt(3) - 1) * (sf(3) - ti(3))/ &
                 (tf(3) - ti(3)) + 1.5)
   if (tni(3) .lt. 1) tni(3) = 1
   if (tnf(3) .gt. nxt(3)) tnf(3) = nxt(3)
   sitemp(3) = ti(3) + real(tni(3) - 1) * dx(3)
   sftemp(3) = ti(3) + real(tnf(3) - 1) * dx(3)
   sni(3) = int( real(nxs(3) - 1) * (sitemp(3) - si(3))/ &
                 (sf(3) - si(3)) + 1.5)
   snf(3) = int( real(nxs(3) - 1) * (sftemp(3) - si(3))/ &
                 (sf(3) - si(3)) + 1.5)
   if (maxdim .eq. 3) then
      do i=1,n_gfcn
      do j=1,ntlev(i)
new%grid%gfcn(i)%tlev(j)%data(tni(1):tnf(1),tni(2):tnf(2), &
      tni(3):tnf(3)) = &
old%grid%gfcn(i)%tlev(j)%data(sni(1):snf(1),sni(2):snf(2), &
      sni(3):snf(3))
      enddo
      enddo
   endif
ENDIF
   endif
ENDIF
   endif
ENDIF  

old=>old%neighbor 
ENDDO  ! while(associated(old))

return
end
