function [error] = readTsgDataNetCDF( hTsgGUI, filename)
% Function to read the TSG data. Should be a NetCDF file
% GOSUD data format TSG V1.4
%
% Input
% -----
% hTsgGUI ............ Handel to the main user interface
% filename ........... Data filename
%
% Output
% ------
% error .............. 1: OK - -1 : an error occured
%
% The data are store using setappdata - Variable name : 'tsg_data'
%
% $Id$

% clear all variables in base workspace
% -------------------------------------
%clear all;

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

% Display read file info on console
% ---------------------------------
fprintf('\nREAD_NETCDF_FILE\n'); tic;

% Open netCDF file
% ----------------
nc = netcdf(filename,'read');
if isempty(nc)
  msg_error = ['TSG_GOSUD file_lecture : Open file error : ' filename];
  warndlg( msg_error, 'NetCDF error dialog');
  sprintf('...cannot locate %s\n', filename);
  error = -1;
  return;
end

% Display more info about read file on console
% --------------------------------------------
fprintf('...reading %s : ', filename);

% populate tsg.file structure
% ---------------------------
tsg.file.name = filename;
tsg.file.type = 'NETCDF';


% get global attributes: meta data
% --------------------------------
global_att = att(nc);

for i=1:length(global_att)
  
  % extract name and value from netcdf globals attributes
  % -----------------------------------------------------
  attribute = name(global_att{i});
  value     = global_att{i}(:);
  
  % assign globals attributes in base workspace
  % -------------------------------------------
  assignin('base', attribute, value);
  
  % populate tsg structure with globals attributes
  % ----------------------------------------------
  tsg.(attribute) = value;
end

% get variables describing TSG installation
% -----------------------------------------
variables = var(nc);

for i=1:length(variables)
  
  % extract name and value from netcdf variables
  % --------------------------------------------
  variable = name(variables{i});
  value    = variables{i}(:);
  
   % assign netcdf variables in base workspace
   % -----------------------------------------
   assignin('base', variable, value);
   
   % populate tsg structure with netcdf variables
   % --------------------------------------------
   tsg.(variable) = value;
   
   % transforme julian days variables to Matlab datenum
   % --------------------------------------------------
   if strmatch('DAYD', variable)
     tsg.(variable) = julianToDatenum(tsg.(variable));
   end
end

% Save the data in the application GUI
% ------------------------------------
setappdata( hTsgGUI, 'tsg_data', tsg );

% Clear the Workspace
% -------------------
clear tsgdata

% Close the file
% --------------
%endef(nc)
close(nc);

% Display time to read file on console
% ------------------------------------
t = toc; fprintf('...done (%5.2f sec).\n\n',t);

% Everything OK
% -------------
error = 1;

end
