module differential_off_mod 

use sep
use adj_mod

implicit none

integer, private     :: nh 
real   , private     :: dh,oh,eps 

contains

! ---------------------------------------------------------------------------
subroutine differential_off_init( nh_in, dh_in , oh_in,eps_in)
         integer ::nh_in
         real    ::dh_in,oh_in,eps_in
 
         nh = nh_in
         dh = dh_in
         oh = oh_in
         eps= eps_in
end subroutine
! ---------------------------------------------------------------------------

! ---------------------------------------------------------------------------
integer function differential_off_exec( adj, add, model, data)
		logical            :: adj, add 
		real, dimension(:) :: model, data 
        integer            :: n, i,ih,ratio

call adjnull( adj, add, model, data)

n=size(data)

ratio=int(n/nh)
if( adj) then
do i=1,ratio
  do ih=1,nh
    model((i-1)*nh+ih) = model((i-1)*nh+ih) + eps*data((i-1)*nh+ih)*abs((nh-1)/2-(ih-1))
  enddo
enddo
else
do i=1,ratio
  do ih=1,nh
    data((i-1)*nh+ih) = data((i-1)*nh+ih) + eps*model((i-1)*nh+ih)*abs((nh-1)/2-(ih-1))
  enddo
enddo
end if

differential_off_exec = 0

end function differential_off_exec
! ---------------------------------------------------------------------------
integer function differential_off2_exec( adj, add, model, data)
		logical            :: adj, add 
		real, dimension(:) :: model, data 
        integer            :: n, i,ih,ratio

call adjnull( adj, add, model, data)

n=size(data)

ratio=int(n/nh)
if( adj) then
do i=1,ratio
  do ih=1,nh
    model((i-1)*nh+ih) = model((i-1)*nh+ih) + eps*data((i-1)*nh+ih)*(abs((nh-1)/2-(ih-1)))**2
  enddo
enddo
else
do i=1,ratio
  do ih=1,nh
    data((i-1)*nh+ih) = data((i-1)*nh+ih) + eps*model((i-1)*nh+ih)*(abs((nh-1)/2-(ih-1)))**2
  enddo
enddo
end if

differential_off2_exec = 0

end function differential_off2_exec
end module
