clc
clear
close all

% Get TSG and bucket data
% -----------------------
% tsg    = getappdata( hMainFig, 'tsg_data');
% bucket = getappdata( hMainFig, 'bucket_data');

% Lecture fichier TSG
% -------------------
filename = 'F:\work\M_TsgQc\tsg_data\past0601.txt';
[tsg, error] = dev_readTsg( filename);

% Lecture fichier bucket
% -----------------------
filename = 'F:\work\M_TsgQc\tsg_data\past0601.btl';
[bucketASCII, error] = dev_readBucket( filename);

% Salinity, in one 1 hour interval, should not depart the average for more
% than SAL_STD_MAX standard deviation
% -----------------------------------------------------------------------
TSG_STDMAX = 0.1;

% 1 hour interval expressed in MATLAB serial Number
% -------------------------------------------------
TSG_DT_SMOOTH = datenum(0, 0, 0, 1, 0 , 0);

% dt between 2 tsg measurements
% -----------------------------
TSG_SAMPLING_TIME = datenum(0, 0, 0, 0, 5 , 0);
% ou plus general
TSG_SAMPLING_TIME = tsg.TIME(2) - tsg.TIME(1);

% Period used to comput the correction
% ------------------------------------
COR_TIME_WINDOWS = 10;

% Running average of TSG time series over TSG_DT_SMOOTH hour.
% Salinity in one INTERVAL_SMOOTHING hour interval, should not depart the
% average for more than SAL_STD_LIM
%
% *************************************************************************

[psal_smooth, nval] = ...
      dev_moveaverage(tsg.TIME, tsg.PSAL, TSG_DT_SMOOTH, TSG_STDMAX);

% Merge the 2 types of 'bucket' records, if they exists
% -----------------------------------------------------
bucketNETCDF      = bucketASCII;
bucketNETCDF.TIME = bucketNETCDF.TIME + 1;
bucketNETCDF.PSAL = bucketNETCDF.PSAL + 1;
bucketNETCDF = struct([]);
sample = dev_mergesample( bucketNETCDF, bucketASCII);
  
% *************************************************************************
%                             Second Step
% 
% Co-location of samples and TSG measurements
% Compute the sample-TSG differences

sample = dev_diffTsgSample(tsg, psal_smooth, sample, TSG_SAMPLING_TIME);

dateMin = tsg.TIME(5000);
dateMax = tsg.TIME(10000);
tsg = dev_corMethod1(tsg, sample, dateMin, dateMax, COR_TIME_WINDOWS);


% ******************************************************************
%                                   Tracé
% ******************************************************************    
figure
plot( tsg.TIME, tsg.PSAL, 'k' );
hold on
plot( tsg.TIME, psal_smooth, 'r' );
datetick('x');

figure
hist(nval, 1:13)

figure
plot( tsg.TIME, psal_smooth, 'k' );
hold on
ind = find( sample.PSAL_QC == 1 & sample.TYPE == 1); 
plot( sample.TIME(ind), sample.PSAL(ind), '.b' );
ind = find( sample.PSAL_QC == 1 & sample.TYPE == 2); 
plot( sample.TIME(ind), sample.PSAL(ind), '.g' );
datetick('x');
hold off

ind = find( sample.PSAL_QC == 0 ); 
if ~isempty(ind)
    plot( sample.TIME(ind), sample.PSAL(ind), '.r' );
    datetick('x');
end

figure
ind = find( sample.PSAL_QC == 1 & sample.TYPE == 1); 
plot( sample.TIME(ind), sample.PSAL(ind), '.b' );
hold on
ind = find( sample.PSAL_QC == 1 & sample.TYPE == 2); 
plot( sample.TIME(ind), sample.PSAL(ind), '.g' );
datetick('x');

figure
plot( tsg.TIME, tsg.PSAL, 'k' );
hold on
plot( tsg.TIME, tsg.PSAL_ADJ, 'r' );
plot( tsg.TIME, tsg.PSAL_ADJ - tsg.PSAL_ERR, 'b' );
plot( tsg.TIME, tsg.PSAL_ADJ + tsg.PSAL_ERR, 'b' );
ind = find( sample.PSAL_QC == 1 & sample.TYPE == 1); 
plot( sample.TIME(ind), sample.PSAL(ind), '.b', 'Markersize', 6 );
ind = find( sample.PSAL_QC == 1 & sample.TYPE == 2); 
plot( sample.TIME(ind), sample.PSAL(ind), '.g', 'Markersize', 6 );
datetick('x');

