pure subroutine meke_baro_transport(nx, ny, nz, rho0, have_rho, &
mflux_x, mflux_y, rho_layer, &
baro_hu, baro_hv)
!! Depth-integrated, mass-weighted barotropic transport through each
!! C-grid face: `baroHu(I,j) = Sum_k rho_face * mass_flux_x_layer`,
!! where `mass_flux_*_layer` is the per-layer VOLUME transport (m^3/s,
!! = u*h_face*dy) and `rho_face` the two-cell average density. The
!! result is a MASS transport (kg/s) so the advective divergence pairs
!! exactly with `IareaT*I_mass` (1/(area*mass)) ⇒ Sum E*area*mass is
!! conserved. Array-edge faces carry zero transport (closed domain).
integer, intent(in) :: nx, ny, nz
real(wp), intent(in) :: rho0
logical, intent(in) :: have_rho
real(wp), intent(in) :: mflux_x(nx + 1, ny, nz)
real(wp), intent(in) :: mflux_y(nx, ny + 1, nz)
real(wp), intent(in) :: rho_layer(nx, ny, nz)
real(wp), intent(out) :: baro_hu(nx + 1, ny)
real(wp), intent(out) :: baro_hv(nx, ny + 1)
integer :: i, j, k
real(wp) :: tsum, rho_face
! u-faces: interior I=2..nx between cells I-1 and I.
do concurrent(j=1:ny, i=1:nx + 1) local(k, tsum, rho_face)
tsum = 0.0_wp
if (i >= 2 .and. i <= nx) then
do k = 1, nz
rho_face = rho0
if (have_rho) rho_face = 0.5_wp*(rho_layer(i - 1, j, k) + rho_layer(i, j, k))
tsum = tsum + rho_face*mflux_x(i, j, k)
end do
end if
baro_hu(i, j) = tsum
end do
! v-faces: interior J=2..ny between cells J-1 and J.
do concurrent(j=1:ny + 1, i=1:nx) local(k, tsum, rho_face)
tsum = 0.0_wp
if (j >= 2 .and. j <= ny) then
do k = 1, nz
rho_face = rho0
if (have_rho) rho_face = 0.5_wp*(rho_layer(i, j - 1, k) + rho_layer(i, j, k))
tsum = tsum + rho_face*mflux_y(i, j, k)
end do
end if
baro_hv(i, j) = tsum
end do
end subroutine meke_baro_transport