-
Notifications
You must be signed in to change notification settings - Fork 6
/
Signature_Reynolds_Stress_5Beam.m
281 lines (217 loc) · 6.64 KB
/
Signature_Reynolds_Stress_5Beam.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
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
% Signature 5-Beam Reynolds Stresses
% Uses RS_5beam.m to estimate the RS
% RS are in Earth coordinates using instrument heading
% -------------
% Modified on May 25th 2017
% Pitch and roll input signs to RS calculation to match Dewey & Stringer (2007)
% Figure 2 and equations 14 to 17.
% -------------
% -------------
% Modified on July 2019
% Added a note on the sign of the along-beam velocities from a Nortek
% Signature
% They are positive away from the instrument
% Sign does not matter for variance calculations
% Added a note on using heading for rotation: ideally beam pairs should
% be aligned with the flow main direction to avoid any rotation
% -------------
clc
clear
close all
% Data:
% Needs along beam velocities for all depths as bi=bi(z,time) for each burst
% Needs pitch and roll for each burst
% Needs u and v also in u(z,time) form for each burst
% We have data saved for each time burst for each bin, we need
% to open each file and form b(depth,time);
% Where is the QC Data:
fpath = ['/Users/Maru/Documents/PhD_UW/5BeamCodes/BinDataSignature'];
prefix = ['SignatureData_QC_Bin'];
%Where to safe the RS
savepath = ['/Users/Maru/Documents/PhD_UW/5BeamCodes/Signature_Production'];
% Bins
Nbin=20;
% Ensembles to use
Ens1=10; %First
EnsN=43; %Last
% Theta: beam slanted angle
theta=25; %Degrees
% Noise in m/s
n_sig=0.0265; % Check noise for every deployment
for bin=1:Nbin
fname = [prefix int2str(bin) '.mat'];
load([ fpath '/' fname ])
fs=SigData.fs;
dt=1/fs;
Ens_time(:,bin)=SigData.Ens_time; %Beggining of each ensemble
[Nt Nens]=size(SigData.vbeam1);
time0=[0:dt:Nt*dt-dt]; % Seconds
fik=1;
for fi=Ens1:EnsN %Good ensembles
v1(bin,:,fik)=SigData.vbeam1(:,fi);
v2(bin,:,fik)=SigData.vbeam2(:,fi);
v3(bin,:,fik)=SigData.vbeam3(:,fi);
v4(bin,:,fik)=SigData.vbeam4(:,fi);
v5(bin,:,fik)=SigData.vbeam5(:,fi);
u_x(bin,:,fik)=SigData.u_x(:,fi);
v_y(bin,:,fik)=SigData.v_y(:,fi);
% Need Pitch and Roll and Heading
pitch(:,fik)=SigData.Pitch(:,fi);
roll(:,fik)=SigData.Roll(:,fi);
heading(:,fik)=SigData.Heading(:,fi);
timeEns=Ens_time(fi,bin)+time0/3600/24; %days
timeV(bin,:,fik)=timeEns;
fik=fik+1;
end
z=SigData.range;
end
%% Reynolds Stresses in time
[Nz Nt Nens]=size(timeV);
% estimate RS [m2/s2] from beam data
for fi=1:Nens
% Add a minus sign for convension with Dewey & Stringer paper
% Nortek beam velocities are positive away from instrument head
% But, the sign does not matter for estimating variances.
b1=v1(:,:,fi);
b2=v2(:,:,fi);
b3=v3(:,:,fi);
b4=v4(:,:,fi);
b5=v5(:,:,fi);
u=u_x(:,:,fi);
v=v_y(:,:,fi);
Uh(:,fi)=nanmean(sqrt(u.^2+v.^2),2);
r=roll(:,fi);
p=pitch(:,fi);
h=heading(:,fi);
% In here we call the Reynolds Stress function from Dewey & Stringer
% 2007, we need to be consistent with the beams and the tilt angles:
[uu(:,fi) vv(:,fi) ww(:,fi) uw(:,fi) vw(:,fi) uv(:,fi) anisotropy(:,fi) q2(:,fi)]=...
RS_5beam(b1,b3,b4,b2,b5,theta,h,r,-p,u,v);
%pause
fi
end
badRS = find( uu<=0 | vv<=0 | ww<=0);
uu(badRS)=NaN;
vv(badRS)=NaN;
ww(badRS)=NaN;
uw(badRS)=NaN;
uv(badRS)=NaN;
vw(badRS)=NaN;
%% Reynolds Stress Tensor
for i=1:Nens
for j=1:Nbin
Tij=[uu(j,i) uv(j,i) uw(j,i); uv(j,i) vv(j,i) vw(j,i) ; uw(j,i) vw(j,i) ww(j,i)];
% IF the tensor has no NaN values
if ~isnan(Tij(1,1)) & ~isnan(Tij(2,1)) & ~isnan(Tij(3,1)) & ~isnan(Tij(2,2)) & ~isnan(Tij(3,2))
% Estimade Eigenvalues
EV=eig(Tij);
% If any eigenvalue is negative, then tensor is not positive
% definite
if (EV(1)<0 | EV(2)<0 | EV(3)<0)
% Tensor is not positive definite
badTS(j,i)=NaN;
else
badTS(j,i)=0;
end
else
badTS(j,i)=NaN;
end
end
end
% Make Reynolds Stresses NaN for bad tensor:
badT=find(isnan(badTS)==1);
uu(badT)=NaN;
vv(badT)=NaN;
ww(badT)=NaN;
uw(badT)=NaN;
uv(badT)=NaN;
vw(badT)=NaN;
%% Variance Technique
for fi=1:Nens
b1=v1(:,:,fi);
b2=v2(:,:,fi);
b3=v3(:,:,fi);
b4=v4(:,:,fi);
b5=v5(:,:,fi);
h=heading(:,fi);
[uw_vt(:,fi) vw_vt(:,fi)]= RS_VT(b1,b3,b4,b2,25,h);
%pause
fi
end
%% Save results into RS structure
RS.uu=uu;
RS.vv=vv;
RS.ww=ww;
RS.uw=uw;
RS.vw=vw;
RS.uv=uv;
RS.uw_vt=uw_vt;
RS.vw_vt=vw_vt;
%% Local coordinates Rynolds Stresses
% Ideally beam pairs should be aligned with the flow main direction to avoid any rotation
% This must be checked for every channel
alpha=50; % degrees from east to flood clockwise; check this for every channel!
alpha=alpha*pi/180; %radians
RS.uu_lc=RS.uu*cos(alpha)^2+RS.vv*sin(alpha)^2-2*RS.uv*sin(alpha)*cos(alpha);
RS.vv_lc=RS.uu*sin(alpha)^2+RS.vv*cos(alpha)^2+2*RS.uv*sin(alpha)*cos(alpha);
RS.uw_lc=RS.uw*cos(alpha)-RS.vw*sin(alpha);
RS.vw_lc=RS.uw*sin(alpha)+RS.vw*cos(alpha);
RS.ww_lc=RS.ww;
RS.uv_lc=(RS.uu-RS.vv)*sin(alpha)*cos(alpha)+RS.uv*(cos(alpha)^2-sin(alpha)^2);
RS.uw_vt_lc=RS.uw_vt*cos(alpha)-RS.vw_vt*sin(alpha);
RS.vw_vt_lc=RS.uw_vt*sin(alpha)+RS.vw_vt*cos(alpha);
% TKE
RS.k=q2; %Total Kinetic energy
RS.k_sum=0.5*(RS.uu+RS.vv+RS.ww);
RS.k_sum_lc=0.5*(RS.uu_lc+RS.vv_lc+RS.ww_lc);
RS.anisotropy=anisotropy;
%% Noise removal
M_sig=Nt; %burst length
var_sig=n_sig^4/(M_sig*(sin(2*theta*pi/180))^2);
sigma_sig=sqrt(var_sig);
% Remove noise:
RS.uu_lc_nr=RS.uu_lc-sigma_sig;
RS.vv_lc_nr=RS.vv_lc-sigma_sig;
RS.ww_lc_nr=RS.ww_lc-sigma_sig;
RS.uw_lc_nr=RS.uw_lc-sigma_sig;
RS.vw_lc_nr=RS.vw_lc-sigma_sig;
RS.uv_lc_nr=RS.uv_lc-sigma_sig;
RS.uw_vt_lc_nr=RS.uw_vt_lc-sigma_sig;
RS.vw_vt_lc_nr=RS.vw_vt_lc-sigma_sig;
%% Save File
savefile=[savepath '/ReynoldsStress.mat'];
save(savefile, 'RS')
%% Plots
cmap=colormap;
figure(1)
clf
plot(nanmean(uw,2),z,'r','LineWidth',2)
hold on
%semilogx(nanmean(-uw_simp,2),z,'g','LineWidth',2)
for i=1:Nens
cindex=(floor(nanmean(Uh(:,i))/2*64));
plot(uw(:,i),z,'color',cmap(cindex,:))
end
hold off
xlabel('uw')
ylabel('Z (m)')
figure(2)
clf
subplot(1,2,1)
semilogx(nanmean(abs(RS.uw_lc_nr),2),z,'r','LineWidth',2)
hold on
semilogy(nanmean(abs(RS.uw_vt_lc_nr),2),z,'b','LineWidth',2)
hold off
xlabel('uw')
ylabel('Z (m)')
legend('5 beam','VT')
axis([10^-3 10^-2 0 20])
subplot(1,2,2)
semilogx(nanmean(abs(RS.vw_lc_nr),2),z,'r','LineWidth',2)
hold on
semilogx(nanmean(abs(RS.vw_vt_lc_nr),2),z,'b','LineWidth',2)
hold off
xlabel('vw')
ylabel('Z (m)')
legend('5 beam','VT')
axis([10^-3 10^-2 0 20])