module OW_wvfldIO1

use sep 
use OW_type
use OW_parms
use sep3d_struct_mod

implicit none

contains 

subroutine owwvfld_io(GG,iw,ixs,ireal)

complex, allocatable			:: disk(:,:,:)
complex					:: GG(:)
integer					:: iw,ixs,ireal
type(sep3d)				:: aximg

allocate(disk(sou%h%n,sou%w%n,nreal)); disk=0.

if (ixs.eq.1.and.iw.eq.1) then
   call init_sep3d(aximg, "OUTPUT","COMPLEX", "REGULAR", ctag="wvfld")
   call axis_allocate(aximg,4)
   aximg%o(1)=sou%h%o; aximg%n(1)=sou%h%n; aximg%d(1)=sou%h%d
   aximg%o(2)=1; aximg%n(2)=1; aximg%d(2)=0
   aximg%o(3)=sou%w%o/(2*pi); aximg%n(3)=sou%w%n; aximg%d(3)=sou%w%d/(2*pi)
   aximg%o(4)=1; aximg%n(4)=nreal; aximg%d(4)=1
   call sep3d_set_sep3d(aximg)
   call sep3d_write_description("wvfld",aximg)
   disk(:,iw,ireal)=GG
   call srite("wvfld",disk,8*sou%h%n*sou%w%n)
   call sep3df_delete(aximg,"wvfld")
else
   call init_sep3d("wvfld",aximg,"SCRATCH")
   call sreed("wvfld",disk,8*sou%h%n*sou%w%n)
   disk(:,iw,ireal)=disk(:,iw,ireal)+GG
   call sep3df_delete(aximg,"wvfld")
   call init_sep3d(aximg, "OUTPUT","COMPLEX", "REGULAR", ctag="wvfld")
   call axis_allocate(aximg,3)
   aximg%o(1)=sou%h%o; aximg%n(1)=sou%h%n; aximg%d(1)=sou%h%d
   aximg%o(2)=1; aximg%n(2)=1; aximg%d(2)=0
   aximg%o(3)=sou%w%o/(2*pi); aximg%n(3)=sou%w%n; aximg%d(3)=sou%w%d/(2*pi)
   aximg%o(4)=1; aximg%n(4)=nreal; aximg%d(4)=1
   call sep3d_set_sep3d(aximg)
   call sep3d_write_description("wvfld",aximg)
   call srite("wvfld",disk,8*sou%h%n*sou%w%n)
   call sep3df_delete(aximg,"wvfld")
end if
deallocate(disk)
end subroutine
end module
