function levitus = read_file_woa01(file)
% read WOA01 Levitus T-S-O2, Stddev & nb_obs Netcdf file in memory
%
% $Id$

% check for file existence
% ------------------------
filesfound = checkforfiles( file );
if filesfound == 0; 
   warning(['Couldn''t find ', file]);
   levitus = -1;
   return;
end

% il existe un "bug" dans netcdf ligne 409, ncitem retourne une
% erreur dans la variable result

% open file in read mode
% ----------------------
nc = netcdf(which(file),'nowrite');

% Initialise la waitbar de chargement du fichier netCDF
% -----------------------------------------------------
wb = waitbar(0,'');

% We cannot change title property to 'none' (default set as 'tex') inside
% waitbar function (line 81), see
% http://www.mathworks.co.uk/matlabcentral/newsreader/view_thread/115725
% -----------------------------------------------------------------------
hchild = get(wb,'children');
htitle = get(hchild,'title');
set(htitle,'Interpreter','None');

% update waitbar title with Tex mode disable
% ------------------------------------------
waitbar(wb, ['Loading file: ' file ' Please  wait...']);

% loop for every variable
% -----------------------
for i = 1:length(var(nc))
  
  % first initialize the dimensions of the data array
  % -------------------------------------------------
  data = zeros(size(nc{i}));
  
  % then fill with values
  % ---------------------
  data(:) = nc{i}(:);
  
  % finally replace fillvalue with NaN when only fillvalue exist
  % ------------------------------------------------------------
  if ~isempty(fillval(nc{i}))
    data(data == fillval(nc{i})) = NaN;
  end
  
  % assign dynamically variable in structure levitus
  % ------------------------------------------------
  levitus.(name(nc{i})) = data;
  
  % moves the waitbar cursor 
  % ------------------------
  waitbar( i/length(nc{i}), wb);
  
end

% close waitbar
% -------------
close(wb);

% close netcdf file
% -----------------
close(nc);


%----------------------------------------------------------------------
%----------------------------------------------------------------------
function filesfound = checkforfiles( file )

if exist(file,'file');
   filesfound = 1;
else 
   filesfound = 0;
end
