subroutine dumpdata(finlev)
use gridfunctions

type(ADAPTIVE_MESH),pointer :: grid,wgrid
integer gridno,finlev
integer lev
integer, SAVE :: timestep=0

gridno=0

!first the base grid

lev=1
grid=>lp(1)%grid
write(72,*) lev
write(72,*) grid%nr(1:2)
write(72,*) grid%rmin(1:2)
write(72,*) grid%rmax(1:2)

gridno=gridno+1
do i=1,grid%nr(1)
 do j=1,grid%nr(2)
   write(72,*) g1(1,i,j,1)
 enddo
enddo

!now level 2
  lev=2
  if(allocated(g21)) then
   nullify(grid)
   wgrid=>lp(2)%grid
   do while(associated(wgrid))
    if(wgrid%grid_id == 1) grid=>wgrid
    wgrid=>wgrid%neighbor
   enddo
   if(associated(grid)) then
    write(72,*) lev
    write(72,*) grid%nr(1:2)
    write(72,*) grid%rmin(1:2)
    write(72,*) grid%rmax(1:2)
   else
    write(6,*) 'grid 21 not found'
    stop
   endif

   gridno=gridno+1
   if(size(g21,dim=2) /= grid%nr(1)) then
    write(6,*) 'Oops, size does not match; g21x', size(g21,dim=2),grid%nr(1)
    stop
   endif
   if(size(g21,dim=3) /= grid%nr(2)) then
    write(6,*) 'Oops, size does not match; g21y', size(g21,dim=3),grid%nr(2)
    stop
   endif
   do i=1,grid%nr(1)
    do j=1,grid%nr(2)
     write(72,*) g21(1,i,j,1)
     enddo
   enddo
!write(72,*) g21(1,:,1,1) 
!write(72,*) '+'
!write(72,*) g21(1,5,:,1),'&' 
  endif

  if(allocated(g22)) then 
   nullify(grid)
   wgrid=>lp(2)%grid
   do while(associated(wgrid))
    if(wgrid%grid_id == 2) grid=>wgrid
    wgrid=>wgrid%neighbor
    
   enddo
   if(associated(grid)) then
    write(72,*) lev
    write(72,*) grid%nr(1:2)
    write(72,*) grid%rmin(1:2)
    write(72,*) grid%rmax(1:2)
   else
    write(6,*) 'grid 22 not found'
    stop
   endif

   gridno=gridno+1
   if(size(g22,dim=2) /= grid%nr(1)) then
    write(6,*) 'Oops, size does not match; g22x', size(g22,dim=2),grid%nr(1)
    stop
   endif
   if(size(g22,dim=3) /= grid%nr(2)) then
    write(6,*) 'Oops, size does not match; g22y', size(g22,dim=3),grid%nr(2)
    stop
   endif
   do i=1,grid%nr(1)
   do j=1,grid%nr(2)
    write(72,*) g22(1,i,j,1)
    enddo
   enddo
  endif   

  if(allocated(g23)) then 
   nullify(grid)
   wgrid=>lp(2)%grid
   do while(associated(wgrid))
    if(wgrid%grid_id == 3)  grid=>wgrid
    wgrid=>wgrid%neighbor
   enddo
   if(associated(grid)) then
    write(72,*) lev
    write(72,*) grid%nr(1:2)
    write(72,*) grid%rmin(1:2)
    write(72,*) grid%rmax(1:2)
   else
    write(6,*) 'grid 23 not found'
    stop
   endif

   gridno=gridno+1
   if(size(g23,dim=2) /= grid%nr(1)) then
    write(6,*) 'Oops, size does not match; g23x', size(g23,dim=2),grid%nr(1)
    stop
   endif
   if(size(g23,dim=3) /= grid%nr(2)) then
    write(6,*) 'Oops, size does not match; g23y', size(g23,dim=3),grid%nr(2)
    stop
   endif
   do i=1,grid%nr(1)
   do j=1,grid%nr(2)
    write(72,*) g23(1,i,j,1)
    enddo
   enddo
  endif   

