subroutine sample_zhao_state_at_z(p, branch, phi0_v, phi_m_v, n_swe_inf_m3, z_m, state)
type(zhao_params_type), intent(in) :: p
character(len=1), intent(in) :: branch
real(dp), intent(in) :: phi0_v, phi_m_v, n_swe_inf_m3, z_m
type(zhao_local_state_type), intent(out) :: state
real(dp) :: phi0_hat, phi_m_hat, phi_hat, z_m_hat
character(len=16) :: side
phi0_hat = phi0_v/p%t_phe_ev
phi_m_hat = phi_m_v/p%t_phe_ev
if (z_m <= 0.0d0) then
phi_hat = phi0_hat
if (branch == 'A') then
side = 'lower'
else
side = 'monotonic'
end if
z_m_hat = 0.0d0
else
select case (branch)
case ('A')
call sample_type_a_phi_hat_at_z(p, phi0_hat, phi_m_hat, n_swe_inf_m3/p%n_phe_ref_m3, z_m, phi_hat, side, z_m_hat)
case ('B', 'C')
call sample_monotonic_phi_hat_at_z( &
p, branch, phi0_hat, phi_m_hat, n_swe_inf_m3/p%n_phe_ref_m3, z_m, phi_hat, side, z_m_hat &
)
case default
error stop 'Unknown Zhao branch in local state reconstruction.'
end select
end if
call evaluate_zhao_state_from_phi_hat( &
p, branch, side, phi_hat, phi0_hat, phi_m_hat, n_swe_inf_m3, z_m_hat*p%lambda_d_phe_ref_m, state &
)
end subroutine sample_zhao_state_at_z