% S4ADplt1.m                                        by:  Edward T Peltzer, MBARI
%                                                   revised:  2008 Feb 21.
%
% 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);
spsd = 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));
    spsd(I) = std(spd(J));
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

plot(yrd5,spd5,'LineWidth',1,'Color',[0 0 1],'LineStyle','-')

hold on

X1 = [227 308];
Y1 = [10 10];

plot(X1,Y1,'LineWidth',1,'Color',[1 0 0],'LineStyle','--')


% Define axis and set tick-label-size

axis('xy');
axis([225 310 -1 33]);

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 = [225:5:310];

N = length(xtl);

for I = 1:N
    if rem(I,2) == 0
        XTL(I,:) = [num2str(xtl(I))];
    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(['Time (yeardays)']);
h=get(gca,'xlabel');
set(h,'FontSize',20);
set(h,'FontWeight','normal');


% Label plot -- Y-Axis

ylabel('Current velocity (cm/s)');
h=get(gca,'ylabel');
set(h,'FontSize',20);
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(235,30,leg1);
set(h,'Color',[0,0,0]);
set(h,'FontSize',16);
set(h,'FontWeight','normal');

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

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

hold off


% Define plotting region and make figure 2

figure(2)
orient tall

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


% Select subset of data

Q = find((yrd5 >= 251) & (yrd5 <= 289));


% Make plot

plot(yrd5(Q),spd5(Q),'LineWidth',1,'Color',[0 0 1],'LineStyle','-')

hold on

X2 = [251 289];
Y2 = [10 10];

plot(X2,Y2,'LineWidth',1,'Color',[1 0 0],'LineStyle','--')


% Define axis and set tick-label-size

axis('xy');
axis([250 290 -1 33]);

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 = [250:5:290];

N = length(xtl);

for I = 1:N
    if rem(I-1,2) == 0
        XTL(I,:) = [num2str(xtl(I))];
    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(['Time (yeardays)']);
h=get(gca,'xlabel');
set(h,'FontSize',20);
set(h,'FontWeight','normal');


% Label plot -- Y-Axis

ylabel('Current velocity (cm/s)');
h=get(gca,'ylabel');
set(h,'FontSize',20);
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(Q)),std(spd5(Q)));
h = text(254,30,leg1);
set(h,'Color',[0,0,0]);
set(h,'FontSize',16);
set(h,'FontWeight','normal');

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

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

leg4a = ['\downarrow'];
h = text(254+(12+45/60)/24,18,leg4a);
set(h,'Color',[1,0,0]);
set(h,'FontSize',16);
set(h,'FontWeight','normal');
set(h,'VerticalAlignment','bottom','HorizontalAlignment','center')

leg4b = ['New'];
h = text(254+(12+45/60)/24,20,leg4b);
set(h,'Color',[1,0,0]);
set(h,'FontSize',14);
set(h,'FontWeight','normal');
set(h,'VerticalAlignment','baseline','HorizontalAlignment','center')

leg5a = ['\downarrow'];
h = text(262+(16+48/60)/24,18,leg5a);
set(h,'Color',[1,0,0]);
set(h,'FontSize',16);
set(h,'FontWeight','normal');
set(h,'VerticalAlignment','bottom','HorizontalAlignment','center')

leg5b = ['1Qtr'];
h = text(262+(16+48/60)/24,20,leg5b);
set(h,'Color',[1,0,0]);
set(h,'FontSize',14);
set(h,'FontWeight','normal');
set(h,'VerticalAlignment','baseline','HorizontalAlignment','center')

leg6a = ['\downarrow'];
h = text(269+(19+45/60)/24,18,leg6a);
set(h,'Color',[1,0,0]);
set(h,'FontSize',16);
set(h,'FontWeight','normal');
set(h,'VerticalAlignment','bottom','HorizontalAlignment','center')

leg6b = ['Full'];
h = text(269+(19+45/60)/24,20,leg6b);
set(h,'Color',[1,0,0]);
set(h,'FontSize',14);
set(h,'FontWeight','normal');
set(h,'VerticalAlignment','baseline','HorizontalAlignment','center')

leg7a = ['\downarrow'];
h = text(276+(10+6/60)/24,18,leg7a);
set(h,'Color',[1,0,0]);
set(h,'FontSize',16);
set(h,'FontWeight','normal');
set(h,'VerticalAlignment','bottom','HorizontalAlignment','center')

leg7b = ['3Qtr'];
h = text(276+(10+6/60)/24,20,leg7b);
set(h,'Color',[1,0,0]);
set(h,'FontSize',14);
set(h,'FontWeight','normal');
set(h,'VerticalAlignment','baseline','HorizontalAlignment','center')

leg8a = ['\downarrow'];
h = text(284+(5+1/60)/24,18,leg8a);
set(h,'Color',[1,0,0]);
set(h,'FontSize',16);
set(h,'FontWeight','normal');
set(h,'VerticalAlignment','bottom','HorizontalAlignment','center')

leg8b = ['New'];
h = text(284+(5+1/60)/24,20,leg8b);
set(h,'Color',[1,0,0]);
set(h,'FontSize',14);
set(h,'FontWeight','normal');
set(h,'VerticalAlignment','baseline','HorizontalAlignment','center')

hold off
