function ok = 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
% ------
% boolean
%
% $Id$

% global variable VERSION, update revision number when tsg structure change
% -------------------------------------------------------------------------
global VERSION CHAR_VERSION DATE_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
      % ------------
      ok = false;
      return 
      
    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

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

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

% update structure tsg with derived value
% ---------------------------------------
updateTsgStructWithSmoothVars(hTsgGUI)

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

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

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

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

ok = true;
return 

% ----------------------------------------------------------------------
% 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.date_version          = DATE_VERSION;
    preference.autoload              = 'off';
    % Climatology
    preference.levitus_version       = {'WOA01','WOA05','ISAS13'};
    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'};
    
    % according with tsg_netcdf.xls file
    % change version number or delete tsgqc.mat
    % -----------------------------------------
    preference.date_format_variable  = 'yyyymmddHHMMSS';
    preference.date_format_attribute = 'yyyymmddHHMMSS';
    preference.positions_format_string = {'DD°MM.SS', '+-DD.CCCC'};
    preference.positions_format_value = 1;
    
    % use in tsg-average by tsg.cst.TSG_DT_SMOOTH
    % this value in in minute
    % ---------------------------------------------
    preference.dt_smooth             = '60';
    
    % Salinity, in 1 hour interval, should not depart the average for more
    % than SAL_STD_MAX standard deviation
    % -------------------------------------
    preference.ssps_stdmax           = '0.1';
    % Maximum time difference between tsg data and water sample used to
    % compute the difference : 5 minutes.
    % ------------------------------------
    preference.ws_timediff           = '5';
    
    
    % save preference struct to 'prefdir.mat' file
    % ---------------------------------------------
    save(config_file, 'preference');
    
  end

end
