Newer
Older
Yves Gouriou
committed
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
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
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
% Conversion d'une liste de fichier NetCDF TSG en fichier ASCCI
%
% La liste des fichiers netCDF doit tre plac dans un dichier ASCII.
% un nom de fichier par ligne
%
clear all;
clc;
% Repertoire des fichiers NetCdf en lecture
% -----------------------------------------
dirIn = 'D:\temp\';
% Repertoire des fichiers ASCII en criture
% -----------------------------------------
dirOut = dirIn;
% Nom du fichier qui liste les fichiers netCdf
% --------------------------------------------
fichierListe = 'list_nc.txt';
% Liste des paramtres crire - Create empty structure array
% ------------------------------------------------------------
PARA = { 'CNDC'; 'CNDC_CAL'; ...
'SSPS'; 'SSPS_QC'; 'SSPS_CAL';...
'SSPS_ADJUSTED'; 'SSPS_ADJUSTED_QC'; 'SSPS_ADJUSTED_ERROR'; ...
'SSJT'; 'SSJT_QC'; 'SSJT_CAL';...
'SSJT_ADJUSTED'; 'SSJT_ADJUSTED_QC'; 'SSJT_ADJUSTED_ERROR'; ...
'SSTP'; 'SSTP_QC'; 'SSTP_CAL';...
'SSTP_ADJUSTED'; 'SSTP_ADJUSTED_QC'; 'SSTP_ADJUSTED_ERROR'};
nPara = size( PARA, 1 );
% Lecture du nom des fichiers convertir. Ceux-ci se trouvent dans
% le fichier liste_nc.txt
% -----------------------------------------------------------------
list_nc = textread([dirIn fichierListe],'%s');
% Conversion du nom des fichiers en caracteres
% --------------------------------------------
list_nc = char(list_nc);
% Boucle sur les fichiers convertir
% ----------------------------------
nfiles = size(list_nc,1);
for i = 1 : nfiles
% Create structure array
% ----------------------
tsg = struct;
fileName = deblank(list_nc(i,:));
display( ['Traitement du fichier : ' fileName]);
% Ouverture d'un fichier NetCdf en lecture
% ----------------------------------------
nc = netcdf([dirIn fileName],'read');
if ~isempty(nc)
% Creation et ouverture du fichier en criture .tsgqc
% ---------------------------------------------------
ind = find(fileName == '.');
fileNameOut = fileName(1 : ind-1);
fid = fopen([dirOut fileNameOut '.tsgqc'],'wt');
display( [' Ecriture du fichier : ' fileNameOut]);
% --------------------------------
% Get global attributes: meta data
% --------------------------------
global_att = att(nc);
for iatt = 1:length(global_att)
% extract name and value from netcdf globals attributes
% -----------------------------------------------------
attribute = name(global_att{iatt});
value = global_att{iatt}(:);
% assign globals attributes in base workspace
% -------------------------------------------
assignin('base', attribute, value);
% populate tsg structure with globals attributes
% ----------------------------------------------
tsg.(attribute) = value;
end % end of attributes loop
% ----------------------------
% Get variables describing TSG
% ----------------------------
variables = var(nc);
for ivar = 1:length(variables)
% extract name and value from netcdf variables
% --------------------------------------------
variable = name(variables{ivar});
nv = nc{ivar};
% use autonan mode, remplace fillValue with NaN
% ---------------------------------------------
nv = autonan(nv, true);
% populate tsg structure with netcdf variables
% --------------------------------------------
tsg.(variable) = nv(:);
% assign netcdf variables in base workspace
% -----------------------------------------
assignin('base', variable, nv(:));
end % end of variables loop
% Initialise le tableau des variables crire
% --------------------------------------------
nbRecords = length( tsg.DAYD );
tsg_data = NaN * ones(nPara+8, nbRecords);
% Selectionne les paramtres et cre le tableau en criture
% --------------------------------------------------------
if nbRecords ~= 0
% Met en forme les dates et positions
% ------------------------------------
year = str2num(tsg.DATE(:,1:4));
month = str2num(tsg.DATE(:,5:6));
day = str2num(tsg.DATE(:,7:8));

jacques.grelet_ird.fr
committed
hour = str2num(tsg.DATE(:,10:11));
min = str2num(tsg.DATE(:,12:13));
sec = str2num(tsg.DATE(:,14:15));
Yves Gouriou
committed
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
%[year, month, day, hour, min, sec] = datevec( julianToDatenum(tsg.DAYD) );
tsg_data = [year month day hour min fix(sec) tsg.LATX mod(tsg.LONX+180,360)-180 ];
header = 'YEAR MNTH DAYX hh mi ss LATX LONX';
format = '%04d %02d %02d %02d %02d %02d %12.7f %12.7f';
% Boucle sur les paramtres sauvegarder
% ---------------------------------------
for iPara = 1 : nPara
% Slectionne le nom du paramtre
% -------------------------------
para = PARA{iPara};
if isempty(tsg.(para))
tsg.(para) = NaN * ones( size( tsg.DAYD ) );
end
if findstr( '_QC', para)
% Test pour contourner un bug de TSGQC. Les QC devaient tre casts
% en byte sinon la valeur 0 devenait gale -127
ind = 0;
ind = find( tsg.(para) == -127 );
if ~isempty(ind)
tsg.(para)(ind) = 0;
end
tsg_data = [tsg_data double(tsg.(para))];
format = [format ' %1d'];
else
tsg_data = [tsg_data tsg.(para)];
format = [format ' %6.3f'];
end
header = [header ' ' para];
end % Fin de boucle sur les paramtres sauvegarder
% Write some information
% ----------------------
if ~isempty( tsg.DATE_CREATION )
fprintf( fid, '%%DATE_CREATION %s\n', tsg.DATE_CREATION );
end
if ~isempty( tsg.CYCLE_MESURE )
fprintf( fid, '%%CYCLE_MESURE %s\n', tsg.CYCLE_MESURE);
end
if ~isempty( tsg.PLATFORM_NAME )
fprintf( fid, '%%PLATFORM_NAME %s\n', tsg.PLATFORM_NAME);
end
if ~isempty( tsg.SHIP_CALL_SIGN )
fprintf( fid, '%%SHIP_CALL_SIGN %s\n', tsg.SHIP_CALL_SIGN);
end
if ~isempty( tsg.PI_NAME )
fprintf( fid, '%%PI_NAME %s\n', tsg.PI_NAME);
end
if ~isempty( tsg.DATA_ACQUISITION )
fprintf( fid, '%%DATA_ACQUISITION %s\n', tsg.DATA_ACQUISITION);
end
% Write the file
% --------------
fprintf( fid, '%%HEADER %s\n', header);
fprintf( fid, [format '\n'], tsg_data');
% Everything OK
% -------------
error = 1;
end % Selectionne les paramtres et cre le tableau en criture
fclose( fid );
% Clear the Workspace
% -------------------
clear tsg tsg_data;
else
msg_error = 'ok';
if isempty(nc)
msg_error = ['Problem opening file : ' fileName];
end
end
end % Find Boucle sur les fichiers convertir