pure subroutine meke_mass(nx, ny, nz, rho0, have_rho, h_layer, rho_layer, &
i_mass, depth_tot, mass_ws)
!! Column mass `mass = Sum_k rho*max(h,H_VANISHED)` (kg/m^2), its
!! inverse `i_mass` (0 where mass<=0), `depth_tot = Sum_k h` (m), and
!! `mass_ws = mass` (the harmonic-mass input for the lateral flux).
integer, intent(in) :: nx, ny, nz
real(wp), intent(in) :: rho0
logical, intent(in) :: have_rho
real(wp), intent(in) :: h_layer(nx, ny, nz)
real(wp), intent(in) :: rho_layer(nx, ny, nz)
real(wp), intent(out) :: i_mass(nx, ny)
real(wp), intent(out) :: depth_tot(nx, ny)
real(wp), intent(out) :: mass_ws(nx, ny)
integer :: i, j, k
real(wp) :: mass, dsum, hk, rhok
do concurrent(j=1:ny, i=1:nx) local(k, mass, dsum, hk, rhok)
mass = 0.0_wp
dsum = 0.0_wp
do k = 1, nz
hk = max(h_layer(i, j, k), H_VANISHED)
rhok = rho0
if (have_rho) rhok = rho_layer(i, j, k)
mass = mass + rhok*hk
dsum = dsum + h_layer(i, j, k)
end do
depth_tot(i, j) = dsum
mass_ws(i, j) = mass
if (mass > 0.0_wp) then
i_mass(i, j) = 1.0_wp/mass
else
i_mass(i, j) = 0.0_wp
end if
end do
end subroutine meke_mass