subroutine interpolate_data(childptr,drc)
!---------------------------------------------------------------------------
! interpolation driver: to be supplied by user
! given existing meshes on coarse (parentptr) and fine (childptr) levels
! data are transferred and interpolated to the fine level for all
! grid functions defined in GF3TL 
!---------------------------------------------------------------------------

! VERSION for W3d

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

!local variables     
integer(I4B)             :: order,dim,rho


! Interpolate grid functions on time level 1.  Where time level
! 2 exists, interpolates 2 time levels, and averages, which
! is consistent with refinement ratio = 2.  

INTERFACE
      subroutine interpolate(childgrid,order,dim,refinerat,xx,yy,zz,nx,ny,nz)
      use types
      implicit none
     
      real(SP), dimension(:,:,:) :: childgrid
      integer(I4B)               :: order, dim, refinerat,no_of_planes
      integer(I4B)               :: i,j,k,no_of_cols,no_of_rows, err
      real(SP), dimension(:)     :: xx,yy,zz     
      integer(I4B)               :: nx,ny,nz
      end subroutine interpolate
END INTERFACE



dim=3      ! dimensionality of grid function (:,:,:)
order=1    ! order of interpolation 
rho=rcxshp ! refinement ratio

if (rho .ne. 2) then
   write(*,*) 'Error: interpolate_data: refine rat. not 2'
   stop
endif

do i=1,n_gfcn
! first transfer 'node' data from parent to child on level 1
call coarse_to_fine(childptr,i,rho)
! now interpolate the data
call interpolate(childptr%grid%gfcn(i)%tlev(1)%data,order,dim,rho, &
                  childptr%grid%c(1)%coord,childptr%grid%c(2)%coord, &
                  childptr%grid%c(3)%coord, &
                  childptr%grid%gfcn(i)%tlev(1)%ddim(1), &
                  childptr%grid%gfcn(i)%tlev(1)%ddim(2), &
                  childptr%grid%gfcn(i)%tlev(1)%ddim(3) )

enddo

! Where there is a level 2, interpolate, then average with
! time level 1 (coarse_to_fine already took care of values 
! on second time level

do i=1,n_gfcn
if(childptr%grid%gfcn(i)%n_t > 1) then
   call interpolate(childptr%grid%gfcn(i)%tlev(2)%data,order,dim,rho, &
                  childptr%grid%c(1)%coord,childptr%grid%c(2)%coord, &
                  childptr%grid%c(3)%coord, &
                  childptr%grid%gfcn(i)%tlev(2)%ddim(1), &
                  childptr%grid%gfcn(i)%tlev(2)%ddim(2), &
                  childptr%grid%gfcn(i)%tlev(2)%ddim(3) )

!  now, average the 2 time level data

   childptr%grid%gfcn(i)%tlev(2)%data =   &
         (childptr%grid%gfcn(i)%tlev(2)%data + &
          childptr%grid%gfcn(i)%tlev(1)%data )/2.0

endif
enddo

! write(6,*)  childptr%grid%gfcn(AMPL)%tlev(2)%data(:,:,:), 'end'


! ESTRE does not require interpolation; it is set to zero by ctrest anyway
! there is no more grind functions defined

return
end subroutine interpolate_data






