-
Notifications
You must be signed in to change notification settings - Fork 0
/
comp_TS.m
276 lines (241 loc) · 9.35 KB
/
comp_TS.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
% Yiwen Mei ([email protected])
% CEIE, George Mason University
% Last update: 7/18/2020
%% Functionality
% This code performs error analysis for one or multiple target time series with
% respect to a common reference. Specifically, it calculates statistics (sample
% size, mean, and variance), error metrics (RMS, CRMS, CC, NSE, and KGE), results
% of three statistical significant tests (for M(R)E, (N)CRMS, and CC), and contigency
% statistics (optional, percentage of H, M, F, and N).
%% Input
% ofl : full name list of .mat files store the time series class (TSCls.m) for
% different locations (ofl can be N-by-1 or N-by-M cell array for 1 or
% M target products and N stations);
% nflg: a flag iindicating whether to calculate network-based statistics and
% error metrics or not;
% P_N : minimum percentage of sample size for the statistics and error metrics
% calculations;
% Tmk : a 2-element cell to specify the time range of interest (the first element
% can be 'A', 'Y', or 'M' stands for no mask, mask based on year, or mask
% based on month; the second element can be NaN for 'A' and vector stores
% the year(s) or month(s) for 'Y' and 'M'; eg., {'M',[1:4 11 12]});
% Sval: a singular value if a time step that both the target and reference values
% equal to it is excluded from the analysis (default is NaN);
% Thr : thresholds used to calculate the contigency statistics for the time series
% of different locations (default is NaN);
% a : significant level for the statistical significant test (defaul is 0.05);
% pflg: parallel flag (false/true - squential/parallel, default is false).
%% Output
% STs: table stores the statistics of target(s) and reference (sample size, mean,
% and variance);
% EMs: table stores the error metrics (RMS, CRMS, CC, NSE, and KGE);
% SGs: table stores the significant tests results (ME/MRE, CRMS/NCRMS, and CC);
% Stg: matched time series;
% CSs: table stores the contigency statistics (percentage of H, M, F, and N).
%% Additional note
% If M products are inputted by ofl following the order of product P1, P2, ...,
% PM, then the outputted results (STs, EMs, SGs, and CSs) follows the reversed
% order as PM, PM-1, ..., P1.
% (N)(C)RMS - (normalized) (centered) root mean squared error;
% CC - correlation coefficient; NSE - Nash-Sutcliff efficiency;
% KGE - Kling-Gupta efficiency; M(R)E - mean (relative) error;
% H - hit; M - missing; F - false alarm; N - correct negative.
% ME is calculated by subtracting mean of target from mean of reference;
% MRE is calculated by dividing ME to mean of reference.
% Require TSCls.m and errM.m.
function [STs,EMs,SGs,Stg,CSs]=comp_TS(ofl,nflg,varargin)
%% Check the inputs
narginchk(2,7);
ips=inputParser;
ips.FunctionName=mfilename;
addRequired(ips,'ofl',@(x) validateattributes(x,{'cell'},{'nonempty'},mfilename,'ofl'));
addRequired(ips,'nflg',@(x) validateattributes(x,{'logical'},{'nonempty'},mfilename,'nflg'));
addOptional(ips,'P_N',0,@(x) validateattributes(x,{'double'},{'nonempty'},mfilename,'P_N'));
addOptional(ips,'Tmk',{'A',NaN},@(x) validateattributes(x,{'cell'},{'numel',2},mfilename,'Tmk'));
addOptional(ips,'Sval',NaN,@(x) validateattributes(x,{'double'},{'scalar'},mfilename,'Sval'));
addOptional(ips,'Thr',NaN,@(x) validateattributes(x,{'double'},{'scalar'},mfilename,'Thr'));
addOptional(ips,'a',.05,@(x) validateattributes(x,{'double'},{'nonempty'},mfilename,'a'));
addOptional(ips,'pflg',false,@(x) validateattributes(x,{'logical'},{'nonempty'},mfilename,'pflg'));
parse(ips,ofl,nflg,varargin{:});
P_N=ips.Results.P_N;
Tmk=ips.Results.Tmk;
Sval=ips.Results.Sval;
Thr=ips.Results.Thr;
a=ips.Results.a;
pflg=ips.Results.pflg;
clear ips varargin
%% Statistics and Error metrics
Stg=[];
Gid=[];
RN={};
STs=[];
EMs=[];
SGs=[];
CSs=[];
% Each station
switch pflg
case true
parfor n=1:size(ofl,1)
[sts,ems,css,sgs,stg,rn,gid]=comp_TS_sub(ofl(n,:),a,Thr,Tmk,P_N,Sval); % Each station
STs=[STs;sts];
EMs=[EMs;ems];
SGs=[SGs;sgs];
CSs=[CSs;css];
Stg=[Stg;stg];
RN=[RN;{rn}];
if n==length(ofl)
Gid=[Gid;gid];
end
end
case false
for n=1:size(ofl,1)
[sts,ems,css,sgs,stg,rn,gid]=comp_TS_sub(ofl(n,:),a,Thr,Tmk,P_N,Sval); % Each station
STs=[STs;sts];
EMs=[EMs;ems];
SGs=[SGs;sgs];
CSs=[CSs;css];
Stg=[Stg;stg];
RN=[RN;rn];
if n==length(ofl)
Gid=[Gid;gid];
end
end
end
% All stations
switch nflg
case true
if size(Stg,1)>1 && size(ofl,1)>1
if isa(Gid,'double')
fprintf('Calculate statistics and error metrics for Group-%i network\n',Gid);
RN=[RN;sprintf('Group-%i',Gid)];
elseif iscell(Gid) && ischar(Gid{1})
fprintf('Calculate statistics and error metrics for G-%s network\n',Gid{1});
RN=[RN;sprintf('G-%s',Gid{1})];
else
error('Group ID must be supplied as a scalar for integer or as a cell for character');
end
[sts,ems,sgs,css]=errM(Stg,a,Thr);
STs=[STs;sts];
EMs=[EMs;ems];
SGs=[SGs;sgs];
CSs=[CSs;css];
end
case false
if isa(Gid,'double')
fprintf('Skip statistics and error metrics for Group-%i network\n',Gid);
elseif iscell(Gid) && ischar(Gid{1})
fprintf('Skip statistics and error metrics for G-%s network\n',Gid{1});
end
end
%% Label the results
if ~isempty(RN)
nl1=cellfun(@(X) sprintf('m_tg%i',X),num2cell(1:size(ofl,2)),'UniformOutput',false);
nl2=cellfun(@(X) sprintf('v_tg%i',X),num2cell(1:size(ofl,2)),'UniformOutput',false);
STs=array2table(STs,'VariableNames',['N' nl1 'm_rf' nl2 'v_rf'],'RowNames',RN);
nl1=cellfun(@(X) sprintf('RMS%i',X),num2cell(1:size(ofl,2)),'UniformOutput',false);
nl2=cellfun(@(X) sprintf('CRMS%i',X),num2cell(1:size(ofl,2)),'UniformOutput',false);
nl3=cellfun(@(X) sprintf('CC%i',X),num2cell(1:size(ofl,2)),'UniformOutput',false);
nl4=cellfun(@(X) sprintf('NSE%i',X),num2cell(1:size(ofl,2)),'UniformOutput',false);
nl5=cellfun(@(X) sprintf('KGE%i',X),num2cell(1:size(ofl,2)),'UniformOutput',false);
EMs=array2table(EMs,'VariableNames',[nl1 nl2 nl3 nl4 nl5],'RowNames',RN);
nl1=cellfun(@(X) sprintf('ME%i',X),num2cell(1:size(ofl,2)),'UniformOutput',false);
nl2=cellfun(@(X) sprintf('CRMS%i',X),num2cell(1:size(ofl,2)),'UniformOutput',false);
nl3=cellfun(@(X) sprintf('CC%i',X),num2cell(1:size(ofl,2)),'UniformOutput',false);
if size(ofl,2)>1
K=num2cell(nchoosek(1:size(ofl,2),2))';
nl4=cellfun(@(X,Y) sprintf('ME%i_%i',X,Y),K(1,:),K(2,:),'UniformOutput',false);
nl5=cellfun(@(X,Y) sprintf('CRMS%i_%i',X,Y),K(1,:),K(2,:),'UniformOutput',false);
nl6=cellfun(@(X,Y) sprintf('CC%i_%i',X,Y),K(1,:),K(2,:),'UniformOutput',false);
SGs=array2table(SGs,'VariableNames',[nl1 nl2 nl3 nl4 nl5 nl6],'RowNames',RN);
else
SGs=array2table(SGs,'VariableNames',[nl1 nl2 nl3],'RowNames',RN);
end
if ~isempty(CSs)
nl1=cellfun(@(X) sprintf('r_h%i',X),num2cell(1:size(ofl,2)),'UniformOutput',false);
nl2=cellfun(@(X) sprintf('r_m%i',X),num2cell(1:size(ofl,2)),'UniformOutput',false);
nl3=cellfun(@(X) sprintf('r_f%i',X),num2cell(1:size(ofl,2)),'UniformOutput',false);
nl4=cellfun(@(X) sprintf('r_n%i',X),num2cell(1:size(ofl,2)),'UniformOutput',false);
CSs=array2table(CSs,'VariableNames',[nl1 nl2 nl3 nl4],'RowNames',RN);
end
end
end
function [sts,ems,css,sgs,TS,rn,gid]=comp_TS_sub(ofl,a,Thr,Tmk,P_N,Sval)
%% Align the time series
for i=1:length(ofl)
% Load the TSCls.m object
OTS=matfile(ofl{i});
OTS=OTS.OTS;
% Match target time resolution to reference
[Ttg,Trf]=OTS.UniTL('rf'); % Unify the time zones
if i==1 % Reference time series
TS=array2table([Trf OTS.TS2],'VariableNames',{'Dnum','Srf'});
end
% Aggregate the target time series
stg=[];
if isa(OTS.TR2,'double') % Regular time resolution (e.g. daily, hourly)
for t=1:length(Trf)
k=Ttg>=Trf(t)-OTS.TR2/24/2 & Ttg<Trf(t)+OTS.TR2/24/2;
if any(k)
stg=[stg;[Trf(t) mean(OTS.TS1(k))]];
end
end
elseif ischar(OTS.TR2) % Monthly, Yearly
for t=1:length(Trf)
[Y,M,~]=datevec(Trf(t));
switch OTS.TR2
case 'monthly'
k=Ttg>=Trf(t) & Ttg<Trf(t)+eomday(Y,M);
case 'yearly'
k=Ttg>=Trf(t) & Ttg<Trf(t)+yeardays(Y);
end
if any(k)
stg=[stg;[Trf(t) mean(OTS.TS1(k))]];
end
end
end
% Join the target with reference
if ~isempty(stg)
stg=array2table(stg,'VariableNames',[{'Dnum'} sprintf('Stg%i',i)]);
TS=innerjoin(stg,TS,'Keys','Dnum');
else
TS=[];
break;
end
end
clear stg k Trf Ttg
%% Perform the stats/error calculation
gid=OTS.gid;
if size(TS,1)>4
[Y,M,~]=datevec(table2array(TS(:,1)));
switch Tmk{1}
case 'Y'
k0=any(Y==Tmk{2},2);
case 'M'
k0=any(M==Tmk{2},2);
case 'A'
k0=true(size(Y));
end
TS=table2array(TS(:,2:end));
k=all(~isnan(TS),2) & any(TS~=Sval,2) & k0;
if sum(k)/sum(k0)>P_N
rn=OTS.Gtg.Sid;
fprintf('Execute errM.m for station %s\n',rn);
TS=TS(k,:);
[sts,ems,sgs,css]=errM(TS,a,Thr);
else
TS=[];
sts=[];
ems=[];
css=[];
sgs=[];
rn='';
end
else
TS=[];
sts=[];
ems=[];
css=[];
sgs=[];
rn='';
end
end