% S4ADplt2.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');
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);
spd5 = 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;
    spd5(I) = mean(spd(J));
    tmp5(I) = mean(tmp(J));
end


% Calculate FFT of velocity and temperature data

V = fft(spd5);
Nv = length(V);
V(1) = [];
Vpwr = abs(V(1:Nv/2)).^2;
Vfreq = (1:Nv/2)/(Nv/2)/2;
Vper = 1./Vfreq;

T = fft(tmp5);
Nt = length(T);
T(1) = [];
Tpwr = abs(T(1:Nt/2)).^2;
Tfreq = (1:Nt/2)/(Nt/2)/2;
Tper = 1./Tfreq;


% 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(Vfreq,Vpwr/10^5,'LineWidth',1,'Color',[1 0 0],'LineStyle','-')

hold on

plot(Tfreq,Tpwr/10^2,'LineWidth',1,'Color',[0 0 1],'LineStyle','-')


% Define axis and set tick-label-size

axis('xy');
axis([0 0.5 -1 16]);

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:0.05:0.5];

N = length(xtl);

for I = 1:N
    if rem(I-1,2) == 0
        if xtl(I) == 0
            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:1:15];

N = length(ytl);

for I = 1:N
    if rem(I-1,5) == 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(['Frequency (cycles/hour)']);
h=get(gca,'xlabel');
set(h,'FontSize',20);
set(h,'FontWeight','normal');


% Label plot -- Y-Axis

ylabel('Power (arbitrary)');
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 = ['-- Velocity'];
h = text(0.3,13,leg1);
set(h,'Color',[1,0,0]);
set(h,'FontSize',16);
set(h,'FontWeight','normal');

leg2 = ['-- Temperature'];
h = text(0.3,11,leg2);
set(h,'Color',[0,0,1]);
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])


% Make plot

plot(Vper,Vpwr/10^5,'LineWidth',1,'Color',[1 0 0],'LineStyle','-')

hold on

plot(Tper,Tpwr/10^2,'LineWidth',1,'Color',[0 0 1],'LineStyle','-')


% Define axis and set tick-label-size

axis('xy');
axis([0 40 -1 16]);

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:40];

N = length(xtl);

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

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


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

ytl = [0:1:15];

N = length(ytl);

for I = 1:N
    if rem(I-1,5) == 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(['Period (hours/cycle)']);
h=get(gca,'xlabel');
set(h,'FontSize',20);
set(h,'FontWeight','normal');


% Label plot -- Y-Axis

ylabel('Power (arbitrary)');
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 = ['-- Velocity'];
h = text(24,13,leg1);
set(h,'Color',[1,0,0]);
set(h,'FontSize',16);
set(h,'FontWeight','normal');

leg2 = ['-- Temperature'];
h = text(24,11,leg2);
set(h,'Color',[0,0,1]);
set(h,'FontSize',16);
set(h,'FontWeight','normal');

hold off
