comparison m-toolbox/test/draftConvert128_4toAOs.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_4toAOs(filename)
8
9 if nargin == 0
10 filename = '/data/home/indiep/DataAnalysis/a109721/DMUDMU_A/LSM10031_tmpk/LSM10031_tmpk_200910052051Z_F200909301020Z_T200909301021Z_hex.txt';
11 end
12
13 fid = fopen(filename);
14
15 desc = fgetl(fid);
16 desc = textscan(desc, '%s', 'Delimiter', '\t', 'CollectOutput', true);
17 desc = desc{1};
18 names = fgetl(fid);
19 names = textscan(names, '%s', 'Delimiter', '\t', 'CollectOutput', true);
20 names = names{1};
21
22 ncols = numCols(fid);
23
24 C = textscan(fid, '%s', 'Delimiter', '\t', 'CollectOutput', true);
25 fclose(fid);
26
27 C =C{1};
28 packetID = hex2dec(C{4});
29
30 if packetID ~= 9001
31 error('### At the moment it is only possible to convert 128,4 with packet 9001 into AOs. But the packet is [%d]', packetID);
32 end
33
34 timeFormat = java.text.SimpleDateFormat('dd-MM-yyyy HH:mm:ss.SSS');
35 timeFormat.setTimeZone(java.util.TimeZone.getTimeZone('UTC'));
36 timeFormat.parse('01-01-2000 00:00:00.000');
37 toff = timeFormat.getCalendar.getTimeInMillis/1000;
38
39 t0 = time();
40 t0.setTimezone('UTC');
41 t0.setEpochtime(round((hex2x_time(C{3}) + toff)*1000));
42
43 N = 43;
44 HORIZ_DWS_F = createDataAO(N, @hex2single, 'rad');
45
46 N = 44;
47 VERT_DWS_F = createDataAO(N, @hex2single, 'rad');
48
49 N = 45;
50 HORIZ_DWS_R = createDataAO(N, @hex2single, 'rad');
51
52 N = 46;
53 VERT_DWS_R = createDataAO(N, @hex2single, 'rad');
54
55 N = 47;
56 HORIZ_DC_R = createDataAO(N, @hex2single, 'rad');
57
58 N = 48;
59 VERT_DC_R = createDataAO(N, @hex2single, 'rad');
60
61 N = 49;
62 CTAST_1 = createDataAO(N, @hex2single, '');
63
64 N = 50;
65 CTAST_12 = createDataAO(N, @hex2single, '');
66
67 N = 51;
68 CTAST_F = createDataAO(N, @hex2single, '');
69
70 N = 52;
71 CTAST_R = createDataAO(N, @hex2single, '');
72
73 N = 53;
74 HORIZ_DC_F = createDataAO(N, @hex2single, 'rad');
75
76 N = 54;
77 VERT_DC_F = createDataAO(N, @hex2single, 'rad');
78
79 N = 17;
80 DMU_INTERIM_13 = createDataAO(N, @hex2single, '');
81
82 N = 18;
83 DMU_INTERIM_14 = createDataAO(N, @hex2single, '');
84
85 N = 19;
86 DMU_INTERIM_15 = createDataAO(N, @hex2single, '');
87
88 N = 20;
89 DMU_INTERIM_16 = createDataAO(N, @hex2single, '');
90
91 N = 21;
92 DMU_INTERIM_17 = createDataAO(N, @hex2single, '');
93
94 N = 22;
95 DMU_INTERIM_18 = createDataAO(N, @hex2single, '');
96
97 N = 23;
98 DMU_INTERIM_19 = createDataAO(N, @hex2single, '');
99
100 N = 24;
101 DMU_INTERIM_20 = createDataAO(N, @hex2single, '');
102
103 N = 25;
104 DMU_INTERIM_21 = createDataAO(N, @hex2single, '');
105
106 N = 26;
107 DMU_INTERIM_22 = createDataAO(N, @hex2single, '');
108
109 N = 27;
110 DMU_INTERIM_23 = createDataAO(N, @hex2single, '');
111
112 N = 28;
113 DMU_INTERIM_24 = createDataAO(N, @hex2single, '');
114
115 N = 29;
116 DMU_INTERIM_25 = createDataAO(N, @hex2single, '');
117
118 N = 30;
119 DMU_INTERIM_26 = createDataAO(N, @hex2single, '');
120
121 N = 31;
122 DMU_INTERIM_27 = createDataAO(N, @hex2single, '');
123
124 N = 32;
125 DMU_INTERIM_28 = createDataAO(N, @hex2single, '');
126
127 N = 33;
128 DMU_INTERIM_29 = createDataAO(N, @hex2single, '');
129
130 N = 34;
131 DMU_INTERIM_30 = createDataAO(N, @hex2single, '');
132
133 N = 35;
134 DMU_INTERIM_31 = createDataAO(N, @hex2single, '');
135
136 N = 36;
137 DMU_INTERIM_32 = createDataAO(N, @hex2single, '');
138
139 N = 37;
140 DMU_INTERIM_33 = createDataAO(N, @hex2single, '');
141
142 N = 38;
143 DMU_INTERIM_34 = createDataAO(N, @hex2single, '');
144
145 N = 39;
146 DMU_INTERIM_35 = createDataAO(N, @hex2single, '');
147
148 N = 40;
149 DMU_INTERIM_36 = createDataAO(N, @hex2single, '');
150
151 out = [...
152 HORIZ_DWS_F, ...
153 VERT_DWS_F, ...
154 HORIZ_DWS_R, ...
155 VERT_DWS_R, ...
156 HORIZ_DC_R, ...
157 VERT_DC_R, ...
158 CTAST_1, ...
159 CTAST_12, ...
160 CTAST_F, ...
161 CTAST_R, ...
162 HORIZ_DC_F, ...
163 VERT_DC_F, ...
164 DMU_INTERIM_13, ...
165 DMU_INTERIM_14, ...
166 DMU_INTERIM_15, ...
167 DMU_INTERIM_16, ...
168 DMU_INTERIM_17, ...
169 DMU_INTERIM_18, ...
170 DMU_INTERIM_19, ...
171 DMU_INTERIM_20, ...
172 DMU_INTERIM_21, ...
173 DMU_INTERIM_22, ...
174 DMU_INTERIM_23, ...
175 DMU_INTERIM_24, ...
176 DMU_INTERIM_25, ...
177 DMU_INTERIM_26, ...
178 DMU_INTERIM_27, ...
179 DMU_INTERIM_28, ...
180 DMU_INTERIM_29, ...
181 DMU_INTERIM_30, ...
182 DMU_INTERIM_31, ...
183 DMU_INTERIM_32, ...
184 DMU_INTERIM_33, ...
185 DMU_INTERIM_34, ...
186 DMU_INTERIM_35, ...
187 DMU_INTERIM_36];
188
189 out = addHistoryStep(out, plist('filename', filename), '$Id: draftConvert128_4toAOs.m,v 1.1 2009/10/19 10:35:12 ingo Exp $', []);
190
191 out.submit();
192
193
194 function out = createDataAO(N, convert_fcn, unit)
195 y = convert_fcn(char(C(N:ncols:end)));
196 d = tsdata(y, 10, t0);
197 out = ao(d);
198 out.setName(names{N}, 'internal');
199 out.setDescription(desc{N}, 'internal');
200 out.setXunits('s', 'internal');
201 out.setYunits(unit, 'internal');
202 end
203
204 end
205
206
207 function t = date2seconds(s)
208
209 t = [];
210
211 timeFormat = java.text.SimpleDateFormat('dd-MM-yyyy HH:mm:ss.SSS');
212 timeFormat.setTimeZone(java.util.TimeZone.getTimeZone('UTC'));
213 timeFormat.parse('01-01-2000 00:00:00.000');
214
215 toff = timeFormat.getCalendar.getTimeInMillis;
216
217 timeFormat = java.text.SimpleDateFormat('dd MMM yyyy HH:mm:ss.SSS');
218 timeFormat.setTimeZone(java.util.TimeZone.getTimeZone('UTC'));
219
220 for ii = 1:size(s,1)
221 timeFormat.parse(s(ii,:));
222 t = [t; (timeFormat.getCalendar.getTimeInMillis - toff)/1000];
223 end
224 end
225
226 function ncols = numCols(fid)
227 position = ftell(fid);
228 line = fgetl(fid);
229 C = textscan(line, '%s', 'Delimiter', '\t');
230 C = C{1};
231 ncols = numel(C);
232 fseek(fid, position, 'bof');
233 end
234
235 function lines = numLines(fid)
236 block = [];
237 position = ftell(fid);
238 lines = 0; % number of lines in file
239 nlchr = uint8(sprintf('\n')); % newline chr as uint8
240 bsize = 4 * 256 * 8192; % block size to read
241
242 while ~feof(fid)
243 block = fread(fid, bsize, '*uint8');
244 lines = lines + sum(block == nlchr);
245 end
246 if ~isempty(block) % in case file is empty
247 lines = lines + double(block(end) ~= nlchr);
248 end
249 fseek(fid, position, 'bof');
250 end
251
252
253 function x = hex2x_time(s)
254
255 % The first 4 bytes are the seconds from the 1 January 2000
256 s1 = s(:, 1:8);
257
258 % The last byte divided by 256 in milliseconds
259 s2 = s(:, 9:10);
260
261 x = hex2dec(s1) + hex2dec(s2)/256;
262
263 end
264
265 %HEX2SINGLE Convert single precision IEEE hexadecimal string to number.
266 % HEX2SINGLE(S), where S is a 8 character string containing
267 % a hexadecimal number, returns a double type number
268 % equal to the IEEE single precision
269 % floating point number it represents. Fewer than 8
270 % characters are padded on the right with zeros.
271 %
272 % If S is a character array, each row is interpreted as a single
273 % precision number (and returned as a double).
274 %
275 % NaNs, infinities and denorms are handled correctly.
276 %
277 % Example:
278 % hexsingle2num('40490fdb') returns Pi.
279 % hexsingle2num('bf8') returns -1.
280 %
281 % See also HEX2NUM.
282
283 % Based on Matlab's hex2num.
284 % Note: IEEE Standard 754 for floating point numbers
285 %
286 % Floating point numbers are represented as:
287 % x = +/- (1+f)*2^e
288 %
289 % doubles: 64 bits
290 % Bit 63 (1 bit) = sign (0=positive, 1=negative)
291 % Bit 62 to 52 (11 bits)= exponent biased by 1023
292 % Bit 51 to 0 (52 bits)= fraction f of the number 1.f
293 % singles: 32 bits
294 % Bit 31 (1 bit) = sign (0=positive, 1=negative)
295 % Bit 30 to 23 (8 bits) = exponent biased by 127
296 % Bit 22 to 0 (23 bits)= fraction f of the number 1.f
297 %
298 % Original file hexsingle2num from Mark Lubinski
299 % Changed on 19-may-05 by Matthias Noell: denormalized power set 2^-126
300 function x = hex2single(s)
301 if iscellstr(s), s = char(s); end
302 if ~ischar(s)
303 error('Input to hexsingle2num must be a string.')
304 end
305 if isempty(s), x = []; return, end
306
307 [row,col] = size(s);
308 blanks = find(s==' '); % Find the blanks at the end
309 if ~isempty(blanks), s(blanks) = '0'; end % Zero pad the shorter hex numbers.
310
311 % Convert characters to numeric digits.
312 % More than 8 characters are ignored
313 % For double: d = zeros(row,16);
314 d = zeros(row,8);
315 d(:,1:col) = abs(lower(s)) - '0';
316 d = d + ('0'+10-'a').*(d>9);
317 neg = d(:,1) > 7;
318 d(:,1) = d(:,1)-8*neg;
319
320 if any(d > 15) | any(d < 0)
321 error('Input string to hexsingle2num should have just 0-9, a-f, or A-F.')
322 end
323
324 % Floating point exponent.
325 % For double: e = 16*(16*(d(:,1)-4) + d(:,2)) + d(:,3) + 1;
326 % For double: e = 256*d(:,1) + 16*d(:,2) + d(:,3) - 1023;
327 expBit = (d(:,3) > 7);
328 e = 32*d(:,1) + 2*d(:,2) + expBit - 127;
329 d(:,3) = d(:,3)-8*expBit; % Remove most sig. bit of d(:,3) which belongs to exponent
330
331 % Floating point fraction.
332 % For double: sixteens = [16;256;4096;65536;1048576;16777216;268435456];
333 % For double: sixteens2 = 268435456*sixteens(1:6);
334 % For double: multiplier = 1./[sixteens;sixteens2];
335 % For double: f = d(:,4:16)*multiplier;
336 sixteens = [16;256;4096;65536;1048576;16777216];
337 multiplier = 2./[sixteens];
338 f = d(:,3:8)*multiplier;
339
340 x = zeros(row,1);
341 % Scale the fraction by 2 to the exponent.
342 % For double: overinf = find((e>1023) & (f==0));
343 overinf = find((e>127) & (f==0));
344 if ~isempty(overinf), x(overinf) = inf; end
345
346 % For double: overNaN = find((e>1023) & (f~=0));
347 overNaN = find((e>127) & (f~=0));
348 if ~isempty(overNaN), x(overNaN) = NaN; end
349
350 % For double: underflow = find(e<-1022);
351 underflow = find(e<-126);
352 if ~isempty(underflow), x(underflow) = pow2(f(underflow),-126); end
353
354 % For double: allothers = find((e<=1023) & (e>=-1022));
355 allothers = find((e<=127) & (e>=-126));
356 if ~isempty(allothers), x(allothers) = pow2(1+f(allothers),e(allothers)); end
357
358 negatives = find(neg);
359 if ~isempty(negatives), x(negatives) = -x(negatives); end
360
361 end