% pHctd_01.m                                        by:  Edward T Peltzer, MBARI
%                                                   revised:  2008 Feb 24.
%
% M-file to plot pH and temperature data from the pH_CTD
% deployment during 2007: Aug 15th (227) thru Nov 2nd (306).
%
% Data file = S4AD_2007Nov_02.xls  /  Worksheet = Select
%
%   Column   Parameter        Units
%
%     A      Time             Yearday  
%     B      Temp             deg C
%     C      Cond             S/m
%     D      Pres             dbar
%     E      pH_513           volts
%     F      pH_507           volts
%     G      pH_508           volts


% Clear environment space and set orientation

clear all
close all


% Load data and assign data vectors

yrd = xlsread('pH_CTD','Select','A2:A11365');
tmp = xlsread('pH_CTD','Select','B2:B11365');
con = xlsread('pH_CTD','Select','C2:C11365');
prs = xlsread('pH_CTD','Select','D2:D11365');
V13 = xlsread('pH_CTD','Select','E2:E11365');
V07 = xlsread('pH_CTD','Select','F2:F11365');
V08 = xlsread('pH_CTD','Select','G2:G11365');


% Calculate salinity from conductivity

sal = salinity(con,tmp,prs);


% Calculate pH for each of three electrodes

slp3 = 4.6557;
off3 = 2.5968;

slp7 = 3.6846;
off7 = 2.5335;

slp8 = 4.5477;
off8 = 2.5902;

pH3 = 7 + (V13 - off3) ./ (slp3 .* (tmp + 273.15) * 1.98416e-4);
pH7 = 7 + (V07 - off7) ./ (slp7 .* (tmp + 273.15) * 1.98416e-4);
pH8 = 7 + (V08 - off8) ./ (slp8 .* (tmp + 273.15) * 1.98416e-4);


% 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(yrd,tmp,'LineWidth',1,'Color',[0 0 0],'LineStyle','-')

hold on

plot(yrd,pH3,'LineWidth',1,'Color',[0 1 0],'LineStyle','-')
plot(yrd,pH7,'LineWidth',1,'Color',[1 0 0],'LineStyle','-')
plot(yrd,pH8,'LineWidth',1,'Color',[0 0 1],'LineStyle','-')
plot([227 308],[7.6223 7.6223],'LineWidth',1,'Color',[0 0 0],'LineStyle',':')


% Define axis and set tick-label-size

axis('xy');
axis([225 310 3 8]);

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 = [230:5:305];

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 = [3:0.5:8];

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 (yearday)']);
h=get(gca,'xlabel');
set(h,'Color',[0,0,0]);
set(h,'FontSize',20);
set(h,'FontWeight','normal');


% Label plot -- Y-Axis

ylabel('pH');
h=get(gca,'ylabel');
set(h,'Color',[0,0,0]);
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.2f ' setstr(177) ' %4.2f ' setstr(176) 'C'],mean(tmp),std(tmp));
h = text(250,3.75,leg1);
set(h,'Color',[0,0,0]);
set(h,'FontSize',16);
set(h,'FontWeight','normal');

leg2 = ['-- = pH 07'];
h = text(280,6.5,leg2);
set(h,'Color',[1,0,0]);
set(h,'FontSize',16);
set(h,'FontWeight','normal');

leg3 = ['-- = pH 08'];
h = text(280,6.0,leg3);
set(h,'Color',[0,0,1]);
set(h,'FontSize',16);
set(h,'FontWeight','normal');

leg4 = ['-- = pH 13'];
h = text(280,5.5,leg4);
set(h,'Color',[0,1,0]);
set(h,'FontSize',16);
set(h,'FontWeight','normal');


% Label plot -- Y-Axis 2

ylab2 = ['Temperature (\circC)'];
h = text(318,6.4,ylab2);
set(h,'Color',[0,0,0]);
set(h,'FontSize',20);
set(h,'FontWeight','normal');
set(h,'VerticalAlignment','middle','HorizontalAlignment','left')
set(h,'Rotation',-90);

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

leg07 = ['7'];
h = text(311,7,leg07);
set(h,'Color',[0,0,0]);
set(h,'FontSize',16);
set(h,'FontWeight','normal');

leg06 = ['6'];
h = text(311,6,leg06);
set(h,'Color',[0,0,0]);
set(h,'FontSize',16);
set(h,'FontWeight','normal');

leg05 = ['5'];
h = text(311,5,leg05);
set(h,'Color',[0,0,0]);
set(h,'FontSize',16);
set(h,'FontWeight','normal');

leg04 = ['4'];
h = text(311,4,leg04);
set(h,'Color',[0,0,0]);
set(h,'FontSize',16);
set(h,'FontWeight','normal');

leg03 = ['3'];
h = text(311,3,leg03);
set(h,'Color',[0,0,0]);
set(h,'FontSize',16);
set(h,'FontWeight','normal');

hold off
