Newer
Older
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

jacques.grelet_ird.fr
committed
% Get the data from the application GUI
% -------------------------------------
tsg = getappdata( hMainFig, 'tsg_data');

jacques.grelet_ird.fr
committed
% Display read file info on console
% ---------------------------------
fprintf('\nREAD_XML_FILE\n'); tic;
% Open the file
% -------------

jacques.grelet_ird.fr
committed
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
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

jacques.grelet_ird.fr
committed

jacques.grelet_ird.fr
committed
prompt = {'Enter median filter size:'};
dlg_title = 'Input for median filter';
num_lines = 1;

jacques.grelet_ird.fr
committed
size = inputdlg(prompt,dlg_title,num_lines,{num2str(tsg.medianSize)});

jacques.grelet_ird.fr
committed
% si l'utilisateur sort par cancel, pas de filtre mais on ne sauvegarde pas

jacques.grelet_ird.fr
committed
% la structure tsg

jacques.grelet_ird.fr
committed
if (isempty(size))

jacques.grelet_ird.fr
committed
tsg.medianSize = 0;

jacques.grelet_ird.fr
committed
else

jacques.grelet_ird.fr
committed
tsg.medianSize = str2num(size{1});
% Save tsg structure
% ------------------
setappdata( hMainFig, 'tsg_data', tsg);

jacques.grelet_ird.fr
committed
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
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'));

jacques.grelet_ird.fr
committed
if (tsg.medianSize > 0 )

jacques.grelet_ird.fr
committed
% filtre median

jacques.grelet_ird.fr
committed
days = medianf( days, tsg.medianSize );

jacques.grelet_ird.fr
committed
% 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' }

jacques.grelet_ird.fr
committed
if (tsg.medianSize > 0 ); data = medianf( data, tsg.medianSize ); end % cas de thermo antea

jacques.grelet_ird.fr
committed
self = set(self, 'data_1d', cles{i}, data);
assignin('base', cles{i}, data );
otherwise

jacques.grelet_ird.fr
committed
if (tsg.medianSize > 0 ); data = medianf( data, tsg.medianSize ); end % cas de thermo antea

jacques.grelet_ird.fr
committed
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);

jacques.grelet_ird.fr
committed
% % populate tsg.file structure
% % ---------------------------
% [tsg.file.pathstr, tsg.file.name, tsg.file.ext] = ...

jacques.grelet_ird.fr
committed
% 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;

jacques.grelet_ird.fr
committed
% Display time to read file on console
% ------------------------------------
t = toc; fprintf('...done (%6.2f sec).\n\n',t);
% Everything is not OK
% -------------
error = -1;
end