Home > classes > @ao > join.m

join

PURPOSE ^

JOIN multiple AOs into a single AO.

SYNOPSIS ^

function varargout = join(varargin)

DESCRIPTION ^

 JOIN multiple AOs into a single AO.

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

 DESCRIPTION: JOIN multiple AOs into a single AO.
              If any two AOs overlap, then the values from the first appear
              in the output AO.

 CALL:        b = join(a1,a2,pl)

 VERSION:     $Id: join.m,v 1.9 2007/10/26 12:26:53 ingo Exp $

 REMARK:      Input AOs should be of the same type; if not, only AOs of the
              type of the first input AO will be joined together to produce
              the output.

 The following call returns a parameter list object that contains the
 default parameter values:

 >> pl = join(ao, 'Params')

 HISTORY: 03-06-07 M Hewitson
             Creation

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

CROSS-REFERENCE INFORMATION ^

This function calls: This function is called by:

SUBFUNCTIONS ^

SOURCE CODE ^

0001 function varargout = join(varargin)
0002 % JOIN multiple AOs into a single AO.
0003 %
0004 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
0005 %
0006 % DESCRIPTION: JOIN multiple AOs into a single AO.
0007 %              If any two AOs overlap, then the values from the first appear
0008 %              in the output AO.
0009 %
0010 % CALL:        b = join(a1,a2,pl)
0011 %
0012 % VERSION:     $Id: join.m,v 1.9 2007/10/26 12:26:53 ingo Exp $
0013 %
0014 % REMARK:      Input AOs should be of the same type; if not, only AOs of the
0015 %              type of the first input AO will be joined together to produce
0016 %              the output.
0017 %
0018 % The following call returns a parameter list object that contains the
0019 % default parameter values:
0020 %
0021 % >> pl = join(ao, 'Params')
0022 %
0023 % HISTORY: 03-06-07 M Hewitson
0024 %             Creation
0025 %
0026 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
0027 
0028 ALGONAME = mfilename;
0029 VERSION  = '$Id: join.m,v 1.9 2007/10/26 12:26:53 ingo Exp $';
0030 
0031 %% Check if this is a call for parameters
0032 if nargin == 2
0033   if isa(varargin{1}, 'ao') && ischar(varargin{2})
0034     in = char(varargin{2});
0035     if strcmp(in, 'Params')
0036       varargout{1} = getDefaultPL();
0037       return
0038     elseif strcmp(in, 'Version')
0039       varargout{1} = VERSION;
0040       return
0041     end
0042   end
0043 end
0044 
0045 
0046 %% capture input variable names
0047 invars = {};
0048 as     = [];
0049 ps     = [];
0050 for j=1:nargin
0051   invars = [invars cellstr(inputname(j))];
0052   if isa(varargin{j}, 'ao')
0053     as = [as varargin{j}];
0054   end
0055   if isa(varargin{j}, 'plist')
0056     ps = [ps varargin{j}];
0057   end
0058 end
0059 
0060 % check plist
0061 if isempty(ps)
0062   pl = getDefaultPL();
0063 else
0064   pl = combine(ps, getDefaultPL);
0065 end
0066 
0067 %----------------------------------------------
0068 % Get data type from the first AO
0069 a     = as(1).data;
0070 dinfo = whos('a');
0071 dtype = dinfo.class;
0072 
0073 %----------------------------------------------
0074 % Go through each AO and collect the data of type 'dtype'
0075 
0076 histin = [];
0077 xo     = [];
0078 yo     = [];
0079 fs     = -1;
0080 dname  = [];
0081 xunits = '';
0082 yunits = '';
0083 
0084 % Compute time offset for tsdata objects
0085 % to avoid rounding errors later
0086 
0087 Toff = getToff(as);
0088 
0089 % loop over AOs
0090 for j=1:numel(as)
0091 
0092   a = as(j);
0093 
0094   % Only get the data type we want
0095   if isa(a.data, dtype)
0096 
0097     switch dtype
0098       case 'tsdata'
0099 
0100         % here we concatonate time-series
0101         [x,y] = get_xy_values(a.data);
0102         t0 = (a.data.t0.utc_epoch_milli - Toff)/1000;
0103 
0104         % make proper time vector
0105         x = x + t0;
0106 
0107         % only add samples past the end of existing
0108         if isempty(xo)
0109           yo = y;
0110           xo = x;
0111         else
0112           idxPost = find(x > max(xo));
0113           idxPre  = find(x < min(xo));
0114           xo = [x(idxPre);xo;x(idxPost)];
0115           yo = [y(idxPre);yo;y(idxPost)];
0116         end
0117 
0118         % store fs
0119         if fs>0 && a.data.fs ~= fs
0120           warning('!!! Data has different sample rates !!!');
0121         end
0122         fs = a.data.fs;
0123 
0124         % store xunits
0125         if ~strcmp(xunits, a.data.xunits) && ~isempty(xunits)
0126           warning('!!! Data has different X units !!!');
0127         end
0128         xunits = a.data.xunits;
0129 
0130         % store yunits
0131         if ~strcmp(yunits, a.data.yunits) && ~isempty(yunits)
0132           warning('!!! Data has different Y units !!!');
0133         end
0134         yunits = a.data.yunits;
0135 
0136       case 'fsdata'
0137         error('### I only work with time-series data at the moment.');
0138       case 'xydata'
0139         error('### I only work with time-series data at the moment.');
0140       case 'cdata'
0141         error('### I only work with time-series data at the moment.');
0142       otherwise
0143         error('### Unknown data type');
0144     end
0145 
0146     % Collect this input history
0147     histin = [histin a.hist];
0148 
0149     % Collect names, invars
0150     dname = [dname ',' a.data.name];
0151 
0152   else
0153     warning('!!! Ignoring AO input with data type %s', dtype);
0154   end
0155 
0156 end
0157 % Tidy up names
0158 dname = dname(2:end);
0159 
0160 %----------------------------------------------
0161 % Now sort output vectors
0162 [xos, idx] = sort(xo);
0163 yos = yo(idx);
0164 
0165 %% Build output data object
0166 switch dtype
0167   case 'tsdata'
0168 
0169     % get t0
0170     t0  = xos(1);
0171     xos = xos - t0;
0172     data = tsdata(xos, yos);
0173     data = set(data, 't0', (Toff+t0));
0174     data = set(data, 'name', dname);
0175     data = set(data, 'xunits', xunits);
0176     data = set(data, 'yunits', yunits);
0177 
0178   case 'fsdata'
0179 
0180   case 'xydata'
0181 
0182   case 'cdata'
0183 
0184 end
0185 
0186 %----------------------------------------------
0187 % Build output AO
0188 
0189 h = history(ALGONAME, VERSION, pl, histin);
0190 h = set(h, 'invars', invars);
0191 a = ao(data, h);
0192 
0193 %----------------------------------------------
0194 % set output
0195 
0196 varargout{1} = a;
0197 
0198 
0199 %--------------------------------------------------------------------------
0200 % Get Offset of this set of time-vectors
0201 function Toff = getToff(as)
0202 
0203 Toff = 1e20;
0204 for j=1:length(as)
0205   if isa(as(j).data, 'tsdata')
0206     t0 = as(j).data.t0.utc_epoch_milli;
0207     if t0 < Toff
0208       Toff = t0;
0209     end
0210   end
0211 end
0212 
0213 %--------------------------------------------------------------------------
0214 % Get default params
0215 function plo = getDefaultPL()
0216 
0217 plo = plist();
0218 
0219 
0220 
0221 
0222 
0223 % END

Generated on Fri 02-Nov-2007 19:39:27 by m2html © 2003