function updateAdjustedVariable( hMainFig )
%
% Update adjusted SSPS, SSJT, and SSTP arrays once calibration has been
% applied to these variable.
%
% The TRICK :  
% The programe test the variable ADJUSTED_ERROR as only records corrected
% compared to water sample get error values
%

% Get tsg application data
% ------------------------
tsg = getappdata( hMainFig, 'tsg_data' );

% Get VALUE_CHANGED
% -----------------
VALUE_CHANGED = get(tsg.qc.hash, 'VALUE_CHANGED', 'code');

% Variables
% ---------
PARA = [ 'SSPS'; 'SSJT'; 'SSTP' ];

for i = 1 :3

  para1 = [ PARA(i,:) '_CAL' ];
  para2 = [ PARA(i,:) '_ADJUSTED' ];
  para3 = [ PARA(i,:) '_ADJUSTED_QC' ];
  para4 = [ PARA(i,:) '_ADJUSTED_ERROR' ];
  para5 =   PARA(i,:);
  para6 = [ PARA(i,:) '_QC' ];
  
  % The variable must exist
  % -----------------------
  if ~isempty( tsg.(para5) )

    % in case of the ADJUSTED_ERROR is not initialised
    % ------------------------------------------------
    if isempty( tsg.(para4) )
      tsg.(para4) = NaN * ones(size(tsg.DAYD));
    end

    % Get Adjusted records that were not corrected using Water samples
    % Only records corrected get error values
    % ----------------------------------------------------------------
    ind = find( isnan(tsg.(para4) ) == 1);

    if ~isempty(tsg.(para1))

      tsg.(para2)(ind) = tsg.(para1)(ind);
      tsg.(para3)(ind) = VALUE_CHANGED;

    else

      % If the calibration has been canceled the ADJUSTED value is set to
      % the raw value
      % -----------------------------------------------------------------
      tsg.(para2)(ind) = tsg.(para5)(ind);
      tsg.(para3)(ind) = tsg.(para6)(ind);

    end
  end
end

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

end