# 1 "<stdin>"
# 1 "<built-in>"
# 1 "<command line>"
# 1 "<stdin>"
module velsimpx
! simple velocity transform
  use adj_mod
  implicit none
  integer, private :: nt
  integer, private :: nx
  integer, private :: ns
  integer, private :: it
  integer, private :: iz
  integer, private :: ix
  integer, private :: is
  real, private :: t0
  real, private :: dt
  real, private :: x0
  real, private :: dx
  real, private :: s0
  real, private :: ds
  real, private :: x
  real, private :: s
  real, private :: sx
  real, private :: t
  real, private :: z
  real, private :: wt
  logical, private :: lint
  contains
  subroutine velsimpx_init ( t0_in,dt_in,x0_in,dx_in,s0_in,ds_in,nt_in&
    &,nx_in,ns_in,lint_in )
    real :: t0_in
    real :: dt_in
    real :: x0_in
    real :: dx_in
    real :: s0_in
    real :: ds_in
    integer :: nt_in
    integer :: nx_in
    integer :: ns_in
    logical :: lint_in
    t0 = t0_in
    dt = dt_in
    x0 = x0_in
    dx = dx_in
    s0 = s0_in
    ds = ds_in
    nt = nt_in
    nx = nx_in
    ns = ns_in
    lint = lint_in
  end subroutine
  function velsimpx_lop ( adj, add, modl, data) result(stat)
    integer :: stat
    logical,intent(in) :: adj,add
    real,dimension(:) :: modl,data
    call adjnull (adj,add,modl,data )
    call velsimpx_lop2(adj,add,modl,data )
    stat=0
  end function
  subroutine velsimpx_lop2(adj,add,modl,data)
    logical,intent(in) :: adj,add
    real, dimension (nt,ns) :: modl
    real, dimension (nt,nx) :: data
    integer :: itl, itr !## New
    real :: cl, cr, tt !## New
    do is = 1,ns
! slowness loop
      s = s0 + (is-1) * ds
      do ix = 1,nx
! offset loop
        x = x0 + (ix-1) * dx
        do iz = 2,nt
! zero-offset traveltime loop
          z = t0 + (iz-1) * dt
          sx = abs( s * x) ! normalized offset
          t = sqrt( z * z + sx * sx) ! NMO
          if ( lint) then
! Do linear interpolation...
! it = 1.5 + (t-t0)/dt ###------------ Remove this line
! if( it > nt) exit # out of the range -- Modify this line
            tt = 1.0 + (t-t0)/dt !## New
            itr = ceiling(tt)
!## New
            itl = floor(tt)
!## New
            cr = tt - itl !## New
            cl = 1.0 - cr !## New
            if ( itr > nt) then
              exit !## New
            end if
          else
! ...or nearest-neighbor.
            it = 1.5 + (t-t0)/dt
            if ( it > nt) then
              exit ! out of the range
            end if
          end if
          wt = 1.0
          ! (t,x)-dependent weighting
          if ( adj) then
            if ( lint) then
! modl(iz,is) += data(it,ix)*wt ###- Remove this line
              modl(iz,is) = modl(iz,is) + ( cl*data(itl,ix)+cr*data(itr&
                &,ix) )*wt !## New
            else
              modl(iz,is) = modl(iz,is) + data(it,ix)*wt
            end if
          else
            if ( lint) then
! data(it,ix) += modl(iz,is)*wt ###- Remove this line
              data(itl,ix) = data(itl,ix) + cl*modl(iz,is) * wt
              !## New
              data(itr,ix) = data(itr,ix) + cr*modl(iz,is) * wt
              !## New
            else
              data(it,ix) = data(it,ix) + modl(iz,is)*wt
            end if
          end if
        end do
! End time loop
      end do
! End offset loop
    end do
! End zero-offset traveltime loop
  end subroutine
  subroutine velsimpx_close()
  end subroutine
end module
! end module
