subroutine init_fine_data(child,parent,old_head,drc,drp,dt,is)
!--------------------------------------------------------------------------
! Initialize data on a fine grid. Steps:
! 1. interpolate data from coarser level (parent) to finer one (child)
! 2. transfer fine data from previous structure, if exist
! 3. impose boundary conditions (internal and external)
!
! User and/or application dependent code is burried inside routines
! interpolate_data and impose_fine_boundaries
!
!--------------------------------------------------------------------------
use types
use amr
type(ADAPTIVE_MESH),pointer    :: child,parent,old_head
real(SP), dimension(3)         :: drc,drp !dr of child and parent, resp.
real(SP)                       :: dt  ! dt of child grid
integer(I4B) is

real(SP) dt0,time,dt1
integer(I4B) i,j,k,ii,jj,kk

INTERFACE
       subroutine interpolate_data(childptr,drc)
       use types
       use amr
       type(ADAPTIVE_MESH),pointer       :: childptr
       real(SP), dimension(3)            :: drc   !dr of child     
       end subroutine interpolate_data

       subroutine rgtran(child)
       use types
       use amr
       type(ADAPTIVE_MESH),pointer    :: child
       end subroutine rgtran

subroutine impose_fine_boundaries(childptr)
use types
use amr
type(ADAPTIVE_MESH), pointer     :: childptr
end subroutine impose_fine_boundaries

END INTERFACE


!--------------------------------------------------------------------------
! initalize integration time; also on all retarded meshes
!--------------------------------------------------------------------------

do i=1,parent%grid%n_gfcn
   dt1=0.0
   do j=1,parent%grid%gfcn(i)%n_t 
        child%grid%gfcn(i)%t(j)=parent%grid%gfcn(i)%t(j)-dt1
        dt1=dt1+dt
   enddo 
enddo

time=child%grid%gfcn(1)%t(1)


 write(*,*) 'init_fine_data: calling with interpolate_data'
call interpolate_data(child,drc)
 write(*,*) 'init_fine_data: done with interpolate_data'

do k=1,41,4
do j=1,45,4
do i=1,45,4
   ii = 15 + ((i-1)/4)
   jj = 15 + ((j-1)/4)
   kk = 15 + ((k-1)/4)
!   write(*,*) 'basecoord: ',ii,jj,kk,parent%grid%c(1)%coord(ii), &
!                            parent%grid%c(2)%coord(jj), &
!                            parent%grid%c(3)%coord(kk)
!   write(*,*) 'finecoord: ',i,j,k,child%grid%c(1)%coord(i), &
!                            child%grid%c(2)%coord(j), &
!                            child%grid%c(3)%coord(k)
!   write(*,*) 'base phi value: ',parent%grid%gfcn(phi)%tlev(1)%data(ii,jj,kk),&
!           '   fine phi value: ',child%grid%gfcn(phi)%tlev(1)%data(i,j,k)
!   write(*,*)

enddo
enddo
enddo

!stop

!if there is any overlap between old mesh at this level, it is better
!to transfer the data between meshes at the same level than to
!interpolate them from coarser grid

 write(*,*) 'init_fine_data: calling rgtran'
 call rgtran(child)
 write(*,*) 'init_fine_data: done with rgtran'

!if gflb or gfrb = 1 global boundaries should be applied

child%gflb=0
child%gfrb=0
if(abs(child%grmin(1)-gxmin).lt.1.0E-20) child%gflb(1)=1
if(abs(child%grmin(2)-gymin).lt.1.0E-20) child%gflb(2)=1
if(abs(child%grmin(3)-gzmin).lt.1.0E-20) child%gflb(3)=1
if(abs(child%grmax(1)-gxmax).lt.1.0E-20) child%gfrb(1)=1
if(abs(child%grmax(2)-gymax).lt.1.0E-20) child%gfrb(2)=1
if(abs(child%grmax(3)-gzmax).lt.1.0E-20) child%gfrb(3)=1

!write(*,*) 'init_fine_data: calling impose_fine_boundaries'
call impose_fine_boundaries(child)
!write(*,*) 'init_fine_data: done with impose_fine_boundaries'

return
end subroutine init_fine_data











