%clear all;close all;
%load ab1202_db
load rv1203_db
% extract the data that you want from the structure
%  this routine assumes that your structure is in a very specific format
%  could be made more general to search btl.names to find the appropriate
%  column for the data you want.
station = [];
oxyv_primary = [];
oxyv_secondary = [];
oxyml_pri = [];
oxyml_sec = [];
theta_pri = [];
theta_sec = [];
sal_pri = [];
sal_sec = [];
%t_secondary = [];
pr = [];


for II = 1: length(btl)
    station = [station; II*ones(length(btl(II).data(:,3)),1)];
    
    pr = [pr; btl(II).data(:,12)];
    
    theta_pri = [theta_pri; btl(II).data(:,3)];
    %t_primary = [t_primary; btl(II).data(:,13)];
    theta_sec = [theta_sec; btl(II).data(:,4)];
    %t_secondary = [t_secondary; btl(II).data(:,14)];
    
    sal_pri = [sal_pri; btl(II).data(:,5)];
    sal_sec = [sal_sec; btl(II).data(:,6)];
    
    oxyv_primary = [oxyv_primary; btl(II).data(:,17)];
    oxyv_secondary = [oxyv_secondary; btl(II).data(:,18)];
    
    oxyml_pri = [oxyml_pri; btl(II).data(:,7)];
    oxyml_sec = [oxyml_sec; btl(II).data(:,8)];
       
end
    oxyml_sec(1:24)=oxyml_pri(1:24);
    oxymm_pri = ox_units(oxyml_pri,sal_pri,theta_pri,'mll2mmk');
    oxymm_sec = ox_units(oxyml_sec,sal_sec,theta_sec,'mll2mmk');

% 
% extract only the data below 500 dbar
%
II = find(pr >= 200.0);
deep_station = station(II);
deep_oxyv_primary = oxyv_primary(II);
deep_oxyv_secondary = oxyv_secondary(II);
deep_oxymm_pri = oxymm_pri(II);
deep_oxymm_sec = oxymm_sec(II);
deep_pr = pr(II);

mean_oxyv_diff = mean(deep_oxyv_primary-deep_oxyv_secondary);
stddev_oxyv_diff = std(deep_oxyv_primary-deep_oxyv_secondary);
median_oxyv_diff = median(deep_oxyv_primary-deep_oxyv_secondary);
iqr_oxyv_diff = iqr(deep_oxyv_primary-deep_oxyv_secondary)/1.349;
var_median_oxyv_diff = var_median(deep_oxyv_primary-deep_oxyv_secondary)/1.349;

median_oxymm_diff = median(deep_oxymm_pri-deep_oxymm_sec)
stddev_oxymm_diff = std(deep_oxymm_pri-deep_oxymm_sec)

%KK = find (deep_station >= 29 & deep_station <= 33);

%
% Now make the figure that you want
%
figure;
subplot(2,1,1)
plot(deep_station, deep_oxymm_pri-deep_oxymm_sec,'+')
  h=gca;
  %set(h,'ylim',[-0.0032 0.0032])
  xlimits = get(h,'xlim');
  xlabel('Station number ','fonts',16)
  ylabel('Primary-Secondary ','fonts',16)
  title('Oxygen ({\mu}mol/kg) Prs > 200','fonts',16)
  font(14)
  hold on;
  plot(xlimits, [median_oxymm_diff median_oxymm_diff],'r')
  plot(xlimits, [median_oxymm_diff-stddev_oxymm_diff median_oxymm_diff-stddev_oxymm_diff],'r--')
  plot(xlimits, [median_oxymm_diff+stddev_oxymm_diff median_oxymm_diff+stddev_oxymm_diff],'r--')

subplot(2,1,2)
  plot(deep_pr, deep_oxymm_pri-deep_oxymm_sec,'+')
  h=gca;
  %set(h,'ylim',[-0.0032 0.0032])
  xlimits = get(h,'xlim');
  xlabel('Pressure, dbar ','fonts',16)
  ylabel('Primary-Secondary ','fonts',16)
  title('Oxygen ({\mu}mol/kg) Prs > 200','fonts',16)
  font(14)
  hold on;
  plot(xlimits, [median_oxymm_diff median_oxymm_diff],'r')
  plot(xlimits, [median_oxymm_diff-stddev_oxymm_diff median_oxymm_diff-stddev_oxymm_diff],'r--')
  plot(xlimits, [median_oxymm_diff+stddev_oxymm_diff median_oxymm_diff+stddev_oxymm_diff],'r--')

    print('-depsc','figures/oxy_sensor_diff_gom')

%return
figure;
  plot(station, oxyv_primary-oxyv_secondary,'+')
  
figure;
font(14)
  %subplot(2,1,1)
  plot(deep_station, deep_oxyv_primary-deep_oxyv_secondary,'+')
  h=gca;
  %set(h,'ylim',[-0.0032 0.0032])
  xlimits = get(h,'xlim');
  xlabel('Station number (V)','fonts',16)
  ylabel('Primary-Secondary','fonts',16)
  title('Oxygen','fonts',16)
  font(14)
  hold on;
  plot(xlimits, [median_oxyv_diff median_oxyv_diff],'r')
  plot(xlimits, [median_oxyv_diff-stddev_oxyv_diff median_oxyv_diff-stddev_oxyv_diff],'r--')
  plot(xlimits, [median_oxyv_diff+stddev_oxyv_diff median_oxyv_diff+stddev_oxyv_diff],'r--')
  
  %plot(xlimits, [mean_oxyv_diff mean_oxyv_diff],'g')
  %plot(xlimits, [mean_oxyv_diff-stddev_oxyv_diff mean_oxyv_diff-stddev_oxyv_diff],'g--')
  %plot(xlimits, [mean_oxyv_diff+stddev_oxyv_diff mean_oxyv_diff+stddev_oxyv_diff],'g--')

  %plot(deep_station(KK), deep_oxyv_primary(KK)-deep_oxyv_secondary(KK),'k+')
  figure
  font(14)
  %subplot(2,1,2)
  plot(deep_pr, deep_oxyv_primary-deep_oxyv_secondary,'+')
  h=gca;
  %set(h,'ylim',[-0.0032 0.0032])
  xlimits = get(h,'xlim');
  xlabel('Pressure, dbar','fonts',16)
  ylabel('Primary-Secondary (V)','fonts',16)
    title('Oxygen','fonts',16)

  font(14)
  %title('Potential Temperature')
  hold on;
  plot(xlimits, [median_oxyv_diff median_oxyv_diff],'r')
  plot(xlimits, [median_oxyv_diff-stddev_oxyv_diff median_oxyv_diff-stddev_oxyv_diff],'r--')
  plot(xlimits, [median_oxyv_diff+stddev_oxyv_diff median_oxyv_diff+stddev_oxyv_diff],'r--')
  %plot(deep_pr(KK), deep_oxyv_primary(KK)-deep_oxyv_secondary(KK),'k+')


