pure subroutine massless_merge_fields(h, kc, nz, nzc, &
u, v, t, s, uc, vc, tc, sc)
!! Thickness-weighted merged means for u, v, T, S — the solver receives
!! MEANS, not integrals, and must not re-divide. Accumulation runs in the
!! same surface-down `k` order as `massless_build_maps` so the
!! column-integral conservation holds to round-off.
!$acc routine seq
integer, intent(in) :: nz, nzc
real(wp), intent(in) :: h(NZL)
integer, intent(in) :: kc(NZLI)
real(wp), intent(in) :: u(NZL), v(NZL), t(NZL), s(NZL)
real(wp), intent(out) :: uc(NZL), vc(NZL), tc(NZL), sc(NZL)
integer :: k, kk
real(wp) :: hc_acc(NZL)
real(wp) :: denom
do kk = 1, nzc
hc_acc(kk) = 0.0_wp
uc(kk) = 0.0_wp
vc(kk) = 0.0_wp
tc(kk) = 0.0_wp
sc(kk) = 0.0_wp
end do
do k = 1, nz
kk = kc(k)
hc_acc(kk) = hc_acc(kk) + h(k)
uc(kk) = uc(kk) + u(k)*h(k)
vc(kk) = vc(kk) + v(k)*h(k)
tc(kk) = tc(kk) + t(k)*h(k)
sc(kk) = sc(kk) + s(k)*h(k)
end do
! Finalise means. hc_acc(kk) > 0 for every kk (a cluster opens only
! on a massive layer or holds an absorbed leading run + its first
! massive layer), so H_DIV_EPS is armour, not a clamp.
do kk = 1, nzc
denom = max(hc_acc(kk), H_DIV_EPS)
uc(kk) = uc(kk)/denom
vc(kk) = vc(kk)/denom
tc(kk) = tc(kk)/denom
sc(kk) = sc(kk)/denom
end do
end subroutine massless_merge_fields