Skip to content
Snippets Groups Projects
readAsciiTsg.m 6.2 KiB
Newer Older
function [error] = readAsciiTsg( hMainFig, filename )
jacques.grelet_ird.fr's avatar
jacques.grelet_ird.fr committed
% readAsciiTsg( hMainFig, filename )
% Function to read TSG data in ASCII format.
%
% Input
% -----
jacques.grelet_ird.fr's avatar
jacques.grelet_ird.fr committed
% hMainFig ........... Handle to the main user interface
% filename ........... Data filename
% type ............... Can read 3 types of data : WS, EXT, TSG
%
% Output
% ------
% error .............. 1: OK - -1 : an error occured
jacques.grelet_ird.fr's avatar
jacques.grelet_ird.fr committed
%
% $Id$
% use waitbar per default
% ----------------------
wbool = true;

% update waitbar title with Tex mode disable
% ------------------------------------------
if wbool
  %set(0,'DefaulttextInterpreter','none')
  [~,file,ext] = fileparts(filename);
  msg = sprintf('Loading file %s.  Please  wait...', strcat(file, ext));
  wb = waitbar(0, msg);
  hchild = get(wb,'children');
  htitle = get(hchild,'title');
  set(htitle,'Interpreter','None');
end

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

% Get the fieldnames of tsg structure
% -----------------------------------
tsgNames     = fieldnames(tsg);
nbFieldNames = length( tsgNames );

% Get NO_CONTROL code value
% -------------------------

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

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

% 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 header till the header line has been read
% --------------------------------------------------
OK = 0;
while ~OK

  % Read every line
  % ---------------
  line = fgetl( fid );
  c = textscan( line, '%s');

  switch char( c{1}(1) )

    case '%HEADER'

      % Read the header then quit the while loop
      % ----------------------------------------
      header = c{1}(2:end);
      nHeader = length( header );
      OK = 1;

    otherwise

      % Get the parameter Name (Delete '%')
      % ----------------------------------
      Para = char( strtok(c{1}(1), '%') );

      % Read the parameter value
      % ------------------------
      ind = strmatch( Para, tsgNames);
      if ~isempty( ind )
        
        % patch added for composed name
        % -----------------------------
        a = c{1}(2:end)';
        str=[];
        for i=a
          str = sprintf('%s %s', str, char(i));
        end
        
        % copy to tsg struct & remove leading and trailing white space
        % ------------------------------------------------------------
        tsg.(Para) = strtrim(str);
% Build the format depending on the header parameters
% 1 - Decimate the HEADER - The 7th first parameters are always
%     %HEADER YEAR MNTH DAYX hh mm ss
% 2 - The 6 Date and time parametes are read in %d
% -------------------------------------------------------------
theFormat = strcat('%d %d %d %d %d %d', repmat(' %f', 1, nHeader - 6));

% Read the data in a cell
% -----------------------
cellData = textscan( fid, theFormat );

% Convert cell to a structure
% ---------------------------
s = cell2struct(cellData, header, 2);

% get the number of line read
% ----------------------------
nlines = length(s.YEAR);

% moves the waitbar cursor
% ------------------------
for i=1 : nlines
  if wbool && (mod(i, 100) == 0)
    waitbar( i/nlines, wb);
  end
end

clear cellData

% Date (y m d h m s) in the first 6 elements in data
% --------------------------------------------------
yy = double( s.(char(header(1))) );
mm = double( s.(char(header(2))) );
dd = double( s.(char(header(3))) );
hh = double( s.(char(header(4))) );
mi = double( s.(char(header(5))) );
ss = double( s.(char(header(6))) );

tsg.report.tsgfile = filename;
tsg.DAYD = datenum(yy, mm, dd, hh, mi, ss);
% Convert date using pre-defined format
% -------------------------------------
tsg.DATE = datestr( tsg.DAYD, tsg.preference.date_format_variable );

nHeader = length( header );
for i = 7 : nHeader

  % QC should be converted in int8
  % ----------------------------------
  if ~isempty( strfind(char(header(i)), '_QC'))
    tsg.(char(header(i))) = int8(s.(char(header(i))));
  else
    tsg.(char(header(i))) = s.(char(header(i)));
  end
% add POSITION_QC
% ---------------
tsg.POSITION_QC = int8(tsg.qc.hash.NO_CONTROL.code) * ones(nlines, 1, 'int8');

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

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

tsg.file.type = 'ASCII';

% check if .ini file exist in data
% --------------------------------
file_ini = fullfile(tsg.file.pathstr, strcat(tsg.file.name, '.ini'));

% display information on command window
% --------------------------------------
fprintf(' %d lines', nlines - 1);
fprintf('\n...reading %s\n', file_ini);

% check if filename exist and should be open in read mode
% -------------------------------------------------------
file_ini_id = fopen(file_ini, 'r');

if (file_ini_id ~= -1)
  
  % Save the data in the application GUI
  % ------------------------------------
  setappdata( hMainFig, 'tsg_data', tsg );
  
  % read .ini file
  % --------------
  readTsgIniLabview(hMainFig, file_ini_id);
  
  % Get the tsg struct from the application GUI
  % -------------------------------------------
  tsg = getappdata( hMainFig, 'tsg_data');
  
end


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

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

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

% Close waitbar
% -------------
if wbool
  close(wb)
end

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

end