 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
 use OW_rndseed1

 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(:),GG(:,:,:)
 real,allocatable                                           :: intmp(:,:,:,:,:)
 real,allocatable                                           :: randz(:)
 complex						:: phase

!-----------------------------------------
! Starting ...
!
 call sep_init()
 call sep_begin_prog()
 start=0.;finish=0.
 call owexec_time(start)
!---------------------------------------
! Parameters
!
 call auxinout("rec_out")
 call auxinout("sou_out")
 call owparam()
! call auxclose("rec_out")
! call auxclose("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)

 allocate(intmp(ncigx,sog%shx%n,ncigy,sog%shy%n,nz),randz(sog%xm%n*sog%ym%n*sou%w%n))
!--------------------------------------------------
! Wavefield extrapolation
!
    do ireal=1,nreal
       do igapx=1,ngapx
          do igapy=1,ngapy
          do ihor=1,nhorz
             randz=0.;
             call init_random_seed3(randz)

             !--------------------------------------------------
             ! Read rotated CIGs
             intmp=0.
             call sreed_window("cig",6,(/sog%xm%n,sog%shx%n,sog%ym%n,sog%shy%n, sog%z%n  ,  nhorz/),&
                                 (/ncigx, sog%shx%n,  ncigy, sog%shy%n,     nz   ,      1/),&
                                 (/igapx-1,       0,igapy-1,         0, nzmin-1  , ihor-1/),&
                                 (/ngapx,         1,  ngapy,         1,   1      ,      1/),&
                                 4,intmp) 

             if (lloyd) then
                call owexec_time(start)
                call owctrlwei_init(ihor,ireal,igapx,igapy)
                call owctrlwei(intmp=intmp,taper=taper,vsal=vsal,randz=randz,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(intmp=intmp,taper=taper,vsal=vsal,randz=randz,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(intmp=intmp,taper=taper,vsal=vsal,randz=randz,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(intmp=intmp,taper=taper,vsal=vsal,randz=randz,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

          if (shape) then
             allocate(GG(kxm%n-2*(padtracesx+tapertrx),kym%n-2*(padtracesy+tapertry),sou%w%n))
             call gathers_init(ihor,ireal,igapx,igapy)
             call gathers_io(GG,"rec_out","reed")
             call owshaper(source,GG,+1)
             call gathers_io(GG,"rec_out","rite")
             call gathers_io(GG,"sou_out","reed")
             call owshaper(source,GG,-1)
             call gathers_io(GG,"sou_out","rite")
             deallocate(GG)
          end if
          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)
 if (allocated(intmp))  deallocate(intmp)
 if (allocated(randz))  deallocate(randz)
!!--------------------------------------------------


 call sep_end_prog()
 end program OW3d_arealsht
