pure subroutine thermal_driving_impl(t_far, s_far, p_top, active, eos, buf)
!! `T_far − eos_freezing_point(eos, S_far, p_top)`. The liquidus
!! comes off the SAME `eos_t` handle the solve used
!! (`&ocean_eos_nml tfreeze_set`), never a local copy of the
!! coefficients — the two sets differ by ~0.03 degC, which is
!! enough to flip the sign of this field.
! assumed-shape-ok: diag fill — fires once per output frame (cadence-bounded).
use rdb_eos, only: eos_t, eos_freezing_point
real(wp), intent(in) :: t_far(:, :), s_far(:, :) ! assumed-shape-ok: diag fill — cadence-bounded
real(wp), intent(in) :: p_top(:, :), active(:, :) ! assumed-shape-ok: diag fill — cadence-bounded
type(eos_t), intent(in) :: eos
real(wp), intent(inout) :: buf(:, :, :) ! assumed-shape-ok: diag fill — cadence-bounded
integer :: i, j, nx, ny
real(wp) :: qnan
nx = min(size(buf, 1), size(t_far, 1), size(p_top, 1), size(active, 1))
ny = min(size(buf, 2), size(t_far, 2), size(p_top, 2), size(active, 2))
qnan = ieee_value(0.0_wp, ieee_quiet_nan)
do concurrent(j=1:ny, i=1:nx)
if (active(i, j) > 0.5_wp) then
buf(i, j, 1) = t_far(i, j) - eos_freezing_point(eos, s_far(i, j), p_top(i, j))
else
buf(i, j, 1) = qnan
end if
end do
end subroutine thermal_driving_impl