function [] = tsg_mergesample( hTsgGUI )
%
% function [] = tsg_mergesample( hTsgGUI )
%
% Merge the 2 types of 'bucket' records, if they exists
%
% Input
% _WS ........ Water sample
% _EXT ....... External samples (ARGO, CTD, etc.)
%
% Ouptut
%

% Get bucket data in ASCII or NETCDF format. If the data does not exist
% getappdata return an empty matrix
% ---------------------------------------------------------------------
tsg  = getappdata( hTsgGUI, 'tsg_data');

% Create a structure with an NaN
% No other solution as I can't add a structure to an empty one
% ------------------------------------------------------------
sample = struct('DAYD',NaN ,        'LATX',NaN ,      'LONX',NaN , ...
                'SSPS',NaN ,        'SSPS_QC',NaN ,   'SSPS_DIF',NaN ,...
                'SSPS_SMOOTH',NaN , 'SSPS_TYPE', NaN);    

% Fill the structure sample with the bucket data.
% This measurements are of TYPE 1
% ------------------------------------------------
if ~isempty(tsg.SSPS_WS)

  sample.DAYD        = [sample.DAYD;        tsg.DAYD_WS];
  sample.LATX        = [sample.LATX;        tsg.LATX_WS];
  sample.LONX        = [sample.LONX;        tsg.LONX_WS];
  sample.SSPS        = [sample.SSPS;        tsg.SSPS_WS];
  if ~isempty(tsg.SSPS_WS_QC)
    sample.SSPS_QC   = [sample.SSPS_QC;     tsg.SSPS_WS_QC];
  else
    sample.SSPS_QC   = [sample.SSPS_QC;     ones(size(tsg.DAYD_WS))];
  end
  
  % Fill the structure with NaN. NaN is tested in diffTsgSample
  % -----------------------------------------------------------
  sample.SSPS_DIF    = [sample.SSPS_DIF;    NaN*ones(size(tsg.DAYD_WS))];
  sample.SSPS_SMOOTH = [sample.SSPS_SMOOTH; NaN*ones(size(tsg.DAYD_WS))];
  
  sample.SSPS_TYPE   = [sample.SSPS_TYPE;   ones(size(tsg.DAYD_WS))];

end

% Fill the structure sample with the external sample
% This measurements are of TYPE EXT
% --------------------------------------------------
if ~isempty(tsg.SSPS_EXT)

  sample.DAYD        = [sample.DAYD;        tsg.DAYD_EXT];
  sample.LATX        = [sample.LATX;        tsg.LATX_EXT];
  sample.LONX        = [sample.LONX;        tsg.LONX_EXT];
  sample.SSPS        = [sample.SSPS;        tsg.SSPS_EXT];
  if ~isempty(tsg.SSPS_EXT_QC)
    sample.SSPS_QC   = [sample.SSPS_QC;     tsg.SSPS_EXT_QC];
  else
    sample.SSPS_QC   = [sample.SSPS_QC;     ones(size(sample.DAYD_EXT))];
  end
  
  % Fill the following structures with NaN. NaN is test in diffTsgSample
  % --------------------------------------------------------------------
  sample.SSPS_DIF    = [sample.SSPS_DIF;    NaN*ones(size(tsg.DAYD_EXT))];
  sample.SSPS_SMOOTH = [sample.SSPS_SMOOTH; NaN*ones(size(tsg.DAYD_EXT))];
  
  sample.SSPS_TYPE   = [sample.SSPS_TYPE;   2*ones(size(tsg.DAYD_EXT))];

end

% Eliminate the first element if NaN
% ----------------------------------
if isnan(sample.DAYD(1))
  sample.DAYD(1)        = [];
  sample.LATX(1)        = [];
  sample.LONX(1)        = [];
  sample.SSPS(1)        = [];
  sample.SSPS_QC(1)     = [];
  sample.SSPS_DIF(1)    = [];
  sample.SSPS_SMOOTH(1) = [];
  sample.SSPS_TYPE(1)   = [];
end  

% Sort the struct sample - increasing time.
% -----------------------------------------
if ~isempty(sample)
  [sample.DAYD, iOrder] = sort(sample.DAYD);
  sample.LATX           = sample.LATX(iOrder);
  sample.LONX           = sample.LONX(iOrder);
  sample.SSPS           = sample.SSPS(iOrder);
  sample.SSPS_QC        = sample.SSPS_QC(iOrder);
  sample.SSPS_DIF       = sample.SSPS_DIF(iOrder);
  sample.SSPS_SMOOTH    = sample.SSPS_SMOOTH(iOrder);
  sample.SSPS_TYPE      = sample.SSPS_TYPE(iOrder);
end

% Update application data sample
% ------------------------------
setappdata( hTsgGUI, 'sample', sample );
