view m-toolbox/test/draftConvert128_4toAOs.m @ 32:e22b091498e4 database-connection-manager

Update makeToolbox
author Daniele Nicolodi <nicolodi@science.unitn.it>
date Mon, 05 Dec 2011 16:20:06 +0100
parents f0afece42f48
children
line wrap: on
line source







function out = draftConvert128_4toAOs(filename)
  
  if nargin == 0
    filename = '/data/home/indiep/DataAnalysis/a109721/DMUDMU_A/LSM10031_tmpk/LSM10031_tmpk_200910052051Z_F200909301020Z_T200909301021Z_hex.txt';
  end
  
  fid = fopen(filename);
  
  desc  = fgetl(fid);
  desc  = textscan(desc, '%s', 'Delimiter', '\t', 'CollectOutput', true);
  desc  = desc{1};
  names = fgetl(fid);
  names = textscan(names, '%s', 'Delimiter', '\t', 'CollectOutput', true);
  names = names{1};
  
  ncols = numCols(fid);
  
  C = textscan(fid, '%s', 'Delimiter', '\t', 'CollectOutput', true);
  fclose(fid);
  
  C =C{1};
  packetID = hex2dec(C{4});
  
  if packetID ~= 9001
    error('### At the moment it is only possible to convert 128,4 with packet 9001 into AOs. But the packet is [%d]', packetID);
  end
  
  timeFormat = java.text.SimpleDateFormat('dd-MM-yyyy HH:mm:ss.SSS');
  timeFormat.setTimeZone(java.util.TimeZone.getTimeZone('UTC'));
  timeFormat.parse('01-01-2000 00:00:00.000');
  toff = timeFormat.getCalendar.getTimeInMillis/1000;
  
  t0 = time();
  t0.setTimezone('UTC');
  t0.setEpochtime(round((hex2x_time(C{3}) + toff)*1000));
  
  N = 43;
  HORIZ_DWS_F = createDataAO(N, @hex2single, 'rad');
  
  N = 44;
  VERT_DWS_F = createDataAO(N, @hex2single, 'rad');
  
  N = 45;
  HORIZ_DWS_R = createDataAO(N, @hex2single, 'rad');
  
  N = 46;
  VERT_DWS_R = createDataAO(N, @hex2single, 'rad');
  
  N = 47;
  HORIZ_DC_R = createDataAO(N, @hex2single, 'rad');
  
  N = 48;
  VERT_DC_R = createDataAO(N, @hex2single, 'rad');
  
  N = 49;
  CTAST_1 = createDataAO(N, @hex2single, '');
  
  N = 50;
  CTAST_12 = createDataAO(N, @hex2single, '');
  
  N = 51;
  CTAST_F = createDataAO(N, @hex2single, '');
  
  N = 52;
  CTAST_R = createDataAO(N, @hex2single, '');
  
  N = 53;
  HORIZ_DC_F = createDataAO(N, @hex2single, 'rad');
  
  N = 54;
  VERT_DC_F = createDataAO(N, @hex2single, 'rad');
  
  N = 17;
  DMU_INTERIM_13 = createDataAO(N, @hex2single, '');
  
  N = 18;
  DMU_INTERIM_14 = createDataAO(N, @hex2single, '');
  
  N = 19;
  DMU_INTERIM_15 = createDataAO(N, @hex2single, '');
  
  N = 20;
  DMU_INTERIM_16 = createDataAO(N, @hex2single, '');
  
  N = 21;
  DMU_INTERIM_17 = createDataAO(N, @hex2single, '');
  
  N = 22;
  DMU_INTERIM_18 = createDataAO(N, @hex2single, '');
  
  N = 23;
  DMU_INTERIM_19 = createDataAO(N, @hex2single, '');
  
  N = 24;
  DMU_INTERIM_20 = createDataAO(N, @hex2single, '');
  
  N = 25;
  DMU_INTERIM_21 = createDataAO(N, @hex2single, '');
  
  N = 26;
  DMU_INTERIM_22 = createDataAO(N, @hex2single, '');
  
  N = 27;
  DMU_INTERIM_23 = createDataAO(N, @hex2single, '');
  
  N = 28;
  DMU_INTERIM_24 = createDataAO(N, @hex2single, '');
  
  N = 29;
  DMU_INTERIM_25 = createDataAO(N, @hex2single, '');
  
  N = 30;
  DMU_INTERIM_26 = createDataAO(N, @hex2single, '');
  
  N = 31;
  DMU_INTERIM_27 = createDataAO(N, @hex2single, '');
  
  N = 32;
  DMU_INTERIM_28 = createDataAO(N, @hex2single, '');
  
  N = 33;
  DMU_INTERIM_29 = createDataAO(N, @hex2single, '');
  
  N = 34;
  DMU_INTERIM_30 = createDataAO(N, @hex2single, '');
  
  N = 35;
  DMU_INTERIM_31 = createDataAO(N, @hex2single, '');
  
  N = 36;
  DMU_INTERIM_32 = createDataAO(N, @hex2single, '');
  
  N = 37;
  DMU_INTERIM_33 = createDataAO(N, @hex2single, '');
  
  N = 38;
  DMU_INTERIM_34 = createDataAO(N, @hex2single, '');
  
  N = 39;
  DMU_INTERIM_35 = createDataAO(N, @hex2single, '');
  
  N = 40;
  DMU_INTERIM_36 = createDataAO(N, @hex2single, '');
  
  out = [...
    HORIZ_DWS_F, ...
    VERT_DWS_F,  ...
    HORIZ_DWS_R, ...
    VERT_DWS_R,  ...
    HORIZ_DC_R,  ...
    VERT_DC_R,   ...
    CTAST_1,     ...
    CTAST_12,    ...
    CTAST_F,     ...
    CTAST_R,     ...
    HORIZ_DC_F,  ...
    VERT_DC_F,   ...
    DMU_INTERIM_13, ...
    DMU_INTERIM_14, ...
    DMU_INTERIM_15, ...
    DMU_INTERIM_16, ...
    DMU_INTERIM_17, ...
    DMU_INTERIM_18, ...
    DMU_INTERIM_19, ...
    DMU_INTERIM_20, ...
    DMU_INTERIM_21, ...
    DMU_INTERIM_22, ...
    DMU_INTERIM_23, ...
    DMU_INTERIM_24, ...
    DMU_INTERIM_25, ...
    DMU_INTERIM_26, ...
    DMU_INTERIM_27, ...
    DMU_INTERIM_28, ...
    DMU_INTERIM_29, ...
    DMU_INTERIM_30, ...
    DMU_INTERIM_31, ...
    DMU_INTERIM_32, ...
    DMU_INTERIM_33, ...
    DMU_INTERIM_34, ...
    DMU_INTERIM_35, ...
    DMU_INTERIM_36];
  
  out = addHistoryStep(out, plist('filename', filename), '$Id: draftConvert128_4toAOs.m,v 1.1 2009/10/19 10:35:12 ingo Exp $', []);
  
  out.submit();
  
  
  function out = createDataAO(N, convert_fcn, unit)
    y = convert_fcn(char(C(N:ncols:end)));
    d = tsdata(y, 10, t0);
    out = ao(d);
    out.setName(names{N}, 'internal');
    out.setDescription(desc{N}, 'internal');
    out.setXunits('s',  'internal');
    out.setYunits(unit, 'internal');
  end
  
