subroutine compute_linear_map(src, dst, matrix, rc)
real(REAL32), intent(in) :: src(:)
real(REAL32), intent(in) :: dst(:)
type(SparseMatrix_sp), intent(out) :: matrix
! real(REAL32), allocatable, intent(out) :: matrix(:, :)
integer, optional, intent(out) :: rc
real(REAL32) :: val, weight(2)
integer :: ndx, status
type(IndexValuePair) :: pair(2)
#ifndef NDEBUG
_ASSERT(maxval(dst) <= maxval(src), "maxval(dst) > maxval(src)")
_ASSERT(minval(dst) >= minval(src), "minval(dst) < minval(src)")
_ASSERT(is_decreasing(src), "src array is not decreasing")
#endif
! allocate(matrix(size(dst), size(src)), source=0., _STAT)
! Expected 2 non zero entries in each row
matrix = SparseMatrix_sp(size(dst), size(src), 2*size(dst))
do ndx = 1, size(dst)
val = dst(ndx)
call find_bracket_(val, src, pair)
call compute_weights_(val, pair%value_, weight)
if (pair(1) == pair(2)) then
! matrix(ndx, pair(1)%index) = weight(1)
call add_row(matrix, ndx, pair(1)%index, [weight(1)])
else
! matrix(ndx, pair(1)%index) = weight(1)
! matrix(ndx, pair(2)%index) = weight(2)
call add_row(matrix, ndx, pair(1)%index, [weight(1), weight(2)])
end if
end do
_RETURN(_SUCCESS)
end subroutine compute_linear_map