module OW_grabvel

use OW_type
use OW_parms

implicit none

contains

subroutine owgrab_vel(ixs,isp,velint,vel,ni)

integer						:: ixs, ni, nv, isp
real						:: velint(:,:), vel(:,:)
real						:: owrk


! workspace origin 

!    owrk=min((ixs-1)*rec%x%d+rec%x%o+rec%h%o,(ixs-1)*rec%x%d+(isp-1)*rec%h%d+rec%x%o)
!    owrk=(ixs-1)*rec%x%d+rec%x%o+rec%h%o

    if (rec%h%o > 0. .and. rec%h%d < 0) then
       owrk=(ixs-1)*rec%x%d+rec%x%o+rec%h%o+(rec%h%n-1)*rec%h%d
    elseif (rec%h%o < 0. .and. rec%h%d > 0) then
       owrk=(ixs-1)*rec%x%d+rec%x%o+rec%h%o
!    else
!       call seperr("TAPER REGION MUST BE ")
    end if 

    if (owrk < img%xm%o .or. owrk > img%xm%o+img%xm%d*(img%xm%n-1)) then
         write(0,*)  'WORKING SPACE GREATER THAN THE IMAGE SPACE FOR SHOT # ',ixs
         call seperr()
    end if
! grab velocity in the workspace       
    nv = ceiling((owrk-v%xm%o)/v%xm%d)
    ni = nv-ceiling((img%xm%o-v%xm%o)/img%xm%d)+1

    write(0,*) owrk,nv,ni

    vel=0;

    vel(:,:)=velint(:,nv:nv+rec%h%n-1);

end subroutine

end module
