Skip to content
Snippets Groups Projects
concatSample.m 7.45 KiB
Newer Older
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
Yves Gouriou's avatar
Yves Gouriou committed
% $Id: concatSample.m 543 2010-01-14 13:36:40Z jgrelet $
tsg = getappdata( hMainFig, 'tsg_data' );

% Get NO_CONTROL code value
% -------------------------
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);
Yves Gouriou's avatar
Yves Gouriou committed
  qstring = {'Samples have already been uploaded in TSGQC '; ' '; ...
    'Do you want to Replace or to Concatenate them?'};
Yves Gouriou's avatar
Yves Gouriou committed
  title   = 'Read samples';
  button = questdlg(qstring, title,'Replace','Concatenate','Cancel', 'Cancel');
Yves Gouriou's avatar
Yves Gouriou committed
    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        = [];
Yves Gouriou's avatar
Yves Gouriou committed
    case 'Concatenate'
Yves Gouriou's avatar
Yves Gouriou committed
      % 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 )));
Yves Gouriou's avatar
Yves Gouriou committed
      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
Yves Gouriou's avatar
Yves Gouriou committed
      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,...
Yves Gouriou's avatar
Yves Gouriou committed
      tsg.SSPS_EXT_QC       = [tsg.SSPS_EXT_QC;     int8(data.SSPS_EXT_QC)];
      tsg.SSPS_EXT_TYPE     = strvcat(tsg.SSPS_EXT_TYPE,...
Yves Gouriou's avatar
Yves Gouriou committed
      tsg.SSPS_EXT_ANALDATE = strvcat(tsg.SSPS_EXT_ANALDATE,...
Yves Gouriou's avatar
Yves Gouriou committed
      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, ...
Yves Gouriou's avatar
Yves Gouriou committed
    case 'Cancel'
Yves Gouriou's avatar
Yves Gouriou committed
      % Nothing is done
      % ---------------
      error = -1;
      return;
Yves Gouriou's avatar
Yves Gouriou committed
    otherwise
Yves Gouriou's avatar
Yves Gouriou committed
      % 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,:) = [];
Yves Gouriou's avatar
Yves Gouriou committed
  % 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
Yves Gouriou's avatar
Yves Gouriou committed
  %
  % 2011-08-15 bug : the first algorithm used to use the same variable M 
  %                  for Month and minutes
  %
  % ------------------------------------------------------------------------
  %   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 );
Yves Gouriou's avatar
Yves Gouriou committed
  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