module OW_imageIO

use sep 
use OW_type
use OW_parms_areal
use sep3d_struct_mod

implicit none

contains 

subroutine owimage_io(GG,ixs)

real					:: disk(img%xm%n,nsecdim,img%z%n)
real					:: GG(:,:,:)
integer					:: ixs
type(sep3d)				:: aximg

disk=0.

if (ixs.eq.1) then
   call init_sep3d(aximg, "OUTPUT","FLOAT", "REGULAR", ctag="bkgimg")
   call axis_allocate(aximg,3)
   aximg%o(1)=img%xm%o; aximg%n(1)=img%xm%n; aximg%d(1)=img%xm%d
   aximg%o(2)=osecdim; aximg%n(2)=nsecdim; aximg%d(2)=dsecdim
   aximg%o(3)=img%z%o; aximg%n(3)=img%z%n; aximg%d(3)=img%z%d
   call sep3d_set_sep3d(aximg)
   call sep3d_write_description("bkgimg",aximg)
   disk=GG
   call srite("bkgimg",disk,4*img%z%n*nsecdim*img%xm%n)
   call sep3df_delete(aximg,"bkgimg")
else
   call init_sep3d("bkgimg",aximg,"SCRATCH")
   call sreed("bkgimg",disk,4*img%z%n*nsecdim*img%xm%n)
   disk=disk+GG
   call sep3df_delete(aximg,"bkgimg")
   call init_sep3d(aximg, "OUTPUT","FLOAT", "REGULAR", ctag="bkgimg")
   call axis_allocate(aximg,3)
   aximg%o(1)=img%xm%o; aximg%n(1)=img%xm%n; aximg%d(1)=img%xm%d
   aximg%o(2)=osecdim; aximg%n(2)=nsecdim; aximg%d(2)=dsecdim
   aximg%o(3)=img%z%o; aximg%n(3)=img%z%n; aximg%d(3)=img%z%d
   call sep3d_set_sep3d(aximg)
   call sep3d_write_description("bkgimg",aximg)
   call srite("bkgimg",disk,4*img%z%n*nsecdim*img%xm%n)
   call sep3df_delete(aximg,"bkgimg")
end if

end subroutine
end module