!now level 3
  lev=3
  if(allocated(g31)) then
   nullify(grid)
   wgrid=>lp(3)%grid
   do while(associated(wgrid))
    if(wgrid%grid_id == 1) grid=>wgrid
    wgrid=>wgrid%neighbor
   enddo
   if(associated(grid)) then
    write(72,*) lev
    write(72,*) grid%nr(1),grid%nr(2)
    write(72,*) grid%rmin(1:2)
    write(72,*) grid%rmax(1:2)
   else
    write(6,*) 'grid 31 not found'
    stop
   endif

   gridno=gridno+1
   if(size(g31,dim=2) /= grid%nr(1)) then
    write(6,*) 'Oops, size does not match; g31x', size(g31,dim=2),grid%nr(1)
    stop
   endif
   if(size(g31,dim=3) /= grid%nr(2)) then
    write(6,*) 'Oops, size does not match; g31y', size(g31,dim=3),grid%nr(2)
    stop
   endif
   do i=1,grid%nr(1)
    do j=1,grid%nr(2)-1
     write(72,*) g31(1,i,j,1)
     enddo
   enddo
!write(72,*) g31(1,:,1,1) 
!write(72,*) '+'
!write(72,*) g31(1,5,:,1) 

  endif


  if(allocated(g32)) then 
   nullify(grid)
   wgrid=>lp(3)%grid
   do while(associated(wgrid))
    if(wgrid%grid_id == 2) grid=>wgrid
    wgrid=>wgrid%neighbor
   enddo
   if(associated(grid)) then
    write(72,*) lev
    write(72,*) grid%nr(1),grid%nr(2)-1
    write(72,*) grid%rmin(1:2)
    write(72,*) grid%rmax(1:2)
   else
    write(6,*) 'grid 32 not found'
    stop
   endif

   gridno=gridno+1
   if(size(g32,dim=2) /= grid%nr(1)) then
    write(6,*) 'Oops, size does not match; g32x', size(g32,dim=2),grid%nr(1)
    stop
   endif
   if(size(g32,dim=3) /= grid%nr(2)) then
    write(6,*) 'Oops, size does not match; g32y', size(g32,dim=3),grid%nr(2)
    stop
   endif

   do i=1,grid%nr(1)
   do j=1,grid%nr(2)-1
    write(72,*) g32(1,i,j,1)
    enddo
   enddo
  endif   

  if(allocated(g33)) then 
   nullify(grid)
   wgrid=>lp(3)%grid
   do while(associated(wgrid))
    if(wgrid%grid_id == 3) grid=>wgrid
    wgrid=>wgrid%neighbor
   enddo
   if(associated(grid)) then
    write(72,*) lev
    write(72,*) grid%nr(1),grid%nr(2)-1
    write(72,*) grid%rmin(1:2)
    write(72,*) grid%rmax(1:2)
!    write(72,*) grid%rmax(1),grid%rmax(2)
   else
    write(6,*) 'grid 33 not found'
    stop
   endif


   gridno=gridno+1
   if(size(g33,dim=2) /= grid%nr(1)) then
    write(6,*) 'Oops, size does not match; g33x', size(g33,dim=2),grid%nr(1)
    stop
   endif
   if(size(g33,dim=3) /= grid%nr(2)) then
    write(6,*) 'Oops, size does not match; g33y', size(g33,dim=3),grid%nr(2)
    stop
   endif
   do i=1,grid%nr(1)
   do j=1,grid%nr(2)-1
    write(72,*) g33(1,i,j,1)
    enddo
   enddo
  endif   


timestep=timestep+1
write(73,*) timestep,gridno
!write(72,*) timestep,gridno,'================ END <<<<<<<<<<<<' 

!if(timestep > 20) stop
return
end




