% S4ADplt3.m                                        by:  Edward T Peltzer, MBARI
%                                                   revised:  2008 Feb 22.
%
% M-file to plot current, temperature and salinity data from S4AD
% deployment during 2007: Aug 15th (227) thru Nov 2nd (306).
%
% Data file = S4AD_2007Nov_02.xls  /  Worksheet = Select
%
%   Column   Parameter        Units
%
%     A      Time             HH:MM:SS  
%     E      Speed            cm/sec
%     F      Direction        deg
%     J      Cond             mS/cm
%     K      Temp             deg C


% Clear environment space and set orientation

clear all
close all


% Load data and assign data vectors

hms = xlsread('S4AD_2007Nov_02','Select','A3:A9472');
spd = xlsread('S4AD_2007Nov_02','Select','E3:E9472');


% Calculate 5 min averages for each hour and assign decimal yearday

maxI = length(hms) / 5;

yrd5 = zeros(maxI,1);
spd5 = zeros(maxI,1);
spdf = zeros(maxI,1);

for I = 1:maxI
    J = [((I-1)*5 + 1):((I-1)*5 + 5)];
    yrd = 227 + floor((I+19)/24);
    yrd5(I) = mean(hms(J))+ yrd;
    spd5(I) = mean(spd(J));
end

for I = 1:30
    K(I) = length(find(floor(spd5)==I-1))*100/maxI;
    L(I) = length(find(spd5 < I))*20/maxI;
end


% Define plotting region and make figure 1

figure(1)
orient tall

left = 1.25/8.5;
bottom = 2.5/11;
width = 6/8.5;
height = 6/11;

subplot('position',[left bottom width height])


% Make plot

bar([0.5:1:29.5],K,'b')

hold on

plot([0:30],[0 L],'LineWidth',1,'Color',[1 0 0],'LineStyle','-')


% Define axis and set tick-label-size

axis('xy');
axis([0 30 -1 21]);

h=gca;
set(h,'FontSize',16);
set(h,'FontWeight','normal');
set(h,'LineWidth',1);


% Manually define and control X-ticks and X-tick labels

xtl = [0:5:30];

N = length(xtl);

for I = 1:N
    if rem(I-1,2) == 0
        if xtl(I) == 0
            XTL(I,:) = [' 0'];
        elseif xtl(I) < 10
            XTL(I,:) = [' ' num2str(xtl(I))];
        else
            XTL(I,:) = [num2str(xtl(I))];
        end
    else
        XTL(I,:) = ['  '];
    end
end

set(h,'XTick',xtl);
set(h,'XTickLabel',XTL);


% Manually define and control Y-ticks and Y-tick labels

ytl = [0:2:32];

N = length(ytl);

for I = 1:N
    if rem(I-1,2) == 0
        if ytl(I) == 0
            YTL(I,:) = ['  0 '];
        elseif ytl(I) < 10
        	YTL(I,:) = ['  ' num2str(ytl(I)) ' '];
        else
            YTL(I,:) = [' ' num2str(ytl(I)) ' '];
        end
    else
        YTL(I,:) = ['    '];
    end    
end

set(h,'YTick',ytl);
set(h,'YTickLabel',YTL);


% Label plot -- X-Axis

xlabel(['Velocity (cm/s)']);
h=get(gca,'xlabel');
set(h,'Color',[0,0,0]);
set(h,'FontSize',20);
set(h,'FontWeight','normal');


% Label plot -- Y-Axis

ylabel('Frequency (%)');
h=get(gca,'ylabel');
set(h,'Color',[0,0,1]);
set(h,'FontSize',20);
set(h,'FontWeight','normal');


% Label plot -- Y-Axis 2

ylab2 = ['Cumulative frequency (%)'];
h = text(33.5,16,ylab2);
set(h,'Color',[1,0,0]);
set(h,'FontSize',20);
set(h,'FontWeight','normal');
set(h,'VerticalAlignment','middle','HorizontalAlignment','left')
set(h,'Rotation',-90);

leg10 = ['100'];
h = text(30.2,20,leg10);
set(h,'Color',[0,0,0]);
set(h,'FontSize',16);
set(h,'FontWeight','normal');

leg08 = [' 80'];
h = text(30.2,16,leg08);
set(h,'Color',[0,0,0]);
set(h,'FontSize',16);
set(h,'FontWeight','normal');

leg06 = [' 60'];
h = text(30.2,12,leg06);
set(h,'Color',[0,0,0]);
set(h,'FontSize',16);
set(h,'FontWeight','normal');

leg04 = [' 40'];
h = text(30.2,8,leg04);
set(h,'Color',[0,0,0]);
set(h,'FontSize',16);
set(h,'FontWeight','normal');

leg02 = [' 20'];
h = text(30.2,4,leg02);
set(h,'Color',[0,0,0]);
set(h,'FontSize',16);
set(h,'FontWeight','normal');

leg00 = [' 0'];
h = text(30.2,0,leg00);
set(h,'Color',[0,0,0]);
set(h,'FontSize',16);
set(h,'FontWeight','normal');


% Label plot -- Title

title(['MARS Test Station 2007']);
h=get(gca,'title');
set(h,'FontSize',20);
set(h,'FontWeight','normal');


% Label plot -- Legends

leg1 = sprintf(['Mean = %4.1f ' setstr(177) ' %3.1f cm/s'],mean(spd5),std(spd5));
h = text(15,17,leg1);
set(h,'Color',[0,0,0]);
set(h,'FontSize',16);
set(h,'FontWeight','normal');

leg2 = sprintf(['Median = %4.1f cm/s'],median(spd5));
h = text(15,15,leg2);
set(h,'Color',[0,0,0]);
set(h,'FontSize',16);
set(h,'FontWeight','normal');

leg3 = sprintf(['Max velocity = %4.1f cm/s'],max(spd5));
h = text(15,13,leg3);
set(h,'Color',[0,0,0]);
set(h,'FontSize',16);
set(h,'FontWeight','normal');

leg4 = sprintf(['Min velocity = %4.2f cm/s'],min(spd5));
h = text(15,11,leg4);
set(h,'Color',[0,0,0]);
set(h,'FontSize',16);
set(h,'FontWeight','normal');

leg5 = sprintf(['Freq < 10 cm/s = %4.1f %%'],length(find(spd5<10))*100/maxI);
h = text(15,7,leg5);
set(h,'Color',[0,0,0]);
set(h,'FontSize',16);
set(h,'FontWeight','normal');

leg6 = sprintf(['Freq  <  1 cm/s  =  %3.1f %%'],length(find(spd5<1))*100/maxI);
h = text(15,5,leg6);
set(h,'Color',[0,0,0]);
set(h,'FontSize',16);
set(h,'FontWeight','normal');

hold off
