module OW_wvfldIO2

use sep 
use OW_type
use OW_parms
use sep3d_struct_mod

implicit none

contains 

subroutine owwvfld_io(GG,iw,ixs)

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

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

if (ixs.eq.1.and.iw.eq.1) then
   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(3)=sou%w%o/(2*pi); aximg%n(3)=sou%w%n-subf; aximg%d(3)=sou%w%d/(2*pi)
   aximg%o(2)=1; aximg%n(2)=1; aximg%d(2)=0
   call sep3d_set_sep3d(aximg)
   call sep3d_write_description("wvfld",aximg)
   disk(:,iw)=GG
   call srite("wvfld",disk,8*sou%h%n*(sou%w%n-subf))
   call sep3df_delete(aximg,"wvfld")
else
   call init_sep3d("wvfld",aximg,"SCRATCH")
   call sreed("wvfld",disk,8*sou%h%n*(sou%w%n-subf))
   disk(:,iw)=disk(:,iw)+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(3)=sou%w%o/(2*pi); aximg%n(3)=sou%w%n-subf; aximg%d(3)=sou%w%d/(2*pi)
   aximg%o(2)=1; aximg%n(2)=1; aximg%d(2)=0
   call sep3d_set_sep3d(aximg)
   call sep3d_write_description("wvfld",aximg)
   call srite("wvfld",disk,8*sou%h%n*(sou%w%n-subf))
   call sep3df_delete(aximg,"wvfld")
end if
deallocate(disk)
end subroutine
end module