end


function t = date2seconds(s)
  
  t = [];
  
  timeFormat = java.text.SimpleDateFormat('dd-MM-yyyy HH:mm:ss.SSS');
  timeFormat.setTimeZone(java.util.TimeZone.getTimeZone('UTC'));
  timeFormat.parse('01-01-2000 00:00:00.000');
  
  toff = timeFormat.getCalendar.getTimeInMillis;
  
  timeFormat = java.text.SimpleDateFormat('dd MMM yyyy HH:mm:ss.SSS');
  timeFormat.setTimeZone(java.util.TimeZone.getTimeZone('UTC'));
  
  for ii = 1:size(s,1)
    timeFormat.parse(s(ii,:));
    t = [t; (timeFormat.getCalendar.getTimeInMillis - toff)/1000];
  end
end

function ncols = numCols(fid)
  position = ftell(fid);
  line = fgetl(fid);
  C = textscan(line, '%s', 'Delimiter', '\t');
  C = C{1};
  ncols = numel(C);
  fseek(fid, position, 'bof');
end

function lines = numLines(fid)
  block = [];
  position = ftell(fid);
  lines = 0;                           % number of lines in file
  nlchr = uint8(sprintf('\n'));        % newline chr as uint8
  bsize = 4 * 256 * 8192;              % block size to read
  
  while ~feof(fid)
    block = fread(fid, bsize, '*uint8');
    lines = lines + sum(block == nlchr);
  end
  if ~isempty(block)                   % in case file is empty
    lines = lines + double(block(end) ~= nlchr);
  end
  fseek(fid, position, 'bof');
end


