 program OW3d_arealsht

 use sep 
 use OW_type
 use OW_parms_areal
 use OW_exec_time
 use OW_taper_areal
 use OW_ctrillwei
 use OW_grabvel_areal
 use OW_shaper
 use lloyd_vel

 implicit none

 real						:: start,finish,start1,finish1,a(2),b,c
 integer						:: i,nv,ixs,j,nsy,iw,ih,icomb,ix,stat,iz,ihor,n_enc_sog,iq,nq
 integer						:: ind_trac,i_pos,i_ini,i_fin,igapx,igapy,ni
 integer						:: jgap,jxs,nnn,hij,k,ib,ic,ireal,igap1
 real, allocatable					:: velint(:),taper(:,:),vel(:)
 real,allocatable					:: vref(:,:,:)
 complex,allocatable				:: source(:)
 complex						:: phase

!-----------------------------------------
! Starting ...
!
 call sep_init()
 call sep_begin_prog()
 start=0.;finish=0.
 call owexec_time(start)
!---------------------------------------
! Parameters
!
 call owparam()
! call auxclose("rec_out")
! call auxclose("sou_out")
! call auxinout("rec_out")
! call auxinout("sou_out")


!---------------------------------------------------
!
! Read velocities
!
 allocate(velint(v%z%n*v%xm%n*v%ym%n),vel(nz*kxm%n*kym%n))
 call sreed("vel",velint,v%z%n*v%xm%n*v%ym%n*4)
 call owgrab_velim(velint,vel,ni)
 deallocate(velint)
 if (extrap(1:4).eq."pspi") then
    if (lloyd) then 
       allocate(vref(nref,2,nz))
       call lloyd_vel1(vel,vref)
    end if
 end if
!--------------------------------------------------
! Read source
!
 allocate(source(sou%w%n))
 call sreed("sou",source,8*sou%w%n)
!--------------------------------------------------
! Computing taper
!
 allocate(taper(kxm%n,kym%n))
 call owtaper(taper)

!!--------------------------------------------------
!! Setting FFTW plans
!call owfftws_init()
!--------------------------------------------------
! Wavefield extrapolation
!
    do ireal=1,nreal
       do igapx=1,ngapx
          do igapy=1,ngapy
          do ihor=1,nhorz
             if (lloyd) then
                call owexec_time(start)
                call owctrlwei_init(ihor,ireal,igapx,igapy)
                call owctrlwei(taper=taper,vsal=vsal,vel=vel,sign=+1,vref1=vref)
                call owexec_time(finish)
                write(0,4) "Receiver wavefield   - realization # ",ireal,' FROM ',nreal,&
                        ' gapx ',igapx,' FROM ',ngapx,' gapy ',igapy,' FROM ',ngapy,  &
                        ' horz ',ihor,' FROM ',nhorz,' in ',finish-start,' s'
                call owexec_time(start)
                call owctrlwei_init(ihor,ireal,igapx,igapy)
                call owctrlwei(taper=taper,vsal=vsal,vel=vel,sign=-1,vref1=vref)
                call owexec_time(finish)
                write(0,4) "Source   wavefield   - realization # ",ireal,' FROM ',nreal,&
                        ' gapx ',igapx,' FROM ',ngapx,' gapy ',igapy,' FROM ',ngapy,  &
                        ' horz ',ihor,' FROM ',nhorz,' in ',finish-start,' s'
             else
                call owexec_time(start)
                call owctrlwei_init(ihor,ireal,igapx,igapy)
                call owctrlwei(taper=taper,vsal=vsal,vel=vel,sign=+1)
                call owexec_time(finish)
                write(0,4) "Receiver wavefield   - realization # ",ireal,' FROM ',nreal,&
                        ' gapx ',igapx,' FROM ',ngapx,' gapy ',igapy,' FROM ',ngapy,  &
                        ' horz ',ihor,' FROM ',nhorz,' in ',finish-start,' s'
                call owexec_time(start)
                call owctrlwei_init(ihor,ireal,igapx,igapy)
                call owctrlwei(taper=taper,vsal=vsal,vel=vel,sign=-1)
                call owexec_time(finish)
                write(0,4) "Source   wavefield   - realization # ",ireal,' FROM ',nreal,&
                        ' gapx ',igapx,' FROM ',ngapx,' gapy ',igapy,' FROM ',ngapy,  &
                        ' horz ',ihor,' FROM ',nhorz,' in ',finish-start,' s'
             end if
          end do
!          call owshaper(source,wave_S,-1)
!          call owshaper(source,wave_R,+1)
          end do
       end do
    end do

4  format(A35,i4,7(A6,i4),A4,f6.2,a2)
!--------------------------------------------------
! Finishing ...
!
 if (allocated(taper))  deallocate(taper)
 if (allocated(vel))    deallocate(vel)
 if (allocated(vref))   deallocate(vref)
 if (allocated(source)) deallocate(source)
!!--------------------------------------------------
!! Destroying FFTW plans
! call owfftws_close()

 call sep_end_prog()
 end program OW3d_arealsht
