%get_vos.m
% read the vos autoimet file all_ai.txt as written 
%  1         2     3        4       5       6       7       8       9      10      11    12    13     14
% Date       time  bpr      hrh     atmp    LWR   prlev   SST      SWR     wE      wN    spd   hdg    vane
% 2008/07/17 23:01 1016.25  76.644  28.326  405.5   13.82  -5.000   31.8   -1.66   -1.64 11.69 146.35  207.6
% 2008/07/17 23:03 1016.22  76.002  28.307  404.5   13.82  -5.000   29.2   -1.38   -1.86 11.07 147.64  227.6
%
% ship’s speed and heading from the AutoIMET GPS, in knots and degrees T.  based on differencing GPS
% fixes, so heading bad if speed is low. A couple knots, appear to work, however.
%
% The last number is the wind vane reading, i.e., the wind direction (relative to
% the ship, it is a wind sensor status indicator. The directional encoder is the
% most vulnerable piece of the wind sensor; a solid (unchanging) number, esp “360”,
% indicates a broken wind sensor.
%
% Wind “E and “N” refer to the wind components relative to the ship, in m/s.
%Positive “E” describes air moving from port to starboard, and positive “N”
% describes air moving from the stern to the bow.
%Conversly, with the ship %moving at 10 knots through calm condition,
%the “E” component will be small and the “N” component will be -5m/s.
%
if exist('shipdir')~=1, shipdir=[]; end;
infile=[shipdir 'all_ai.txt'];
fid=fopen(infile,'rt');
a=textscan(fid,...
    '%.0f/%.0f/%.0f %.0f:%.0f %f %f %f %f %f %f %f %f %f %f %f %f',...
    'treatAsEmpty',{'Na','N'});
% once we read the data, close the file again 
fclose(fid);

%dstr=[a{1}(:) a{2}(:) ':00' ];
fns = {'year', 'month', 'day','hour', 'minute',...
    'bpr','hrh','atmp','lwr','precip','sst',...
    'swr', 'wndu','wndv','shspd','hdg','vane'};
vos=cell2struct(a,fns,2);
% get mday
vos.second=vos.minute(:) * 0;
vos.mday=datenum(vos.year,vos.month,vos.day,vos.hour,vos.minute,vos.second);
vos.yday=vos.mday-datenum(2011,1,0);

npts=length(vos.day);
%fprintf('%d new records  ', npts);
myrmfld='sst';
vos=rmfield(vos,myrmfld);
save vos.mat vos

myrmflds={ 'hdg' 'vane'  'year' 'month' 'day' 'hour' 'minute' };
vos=rmfield(vos,myrmflds);
warning off all
vos=get_str_avg(vos);
[vos.wnds, vos.wndd] = vect2md(vos.wndu,vos.wndv);
ij = find(vos.wndd > 360) ;
if ij
    vos.wndd(ij) = vos.wndd(ij) - 360;
end
% maybe change the sense of the wind? 
% vos.wndd=vos.wndd-180; 
warning on all
save vos1hr.mat vos



