function [error] = corTsgMedian(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 % 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 ? % Get application data % -------------------- tsg = getappdata( hMainFig, 'tsg_data'); % Shorten the variable name % ------------------------- TIME_WINDOWS = tsg.cst.COR_TIME_WINDOWS; % Get PROBABLY_GOOD, PROBABLY_BAD and VALUE_CHANGED codes % ------------------------------------------------------- PROBABLY_GOOD = get(tsg.qc.hash, 'PROBABLY_GOOD', 'code'); PROBABLY_BAD = get(tsg.qc.hash, 'PROBABLY_BAD', 'code'); VALUE_CHANGED = get(tsg.qc.hash, 'VALUE_CHANGED', 'code'); % 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); if dateMax > dateMin % intialisation % ------------- if isempty( tsg.SSPS_ADJUSTED ) tsg.SSPS_ADJUSTED = tsg.SSPS; tsg.SSPS_ADJUSTED_ERROR = NaN * ones( size( tsg.SSPS ) ); tsg.SSPS_ADJUSTED_QC = tsg.SSPS_QC; end % Find the indices of samples within the time limits. % -------------------------------------------------- indSample = find(tsg.DAYD_SPL >= dateMin & tsg.DAYD_SPL <= dateMax); indCor = 0; for i = 1:length(indSample) % Find samples within TIME_WINDOWS with Good and probably Good QC % --------------------------------------------------------------- ind = find( tsg.DAYD_SPL >= (tsg.DAYD_SPL(indSample(i)) - TIME_WINDOWS/2) &... tsg.DAYD_SPL <= (tsg.DAYD_SPL(indSample(i)) + TIME_WINDOWS/2) &... tsg.SSPS_SPL_QC <= PROBABLY_GOOD); if ~isempty(ind) % detect NaN in sample.SSPS_DIF due to bad QC code in tsg.SSPS % ------------------------------------------------------------ ind2 = find(~isnan(tsg.SSPS_SPL_DIF(ind))); % Compute the median difference and error within TIME_WINDOWS % ----------------------------------------------------------- if ~isempty(ind2) if ~isempty(tsg.SSPS_SPL_DIF(ind(ind2))) A = tsg.SSPS_SPL_DIF(ind(ind2)); meanA = mean(A); stdA = std(A); % Standard deviation test: keep these values % ------------------------------------------ ind3 = find( A >= meanA-3*stdA & A <= meanA+3*stdA); B = tsg.SSPS_SPL_DIF(ind(ind2(ind3))); if ~isempty( B ) indCor = indCor + 1; cor.DAYD(indCor) = tsg.DAYD_SPL((indSample(i))); cor.DIFF(indCor) = median(B); cor.ERROR(indCor) = nanstd(B)/sqrt(length(B)); cor.NVALUE(indCor) = length(B); end % Standard deviation test: don't keep these values % QC PROBABLY_BAD % ------------------------------------------------------ ind4 = find( A < meanA-3*stdA | A > meanA+3*stdA); if ~isempty( ind4 ) tsg.SSPS_SPL_QC(ind(ind2(ind4))) = PROBABLY_BAD; end end end end 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 % The correction is applied to the TSG between dateMin and dateMax using % a linear interpolation only on measurements with GOOD and % PROBABLY_GOOD QC % ---------------------------------------------------------------------- dtTsg = find( tsg.DAYD >= dateMin & tsg.DAYD <= dateMax &... tsg.SSPS_QC <= PROBABLY_GOOD); 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)); % VALUE_CHANGED code % ------------------ tsg.SSPS_ADJUSTED_QC(dtTsg) = VALUE_CHANGED; end % Update tsg application data % --------------------------- setappdata( hMainFig, 'tsg_data', tsg); % everything OK % ------------- error = 1; else % DateMax <= DateMin % ------------------ error = -1; end