program OWconv_areal
use sep 
use OW_type
use OW_exec_time
implicit none

real						:: oxplane
integer						:: iang,iw,idip,ih,iz,nxplane,iowrk,nshp,nsht
complex, allocatable				:: recv(:,:,:),source1(:)
complex, allocatable				:: recv_pl(:,:,:),source_pl(:,:,:)
type (axis) 					:: ang,h,w,xd
type (dat_grid)	 				:: rec,sou

!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
 call sep_init()
 call sep_begin_prog()
 call from_param("nsht", nsht, 1)
 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)

 nxplane=rec%h%n+int((rec%xd%n-1)*(rec%xd%d/rec%h%d))
 oxplane=rec%h%o+rec%xd%o
 
 ang%n=int(rec%xd%n/nsht)
 ang%d=1.
 ang%o=1.

 call to_history("n1", nxplane,"rec_pl");call to_history("d1", rec%h%d,"rec_pl");call to_history("o1", oxplane,"rec_pl")
 call to_history("n2",       1,"rec_pl");call to_history("d2",      1.,"rec_pl");call to_history("o2",      0.,"rec_pl")
 call to_history("n3", rec%w%n,"rec_pl");call to_history("d3", rec%w%d,"rec_pl");call to_history("o3", rec%w%o,"rec_pl")
 call to_history("n4",   ang%n,"rec_pl");call to_history("d4",   ang%d,"rec_pl");call to_history("o4",   ang%o,"rec_pl")  
 call to_history("esize",8,"rec_pl")

 call to_history("n1", nxplane,"sou_pl");call to_history("d1", rec%h%d,"sou_pl");call to_history("o1", oxplane,"sou_pl")
 call to_history("n2",       1,"sou_pl");call to_history("d2",      1.,"sou_pl");call to_history("o2",      0.,"sou_pl")
 call to_history("n3", rec%w%n,"sou_pl");call to_history("d3", rec%w%d,"sou_pl");call to_history("o3", rec%w%o,"sou_pl")
 call to_history("n4",   ang%n,"sou_pl");call to_history("d4",   ang%d,"sou_pl");call to_history("o4",   ang%o,"sou_pl")  
 call to_history("esize",8,"sou_pl")

 if (sou%w%n .ne. rec%w%n) call seperr("# source freq. .ne. # receiver freq.")
 if (sou%w%d .ne. rec%w%d) call seperr("source freq.interval .ne. receiver freq.interval")
 if (sou%w%o .ne. rec%w%o) call seperr("source freq.origin .ne. receiver freq.origin")

 allocate(source1(sou%w%n)); source1=0; call sreed("sou",source1,8*sou%w%n)
 allocate(recv(rec%h%n,rec%w%n,rec%xd%n)); call sreed("rec",recv,8*rec%h%n*rec%w%n*rec%xd%n)
 allocate(source_pl(nxplane,sou%w%n,ang%n));source_pl=0
 allocate(recv_pl(nxplane,sou%w%n,ang%n));recv_pl=0
 
 do idip=1,nsht
    do iang=1,ang%n
       nshp=(iang-1)+1+(idip-1)*ang%n
       do iw = 1,rec%w%n
          source_pl(nshp,iw,iang)=source1(iw)
          recv_pl(nshp:rec%h%n+nshp-1,iw,iang)=recv(:,iw,nshp)
       end do
    end do
 end do

 call srite("sou_pl",source_pl,8*nxplane*sou%w%n*ang%n)
 call srite("rec_pl",recv_pl,8*nxplane*sou%w%n*ang%n)
 deallocate(recv_pl,recv,source_pl,source1)
 call sep_end_prog()

end program 
