!!-------------------------------------------------------------------------------
!!.. Author: Yaxun Tang, tang@sep.stanford.edu
!!-------------------------------------------------------------------------------
module base_velgrd_mod
  use base_struct_mod
  implicit none

contains

  subroutine base_velgrd_gen3d(vgrid, pgrid)
    type(vel_grid) :: vgrid
    type(pro_grid) :: pgrid

    !!.. calculate the index of propagation grid on the entire velocity grid
    vgrid%index_provel_xmin = 1.5 + (pgrid%ax%o - vgrid%ax%o)/vgrid%ax%d
    vgrid%index_provel_ymin = 1.5 + (pgrid%ay%o - vgrid%ay%o)/vgrid%ay%d
    vgrid%index_provel_zmin = 1.5 + (pgrid%az%o - vgrid%az%o)/vgrid%az%d
    vgrid%index_provel_xmax = vgrid%index_provel_xmin + pgrid%ax%n - 1
    vgrid%index_provel_ymax = vgrid%index_provel_ymin + pgrid%ay%n - 1
    vgrid%index_provel_zmax = vgrid%index_provel_zmin + pgrid%az%n - 1

    if (vgrid%index_provel_xmin < 1 .or. vgrid%index_provel_xmax > vgrid%ax%n) then
       write(0,*)"error: vgrid%index_provel_xmin or xmax"
       stop
    end if
    if (vgrid%index_provel_ymin < 1 .or. vgrid%index_provel_ymax > vgrid%ay%n) then
       write(0,*)"error: vgrid%index_provel_ymin or ymax"
       stop
    end if
    if (vgrid%index_provel_zmin < 1 .or. vgrid%index_provel_zmax > vgrid%az%n) then
       write(0,*)"error: vgrid%index_provel_zmin or zmax"
       stop
    end if

    return
  end subroutine

end module
