function [sample] = dev_mergesample( bucketNETCDF, bucketASCII)
%
% 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
%

% Create an empty structure with no fields
% ----------------------------------------
sample = struct('TIME',{} ,'LATITUDE',{} ,'LONGITUDE',{} ,'PSAL',{} , ...
                'PSAL_QC',{} ,'PSAL_DIF',{} ,'PSAL_SMOOTH',{} ,'TYPE', {});    

% Fill the structure sample with the bucketNETCDF data.
% This measurements are of TYPE 1
% ----------------------------------------------------
if ~isempty(bucketNETCDF)
    
    % Complete the bucket structure
    % -----------------------------
    bucketNETCDF.PSAL_DIF    = zeros(size(bucketNETCDF.PSAL));
    bucketNETCDF.PSAL_SMOOTH = zeros(size(bucketNETCDF.PSAL));
    bucketNETCDF.TYPE        = ones(size(bucketNETCDF.PSAL));
    
    sample = bucketNETCDF;

end

% Fill the structure sample with the bucketNETCDF data.
% This measurements are of TYPE 2
% ----------------------------------------------------
if ~isempty(bucketASCII)
    
    % Complete the bucketNETCDF structure
    % -----------------------------------
    bucketASCII.PSAL_DIF    = zeros(size(bucketASCII.PSAL));
    bucketASCII.PSAL_SMOOTH = zeros(size(bucketASCII.PSAL));
    bucketASCII.TYPE        = 2 * ones(size(bucketASCII.PSAL));
       
    % Fill in the sample structure
    % ----------------------------
    if isempty( sample )
        sample = bucketASCII;
    else
        sample.TIME        = [sample.TIME;        bucketASCII.TIME];
        sample.LATITUDE    = [sample.LATITUDE;    bucketASCII.LATITUDE];
        sample.LONGITUDE   = [sample.LONGITUDE;   bucketASCII.LONGITUDE];
        sample.PSAL        = [sample.PSAL;        bucketASCII.PSAL];
        sample.PSAL_QC     = [sample.PSAL_QC;     bucketASCII.PSAL_QC];
        sample.PSAL_DIF    = [sample.PSAL_DIF;    bucketASCII.PSAL_DIF];
        sample.PSAL_SMOOTH = [sample.PSAL_SMOOTH; bucketASCII.PSAL_SMOOTH];
        sample.TYPE        = [sample.TYPE;        bucketASCII.TYPE];
    end
end

% Sort the struct sample - increasing time.
% -----------------------------------------
if ~isempty(sample)
    [sample.TIME, iOrder]  = sort(sample.TIME);
    sample.LATITUDE    = sample.LATITUDE(iOrder);
    sample.LONGITUDE   = sample.LONGITUDE(iOrder);
    sample.PSAL        = sample.PSAL(iOrder);
    sample.PSAL_QC     = sample.PSAL_QC(iOrder);
    sample.PSAL_DIF    = sample.PSAL_DIF(iOrder);
    sample.PSAL_SMOOTH = sample.PSAL_SMOOTH(iOrder);
    sample.TYPE        = sample.TYPE(iOrder);
end
