pure subroutine ice_gather_flux_x_layer_impl(uh, val4, layer, tr_flux_x_work, ncat, nk, nx, ny)
!! Layer-indexed twin of `ice_gather_flux_x_impl` for
!! `enth_ice`/`sal_ice`/`enth_snow` (shape `(nx,ny,ncat,nk)`).
integer, intent(in) :: layer, ncat, nk, nx, ny
real(wp), intent(in) :: uh(nx + 1, ny, ncat)
real(wp), intent(in) :: val4(nx, ny, ncat, nk)
real(wp), intent(out) :: tr_flux_x_work(nx + 1, ny, ncat)
integer :: i, j, c
do concurrent(j=1:ny, i=1:nx + 1, c=1:ncat)
if (i == 1) then
tr_flux_x_work(i, j, c) = val4(1, j, c, layer)
else if (i == nx + 1) then
tr_flux_x_work(i, j, c) = val4(nx, j, c, layer)
else if (uh(i, j, c) >= 0.0_wp) then
tr_flux_x_work(i, j, c) = val4(i - 1, j, c, layer)
else
tr_flux_x_work(i, j, c) = val4(i, j, c, layer)
end if
end do
end subroutine ice_gather_flux_x_layer_impl