subroutine regrid(base,base_grid,g1,g2,g3,g4,level,finest_level,lp)
use adaptive
use parameters
type(ADAPTIVE_MESH),pointer    :: base
real,dimension(:,:,:)          :: base_grid
real,dimension(:,:,:,:),target :: g1,g2,g3,g4
integer level,finest_level
type(LEVEL_INFO), dimension(maxlevel)   :: lp

!HPF$ PROCESSORS P(2,2)
!HPF$ DISTRIBUTE *(*,BLOCK,BLOCK) onto *P :: base_grid
!HPF$ DISTRIBUTE *(*,*,BLOCK,BLOCK) onto *P :: g1,g2,g3,g4

! local variables
type(ADAPTIVE_MESH),pointer    :: grid
real, dimension(:,:,:),pointer :: gridpointer      
real, dimension(2)             :: rmin
real, dimension(2)             :: rmax
integer, dimension(2)          :: nr
real, dimension(0:2)           :: dr

!HPF$ DISTRIBUTE (*,BLOCK,BLOCK) :: gridpointer
INTERFACE

  subroutine initialize_child(parent,child,grid)
  use adaptive
  real,dimension(:,:,:) :: parent,child
  type(ADAPTIVE_MESH),pointer :: grid
!HPF$ PROCESSORS P(2,2)
!HPF$ DISTRIBUTE *(*,BLOCK,BLOCK) ONTO *P :: parent,child
  end subroutine initialize_child

END INTERFACE

   rmin=(/10,10/)
   rmax=rmin+10    
   nr=(/11,11/)
   dr=lp(level+1)%dr
   t=base%t
   call allocate_ADAPTIVE_MESH(lp,level+1,base,grid,t,rmin,rmax,nr)

   select case(grid%grid_id)
   case (1)
            gridpointer=>g1(level+1,:,:,:)
   case (2)
            gridpointer=>g2(level+1,:,:,:)
   case (3)
            gridpointer=>g3(level+1,:,:,:)
   case (4)
            gridpointer=>g4(level+1,:,:,:)
   case default
            write(6,*) 'no more grids allowed at this level'
            stop
   end select

!  initialize data
   call initialize_child(base_grid,gridpointer,grid)
    finest_level=level+1

! ====================== end of regridding ===========================

      return
      end

