comparison m-toolbox/test/draftConvert128_3toAOs.m @ 0:f0afece42f48

Import.
author Daniele Nicolodi <nicolodi@science.unitn.it>
date Wed, 23 Nov 2011 19:22:13 +0100
parents
children
comparison
equal deleted inserted replaced
-1:000000000000 0:f0afece42f48
1
2
3
4
5
6
7 function out = draftConvert128_3toAOs(filename)
8
9 if nargin == 0
10 filename = '/data/home/indiep/DataAnalysis/a109721/DMUDMU_A/LSM10030_tmpk/LSM10030_tmpk_200910052051Z_F200909301020Z_T200909301021Z_hex.txt';
11 end
12
13 fid = fopen(filename);
14
15 format128_3 = '%s %s %s %s %s %s %s %s %s %s %s %s %s %s %s %s %s %s %s %s %s %s %s %s %s %s %s %s %s %s %s %s %s %s %s %s %s %s %s %s %s %s %s %s %*[^\n]';
16
17 C = textscan(fid, format128_3, 'Delimiter', '\t');
18
19 fclose(fid);
20
21 timeFormat = java.text.SimpleDateFormat('dd-MM-yyyy HH:mm:ss.SSS');
22 timeFormat.setTimeZone(java.util.TimeZone.getTimeZone('UTC'));
23 timeFormat.parse('01-01-2000 00:00:00.000');
24
25 toff = timeFormat.getCalendar.getTimeInMillis;
26
27 timeFormat = java.text.SimpleDateFormat('dd MMM yyyy HH:mm:ss.SSS');
28 timeFormat.setTimeZone(java.util.TimeZone.getTimeZone('UTC'));
29
30 %%%%%%%%%%%%%%%%%%%%%%%%%% Convert time string %%%%%%%%%%%%%%%%%%%%%%%%%%
31
32 % Column 2
33 N = 2;
34 tUDMS = [];
35 for ii = 3:numel(C{N})
36 timeFormat.parse(C{N}{ii});
37 tUDMS = [tUDMS; (timeFormat.getCalendar.getTimeInMillis - toff)/1000];
38 end
39
40 % Column 3
41 N = 3;
42 tDMU = hex2x_time(char(C{N}{3:end}));
43
44 DMU_UDMS_TIME = ao(xydata(tDMU, tUDMS));
45 DMU_UDMS_TIME.setName('DMU_UDMS_TIME', 'internal');
46
47 t0 = time();
48 t0.setTimezone('UTC');
49 t0.setEpochtime(round(toff + tDMU(1)*1000));
50
51 %%%%%%%%%%%%%%%%%%%%%%%%%%%%% Convert flags %%%%%%%%%%%%%%%%%%%%%%%%%%%%%
52
53 % Column 5
54 N = 5;
55 DMU_X1_C1_GT_5 = createFlagAO(N, @hex2dec);
56
57 % Column 6
58 N = 6;
59 DMU_X1_C1_GT_42 = createFlagAO(N, @hex2dec);
60
61 % Column 7
62 N = 7;
63 DMU_X1_C1_LT_38 = createFlagAO(N, @hex2dec);
64
65 % Column 8
66 N = 8;
67 DMU_X1_C1_GT_70 = createFlagAO(N, @hex2dec);
68
69 % Column 9
70 N = 9;
71 DMU_X1_ERR1 = createFlagAO(N, @hex2dec);
72
73 % Column 10
74 N = 10;
75 DMU_X1_ERR2 = createFlagAO(N, @hex2dec);
76
77 % Column 11
78 N = 11;
79 DMU_X1_ERR3 = createFlagAO(N, @hex2dec);
80
81 % Column 12
82 N = 12;
83 DMU_X1_ETA_PHI = createFlagAO(N, @hex2dec);
84
85 % Column 13
86 N = 13;
87 DMU_X1_VALIDITY = createFlagAO(N, @hex2dec);
88
89 % Column 14
90 N = 14;
91 DMU_X1_ETA_PHI_VALIDITY = createFlagAO(N, @hex2dec);
92
93 % Column 15
94 N = 15;
95 DMU_X1_C1_GT_10 = createFlagAO(N, @hex2dec);
96
97 % Column 16
98 N = 16;
99 DMU_X1_C1_LT_6 = createFlagAO(N, @hex2dec);
100
101 % Column 19
102 N = 19;
103 DMU_X12_C12_GT_5 = createFlagAO(N, @hex2dec);
104
105 % Column 20
106 N = 20;
107 DMU_X12_C12_GT_42 = createFlagAO(N, @hex2dec);
108
109 % Column 21
110 N = 21;
111 DMU_X12_C12_LT_38 = createFlagAO(N, @hex2dec);
112
113 % Column 22
114 N = 22;
115 DMU_X12_C12_GT_70 = createFlagAO(N, @hex2dec);
116
117 % Column 23
118 N = 23;
119 DMU_X12_ERR4 = createFlagAO(N, @hex2dec);
120
121 % Column 24
122 N = 24;
123 DMU_X12_ERR5 = createFlagAO(N, @hex2dec);
124
125 % Column 25
126 N = 25;
127 DMU_X12_ERR6 = createFlagAO(N, @hex2dec);
128
129 % Column 26
130 N = 26;
131 DMU_X12_ETA_PHI = createFlagAO(N, @hex2dec);
132
133 % Column 27
134 N = 27;
135 DMU_X12_VALIDITY = createFlagAO(N, @hex2dec);
136
137 % Column 28
138 N = 28;
139 DMU_X12_ETA_PHI_VALIDITY = createFlagAO(N, @hex2dec);
140
141 % Column 29
142 N = 29;
143 DMU_X12_C1_GT_10 = createFlagAO(N, @hex2dec);
144
145 % Column 30
146 N = 30;
147 DMU_X12_C1_LT_6 = createFlagAO(N, @hex2dec);
148
149 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%% Convert data %%%%%%%%%%%%%%%%%%%%%%%%%%%%%
150
151 % Column 17
152 N = 17;
153 DMU_X1_FILT = createDataAO(N, @hex2num, 'm');
154
155 % Column 31
156 N = 31;
157 DMU_X12_FILT = createDataAO(N, @hex2num, 'm');
158
159 % Column 32
160 N = 32;
161 DMU_DC_PHI_1_FILT = createDataAO(N, @hex2single, 'rad');
162
163 % Column 33
164 N = 33;
165 DMU_DC_ETA_1_FILT = createDataAO(N, @hex2single, 'rad');
166
167 % Column 34
168 N = 34;
169 DMU_DC_PHI_2_FILT = createDataAO(N, @hex2single, 'rad');
170
171 % Column 35
172 N = 35;
173 DMU_DC_ETA_2_FILT = createDataAO(N, @hex2single, 'rad');
174
175 % Column 36
176 N = 36;
177 DMU_DWS_PHI_1_FILT = createDataAO(N, @hex2single, 'rad');
178
179 % Column 37
180 N = 37;
181 DMU_DWS_ETA_1_FILT = createDataAO(N, @hex2single, 'rad');
182
183 % Column 38
184 N = 38;
185 DMU_DWS_PHI_2_FILT = createDataAO(N, @hex2single, 'rad');
186
187 % Column 39
188 N = 39;
189 DMU_DWS_ETA_2_FILT = createDataAO(N, @hex2single, 'rad');
190
191 % Column 40
192 N = 40;
193 DMU_PSI_F_FILT = createDataAO(N, @hex2num, 'rad');
194
195 % Column 41
196 N = 41;
197 DMU_PSI_R_FILT = createDataAO(N, @hex2num, 'rad');
198
199 % Column 42
200 N = 42;
201 DMU_SIGMA_F_FILT = createDataAO(N, @hex2single, '');
202
203 % Column 43
204 N = 43;
205 DMU_SIGMA_R_FILT = createDataAO(N, @hex2single, '');
206
207 out = [...
208 DMU_X1_FILT, ...
209 DMU_X12_FILT, ...
210 DMU_DC_PHI_1_FILT, ...
211 DMU_DC_ETA_1_FILT, ...
212 DMU_DC_PHI_2_FILT, ...
213 DMU_DC_ETA_2_FILT, ...
214 DMU_DWS_PHI_1_FILT, ...
215 DMU_DWS_ETA_1_FILT, ...
216 DMU_DWS_PHI_2_FILT, ...
217 DMU_DWS_ETA_2_FILT, ...
218 DMU_PSI_F_FILT, ...
219 DMU_PSI_R_FILT, ...
220 DMU_SIGMA_F_FILT, ...
221 DMU_SIGMA_R_FILT, ...
222 DMU_X1_C1_GT_5, ...
223 DMU_X1_C1_GT_42, ...
224 DMU_X1_C1_LT_38, ...
225 DMU_X1_C1_GT_70, ...
226 DMU_X1_ERR1, ...
227 DMU_X1_ERR2, ...
228 DMU_X1_ERR3, ...
229 DMU_X1_ETA_PHI, ...
230 DMU_X1_VALIDITY, ...
231 DMU_X1_ETA_PHI_VALIDITY, ...
232 DMU_X1_C1_GT_10, ...
233 DMU_X1_C1_LT_6, ...
234 DMU_X12_C12_GT_5, ...
235 DMU_X12_C12_GT_42, ...
236 DMU_X12_C12_LT_38, ...
237 DMU_X12_C12_GT_70, ...
238 DMU_X12_ERR4, ...
239 DMU_X12_ERR5, ...
240 DMU_X12_ERR6, ...
241 DMU_X12_ETA_PHI, ...
242 DMU_X12_VALIDITY, ...
243 DMU_X12_ETA_PHI_VALIDITY, ...
244 DMU_X12_C1_GT_10, ...
245 DMU_X12_C1_LT_6, ...
246 DMU_UDMS_TIME];
247
248 out = addHistoryStep(out, plist('filename', filename), '$Id: draftConvert128_3toAOs.m,v 1.3 2009/10/19 10:35:12 ingo Exp $', []);
249
250 out.submit();
251
252 function out = createFlagAO(N, convert_fcn)
253 d = cdata(convert_fcn(char(C{N}{3:end})));
254 out = ao(d);
255 out.setName(C{N}{2}, 'internal');
256 out.setDescription(C{N}{1}, 'internal');
257 end
258
259 function out = createDataAO(N, convert_fcn, unit)
260 d = tsdata(convert_fcn(char(C{N}{3:end})), 10, t0);
261 out = ao(d);
262 out.setName(C{N}{2}, 'internal');
263 out.setDescription(C{N}{1}, 'internal');
264 out.setXunits('s', 'internal');
265 out.setYunits(unit, 'internal');
266 end
267
268 end
269
270
271 function x = hex2x_time(s)
272
273 % The first 4 bytes are the seconds from the 1 January 2000
274 s1 = s(:, 1:8);
275
276 % The last byte divided by 256 in milliseconds
277 s2 = s(:, 9:10);
278
279 x = hex2dec(s1) + hex2dec(s2)/256;
280
281 end
282
283 %HEX2SINGLE Convert single precision IEEE hexadecimal string to number.
284 % HEX2SINGLE(S), where S is a 8 character string containing
285 % a hexadecimal number, returns a double type number
286 % equal to the IEEE single precision
287 % floating point number it represents. Fewer than 8
288 % characters are padded on the right with zeros.
289 %
290 % If S is a character array, each row is interpreted as a single
291 % precision number (and returned as a double).
292 %
293 % NaNs, infinities and denorms are handled correctly.
294 %
295 % Example:
296 % hexsingle2num('40490fdb') returns Pi.
297 % hexsingle2num('bf8') returns -1.
298 %
299 % See also HEX2NUM.
300
301 % Based on Matlab's hex2num.
302 % Note: IEEE Standard 754 for floating point numbers
303 %
304 % Floating point numbers are represented as:
305 % x = +/- (1+f)*2^e
306 %
307 % doubles: 64 bits
308 % Bit 63 (1 bit) = sign (0=positive, 1=negative)
309 % Bit 62 to 52 (11 bits)= exponent biased by 1023
310 % Bit 51 to 0 (52 bits)= fraction f of the number 1.f
311 % singles: 32 bits
312 % Bit 31 (1 bit) = sign (0=positive, 1=negative)
313 % Bit 30 to 23 (8 bits) = exponent biased by 127
314 % Bit 22 to 0 (23 bits)= fraction f of the number 1.f
315 %
316 % Original file hexsingle2num from Mark Lubinski
317 % Changed on 19-may-05 by Matthias Noell: denormalized power set 2^-126
318 function x = hex2single(s)
319 if iscellstr(s), s = char(s); end
320 if ~ischar(s)
321 error('Input to hexsingle2num must be a string.')
322 end
323 if isempty(s), x = []; return, end
324
325 [row,col] = size(s);
326 blanks = find(s==' '); % Find the blanks at the end
327 if ~isempty(blanks), s(blanks) = '0'; end % Zero pad the shorter hex numbers.
328
329 % Convert characters to numeric digits.
330 % More than 8 characters are ignored
331 % For double: d = zeros(row,16);
332 d = zeros(row,8);
333 d(:,1:col) = abs(lower(s)) - '0';
334 d = d + ('0'+10-'a').*(d>9);
335 neg = d(:,1) > 7;
336 d(:,1) = d(:,1)-8*neg;
337
338 if any(d > 15) | any(d < 0)
339 error('Input string to hexsingle2num should have just 0-9, a-f, or A-F.')
340 end
341
342 % Floating point exponent.
343 % For double: e = 16*(16*(d(:,1)-4) + d(:,2)) + d(:,3) + 1;
344 % For double: e = 256*d(:,1) + 16*d(:,2) + d(:,3) - 1023;
345 expBit = (d(:,3) > 7);
346 e = 32*d(:,1) + 2*d(:,2) + expBit - 127;
347 d(:,3) = d(:,3)-8*expBit; % Remove most sig. bit of d(:,3) which belongs to exponent
348
349 % Floating point fraction.
350 % For double: sixteens = [16;256;4096;65536;1048576;16777216;268435456];
351 % For double: sixteens2 = 268435456*sixteens(1:6);
352 % For double: multiplier = 1./[sixteens;sixteens2];
353 % For double: f = d(:,4:16)*multiplier;
354 sixteens = [16;256;4096;65536;1048576;16777216];
355 multiplier = 2./[sixteens];
356 f = d(:,3:8)*multiplier;
357
358 x = zeros(row,1);
359 % Scale the fraction by 2 to the exponent.
360 % For double: overinf = find((e>1023) & (f==0));
361 overinf = find((e>127) & (f==0));
362 if ~isempty(overinf), x(overinf) = inf; end
363
364 % For double: overNaN = find((e>1023) & (f~=0));
365 overNaN = find((e>127) & (f~=0));
366 if ~isempty(overNaN), x(overNaN) = NaN; end
367
368 % For double: underflow = find(e<-1022);
369 underflow = find(e<-126);
370 if ~isempty(underflow), x(underflow) = pow2(f(underflow),-126); end
371
372 % For double: allothers = find((e<=1023) & (e>=-1022));
373 allothers = find((e<=127) & (e>=-126));
374 if ~isempty(allothers), x(allothers) = pow2(1+f(allothers),e(allothers)); end
375
376 negatives = find(neg);
377 if ~isempty(negatives), x(negatives) = -x(negatives); end
378
379 end