/
WTDlagGPPLos2_5_2021.m
150 lines (124 loc) · 3.77 KB
/
WTDlagGPPLos2_5_2021.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
%% Clear workspace, clear variables, and clear figures
clear all;close all;clc;
load('USLos2020_2019_calcresp.mat')
load('woodruffprecip.mat')
load('dischargevars.mat')
%% Replace placeholders for missing values with NaN
USLosHH2019.Properties.VariableNames = USLosHH2020.Properties.VariableNames;
USLos = [USLosHH2019;USLosHH2020];
for i= 3:width(USLos)
USLos.(i)(USLos.(i)==-9999) = NaN;
end
%% Sort data by the half hour for whichever month or months you want
precip = woodruffprecipS3.Precipitationin(2:end,:)*2.54;% convert in to cm
dis = USGSLos;
dis.discharge = (dis.discharge_cfs).*0.028316847;% convert cfs to m3/s
yr2 = year(dis.datetime);
mon2 = month(dis.datetime);
%% Make daily averages
mo = month(USLos.TIMESTAMP_END);
monthlyCH4 = accumarray(mo,USLos.FCH4_RF_filled,[],@nanmean);
daily_CH4 = dailyaverage2(USLos.FCH4_RF_filled);
dailyCH4 = daily_CH4(:,1);
CH4dates = USLos.TIMESTAMP_END(daily_CH4(:,2).*48);
daily_Tairf = dailyaverage2(USLos.TA_F_1_1_1);
dailyTairf = daily_Tairf(:,1);
daily_WTD = dailyaverage2(USLos.WTD);
dailyWTD = daily_WTD(:,1);
daily_GPP = dailyaverage2(USLos.GPP_F);
dailyGPP = daily_GPP(:,1);
daily_VPD = dailyaverage2(USLos.VPD_F_1_1_1);
dailyVPD = daily_VPD(:,1);
%% Lag Analysis of WTD and FCH4 2019
yr = year(CH4dates);
Z = [];
dailyGPP1 = dailyGPP(year(CH4dates)==2019);
dailyWTD1 = dailyWTD(year(CH4dates)==2019);
%For WTD
for k = 1:200
B = NaN(length(dailyWTD1),1);
B((k+1):end) = dailyWTD1(1:(end-k));% shift WTD down
Z(:,k) = B;
end
Z = [dailyWTD1 Z(:,1:200)];
% Remove any missing data from both datasets
TF = isnan(Z);
TF2 = isnan(dailyGPP1);
location = [];
for i = 1:size(TF,1)
for j = 1:200
if (TF(i,j)==1) || (TF2(i)==1)
location = [location;[i j]];
end
end
end
A = repmat(dailyGPP1,1,200);
for i = 1:length(location)
Z(location(i,1),location(i,2)) = NaN;
A(location(i,1),location(i,2)) = NaN;
end
% Calculate cross correlations
for i = 1:200
dayWTD = Z(:,i);
dayGPP = A(:,i);
[rho3(i),pval3(i)] = corr(dayWTD(~isnan(dayWTD)),dayGPP(~isnan(dayGPP)));
end
%% For 2020
Z = [];
dailyGPP = dailyGPP(year(CH4dates)==2020);
dailyWTD = dailyWTD(year(CH4dates)==2020);
for k = 1:200
B = NaN(length(dailyWTD),1);
B((k+1):end) = dailyWTD(1:(end-k));% shift WTD down
Z(:,k) = B;
end
Z = [dailyWTD Z(:,1:200)];
% Remove any missing data from both datasets
TF = isnan(Z);
TF2 = isnan(dailyGPP);
location = [];
for i = 1:size(TF,1)
for j = 1:200
if (TF(i,j)==1) || (TF2(i)==1)
location = [location;[i j]];
end
end
end
A = repmat(dailyGPP,1,200);
for i = 1:length(location)
Z(location(i,1),location(i,2)) = NaN;
A(location(i,1),location(i,2)) = NaN;
end
% Calculate cross correlations
for i = 1:200
dayWTD = Z(:,i);
dayGPP = A(:,i);
[rho(i),pval(i)] = corr(dayWTD(~isnan(dayWTD)),dayGPP(~isnan(dayGPP)));
end
%% Plot
lag2 = 1:200;
figure()
subplot(1,2,1)
hold on
plot(lag2,rho3,'o','MarkerEdgeColor','k','MarkerSize',11)
plot(lag2(pval3<0.05 & rho3>0),rho3(pval3<0.05 & rho3>0),'o','MarkerFaceColor','b','MarkerEdgeColor','k','MarkerSize',11);
hold off
xlabel('time lag (days)')
ylabel('correlation coefficient')
% title('Lag Corr of WTD & GPP US-Los 2019')
title('US-Los 2019')
set(gca,'FontSize',17)
ylim([0 1])
xlim([0 200])
subplot(1,2,2)
hold on
plot(lag2,rho,'o','MarkerEdgeColor','k','MarkerSize',11)
plot(lag2(pval<0.05 & rho>0),rho(pval<0.05 & rho>0),'o','MarkerFaceColor','b','MarkerEdgeColor','k','MarkerSize',11);
hold off
xlabel('time lag (days)')
ylabel('correlation coefficient')
% title('Lag Corr of WTD & GPP US-Los 2020')
title('US-Los 2020')
set(gca,'FontSize',17)
ylim([0 1])
xlim([0 200])