Newer
Older
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
function [error] = corTsgLinear(hMainFig, dateMin, dateMax)
%
% Correct the TSG salinity time series with the Water sample.
% Use the median value of TIME_WINDOWS water sample to compute the
% correction. see the documentation
%
% Input
% hMainFig ..... Handle to the main GUI
% dateMin ...... the correction is applied between dateMin and date Max
% dateMax ...... the correction is applied between dateMin and date Max
%
% Output
% Error ........ 1 everything OK
% ........ -1 dateMax <= date Min
%
% TO DO
% corTsgMethod1.m
% 1) Test if there are NaN values in 'sample':
% We could get some problem with the computation of the median and std
% 2) Initialisation de la structure 'cor'
% 3) Test for anormal sample-tsg difference - Suppress bad points
% 4) Test on the validation code. do we apply the correction whatever the
% is the code ?
% 5) Return an Error code ?
% 6) Test Date_Min < Date_Max
% Get application data
% --------------------
tsg = getappdata( hMainFig, 'tsg_data');
sample = getappdata( hMainFig, 'sample');
% Shorten the variable name
% -------------------------
TIME_WINDOWS = tsg.cst.COR_TIME_WINDOWS;
% Get PROBABLY_GOOD and VALUE_CHANGED code
% ----------------------------------------
PROBABLY_GOOD = get(tsg.qc.hash, 'PROBABLY_GOOD', 'code');
VALUE_CHANGED = get(tsg.qc.hash, 'VALUE_CHANGED', 'code');
% The correction is computed between dateMin and dateMax
% ------------------------------------------------------
dt = find(sample.DAYD >= dateMin & sample.DAYD <= dateMax);
if dateMax > dateMin
% Create a structure with an NaN
% No other solution as I can't add a structure to an empty one
% ------------------------------------------------------------
cor = struct('DAYD', NaN, 'DIFF', NaN, 'ERROR', NaN, 'NVALUE', NaN);
% --------------------
for i = 1 : length(dt)
% Find the sample within TIME_WINDOWS with Good and probably Good QC
% ------------------------------------------------------------------
ind = find( sample.DAYD(dt) >= sample.DAYD(dt(i)) - TIME_WINDOWS/2 &...
sample.DAYD(dt) <= sample.DAYD(dt(i)) + TIME_WINDOWS/2 &...
sample.SSPS_QC(dt) <= PROBABLY_GOOD);
end
% Eliminate the first element if NaN
% ----------------------------------
if isnan(cor.DAYD(1))
cor.DAYD(1) = [];
cor.DIFF(1) = [];
cor.ERROR(1) = [];
cor.NVALUE(1) = [];
end
if ~isempty( cor.DAYD )
% The error is maximum if the median is computed with less than 4 samples
% -----------------------------------------------------------------------
cor.ERROR( cor.NVALUE < 4 ) = 1;
% The correction is applied between dateMin and dateMax
% We attribute to dateMin the first correction computed
% and to dateMax the last one
%
% Find the tsg date in the interval dateMin-dateMax
% -------------------------------------------------
dtTsg = find(tsg.DAYD >= dateMin & tsg.DAYD <= dateMax);
if cor.DAYD(1) ~= dateMin
cor.DAYD = [tsg.DAYD(dtTsg(1)) cor.DAYD];
cor.DIFF = [cor.DIFF(1) cor.DIFF];
cor.ERROR = [cor.ERROR(1) cor.ERROR];
cor.NVALUE = [cor.NVALUE(1) cor.NVALUE];
end
if cor.DAYD(end) ~= dateMax
cor.DAYD = [cor.DAYD tsg.DAYD(dtTsg(end))];
cor.DIFF = [cor.DIFF cor.DIFF(end)];
cor.ERROR = [cor.ERROR cor.ERROR(end)];
cor.NVALUE = [cor.NVALUE cor.NVALUE(end)];
end
p = polyfit( x, y, 1);
y = polyval(p,x);
% The correction is applied to the TSG between dateMin and dateMax using
% a linear interpolation
% ----------------------------------------------------------------------
tsg.SSPS_ADJUSTED(dtTsg) = tsg.SSPS(dtTsg) + ...
interp1(cor.DAYD, cor.DIFF, tsg.DAYD(dtTsg));
tsg.SSPS_ADJUSTED_ERROR(dtTsg) = ...
interp1(cor.DAYD, cor.ERROR, tsg.DAYD(dtTsg));
tsg.SSPS_ADJUSTED_QC(dtTsg) = VALUE_CHANGED;
else
% RAZ if no correction
% --------------------
dt = find(tsg.DAYD >= dateMin & tsg.DAYD <= dateMax);
tsg.SSPS_ADJUSTED(dt) = NaN*ones(size(tsg.SSPS(dt)));
tsg.SSPS_ADJUSTED_ERROR(dt) = NaN*ones(size(tsg.SSPS(dt)));
tsg.SSPS_ADJUSTED_QC(dt) = zeros(size(tsg.SSPS(dt)));
end
% Update tsg application data
% ---------------------------
setappdata( hMainFig, 'tsg_data', tsg);
% everything OK
% -------------
error = 1;
else
% DateMax <= DateMin
% ------------------
error = -1;
end