 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_rndseed1
 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					:: randz(:),vref(:,:,:)
 complex,allocatable				:: source(:),GG(:,:,:)
 complex, allocatable				:: wave_R(:,:,:),wave_S(:,:,:)
 complex						:: phase

!-----------------------------------------
! Starting ...
!
 call sep_init()
 call sep_begin_prog()
 open(unit=2,file="logfile.txt")
 open(unit=3,file="logfile1.txt")
 open(unit=4,file="logfile2.txt")
 open(unit=5,file="logfile3.txt")
 start=0.;finish=0.
 call owexec_time(start)
!---------------------------------------
! Parameters
!
 call owparam()

 allocate(randz(sog%xm%n*sog%ym%n*sou%w%n))
!---------------------------------------------------
!
! 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)
!--------------------------------------------------
! Wavefield extrapolation
!
 allocate(GG(kxm%n,kym%n,sou%w%n))
! if (encode.and.(rand.or.exist_file("InCode"))) then
    allocate(wave_S(kxm%n,kym%n,sou%w%n),wave_R(kxm%n,kym%n,sou%w%n))
    do ireal=1,nreal
       do igapx=1,ngapx
          do igapy=1,ngapy
          wave_S=0;wave_R=0
          do ihor=1,nhorz
             randz=0
             call init_random_seed3(randz)
             if (lloyd) then
                call owexec_time(start)
                GG=0
                call owctrlwei_init(ihor,ireal)
                call owctrlwei(taper=taper,vsal=vsal,vel=vel,GG=GG,sign=+1,&
                            rand1=randz,vref1=vref,igapx=igapx,igapy=igapy)
                wave_R=wave_R+GG
                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)
                GG=0
                call owctrlwei_init(ihor,ireal)
                call owctrlwei(taper=taper,vsal=vsal,vel=vel,GG=GG,sign=-1,&
                            rand1=randz,vref1=vref,igapx=igapx,igapy=igapy)
                wave_S=wave_S+GG
                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)
                GG=0
                call owctrlwei_init(ihor,ireal)
                call owctrlwei(taper=taper,vsal=vsal,vel=vel,GG=GG,sign=+1,&
                            rand1=randz,igapx=igapx,igapy=igapy)
                wave_R=wave_R+GG
                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)
                GG=0
                call owctrlwei_init(ihor,ireal)
                call owctrlwei(taper=taper,vsal=vsal,vel=vel,GG=GG,sign=-1,&
                            rand1=randz,igapx=igapx,igapy=igapy)
                wave_S=wave_S+GG
                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)
          call srite("rec_out",wave_R(padtracesx+tapertrx+1:kxm%n-(padtracesx+tapertrx),&
                                      padtracesy+tapertry+1:kym%n-(padtracesy+tapertry),:),&
                                      8*sou%w%n*(kxm%n-2*(padtracesx+tapertrx))*(kym%n-2*(padtracesy+tapertry)))
          call srite("sou_out",wave_S(padtracesx+tapertrx+1:kxm%n-(padtracesx+tapertrx),&
                                      padtracesy+tapertry+1:kym%n-(padtracesy+tapertry),:),&
                                      8*sou%w%n*(kxm%n-2*(padtracesx+tapertrx))*(kym%n-2*(padtracesy+tapertry)))
          end do
       end do
    end do
  
! end if
3  format(A35,i4,A6,i4,A4,f6.2,a2)
4  format(A35,i4,7(A6,i4),A4,f6.2,a2)

!--------------------------------------------------
! Finishing ...
!
 close(2)
 close(3)
 close(4)
 close(5)

 deallocate(GG,wave_R,wave_S,taper)
 deallocate(vel)

 call sep_end_prog()
 end program OW3d_arealsht