function x = hex2x_time(s)
  
  % The first 4 bytes are the seconds from the 1 January 2000
  s1 = s(:, 1:8);
  
  % The last byte divided by 256 in milliseconds
  s2 = s(:, 9:10);
  
  x = hex2dec(s1) + hex2dec(s2)/256;
  
end

%HEX2SINGLE Convert single precision IEEE hexadecimal string to number.
%   HEX2SINGLE(S), where S is a 8 character string containing
%   a hexadecimal number, returns a double type number
%   equal to the IEEE single precision
%   floating point number it represents.  Fewer than 8
%   characters are padded on the right with zeros.
%
%   If S is a character array, each row is interpreted as a single
%   precision number (and returned as a double).
%
%   NaNs, infinities and denorms are handled correctly.
%
%   Example:
%       hexsingle2num('40490fdb') returns Pi.
%       hexsingle2num('bf8') returns -1.
%
%   See also HEX2NUM.

% Based on Matlab's hex2num.
% Note: IEEE Standard 754 for floating point numbers
%
%  Floating point numbers are represented as:
%  x = +/- (1+f)*2^e
%
%  doubles: 64 bits
%           Bit 63       (1 bit)  = sign (0=positive, 1=negative)
%           Bit 62 to 52 (11 bits)= exponent biased by 1023
%           Bit 51 to 0  (52 bits)= fraction f of the number 1.f
%  singles: 32 bits
%           Bit 31       (1 bit)  = sign (0=positive, 1=negative)
%           Bit 30 to 23 (8 bits) = exponent biased by 127
%           Bit 22 to 0  (23 bits)= fraction f of the number 1.f
%
% Original file hexsingle2num from Mark Lubinski
% Changed on 19-may-05 by Matthias Noell: denormalized power set 2^-126
function x = hex2single(s)
  if iscellstr(s), s = char(s); end
  if ~ischar(s)
    error('Input to hexsingle2num must be a string.')
  end
  if isempty(s), x = []; return, end
  
  [row,col] = size(s);
  blanks = find(s==' '); % Find the blanks at the end
  if ~isempty(blanks), s(blanks) = '0'; end % Zero pad the shorter hex numbers.
  
  % Convert characters to numeric digits.
  % More than 8 characters are ignored
  % For double: d = zeros(row,16);
  d = zeros(row,8);
  d(:,1:col) = abs(lower(s)) - '0';
  d = d + ('0'+10-'a').*(d>9);
  neg = d(:,1) > 7;
  d(:,1) = d(:,1)-8*neg;
  
  if any(d > 15) | any(d < 0)
    error('Input string to hexsingle2num should have just 0-9, a-f, or A-F.')
  end
  
  % Floating point exponent.
  % For double: e = 16*(16*(d(:,1)-4) + d(:,2)) + d(:,3) + 1;
  % For double: e = 256*d(:,1) + 16*d(:,2) + d(:,3) - 1023;
  expBit = (d(:,3) > 7);
  e = 32*d(:,1) + 2*d(:,2) + expBit - 127;
  d(:,3) = d(:,3)-8*expBit;  % Remove most sig. bit of d(:,3) which belongs to exponent
  
  % Floating point fraction.
  % For double: sixteens = [16;256;4096;65536;1048576;16777216;268435456];
  % For double: sixteens2 = 268435456*sixteens(1:6);
  % For double: multiplier = 1./[sixteens;sixteens2];
  % For double: f = d(:,4:16)*multiplier;
  sixteens = [16;256;4096;65536;1048576;16777216];
  multiplier = 2./[sixteens];
  f = d(:,3:8)*multiplier;
  
  x = zeros(row,1);
  % Scale the fraction by 2 to the exponent.
  % For double: overinf = find((e>1023) & (f==0));
  overinf = find((e>127) & (f==0));
  if ~isempty(overinf), x(overinf) = inf; end
  
  % For double: overNaN = find((e>1023) & (f~=0));
  overNaN = find((e>127) & (f~=0));
  if ~isempty(overNaN), x(overNaN) = NaN; end
  
  % For double: underflow = find(e<-1022);
  underflow = find(e<-126);
  if ~isempty(underflow), x(underflow) = pow2(f(underflow),-126); end
  
  % For double: allothers = find((e<=1023) & (e>=-1022));
  allothers = find((e<=127) & (e>=-126));
  if ~isempty(allothers), x(allothers) = pow2(1+f(allothers),e(allothers)); end
  
  negatives = find(neg);
  if ~isempty(negatives), x(negatives) = -x(negatives); end
  
end