% S4ADplt4.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');
tmp = xlsread('S4AD_2007Nov_02','Select','K3:K9472');


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

maxI = length(hms) / 5;

yrd5 = zeros(maxI,1);
tmp5 = 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;
    tmp5(I) = mean(tmp(J));
end

for I = 1:40
    K(I) = length(find(floor(tmp5*20)==(I+69)))*100/maxI;
    L(I) = length(find(tmp5*20 < I+70))*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([3.525:0.05:5.475],K,'b')

hold on

plot([3.5:0.05:5.6],[0 L 20 20],'LineWidth',1,'Color',[1 0 0],'LineStyle','-')


% Define axis and set tick-label-size

axis('xy');
axis([3.6 5.6 -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 = [3.6:0.2:5.6];

N = length(xtl);

for I = 1:N
    if rem(I-1,2) == 0
        if rem(xtl(I)*10,10) == 0
            XTL(I,:) = [num2str(xtl(I)) '.0'];
        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(['Temperature (\circC)']);
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(5.84,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(5.64,20,leg10);
set(h,'Color',[0,0,0]);
set(h,'FontSize',16);
set(h,'FontWeight','normal');

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

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

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

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

leg00 = [' 0'];
h = text(5.64,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.2f ' setstr(177) ' %4.2f ' setstr(176) 'C'],mean(tmp5),std(tmp5));
h = text(4.7,16,leg1);
set(h,'Color',[0,0,0]);
set(h,'FontSize',16);
set(h,'FontWeight','normal');

leg2 = sprintf(['Median = %4.2f ' setstr(176) 'C'],median(tmp5));
h = text(4.8,14,leg2);
set(h,'Color',[0,0,0]);
set(h,'FontSize',16);
set(h,'FontWeight','normal');

leg3 = sprintf(['Max temp = %4.2f ' setstr(176) 'C'],max(tmp5));
h = text(4.8,12,leg3);
set(h,'Color',[0,0,0]);
set(h,'FontSize',16);
set(h,'FontWeight','normal');

leg4 = sprintf(['Min temp = %4.2f ' setstr(176) 'C'],min(tmp5));
h = text(4.8,10,leg4);
set(h,'Color',[0,0,0]);
set(h,'FontSize',16);
set(h,'FontWeight','normal');

hold off
