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

contains

  subroutine base_blkcal(xblock, ax)
    type(block)  :: xblock
    type(axis)   :: ax
    integer      :: ixb

    xblock%bres = .false.
    xblock%ds = ax%d
    xblock%nb = ax%n / xblock%ns
    xblock%nb_res = mod(ax%n, xblock%ns)

    if(xblock%nb_res /= 0) then
       xblock%nb = xblock%nb + 1
       xblock%bres = .true.
    end if

    if(allocated(xblock%blen)) deallocate(xblock%blen)
    if(allocated(xblock%os))   deallocate(xblock%os)
    if(allocated(xblock%indx)) deallocate(xblock%indx)

    allocate(xblock%blen(xblock%nb), xblock%os(xblock%nb), xblock%indx(xblock%nb))

    xblock%blen = xblock%ns

    if(xblock%bres) xblock%blen(xblock%nb) = xblock%nb_res

    do ixb=1, xblock%nb
       xblock%os(ixb)   = ax%o + (ixb-1)*xblock%ns*ax%d
       xblock%indx(ixb) = 1 + (ixb-1)*xblock%ns
    end do
  end subroutine


end module
