function tsg_preferences(hTsgGUI, app_name, DEFAULT_PATH_FILE)
% tsg_preferences(S1,S2) returns structure tsg saved in S1.mat file
% and with S2 internal version number.
% If S1.mat dosn't exist, call tsg_initialisation to create it in
% prefdir directory
%
% Input
% -----
% hTsgGUI    ............ Handle to the main user interface
% app_name   ............ application name
%
% Output
% ------
% none
%
% $Id$

% global variable VERSION, update revision number when tsg structure change
% -------------------------------------------------------------------------
global VERSION CHAR_VERSION

% Initialize loading preference file waitbar
% ------------------------------------------
wb = waitbar(0, 'Loading preference file. Please  wait...');

% display waitbar
% ---------------
waitbar( 1/10, wb, 'loading structure tsg' );

% Construct config file path
% --------------------------
config_file = [prefdir, filesep, app_name, '.mat'];

% Open config file
% ----------------

% read preference mat file
% ------------------------
fid = fopen( config_file, 'r' );

% test fopen return value
% -----------------------
switch fid
  
  % preference file is missing
  % --------------------------
  case -1
    
    % create new tsg structure and save it to appdata
    % -----------------------------------------------
    preference = new_config;
    
    % file exist and is open, fid > 2
    % -------------------------------
  otherwise
    
    % Pointer set to watch during loading preference file
    % ---------------------------------------------------
    set( hTsgGUI, 'Pointer', 'watch' );
    
    try
      
      % if file exist, load preference struct to workspace
      % --------------------------------------------------
      load( config_file, 'preference' );
      
    catch
      
      % display warning
      % ---------------
      warning('tsgqc_GUI:tsg_preferences', ...
        'Can''t load preference file ''%s''', config_file);
      
      % quit program
      % ------------
      quitProgram(DEFAULT_PATH_FILE, hTsgGUI);
      
    end
    
    % If there no field version or wrong version, initialize
    % -------------------------------------------------------
    if ~(exist('preference','var') && ...
        isfield( preference, 'char_version') && ...
        strcmp(preference.char_version, CHAR_VERSION))
      
      % call new config private function
      % --------------------------------
      preference = new_config;
      
    end
    
end

% call default tsg_initialisation
% -------------------------------
tsg_initialisation(hTsgGUI);

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

% add preference struct to tsg struct
% -----------------------------------
tsg.preference = preference;

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

% display waitbar
% ---------------
waitbar( 1/2, wb, 'loading structure tsg' );

% Close waitbar
% -------------
close(wb)

% cursor back to normal
% ---------------------
set(hTsgGUI,'Pointer','arrow');


% ----------------------------------------------------------------------
% nested function new_config
% generate a new preference structure with default values
% ----------------------------------------------------------------------
  function preference = new_config
    
    % add tsg.preference to default values
    % -------------------------------------
    preference.version               = VERSION;
    preference.char_version          = CHAR_VERSION;
    preference.autoload              = 'off';
    % Climatology
    preference.levitus_version       = {'WOA01','WOA05'};
    preference.levitus_value         = 2;  % WOA05
    preference.levitus_depth_string  = {'0','10'};
    preference.levitus_depth_value   = 1;
    preference.coeff_type_string     = {'use A-D', 'use G-J'};
    preference.coeff_type_value      = 2;
    % Plot
    preference.map_resolution_string = {'low','medium','high'};
    preference.map_resolution        = 1;
    preference.plot_connected_string = {'none', '-', '--', ':', '-.'};
    preference.plot_connected_value  = 1;  % 0, line not connected
    % QC test
    preference.flow_min_string       = {'1'};
    preference.press_min_string      = {'1'};
    preference.ship_speed_min_string = {'1'};
    preference.ssps_min_string       = {'0'};
    preference.ssps_max_string       = {'50'};
    preference.ssjt_min_string       = {'-3'};
    preference.ssjt_max_string       = {'40'};
    preference.sstp_min_string       = {'-3'};
    preference.sstp_max_string       = {'40'};
    
    % save preference struct to 'prefdir.mat' file
    % ---------------------------------------------
    save(config_file, 'preference');
    
  end

end