function [error] = readTsgDataXML( hMainFig, filename)
% [error] = readTsgDataXML( hMainFig, filename)
% Function to read the TSG data XML file
%
% Input
% -----
% hMainFig ............ 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'
%
% Function not yet implemented
% Caution : replace the fill-value with NaN
%
% $Id$

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

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

% Open the file
% -------------
fd = fopen( file(self), 'rt' );
if( fd == -1 )
  warndlg( msg_error, 'XML error dialog');
  sprintf('...cannot locate %s\n', filename);
  error = -1;
  return;
end

fprintf('...reading %s : ', filename);

h = hashtable;

%% parse le fichier XML
tree = xml_read( fileread( file( self ) ) );

% decode l'entete, a completer
%tsg.CYCLE_MESURE = tree.ENTETE.CYCLE_MESURE.LIBELLE;
tsg.DATA_ACQUISITION     = tree.ENTETE.CYCLE_MESURE.INSTITUT;
tsg.PLATFORME   = tree.ENTETE.PLATEFORME.LIBELLE;
tsg.TSG_TYPE   = tree.ENTETE.INSTRUMENT.ATTRIBUTE.TYPE;
fclose(fd);

%% deuxieme lecture des donnees dans le fichier apres la balise <DATA>
% on lit la deuxieme ligne contenant les entetes des colonnes
fd = fopen( file(self), 'rt' );
while ~feof(fd)
  line = fgetl(fd);
  [tok match] = regexp(line,'<(\w+)>','tokens');  
  if isempty(match), continue, end
  if strmatch(tok{1},'DATA'), break, end
end  

% lit la ligne contenant les entetes des colonnes
header = fgetl(fd);
%disp( ['Entete: ', header] );
variables = strread( header, '%s' ); 
columns = length( variables );

% cree l'equivalent d'une table de hashage des codes Roscop (GF3)
% valides -> colonne associe
for i = 1: columns
  if (~strcmp(variables(i),'N/A') && ~strcmp(variables(i),'YEAR') )
    h = put( h, variables{i}, i );
  elseif( strcmp(variables(i),'YEAR') )
    col_year = i;
  end
end 

cles = keys(h);
val  = values(h);

[A,count] = fscanf( fd, '%g', [columns,inf] );
% 
nb = count / columns;
disp( [num2str(nb),' records'] );
% 
% on inverse la matrice
A=A';

% test si filtre median necessaire

prompt = {'Enter median filter size:'};
dlg_title = 'Input for median filter';
num_lines = 1;
size = inputdlg(prompt,dlg_title,num_lines,{num2str(tsg.medianSize)});
% si l'utilisateur sort par cancel, pas de filtre mais on ne sauvegarde pas
% la structure tsg
if (isempty(size))
  tsg.medianSize = 0;
else  
  tsg.medianSize = str2num(size{1});
  % Save tsg structure 
  % ------------------
  setappdata( hMainFig, 'tsg_data', tsg);
end

% gere les dates
year = A(:,col_year);
year_base = year(1);
% met la date au format YYYYMMDDHHmmss
ref_date  = [num2str(year_base) '0101000000'];

%% attributs globaux (data_0d)
assignin('base', 'CYCLE_MESURE', tsg.CYCLE_MESURE );
assignin('base', 'INSTITUT', tsg.CYCLE_MESURE );
assignin('base', 'INSTITUT', tsg.PLATFORME );
assignin('base', 'INSTRUMENT', tsg.TSG_TYPE );

%% variables 1 dimension (data_1d)
assignin('base', 'REFERENCE_DATE_TIME', tsg.REFERENCE_DATE_TIME );

for i = 1 : length(cles)
  data = A(:,val{i});
  data( find( data > 1e35 ) ) = NaN;
  switch cles{i}
    case 'DAYD'
      % pre-allocation
      %days = ones(nb,1);
      %for k=1: nb  % DAYD
      %  days(k) = to_day(year_base,to_date(year(k), data(k), 'n'));
      %end
      days = to_day(year_base,to_date(year,data,'n'));
      if (tsg.medianSize > 0 )
        % filtre median
        days = medianf( days, tsg.medianSize );
        % affichage de la taille apres 
        fprintf('...find %d records after median filter', length(days) );
      end  % cas de thermo antea
      self = set(self, 'data_1d', cles{i}, days);
      assignin('base', cles{i}, days );
    case {'LATX','LONX' }
      if (tsg.medianSize > 0 ); data = medianf( data, tsg.medianSize ); end  % cas de thermo antea
      self = set(self, 'data_1d', cles{i}, data);
      assignin('base', cles{i}, data );
    otherwise
      if (tsg.medianSize > 0 ); data = medianf( data, tsg.medianSize ); end  % cas de thermo antea
      self = set(self, 'data_2d', cles{i}, data);
      assignin('base', cles{i}, data);
  end
end

%     % Read the file
%     % -------------
%     tsgData = fscanf(fid, '%d/%d/%d %d:%d:%d %f %f %f %f %d %f %f', ...
%                      [13 Inf])';
%     
%     % Every variable are put in a structure
%     % -------------------------------------
%     tsg.TIME       = datenum(tsgData(:,3), tsgData(:,2),tsgData(:,1), ...
%                              tsgData(:,4),tsgData(:,5),tsgData(:,6));
%     tsg.LATITUDE   = tsgData(:,7);
%     tsg.LONGITUDE  = tsgData(:,8);
%     tsg.TEMP_TSG   = tsgData(:,9);
%     tsg.PSAL       = tsgData(:,10);
%     tsg.PSAL_QC    = tsgData(:,11);
%     tsg.PSAL_ADJ   = tsgData(:,12);
%     tsg.PSAL_ERR   = tsgData(:,13);

%     % populate tsg.file structure
%     % ---------------------------
%     [tsg.file.pathstr, tsg.file.name, tsg.file.ext] = ...
%       fileparts(filename);
%     tsg.file.type = 'XML';
%
%     % Save the data in the application GUI
%     % ------------------------------------
%     setappdata( hMainFig, 'tsg_data', tsg );
    
    % Clear the Workspace
    % -------------------
    clear tsgdata
    
    % Close the file
    % --------------
    fclose( fid );
   
    % Perform somme automatic tests
    % -----------------------------
    automaticQC( hMainFig )
    
    % Keep somme information for the log file
    % ---------------------------------------
    tsg.report.tsgfile = filename;

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

    % Everything is not OK 
    % -------------
    error = -1;
end
