load ab1209_db
%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 = [];
co_primary = [];
sal_primary = [];
co_secondary = [];
sal_secondary = [];
pr = [];
for II = 1: length(btl)
    station = [station; II*ones(length(btl(II).data(:,15)),1)];
    co_primary = [co_primary; btl(II).data(:,15)];
    sal_primary = [sal_primary; btl(II).data(:,5)];
    co_secondary = [co_secondary; btl(II).data(:,16)];
    sal_secondary = [sal_secondary; btl(II).data(:,6)];
    pr = [pr; btl(II).data(:,12)];
end

% 
% extract only the data below 500 dbar
%
II = find(pr >= 500.0);
deep_station = station(II);
deep_co_primary = co_primary(II);
deep_sal_primary = sal_primary(II);
deep_co_secondary = co_secondary(II);
deep_sal_secondary = sal_secondary(II);
deep_pr = pr(II);

mean_co_diff = nanmean(deep_co_primary-deep_co_secondary);
stddev_co_diff = nanstd(deep_co_primary-deep_co_secondary);
median_co_diff = nanmedian(deep_co_primary-deep_co_secondary);
iqr_co_diff = iqr(deep_co_primary-deep_co_secondary)/1.349;

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

%
% Now make the figure that you want
%

figure;
  plot(station, co_primary-co_secondary,'+')
  
figure;
  subplot(2,1,1)
  plot(deep_station, deep_co_primary-deep_co_secondary,'+')
  h=gca;
  %set(h,'ylim',[0.00012 0.00072])
  xlimits = get(h,'xlim');
  xlabel('Station number','fonts',14)
  ylabel('Primary-Secondary','fonts',14)
  title('Conductivity (Prs > 200 db) ','fonts',14)
  font(14)
  hold on;
  plot(xlimits, [median_co_diff median_co_diff],'r')
  plot(xlimits, [median_co_diff-stddev_co_diff median_co_diff-stddev_co_diff],'r--')
  plot(xlimits, [median_co_diff+stddev_co_diff median_co_diff+stddev_co_diff],'r--')
  %plot(deep_station(KK), deep_co_primary(KK)-deep_co_secondary(KK),'k+')
  
  subplot(2,1,2)
  plot(deep_pr, deep_co_primary-deep_co_secondary,'+')
  h=gca;
  %set(h,'ylim',[0.00012 0.00072])
  xlimits = get(h,'xlim');
  xlabel('Pressure, dbar','fonts',14)
  ylabel('Primary-Secondary','fonts',14)
  ylim(-0.003,0.003)
  %title('Conductivity')
  font(14)
  hold on;
  plot(xlimits, [median_co_diff median_co_diff],'r')
  plot(xlimits, [median_co_diff-stddev_co_diff median_co_diff-stddev_co_diff],'r--')
  plot(xlimits, [median_co_diff+stddev_co_diff median_co_diff+stddev_co_diff],'r--')
  %plot(deep_pr(KK), deep_co_primary(KK)-deep_co_secondary(KK),'k+')
  
  print('-depsc','figures/co_sensor_diff_gom')

 