function [] = tsg_mergesample( hTsgGUI )
%
% function [sample] = dev_mergesample( bucketNETCDF, bucketASCII)
%
% Merge the 2 types of 'bucket' records, if they exists
%
% Input
% bucketNETCDF ........ Struct. bucket data included in the TSG NetCDF file
% bucketASCII ......... Struct. bucket data store in an ASCII file
%
% Ouptut
% sample .............. Struct. gathering all bucket measurements
%

% 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 an empty structure with no fields
% ----------------------------------------
sample = struct('DAYD',{} ,'LATX',{} ,'LONX',{} ,'SSPS',{} , ...
         'SSPS_QC',{} ,'SSPS_DIF',{} ,'SSPS_SMOOTH',{} ,'SSPS_TYPE', {});    

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

  sample.DAYD        = tsg.DAYD_WS;
  sample.LATX        = tsg.LATX_WS;
  sample.LONX        = tsg.LONX_WS;
  sample.SSPS        = tsg.SSPS_WS;
  sample.SSPS_QC     = tsg.SSPS_WS_QC;
  sample.SSPS_DIF    = zeros(size(sample.DAYD));
  sample.SSPS_SMOOTH = zeros(size(sample.DAYD));
  sample.SSPS_TYPE   = ones(size(sample.DAYD));

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_WS];
  sample.LATX        = [sample.LATX;        tsg.LATX_WS];
  sample.LONX        = [sample.LONX;        tsg.LONX_WS];
  sample.SSPS        = [sample.SSPS;        tsg.SSPS_WS];
  sample.SSPS_QC     = [sample.SSPS_QC;     tsg.SSPS_WS_QC];
  sample.SSPS_DIF    = [sample.SSPS_DIF;    zeros(size(tsg.DAYD_WS))];
  sample.SSPS_SMOOTH = [sample.SSPS_SMOOTH; zeros(size(tsg.DAYD_WS))];
  sample.SSPS_TYPE   = [sample.SSPS_TYPE;   tsg.SSPS_EXT_TYPE];

end

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

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