%plotOpt2Sed.m
%
%Program to plot the beam attenuation and optical backscatter (x axis)
%versus the sediment concentration from Niskin bottles from the Oceanus
%Cruise.
%
%Soupy Alexander, 5/14/02

%Load in the Niskin bottle data
%Created from Oceanus file
load C:\Data_Reports\NewYorkInProg\Report\CTD\oc_rtf.txt

%Load in the bottle file data
%These files are not included in the data report:
%they were created by removing the headers/date vectors from the ASCII format
%Seabird files
for index = [(1:25) (30:33) (35:36)];    
eval(['load C:\Seabird\Oceanus\Process\Bottle_Files\Text_Files\cast' num2str(index) '.txt']);
end

[m,n] = size(oc_rtf);

%Go bottle info by bottle info to line up the data
%Saving:
%1)Filter Number  2)CTD Station  3)Bottle Height  4)Volume Filtered
%5)Filter Mass  6)Filter+Sed  7)Sed  8)Conc  9)Resolution
%10)Bottle Position  11)Salinity  12)SigmaT  13)Pressure
%14)Temperature  15)Salinity  16)Conductivity  17)Fluorescence
%18)Transmissomter  19)OBS  20)Altimeter

bottle_mat = repmat(NaN,m,20);

for index = 1:m;
    %Fill in the filtered bottle data
    bottle_mat(index,1:9) = oc_rtf(index,:);
    
    %Look up the station
    station_num = oc_rtf(index,2);
    height = oc_rtf(index,3);
    
    if station_num~=9999 & height~=9999;
        eval(['cast=cast' num2str(station_num) ';']);
        
        closest_bot = value2Index(cast(:,11),height);
        
        if isnan(closest_bot) == 0;
            bottle_mat(index,10:20) = cast(closest_bot,1:11);
        end
    else bottle_mat(index,:) = NaN;
    end
end

bottom_read = find(bottle_mat(:,20) < 5);
top_read = find(bottle_mat(:,20) >=5);

%Plot transmission vs. concentration
figure(1)
trans_per = 20*bottle_mat(:,18) + 0;
attn = -4*log(trans_per./100);
hold on
plot(bottle_mat(bottom_read,18),bottle_mat(bottom_read,8),'.')
plot(bottle_mat(top_read,18),bottle_mat(top_read,8),'^')
set(gca,'fontsize',12)
axis([0 5 0 5])
xlabel('Transmissometer Voltage (V)')
ylabel('Sed. Concentration (mg/L)')
title('Transmission vs. Concentration')
legend('Less Than 5 mab','More Than 5 mab')
box('on')

%Do a fit
[p_t,s] = polyfit(bottle_mat(bottom_read,18),bottle_mat(bottom_read,8),1);
[t_fit,t_delta] = polyval(p_t,[0:0.1:5],s);
plot([0:0.1:5],t_fit)
plot([0:0.1:5],t_fit+t_delta,'-.')
plot([0:0.1:5],t_fit-t_delta,'-.')
text(0.5,4,['Slope = ' num2str(p_t(1),'%0.2f') ', Int. = ' num2str(p_t(2),'%0.2f')])

figure(2)
hold on
plot(attn(bottom_read),bottle_mat(bottom_read,8),'.')
plot(attn(top_read),bottle_mat(top_read,8),'^')
set(gca,'fontsize',12)
axis([0 5 0 5])
xlabel('Beam Attenuation (m^-^1)')
ylabel('Sed. Concentration (mg/L)')
t = title('Oceanus Cruise:  Beam Attenuation vs. Concentration');
legend('Less Than 5 mab','More Than 5 mab')
set(t,'fontsize',14)
box('on')

%Do a fit
[p_at,s] = polyfit(attn(bottom_read),bottle_mat(bottom_read,8),1);
[at_fit,at_delta] = polyval(p_at,[0:0.1:5],s);
plot([0:0.1:5],at_fit)
plot([0:0.1:5],at_fit+at_delta,'-.')
plot([0:0.1:5],at_fit-at_delta,'-.')
text(2.5,2,['Slope = ' num2str(p_at(1),'%0.2f') ', Intercept = ' num2str(p_at(2),'%0.2f')])

%Plot OBS vs. concentration
figure(3)
hold on
plot(bottle_mat(bottom_read,19),bottle_mat(bottom_read,8),'.')
plot(bottle_mat(top_read,19),bottle_mat(top_read,8),'^')
set(gca,'fontsize',12)
axis([0 1 0 5])
xlabel('OBS Voltage (V)')
ylabel('Sed. Concentration (mg/L)')
t = title('Oceanus Cruise:  Optical Backscatter vs. Concentration');
set(t,'fontsize',14)
legend('Less Than 5 mab','More Than 5 mab')
box('on')
%Do a fit
[p_o,s] = polyfit(bottle_mat(bottom_read,19),bottle_mat(bottom_read,8),1);
[o_fit,o_delta] = polyval(p_o,[0:0.1:1],s);
plot([0:0.1:1],o_fit)
plot([0:0.1:1],o_fit+o_delta,'-.')
plot([0:0.1:1],o_fit-o_delta,'-.')
text(0.6,1.5,['Slope = ' num2str(p_o(1),'%0.2f') ', Int. = ' num2str(p_o(2),'%0.2f')])
