module mva_borndf_mod
  use  mva_phsscr_mod
  use  mva_cpsfou_mod
  implicit none


contains

  integer function mva_bornsc_init(kx, ky, dx, dy, dz, nx, ny) result(stat)
    real, dimension(:) :: kx, ky
    real    :: dx, dy, dz
    integer :: nx, ny
    stat = mva_phsscr_init(dx, dy, dz, nx, ny)
    !stat = mva_cpsfou_init(kx, ky, dx, dy, dz, nx, ny)
    stat = 0
  end function

  !!.. forward: dS => dU: dU = G (P,S) dS
  !!.. adjoint: dU => dS; dS = G'(P,S) dU
  integer function mva_bornsc_oper(adj, dS, dU, P, S, ws) result(stat)
    logical                 :: adj
    complex, dimension(:,:) :: dS
    complex, dimension(:,:) :: dU
    complex, dimension(:,:) :: P
    real   , dimension(:,:) :: S 
    real                    :: ws

    if (adj) then
       stat = mva_phsscr_oper(dS, P, S, ws)
       !stat = mva_cpsfou_oper(dS, P, S, ws)
       dS = conjg(dS)*dU
    else
       dU = P * dS 
       stat = mva_phsscr_oper(dU, P, S, ws)
       !stat = mva_cpsfou_oper(dU, P, S, ws)
    end if
    stat = 0
  end function
end module
