module base_grad1d_mod
  use  base_adjnul_mod
  implicit none


  integer, private :: n1

contains

  integer function base_grad1d_init(n1_in) result(stat)
    integer :: n1_in
    n1 = n1_in
    stat = 0
  end function

  integer function base_grad1d_oper(adj, add, modl, data) result(stat)
    logical            :: adj, add
    real, dimension(:) :: modl, data
    call base_adjnul_real(adj, add, modl, data)
    call base_grad1d(adj, modl, data)
    stat = 0
  end function

  integer function base_grad1d_clos() result(stat)
    n1 = 0
    stat = 0
  end function

  subroutine base_grad1d(adj, modl, data)
    logical             :: adj
    real, dimension(n1) :: modl, data
    integer             :: i

    do i=1, size(modl)-1
       if (adj) then
          modl(i+1) = modl(i+1) + data(i)
          modl(i  ) = modl(i  ) - data(i)
       else
          data(i)   = data(i) + modl(i+1) - modl(i)
       end if
    end do
    return
  end subroutine
end module
