program OWplane_areal

use sep 
use OW_type
use OW_exec_time

implicit none

real						:: start, finish, start1, finish1, o3,a
integer						:: ixs,ino,ni,i,st,idip,ih,iz
complex, allocatable				:: recv(:,:),source(:,:),source1(:,:)
complex                                                     :: phase
type (axis) ,public, save				:: ang, h, w, xd
type (dat_grid)	 ,public, save			:: rec, sou

!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
 call sep_init()
 call sep_begin_prog()
 start=0.;finish=0.
 call owexec_time(start)

 call from_param("ango", ang%o, -30.); call from_param("angd", ang%d, 0.5); call from_param("angn", ang%n, int(2*abs(ang%o)/ang%d)+1 )
 call from_param("v0",v0,1500.)
 call from_aux("rec","n1",rec%h%n);	call from_aux("rec","d1",rec%h%d);call from_aux("rec","o1",rec%h%o)
 call from_aux("rec","n3",rec%w%n);	call from_aux("rec","d3",rec%w%d);call from_aux("rec","o3",rec%w%o)
 call from_aux("rec","n4",rec%xd%n);call from_aux("rec","d4",rec%xd%d);call from_aux("rec","o4",rec%xd%o)  

 call from_aux("sou","n3",sou%w%n);	call from_aux("sou","d3",sou%w%d);	call from_aux("sou","o3",sou%w%o)

 if (sou%w%n .ne. rec%w%n) then
    call seperr("# source freq. .ne. # receiver freq.")
 end if
 if (sou%w%d .ne. rec%w%d) then
    call seperr("source freq.interval .ne. receiver freq.interval")
 end if
 if (sou%w%o .ne. rec%w%o) then
    call seperr("source freq.origin .ne. receiver freq.origin")
 end if
!--------------------------------------------------
! Frequency vector
! no zero frequency component
!
rec%w%o   = 2*pi * rec%w%o
rec%w%d   = 2*pi * rec%w%d
allocate(rec%w%all(rec%w%n))
do iwb = 1,rec%w%n
   rec%w%all(iwb) = rec%w%o + (iwb - 1) * rec%w%d
end do

 if (allocated(source1)) deallocate(source1); allocate(source1(sou%w%n))
 source1=0;
 call sreed("sou",source1,8*sou%w%n)

 start1=0.;finish1=0.;

 call owexec_time(start1)

 do idip=1,rec%xd%n
    start=0.;finish=0. 
    call owexec_time(start)
    if (allocated(recv))   deallocate(recv);   allocate(recv(rec%h%n,rec%w%n))
    if (allocated(source)) deallocate(source); allocate(source(rec%h%n,sou%w%n))
    source=0;recv=0
    call sreed("rec",recv,8*rec%h%n*rec%w%n)
    phase=
    call owexec_time(finish)

          write(3,111) "EXTRAPOLATED WAVEFIELDS = ",idip," OUT OF ",rec%xd%n," IN ",(finish-start)/60," min"
       end do

       call owexec_time(finish1)
       write(3,112) "COMPUTED BACKGROUND IMAGE IN ",(finish1-start1)/60," min"

       call srite("bkgimg",B_image,4*img%z%n*nsecdim*img%xm%n)

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

 if (allocated(source)) deallocate(source)
 if (allocated(source1)) deallocate(source1)
 if (allocated(recv)) deallocate(recv)

 call sep_end_prog()

end program 
