function plot_mavs(inputFile)
% plot_mavs  load and process a Nobska MAVS data file
% =========================================================================
% plot_mavs  Version 1.0 24-Jul-2005
%
% Usage: 
%   plot_mavs(varargin)
%
% Description:
%   This function loads a MAVS data file logged on the SeaHorse profiler
%   and processes it.
%
% Input:
%   inputFile is a character string containing the name of the MAVS data
%   file.
%
% 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 24-Jul-2005


[month,day,year,hours,minutes,seconds,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');
%[month,day,year,hours,minutes,seconds,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',0);


% define a 10-point boxcar filter
%a = 1;
%b = [1/4 1/4 1/4 1/4];
%b = [1/10 1/10 1/10 1/10 1/10 1/10 1/10 1/10 1/10 1/10];

% define a low pass butterworth filter
[b,a] = butter(10,0.5/3,'low'); % 10th-order 


meanPitch = mean(Pitch);
meanRoll = mean(Roll);
PitchMeanRemoved = Pitch - meanPitch;
RollMeanRemoved = Roll - meanRoll;


% plot raw 6 Hz data along with low-pass filtered (butterworth) data
figure(1)
subplot(131)
plot(U,P,'.-')
Ufilt = filter(b,a,U);
hold on
plot(Ufilt,P,'r-')
set(gca,'ydir','rev')
xlabel('U [cm/s]')
ylabel('Depth [m]')
title(inputFile,'Interpreter','none')
subplot(132)
plot(V,P,'.-')
Vfilt = filter(b,a,V);
hold on
plot(Vfilt,P,'r-')
set(gca,'ydir','rev')
xlabel('V [cm/s]')
ylabel('Depth [m]')
title(inputFile,'Interpreter','none')
subplot(133)
plot(W,P,'.-')
Wfilt = filter(b,a,W);
hold on
plot(Wfilt,P,'r-')
set(gca,'ydir','rev')
xlabel('W [cm/s]')
ylabel('Depth [m]')
title(inputFile,'Interpreter','none')

figure(2)
subplot(121)
plot(MX,P,'.-')
MXfilt = filter(b,a,MX);
hold on
plot(MXfilt,P,'r-')
set(gca,'ydir','rev')
xlabel('MX')
ylabel('Depth [m]')
title(inputFile,'Interpreter','none')
subplot(122)
plot(MY,P,'.-')
MYfilt = filter(b,a,MY);
hold on
plot(MYfilt,P,'r-')
set(gca,'ydir','rev')
xlabel('MY')
ylabel('Depth [m]')
title(inputFile,'Interpreter','none')


figure(3)
subplot(121)
plot(Pitch,P,'.-')
Pitchfilt = filter(b,a,Pitch);
hold on
plot(Pitchfilt,P,'r-')
set(gca,'ydir','rev')
xlabel('Pitch [deg]')
ylabel('Depth [m]')
title(inputFile,'Interpreter','none')
subplot(122)
plot(Roll,P,'.-')
Rollfilt = filter(b,a,Roll);
hold on
plot(Rollfilt,P,'r-')
set(gca,'ydir','rev')
xlabel('Roll [deg]')
ylabel('Depth [m]')
title(inputFile,'Interpreter','none')

% heading
figure(11)
hdng = (atan2(-1.*MX,MY)).*(180/pi);
findNegHdng = find(hdng < 0);
hdng(findNegHdng) = hdng(findNegHdng) + 360;
hdngFilt = filter(b,a,hdng);
plot(hdng,P,'.-')
hold on
plot(hdngFilt,P,'r-')
set(gca,'ydir','rev')
xlabel('Heading [deg 0-North 90-East 180-South 270-West]')
ylabel('Depth [m]')
title(inputFile,'Interpreter','none')

% magnitude
figure(12)
mag1 = (U.^2 + V.^2).^0.5;
%magFilt = filter(b,a,mag);
plot(mag1,P,'.-')
%plot(magFilt,P,'r-')
set(gca,'ydir','rev')
xlabel('Magnitude [cm/s]')
ylabel('Depth [m]')
title(inputFile,'Interpreter','none')




%%%%%%%%% 2 m bin averages %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
bins = 100;
for ii = 1:bins
    findP = find((P > ((2*ii)+8)) & (P < ((2*ii)+10)));
    Ubin(ii) = mean(U(findP));
    StdUbin(ii) = std(U(findP));
    Vbin(ii) = mean(V(findP));
    StdVbin(ii) = std(V(findP));
    Wbin(ii) = mean(W(findP));
    StdWbin(ii) = std(W(findP));
    Pitchbin(ii) = mean(Pitch(findP));
    StdPitchbin(ii) = std(Pitch(findP));
    Rollbin(ii) = mean(Roll(findP));
    StdRollbin(ii) = std(Roll(findP));
    hdngbin(ii) = mean(hdng(findP));
    Stdhdngbin(ii) = std(hdng(findP));
    magbin(ii) = mean(mag1(findP));
    Stdmagbin(ii) = std(mag1(findP));
end

xx = 11:2:209;

figure
subplot(121)
plot(Ubin,xx,'b.-')
hold on
set(gca,'ydir','rev')
xlabel('U [cm/s]')
ylabel('Depth [m]')
title(inputFile,'Interpreter','none')

subplot(122)
plot(Vbin,xx,'b.-')
hold on
set(gca,'ydir','rev')
xlabel('V [cm/s]')
ylabel('Depth [m]')
title(inputFile,'Interpreter','none')

figure
plot(magbin,xx,'b.-')
hold on
set(gca,'ydir','rev')
xlabel('Magnitude [cm/s]')
ylabel('Depth [m]')
title(inputFile,'Interpreter','none')

figure
plot(hdngbin,xx,'b.-')
hold on
set(gca,'ydir','rev')
xlabel('Heading [deg 0-North 90-East 180-South 270-West]')
ylabel('Depth [m]')
title(inputFile,'Interpreter','none')

% calculate shear for the 2 m bins

for qq = 1:bins-1
    delu(qq) = (Ubin(qq)-Ubin(qq+1))/(xx(qq)-xx(qq+1));
    delv(qq) = (Vbin(qq)-Vbin(qq+1))/(xx(qq)-xx(qq+1));
    shear(qq) = (delu(qq).^2 + delv(qq).^2)^0.5;
end

% depth of shear bins
sbins = xx+1:2:xx(end)-1;

figure
plot(shear,sbins,'b.-')
hold on
set(gca,'ydir','rev')
xlabel('Shear [s^{-1}]')
ylabel('Depth [m]')
title(inputFile,'Interpreter','none')




%%%%%%%%% 4 m bin averages %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
bins = 50;
for ii = 1:bins
    findP = find((P > ((4*ii)+6)) & (P < ((4*ii)+10)));
    Ubin4(ii) = mean(U(findP));
    StdUbin4(ii) = std(U(findP));
    Vbin4(ii) = mean(V(findP));
    StdVbin4(ii) = std(V(findP));
    Wbin4(ii) = mean(W(findP));
    StdWbin4(ii) = std(W(findP));
    Pitchbin4(ii) = mean(Pitch(findP));
    StdPitchbin4(ii) = std(Pitch(findP));
    Rollbin4(ii) = mean(Roll(findP));
    StdRollbin4(ii) = std(Roll(findP));
    hdngbin4(ii) = mean(hdng(findP));
    Stdhdngbin4(ii) = std(hdng(findP));
    magbin4(ii) = mean(mag1(findP));
    Stdmagbin4(ii) = std(mag1(findP));
end

xx4 = 11:4:209;

figure
plot(magbin4,xx4,'b.-')
hold on
set(gca,'ydir','rev')
xlabel('Magnitude [cm/s]')
ylabel('Depth [m]')
title(inputFile,'Interpreter','none')

figure
plot(hdngbin4,xx4,'b.-')
hold on
set(gca,'ydir','rev')
xlabel('Heading [deg 0-North 90-East 180-South 270-West]')
ylabel('Depth [m]')
title(inputFile,'Interpreter','none')


% calculate shear for the 4 m bins

for qq = 1:bins-1
    delu4(qq) = (Ubin4(qq)-Ubin4(qq+1))/(xx4(qq)-xx4(qq+1));
    delv4(qq) = (Vbin4(qq)-Vbin4(qq+1))/(xx4(qq)-xx4(qq+1));
    shear4(qq) = (delu4(qq).^2 + delv4(qq).^2)^0.5;
end

% depth of shear bins
sbins4 = xx4+1:4:xx4(end)-1;

figure
plot(shear4,sbins4,'b.-')
hold on
set(gca,'ydir','rev')
xlabel('Shear [s^{-1}]')
ylabel('Depth [m]')
title(inputFile,'Interpreter','none')




%{
% see how 2m bin averages compare to 10 point boxcar filter
figure
subplot(121)
plot(Ubin,xx,'r')
set(gca,'ydir','rev')
hold on
plot(Ufilt,P,'r-')

% see how 2m bin averages compare to 10 point boxcar filter
subplot(122)
plot(Vbin,xx,'g')
set(gca,'ydir','rev')
hold on
plot(Vnfilt,P,'r-')



%keyboard


%figure
%errorbarx(1:bins,Uebin,StdUebin)
%verticalfill(1:25,Uebin-StdUebin,Uebin+StdUebin,[0.8 0.8 0.8])
%set(gca,'ydir','rev')
%hold on
%plot(Uebin,1:bins,'r-')
%plot(Ue,P,'b.')

figure
subplot(131)
verticalfill(1:25,Pitchbin-StdPitchbin,Pitchbin+StdPitchbin,[0.8 0.8 0.8])
hold on
plot(Pitch,P,'.-')
plot(Pitchbin,1:bins,'r-')
set(gca,'ydir','rev','xlim',[-20 20])
xlabel('Pitch [deg]')
ylabel('Depth [m]')
ht = title(inputFile);
set(ht,'interpreter','none')
subplot(132)
verticalfill(1:25,Rollbin-StdRollbin,Rollbin+StdRollbin,[0.8 0.8 0.8])
hold on
plot(Roll,P,'.-')
plot(Rollbin,1:bins,'r-')
set(gca,'ydir','rev','xlim',[-20 20])
xlabel('Roll [deg]')
ylabel('Depth [m]')
ht = title(inputFile);
set(ht,'interpreter','none')
subplot(133)
verticalfill(1:25,hdngbin-Stdhdngbin,hdngbin+Stdhdngbin,[0.8 0.8 0.8])
hold on
plot(hdng,P,'.-')
plot(hdngbin,1:bins,'r-')
set(gca,'ydir','rev')
xlabel('Heading')
ylabel('Depth [m]')
ht = title(inputFile);
set(ht,'interpreter','none')
% print figure
eval(['print -dpsc prh_', inputFile(1:6), '.ps'])
eval(['print -depsc -tiff prh_', inputFile(1:6), '.eps'])

figure
subplot(131)
verticalfill(1:25,Ubin-StdUbin,Ubin+StdUbin,[0.8 0.8 0.8])
hold on
plot(U,P,'.-')
plot(Ubin,1:bins,'r-')
set(gca,'ydir','rev','xlim',[-20 20])
xlabel('Ueast [cm/s]')
ylabel('Depth [m]')
ht = title(inputFile);
set(ht,'interpreter','none')
subplot(132)
verticalfill(1:25,Vbin-StdVbin,Vbin+StdVbin,[0.8 0.8 0.8])
hold on
plot(V,P,'.-')
plot(Vbin,1:bins,'r-')
set(gca,'ydir','rev','xlim',[-20 20])
xlabel('Vnorth [cm/s]')
ylabel('Depth [m]')
ht = title(inputFile);
set(ht,'interpreter','none')
subplot(133)
verticalfill(1:25,Wbin-StdWbin,Wbin+StdWbin,[0.8 0.8 0.8])
hold on
plot(W,P,'.-')
plot(Wbin,1:bins,'r-')
set(gca,'ydir','rev','xlim',[-40 10])
xlabel('Wup [cm/s]')
ylabel('Depth [m]')
ht = title(inputFile);
set(ht,'interpreter','none')
% print figure
%eval(['print -dpsc uvw_', inputFile(1:6), '.ps'])
%eval(['print -depsc -tiff uvw_', inputFile(1:6), '.eps'])


%}
