% analysis of background cast (SAM_012)

load SAM_012_down

pr1m=[9:164];
A=[press,temp0,sal0,sigma0,fl,lat,lon];
%B=interpP(A,pr1m,1);
B=interpT(A,pr1m,1);

t0d1m=B(:,2);
s0d1m=B(:,3);
sig0d1m=B(:,4);
fld1m=B(:,5);
latd1m=B(:,6);
lond1m=B(:,7);

load SAM_012_up

A=[press,temp0,sal0,sigma0,fl,lat,lon];
%B=interpP(A,pr1m,1);
B=interpT(A,pr1m,1);

t0u1m=B(:,2);
s0u1m=B(:,3);
sig0u1m=B(:,4);
flu1m=B(:,5);
latu1m=B(:,6);
lonu1m=B(:,7);

mt0=mean([t0d1m  t0u1m]')';
ms0=mean([s0d1m  s0u1m]')';
msig0=mean([sig0d1m  sig0u1m]')';

if 0
  diary mean_12_t
  
  '   pr         sig_d    sig_u    sig_mean'
  
  [pr1m'  sig0d1m  sig0u1m  mean([sig0d1m  sig0u1m]')']

  diary off
end

%%%%%%%%%%%%%%%%
% bottle heights:
bh=[0 6 12 18 24 30 36 42 48 54 60 66 72 78 84 90 96 102 108];

% first bottle is 5m above sled
% mean sled depth = 164.053;
bp=164-(bh+5);  % bottle pressures

for ii = 1:length(bp)
  aa(ii) = find(pr1m == bp(ii));
end

fid=fopen('bottle_bkg.txt','w');
  fprintf(fid,['bh pr      down                        up' ...
	       '                   mean\n']);
  fprintf(fid,['           t0     s0      sig0   t0        s0' ...
	       '     sig0    t0     s0      sig0\n']);

for ii = 1:length(bp)
  fprintf(fid,'%2d %2d %6.4f %6.4f %6.4f  %6.4f %6.4f %6.4f  %6.4f %6.4f %6.4f\n', ...
	  bh(ii),bp(ii),t0d1m(aa(ii)),s0d1m(aa(ii)), ...
	  sig0d1m(aa(ii)),t0u1m(aa(ii)),s0u1m(aa(ii)), ...
	  sig0u1m(aa(ii)),mt0(aa(ii)),ms0(aa(ii)), ...
	  msig0(aa(ii)));

end
fclose(fid);
