program OWwemva_opers

use sep 
use OW_type
use OW_grabvel_areal
use OW_exec_time
use OW_parms_areal
use OW_taper_areal
use OW_wei_areal
use OW_dotst

implicit none

logical						:: dotimg
real						:: start, finish, o3
integer						:: ixs,ino,ni,i,st,idip,n3,t3,iz
real, allocatable					:: velint(:,:),taper(:),deltaSint(:,:)
real, allocatable					:: R_image(:,:,:),scatS(:,:)
real, allocatable					:: vel(:,:),vref(:,:,:)
complex, allocatable				:: recv(:,:),source(:,:)
real,allocatable					:: deltaI(:,:,:),deltaS(:,:)

 call sep_init()
 call sep_begin_prog()

 start=0.;finish=0.
 call owexec_time(start)
 call owparam()
 allocate(source(sou%h%n,sou%w%n))
 allocate(recv(rec%h%n,rec%w%n))
 if (not(dot)) then
    if (adj) then
       allocate(deltaS(img%z%n,img%xm%n));deltaS=0.
       allocate(scatS(img%z%n,img%xm%n));scatS=0.
       allocate(deltaI(img%z%n,img%sh%n,img%xm%n));deltaI=0.
       call sreed("deltaI",deltaI(:,:,tapertr+padtraces+1:img%xm%n-(tapertr+padtraces)), &
                                    4*img%z%n*img%sh%n*(img%xm%n-2*(tapertr+padtraces)))
    else
       allocate(deltaSint(v%z%n,v%xm%n),deltaS(img%z%n,img%xm%n))
       allocate(deltaI(img%z%n,img%sh%n,img%xm%n))
       allocate(R_image(img%z%n,img%sh%n,img%xm%n));R_image=0.
       call sreed("deltaS",deltaSint,v%z%n*v%xm%n*4)
       call owgrab_vel(deltaSint,deltaS,ni)
       deallocate(deltaSint)
    end if
 end if
 allocate(taper(img%xm%n))
 call owtaper(tapertr,taper)

 allocate(velint(v%z%n,v%xm%n),vel(img%z%n,img%xm%n))
 allocate(vref(nref,2,v%z%n))

 call sreed("vel",velint,v%z%n*v%xm%n*4)
 call owgrab_vel(velint,vel,ni)
 deallocate(velint)
 if(exist_file("vref")) then
   call sreed("vref",vref,v%z%n*2*nref*4)
 end if
 call owexec_time(finish)
 write(0,'(8x,a,f10.2,a)')' Init.phase - Total elapsed time:', (finish - start),' (sec)'

 open(unit=2,file='lixo')
 if (dot) then
    start=0.;finish=0. 
    call owexec_time(start)
    call from_param("dotimg",dotimg,.false.)
    recv=0;source=0;
    call sreed("sou",source,8*sou%h%n*sou%w%n)
    call sreed("in",recv,8*rec%h%n*rec%w%n)
    if(exist_file("vref")) then
       call owdotst_wemva(taper,recv,source,vel,vref)
    else
       call owdotst_wemva(taper,recv,source,vel)
    end if
    call owexec_time(finish)
 else
    if (adj) then
       do idip=1,rec%xd%n
          start=0.;finish=0. 
          call owexec_time(start)
          recv=0;source=0;deltaS=0
          call sreed("sou",source,8*sou%h%n*sou%w%n)
          call sreed("in",recv,8*rec%h%n*rec%w%n)
          if(exist_file("vref")) then
             call owwemva(.true.,taper,recv,source,vel,deltaS,deltaI,vref)
          else
             call owwemva(.true.,taper,recv,source,vel,deltaS,deltaI)
          end if
          scatS=scatS+deltaS
          call owexec_time(finish)
          write(0,111) "EXTRAPOLATED WAVEFIELDS = ",idip," OUT OF ",rec%xd%n," IN ",(finish-start)/60," min"
       end do
   else
       do idip=1,rec%xd%n
          start=0.;finish=0. 
          call owexec_time(start)
          recv=0;source=0;deltaI=0
          call sreed("sou",source,8*sou%h%n*sou%w%n)
          call sreed("in",recv,8*rec%h%n*rec%w%n)
          if(exist_file("vref")) then
             call owwemva(.false.,taper,recv,source,vel,deltaS,deltaI,vref)
          else
             call owwemva(.false.,taper,recv,source,vel,deltaS,deltaI)
          end if
          R_image=R_image+deltaI
          call owexec_time(finish)
          write(0,111) "EXTRAPOLATED WAVEFIELDS = ",idip," OUT OF ",rec%xd%n," IN ",(finish-start)/60," min"
       end do
   end if
   111 format(a26,i4,a8,i4,a4,f6.2,a4)
    if (adj) then
       call srite("out",scatS(:,tapertr+padtraces+1:img%xm%n-tapertr-padtraces), &
                  4*img%z%n*(img%xm%n-2*(tapertr+padtraces)))
       deallocate(deltaI,scatS,recv,source,vel,vref,deltaS)
    else
       call srite("out",R_image(:,:,tapertr+padtraces+1:img%xm%n-tapertr-padtraces), &
                  4*img%z%n*img%sh%n*(img%xm%n-2*(tapertr+padtraces)))
       deallocate(deltaI,R_image,recv,source,vel,vref,deltaS)
    end if
    close(2)
 end if
 call sep_end_prog()
end program OWwemva_opers
       
