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

Generated on Mon 03-Sep-2007 12:12:34 by m2html © 2003