-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathbinRBR.m
95 lines (73 loc) · 2.12 KB
/
binRBR.m
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
function out = binRBR(in,by,binWidth)
% bin average RBR profiles
%
% out = binRBR(in,by,binWidth);
%
% where
% in : structure of RBR profiler data (i.e., created
% by output from rbrExtractVals.m)
% by : is a string specifying how to bin the data
% ('depth' or 'pressure')
% binWidth : is the bin width
%
% note: if by = 'depth', and depth hasn't yet been calculated, then
% depth is calculated from pressure and latitude using the GSW
% function gsw_z_from_p
%% for testing
% in = sbe;
% % by = 'depth';
% by = 'pressure';
% binWidth = 10;
%% develop a list of sensors to bin
vars = fieldnames(in);
ind = [];
for k=1:length(vars),
if isnumeric(in.(vars{k})) & numel(in.(vars{k}))>1,
ind = [ind; k];
end
end
vars = vars(ind);
% of course we don't want to bin pressure or depth
vars = vars(~strcmp(vars,{'Pressure'}));
vars = vars(~strcmp(vars,{'Depth'}));
out = in;
switch by
case 'pressure'
binCenter = [binWidth:binWidth:ceil(max(in.Pressure))]';
out.Pressure = binCenter;
unit = 'dbar'; % for processing log text
case 'depth'
in.Depth = -gsw_z_from_p(in.Pressure,52);
binCenter = [binWidth:binWidth:ceil(max(in.Depth))]';
out.Depth = binCenter;
out.Pressure = gsw_p_from_z(-out.Depth,52);
unit = 'm'; % for processing log text
end
out.units(end+1) = {'m'};
% initialize the binned output fields
for k=1:length(vars),
out.(vars{k}) = NaN(length(binCenter),1);
end
for k=1:length(binCenter),
switch by
case 'pressure'
kk = in.Pressure>=binCenter(k)-binWidth/2 & ...
in.Pressure< binCenter(k)+binWidth/2;
case 'depth'
kk = in.Depth>=binCenter(k)-binWidth/2 & ...
in.Depth< binCenter(k)+binWidth/2;
end
if any(kk),
for j=1:length(vars),
out.(vars{j})(k) = nanmean(in.(vars{j})(kk));
end
end
end
%% append processing log
if isfield(in,'processingLog');
nlog = length(in.processingLog);
else
nlog = 0;
end
out.processingLog(nlog+1) = {['Data binned by ' by ' to ' num2str(binWidth) ...
' ' unit ' intervals']};