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


U2m = NaN*zeros(100,1:endp);
V2m = NaN*zeros(100,1:endp);
Mag2m = NaN*zeros(100,1:endp);
Dir2m = NaN*zeros(100,1:endp);
shear2m = NaN*zeros(100,1:endp);
dt = NaN*zeros(1,82);

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 = 100;
    for ii = 1:bins
        findP = find((P > ((2*ii)+8)) & (P < ((2*ii)+10)));
        U2m(ii,tt) = mean(U(findP))./100;  % convert to m/s
        V2m(ii,tt) = mean(V(findP))./100;
        Mag2m(ii,tt) = (U2m(ii,tt).^2 + V2m(ii,tt).^2).^0.5;
        Dir2m(ii,tt) = atan2(U2m(ii,tt),V2m(ii,tt))*180/pi;
    end
    
    % calculate shear for the 2 m bins
    xx = 11:2:209;
    for qq = 1:bins-1
        delu = (U2m(qq,tt)-U2m(qq+1,tt))/(xx(qq)-xx(qq+1));
        delv = (V2m(qq,tt)-V2m(qq+1,tt))/(xx(qq)-xx(qq+1));
        shear2m(qq,tt) = (delu.^2 + delv.^2)^0.5;
    end

end

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

figure
pcolor(dt,xx,U2m)
set(gca,'ydir','rev')
shading flat
V = get(gca,'xtick');
I = datestr(V(1:1:length(V)),'HH:MM dd-mmm');
set(gca,'xtick',V(1:1:length(V)),'xticklabel',I)
title('MAVS U 2m 31 Aug - 7 Sep 2005')
ylabel('Depth (m)')
set(gca,'CLim',[-.3 .3])
colorbar

figure
pcolor(dt,xx,V2m)
set(gca,'ydir','rev')
shading flat
V = get(gca,'xtick');
I = datestr(V(1:1:length(V)),'HH:MM dd-mmm');
set(gca,'xtick',V(1:1:length(V)),'xticklabel',I)
title('MAVS V 2m 31 Aug - 7 Sep 2005')
ylabel('Depth (m)')
set(gca,'CLim',[-.3 .3])
colorbar

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

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

shear_bin_depth = xx;

save shear2m.mat U2m V2m Mag2m Dir2m shear2m dt shear_bin_depth



