-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathestimate_mld.m
More file actions
338 lines (328 loc) · 10.4 KB
/
estimate_mld.m
File metadata and controls
338 lines (328 loc) · 10.4 KB
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
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
function [ mld, mld_i ] = estimate_mld( z, varargin )
%ESTIMATE_MLD Estimates mixer layer depth (MLD)
% The MLD is estimated directly from profiles given in input
% Various criterion can be used to determine the MLD this function use by
% default the fixed criterion in density of 0.03kg/m^3 difference from
% surface. A fixed criterion in temperature (0.2 C) or a variable
% criterion in density (which enables the definition of BLT) can also be
% used.
%
% Inputs:
% Required:
% z Nx1 double array of depth profile in m
% Optional:
% rho Nx1 double array of in-situ density in kg/m^3
% t Nx1 double array of Conservative Temperature (ITS-90) in deg C
% s Nx1 double array of Absolute Salinity in g/kg
% method string containing the method to use:
% fixed_temperature threshold
% fixed_density threshold (default)
% variable_density threshold
% fixed_density_gradient
% average (average of value obtain by 4 previous methods)
% robust (median of value obtain by 4 previous methods)
% criterion double containing the criterion
% 0.2 degre C by default if fixed_temperature method
% 0.03 kg/m^3 by default if fixed_density method
%
% Outputs:mld double containing the estimate MLD
% mld_i integer containing the index of the estimate of the MLD
% for the sorted depth with shallow depth at top
%
% Examples:
% % Get MLD from a density profile rho
% mld = estimate_mld(z, rho);
% % or
% mld = estimate_mld(z, rho, 'fixed_density');
%
% % Get MLD from a temperature profile t
% mld = estimate_mld(z, t, 'fixed_temperature');
%
% % Get MLD with a custom criterion custom_criterion
% mld = estimate_mld(z, t, 'fixed_temperature', custom_criterion);
% % or
% mld = estimate_mld(z, rho, custom_criterion);
%
% % Get MLD with a variable density threshold criterion
% mld = estimate_mld(z, rho, theta, sa, 'variable_density');
%
% % Get MLD with a fixed density gradient criterion
% mld = estimate_mld(z, rho, 'fixed_density_gradient');
%
% % Get MLD with the average of all the methods
% mld = estimate_mld(z, rho, theta, sa, 'average');
%
% % Get MLD with the median of all the methods
% mld = estimate_mld(z, rho, theta, sa, 'robust');
%
% Required:
% Gibbs Seawater Oceanographic Toolbox (GSW)
% gsw_rho(si(i),thetai(j),0);
%
% Tested with: Matlab R2015a
%
% Author: Nils Haentjens, Ms, University of Maine
% Email: nils.haentjens@maine.edu
% Created: 26th May 2015
% Last update: 26th May 2015
%
% References:
% Zawada, D. G., Zaneveld, J. R. V, Boss, E., Gardner, W. D.,
% Richardson, M. J., & Mishonov, A. V. (2005). A comparison of
% hydrographically and optically derived mixed layer depths.
% Journal of Geophysical Research: Oceans, 110(11), 1?13.
% http://doi.org/10.1029/2004JC002417
% http://www.ifremer.fr/cerweb/deboyer/mld/Surface_Mixed_Layer_Depth.php
% Check input
if nargin > 6
error('Too many input arguments')
elseif nargin < 1
error('Not enough input arguments')
end
% Check options
% Default arguments
method = 'fixed_density';
rho_criterion = 0.03; % kg/m^3
theta_criterion = 0.2; % degre C
density_gradient_criterion = 0.01; % kg/m^3
z_ref = 10; % m % reference depth
% Get method from arguments
for i=1:nargin-1;
if ischar(varargin{i});
if strcmp(varargin{i}, 'fixed_temperature') ||...
strcmp(varargin{i}, 'fixed_density') ||...
strcmp(varargin{i}, 'variable_density') ||...
strcmp(varargin{i}, 'fixed_density_gradient') ||...
strcmp(varargin{i}, 'average') ||...
strcmp(varargin{i}, 'robust');
method = varargin{i};
else
warning('Unknown method %s', varargin{i});
end;
end;
end;
% Load other arguments
i=1;
while i <= nargin-1;
if ismatrix(varargin{i}) && ~ischar(varargin{i}) && ~isscalar(varargin{i});
if strcmp(method, 'fixed_density')
rho = varargin{i};
elseif strcmp(method, 'fixed_temperature')
theta = varargin{i};
elseif strcmp(method, 'variable_density') || strcmp(method, 'average') || strcmp(method, 'robust')
rho = varargin{i};
theta = varargin{i+1};
sa = varargin{i+2};
i=i+2;
elseif strcmp(method, 'fixed_density_gradient')
rho = varargin{i};
else
error('Unknown method');
end;
elseif isscalar(varargin{i});
if strcmp(method, 'fixed_density')
rho_criterion = varargin{i};
elseif strcmp(method, 'fixed_temperature') || strcmp(method, 'variable_density')
theta_criterion = varargin{i};
elseif strcmp(method, 'fixed_density_gradient')
density_gradient_criterion = varargin{i};
elseif strcmp(method, 'average') || strcmp(method, 'robust')
warning('Default criterion will be used for every method');
else
error('Unknown method');
end;
elseif ~ischar(varargin{i})
error('Unknown argument format');
end;
i=i+1;
end;
%% Format Profiles
% Set orientation to Nx1 array if necessary
s = size(z);
if s(2) > s(1)
z = z';
end;
[z, z_i] = sort(z);
iz_nan = isnan(z);
z(iz_nan) = [];
if exist('rho', 'var');
s = size(rho);
if s(2) > s(1)
rho = rho';
end;
end;
if exist('theta', 'var');
s = size(theta);
if s(2) > s(1)
theta = theta';
end;
end;
if exist('sa', 'var');
s = size(theta);
if s(2) > s(1)
sa = sa';
end;
end;
% Set order according to depth to be sure interp1 works properly
% Shallow first and deeper last
% Remove NaN values
if exist('rho', 'var');
rho = rho(z_i);
rho(iz_nan) = [];
ir_nan = isnan(rho);
z(ir_nan) = [];
rho(ir_nan)=[];
end;
if exist('theta', 'var');
theta = theta(z_i);
theta(iz_nan) = [];
if exist('rho','var') && ~isempty(ir_nan);
theta(ir_nan) = [];
end;
it_nan = isnan(theta);
z(it_nan) = [];
if exist('rho', 'var');
rho(it_nan)=[];
end;
theta(it_nan) = [];
end;
if exist('sa', 'var');
sa = sa(z_i);
sa(iz_nan) = [];
if exist('ir_nan','var') && ~isempty(ir_nan);
sa(ir_nan) = [];
end;
if exist('ir_nan','var') && ~isempty(it_nan);
sa(it_nan) = [];
end;
is_nan = isnan(theta);
z(is_nan) = [];
if exist('rho', 'var');
rho(is_nan)=[];
end;
if exist('theta', 'var');
theta(is_nan)=[];
end;
sa(is_nan) = [];
end;
%% Find MLD according to the selected method
switch method
case 'fixed_temperature'
% Kara Isothermal Layer Depth (ILD)
% Find temperature at reference depth (theta_ref)
[zu, zui] = unique(z); theta_u = theta(zui);
if size(zu,1) ~= 1;
theta_ref = interp1(zu, theta_u, z_ref,'linear','extrap');
else
theta_ref = nanmean(theta_u);
end;
% Find depth where theta = theta_ref +- theta_criterion
[i i]=min(abs(z-z_ref)); % Start at index of z_ref
while i < size(theta,1) && abs(theta(i) - theta_ref) < theta_criterion;
i = i + 1;
end;
if 1 < i && i < size(theta,1);
mld = interp1([theta(i-1) theta(i)], [z(i-1) z(i)], theta_ref-theta_criterion); % +
if isnan(mld)
mld = interp1([theta(i-1) theta(i)], [z(i-1) z(i)], theta_ref+theta_criterion); % -
end;
mld_i = i;
else
warning('Unable to find MLD with fixed temperature threshold');
mld = NaN;
mld_i = NaN;
end;
case 'fixed_density'
% Levitus Density Difference (Lev)
% Find density at reference depth (theta_ref)
[zu, zui] = unique(z); rho_u = rho(zui);
if size(zu,1) ~= 1;
rho_ref = interp1(zu, rho_u, z_ref,'linear','extrap');
else
rho_ref = nanmean(rho_u);
end;
% Find depth by interp
% mld = interp1(rho, z, rho_criterion + rho_ref); % might find it way to low
% mld_i = min(abs(z-mld));
% Find depth where rho = rho_ref + rho_criterion by incrementation
[i i]=min(abs(z-z_ref)); % Start at index of z_ref
while i < size(rho,1) && rho(i) < rho_criterion + rho_ref;
i = i + 1;
end;
if i > 1
if rho(i-1) ~= rho(i)
mld= interp1([rho(i-1) rho(i)], [z(i-1) z(i)], rho_ref+rho_criterion);
else
mld=nanmean([z(i-1) z(i)]);
end;
else
warning('utils:misc_lab:estimate_mld:fixed_density', 'MLD is first value available of profile');
mld = z(1);
end;
mld_i = i;
case 'variable_density'
% Kara Density Difference (Kara)
% Interpolate reference values
[zu, zui] = unique(z);
sa_u = sa(zui); theta_u = theta(zui); rho_u = rho(zui);
if size(zu,1) ~= 1;
sa_ref = interp1(zu, sa_u, z_ref,'linear','extrap');
theta_ref = interp1(zu, theta_u, z_ref,'linear','extrap');
rho_ref = interp1(zu, rho_u, z_ref,'linear','extrap');
else
sa_ref = nanmean(sa_u);
theta_ref = nanmean(theta_u);
rho_ref = nanmean(rho_u);
end;
[i i]=min(abs(z-z_ref)); % Start at index of z_ref
% Compute variable density criterion threshold
delta_rho = gsw_rho(sa_ref,theta_ref-theta_criterion,0) - gsw_rho(sa_ref,theta_ref,0);
% Find MLD
while i < size(rho,1) && rho(i) < delta_rho + rho_ref;
i = i + 1;
end;
if i > 1
if rho(i-1) ~= rho(i)
mld= interp1([rho(i-1) rho(i)], [z(i-1) z(i)], rho_ref+delta_rho);
else
mld= nanmean([z(i-1) z(i)]);
end;
else
warning('utils:misc_lab:estimate_mld:variable_density', 'MLD is first value available of profile');
mld = z(1);
end;
mld_i = i;
case 'fixed_density_gradient'
% Lukas-Lindstorm Fixed Density Gradient (fxGrad)
% Interpolate rho every step (2 m)
step = 2;
[zu, zui] = unique(z); rho_u = rho(zui);
if size(zu,1) ~= 1;
rho2(:,1) = interp1(zu, rho_u, z_ref:2:z(end),'linear','extrap');
else
rho2(:,1) = nanmean(rho_u);
end;
i=1;
while i < size(rho2,1) && rho2(i) < density_gradient_criterion + rho2(1);
i = i + 1;
end;
mld = z_ref + step * i;
[mld_i mld_i] = min(abs(z-mld)); % Should be the shallower and not the minimum
case 'average'
mld(4) = estimate_mld(z, theta, 'fixed_temperature');
mld(3) = estimate_mld(z, rho, 'fixed_density');
mld(2) = estimate_mld(z, rho, theta, sa, 'variable_density');
mld(1) = estimate_mld(z, rho, 'fixed_density_gradient');
mld = mean(mld);
[mld_i mld_i] = min(abs(z-mld));
case 'robust'
mld(4) = estimate_mld(z, theta, 'fixed_temperature');
mld(3) = estimate_mld(z, rho, 'fixed_density');
mld(2) = estimate_mld(z, rho, theta, sa, 'variable_density');
mld(1) = estimate_mld(z, rho, 'fixed_density_gradient');
mld = nanmedian(mld);
[mld_i mld_i] = min(abs(z-mld));
otherwise
error('Unknown method');
end;
end