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

contains

logical function load_wt_diag_op(data)
real           :: data(:)
type(sep3d) :: wt_s
integer, external :: auxin,sreed

load_wt_diag_op=.false.
ndomain=size(data)
if(-1==auxin("wts")) then
 allocate(wts(ndomain))
 wts=1
else
 call init_sep3d("wts",wt_s,"INPUT")
 allocate(wts(product(wt_s%n(1:sep3d_ndims(wt_s)))))
 if(4*size(wts)/=sreed("wts",wts,size(wts)*4)) then
   write(0,*)"trouble loading wt_op"
   return
 end if
 wts=wts/(sum(wts)/size(wts))
end if
!wts(229)=1000.
nrange=size(wts)
load_wt_diag_op=.true.
end function

integer function wt_diag_range()
 wt_diag_range=nrange
end function

integer function wt_diag_domain()
 wt_diag_domain=ndomain
end function

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

call adjnull(adj,add,model,data)
n=size(wts)
if(.not. adj) then
  data(1:n)=data+model(1:n)*wts(1:n)
else 
  model(1:n)=model(1:n)+data(1:n)*wts(1:n)
end if
wt_diag_op=0

end function


end module
