function [spd,dir,Un,Uw] = uv_rel_to_sd_true(U,V,hed,cog,sog)
%{
    Computes true wind speed and direction from ship-relative wind
    components U and V, where U is defined as positive for wind blowing
    toward the bow and V is positive for wind blowing toward port.
    This is a right-handed convention and is the normal format for sonic
    anemometer wind components in all NOAA/PSD flux scripts.

    Ship heading (0-360 deg true) and speed/course are used to compute
    the true wind.

    dir format is the direction wind is coming FROM, 0-360 deg.

    True wind components in the earth frame, Un/Uw, are also returned.

    Jan 2016, BWB
%}

% pol2cart follows left-handed convention so components are N/E!
[hed_n, hed_e] = pol2cart(hed*pi/180, 1);   % N/E heading components
[sog_n, sog_e] = pol2cart(cog*pi/180, sog); % N/E ship speed components

% true wind components in earth frame: do CCW rotation & sum with ship speed
Un = U.*hed_n + V.*hed_e + sog_n;
Uw = -U.*hed_e + V.*hed_n - sog_e;	% minus sog_e since it is positive to East!

[spd,dir] = uv_to_sd(Un,Uw);

end
