Skip to content
Snippets Groups Projects
tsg_mergesample.m 6.50 KiB
function [] = tsg_mergesample( hTsgGUI, PARA )
%
% 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');

% There are only SSTP samples. The SSJT time series should
% be compared to the SSTP samples. To simplfies the soft we use a
% variable SSJT sample equal to the SSTP sample structure
% ---------------------------------------------------------------
PARA_OLD = PARA;
if strcmp( PARA, 'SSJT' )
  PARA_OLD = PARA;
  PARA     = 'SSTP';
end

% 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 , ...
                'PARM',NaN ,        'PARM_QC',NaN ,   'PARM_DIF',NaN ,...
                'PARM_SMOOTH',NaN , 'PARM_TYPE', NaN, 'PARM_INDICE', NaN);    

% Fill the structure sample with the bucket data.
% WS exist only for SSPS 
% This measurements are of TYPE 1
% ------------------------------------------------
if strcmp( PARA, 'SSPS') && ~isempty(tsg.([PARA '_WS']))

  sample.DAYD        = [sample.DAYD;        tsg.DAYD_WS];
  sample.LATX        = [sample.LATX;        tsg.LATX_WS];
  sample.LONX        = [sample.LONX;        tsg.LONX_WS];
  sample.PARM        = [sample.PARM;        tsg.([PARA '_WS']) ];
  if ~isempty(tsg.([PARA '_WS_QC']))
    sample.PARM_QC   = [sample.PARM_QC;     tsg.([PARA '_WS_QC']) ];
  else
    sample.PARM_QC   = [sample.PARM_QC;     ones(size(tsg.DAYD_WS))];
  end
  
  % Fill the structure with NaN. NaN is tested in diffTsgSample
  % -----------------------------------------------------------
  sample.PARM_DIF    = [sample.PARM_DIF;    NaN*ones(size(tsg.DAYD_WS))];
  sample.PARM_SMOOTH = [sample.PARM_SMOOTH; NaN*ones(size(tsg.DAYD_WS))];
  
  % INDICE is used to put back the QC once the validation procedure has
  % been applied
  % -------------------------------------------------------------------
  sample.PARM_INDICE = [sample.PARM_INDICE; [1:length(tsg.DAYD_WS)]'];
  
  % Used for the plots : WS are given a type 1
  % ------------------------------------------
  sample.PARM_TYPE   = [sample.PARM_TYPE;   ones(size(tsg.DAYD_WS))];

end

% Fill the structure sample with the external sample
% This measurements are of TYPE EXT
% --------------------------------------------------
if ~isempty(tsg.([PARA '_EXT']))

  sample.DAYD        = [sample.DAYD;        tsg.DAYD_EXT];
  sample.LATX        = [sample.LATX;        tsg.LATX_EXT];
  sample.LONX        = [sample.LONX;        tsg.LONX_EXT];
  sample.PARM        = [sample.PARM;        tsg.([PARA '_EXT'])];
  if ~isempty(tsg.([PARA '_EXT_QC']))
    sample.PARM_QC   = [sample.PARM_QC;     tsg.([PARA '_EXT_QC'])];
  else
    sample.PARM_QC   = [sample.PARM_QC;     ones(size(sample.DAYD_EXT))];
  end
  
  % Fill the following structures with NaN. NaN is test in diffTsgSample
  % --------------------------------------------------------------------
  sample.PARM_DIF    = [sample.PARM_DIF;    NaN*ones(size(tsg.DAYD_EXT))];
  sample.PARM_SMOOTH = [sample.PARM_SMOOTH; NaN*ones(size(tsg.DAYD_EXT))];
  
  % Used for the plots : EXT sample are given a type 2
  % --------------------------------------------------
  sample.PARM_TYPE   = [sample.PARM_TYPE;   2*ones(size(tsg.DAYD_EXT))];
  
  % INDICE is used to put back the QC once the validation procedure has
  % been applied
  % -------------------------------------------------------------------
  sample.PARM_INDICE = [sample.PARM_INDICE; [1:length(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.PARM(1)        = [];
  sample.PARM_QC(1)     = [];
  sample.PARM_DIF(1)    = [];
  sample.PARM_SMOOTH(1) = [];
  sample.PARM_TYPE(1)   = [];
  sample.PARM_INDICE(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.PARM           = sample.PARM(iOrder);
  sample.PARM_QC        = sample.PARM_QC(iOrder);
  sample.PARM_DIF       = sample.PARM_DIF(iOrder);
  sample.PARM_SMOOTH    = sample.PARM_SMOOTH(iOrder);
  sample.PARM_TYPE      = sample.PARM_TYPE(iOrder);
  sample.PARM_INDICE    = sample.PARM_INDICE(iOrder);
  
  % Test if DAYD is strictly increasing
  % -----------------------------------
  increasingInd = 1:length(sample.DAYD);
  diffDay       = diff(sample.DAYD);
  ind           = find(diffDay == 0);
  if ~isempty(ind)
    increasingInd(ind) = [];
  end
  
  % put the structure sample in the tsg structure
  % ---------------------------------------------
  if ~isempty( increasingInd )
    tsg.DAYD_SPL                  = sample.DAYD(increasingInd);
    tsg.LATX_SPL                  = sample.LATX(increasingInd);
    tsg.LONX_SPL                  = sample.LONX(increasingInd);
    tsg.([PARA '_SPL'])           = sample.PARM(increasingInd);
    tsg.([PARA '_SPL_QC'])        = sample.PARM_QC(increasingInd);
    tsg.([PARA '_SPL_DIF'])       = sample.PARM_DIF(increasingInd);
    tsg.([PARA '_SPL_SMOOTH'])    = sample.PARM_SMOOTH(increasingInd);
    tsg.([PARA '_SPL_TYPE'])      = sample.PARM_TYPE(increasingInd);
    tsg.([PARA '_SPL_INDICE'])    = sample.PARM_INDICE(increasingInd);
  end
  
end

% There are only SSTP samples. The SSJT time series should
% be compared to the SSTP samples. To simplify the soft we use a
% variable SSJT sample equals to the SSTP sample structure
% ---------------------------------------------------------------
if strcmp( PARA_OLD, 'SSJT' )
  if ~isempty( tsg.DAYD_SPL )
    tsg.SSJT_SPL         = tsg.SSTP_SPL;
    tsg.SSJT_SPL_QC      = tsg.SSTP_SPL_QC;
    tsg.SSJT_SPL_DIF     = tsg.SSTP_SPL_DIF;
    tsg.SSJT_SPL_SMOOTH  = tsg.SSTP_SPL_SMOOTH;
    tsg.SSJT_SPL_TYPE    = tsg.SSTP_SPL_TYPE;
    tsg.SSJT_SPL_INDICE  = tsg.SSTP_SPL_INDICE;
  end
end

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