function error = concatSample( hMainFig, data )
%
% Function that concatenates the sample structure data
% to the 'tsg' structure with _EXT fields
%
% input
%  hMainFig .... handle to the tsgqc main program
%  data ........ structure of _EXT data to trasfer to the tsg structure
%
% output
%
%  return ...... 1 data structure transfered to tsg structure
%               -1 nothing has been done
% $Id: concatSample.m 543 2010-01-14 13:36:40Z jgrelet $

error = 1;

% retrieve tsg struct
% -------------------
tsg = getappdata( hMainFig, 'tsg_data' );

% Get NO_CONTROL code value
% -------------------------
NO_CONTROL = tsg.qc.hash.NO_CONTROL.code;

if isempty( tsg.DAYD_EXT )
  
  tsg.DAYD_EXT          = data.DAYD_EXT;
  tsg.DATE_EXT          = data.DATE_EXT;
  tsg.LATX_EXT          = data.LATX_EXT;
  tsg.LONX_EXT          = data.LONX_EXT;
  tsg.SSPS_EXT          = data.SSPS_EXT;
  tsg.SSPS_EXT_QC       = int8(data.SSPS_EXT_QC);
  tsg.SSPS_EXT_TYPE     = char(data.SSPS_EXT_TYPE);
  tsg.SSPS_EXT_ANALDATE = char(data.SSPS_EXT_ANALDATE);
  tsg.SSPS_EXT_BOTTLE   = char(data.SSPS_EXT_BOTTLE);
  tsg.SSTP_EXT          = data.SSTP_EXT;
  tsg.SSTP_EXT_QC       = int8(data.SSTP_EXT_QC);
  tsg.SSTP_EXT_TYPE     = char(data.SSTP_EXT_TYPE);
  
else
  
  qstring = {'Samples have already been uploaded in TSGQC '; ' '; ...
    'Do you want to Replace or to Concatenate them?'};
  title   = 'Read samples';
  button = questdlg(qstring, title,'Replace','Concatenate','Cancel', 'Cancel');
  
  switch button
    case 'Replace'
      
      tsg.DAYD_EXT          = data.DAYD_EXT;
      tsg.DATE_EXT          = data.DATE_EXT;
      tsg.LATX_EXT          = data.LATX_EXT;
      tsg.LONX_EXT          = data.LONX_EXT;
      tsg.SSPS_EXT          = data.SSPS_EXT;
      tsg.SSPS_EXT_QC       = int8(data.SSPS_EXT_QC);
      tsg.SSPS_EXT_TYPE     = char(data.SSPS_EXT_TYPE);
      tsg.SSPS_EXT_ANALDATE = char(data.SSPS_EXT_ANALDATE);
      tsg.SSPS_EXT_BOTTLE   = char(data.SSPS_EXT_BOTTLE);
      tsg.SSTP_EXT          = data.SSTP_EXT;
      tsg.SSTP_EXT_QC       = int8(data.SSTP_EXT_QC);
      tsg.SSTP_EXT_TYPE     = char(data.SSTP_EXT_TYPE);
      tsg.EXT_DIF           = [];
      tsg.EXT_SMOOTH        = [];
      
    case 'Concatenate'
      
      % Test if at least one variable (SSPS or SSTP) exists. If one is missing
      % create the array filled with NaN
      % ----------------------------------------------------------------------
      if isempty(tsg.SSPS_EXT) && isempty(tsg.SSTP_EXT)
        error = -1;
        return;
      end
      if isempty(tsg.SSTP_EXT) && ~isempty(tsg.SSPS_EXT)
        
        tsg.SSTP_EXT      = NaN * ones( size( tsg.DAYD_EXT ) );
        label             = 'UNKN';
        tsg.SSTP_EXT_TYPE = label( ones(length(tsg.SSTP_EXT),1), :);
        tsg.SSTP_EXT_QC   = int8(NO_CONTROL) * int8(ones( size( tsg.SSTP_EXT )));
        
      end
      if isempty(tsg.SSPS_EXT) && ~isempty(tsg.SSTP_EXT)
        
        tsg.SSPS_EXT      = NaN * ones( size( tsg.DAYD_EXT ) );
        tsg.SSPS_EXT_TYPE = label( ones(length(tsg.SSTP_EXT),1), :);
        tsg.SSPS_EXT_QC   = int8(NO_CONTROL) * int8(ones(size( tsg.SSTP_EXT )));
        label             = '19500101000000';
        tsg.SSPS_EXT_ANALDATE = label( ones(length(tsg.SSTP_EXT),1), :);
        label                 = '0';
        tsg.SSPS_EXT_BOTTLE   = label( ones(length(tsg.SSTP_EXT),1), :);
        
      end
      
      tsg.DAYD_EXT          = [tsg.DAYD_EXT;        data.DAYD_EXT];
      tsg.DATE_EXT          = [tsg.DATE_EXT;        data.DATE_EXT];
      tsg.LATX_EXT          = [tsg.LATX_EXT;        data.LATX_EXT];
      tsg.LONX_EXT          = [tsg.LONX_EXT;        data.LONX_EXT];
      tsg.SSPS_EXT          = [tsg.SSPS_EXT;        data.SSPS_EXT];
      tsg.SSPS_EXT_BOTTLE   = strvcat(tsg.SSPS_EXT_BOTTLE,...
        char(data.SSPS_EXT_BOTTLE));
      tsg.SSPS_EXT_QC       = [tsg.SSPS_EXT_QC;     int8(data.SSPS_EXT_QC)];
      tsg.SSPS_EXT_TYPE     = strvcat(tsg.SSPS_EXT_TYPE,...
        char(data.SSPS_EXT_TYPE));
      tsg.SSPS_EXT_ANALDATE = strvcat(tsg.SSPS_EXT_ANALDATE,...
        char(data.SSPS_EXT_ANALDATE));
      tsg.SSTP_EXT          = [tsg.SSTP_EXT;        data.SSTP_EXT];
      tsg.SSTP_EXT_QC       = [tsg.SSTP_EXT_QC;     int8(data.SSTP_EXT_QC)];
      tsg.SSTP_EXT_TYPE     = strvcat(tsg.SSTP_EXT_TYPE, ...
        char(data.SSTP_EXT_TYPE));
      
    case 'Cancel'
      
      % Nothing is done
      % ---------------
      error = -1;
      return;
      
    otherwise
      
      % Nothing is done
      % ---------------
      error = -1;
      return;
      
  end
