function updateTsgStruct(hMainFig)
%
% This function is called after reading data and update tsg structure
% with common values
%
% Input
% -----
% hMainFig ............ Handel to the main user interface
%
% $Id$


% Compute ship velocity from positions if sog not available
% Must be done first as we use getappdata and setappdata within
% the function
% ---------------------------------------------------------
shipVelocity( hMainFig );

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

PARA  = {'SSPS', 'SSJT', 'SSTP'};

% Get NO_CONTROL and INTERPOLATED_VALUE codes
% a modifier !!!!!!!!!!!!!
% -------------------------------------------
s = tsg.qc.hash.NO_CONTROL;
NO_CONTROL = s.code;

% shift to [-180 180] longitude by default
% ----------------------------------------
tsg.LONX = mod(tsg.LONX + 180, 360) - 180;

% Suppress longitude discontinuity at -180 or 180 crossing
% --------------------------------------------------------
deflon = find(isfinite(tsg.LONX));
dlon = diff(tsg.LONX(deflon));
tsg.indcross = find(abs(dlon) > 180);
if ~isempty(tsg.indcross)
    tsg.loncross = sign(dlon(tsg.indcross)) * 360;
    tsg.indcross = deflon(tsg.indcross);
    for i=1:length(tsg.indcross)
        tsg.LONX(tsg.indcross(i)+1:end) = ...
          tsg.LONX(tsg.indcross(i)+1:end) - tsg.loncross(i);
    end
end
tsg.lonplus = 0;
tsg.lonmod  = 0;

% get min and max values for position and set to globals attributes
% -----------------------------------------------------------------
tsg.SOUTH_LATX = min(tsg.LATX);
tsg.NORTH_LATX = max(tsg.LATX);
if (max(tsg.LONX)-min(tsg.LONX)) > 360
    tsg.WEST_LONX  = -180;
    tsg.EAST_LONX  = 180;
else
    tsg.WEST_LONX  = mod(min(tsg.LONX)+180, 360) - 180;
    tsg.EAST_LONX  = mod(max(tsg.LONX)+180, 360) - 180;
end

% get date start and end value and set to globals attributes
% -----------------------------------------------------------------
date           = datestr(min(tsg.DAYD), 30);
tsg.DATE_START = [date(1:8) date(10:15)];
date           = datestr(max(tsg.DAYD), 30);
tsg.DATE_END   = [date(1:8) date(10:15)];

% Variables must exists
% ---------------------
for para = PARA

  para = char(para);
  
  if ~isempty( tsg.(para) ) && isempty( tsg.([para '_QC']) )

    % If QC variables are emptied, fill them with NO_CONTROL code value
    % -----------------------------------------------------------------
    tsg.([para '_QC']) = castByteQC( NO_CONTROL, tsg.DAYD );

  end
  
  if ~isempty( tsg.(para) ) && isempty( tsg.([para '_ADJUSTED_QC']) )

    % If QC variables are emptied, fill them with NO_CONTROL code value
    % -----------------------------------------------------------------
    tsg.([para '_ADJUSTED_QC']) = castByteQC( NO_CONTROL, tsg.DAYD );

  end
end

% initialize POSITION_QC
% ----------------------
tsg.('POSITION_QC') = castByteQC( NO_CONTROL, tsg.DAYD );

% Empty variables filled with NaN
% -------------------------------
names = fieldnames( tsg );
for i = 1: length(names)
  para = char( names(i) );
  if ~isstruct(tsg.(para)) && ~isempty(tsg.(para)) && ~iscell(tsg.(para))
    A = tsg.(para);
    ind = find( isnan( A ) == 0 );
    if isempty( ind )
      tsg.(para) = [];
    end
  end
end

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

% fill tsg calibration variables
% ------------------------------
updateTsgStructWithCalCoeff(hMainFig);

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

% Initialise  'variables_LINCOEF'
% in tsg_initialisation now
% -------------------------------
var ={'SSJT_LINCOEF', 'CNDC_LINCOEF', 'SSTP_LINCOEF'};
lin_type = {padding('SLOPE',tsg.dim.COEF_CONV_SIZE),...
            padding('OFFSET',tsg.dim.COEF_CONV_SIZE)};
lin_val  = [1,0];

% loop over each variables
% ------------------------
for i = 1:numel(var)
  
  % check if var_LINCOEF not empty
  % -----------------------------------  
  if isempty(tsg.(var{i}))
    for j = 1:tsg.dim.LINCOEF
      tsg.(var{i})(j) = lin_val(j);
    end
  end
  
  % rewrite LINCOEF's
  % convert cell array of string to char array
  % ------------------------------------------
  tsg.([var{i} '_CONV']) = char(lin_type);
  
end


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

end