subroutine dumpinfo()
use types
use amr
use bh02
implicit none
type(ADAPTIVE_MESH), pointer :: p
real(SP) :: z_val,z_coord_real
integer(I4B) :: lev,gridno, temp1, temp2, temp3, z_coord, nz
integer(I4B) , SAVE :: timestep
integer(I4B) :: i,j

! z_val is the z coordinate for which the data is to be dumped

!z_val = -2.564102     ! value for 40 gridlength
z_val = -1.265823      !value for 80 gridlength
!z_val = -0.6289291    ! value for 160 gridlength

! gridno will eventually have the total # of grids for which data was 
! dumped during this call of dumpinfo ( i.e for this timestep)

gridno=0

! Traverse the data structure for all existing grids, upto the finest level

LEVELS:do lev=1, finlev

!write(6,*) 'maxlev=', maxlev


p=>lp(lev)%ml

!write(6,*) 'lev=', lev
!write(6,*) 'associated(p)=', associated(p)


 if (associated(p)) then

   do while(associated(p))
!write(6,*) 'p%grmin(3)', p%grmin(3)
!write(6,*) 'p%grmax(3)', p%grmax(3)
   

! handle the base grid separately, here you know the index of the 
! z dimension for which you have to dump the information, for the 
! other grids you need to calculate it, that is done in the calculation
! of z_coord few lines below
   
   if(lev.eq.1) then

! for each grid that you dump, dump in the following order, first the 
! level, then the dimensions, then the minimum extents, max extents 
! and finally the data

      write(72,*) lev
      write(72,*) p%gnr(1),p%gnr(2)
      write(72,*) p%grmin(1), p%grmin(2)
      write(72,*) p%grmax(1), p%grmax(2)

!     write(72,*) 'begindata'

! Whenever you dump a grid, increase the value of gridno by 1
   gridno=gridno+1
     do i=1,p%gnr(1)
      do j=1,p%gnr(2)
     write(72,*)  p%grid%gfcn(phi)%tlev(1)%data(i,j,40)
      enddo
     enddo


   else ! handle the other levels
 
! check if the this grid is present at the z coordinate for which 
! we are dumping information 
    if(.not.((z_val.lt.p%grmin(3)).or.(z_val.gt.p%grmax(3)))) then
  

! for each grid that you dump, dump in the following order, first the 
! level, then the dimensions, then the minimum extents, max extents 
! and finally the data

     write(72,*) lev
     write(72,*) p%gnr(1), p%gnr(2)
     write(72,*) p%grmin(1), p%grmin(2)
     write(72,*) p%grmax(1), p%grmax(2)      


!     write(72,*) 'begindata'
! Calculate the index of the z dimension which corresponds to z_val
     nz = p%grid%gfcn(1)%tlev(1)%ddim(3)
     z_coord_real = 1.5 + ((real(nz - 1) * (z_val - p%grid%c(3)%coord(1)))/ &
        (p%grid%c(3)%coord(nz) - p%grid%c(3)%coord(1)))
     z_coord = z_coord_real
!write(*,*) 'dumpinfo: z_coord index is ',z_coord
!write(*,*) 'dumpinfo: z_val is ',z_val

! Whenever you dump a grid, increase the value of gridno by 1
   gridno=gridno+1
     do i=1,p%gnr(1)
      do j=1,p%gnr(2)
     write(72,*)  p%grid%gfcn(phi)%tlev(1)%data(i,j,z_coord)
      enddo
     enddo    
!     write(72,*) 'enddata'
!     write(72,*) 'endlevel'
    
   endif ! if(.not.(.......blah blah..

  endif ! lev.eq.1

   p=>p%neighbor


   enddo ! while associated(p)
 
 endif ! assocaited(p)

ENDDO LEVELS

! Note that this a separate file from the one in which we are dumping the data
! In this file we are dumping the time-step and the no. of grids
! for which info was dumped during this timestep. Note that timestep 
! is declared with the SAVE attribute, so that it's value is retained 
! between calls to dumpinfo

write(73,*) timestep, gridno

timestep=timestep+1
!write(72,*) 'endtimestep'

end subroutine dumpinfo

!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
! So after the run of the program we should have 2 files generated :
! 
! 1. fort.73 : this file contains information on how many grids each
!              timestep generated. 
! 
!   eg.
!
!           0           1
!           1           1
!           2           2
!           3           2
!           4           4
!
!
! 2. fort.72 : this file will contain the actual data in the following form:
! 
!              for each grid it will have :
!
!  level no.   
!  dim(1) dim(2)
!  min_extent(1) min_extent(2)
!  max_extent(1) max_extent(2)
!  data ( with one data element per line)
!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!









