-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathtuvTotalQCtests_v11.m
215 lines (187 loc) · 7.8 KB
/
tuvTotalQCtests_v11.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
%% tuvTotalQCtests_v11.m
% This function performs the QC tests on Codar native tuv total velocity data.
% The tests are defined the European common QC model, compliant to CMEMS needs.
% In particular, the following tests are performed:
% - Velocity threshold
% - GDOP threshold
% - Variance threshold
% - Temporal Derivative
% - Data Density threshold
% - Balance of contributing radials from different sites
% INPUT:
% mat_tot: structure containing total file in Codar format
% Total_QC_params: structure containing parameters for total QC tests
% OUTPUT:
% overall: overall quality flag (good data value is assigned
% if and only if all QC tests are passed
% varThr: Variance threshold quality flags
% tempDer: Temporal Derivative quality flags
% GDOPThr: GDOP threshold quality flags
% dataDens: Data Density quality flags
% velThr: Velocity threshold quality flags
% Author: Lorenzo Corgnati
% Date: June 6, 2019
% E-mail: lorenzo.corgnati@sp.ismar.cnr.it
%%
function [overall, varThr, tempDer, GDOPThr, dataDens, velThr] = tuvTotalQCtests_v11(mat_tot, Total_QC_params)
disp(['[' datestr(now) '] - - ' 'tuvTotalQCtests_v11.m started.']);
TQC_err = 0;
%% Prepare QC flag variables
try
overall = netcdf.getConstant('NC_FILL_BYTE').*int8(ones(size(mat_tot.gridLat)));
varThr = netcdf.getConstant('NC_FILL_BYTE').*int8(ones(size(mat_tot.gridLat)));
tempDer = netcdf.getConstant('NC_FILL_BYTE').*int8(ones(size(mat_tot.gridLat)));
GDOPThr = netcdf.getConstant('NC_FILL_BYTE').*int8(ones(size(mat_tot.gridLat)));
dataDens = netcdf.getConstant('NC_FILL_BYTE').*int8(ones(size(mat_tot.gridLat)));
velThr = netcdf.getConstant('NC_FILL_BYTE').*int8(ones(size(mat_tot.gridLat)));
catch err
disp(['[' datestr(now) '] - - ERROR in ' mfilename ' -> ' err.message]);
TQC_err = 1;
end
%%
%% Prepare variables for QC tests
try
% Variance Threshold QC test
variance = ((mat_tot.U_grid.^2).*(mat_tot.U_std).^2) + ((mat_tot.V_grid.^2).*(mat_tot.V_std).^2);
% Velocity Threshold QC test
totVel = sqrt(((mat_tot.U_grid).^2) + ((mat_tot.V_grid).^2));
% Temporal Derivative QC test
tempDer_Thr = Total_QC_params.TempDerThr.threshold;
% Check if the files of the previous two hours exist
if ((exist(Total_QC_params.TempDerThr.hour2) == 2) && (exist(Total_QC_params.TempDerThr.hour1) == 2))
tD_go = true;
[filepath1h,name1h,ext1h] = fileparts(Total_QC_params.TempDerThr.hour1);
else
tD_go = false;
end
catch err
disp(['[' datestr(now) '] - - ERROR in ' mfilename ' -> ' err.message]);
TQC_err = 1;
end
%%
%% Populate QC variables
try
% Velocity Threshold quality flags
velThr(totVel>Total_QC_params.VelThr) = 4;
velThr(totVel<=Total_QC_params.VelThr) = 1;
% GDOP Threshold quality flags
GDOPThr(mat_tot.GDOP>Total_QC_params.GDOPThr) = 4;
GDOPThr(mat_tot.GDOP<=Total_QC_params.GDOPThr) = 1;
% Data Density Threshold quality flag
dataDens(mat_tot.DDENS<Total_QC_params.DataDensityThr) = 4;
dataDens(mat_tot.DDENS>=Total_QC_params.DataDensityThr) = 1;
% Variance Threshold quality flags
varThr(variance>Total_QC_params.VarThr) = 4;
varThr(variance<=Total_QC_params.VarThr) = 1;
% Temporal Derivative quality flags
if (tD_go)
tempDer1h = tempDer;
% Extract the U and V velocity fields from the previous two hours files
totU1h = ncread(Total_QC_params.TempDerThr.hour1,'EWCT');
totV1h = ncread(Total_QC_params.TempDerThr.hour1,'NSCT');
totU2h = ncread(Total_QC_params.TempDerThr.hour2,'EWCT');
totV2h = ncread(Total_QC_params.TempDerThr.hour2,'NSCT');
% Check if the data matrix has to be transposed
trs = 0;
if(size(mat_tot.gridLat)~=size(totU1h))
trs = 1;
totU1h = totU1h';
totV1h = totV1h';
totU2h = totU2h';
totV2h = totV2h';
end
% Evaluate total velocities
totVel1h = sqrt(((totU1h).^2) + ((totV1h).^2));
totVel2h = sqrt(((totU2h).^2) + ((totV2h).^2));
for tVr=1:size(totVel,1)
for tVc=1:size(totVel,2)
if (~isnan(totVel1h(tVr,tVc)))
if ((isnan(totVel(tVr,tVc))) || (isnan(totVel2h(tVr,tVc))))
tempDer1h(tVr,tVc) = 1;
elseif ((abs(totVel(tVr,tVc) - totVel1h(tVr,tVc)) < tempDer_Thr) && (abs(totVel2h(tVr,tVc) - totVel1h(tVr,tVc)) < tempDer_Thr))
tempDer1h(tVr,tVc) = 1;
else
tempDer1h(tVr,tVc) = 4;
end
end
end
end
% Modify the VART_QC variable of the nc file of the previous hour
if(trs==0)
ncwrite(Total_QC_params.TempDerThr.hour1,'VART_QC',tempDer1h);
elseif(trs==1)
ncwrite(Total_QC_params.TempDerThr.hour1,'VART_QC',tempDer1h');
end
disp(['[' datestr(now) '] - - ' [name1h,ext1h] ' previous time step nc file successfully updated with the Temporal Derivative QC variable.']);
end
% Set the QC flag for the current hour to 0 (no QC performed)
tempDer(~isnan(mat_tot.U_grid)) = 0;
catch err
disp(['[' datestr(now) '] - - ERROR in ' mfilename ' -> ' err.message]);
TQC_err = 1;
end
%%
%% Populate the overall quality variable
% Current data file
try
for ii=1:size(overall,1)
for jj = 1:size(overall,2)
if(~isnan(mat_tot.U_grid(ii,jj)))
if((tempDer(ii,jj) == 1) && (velThr(ii,jj) == 1) && (GDOPThr(ii,jj) == 1) && (dataDens(ii,jj) == 1))
overall(ii,jj) = 1;
else
overall(ii,jj) = 4;
end
end
end
end
catch err
disp(['[' datestr(now) '] - - ERROR in ' mfilename ' -> ' err.message]);
TQC_err = 1;
end
% Previous hour data file
try
if (tD_go)
% Extract the QC variables from the previous hour file
overall1h = ncread(Total_QC_params.TempDerThr.hour1,'QCflag');
velThr1h = ncread(Total_QC_params.TempDerThr.hour1,'CSPD_QC');
GDOPThr1h = ncread(Total_QC_params.TempDerThr.hour1,'GDOP_QC');
dataDens1h = ncread(Total_QC_params.TempDerThr.hour1,'DDNS_QC');
if(trs==1)
overall1h = overall1h';
velThr1h = velThr1h';
GDOPThr1h = GDOPThr1h';
dataDens1h = dataDens1h';
end
% Fill the overall QC variable
for ii=1:size(overall1h,1)
for jj = 1:size(overall1h,2)
if(~isnan(overall1h(ii,jj)))
if((tempDer1h(ii,jj) == 1) && (velThr1h(ii,jj) == 1) && (GDOPThr1h(ii,jj) == 1) && (dataDens1h(ii,jj) == 1))
overall1h(ii,jj) = 1;
else
overall1h(ii,jj) = 4;
end
else
overall1h(ii,jj) = netcdf.getConstant('NC_FILL_BYTE');
end
end
end
% Modify the overall QC variable of the nc file of the previous hour
if(trs==0)
ncwrite(Total_QC_params.TempDerThr.hour1,'QCflag',int16(overall1h));
elseif(trs==1)
ncwrite(Total_QC_params.TempDerThr.hour1,'QCflag',int16(overall1h'));
end
ncwriteatt(Total_QC_params.TempDerThr.hour1,'/','date_update',char([datestr(now, 'yyyy-mm-dd') 'T' datestr(now, 'HH:MM:SS') 'Z']));
disp(['[' datestr(now) '] - - ' [name1h,ext1h] ' previous time step nc file successfully updated with the overall QC variable.']);
end
catch err
disp(['[' datestr(now) '] - - ERROR in ' mfilename ' -> ' err.message]);
TQC_err = 1;
end
%%
if(TQC_err==0)
disp(['[' datestr(now) '] - - ' 'tuvTotalQCtests_v11.m successfully executed.']);
end
return