module OW_imageIO

use sep 
use OW_type
use OW_parms
use sep3d_struct_mod

implicit none

contains 

subroutine owimage_io(GG,ni,ixs)

real, allocatable				:: disk(:,:,:)
real					:: GG(:,:,:)
integer					:: ni,ixs
type(sep3d)				:: aximg

allocate(disk(img%xm%n,img%sh%n,img%z%n)); disk=0.

if (ixs.eq.1) then
   call init_sep3d(aximg, "OUTPUT","FLOAT", "REGULAR", ctag="image")
   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)=img%sh%o; aximg%n(2)=img%sh%n; aximg%d(2)=img%sh%d
   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("image",aximg)
   disk(ni:ni+rec%h%n-1,:,:)=GG
   call srite("image",disk,4*img%z%n*img%sh%n*img%xm%n)
   call sep3df_delete(aximg,"image")
else
   call init_sep3d("image",aximg,"SCRATCH")
   call sreed("image",disk,4*img%z%n*img%sh%n*img%xm%n)
   disk(ni:ni+rec%h%n-1,:,:)=disk(ni:ni+rec%h%n-1,:,:)+GG
   call sep3df_delete(aximg,"image")
   call init_sep3d(aximg, "OUTPUT","FLOAT", "REGULAR", ctag="image")
   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)=img%sh%o; aximg%n(2)=img%sh%n; aximg%d(2)=img%sh%d
   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("image",aximg)
   call srite("image",disk,4*img%z%n*img%sh%n*img%xm%n)
   call sep3df_delete(aximg,"image")
end if
deallocate(disk)
end subroutine
end module
