view m-toolbox/test/draftConvert128_3toAOs.m @ 15:ce3fbb7ebe71 database-connection-manager

Remove broken functions from utils.jmysql
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_3toAOs(filename)
  
  if nargin == 0
    filename = '/data/home/indiep/DataAnalysis/a109721/DMUDMU_A/LSM10030_tmpk/LSM10030_tmpk_200910052051Z_F200909301020Z_T200909301021Z_hex.txt';
  end
  
  fid = fopen(filename);
  
  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]';
  
  C = textscan(fid, format128_3, 'Delimiter', '\t');
  
  fclose(fid);
  
  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'));
  
  %%%%%%%%%%%%%%%%%%%%%%%%%%   Convert time string   %%%%%%%%%%%%%%%%%%%%%%%%%%
  
  % Column 2
  N     = 2;
  tUDMS = [];
  for ii = 3:numel(C{N})
    timeFormat.parse(C{N}{ii});
    tUDMS = [tUDMS; (timeFormat.getCalendar.getTimeInMillis - toff)/1000];
  end
  
  % Column 3
  N = 3;
  tDMU = hex2x_time(char(C{N}{3:end}));
  
  DMU_UDMS_TIME = ao(xydata(tDMU, tUDMS));
  DMU_UDMS_TIME.setName('DMU_UDMS_TIME', 'internal');
  
  t0 = time();
  t0.setTimezone('UTC');
  t0.setEpochtime(round(toff + tDMU(1)*1000));
  
  %%%%%%%%%%%%%%%%%%%%%%%%%%%%%   Convert flags   %%%%%%%%%%%%%%%%%%%%%%%%%%%%%
  
  % Column 5
  N = 5;
  DMU_X1_C1_GT_5 = createFlagAO(N, @hex2dec);
  
  % Column 6
  N = 6;
  DMU_X1_C1_GT_42 = createFlagAO(N, @hex2dec);
  
  % Column 7
  N = 7;
  DMU_X1_C1_LT_38 = createFlagAO(N, @hex2dec);
  
  % Column 8
  N = 8;
  DMU_X1_C1_GT_70 = createFlagAO(N, @hex2dec);
  
  % Column 9
  N = 9;
  DMU_X1_ERR1 = createFlagAO(N, @hex2dec);
  
  % Column 10
  N = 10;
  DMU_X1_ERR2 = createFlagAO(N, @hex2dec);
  
  % Column 11
  N = 11;
  DMU_X1_ERR3 = createFlagAO(N, @hex2dec);
  
  % Column 12
  N = 12;
  DMU_X1_ETA_PHI = createFlagAO(N, @hex2dec);
  
  % Column 13
  N = 13;
  DMU_X1_VALIDITY = createFlagAO(N, @hex2dec);
  
  % Column 14
  N = 14;
  DMU_X1_ETA_PHI_VALIDITY = createFlagAO(N, @hex2dec);
  
  % Column 15
  N = 15;
  DMU_X1_C1_GT_10 = createFlagAO(N, @hex2dec);
  
  % Column 16
  N = 16;
  DMU_X1_C1_LT_6 = createFlagAO(N, @hex2dec);
  
  % Column 19
  N = 19;
  DMU_X12_C12_GT_5 = createFlagAO(N, @hex2dec);
  
  % Column 20
  N = 20;
  DMU_X12_C12_GT_42 = createFlagAO(N, @hex2dec);
  
  % Column 21
  N = 21;
  DMU_X12_C12_LT_38 = createFlagAO(N, @hex2dec);
  
  % Column 22
  N = 22;
  DMU_X12_C12_GT_70 = createFlagAO(N, @hex2dec);
  
  % Column 23
  N = 23;
  DMU_X12_ERR4 = createFlagAO(N, @hex2dec);
  
  % Column 24
  N = 24;
  DMU_X12_ERR5 = createFlagAO(N, @hex2dec);
  
  % Column 25
  N = 25;
  DMU_X12_ERR6 = createFlagAO(N, @hex2dec);
  
  % Column 26
  N = 26;
  DMU_X12_ETA_PHI = createFlagAO(N, @hex2dec);
  
  % Column 27
  N = 27;
  DMU_X12_VALIDITY = createFlagAO(N, @hex2dec);
  
  % Column 28
  N = 28;
  DMU_X12_ETA_PHI_VALIDITY = createFlagAO(N, @hex2dec);
  
  % Column 29
  N = 29;
  DMU_X12_C1_GT_10 = createFlagAO(N, @hex2dec);
  
  % Column 30
  N = 30;
  DMU_X12_C1_LT_6 = createFlagAO(N, @hex2dec);
  
  %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%   Convert data   %%%%%%%%%%%%%%%%%%%%%%%%%%%%%
  
  % Column 17
  N = 17;
  DMU_X1_FILT = createDataAO(N, @hex2num, 'm');
  
  % Column 31
  N = 31;
  DMU_X12_FILT = createDataAO(N, @hex2num, 'm');
  
  % Column 32
  N = 32;
  DMU_DC_PHI_1_FILT = createDataAO(N, @hex2single, 'rad');
  
  % Column 33
  N = 33;
  DMU_DC_ETA_1_FILT = createDataAO(N, @hex2single, 'rad');
  
  % Column 34
  N = 34;
  DMU_DC_PHI_2_FILT = createDataAO(N, @hex2single, 'rad');
  
  % Column 35
  N = 35;
  DMU_DC_ETA_2_FILT = createDataAO(N, @hex2single, 'rad');
  
  % Column 36
  N = 36;
  DMU_DWS_PHI_1_FILT = createDataAO(N, @hex2single, 'rad');
  
  % Column 37
  N = 37;
  DMU_DWS_ETA_1_FILT = createDataAO(N, @hex2single, 'rad');
  
  % Column 38
  N = 38;
  DMU_DWS_PHI_2_FILT = createDataAO(N, @hex2single, 'rad');
  
  % Column 39
  N = 39;
  DMU_DWS_ETA_2_FILT = createDataAO(N, @hex2single, 'rad');
  
  % Column 40
  N = 40;
  DMU_PSI_F_FILT = createDataAO(N, @hex2num, 'rad');
  
  % Column 41
  N = 41;
  DMU_PSI_R_FILT = createDataAO(N, @hex2num, 'rad');
  
  % Column 42
  N = 42;
  DMU_SIGMA_F_FILT = createDataAO(N, @hex2single, '');
  
  % Column 43
  N = 43;
  DMU_SIGMA_R_FILT = createDataAO(N, @hex2single, '');
  
  out = [...
    DMU_X1_FILT, ...
    DMU_X12_FILT, ...
    DMU_DC_PHI_1_FILT, ...
    DMU_DC_ETA_1_FILT, ...
    DMU_DC_PHI_2_FILT, ...
    DMU_DC_ETA_2_FILT, ...
    DMU_DWS_PHI_1_FILT, ...
    DMU_DWS_ETA_1_FILT, ...
    DMU_DWS_PHI_2_FILT, ...
    DMU_DWS_ETA_2_FILT, ...
    DMU_PSI_F_FILT, ...
    DMU_PSI_R_FILT, ...
    DMU_SIGMA_F_FILT, ...
    DMU_SIGMA_R_FILT, ...
    DMU_X1_C1_GT_5, ...
    DMU_X1_C1_GT_42, ...
    DMU_X1_C1_LT_38, ...
    DMU_X1_C1_GT_70, ...
    DMU_X1_ERR1, ...
    DMU_X1_ERR2, ...
    DMU_X1_ERR3, ...
    DMU_X1_ETA_PHI, ...
    DMU_X1_VALIDITY, ...
    DMU_X1_ETA_PHI_VALIDITY, ...
    DMU_X1_C1_GT_10, ...
    DMU_X1_C1_LT_6, ...
    DMU_X12_C12_GT_5, ...
    DMU_X12_C12_GT_42, ...
    DMU_X12_C12_LT_38, ...
    DMU_X12_C12_GT_70, ...
    DMU_X12_ERR4, ...
    DMU_X12_ERR5, ...
    DMU_X12_ERR6, ...
    DMU_X12_ETA_PHI, ...
    DMU_X12_VALIDITY, ...
    DMU_X12_ETA_PHI_VALIDITY, ...
    DMU_X12_C1_GT_10, ...
    DMU_X12_C1_LT_6, ...
    DMU_UDMS_TIME];
  
  out = addHistoryStep(out, plist('filename', filename), '$Id: draftConvert128_3toAOs.m,v 1.3 2009/10/19 10:35:12 ingo Exp $', []);
  
  out.submit();
  
  function out = createFlagAO(N, convert_fcn)
    d = cdata(convert_fcn(char(C{N}{3:end})));
    out = ao(d);
    out.setName(C{N}{2}, 'internal');
    out.setDescription(C{N}{1}, 'internal');
  end
  
  function out = createDataAO(N, convert_fcn, unit)
    d = tsdata(convert_fcn(char(C{N}{3:end})), 10, t0);
    out = ao(d);
    out.setName(C{N}{2}, 'internal');
    out.setDescription(C{N}{1}, 'internal');
    out.setXunits('s',  'internal');
    out.setYunits(unit, 'internal');
  end
  
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