% MATLAB to read & process NOAA BUOY realtime data from:
%ftp://www.ndbc.noaa.gov/data/l_stdmet/44011.txt
%or
%ftp://www.ndbc.noaa.gov/data/realtime/44011.txt
%where the later is really recent

fid=fopen('/data5/jmanning/ship/oc296/44011now.txt','r');

%skip header
fgetl(fid);

%read all the data
d=fscanf(fid,'%f',inf);
dd=reshape(d,17,length(d)/17);
dd=dd';

% find good wind
idw=find(dd(:,6)~=99.0);

% generate a yearday vector for good wind records

yr=dd(1,1);

for k=1:length(idw);
  mth(k)=dd(idw(k),2);
  day(k)=dd(idw(k),3);
  hr(k)=dd(idw(k),4);
  yd(k)=julian(mth(k),day(k),yr,hr(k)*100);
  wdir=dd(idw(k),5);
  wspd=dd(idw(k),6);
  [u(k),v(k)]=sd2uv(wspd,wdir);
end;

% put wind in oceanographic convention
u=-1*u;v=-1*v;

% calculate stress
% (note: the "hgt=5" for buoy11 and hgt=13.2 for buoy8)
hgt=5;
disp('Hardcoded to sensor hgt=5m');
for kk=1:length(u);
  [taux(kk),tauy(kk)]=wstr(u(kk),v(kk),hgt);
end;

% sort by yearday
[nyd,nid]=sort(yd);

db=[yd(nid); taux(nid); tauy(nid); u(nid); v(nid); day(nid); hr(nid)];
db=db';

save /data5/jmanning/ship/oc296/wind.dat db -ascii



