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.11 2007/11/26 14:48:13 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.11 2007/11/26 14:48:13 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.11 2007/11/26 14:48:13 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 % Collect input ao's, plist's and ao variable names
0047 in_names = {};
0048 for ii = 1:nargin
0049   in_names{end+1} = inputname(ii);
0050 end
0051 
0052 [as, ps, invars] = collect_inputs(varargin, in_names);
0053 
0054 % check plist
0055 if isempty(ps)
0056   pl = getDefaultPL();
0057 else
0058   pl = combine(ps, getDefaultPL);
0059 end
0060 
0061 %----------------------------------------------
0062 % Get data type from the first AO
0063 a     = as(1).data;
0064 dinfo = whos('a');
0065 dtype = dinfo.class;
0066 
0067 %----------------------------------------------
0068 % Go through each AO and collect the data of type 'dtype'
0069 
0070 histin = [];
0071 xo     = [];
0072 yo     = [];
0073 fs     = -1;
0074 dname  = [];
0075 xunits = '';
0076 yunits = '';
0077 
0078 % Compute time offset for tsdata objects
0079 % to avoid rounding errors later
0080 
0081 Toff = getToff(as);
0082 
0083 % loop over AOs
0084 for j=1:numel(as)
0085 
0086   a = as(j);
0087 
0088   % Only get the data type we want
0089   if isa(a.data, dtype)
0090 
0091     switch dtype
0092       case 'tsdata'
0093 
0094         % here we concatonate time-series
0095         [x,y] = get_xy_values(a.data);
0096         t0 = (a.data.t0.utc_epoch_milli - Toff)/1000;
0097 
0098         % make proper time vector
0099         x = x + t0;
0100 
0101         % only add samples past the end of existing
0102         if isempty(xo)
0103           yo = y;
0104           xo = x;
0105         else
0106           idxPost = find(x > max(xo));
0107           idxPre  = find(x < min(xo));
0108           xo = [x(idxPre);xo;x(idxPost)];
0109           yo = [y(idxPre);yo;y(idxPost)];
0110         end
0111 
0112         % store fs
0113         if fs>0 && a.data.fs ~= fs
0114           warning('!!! Data has different sample rates !!!');
0115         end
0116         fs = a.data.fs;
0117 
0118         % store xunits
0119         if ~strcmp(xunits, a.data.xunits) && ~isempty(xunits)
0120           warning('!!! Data has different X units !!!');
0121         end
0122         xunits = a.data.xunits;
0123 
0124         % store yunits
0125         if ~strcmp(yunits, a.data.yunits) && ~isempty(yunits)
0126           warning('!!! Data has different Y units !!!');
0127         end
0128         yunits = a.data.yunits;
0129 
0130       case 'fsdata'
0131         error('### I only work with time-series data at the moment.');
0132       case 'xydata'
0133         error('### I only work with time-series data at the moment.');
0134       case 'cdata'
0135         error('### I only work with time-series data at the moment.');
0136       otherwise
0137         error('### Unknown data type');
0138     end
0139 
0140     % Collect this input history
0141     histin = [histin a.hist];
0142 
0143     % Collect names, invars
0144     dname = [dname ',' a.data.name];
0145 
0146   else
0147     warning('!!! Ignoring AO input with data type %s', dtype);
0148   end
0149 
0150 end
0151 % Tidy up names
0152 dname = dname(2:end);
0153 
0154 %----------------------------------------------
0155 % Now sort output vectors
0156 [xos, idx] = sort(xo);
0157 yos = yo(idx);
0158 
0159 %% Build output data object
0160 switch dtype
0161   case 'tsdata'
0162 
0163     % get t0
0164     t0  = xos(1);
0165     xos = xos - t0;
0166     data = tsdata(xos, yos);
0167     data = set(data, 't0', (Toff+t0));
0168     data = set(data, 'name', dname);
0169     data = set(data, 'xunits', xunits);
0170     data = set(data, 'yunits', yunits);
0171 
0172   case 'fsdata'
0173 
0174   case 'xydata'
0175 
0176   case 'cdata'
0177 
0178 end
0179 
0180 %----------------------------------------------
0181 % Build output AO
0182 
0183 h = history(ALGONAME, VERSION, pl, histin);
0184 h = set(h, 'invars', invars);
0185 a = ao(data, h);
0186 
0187 %----------------------------------------------
0188 % set output
0189 
0190 varargout{1} = a;
0191 
0192 
0193 %--------------------------------------------------------------------------
0194 % Get Offset of this set of time-vectors
0195 function Toff = getToff(as)
0196 
0197 Toff = 1e20;
0198 for j=1:length(as)
0199   if isa(as(j).data, 'tsdata')
0200     t0 = as(j).data.t0.utc_epoch_milli;
0201     if t0 < Toff
0202       Toff = t0;
0203     end
0204   end
0205 end
0206 
0207 %--------------------------------------------------------------------------
0208 % Get default params
0209 function plo = getDefaultPL()
0210 
0211 plo = plist();
0212 
0213 
0214 
0215 
0216 
0217 % END

Generated on Tue 22-Jan-2008 10:39:13 by m2html © 2003