From 10d4bb7030248dc3cdb6caf2b8b7698badfaf37d Mon Sep 17 00:00:00 2001
From: Jacques Grelet <jacques.grelet@ird.fr>
Date: Thu, 17 Jan 2008 13:43:26 +0000
Subject: [PATCH] just write datagui code to read XML trajectory files, need to
 be modified and update

---
 tsg_io/readTsgDataXML.m | 135 ++++++++++++++++++++++++++++++++++++++--
 1 file changed, 131 insertions(+), 4 deletions(-)

diff --git a/tsg_io/readTsgDataXML.m b/tsg_io/readTsgDataXML.m
index 1fa406a..1dc7aff 100644
--- a/tsg_io/readTsgDataXML.m
+++ b/tsg_io/readTsgDataXML.m
@@ -16,13 +16,136 @@ function [error] = readTsgDataXML( hTsgGUI, filename)
 % Caution : replace the fill-value with NaN
 % $Id$
 
+% Get the data from the application GUI
+% -------------------------------------
+tsg = getappdata( hTsgGUI, 'tsg_data');
+
+% Display read file info on console
+% ---------------------------------
+fprintf('\nREAD_XML_FILE\n'); tic;
+
 % Open the file
 % -------------
-fid = fopen( filename, 'r' );
+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_parseany( fileread( file( self ) ) );
+%data = tree.DATA{1}.CONTENT;
+
+% decode l'entete, a completer
+%tsg.CYCLE_MESURE = tree.ENTETE{1}.CYCLE_MESURE{1}.LIBELLE{1}.CONTENT;
+tsg.DATA_ACQUISITION     = tree.ENTETE{1}.CYCLE_MESURE{1}.INSTITUT{1}.CONTENT;
+tsg.PLATFORME   = tree.ENTETE{1}.PLATEFORME{1}.LIBELLE{1}.CONTENT;
+tsg.TSG_TYPE   = tree.ENTETE{1}.INSTRUMENT{1}.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
+root = get(0, 'UserData' );
+prompt = {'Enter median filter size:'};
+dlg_title = 'Input for median filter';
+num_lines = 1;
+size = inputdlg(prompt,dlg_title,num_lines,{num2str(root.medianSize)});
+% si l'utilisateur sort par cancel, pas de filtre mais on ne sauvegarde pas
+% la structure root
+if (isempty(size))
+  root.medianSize = 0;
+else  
+  root.medianSize = str2num(size{1});
+  set(0, 'UserData', root );
+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 (root.medianSize > 0 )
+        % filtre median
+        days = medianf( days, root.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 (root.medianSize > 0 ); data = medianf( data, root.medianSize ); end  % cas de thermo antea
+      self = set(self, 'data_1d', cles{i}, data);
+      assignin('base', cles{i}, data );
+    otherwise
+      if (root.medianSize > 0 ); data = medianf( data, root.medianSize ); end  % cas de thermo antea
+      self = set(self, 'data_2d', cles{i}, data);
+      assignin('base', cles{i}, data);
+  end
+end
 
-error = -1;
-if fid ~= -1
-    disp('Read XML file, not yet implemeted ...');
 %     % Read the file
 %     % -------------
 %     tsgData = fscanf(fid, '%d/%d/%d %d:%d:%d %f %f %f %f %d %f %f', ...
@@ -57,6 +180,10 @@ if fid ~= -1
     % --------------
     fclose( fid );
     
+    % Display time to read file on console
+    % ------------------------------------
+    t = toc; fprintf('...done (%6.2f sec).\n\n',t);
+
     % Everything is not OK 
     % -------------
     error = -1;
-- 
GitLab