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.14 2008/02/24 13:43:40 hewitson 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')

 The following call returns a string that contains the routine CVS version:

 >> version = join(ao,'Version')

 The following call returns a string that contains the routine category:

 >> category = join(ao,'Category')

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

Generated on Mon 31-Mar-2008 13:54:54 by m2html © 2003