module tomo_mask_mod
use sep
use adj_mod
use sep3d_struct_mod
implicit none
real, private, save,  pointer :: mymask(:)
integer, private,save :: ndomain,nrange


contains
logical function load_mask_op(slow_s)
type(sep3d) :: slow_s,wt_s
integer, external :: auxin,sreed
integer :: ierr,n
real,   allocatable :: wts(:)

load_mask_op=.false.
ndomain=product(slow_s%n(1:sep3d_ndims(slow_s)))
if(-1==auxin("model_mask")) then
 allocate(mymask(product(slow_s%n(1:sep3d_ndims(slow_s)))))
 mymask=1
else
 call init_sep3d("model_mask",wt_s,"INPUT")
 allocate(mymask(product(wt_s%n(1:sep3d_ndims(wt_s)))))
 allocate(wts(product(wt_s%n(1:sep3d_ndims(wt_s)))))
! ierr=sreed("model_mask",mymask,product(wt_s%n(1:sep3d_ndims(wt_s)))*4) 
 n=size(mymask)*4
 ierr=sreed("model_mask",wts,n)
 if(4*size(mymask)/=ierr) then
   write(0,*)"trouble loading mask_op"
   return
 end if
 mymask=wts
 deallocate(wts)
end if

nrange=size(mymask)
load_mask_op=.true.
end function

integer function mask_range()
 mask_range=nrange
end function

integer function mask_domain()
 mask_domain=ndomain
end function

integer function mask_op(adj,add,model,data)
logical,intent(in) :: adj,add
real    :: model(:),data(:)

call adjnull(adj,add,model,data)
if(.not. adj) then
  data=data+model*mymask
else 
  model=model+data*mymask
end if
mask_op=0

end function


end module
