% test true wind calc
% read an scs and son file first

jd = scs(:,1);
wspd = scs(:,13);
wdir = scs(:,14);
sog = scs(:,5);
cog = scs(:,4);
hed = scs(:,12);

jd2 = [jd;jd(end)+1/86400];
son1 = interval_avg(son(:,1), son(:,2:5), jd2');

U = son1(:,2);
V = son1(:,3);

rdcon = pi/180;
                                            
[SOGn, SOGe] = pol2cart(cog*rdcon, sog);    % ship velocity components
                                            % dir is 'toward' N & E
                                            
[hedn, hede] = pol2cart(hed*rdcon, 1);      % heading components

Un_rel = U.*hedn + V.*hede;                 % earth ref rel wind components
Ue_rel = U.*hede - V.*hedn;

Un = Un_rel + SOGn;                         % true wind components
Ue = Ue_rel + SOGe;

wspd2 = sqrt(Un.*Un + Ue.*Ue);
wdir2 = mod(atan2(-Ue,-Un)/rdcon + 360, 360);


figure; plot(jd, wspd,'b',jd,wspd2,'r');
figure; plot(jd,wdir,'b',jd,wdir2,'r'); ylim([0 360]);


