Skip to content
Snippets Groups Projects
readTsgDataTsg.m 3.76 KiB
Newer Older
function [error] = readTsgDataTsg( hMainFig, filename)
% Function to read the TSG data. Should be a NetCDF file
%
% Input
% -----
% hMainFig ............ Handle 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'
%
%
% Caution : replace the fill-value with NaN
% $Id: readTsgDataTxt.m 297 2008-03-20 16:33:37Z ygouriou $

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

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

% Open the file
% -------------
fid = fopen( filename, 'r' );

% Check file
% -----------
if fid == -1
  msg_error = ['TSG_GOSUD file_lecture : Open file error : ' filename];
  warndlg( msg_error, 'ASCII error dialog');
  sprintf('...cannot locate %s\n', filename);
  error = -1;
  return;
end

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

% Read the file
% -------------
c = textscan( fid, '%s', 1 );
while ~strcmp( char( c{1} ) , '%DATA' );
  switch char( c{1} )
    case '%HEADER'
      nHeader = 0;
      while ~strcmp( char( c{1} ) , '%DATA' );
        c = textscan( fid, '%s', 1 );
        nHeader = nHeader + 1;
        header{ nHeader } = c;
      end
    otherwise
      
      % Delete '%' in the parameter Name
      % --------------------------------
      Para = strtok(char( c{1} ), '%');
      c = textscan( fid, '%s', 1);
      tsg.(Para) = char( c{1} );
      c = textscan( fid, '%s', 1 );
  end
end

% Suppress the last header element. Should be <DATA>
% --------------------------------------------------
nHeader = nHeader - 1;

% Read the data in a cell
% -----------------------

% Convert the data to double
% --------------------------
nCol  = nHeader;
mLine = length( cellData{:} )/nCol;
data = NaN * ones( mLine, nCol );
ind = 0;
for i = 1:mLine
  for j = 1:nCol
    ind = ind + 1;
    data(i,j) = cellData{1}(ind);
  end
end

clear cellData

% Every variable are put in the tsg structure
% --------------------------------------------

% Date (y m d h m s) in the first 6 elements in data
% --------------------------------------------------
tsg.DAYD = datenum(data(:,1), data(:,2),data(:,3),data(:,4),data(:,5),data(:,6));

% Loop over the header. Skip the 6 first elements
% -----------------------------------------------
for i = 7 : nHeader
  
  % QC should be converted in int8
  % ------------------------------
  if ~isempty( strfind(char(header{i}{1}), '_QC'))
    tsg.(char(header{i}{1})) = int8( data(:,i) );
  else
    tsg.(char(header{i}{1})) = data(:,i);
  end
end

% populate tsg.file structure
% ---------------------------
[tsg.file.pathstr, tsg.file.name, tsg.file.ext, tsg.file.versn] = ...
                                                         fileparts(filename);
%tsg.file.name = filename;
tsg.file.type = 'ASCII';

% Keep somme information for the log file
% ---------------------------------------
tsg.report.tsgfile = filename;

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

% Perform somme automatic tests
% -----------------------------
automaticQC( hMainFig );

% Close the file
% --------------
fclose( fid );

% Clear the Workspace
% -------------------
clear data

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

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

end