program OWmig3d_areal

use sep 
use OW_type
use OW_grabvel2_areal
use OW_exec_time
use OW_parms_areal
use OW_taper_areal
use OW_wei_areal
use lloyd_vel

implicit none

character*80                                                :: listagem
real						:: start, finish, start1, finish1, o3,a
integer						:: ixs,ino,ni,i,st,idip,ih,iz,ireal
real, allocatable					:: velint(:),taper(:,:)
real, allocatable					:: vel(:),vref(:,:,:)


!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
 call sep_init()
 call sep_begin_prog()

 call from_param("list",listagem,"listagem")
 open(unit=3,file=listagem)
 start=0.;finish=0.
 call owexec_time(start)
 call owparam()
 call auxclose("bkgimg")
 call auxinout("bkgimg")
!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
!
! Memory allocation
!
allocate(taper(kxm%n,kym%n))
allocate(velint(v%z%n*v%xm%n*v%ym%n),vel(img%z%n*kxm%n*kym%n))
!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
!
! Set up the taper
!
 call owtaper(taper)
 call image_io("bkgimg","init")

!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
!
! Read velocities
!

 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,img%z%n))
      call lloyd_vel1(vel,vref)
    end if
 end if

       start1=0.;finish1=0.;
       call owexec_time(start1)

!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
!
! Computing bkg.image
!

       call sseek("rec",0,0)
       call sseek("sou",0,0)
       do ireal=1,nreal
       do idip=1,rec%xd%n
          call owexec_time(start)
          if (lloyd) then
             call owwei_init(idip,ireal)
             call owwei(taper,vel,vref)
          else
             call owwei_init(idip,ireal)
             call owwei(taper,vel)
          end if
          call owexec_time(finish)
          write(0,111) "Shot = ",idip," OUT OF ",rec%xd%n," IN ",(finish-start)/60," min"
       end do
       end do
       call owexec_time(finish1)
       write(3,112) "COMPUTED BACKGROUND IMAGE IN ",(finish1-start1)/60," min"

 111 format(a7,i4,a8,i4,a4,f6.2,a4)
 112 format(a29,f10.2,a4)
 close(3)

 if (allocated(taper)) deallocate(taper)
 if (allocated(vref)) deallocate(vref)
 if (allocated(vel)) deallocate(vel)

 call sep_end_prog()

end program OWmig3d_areal
