function calibration( hMainFig )
%
% Calibrate :
% CNDC ...... Conductivity
% SSJT ...... Jacket temperature
% SSTP ...... Precise temperature
%
% Compute salinity from calibrated conductivity and jacket temperature 
%
% Bug :
% 1 - tsg.SSPS_CAL = sw_salt( cond/sw_c3515(),t90TOt68(temp),zeros(size(cond)));
%     replaced by
%     tsg.SSPS_CAL = sw_salt( cond/sw_c3515(),temp,zeros(size(cond)));
%
%     CSIRO toolbox modified sw_salt modified to use temprature ITS90
%
%

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

% Variables
% ---------
PARA =  {'CNDC', 'SSJT', 'SSTP'};

for i = PARA

  % convert cell to char
  % --------------------
  ip = char(i);
  
  if ~isempty( tsg.(ip) )

    para1 = strcat(ip, '_LINCOEF_NEW');
    para2 = strcat(ip, '_CAL');
    key   = strcat(ip, '_LINCOEF_CONV');
    
    % get indice off slope and offset
    % -------------------------------
    slope = strmatch('SLOPE', tsg.(key));
    offset = strmatch('OFFSET', tsg.(key));
    
    % If the Slope = 1 and the offset = 0 : No calibration
    % The variable _CAL must be emptied
    % ---------------------------------------------------------------------
    if abs(1-tsg.(para1)(slope)) > .00000001 || ...
        abs(0-tsg.(para1)(offset)) > .00000001
      tsg.(para2) = tsg.(para1)(slope) * tsg.(ip) + tsg.(para1)(offset);
    else
      tsg.(para2) = [];
    end

  elseif strcmp( ip, 'CNDC' ) || strcmp( ip, 'SSJT' )

    msgbox( [ '    No ' ip ' 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 )

% Modification du 13/02/2009
% tsg.SSPS_CAL = sw_salt( ...
%       cond/sw_c3515(), t90TOt68(temp), zeros(size(cond)));
    tsg.SSPS_CAL = sw_salt( cond/sw_c3515(), 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
