 program OW_exprefl

 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_rndseed1
 use OW_encoding
 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,igap,ni
 integer						:: jgap,jxs,nnn,hij,k,ib,ic,ireal,igap1
 real, allocatable					:: velint(:),taper(:),vel(:)
 real,allocatable					:: vref(:,:,:),temp(:)
 complex,allocatable				:: source(:), wave_R(:,:), phase(:)

!-----------------------------------------
! Starting ...
!
 call sep_init()
 call sep_begin_prog()
 start=0.;finish=0.
 call owexec_time(start)
!---------------------------------------
! Parameters
!
 call owparam()
 start=0.;finish=0. 
!--------------------------------------------------
! Read source
!
 allocate(source(sou%w%n))
 call sreed("sou",source,8*sou%w%n)
 allocate(phase(sou%w%n))
 phase = cmplx(cos(pi/4.), -sin(pi/4.)) * sqrt(sou%w%all)
 source=source*phase
 deallocate(phase)
!---------------------------------------------------
!
! Read velocities
!
 allocate(velint(v%z%n*v%xm%n),vel(nz*kxm%n))
 call sreed("vel",velint,v%z%n*v%xm%n*4)
 call owgrab_velim(velint,vel,ni)
 vel=vel*0.5
 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
!--------------------------------------------------
! Computing taper
!
 allocate(taper(kxm%n))
 call owtaper_init()
 call owtaper(taper)
!--------------------------------------------------
! Wavefield extrapolation
!
    allocate(wave_R(kxm%n,sou%w%n))
       do igap=1,ngap
          wave_R=0
          if (lloyd) then
             call owexec_time(start)
             call owctrlwei(taper=taper,vsal=vsal,vel=vel,GG=wave_R,sign=+1,vref1=vref)
             call owexec_time(finish)
             write(0,4) "Receiver wavefield   - realization # ",ireal,' FROM ',nreal,&
                     ' igap ',igap,' FROM ',ngap,' horz ',ihor,' FROM ',nhorz,' in ',finish-start,' s'
          else
             call owexec_time(start)
             call owctrlwei(taper=taper,vsal=vsal,vel=vel,GG=wave_R,sign=+1)
             call owexec_time(finish)
             write(0,4) "Receiver wavefield   - realization # ",ireal,' FROM ',nreal,&
                     ' igap ',igap,' FROM ',ngap,' horz ',ihor,' FROM ',nhorz,' in ',finish-start,' s'
             end if
          end do
          if (shaper) call owshaper(source,wave_R,+1)
          call srite("rec_out",wave_R(padtraces+tapertr+1:kxm%n-(padtraces+tapertr),:),8*sou%w%n*(kxm%n-2*(padtraces+tapertr)))
       end do
    end do
3  format(A35,i4,A6,i4,A4,f6.2,a2)
4  format(A35,i4,5(A6,i4),A4,f6.2,a2)
 deallocate(wave_R,taper,source)
 deallocate(vel)
 call sep_end_prog()
 end program
