function calibration( hMainFig )
%
% Calibrate :
% CNDC ...... Conductivity
% SSJT ...... Jacket temperature
% SSTP ...... Precise temperature
%
% Compute salinity from calibrated conductivity and jacket temperature 
%

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

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

for i = 1:3

  if ~isempty( tsg.(PARA(i,:)) )

    para1 = [PARA(i,:) '_LINCOEF'];
    para2 = [PARA(i,:) '_CAL'];
    
    % If the Slope = 1 and the offset = 0 : No calibration
    % The variable _CAL must be emptied
    % ---------------------------------------------------------------------
    if abs(1-tsg.(para1)(1)) > .00000001 || abs(0-tsg.(para1)(2)) > .00000001
      tsg.(para2) = tsg.(para1)(1) * tsg.(PARA(i,:)) + tsg.(para1)(2);
    else
      tsg.(para2) = [];
    end

  elseif strcmp( PARA(i,:), 'CNDC' ) || strcmp( PARA(i,:), 'SSJT' )

    msgbox( [ '    No ' PARA(i,:) ' data'],...
      'Function ''Calibration''',...
      'warn', 'modal');
    return
  end
end

% Compute salinity - Use CSIRO functions
% --------------------------------------

% Test if Conductivity or temperature has been calibrated
% -------------------------------------------------------
indC = find( isnan( tsg.CNDC_CAL ) == 0 );
indT = find( isnan( tsg.SSJT_CAL ) == 0 );
if ~isempty( indC ) || ~isempty( indT ) 

  % If SSJT has not been calibrated used SSJT and not SSJT_CAL to compute
  % SSPS_CAL
  % ---------------------------------------------------------------------
  temp = tsg.SSJT_CAL;
  ind = find( isnan( temp ) == 0 );
  if isempty(ind)
    temp = tsg.SSJT;
  end

  % If CNDC has not been calibrated used CNDC and not CNDC_CAL to compute
  % SSPS_CAL
  % ---------------------------------------------------------------------
  cond = tsg.CNDC_CAL;
  ind = find( isnan( cond ) == 0 );
  if isempty(ind)
    cond = tsg.CNDC;
  end
  
  if ~isempty( temp ) && ~isempty( cond )
    tsg.SSPS_CAL = sw_salt( ...
      cond/sw_c3515(), t90TOt68(temp), zeros(size(cond)));
  end

else
  
  % If CNDC_CAL is empty SSPS_CAL must be also empty
  % ------------------------------------------------
  tsg.SSPS_CAL = [];
  
end

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

end