end

% Sort the struct tsg - increasing time.
% -----------------------------------------
if ~isempty(tsg.DAYD_EXT)
  [tsg.DAYD_EXT, iOrder] = sort(tsg.DAYD_EXT);
  tsg.DATE_EXT           = tsg.DATE_EXT(iOrder,:);
  tsg.LATX_EXT           = tsg.LATX_EXT(iOrder);
  tsg.LONX_EXT           = tsg.LONX_EXT(iOrder);
  tsg.SSPS_EXT           = tsg.SSPS_EXT(iOrder);
  tsg.SSPS_EXT_QC        = tsg.SSPS_EXT_QC(iOrder);
  tsg.SSPS_EXT_BOTTLE    = tsg.SSPS_EXT_BOTTLE(iOrder,:);
  tsg.SSPS_EXT_TYPE      = tsg.SSPS_EXT_TYPE(iOrder,:);
  tsg.SSPS_EXT_ANALDATE  = tsg.SSPS_EXT_ANALDATE(iOrder,:);
  tsg.SSTP_EXT           = tsg.SSTP_EXT(iOrder);
  tsg.SSTP_EXT_QC        = tsg.SSTP_EXT_QC(iOrder);
  tsg.SSTP_EXT_TYPE      = tsg.SSTP_EXT_TYPE(iOrder,:);
  
  % _TYPE must be of type CHAR*4
  % ----------------------------
  tsg.SSPS_EXT_TYPE = char( blanks(4), tsg.SSPS_EXT_TYPE);
  tsg.SSPS_EXT_TYPE(1,:) = [];
  tsg.SSTP_EXT_TYPE = char( blanks(4), tsg.SSTP_EXT_TYPE);
  tsg.SSTP_EXT_TYPE(1,:) = [];
  
  % _BOTTLE must be of type CHAR*4
  % ------------------------------
  tsg.SSPS_EXT_BOTTLE = char( blanks(4), tsg.SSPS_EXT_BOTTLE);
  tsg.SSPS_EXT_BOTTLE(1,:) = [];
  
  % Test : suppress measurements made at the same date
  % The comparison cannot be made using tsg.DAYD_EXT. This cannot be done using
  % the Matlab Serial Date format as there can be
  % some loss of precision. Especially when a floating number was written in
  % a file.
  %
  % Use tsg.DATE_EXT
  %
  % 2011-08-15 bug : the first algorithm used to use the same variable M 
  %                  for Month and minutes
  %
  % Use now the function str2num
  % ------------------------------------------------------------------------
  %   Y  = str2num( tsg.DATE_EXT(:,1:4) );
  %   M  = str2num( tsg.DATE_EXT(:,5:6) );
  %   D  = str2num( tsg.DATE_EXT(:,7:8) );
  %   H  = str2num( tsg.DATE_EXT(:,9:10) );
  %   M = str2num( tsg.DATE_EXT(:,11:12) );
  %   S  = str2num( tsg.DATE_EXT(:,13:14) );
  %   date = datenum( [Y M D H MN S] );
  
  % Convert string array in numeric string
  date = str2num(tsg.DATE_EXT);
  
  date_diff = find( diff( date ) == 0 );
  if ~isempty( date_diff )
    tsg.DAYD_EXT(date_diff)           = [];
    tsg.DATE_EXT(date_diff,:)         = [];
    tsg.LATX_EXT(date_diff)           = [];
    tsg.LONX_EXT(date_diff)           = [];
    tsg.SSPS_EXT(date_diff)           = [];
    tsg.SSPS_EXT_QC(date_diff)        = [];
    tsg.SSPS_EXT_BOTTLE(date_diff,:)  = [];
    tsg.SSPS_EXT_TYPE(date_diff,:)    = [];
    tsg.SSPS_EXT_ANALDATE(date_diff,:)= [];
    tsg.SSTP_EXT(date_diff)           = [];
    tsg.SSTP_EXT_QC(date_diff)        = [];
    tsg.SSTP_EXT_TYPE(date_diff,:)    = [];
  end
  
end

% Save the data in the application GUI
% ------------------------------------
setappdata( hMainFig, 'tsg_data', tsg );

end

