-
Notifications
You must be signed in to change notification settings - Fork 0
/
DiseaseAtSea_dive_stat_analysis_6018_V3.m
428 lines (366 loc) · 15.7 KB
/
DiseaseAtSea_dive_stat_analysis_6018_V3.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
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
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
%Created by: Rachel Holser ([email protected]) and Arina Favilla
%Last Updated: 18-Sep-2022
%Completes calculations needed and generates Figures 4 and 5 of Holser et
%al. 2022
%Requires:
% DayOfTripColormap.mat
% findseq.m
% (https://www.mathworks.com/matlabcentral/fileexchange/28113-findseq)
%% Load data from .mat files for PB2017 deployments
load('2017001_6018_RRHmod_TV3.mat')
A=load('2017002_U 20_RRHmod_TV4_alpha_2.mat');
B=load('2017003_5712_RRHmod_TV4_alpha_2.mat');
C=load('2017004_B794_RRHmod_TV4_alpha_2.mat');
D=load('2017005_6871_RRHmod_TV4_alpha_2.mat');
%E=load('2017006_5950_RRHmod_TV3.mat'); - no dive data
F=load('2017007_6767_RRHmod_TV4_alpha_2.mat');
G=load('2017008_7238_RRHmod_TV4_alpha_2.mat');
H=load('2017009_6260_RRHmod_TV4_alpha_2.mat');
%% Figure 5 - Kernel densities of dive statistics, calculated day and night
%Define grid over which to estimate density for Duration vs Depth
gridx1=0:10:1000; %depths
gridx2=0:2:40; %duration (minutes)
[X,Y] = meshgrid(gridx1, gridx2);
x1 = X(:);
x2 = Y(:);
xi = [x1 x2];
%Create data array for ksdensity calculation, using NIGHT dives only (based
%on solar elevation). Data in array needs to match the xi dimensions
data=[A.DiveStat.Maxdepth(A.DiveStat.SolarEl<0) A.DiveStat.Dduration(A.DiveStat.SolarEl<0)/60];
data=[data;B.DiveStat.Maxdepth(B.DiveStat.SolarEl<0) B.DiveStat.Dduration(B.DiveStat.SolarEl<0)/60;...
C.DiveStat.Maxdepth(C.DiveStat.SolarEl<0) C.DiveStat.Dduration(C.DiveStat.SolarEl<0)/60;...
F.DiveStat.Maxdepth(F.DiveStat.SolarEl<0) F.DiveStat.Dduration(F.DiveStat.SolarEl<0)/60;...
G.DiveStat.Maxdepth(G.DiveStat.SolarEl<0) G.DiveStat.Dduration(G.DiveStat.SolarEl<0)/60];
%Calculate kernel density of NIGHT duration against depth for all other 2017 animals
[f,xi2] = ksdensity(data,xi,'Function','pdf');
%Reshape ksdensity output for contour
count=1;
for i=1:size(Y,2)
fn(:,i)=f(count:count+20);
count=count+21;
end
%Create data array for ksdensity calculation, using DAYTIME dives only
%(based on solar elevation). Data in array needs to match the xi dimensions
data2=[A.DiveStat.Maxdepth(A.DiveStat.SolarEl>=0) A.DiveStat.Dduration(A.DiveStat.SolarEl>=0)/60];
data2=[data2;B.DiveStat.Maxdepth(B.DiveStat.SolarEl>=0) B.DiveStat.Dduration(B.DiveStat.SolarEl>=0)/60;...
C.DiveStat.Maxdepth(C.DiveStat.SolarEl>=0) C.DiveStat.Dduration(C.DiveStat.SolarEl>=0)/60;...
F.DiveStat.Maxdepth(F.DiveStat.SolarEl>=0) F.DiveStat.Dduration(F.DiveStat.SolarEl>=0)/60;...
G.DiveStat.Maxdepth(G.DiveStat.SolarEl>=0) G.DiveStat.Dduration(G.DiveStat.SolarEl>=0)/60];
%Calculate kernel density of DAY duration against depth for all other 2017 animals
[f2,xi22] = ksdensity(data2,xi);
%Reshape ksdensity output for contour
count=1;
for i=1:size(Y,2)
fd(:,i)=f2(count:count+20);
count=count+21;
end
%Define grid over which to estimate kernel density for PDI~Duration
gridx1=0:0.1:100; %PDI
gridx2=0:2:40; %duration (minutes)
[X2,Y2] = meshgrid(gridx1, gridx2);
x1 = X2(:);
x2 = Y2(:);
xi = [x1 x2];
%pull out DAY PDI and duration data (columns need to match order in xi above)
dataDay=[A.DiveStat.PDI(A.DiveStat.SolarEl>=0)/60 A.DiveStat.Dduration(A.DiveStat.SolarEl>=0)/60];
dataDay=[dataDay;B.DiveStat.PDI(B.DiveStat.SolarEl>=0)/60 B.DiveStat.Dduration(B.DiveStat.SolarEl>=0)/60;...
C.DiveStat.PDI(C.DiveStat.SolarEl>=0)/60 C.DiveStat.Dduration(C.DiveStat.SolarEl>=0)/60;...
D.DiveStat.PDI(D.DiveStat.SolarEl>=0)/60 D.DiveStat.Dduration(D.DiveStat.SolarEl>=0)/60;...
F.DiveStat.PDI(F.DiveStat.SolarEl>=0)/60 F.DiveStat.Dduration(F.DiveStat.SolarEl>=0)/60;...
G.DiveStat.PDI(G.DiveStat.SolarEl>=0)/60 G.DiveStat.Dduration(G.DiveStat.SolarEl>=0)/60];
[fD,xi2] = ksdensity(dataDay,xi,'Function','pdf');
%reshape output array for use in contour plot
count=1;
for i=1:size(Y2,2)
fDb(:,i)=fD(count:count+20);
count=count+21;
end
%pull out NIGHT PDI and duration data (columns need to match order in xi above)
dataNight=[A.DiveStat.PDI(A.DiveStat.SolarEl<0)/60 A.DiveStat.Dduration(A.DiveStat.SolarEl<0)/60];
dataNight=[dataNight;B.DiveStat.PDI(B.DiveStat.SolarEl<0)/60 B.DiveStat.Dduration(B.DiveStat.SolarEl<0)/60;...
C.DiveStat.PDI(C.DiveStat.SolarEl<0)/60 C.DiveStat.Dduration(C.DiveStat.SolarEl<0)/60;...
D.DiveStat.PDI(D.DiveStat.SolarEl<0)/60 D.DiveStat.Dduration(D.DiveStat.SolarEl<0)/60;...
F.DiveStat.PDI(F.DiveStat.SolarEl<0)/60 F.DiveStat.Dduration(F.DiveStat.SolarEl<0)/60;...
G.DiveStat.PDI(G.DiveStat.SolarEl<0)/60 G.DiveStat.Dduration(G.DiveStat.SolarEl<0)/60];
[fN,xi2] = ksdensity(dataNight,xi,'Function','pdf');
%reshape output array for use in contour plot
count=1;
for i=1:size(Y2,2)
fNb(:,i)=fN(count:count+20);
count=count+21;
end
%plot kernel density contour lines with 6018 data points scattered on top
%(of night time dives for both). Use 9 contour lines to divide the kernel
%density into 10 equal "slices." Use log scale on y-axis of PDI plots.
load('DayOfTripColormap.mat');
DiveStat=sortrows(DiveStat,8,'descend');
figure(1)
t=tiledlayout(2,2);
t.TileSpacing='compact';
nexttile
ax=gca;
hold on
[con,conax]=contour(Y,X,fd,9);
scatter(DiveStat.Dduration(DiveStat.SolarEl>=0)/60,DiveStat.Maxdepth(DiveStat.SolarEl>=0),20,(DiveStat.JulDate(DiveStat.SolarEl>=0)-min(DiveStat.JulDate)),'filled')
conax.LineWidth=2;
conax.LineColor='k';
ax.Colormap=DayOfTripColormap;
ax.YLabel.String='Dive Depth (m)';
ax.Color='w';
ax.XColor='k';
ax.YColor='k';
ax.YDir='reverse';
ax.FontSize=20;
ax.XLim=[0,35];
ax.YLim=[0,900];
ax.LabelFontSizeMultiplier = 1.2;
ax.Title.Color='w';
ax.CLim=[0,100];
%Plot daytime Duration~PDI
nexttile
ax=gca;
hold on
[con,conax]=contour(Y2,X2,fDb,9);
%50% is 0.061414, 90% is 0.1228, 75% is 0.03070
scatter(DiveStat.Dduration(DiveStat.SolarEl>=0)/60,DiveStat.PDI(DiveStat.SolarEl>=0)/60,20,(DiveStat.JulDate(DiveStat.SolarEl>=0)-min(DiveStat.JulDate)),'filled')
conax.LineWidth=2;
conax.LineColor='k';
ax.Colormap=DayOfTripColormap;
c=colorbar;
c.Color='k';
c.FontSize=20;
c.Label.String='Day of Trip';
ax.Color='w';
ax.XColor='k';
ax.YColor='k';
ax.FontSize=20;
ax.XLim=[0,35];
ax.LabelFontSizeMultiplier = 1.2;
ax.YLabel.String='PDI (min)';
ax.Title.Color='w';
ax.YLim=[0.5,400];
ax.YScale='log';
ax.CLim=[0,100];
%Plot night Depth~Duration
nexttile
ax=gca;
hold on
[con,conax]=contour(Y,X,fn,9);
scatter(DiveStat.Dduration(DiveStat.SolarEl<0)/60,DiveStat.Maxdepth(DiveStat.SolarEl<0),20,(DiveStat.JulDate(DiveStat.SolarEl<0)-min(DiveStat.JulDate)),'filled')
conax.LineWidth=2;
conax.LineColor='k';
ax.Colormap=DayOfTripColormap;
ax.YLabel.String='Dive Depth (m)';
ax.XLabel.String='Dive Duration (min)';
ax.Color='w';
ax.XColor='k';
ax.YColor='k';
ax.YDir='reverse';
ax.FontSize=20;
ax.XLim=[0,35];
ax.YLim=[0,900];
ax.LabelFontSizeMultiplier = 1.2;
ax.Title.Color='w';
ax.CLim=[0,100];
%Plot night Duration~PDI
nexttile
ax=gca;
hold on
[con,conax]=contour(Y2,X2,fNb,9);
scatter(DiveStat.Dduration(DiveStat.SolarEl<0)/60,DiveStat.PDI(DiveStat.SolarEl<0)/60,20,(DiveStat.JulDate(DiveStat.SolarEl<0)-min(DiveStat.JulDate)),'filled')
conax.LineWidth=2;
conax.LineColor='k';
ax.Colormap=DayOfTripColormap;
c1=colorbar;
c1.Color='k';
c1.FontSize=20;
c1.Label.String='Day of Trip';
ax.XLabel.String='Dive Duration (min)';
ax.Color='w';
ax.XColor='k';
ax.YColor='k';
ax.FontSize=20;
ax.XLim=[0,35];
ax.LabelFontSizeMultiplier = 1.2;
ax.YLabel.String='PDI (min)';
ax.Title.Color='w';
ax.YScale='log';
ax.YLim=[0.5,400];
ax.CLim=[0,100];
%% Load temperature data needed for Figure 4
% Setup the Import Options and import the data
opts = delimitedTextImportOptions("NumVariables", 5);
% Specify range and delimiter
opts.DataLines = [26, Inf];
opts.Delimiter = ",";
% Specify column names and types
opts.VariableNames = ["CorrectedDepth", "time", "depth", "etemp", "alight"];
opts.VariableTypes = ["double", "double", "double", "double", "double"];
% Specify file level properties
opts.ExtraColumnsRule = "ignore";
opts.EmptyLineRule = "read";
% Temp data
ts = readtable('2017001_1590113_full_iknos_raw_data.csv', opts);
ts.DateUTC = datetime(ts.time, 'ConvertFrom','datenum'); % keeps fractions of seconds
% remove fractions of seconds
[ts.Year, ts.Month, ts.Day, ts.Hour, ts.Min, ts.Sec]...
=datevec(ts.time);
ts.Sec=round(ts.Sec);
ts.DateUTC = datetime(ts.Year, ts.Month, ts.Day, ts.Hour, ts.Min, ts.Sec);
ts = ts(:,1:6);
% check for NaN in timeseries and remove
NaNdepth = isnan(ts.CorrectedDepth);
if sum(NaNdepth)>0
ts = ts(~NaNdepth,:);
end
clear NaNdepth
% Mat files
load('2017001_6018_RRHmod_TV3.mat');
DiveStat.DayOfTrip = ceil(DiveStat.JulDate)-floor(MetaData.DepartDate);
Track_Best.DateUTC = datetime(datestr(Track_Best.JulDate)); % fix Date format
% Crop ts to match DiveStat
DiveStat.DateUTC = datetime(DiveStat.Year, DiveStat.Month, DiveStat.Day, DiveStat.Hour,DiveStat.Min, DiveStat.Sec);
DSstart = find(ts.DateUTC>=DiveStat.DateUTC(1),1,'first');
DSend = find(ts.DateUTC>=(DiveStat.DateUTC(end)+seconds(DiveStat.Dduration(end))),1,'first');
ts=ts(DSstart:DSend,:);
clear DSstart DSend
%% Fix water temperature data
% (1) Apply correction suggested by Simmons et al. 2009 for MK9 fast-response thermistor
ts.ExtTempShift=ts.etemp;
% Subtract 0.05 degC and apply 1-s time lag (Simmons et al. 2009)
ts.ExtTempShift(2:end)=ts.etemp(1:end-1)-0.05;
ts.WaterTemp=ts.ExtTempShift;
%% Surface Temp Stats
% (2) Determine average water temperature during surface interval
% surface interval defined as ?
water_surface=cell(length(DiveStat.DiveNumber),1);
for w=1:length(DiveStat.DiveNumber)
water_surface{w}=find(ts.DateUTC>=(DiveStat.DateUTC(w)+seconds(DiveStat.Dduration(w))) & ts.DateUTC<=(DiveStat.DateUTC(w)+seconds(DiveStat.Dduration(w))+seconds(DiveStat.PDI(w))));
end
% calculate stats of water surface layer - takes awhile to run
SurfaceTempStats = table('Size', [length(DiveStat.DiveNumber) 8], 'VariableTypes', ...
{'double','double','double','double','double','double','double','double'}, ...
'VariableNames', {'DiveNum','AscPts','DescPts','Avg','Std','Min','Median','Max'});
SurfaceTempStats.DiveNum = DiveStat.DiveNumber;
for i=1:length(DiveStat.DiveNumber)
i
wt = ts.CorrectedDepth(water_surface{i})>0;
% wt = Dive.Dry(water_surface{i})==0;
asc_end = find(wt==0,1,'first')-1;
desc_st = find(wt==0,1,'last')+1;
if ~isempty(desc_st) && desc_st > length(wt)
wt_idx = water_surface{i}([1:asc_end length(wt)]);
else
wt_idx = water_surface{i}([1:asc_end desc_st:length(wt)]);
end
if isempty(wt_idx)
wt_idx = water_surface{i};
else
water_surface{i,2} = wt_idx;
SurfaceTempStats.AscPts(i) = asc_end;
SurfaceTempStats.DescPts(i) = length(wt)-desc_st+1;
SurfaceTempStats.Avg(i) = mean(ts.WaterTemp(wt_idx));
SurfaceTempStats.Std(i) = std(ts.WaterTemp(wt_idx));
SurfaceTempStats.Min(i) = min(ts.WaterTemp(wt_idx));
SurfaceTempStats.Median(i) = median(ts.WaterTemp(wt_idx));
SurfaceTempStats.Max(i) = max(ts.WaterTemp(wt_idx));
ts.WaterTemp(water_surface{i,1}) = SurfaceTempStats.Avg(i);
end
end
%% Bottom temp stats
% surface interval defined as ?
water_bottom=cell(length(DiveStat.DiveNumber),1);
for w=1:length(DiveStat.DiveNumber)
water_bottom{w}=find(ts.DateUTC>=(DiveStat.DateUTC(w)+seconds(DiveStat.DescTime(w)))...
& ts.DateUTC<=(DiveStat.DateUTC(w)+seconds(DiveStat.DescTime(w))+seconds(DiveStat.Botttime(w))));
end
BottomTempStats = table('Size', [length(DiveStat.DiveNumber) 8], 'VariableTypes', ...
{'double','double','double','double','double','double','double','double'}, ...
'VariableNames', {'DiveNum','AscPts','DescPts','Avg','Std','Min','Median','Max'});
BottomTempStats.DiveNum = DiveStat.DiveNumber;
for i=1:length(DiveStat.DiveNumber)
i
wt_idx = water_bottom{i};
water_bottom{i,2} = wt_idx;
BottomTempStats.Avg(i) = mean(ts.WaterTemp(wt_idx));
BottomTempStats.Std(i) = std(ts.WaterTemp(wt_idx));
BottomTempStats.Min(i) = min(ts.WaterTemp(wt_idx));
BottomTempStats.Median(i) = median(ts.WaterTemp(wt_idx));
BottomTempStats.Max(i) = max(ts.WaterTemp(wt_idx));
ts.WaterTemp(water_bottom{i,1}) = BottomTempStats.Avg(i);
end
%% Figure 4 - Fit curve to dive duration of normal animals
days=3; %number of days to cut off of end of record to eliminate "arrival" dives from curve
DurationData=[(A.DiveStat.JulDate(A.DiveStat.JulDate<(A.MetaData.ArriveDate-days))...
-A.MetaData.DepartDate) A.DiveStat.Dduration(A.DiveStat.JulDate<(A.MetaData.ArriveDate-days))/60;...
(B.DiveStat.JulDate(B.DiveStat.JulDate<(B.MetaData.ArriveDate-days))...
-B.MetaData.DepartDate) B.DiveStat.Dduration(B.DiveStat.JulDate<(B.MetaData.ArriveDate-days))/60;...
(C.DiveStat.JulDate(C.DiveStat.JulDate<(C.MetaData.ArriveDate-days))...
-C.MetaData.DepartDate) C.DiveStat.Dduration(C.DiveStat.JulDate<(C.MetaData.ArriveDate-days))/60;...
(D.DiveStat.JulDate(D.DiveStat.JulDate<(D.MetaData.ArriveDate-days))...
-D.MetaData.DepartDate) D.DiveStat.Dduration(D.DiveStat.JulDate<(D.MetaData.ArriveDate-days))/60;...
(F.DiveStat.JulDate(F.DiveStat.JulDate<(F.MetaData.ArriveDate-days))...
-F.MetaData.DepartDate) F.DiveStat.Dduration(F.DiveStat.JulDate<(F.MetaData.ArriveDate-days))/60;...
(G.DiveStat.JulDate(G.DiveStat.JulDate<(G.MetaData.ArriveDate-days))...
-G.MetaData.DepartDate) G.DiveStat.Dduration(G.DiveStat.JulDate<(G.MetaData.ArriveDate-days))/60;...
(H.DiveStat.JulDate(H.DiveStat.JulDate<(H.MetaData.ArriveDate-days))...
-H.MetaData.DepartDate) H.DiveStat.Dduration(H.DiveStat.JulDate<(H.MetaData.ArriveDate-days))/60];
DurationData=sortrows(DurationData,1);
%fit curve to normal seal dive duration
fitobj=fit(DurationData(:,1),DurationData(:,2),'poly3');
fitci=confint(fitobj);
p22 = predint(fitobj,DurationData(:,1),0.95,'functional','off','observation','on');
%fit curve to 6018 dive duration
DiveStat.DayOfTrip = ceil(DiveStat.JulDate)-floor(MetaData.DepartDate);
DiveStat.DayOfTrip2=DiveStat.JulDate-floor(MetaData.DepartDate);
fitobj2=fit(DiveStat.DayOfTrip2,DiveStat.Dduration/60,'poly3');
p22b = predint(fitobj2,DiveStat.DayOfTrip2,0.95,'functional','off','observation','on');
%Plot PDI and Duration in two panel figure
figure(2)
t=tiledlayout(2,1);
t.TileSpacing='compact';
%Plot PDI ~ Day of Trip with temperature
nexttile
ax=gca;
s1=scatter(DiveStat.DayOfTrip2(DiveStat.SolarEl>0),DiveStat.PDI(DiveStat.SolarEl>0)/60,...
50,SurfaceTempStats.Avg(DiveStat.SolarEl>0),'Linewidth',1.5);
hold on
s2=scatter(DiveStat.DayOfTrip2(DiveStat.SolarEl<0),DiveStat.PDI(DiveStat.SolarEl<0)/60,...
35,SurfaceTempStats.Avg(DiveStat.SolarEl<0),'filled');
colormap(turbo)
c=colorbar;
p5=yline(2.1,'k--','LineWidth',2.5);
p6=yline(2.7,'k-','LineWidth',2.5);
ax.CLim=[9,16];
ax.XLim=[0,91];
ax.YScale='log';
ax.YLim=[1,300];
%ax.XLabel.String='Day of Trip';
ax.YLabel.String='PDI (min)';
c.Label.String = 'Surface Water Temp (\circC)';
ax.FontSize=24;
l=legend([s1 s2 p5 p6],'Day','Night','Normal','6018');
l.FontSize=16;
l.Location='Northwest';
l.NumColumns=2;
%Plot Duration ~ Day of Trip with solar elevation
nexttile
ax=gca;
scatter(DiveStat.DayOfTrip2,DiveStat.Dduration/60,20,DiveStat.SolarEl,'filled')
hold on
ax.YLabel.String='Dive Duration (min)';
ax.XLabel.String='Day of Trip';
ax.Colormap=parula;
p3=plot(DiveStat.DayOfTrip2,p22b,'k-','LineWidth',2);
p4=plot(DurationData(:,1),p22,'k--','LineWidth',2);
ax.XColor='k';
ax.YColor='k';
ax.YDir='normal';
c=colorbar;
c.Label.String='Solar Elevation';
ax.FontSize=24;
ax.CLim=[-70,80];
ax.YLim=[0,35];
ax.XLim=[0,91];
ax.LabelFontSizeMultiplier = 1.2;