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
!=======================================================================
subroutine owvel_io(GG,ixs,rite,mm)

integer,optional                                  :: mm
logical                                           :: rite
real					:: GG(img%xm%n,img%z%n)
integer					:: ixs
type(sep3d)				:: aximg

if (rite) then
   call init_sep3d(aximg, "OUTPUT","FLOAT", "REGULAR", ctag="Velout")
   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)=1.; aximg%n(2)=1.; aximg%d(2)=1.
   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("Velout",aximg)
   call to_history("n4",ixs,"Velout")
   call srite("Velout",GG,4*img%z%n*1*img%xm%n)
   call sep3df_delete(aximg,"Velout")
else
   call init_sep3d("Velout",aximg,"INPUT")
   call sreed_window("Velout",3,(/img%xm%n,1,     img%z%n,      ixs/),&
                                (/img%xm%n,1,     img%z%n,      ixs/),&
                                (/       0,0, 0          ,     mm-1/),&
                                (/       1,1,           1,        1/),&
                                                                4,GG)

!   call sreed("Velout",GG,4*img%z%n*nsecdim*img%xm%n)
   call sep3df_delete(aximg,"Velout")
end if

end subroutine
end module
