function mavs_shear(startp,endp)
% mavs_shear  compute vertical shear for MAVS current meter data
% =========================================================================
% mavs_shear  Version 1.0 28-Jul-2005
%
% Usage: 
%   mavs_shear(startp,endp)
%
% Description:
%   This function loads a MAVS data file logged on the SeaHorse profiler
%   and produces estimates of vertical shear.
%
% Input:
%   startp - profile to start shear computation (typically profile 1 has no useful data)
%   endp - last profile to process 
%
% Output:
%   n/a
%
% Called by:
%   n/a
%
% Calls:
%   n/a
%
% Author:
%   Blair Greenan
%   Bedford Institute of Oceanography
%   Matlab 7
%   greenanb@mar.dfo-mpo.gc.ca
% =========================================================================
%

% History
% Version 1.0 28-Jul-2005


U10m = NaN*zeros(21,1:endp);
V10m = NaN*zeros(21,1:endp);
Mag10m = NaN*zeros(21,1:endp);
Dir10m = NaN*zeros(21,1:endp);
shear10m = NaN*zeros(21,1:endp);
dt = NaN*zeros(1,136);

for tt = startp:endp
    inputFile = ['mavs_',num2str(tt),'.dat'];
    [month,day,year,hour,minute,second,VA,VB,VC,VD,U,V,W,T,P,MX,MY,Pitch,Roll] = textread(inputFile,'%d %d %d %d %d %f %s %s %s %s %f %f %f %f %f %f %f %f %f ','headerlines',19,'endofline','\r\n');
    dt(tt) = datenum(year(1),month(1),day(1),hour(1),minute(1),second(1));  % pick first point in time series as representative of profile
    %%%%%%%%% 2 m bin averages %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
    bins = 21;
    for ii = 1:bins
        findP = find((P > ((10*ii)-10)) & (P < (10*ii)));
        U10m(ii,tt) = mean(U(findP))./100;  % convert to m/s
        V10m(ii,tt) = mean(V(findP))./100;
        Mag10m(ii,tt) = (U10m(ii,tt).^2 + V10m(ii,tt).^2).^0.5;
        Dir10m(ii,tt) = atan2(U10m(ii,tt),V10m(ii,tt))*180/pi;
    end
    
    % calculate shear for the 2 m bins
    xx = 5:10:205;
    for qq = 1:bins-1
        delu = (U10m(qq,tt)-U10m(qq+1,tt))/(xx(qq)-xx(qq+1));
        delv = (V10m(qq,tt)-V10m(qq+1,tt))/(xx(qq)-xx(qq+1));
        shear10m(qq,tt) = (delu.^2 + delv.^2)^0.5;
    end

end

figure
pcolor(dt,xx,shear10m)
set(gca,'ydir','rev')
shading flat
V = get(gca,'xtick');
I = datestr(V(1:1:length(V)),'dd-mmm');
set(gca,'xtick',V(1:1:length(V)),'xticklabel',I)
title('MAVS 10m Shear 31 Aug - 7 Sep2005')
ylabel('Depth (m)')
set(gca,'CLim',[0 .02])
colorbar

figure
pcolor(dt,xx,U10m)
set(gca,'ydir','rev')
shading flat
V = get(gca,'xtick');
I = datestr(V(1:1:length(V)),'dd-mmm');
set(gca,'xtick',V(1:1:length(V)),'xticklabel',I)
title('MAVS U 10m 20-23 July 2005')
ylabel('Depth (m)')
set(gca,'CLim',[-.1 .2])
colorbar

figure
pcolor(dt,xx,V10m)
set(gca,'ydir','rev')
shading flat
V = get(gca,'xtick');
I = datestr(V(1:1:length(V)),'dd-mmm');
set(gca,'xtick',V(1:1:length(V)),'xticklabel',I)
title('MAVS V 10m 20-23 July 2005')
ylabel('Depth (m)')
set(gca,'CLim',[-.1 .2])
colorbar

figure
pcolor(dt,xx,Mag10m)
set(gca,'ydir','rev')
shading flat
V = get(gca,'xtick');
I = datestr(V(1:1:length(V)),'dd-mmm');
set(gca,'xtick',V(1:1:length(V)),'xticklabel',I)
title('MAVS Magnitude 10m 31 Aug - 7 Sep2005')
ylabel('Depth (m)')
set(gca,'CLim',[0 .2])
colorbar

figure
pcolor(dt,xx,Dir10m)
set(gca,'ydir','rev')
shading flat
V = get(gca,'xtick');
I = datestr(V(1:1:length(V)),'dd-mmm');
set(gca,'xtick',V(1:1:length(V)),'xticklabel',I)
title('MAVS Direction 10m 31 Aug - 7 Sep2005')
ylabel('Depth (m)')
set(gca,'CLim',[-180 180])
colorbar

shear_bin_depth = xx;

save shear10m.mat U10m V10m Mag10m Dir10m shear10m dt shear_bin_depth



