subroutine rgrid2()
!-------------------------------------------------------------------
! Creates new mesh hierarchy from curlev+1 up to updated finlev
! according to the cluster structure and then initialize data
! (by interpolation) and imposes boundary conditions.
!
! User and/or application dependent code is buried in subroutine
! init_fine_data
! 
!-------------------------------------------------------------------
use amr

! local variables
type(ADAPTIVE_MESH),pointer :: parent,child,head,current,old
type(CLUSTERS),pointer      :: clp
character(200)              :: chname = 'child'  
integer(I4B) g 

INTERFACE
subroutine init_fine_data(child,parent,old_head,drc,drp,dt,is)
use types
use amr
type(ADAPTIVE_MESH),pointer    :: child,parent,old_head
real(SP), dimension(3)         :: drc,drp
real(SP)                       :: dt 
integer(I4B) is
end subroutine init_fine_data
END INTERFACE

!------------------------------------------------------- 
! DETERMINE NEW FINEST LEVEL OF REFINEMENT 
! -------------------------------------------------------

finlev=curlev
do lev = curlev + 1, maxlev
   if(associated(lp(lev)%clhead)) finlev = lev
enddo

write(*,*) 'rgrid2: finlev definition is ',finlev

IF(curlev+1.le.finlev) THEN       
   do lev=curlev+1,finlev 

       nullify(child)
       nullify(head)
       nullify(parent)
       nullify(current)

       clp=>lp(lev)%clhead
 
       DO WHILE(associated(clp))
          call find_parent(lp(lev-1)%ml,parent,clp%clrmin,clp%clrmax)
          if(.not.associated(parent)) then
             write(6,*) 'regrid: parent not found'
             stop
          endif

write(*,*) 'rgrid2: calling initialize_grid with data:'
write(*,*) '   lev:',lev
write(*,*) '   min:',clp%clrmin
write(*,*) '   max:',clp%clrmax
write(*,*) '   dr :',lp(lev)%ldr
           call initialize_grid(child,lev,chname,clp%clrmin(1), &
           clp%clrmin(2),clp%clrmin(3),clp%clrmax(1),clp%clrmax(2),&
           clp%clrmax(3),lp(lev)%ldr)
         
          child%parent=>parent 

          is=int(lp(lev)%rcxshp)
write(*,*) 'rgrid2: calling init_fine_data'
          call init_fine_data(child,parent,lp(lev)%ml,lp(lev)%ldr,&
          lp(lev-1)%ldr,lp(lev)%ldt,is)
write(*,*) 'rgrid2: done with init_fine_data'

          child%parent=>parent
          if(associated(current)) then
             if(parent%g == current%parent%g) then
                current%sibiling=>child
             endif
          endif

          if(associated(head)) then             
             current%neighbor=>child
             current=>current%neighbor           
             nullify(current%neighbor)
          else
             head=>child
             current=>child
          endif 

          clp=>clp%clnext

         ENDDO

        current=>lp(lev)%ml
        DO WHILE(associated(current))
         old=>current%neighbor
         call deallocateGF3TL(current%grid)
         g=get_g(current%g)
         deallocate(current)
         current=>old
        ENDDO

        lp(lev)%ml=>head
        ENDDO 

       ENDIF

!      Now, deallocate the possibly left-over levels after finlev
write(*,*) 'rgrid: entering new deallocation routine '

       IF (finlev .lt. maxlev) THEN
          do lev=finlev+1,maxlev
write(*,*) 'rgrid2: in last loop lev is ',lev
             current=>lp(lev)%ml
             if(associated(lp(lev)%ml)) then
                nullify(lp(lev)%ml)
             endif
             DO WHILE(associated(current))
write(*,*) 'rgrid2: in while loop lev is ',lev
                old=>current%neighbor
                call deallocateGF3TL(current%grid)
                g=get_g(current%g)
                deallocate(current)
                current=>old
write(*,*) 'rgrid2: end of while loop lev is ',lev
             ENDDO
          enddo
       ENDIF

       return 
       end subroutine rgrid2






