module base_weight_mod
  implicit none

  real, dimension(:), pointer, private :: wght

contains

  integer function base_weight_init(wght_in) result(stat)
    real, dimension(:), target :: wght_in
    wght => wght_in
    stat = 0 
  end function

  integer function base_weight_oper(adj, add, modl, data) result(stat)
    logical            :: adj, add
    real, dimension(:) :: modl
    real, dimension(:) :: data

    if (.not.add) then
       if (adj) then
          modl = 0.
       else
          data = 0.
       end if 
    end if

    if (adj) then
        modl = modl + wght * data
    else
        data = data + wght * modl
    end if
    stat = 0
  end function

  integer function base_weight_clos() result(stat)
    nullify(wght)
    stat = 0
  end function
end module
